Example #1
0
        public static TileGrid BuildFromXML(string filename)
        {
            XmlDocument xmlDoc = new XmlDocument();
            XmlReader   reader = new XmlTextReader(filename);

            xmlDoc.Load(reader);
            XmlElement root      = xmlDoc.DocumentElement;
            int        Dimension = (int)Math.Sqrt(root.ChildNodes.Count);
            TileGrid   tileGrid  = new TileGrid(Dimension);
            int        TileSize  = (int)Math.Sqrt(root.ChildNodes[0].ChildNodes.Count);

            foreach (XmlElement tile in root.ChildNodes)
            {
                FlowTile flowTile = new FlowTile(TileSize);
                foreach (XmlElement velocity in tile)
                {
                    double x  = double.Parse(velocity.GetAttribute("relX"));
                    double y  = double.Parse(velocity.GetAttribute("relY"));
                    double vx = double.Parse(velocity.GetAttribute("vx"));
                    double vy = double.Parse(velocity.GetAttribute("vy"));
                    flowTile.SetVelocity(x, y, vx, vy);
                }
                tileGrid.AddTile(int.Parse(tile.GetAttribute("row")), int.Parse(tile.GetAttribute("col")), flowTile);
            }
            return(tileGrid);
        }
 public void Execute(int index)
 {
     //make the field
     //this could definitely be done in parallel
     int2 mapSize   = field.Dimensions;
     int2 mPos      = new int2(0, index);
     int  dirLength = directions.Length;
     //     for (mPos.y = 0; mPos.y < mapSize.y; mPos.y++)
     {
         int mapIndex = index * field.Dimensions.x;
         for (mPos.x = 0; mPos.x < mapSize.x; mPos.x++, mapIndex++)
         {
             byte mask      = 0;
             uint highValue = uint.MaxValue;
             //uint currentCost = state[mPos];
             for (int i = 0; i < dirLength; i++)
             {
                 int2 searchPos = mPos + directions[i];
                 if (math.all(searchPos > 0 & searchPos < mapSize))
                 {
                     uint searchVal = state[searchPos];
                     if (searchVal < highValue)
                     {
                         mask      = (byte)i;
                         highValue = searchVal;
                     }
                 }
             }
             FlowTile tile = field[mapIndex];
             tile.direction  = mask;
             field[mapIndex] = tile;
         }
     }
 }
Example #3
0
        public TileGrid BuildRandomTileGrid()
        {
            //Clear the tilegrid.
            tileGrid = new TileGrid(gridDimension);

            Random RNG = new Random();

            for (int row = 0; row < gridDimension; row++)
            {
                for (int col = 0; col < gridDimension; col++)
                {
                    LPSolve.BuildInitialModel(minXFlux, maxXFlux, minYFlux, maxYFlux, tileGrid, boundaryConditions);
                    List <FlowTile> validTiles = ValidTiles(row, col);
                    FlowTile        newTile    = validTiles[RNG.Next(0, validTiles.Count - 1)];

                    /*
                     * Console.WriteLine("(" + row + ", " + col + ")");
                     * Console.WriteLine("Top: " + newTile.Flux.topEdge);
                     * Console.WriteLine("Right: " + newTile.Flux.rightEdge);
                     * Console.WriteLine("Bottom: " + newTile.Flux.bottomEdge);
                     * Console.WriteLine("Left: " + newTile.Flux.leftEdge);
                     */
                    tileGrid.AddTile(row, col, newTile);

                    LPSolve.FreeModel();
                }
            }

            return(tileGrid);
        }
Example #4
0
        static void Main(string[] args)
        {
            float       baseFlux    = 2f;
            GridBuilder gridBuilder = new GridBuilder(-4, 4, -4, 4, baseFlux, 4, 10);

            while (true)
            {
                int[] tile = gridBuilder.AskUserForObstacle();
                if (tile == null)
                {
                    break;
                }
                gridBuilder.AddObstacle(tile[0], tile[1]);
            }

            for (int row = 0; row < gridBuilder.gridDimension; row++)
            {
                for (int col = 0; col < gridBuilder.gridDimension; col++)
                {
                    FlowTile tile = gridBuilder.AskUserForTile(row, col);
                    gridBuilder.AddTile(row, col, tile);
                }
            }

            TileGrid tileGrid = gridBuilder.GetTileGrid();

            tileGrid.WriteToXML("/home/felix/FTGridBuilding/Tilings/LeoGrid2.xml");
            //tileGrid.WriteToXML(@"C:\Users\Felix Liu\source\repos\FTGridbuilding\Tilings\Curve.xml");
        }
            public void Execute()
            {
                int2 mapSize   = field.Dimensions;
                int  dirLength = directions.Length;

                state[dest] = 1;
                search.Enqueue(new SearchNode(dest, 1));
                //create the costs all valid tile will be >=1
                //this probably can't be parallel
                while (search.Count > 0)
                {
                    SearchNode node = search.Dequeue();
                    for (int i = 0; i < dirLength; i++)
                    {
                        int2 searchPos = node.root + directions[i];
                        if (math.all(searchPos >= 0 & searchPos < mapSize))
                        {
                            int  index       = state.GetIndexFromPos(searchPos);
                            uint currentCost = state[index];
                            //if a cheaper parent is already written skip
                            if (currentCost <= node.cost)
                            {
                                //keep in map bounds

                                FlowTile tile     = field[index];
                                uint     tileCost = node.cost + 1;
                                //a blocked tile don't search it
                                if (tile.state != 0)
                                {
                                    state[index] = uint.MaxValue;
                                    continue;
                                }
                                //not searched or cheaper add to search
                                if (state[index] == 0 || state[index] > tileCost)
                                {
                                    state[index] = tileCost;
                                    search.Enqueue(new SearchNode(searchPos, tileCost));
                                }
                            }
                        }
                    }
                }
            }
        public void ConvertToMesh(Mesh mesh, float scale)
        {
            Vector3[] positions = new Vector3[field.Width * field.Height];
            Vector3[] normals   = new Vector3[field.Width * field.Height];
            for (int y = 0; y < field.Height; y++)
            {
                for (int x = 0; x < field.Width; x++)
                {
                    positions[y * field.Width + x] = new Vector3(x, 0, y) * scale;
                }
            }

            List <int> indices = new List <int>();

            for (int y = 0; y < field.Height; y++)
            {
                for (int x = 0; x < field.Width; x++)
                {
                    FlowTile tile = field[x, y];
                    if (tile.state == 0)
                    {
                        indices.Add(y * field.Width + x);
                        int2 pos = new int2(x, y) + directions[tile.direction];
                        if (math.all(pos >= 0 & pos < field.Dimensions))
                        {
                            indices.Add(pos.y * field.Width + pos.x);
                        }
                    }
                }
            }
            mesh.Clear();
            mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
            mesh.SetVertices(positions);
            mesh.SetNormals(normals);
            mesh.SetIndices(indices, MeshTopology.Lines, 0);
        }
Example #7
0
 public void AddTile(int rowIndex, int colIndex, FlowTile flowTile)
 {
     tileGrid.AddTile(rowIndex, colIndex, flowTile);
 }