mirror of
https://github.com/jhasse/poly2tri.git
synced 2024-11-05 22:09:52 +01:00
add holes to API
This commit is contained in:
parent
5dbe785975
commit
6ab0f0ae00
@ -32,14 +32,15 @@
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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()
|
||||
|
@ -39,9 +39,9 @@ class CDT
|
||||
public:
|
||||
|
||||
/// Constructor
|
||||
CDT(Point** poly_line, const int& point_count);
|
||||
CDT(std::vector<Point*> polyline);
|
||||
/// Add a hole
|
||||
void AddHole(const Point poly_line[], const int point_count);
|
||||
void AddHole(std::vector<Point*> polyline);
|
||||
/// Triangulate points
|
||||
void Triangulate();
|
||||
/// Get Delaunay triangles
|
||||
|
@ -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);
|
||||
|
@ -35,16 +35,22 @@
|
||||
|
||||
namespace p2t {
|
||||
|
||||
SweepContext::SweepContext(Point** polyline, const int& point_count)
|
||||
SweepContext::SweepContext(std::vector<Point*> polyline)
|
||||
{
|
||||
basin = Basin();
|
||||
edge_event = EdgeEvent();
|
||||
|
||||
points_ = polyline;
|
||||
point_count_ = point_count;
|
||||
|
||||
InitEdges(points_, point_count_);
|
||||
InitTriangulation();
|
||||
InitEdges(points_);
|
||||
}
|
||||
|
||||
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()
|
||||
@ -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<Point*> 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]));
|
||||
}
|
||||
}
|
||||
|
@ -47,7 +47,7 @@ class SweepContext {
|
||||
public:
|
||||
|
||||
// Constructor
|
||||
SweepContext(Point** polyline, const int& point_count);
|
||||
SweepContext(std::vector<Point*> polyline);
|
||||
// Destructor
|
||||
~SweepContext();
|
||||
|
||||
@ -74,6 +74,8 @@ Point* GetPoints();
|
||||
|
||||
void RemoveFromMap(Triangle* triangle);
|
||||
|
||||
void AddHole(std::vector<Point*> polyline);
|
||||
|
||||
AdvancingFront* front();
|
||||
|
||||
void MeshClean(Triangle& triangle);
|
||||
@ -118,11 +120,12 @@ EdgeEvent edge_event;
|
||||
|
||||
private:
|
||||
|
||||
friend class Sweep;
|
||||
|
||||
std::vector<Triangle*> triangles_;
|
||||
std::list<Triangle*> map_;
|
||||
|
||||
Point** points_;
|
||||
int point_count_;
|
||||
std::vector<Point*> 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<Point*> 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)
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user