int main() { TPPLPartition pp; list<TPPLPoly> testpolys,result; ReadPolyList("failing_mono_clean - copy.txt",&testpolys); DrawPolyList("test.bmp", &testpolys); if(!pp.Triangulate_MONO(&testpolys,&result)) printf("Error\n"); DrawPolyList("test2.bmp", &result); }
void GenerateTestData() { TPPLPartition pp; list<TPPLPoly> testpolys,result,expectedResult; ReadPolyList("test_input.txt",&testpolys); DrawPolyList("test_input.bmp",&testpolys); pp.Triangulate_EC(&testpolys,&result); //pp.Triangulate_EC(&(*testpolys.begin()),&result); DrawPolyList("test_triangulate_EC.bmp",&result); WritePolyList("test_triangulate_EC.txt",&result); result.clear(); expectedResult.clear(); pp.Triangulate_OPT(&(*testpolys.begin()),&result); DrawPolyList("test_triangulate_OPT.bmp",&result); WritePolyList("test_triangulate_OPT.txt",&result); result.clear(); expectedResult.clear(); pp.Triangulate_MONO(&testpolys,&result); //pp.Triangulate_MONO(&(*testpolys.begin()),&result); DrawPolyList("test_triangulate_MONO.bmp",&result); WritePolyList("test_triangulate_MONO.txt",&result); result.clear(); expectedResult.clear(); pp.ConvexPartition_HM(&testpolys,&result); //pp.ConvexPartition_HM(&(*testpolys.begin()),&result); DrawPolyList("test_convexpartition_HM.bmp",&result); WritePolyList("test_convexpartition_HM.txt",&result); result.clear(); expectedResult.clear(); pp.ConvexPartition_OPT(&(*testpolys.begin()),&result); DrawPolyList("test_convexpartition_OPT.bmp",&result); WritePolyList("test_convexpartition_OPT.txt",&result); }
std::vector<std::vector<Point3d> > computeTriangulation(const Point3dVector& vertices, const std::vector<std::vector<Point3d> >& holes, double tol) { std::vector<std::vector<Point3d> > result; // check input if (vertices.size () < 3){ return result; } boost::optional<Vector3d> normal = getOutwardNormal(vertices); if (!normal || normal->z() > -0.999){ return result; } for (const auto& hole : holes){ normal = getOutwardNormal(hole); if (!normal || normal->z() > -0.999){ return result; } } std::vector<Point3d> allPoints; // PolyPartition does not support holes which intersect the polygon or share an edge // if any hole is not fully contained we will use boost to remove all the holes bool polyPartitionHoles = true; for (const std::vector<Point3d>& hole : holes){ if (!within(hole, vertices, tol)){ // PolyPartition can't handle this polyPartitionHoles = false; break; } } if (!polyPartitionHoles){ // use boost to do all the intersections std::vector<std::vector<Point3d> > allFaces = subtract(vertices, holes, tol); std::vector<std::vector<Point3d> > noHoles; for (const std::vector<Point3d>& face : allFaces){ std::vector<std::vector<Point3d> > temp = computeTriangulation(face, noHoles); result.insert(result.end(), temp.begin(), temp.end()); } return result; } // convert input to vector of TPPLPoly std::list<TPPLPoly> polys; TPPLPoly outerPoly; // must be counter-clockwise, input vertices are clockwise outerPoly.Init(vertices.size()); outerPoly.SetHole(false); unsigned n = vertices.size(); for(unsigned i = 0; i < n; ++i){ // should all have zero z coordinate now double z = vertices[n-i-1].z(); if (abs(z) > tol){ LOG_FREE(Error, "utilities.geometry.computeTriangulation", "All points must be on z = 0 plane for triangulation methods"); return result; } Point3d point = getCombinedPoint(vertices[n-i-1], allPoints, tol); outerPoly[i].x = point.x(); outerPoly[i].y = point.y(); } outerPoly.SetOrientation(TPPL_CCW); polys.push_back(outerPoly); for (const std::vector<Point3d>& holeVertices : holes){ if (holeVertices.size () < 3){ LOG_FREE(Error, "utilities.geometry.computeTriangulation", "Hole has fewer than 3 points, ignoring"); continue; } TPPLPoly innerPoly; // must be clockwise, input vertices are clockwise innerPoly.Init(holeVertices.size()); innerPoly.SetHole(true); //std::cout << "inner :"; for(unsigned i = 0; i < holeVertices.size(); ++i){ // should all have zero z coordinate now double z = holeVertices[i].z(); if (abs(z) > tol){ LOG_FREE(Error, "utilities.geometry.computeTriangulation", "All points must be on z = 0 plane for triangulation methods"); return result; } Point3d point = getCombinedPoint(holeVertices[i], allPoints, tol); innerPoly[i].x = point.x(); innerPoly[i].y = point.y(); } innerPoly.SetOrientation(TPPL_CW); polys.push_back(innerPoly); } // do partitioning TPPLPartition pp; std::list<TPPLPoly> resultPolys; int test = pp.Triangulate_EC(&polys,&resultPolys); if (test == 0){ test = pp.Triangulate_MONO(&polys, &resultPolys); } if (test == 0){ LOG_FREE(Error, "utilities.geometry.computeTriangulation", "Failed to partition polygon"); return result; } // convert back to vertices std::list<TPPLPoly>::iterator it, itend; //std::cout << "Start" << std::endl; for(it = resultPolys.begin(), itend = resultPolys.end(); it != itend; ++it){ it->SetOrientation(TPPL_CW); std::vector<Point3d> triangle; for (long i = 0; i < it->GetNumPoints(); ++i){ TPPLPoint point = it->GetPoint(i); triangle.push_back(Point3d(point.x, point.y, 0)); } //std::cout << triangle << std::endl; result.push_back(triangle); } //std::cout << "End" << std::endl; return result; }