Exemplo n.º 1
0
 /// <summary>
 ///     Add every element from the other model, with weights multiplied by the rate
 /// </summary>
 /// <param name="model">the model whose elements should be added</param>
 /// <param name="rate">the rate to weight model's elements by, with 1.0 the default</param>
 public void AddAll(SuccessorTileModel model, double rate = 1.0)
 {
     foreach (KeyValuePair<Color, double> kvp in model.counts)
     {
         this.AddWeight(kvp.Key, kvp.Value * rate);
     }
 }
Exemplo n.º 2
0
        public Model(int maxN)
        {
            this.maxN = maxN;
            this.nullaryModel = new SuccessorTileModel();
            this.naryModels = new KDMap<SuccessorTileModel>[maxN];

            for (int i = 1; i <= maxN; i++)
            {
                this.naryModels[i - 1] = new KDMap<SuccessorTileModel>(3 * i);
            }
        }
Exemplo n.º 3
0
        public Bitmap GenImage(int wTiles, int hTiles)
        {
            int w = wTiles, h = hTiles;
            Bitmap img = new Bitmap(w, h);
            Random rng = new Random();

            Digraph<TileGenerationRecord> depGraph = MakeDependencyGraph(w, h);

            while (true)
            {
                IEnumerable<Digraph<TileGenerationRecord>.Vertex> tilesToGenerate =
                    depGraph.Vertices
                        .Where(v => (v.Label.IsGenerated == false))
                        .OrderBy(v => v.Predecessors.Count(vv => !vv.Label.IsGenerated));

                if (tilesToGenerate.Count() == 0)
                {
                    break;
                }

                Digraph<TileGenerationRecord>.Vertex nextTileToGenerate = tilesToGenerate.First();

                Console.WriteLine("{0}, {1}", nextTileToGenerate.Label.Point.X, nextTileToGenerate.Label.Point.Y);

                // actually generate the tile.
                SuccessorTileModel mergedModel = new SuccessorTileModel();

                // grab a list of all of the model positions represented by all the predecessors to this tile
                List<byte[]> positions =
                    GetPredecessorModelPositions(img, 1, nextTileToGenerate, new byte[] { }, this.maxN, 1, true, true);

                // look 'em all up
                foreach (byte[] pos in positions)
                {
                    if (pos.Length == 0)
                    {
                        mergedModel.AddAll(this.nullaryModel);
                    }
                    else
                    {
                        int n = pos.Length / 3;
                        SuccessorTileModel[] models = this.naryModels[n - 1].RangeSearch(pos, 2);

                        foreach (SuccessorTileModel model in models)
                        {
                            mergedModel.AddAll(model);
                        }
                    }
                }

                // choose randomly from that model
                Color c = mergedModel.ChooseRandomly(rng);

                if (c != null)
                {
                    img.SetPixel(nextTileToGenerate.Label.Point.X, nextTileToGenerate.Label.Point.Y, c);
                }

                nextTileToGenerate.Label.IsGenerated = true;
            }

            return img;
        }
Exemplo n.º 4
0
        public void LoadImage(Bitmap image)
        {
            // build dep graph for loading image
            Digraph<TileGenerationRecord> depGraph = MakeDependencyGraph(image.Width, image.Height);
            int nPixelsConsumed = 0;

            foreach (Digraph<TileGenerationRecord>.Vertex v in depGraph.Vertices)
            {
                List<byte[]> positions =
                    GetPredecessorModelPositions(image, 1, v, new byte[] { }, this.maxN, 1, false, false);
                Color thisPixel = image.GetPixel(v.Label.Point.X, v.Label.Point.Y);

                foreach (byte[] pos in positions)
                {
                    if (pos.Length == 0)
                    {
                        this.nullaryModel.Add(thisPixel);
                    }
                    else
                    {
                        int n = pos.Length / 3;
                        SuccessorTileModel model = this.naryModels[n - 1].Get(pos);

                        if (model == null)
                        {
                            model = new SuccessorTileModel();
                            model.Add(thisPixel);
                            this.naryModels[n - 1].Insert(pos, model);
                        }
                        else
                        {
                            model.Add(thisPixel);
                        }
                    }
                }

                nPixelsConsumed++;

                if (nPixelsConsumed % 10000 == 0)
                {
                    Console.Write(".");
                    Console.Out.Flush();
                }
            }

            Console.WriteLine();
        }