diff --git a/poly2tri/sweep/cdt.cc b/poly2tri/sweep/cdt.cc index 5aec1ea..dcca815 100644 --- a/poly2tri/sweep/cdt.cc +++ b/poly2tri/sweep/cdt.cc @@ -32,14 +32,15 @@ namespace p2t { -CDT::CDT(Point** polyline, const int& point_count) +CDT::CDT(std::vector polyline) { - sweep_context_ = new SweepContext(polyline, point_count); + sweep_context_ = new SweepContext(polyline); sweep_ = new Sweep; } -void CDT::AddHole(const Point poly_line[], const int point_count) +void CDT::AddHole(std::vector polyline) { + sweep_context_->AddHole(polyline); } void CDT::Triangulate() diff --git a/poly2tri/sweep/cdt.h b/poly2tri/sweep/cdt.h index db4fdbb..84c781e 100644 --- a/poly2tri/sweep/cdt.h +++ b/poly2tri/sweep/cdt.h @@ -39,9 +39,9 @@ class CDT public: /// Constructor -CDT(Point** poly_line, const int& point_count); +CDT(std::vector polyline); /// Add a hole -void AddHole(const Point poly_line[], const int point_count); +void AddHole(std::vector polyline); /// Triangulate points void Triangulate(); /// Get Delaunay triangles diff --git a/poly2tri/sweep/sweep.cc b/poly2tri/sweep/sweep.cc index 280cec2..9077133 100644 --- a/poly2tri/sweep/sweep.cc +++ b/poly2tri/sweep/sweep.cc @@ -38,6 +38,7 @@ namespace p2t { // Triangulate simple polygon with holes void Sweep::Triangulate(SweepContext& tcx) { + tcx.InitTriangulation(); tcx.CreateAdvancingFront(); // Sweep points; build mesh SweepPoints(tcx); diff --git a/poly2tri/sweep/sweep_context.cc b/poly2tri/sweep/sweep_context.cc index 66e574c..d3fb3dc 100644 --- a/poly2tri/sweep/sweep_context.cc +++ b/poly2tri/sweep/sweep_context.cc @@ -35,16 +35,22 @@ namespace p2t { -SweepContext::SweepContext(Point** polyline, const int& point_count) +SweepContext::SweepContext(std::vector polyline) { basin = Basin(); edge_event = EdgeEvent(); points_ = polyline; - point_count_ = point_count; - InitEdges(points_, point_count_); - InitTriangulation(); + InitEdges(points_); +} + +void SweepContext::AddHole(std::vector polyline) +{ + InitEdges(polyline); + for(int i = 0; i < polyline.size(); i++) { + points_.push_back(polyline[i]); + } } std::vector SweepContext::GetTriangles() @@ -63,8 +69,8 @@ void SweepContext::InitTriangulation() double ymax(points_[0]->y), ymin(points_[0]->y); // Calculate bounds. - for (int i = 0; i < point_count_; i++) { - Point p = *points_[i]; + for (int i = 0; i < points_.size(); i++) { + Point& p = *points_[i]; if (p.x > xmax) xmax = p.x; if (p.x < xmin) @@ -81,14 +87,15 @@ void SweepContext::InitTriangulation() tail_ = new Point(xmin - dx, ymin - dy); // Sort points along y-axis - std::sort(points_, points_ + point_count_, cmp); - + std::sort(points_.begin(), points_.end(), cmp); + } -void SweepContext::InitEdges(Point** polyline, const int& point_count) +void SweepContext::InitEdges(std::vector polyline) { - for (int i = 0; i < point_count; i++) { - int j = i < point_count - 1 ? i + 1 : 0; + int num_points = polyline.size(); + for (int i = 0; i < num_points; i++) { + int j = i < num_points - 1 ? i + 1 : 0; edge_list.push_back(new Edge(*polyline[i], *polyline[j])); } } diff --git a/poly2tri/sweep/sweep_context.h b/poly2tri/sweep/sweep_context.h index 37a070a..80cb3ea 100644 --- a/poly2tri/sweep/sweep_context.h +++ b/poly2tri/sweep/sweep_context.h @@ -47,7 +47,7 @@ class SweepContext { public: // Constructor -SweepContext(Point** polyline, const int& point_count); +SweepContext(std::vector polyline); // Destructor ~SweepContext(); @@ -74,6 +74,8 @@ Point* GetPoints(); void RemoveFromMap(Triangle* triangle); +void AddHole(std::vector polyline); + AdvancingFront* front(); void MeshClean(Triangle& triangle); @@ -118,11 +120,12 @@ EdgeEvent edge_event; private: +friend class Sweep; + std::vector triangles_; std::list map_; -Point** points_; -int point_count_; +std::vector points_; // Advancing front AdvancingFront* front_; @@ -134,7 +137,7 @@ Point* tail_; //EdgeEvent edgeEvent = new EdgeEvent(); void InitTriangulation(); -void InitEdges(Point** polyline, const int& point_count); +void InitEdges(std::vector polyline); }; @@ -145,7 +148,7 @@ inline AdvancingFront* SweepContext::front() inline int SweepContext::point_count() { - return point_count_; + return points_.size(); } inline void SweepContext::set_head(Point* p1) diff --git a/testbed/main.cc b/testbed/main.cc index 276349a..ba074c1 100644 --- a/testbed/main.cc +++ b/testbed/main.cc @@ -122,19 +122,13 @@ int main(int argc, char* argv[]) cout << "File not opened" << endl; } - int num_points = points.size(); - cout << "Number of points = " << num_points << endl; - - Point** polyline = new Point *[num_points]; - for (int i = 0; i < num_points; i++) { - polyline[i] = points[i]; - } + cout << "Number of points = " << points.size() << endl; Init(); // Perform triangulation double init_time = glfwGetTime(); - CDT * cdt = new CDT(polyline, num_points); + CDT * cdt = new CDT(points); cdt->Triangulate(); double dt = glfwGetTime() - init_time; cout << "Elapsed time (secs) = " << dt << endl; @@ -144,7 +138,6 @@ int main(int argc, char* argv[]) MainLoop(atof(argv[4])); - delete [] polyline; ShutDown(0); return 0; }