public TPPLPoly(TPPLPoly src) { Clear(); Hole = src.Hole; if (src.Points != null) { Points = new List <TPPLPoint>(src.Points.Count); for (int i = 0; i < Points.Count; i++) { Points[i] = new TPPLPoint(src.Points[i]); } } }
public int Triangulate_EC(TPPLPoly poly, IList <TPPLPoly> triangles) { int numvertices; List <PartitionVertex> vertices = null; PartitionVertex ear = null; TPPLPoly triangle = new TPPLPoly(); int i, j; bool earfound; if (poly.Count < 3) { return(0); } if (poly.Count == 3) { triangles.Add(poly); return(1); } numvertices = poly.Count; vertices = new List <PartitionVertex>(numvertices); for (i = 0; i < numvertices; i++) { vertices.Add(new PartitionVertex()); } for (i = 0; i < numvertices; i++) { vertices[i].isActive = true; vertices[i].p = poly[i]; if (i == (numvertices - 1)) { vertices[i].next = vertices[0]; } else { vertices[i].next = vertices[i + 1]; } if (i == 0) { vertices[i].previous = vertices[numvertices - 1]; } else { vertices[i].previous = vertices[i - 1]; } } for (i = 0; i < numvertices; i++) { UpdateVertex(vertices[i], vertices); } for (i = 0; i < numvertices - 3; i++) { earfound = false; //find the most extruded ear for (j = 0; j < numvertices; j++) { if (!vertices[j].isActive) { continue; } if (!vertices[j].isEar) { continue; } if (!earfound) { earfound = true; ear = vertices[j]; } else { if (vertices[j].angle > ear.angle) { ear = vertices[j]; } } } if (!earfound) { vertices.Clear(); return(0); } triangle = new TPPLPoly(ear.previous.p, ear.p, ear.next.p); triangles.Add(triangle); ear.isActive = false; ear.previous.next = ear.next; ear.next.previous = ear.previous; if (i == numvertices - 4) { break; } UpdateVertex(ear.previous, vertices); UpdateVertex(ear.next, vertices); } for (i = 0; i < numvertices; i++) { if (vertices[i].isActive) { triangle = new TPPLPoly(vertices[i].previous.p, vertices[i].p, vertices[i].next.p); triangles.Add(triangle); break; } } vertices.Clear(); return(1); }
public int ConvexPartition_HM(TPPLPoly poly, IList <TPPLPoly> parts) { List <TPPLPoly> triangles = new List <TPPLPoly>(); // List<TPPLPoly> iter1 = new List<TPPLPoly>(); // List<TPPLPoly> iter2 = new List<TPPLPoly>(); TPPLPoly poly1 = null; TPPLPoly poly2 = null; TPPLPoly newpoly = new TPPLPoly(); TPPLPoint d1, d2, p1, p2, p3; int i11 = 0, i12 = 0, i21 = 0, i22 = 0, i13 = 0, i23 = 0, j, k; bool isdiagonal; long numreflex; //check if the poly is already convex numreflex = 0; for (i11 = 0; i11 < poly.Count; i11++) { if (i11 == 0) { i12 = poly.Count - 1; } else { i12 = i11 - 1; } if (i11 == (poly.Count - 1)) { i13 = 0; } else { i13 = i11 + 1; } if (IsReflex(poly[i12], poly[i11], poly[i13])) { numreflex = 1; break; } } if (numreflex == 0) { parts.Add(poly); return(1); } if (Triangulate_EC(poly, triangles) == 0) { return(0); } int iter1 = 0; int iter2 = 0; for (iter1 = 0; iter1 != triangles.Count; iter1++) { // for (iter1 = 0; iter1 != triangles.end(); iter1++) { poly1 = triangles[iter1]; // poly1 = &(*iter1); for (i11 = 0; i11 < poly1.Count; i11++) { d1 = poly1[i11]; i12 = (i11 + 1) % poly1.Count; d2 = poly1[i12]; isdiagonal = false; for (iter2 = iter1; iter2 != triangles.Count; iter2++) { // for (iter2 = iter1; iter2 != triangles.end(); iter2++) { if (iter1 == iter2) { continue; } poly2 = triangles[iter2]; for (i21 = 0; i21 < poly2.Count; i21++) { if ((d2.X != poly2[i21].X) || (d2.Y != poly2[i21].Y)) { continue; } i22 = (i21 + 1) % (poly2.Count); if ((d1.X != poly2[i22].X) || (d1.Y != poly2[i22].Y)) { continue; } isdiagonal = true; break; } if (isdiagonal) { break; } } if (!isdiagonal) { continue; } p2 = poly1[i11]; if (i11 == 0) { i13 = poly1.Count - 1; } else { i13 = i11 - 1; } p1 = poly1[i13]; if (i22 == poly2.Count - 1) { i23 = 0; } else { i23 = i22 + 1; } p3 = poly2[i23]; if (!IsConvex(p1, p2, p3)) { continue; } p2 = poly1[i12]; if (i12 == (poly1.Count - 1)) { i13 = 0; } else { i13 = i12 + 1; } p3 = poly1[i13]; if (i21 == 0) { i23 = poly2.Count - 1; } else { i23 = i21 - 1; } p1 = poly2[i23]; if (!IsConvex(p1, p2, p3)) { continue; } newpoly = new TPPLPoly(poly1.Count + poly2.Count - 2); k = 0; for (j = i12; j != i11; j = (j + 1) % (poly1.Count)) { newpoly[k] = poly1[j]; k++; } for (j = i22; j != i21; j = (j + 1) % (poly2.Count)) { newpoly[k] = poly2[j]; k++; } triangles.RemoveAt(iter2); triangles[iter1] = newpoly; poly1 = newpoly; i11 = -1; continue; } } for (iter1 = 0; iter1 != triangles.Count; iter1++) { parts.Add(triangles[iter1]); } return(1); }