Esempio n. 1
0
        private void CreatePatterns <T>(ValuesManager <T> valuesManager, IFindNeighboutStrategy startegy, bool equalWeights)
        {
            var patternFinderResults = PatternFinder.GetPatternDataFromGrid(valuesManager, _patternSize, equalWeights);

            patternDataIndexDictionary = patternFinderResults.PatternIndexDictionary;
            GetPatternNeighbours(patternFinderResults, startegy);
        }
Esempio n. 2
0
        public void ProcessGrid <T>(ValuesManager <T> valuesManager, bool equalWeights, string strategyName = null)
        {
            NeighbourStrategyFactory strategyFactory = new NeighbourStrategyFactory();

            strategy = strategyFactory.CreateInstance(strategyName == null? _patternSize + "" : strategyName);
            CreatePatterns(valuesManager, strategy, equalWeights);
        }
        public void CreateNewTileMap()
        {
            reader         = new InputReader(inputTilemap);
            patternManager = new PatternManager(this.patternSize);
            var gridOfValues = reader.ReadInputToGrid();

            if (gridOfValues != null)
            {
                valuesManager = new ValuesManager <TileBase>(gridOfValues);
                patternManager.ProcessGrid(valuesManager, this.equalWeights, this.strategyName);
                core = new WFCCore(this.outputWidth, this.outputHeight, patternManager, this.maxIterations);
                int[][] wfcOutput = core.CreateOutputGrid();
                tileOutput = new TileMapOutput(valuesManager, this.outputImage);
                tileOutput.CreateOutput(patternManager, wfcOutput, outputWidth, outputHeight);
            }
            else
            {
                throw new System.Exception("Tilemap must contain only 1 square of non-empty tiles. See the example prefab.");
            }
        }
 public TileMapOutput(ValuesManager <TileBase> valueManager, Tilemap outputImage)
 {
     this.outputImage  = outputImage;
     this.valueManager = valueManager;
 }
        public static PatternDataResults GetPatternDataFromGrid <T>(ValuesManager <T> valuesManager, int patternSize, bool equalWeights)
        {
            Dictionary <string, PatternData> patternHashcodeDictionary = new Dictionary <string, PatternData>();
            Dictionary <int, PatternData>    patternIndexDictionary    = new Dictionary <int, PatternData>();
            Vector2 sizeOfGrid = valuesManager.GetGridSize();
            int     patternGridSizeX = 0;
            int     patternGridSizeY = 0;
            int     rowMin = -1, colMin = -1, rowMax = -1, colMax = -1;

            if (patternSize < 3)
            {
                patternGridSizeX = (int)sizeOfGrid.x + 3 - patternSize;
                patternGridSizeY = (int)sizeOfGrid.y + 3 - patternSize;
                rowMax           = patternGridSizeY - 1;
                colMax           = patternGridSizeX - 1;
            }
            else
            {
                patternGridSizeX = (int)sizeOfGrid.x + patternSize - 1;
                patternGridSizeY = (int)sizeOfGrid.y + patternSize - 1;
                rowMin           = 1 - patternSize;
                colMin           = 1 - patternSize;
                rowMax           = (int)sizeOfGrid.y;
                colMax           = (int)sizeOfGrid.x;
            }

            int[][] patternIndicesGrid = MyCollectionExtension.CreateJaggedArray <int[][]>(patternGridSizeY, patternGridSizeX);
            int     totalFrequency     = 0;

            //we loop with offset -1 / +1 to get patterns. At the same time we have to account for patten size.
            //If pattern is of size 2 we will be reaching x+1 and y+1 to check all 4 values. Need visual aid.

            int patternIndex = 0;

            for (int row = rowMin; row < rowMax; row++)
            {
                for (int col = colMin; col < colMax; col++)
                {
                    int[][] gridValues = valuesManager.GetPatternValuesFromGridAt(col, row, patternSize);
                    string  hashValue  = HashCodeCalculator.CalculateHashCode(gridValues);

                    if (patternHashcodeDictionary.ContainsKey(hashValue) == false)
                    {
                        Pattern pattern = new Pattern(gridValues, hashValue, patternIndex);
                        patternIndex++;
                        AddNewPattern(patternHashcodeDictionary, patternIndexDictionary, hashValue, pattern);
                    }
                    else
                    {
                        if (equalWeights == false)
                        {
                            patternIndexDictionary[patternHashcodeDictionary[hashValue].Pattern.Index].AddToFrequency();
                        }
                    }
                    //if (patternSize > colMin || row >= rowMin && row < rowMax-1 && col >= colMin && col < colMax-1)
                    //{

                    //    totalFrequency++;

                    //}
                    totalFrequency++;
                    if (patternSize < 3)
                    {
                        patternIndicesGrid[row + 1][col + 1] = patternHashcodeDictionary[hashValue].Pattern.Index;
                    }
                    else
                    {
                        patternIndicesGrid[row + patternSize - 1][col + patternSize - 1] = patternHashcodeDictionary[hashValue].Pattern.Index;
                    }
                }
            }

            CalculateRelativeFrequency(patternIndexDictionary, totalFrequency);

            return(new PatternDataResults(patternIndicesGrid, patternIndexDictionary));
        }