add holes to API

This commit is contained in:
zzzzrrr 2010-01-22 21:38:19 -05:00
parent 5dbe785975
commit 6ab0f0ae00
6 changed files with 35 additions and 30 deletions

View File

@ -32,14 +32,15 @@
namespace p2t { namespace p2t {
CDT::CDT(Point** polyline, const int& point_count) CDT::CDT(std::vector<Point*> polyline)
{ {
sweep_context_ = new SweepContext(polyline, point_count); sweep_context_ = new SweepContext(polyline);
sweep_ = new Sweep; sweep_ = new Sweep;
} }
void CDT::AddHole(const Point poly_line[], const int point_count) void CDT::AddHole(std::vector<Point*> polyline)
{ {
sweep_context_->AddHole(polyline);
} }
void CDT::Triangulate() void CDT::Triangulate()

View File

@ -39,9 +39,9 @@ class CDT
public: public:
/// Constructor /// Constructor
CDT(Point** poly_line, const int& point_count); CDT(std::vector<Point*> polyline);
/// Add a hole /// Add a hole
void AddHole(const Point poly_line[], const int point_count); void AddHole(std::vector<Point*> polyline);
/// Triangulate points /// Triangulate points
void Triangulate(); void Triangulate();
/// Get Delaunay triangles /// Get Delaunay triangles

View File

@ -38,6 +38,7 @@ namespace p2t {
// Triangulate simple polygon with holes // Triangulate simple polygon with holes
void Sweep::Triangulate(SweepContext& tcx) void Sweep::Triangulate(SweepContext& tcx)
{ {
tcx.InitTriangulation();
tcx.CreateAdvancingFront(); tcx.CreateAdvancingFront();
// Sweep points; build mesh // Sweep points; build mesh
SweepPoints(tcx); SweepPoints(tcx);

View File

@ -35,16 +35,22 @@
namespace p2t { namespace p2t {
SweepContext::SweepContext(Point** polyline, const int& point_count) SweepContext::SweepContext(std::vector<Point*> polyline)
{ {
basin = Basin(); basin = Basin();
edge_event = EdgeEvent(); edge_event = EdgeEvent();
points_ = polyline; points_ = polyline;
point_count_ = point_count;
InitEdges(points_, point_count_); InitEdges(points_);
InitTriangulation(); }
void SweepContext::AddHole(std::vector<Point*> polyline)
{
InitEdges(polyline);
for(int i = 0; i < polyline.size(); i++) {
points_.push_back(polyline[i]);
}
} }
std::vector<Triangle*> SweepContext::GetTriangles() std::vector<Triangle*> SweepContext::GetTriangles()
@ -63,8 +69,8 @@ void SweepContext::InitTriangulation()
double ymax(points_[0]->y), ymin(points_[0]->y); double ymax(points_[0]->y), ymin(points_[0]->y);
// Calculate bounds. // Calculate bounds.
for (int i = 0; i < point_count_; i++) { for (int i = 0; i < points_.size(); i++) {
Point p = *points_[i]; Point& p = *points_[i];
if (p.x > xmax) if (p.x > xmax)
xmax = p.x; xmax = p.x;
if (p.x < xmin) if (p.x < xmin)
@ -81,14 +87,15 @@ void SweepContext::InitTriangulation()
tail_ = new Point(xmin - dx, ymin - dy); tail_ = new Point(xmin - dx, ymin - dy);
// Sort points along y-axis // 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<Point*> polyline)
{ {
for (int i = 0; i < point_count; i++) { int num_points = polyline.size();
int j = i < point_count - 1 ? i + 1 : 0; 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])); edge_list.push_back(new Edge(*polyline[i], *polyline[j]));
} }
} }

View File

@ -47,7 +47,7 @@ class SweepContext {
public: public:
// Constructor // Constructor
SweepContext(Point** polyline, const int& point_count); SweepContext(std::vector<Point*> polyline);
// Destructor // Destructor
~SweepContext(); ~SweepContext();
@ -74,6 +74,8 @@ Point* GetPoints();
void RemoveFromMap(Triangle* triangle); void RemoveFromMap(Triangle* triangle);
void AddHole(std::vector<Point*> polyline);
AdvancingFront* front(); AdvancingFront* front();
void MeshClean(Triangle& triangle); void MeshClean(Triangle& triangle);
@ -118,11 +120,12 @@ EdgeEvent edge_event;
private: private:
friend class Sweep;
std::vector<Triangle*> triangles_; std::vector<Triangle*> triangles_;
std::list<Triangle*> map_; std::list<Triangle*> map_;
Point** points_; std::vector<Point*> points_;
int point_count_;
// Advancing front // Advancing front
AdvancingFront* front_; AdvancingFront* front_;
@ -134,7 +137,7 @@ Point* tail_;
//EdgeEvent edgeEvent = new EdgeEvent(); //EdgeEvent edgeEvent = new EdgeEvent();
void InitTriangulation(); void InitTriangulation();
void InitEdges(Point** polyline, const int& point_count); void InitEdges(std::vector<Point*> polyline);
}; };
@ -145,7 +148,7 @@ inline AdvancingFront* SweepContext::front()
inline int SweepContext::point_count() inline int SweepContext::point_count()
{ {
return point_count_; return points_.size();
} }
inline void SweepContext::set_head(Point* p1) inline void SweepContext::set_head(Point* p1)

View File

@ -122,19 +122,13 @@ int main(int argc, char* argv[])
cout << "File not opened" << endl; cout << "File not opened" << endl;
} }
int num_points = points.size(); cout << "Number of points = " << points.size() << endl;
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];
}
Init(); Init();
// Perform triangulation // Perform triangulation
double init_time = glfwGetTime(); double init_time = glfwGetTime();
CDT * cdt = new CDT(polyline, num_points); CDT * cdt = new CDT(points);
cdt->Triangulate(); cdt->Triangulate();
double dt = glfwGetTime() - init_time; double dt = glfwGetTime() - init_time;
cout << "Elapsed time (secs) = " << dt << endl; cout << "Elapsed time (secs) = " << dt << endl;
@ -144,7 +138,6 @@ int main(int argc, char* argv[])
MainLoop(atof(argv[4])); MainLoop(atof(argv[4]));
delete [] polyline;
ShutDown(0); ShutDown(0);
return 0; return 0;
} }