Beispiel #1
0
            public void triangulate_square(map_square i_squares)
            {
                switch (i_squares.configuration)
                {
                case 0:
                    break;

                // 1 points:
                case 1:
                    MeshFromPoints(i_squares.centrebottom, i_squares.bottomleft, i_squares.centreleft);
                    break;

                case 2:
                    MeshFromPoints(i_squares.centreright, i_squares.bottomright, i_squares.centrebottom);
                    break;

                case 4:
                    MeshFromPoints(i_squares.centretop, i_squares.topright, i_squares.centreright);
                    break;

                case 8:
                    MeshFromPoints(i_squares.topleft, i_squares.centretop, i_squares.centreleft);
                    break;

                // 2 points:
                case 3:
                    MeshFromPoints(i_squares.centreright, i_squares.bottomright, i_squares.bottomleft, i_squares.centreleft);
                    break;

                case 6:
                    MeshFromPoints(i_squares.centretop, i_squares.topright, i_squares.bottomright, i_squares.centrebottom);
                    break;

                case 9:
                    MeshFromPoints(i_squares.topleft, i_squares.centretop, i_squares.centrebottom, i_squares.bottomleft);
                    break;

                case 12:
                    MeshFromPoints(i_squares.topleft, i_squares.topright, i_squares.centreright, i_squares.centreleft);
                    break;

                case 5:
                    MeshFromPoints(i_squares.centretop, i_squares.topright, i_squares.centreright, i_squares.centrebottom, i_squares.bottomleft, i_squares.centreleft);
                    break;

                case 10:
                    MeshFromPoints(i_squares.topleft, i_squares.centretop, i_squares.centreright, i_squares.bottomright, i_squares.centrebottom, i_squares.centreleft);
                    break;

                // 3 point:
                case 7:
                    MeshFromPoints(i_squares.centretop, i_squares.topright, i_squares.bottomright, i_squares.bottomleft, i_squares.centreleft);
                    break;

                case 11:
                    MeshFromPoints(i_squares.topleft, i_squares.centretop, i_squares.centreright, i_squares.bottomright, i_squares.bottomleft);
                    break;

                case 13:
                    MeshFromPoints(i_squares.topleft, i_squares.topright, i_squares.centreright, i_squares.centrebottom, i_squares.bottomleft);
                    break;

                case 14:
                    MeshFromPoints(i_squares.topleft, i_squares.topright, i_squares.bottomright, i_squares.centrebottom, i_squares.centreleft);
                    break;

                // 4 point:
                case 15:
                    MeshFromPoints(i_squares.topleft, i_squares.topright, i_squares.bottomright, i_squares.bottomleft);
                    break;
                }
            }
Beispiel #2
0
            public square_grid(int[,] map, int square_size)
            {
                int nodecountX = map.GetLength(0);
                int nodecountY = map.GetLength(1);

                float mapwidth = nodecountX * square_size;
                float mapheight = nodecountY * square_size;
                int   i, j;


                control_node[,] the_controlnodes = new control_node[nodecountX, nodecountY];
                // set the control nodes
                for (int x = 0; x < nodecountX; x++)
                {
                    for (int y = 0; y < nodecountY; y++)
                    {
                        double pos_x = -(mapwidth / 2) + (x * square_size) + (square_size / 2);
                        double pos_y = -(mapheight / 2) + (y * square_size) + (square_size / 2);              // -mapheight/2 is orgin

                        the_controlnodes[x, y] = new control_node(pos_x, pos_y, map[x, y] == 1, square_size); // active only if solid
                    }
                }

                // Re-initialize the list
                all_nodes    = new List <map_node>();
                all_triangle = new List <map_triangle>();
                border_nodes = new List <map_node>();
                border_edges = new List <map_edge>();

                the_squares = new map_square[nodecountX - 1, nodecountY - 1];
                // set the squares
                for (int x = 0; x < nodecountX - 1; x++)
                {
                    for (int y = 0; y < nodecountY - 1; y++)
                    {
                        the_squares[x, y] = new map_square(the_controlnodes[x, y + 1], the_controlnodes[x + 1, y + 1], the_controlnodes[x + 1, y], the_controlnodes[x, y]);
                        triangulate_square(the_squares[x, y]);
                    }
                }


                //List<map_node> temp_border_nodes = new List<map_node>();
                List <map_edge> temp_border_edges = new List <map_edge>();

                // Set the border edges
                for (i = 0; i < all_triangle.Count; i++)
                {
                    bool edge1_found = false;
                    bool edge2_found = false;
                    bool edge3_found = false;

                    for (j = 0; j < all_triangle.Count; j++)
                    {
                        if (i == j)
                        {
                            continue;
                        }

                        // Edge 1
                        if ((all_triangle[i].index_0 == all_triangle[j].index_0 &&
                             all_triangle[i].index_1 == all_triangle[j].index_1) ||
                            (all_triangle[i].index_0 == all_triangle[j].index_1 &&
                             all_triangle[i].index_1 == all_triangle[j].index_2) ||
                            (all_triangle[i].index_0 == all_triangle[j].index_2 &&
                             all_triangle[i].index_1 == all_triangle[j].index_0) ||
                            (all_triangle[i].index_0 == all_triangle[j].index_1 &&
                             all_triangle[i].index_1 == all_triangle[j].index_0) ||
                            (all_triangle[i].index_0 == all_triangle[j].index_2 &&
                             all_triangle[i].index_1 == all_triangle[j].index_1) ||
                            (all_triangle[i].index_0 == all_triangle[j].index_0 &&
                             all_triangle[i].index_1 == all_triangle[j].index_2))
                        {
                            edge1_found = true;
                        }

                        // Edge 2
                        if ((all_triangle[i].index_1 == all_triangle[j].index_0 &&
                             all_triangle[i].index_2 == all_triangle[j].index_1) ||
                            (all_triangle[i].index_1 == all_triangle[j].index_1 &&
                             all_triangle[i].index_2 == all_triangle[j].index_2) ||
                            (all_triangle[i].index_1 == all_triangle[j].index_2 &&
                             all_triangle[i].index_2 == all_triangle[j].index_0) ||
                            (all_triangle[i].index_1 == all_triangle[j].index_1 &&
                             all_triangle[i].index_2 == all_triangle[j].index_0) ||
                            (all_triangle[i].index_1 == all_triangle[j].index_2 &&
                             all_triangle[i].index_2 == all_triangle[j].index_1) ||
                            (all_triangle[i].index_1 == all_triangle[j].index_0 &&
                             all_triangle[i].index_2 == all_triangle[j].index_2))
                        {
                            edge2_found = true;
                        }

                        // Edge 3
                        if ((all_triangle[i].index_2 == all_triangle[j].index_0 &&
                             all_triangle[i].index_0 == all_triangle[j].index_1) ||
                            (all_triangle[i].index_2 == all_triangle[j].index_1 &&
                             all_triangle[i].index_0 == all_triangle[j].index_2) ||
                            (all_triangle[i].index_2 == all_triangle[j].index_2 &&
                             all_triangle[i].index_0 == all_triangle[j].index_0) ||
                            (all_triangle[i].index_2 == all_triangle[j].index_1 &&
                             all_triangle[i].index_0 == all_triangle[j].index_0) ||
                            (all_triangle[i].index_2 == all_triangle[j].index_2 &&
                             all_triangle[i].index_0 == all_triangle[j].index_1) ||
                            (all_triangle[i].index_2 == all_triangle[j].index_0 &&
                             all_triangle[i].index_0 == all_triangle[j].index_2))
                        {
                            edge3_found = true;
                        }

                        if (edge1_found == true && edge2_found == true && edge3_found == true)
                        {
                            break; // exit the loop if all the edges are connected to some other triangle
                        }
                    }

                    if (edge1_found == false)
                    {
                        temp_border_edges.Add(new map_edge(all_triangle[i].index_0, all_triangle[i].index_1));
                    }

                    if (edge2_found == false)
                    {
                        temp_border_edges.Add(new map_edge(all_triangle[i].index_1, all_triangle[i].index_2));
                    }

                    if (edge3_found == false)
                    {
                        temp_border_edges.Add(new map_edge(all_triangle[i].index_2, all_triangle[i].index_0));
                    }
                }

                // Remove the colinear nodes
                //List<map_edge> temp_border_edges = new List<map_edge>();
                //foreach (map_edge ed in temp_border_edges)
                //{
                //    border_edges.Add(ed);
                //}



                //// _______________________________________________________________________________________________________________
                //// _______________________________________________________________________________________________________________
                //// _______________________________________________________________________________________________________________
                List <map_edge> temp_border_edges_0 = new List <map_edge>(); // border edge sub list 1

                temp_border_edges_0.Add(temp_border_edges[0]);               // add the first edges
                while (temp_border_edges.Count > 0)
                {
                    // step 1 is to find a sublist of edges with same slope
                    List <map_edge> temp_border_edges_1 = new List <map_edge>(); // border edge sub list 2
                    temp_border_edges_1.AddRange(temp_border_edges.FindAll(obj => compare_slope(temp_border_edges_0[0], obj) == true));
                    temp_border_edges_1.Remove(temp_border_edges_0[0]);          //Remove the only duplicate


                    // step 2 is to find the connected edges
                    j = 0;
                    while (temp_border_edges_1.Count > 0) // Exit if there is no edges with the same slope
                    {
                        List <map_edge> temp_border_edges_2 = new List <map_edge>();

                        for (i = j; i < temp_border_edges_0.Count; i++)
                        {
                            // find the connectivity to first list
                            temp_border_edges_2.AddRange(temp_border_edges_1.FindAll(obj => obj.is_connected_to(temp_border_edges_0[i]) == true));
                        }

                        // j is the variable to avoid checking the same edges again and again
                        j = temp_border_edges_0.Count;

                        if (temp_border_edges_2.Count == 0) // Exit if no connected edge is found
                        {
                            break;
                        }

                        // add the new found edges to the list
                        temp_border_edges_0.AddRange(temp_border_edges_2);

                        // Remove from the first list  (where the slope is identical)
                        foreach (map_edge ed in temp_border_edges_2)
                        {
                            temp_border_edges_1.Remove(ed);
                        }
                    }

                    // create a temporary border node list
                    List <map_node> temp_border_nodes = new List <map_node>();
                    foreach (map_edge ed in temp_border_edges_0)
                    {
                        temp_border_edges.Remove(ed); // Remove from the master edge list
                                                      // Also add the nodes to a new list
                        if (temp_border_nodes.Exists(obj => obj.Equals(all_nodes[ed.index_0])) == false)
                        {
                            temp_border_nodes.Add(all_nodes[ed.index_0]);
                        }
                        if (temp_border_nodes.Exists(obj => obj.Equals(all_nodes[ed.index_1])) == false)
                        {
                            temp_border_nodes.Add(all_nodes[ed.index_1]);
                        }
                    }

                    // sort the border nodes
                    temp_border_nodes = temp_border_nodes.OrderBy(obj => obj.x).ThenBy(obj => obj.y).ToList(); // first by x and then by y

                    // now the first and last nodes are the final result (intermediate nodes are not required)
                    if (border_nodes.Exists(obj => obj.Equals(temp_border_nodes[0])) == false)
                    {
                        border_nodes.Add(temp_border_nodes[0]);
                    }
                    if (border_nodes.Exists(obj => obj.Equals(temp_border_nodes[temp_border_nodes.Count - 1])) == false)
                    {
                        border_nodes.Add(temp_border_nodes[temp_border_nodes.Count - 1]);
                    }

                    border_edges.Add(new map_edge(temp_border_nodes[0].vertex_index, temp_border_nodes[temp_border_nodes.Count - 1].vertex_index));

                    if (temp_border_edges.Count > 0)
                    {
                        // renew the border edge sub list 1
                        temp_border_edges_0 = new List <map_edge>();   // border edge sub list 1
                        temp_border_edges_0.Add(temp_border_edges[0]); // add the first edges
                    }
                }
                //// _______________________________________________________________________________________________________________
                //// _______________________________________________________________________________________________________________
                //// _______________________________________________________________________________________________________________
            }