mirror of
https://github.com/jhasse/poly2tri.git
synced 2025-08-14 03:05:39 +02:00
fixed more pointer bugs
This commit is contained in:
@@ -43,7 +43,7 @@ void CDT::Triangulate() {
|
||||
sweep_->Triangulate(*sweep_context_);
|
||||
}
|
||||
|
||||
std::list<Triangle*> CDT::GetTriangles() {
|
||||
std::vector<Triangle*> CDT::GetTriangles() {
|
||||
return sweep_context_->GetTriangles();
|
||||
}
|
||||
|
||||
|
@@ -45,7 +45,7 @@ public:
|
||||
/// Triangulate points
|
||||
void Triangulate();
|
||||
/// Get Delaunay triangles
|
||||
std::list<Triangle*> GetTriangles();
|
||||
std::vector<Triangle*> GetTriangles();
|
||||
|
||||
private:
|
||||
|
||||
|
@@ -35,31 +35,22 @@
|
||||
|
||||
// Triangulate simple polygon with holes
|
||||
void Sweep::Triangulate(SweepContext& tcx) {
|
||||
|
||||
|
||||
tcx.CreateAdvancingFront();
|
||||
|
||||
// Sweep points; build mesh
|
||||
SweepPoints(tcx);
|
||||
|
||||
/*
|
||||
// Finalize triangulation
|
||||
if( tcx.getTriangulationMode() == TriangulationMode.Polygon ) {
|
||||
finalizationPolygon( tcx );
|
||||
} else {
|
||||
finalizationConvexHull( tcx );
|
||||
}
|
||||
*/
|
||||
// Clean up
|
||||
FinalizationPolygon(tcx);
|
||||
|
||||
}
|
||||
|
||||
void Sweep::SweepPoints(SweepContext& tcx ) {
|
||||
void Sweep::SweepPoints(SweepContext& tcx) {
|
||||
|
||||
for(int i = 1; i < tcx.point_count(); i++ ) {
|
||||
|
||||
Point& point = *tcx.GetPoint(i);
|
||||
|
||||
Node& node = PointEvent(tcx, point);
|
||||
|
||||
Node& node = PointEvent(tcx, point);
|
||||
|
||||
for(int i = 0; i < point.edge_list.size(); i++) {
|
||||
EdgeEvent(tcx, point.edge_list[i], node);
|
||||
}
|
||||
@@ -68,6 +59,19 @@ void Sweep::SweepPoints(SweepContext& tcx ) {
|
||||
|
||||
}
|
||||
|
||||
void Sweep::FinalizationPolygon(SweepContext& tcx) {
|
||||
|
||||
// Get an Internal triangle to start with
|
||||
Triangle* t = tcx.front()->head()->next->triangle;
|
||||
Point* p = tcx.front()->head()->next->point;
|
||||
while(!t->GetConstrainedEdgeCW(*p)) {
|
||||
t = t->NeighborCCW(*p);
|
||||
}
|
||||
|
||||
// Collect interior triangles constrained by edges
|
||||
tcx.MeshClean(*t);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find closes node to the left of the new point and
|
||||
* create a new triangle. If needed new holes and basins
|
||||
@@ -102,7 +106,7 @@ void Sweep::EdgeEvent(SweepContext& tcx, Edge& edge, Node& node) {
|
||||
if(IsEdgeSideOfTriangle(*node.triangle, *edge.p, *edge.q)){
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// For now we will do all needed filling
|
||||
// TODO: integrate with flip process might give some better performance
|
||||
// but for now this avoid the issue with cases that needs both flips and fills
|
||||
@@ -615,7 +619,7 @@ void Sweep::TurnAdvancingFrontConvex(SweepContext& tcx, Node& b, Node& c) {
|
||||
|
||||
Node& first = b;
|
||||
|
||||
while(c != *tcx.front()->tail()) {
|
||||
while(&c != tcx.front()->tail()) {
|
||||
|
||||
if(Orient2d(*b.point, *c.point, *c.next->point) == CCW) {
|
||||
// [b,c,d] Concave - fill around c
|
||||
@@ -623,7 +627,7 @@ void Sweep::TurnAdvancingFrontConvex(SweepContext& tcx, Node& b, Node& c) {
|
||||
c = *c.next;
|
||||
} else {
|
||||
// [b,c,d] Convex
|
||||
if(b != first && Orient2d(*b.prev->point, *b.point, *c.point) == CCW) {
|
||||
if(&b != &first && Orient2d(*b.prev->point, *b.point, *c.point) == CCW) {
|
||||
// [a,b,c] Concave - fill around b
|
||||
Fill(tcx, b);
|
||||
b = *b.prev;
|
||||
|
@@ -111,6 +111,8 @@ private:
|
||||
|
||||
Point& NextFlipPoint(Point& ep, Point& eq, Triangle& ot, Point& op );
|
||||
|
||||
void FlipScanEdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle& flip_triangle, Triangle& t, Point& p);
|
||||
void FlipScanEdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle& flip_triangle, Triangle& t, Point& p);
|
||||
|
||||
void FinalizationPolygon(SweepContext& tcx);
|
||||
|
||||
};
|
||||
|
@@ -9,27 +9,26 @@ SweepContext::SweepContext(Point** polyline, const int& point_count) {
|
||||
basin = Basin();
|
||||
edge_event = EdgeEvent();
|
||||
|
||||
for(int i = 0; i < point_count; i++) {
|
||||
points_.push_back(**&polyline[i]);
|
||||
}
|
||||
points_ = polyline;
|
||||
point_count_ = point_count;
|
||||
|
||||
InitEdges(polyline, point_count);
|
||||
InitEdges(points_, point_count_);
|
||||
InitTriangulation();
|
||||
|
||||
}
|
||||
|
||||
std::list<Triangle*> SweepContext::GetTriangles() {
|
||||
return tri_list_;
|
||||
std::vector<Triangle*> SweepContext::GetTriangles() {
|
||||
return triangles_;
|
||||
}
|
||||
|
||||
void SweepContext::InitTriangulation() {
|
||||
|
||||
double xmax(points_[0].x), xmin(points_[0].x);
|
||||
double ymax(points_[0].y), ymin(points_[0].y);
|
||||
double xmax(points_[0]->x), xmin(points_[0]->x);
|
||||
double ymax(points_[0]->y), ymin(points_[0]->y);
|
||||
|
||||
// Calculate bounds.
|
||||
for(int i = 0; i < points_.size(); i++) {
|
||||
Point p = points_[i];
|
||||
for(int i = 0; i < point_count_; i++) {
|
||||
Point p = *points_[i];
|
||||
if(p.x > xmax)
|
||||
xmax = p.x;
|
||||
if(p.x < xmin)
|
||||
@@ -47,26 +46,46 @@ void SweepContext::InitTriangulation() {
|
||||
|
||||
// Sort points along y-axis
|
||||
double init_time = glfwGetTime();
|
||||
std::sort(points_.begin(), points_.end());
|
||||
std::sort(points_, points_ + point_count_, cmp);
|
||||
double dt = glfwGetTime() - init_time;
|
||||
printf("Sort time (secs) = %f\n", dt);
|
||||
|
||||
|
||||
/*
|
||||
printf("*************************\n");
|
||||
for(int i = 0; i < point_count_; i++) {
|
||||
printf("%p ", points_[i]);
|
||||
printf("%f,%f\n", points_[i]->x, points_[i]->y);
|
||||
}
|
||||
|
||||
printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
|
||||
for(int i = 0; i < edge_list.size(); i++) {
|
||||
printf("%p, %p\n", edge_list[i]->p, edge_list[i]->q);
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
void SweepContext::InitEdges(Point** polyline, const int& point_count) {
|
||||
|
||||
for(int i = 0; i < point_count; i++) {
|
||||
int j = i < points_.size() - 1 ? i + 1 : 0;
|
||||
edge_list.push_back(new Edge(**&polyline[i], **&polyline[j]));
|
||||
int j = i < point_count - 1 ? i + 1 : 0;
|
||||
edge_list.push_back(new Edge(*polyline[i], *polyline[j]));
|
||||
}
|
||||
|
||||
/*
|
||||
for(int i = 0; i < edge_list.size(); i++) {
|
||||
printf("%p, %p\n", edge_list[i]->p, edge_list[i]->q);
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
Point* SweepContext::GetPoint(const int& index) {
|
||||
return &points_[index];
|
||||
return points_[index];
|
||||
}
|
||||
|
||||
void SweepContext::AddToMap(Triangle* triangle ) {
|
||||
tri_list_.push_back(triangle);
|
||||
map_.push_back(triangle);
|
||||
}
|
||||
|
||||
Node& SweepContext::LocateNode(Point& point) {
|
||||
@@ -77,9 +96,9 @@ Node& SweepContext::LocateNode(Point& point) {
|
||||
void SweepContext::CreateAdvancingFront() {
|
||||
|
||||
// Initial triangle
|
||||
Triangle* triangle = new Triangle(points_[0], *tail_, *head_);
|
||||
Triangle* triangle = new Triangle(*points_[0], *tail_, *head_);
|
||||
|
||||
tri_list_.push_back(triangle);
|
||||
map_.push_back(triangle);
|
||||
|
||||
front_ = new AdvancingFront;
|
||||
|
||||
@@ -114,46 +133,22 @@ void SweepContext::MapTriangleToNodes(Triangle& t) {
|
||||
}
|
||||
|
||||
void SweepContext::RemoveFromMap(Triangle* triangle) {
|
||||
tri_list_.remove(triangle);
|
||||
map_.remove(triangle);
|
||||
}
|
||||
|
||||
/*
|
||||
void SweepContext::MeshClean(Triangle& triangle) {
|
||||
pointset_.ClearTriangulation();
|
||||
MeshCleanReq(triangle);
|
||||
}
|
||||
void SweepContext::MeshClean(Triangle& triangle ) {
|
||||
|
||||
AFront SweepContext::front_() {
|
||||
return front_;
|
||||
}
|
||||
|
||||
void SweepContext::Clear() {
|
||||
super.clear();
|
||||
tri_list_.Clear();
|
||||
}
|
||||
|
||||
|
||||
Node* SweepContext::LocateNode(Point& point) {
|
||||
// TODO implement tree
|
||||
return front_.Locate(point.x);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
void SweepContext::MeshCleanReq(Triangle& triangle ) {
|
||||
if(triangle != NULL && !triangle.isInterior()) {
|
||||
if(&triangle != NULL && !triangle.IsInterior()) {
|
||||
triangle.IsInterior(true);
|
||||
pointset_.AddTriangle(triangle);
|
||||
triangles_.push_back(&triangle);
|
||||
for(int i = 0; i < 3; i++) {
|
||||
if(!triangle.cEdge[i])
|
||||
MeshCleanReq(triangle.neighbors[i]);
|
||||
}
|
||||
}
|
||||
if(!triangle.constrained_edge[i])
|
||||
MeshClean(*triangle.GetNeighbor(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
SweepContext::~SweepContext() {
|
||||
//delete [] points_;
|
||||
delete head_;
|
||||
delete tail_;
|
||||
delete front_;
|
||||
|
@@ -80,7 +80,9 @@ public:
|
||||
|
||||
AdvancingFront* front();
|
||||
|
||||
std::list<Triangle*> GetTriangles();
|
||||
void MeshClean(Triangle& triangle);
|
||||
|
||||
std::vector<Triangle*> GetTriangles();
|
||||
|
||||
std::vector<Edge*> edge_list;
|
||||
|
||||
@@ -119,8 +121,11 @@ public:
|
||||
|
||||
private:
|
||||
|
||||
std::list<Triangle*> tri_list_;
|
||||
std::vector<Point> points_;
|
||||
std::vector<Triangle*> triangles_;
|
||||
std::list<Triangle*> map_;
|
||||
|
||||
Point** points_;
|
||||
int point_count_;
|
||||
|
||||
// Advancing front
|
||||
AdvancingFront* front_;
|
||||
@@ -147,7 +152,7 @@ private:
|
||||
|
||||
inline AdvancingFront* SweepContext::front() { return front_; }
|
||||
|
||||
inline int SweepContext::point_count() { return points_.size(); }
|
||||
inline int SweepContext::point_count() { return point_count_; }
|
||||
|
||||
inline void SweepContext::set_head(Point* p1) { head_ = p1; }
|
||||
|
||||
|
Reference in New Issue
Block a user