bool TriangulatorAdaptor::triangulate(int *triangleIndexes) { int XX, YY; from3DTo2D(XX, YY); map<pair<double, double> , int> coorToPosMap; // coordinate to position map for (int i = 0; i < polygon.size(); i++) { coorToPosMap.insert( pair<pair<double, double> , int>( pair<double, double>(polygon[i][XX], polygon[i][YY]), i)); } TPPLPoly poly; poly.Init(polygon.size()); for (int i = 0; i < polygon.size(); i++) { poly[i].x = polygon[i][XX]; poly[i].y = polygon[i][YY]; } if (poly.GetOrientation() == TPPL_CW) { isClockwise = true; poly.Invert(); // polygon has to be counter-clockwise } else if(poly.GetOrientation() == TPPL_CCW) { isClockwise = false; } else { return false; } list<TPPLPoly> triangles; TPPLPartition triangulator; // see http://code.google.com/p/polypartition/ int success = triangulator.Triangulate_OPT(&poly, &triangles); if (success != 1) { cout << "Error: polygon cannot be triangulated!" << endl; cout << "Polygon:" << endl; for (int i=0; i<polygon.size(); i++) { cout << polygon[i][0] << " " << polygon[i][1] << " " << polygon[i][2] << endl; } return false; } //assert(success == 1); assert(triangles.size() +2 == polygon.size()); int count = 0; for (list<TPPLPoly>::iterator it = triangles.begin(); it != triangles.end(); it++) { if (!isClockwise) { for (int j = 0; j < 3; j++) { triangleIndexes[count * 3 + j] = coorToPosMap[pair<double, double>((*it)[j].x, (*it)[j].y)]; } } else { for (int j = 0; j < 3; j++) { triangleIndexes[count * 3 + 2 - j] = coorToPosMap[pair<double, double>((*it)[j].x, (*it)[j].y)]; } } count++; } assert(count == triangles.size()); return true; }
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); }
void partition() { TPPLPoly poly; std::vector<Point> nodes = polygons_[0].nodes_; poly.Init(nodes.size()); unsigned int i = 0; for (std::vector<Point>::const_iterator p = nodes.begin(); p != nodes.end(); ++p, ++i) { poly[i].x = p->lat; poly[i].y = p->lon; } std::list<TPPLPoly> convex_polys; TPPLPartition partitioner; partitioner.Triangulate_OPT(&poly, &convex_polys); //partitioner.ConvexPartition_HM(&poly, &convex_polys); polygons_.clear(); for (std::list<TPPLPoly>::iterator p = convex_polys.begin(); p != convex_polys.end(); ++p) { add_polygon(); for (long i = 0; i < p->GetNumPoints(); ++i) { TPPLPoint point = p->GetPoint(i); add_node(Point(point.x, point.y)); } } }