mirror of
https://github.com/jhasse/poly2tri.git
synced 2025-08-14 03:05:39 +02:00
fixed edge pointer bug - yay
This commit is contained in:
@@ -86,10 +86,12 @@ Node* AdvancingFront::LocatePoint(Point* point) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
search_node_ = node;
|
||||
if(node) search_node_ = node;
|
||||
return node;
|
||||
}
|
||||
|
||||
AdvancingFront::~AdvancingFront() {
|
||||
delete head_; search_node_; tail_;
|
||||
delete head_;
|
||||
delete search_node_;
|
||||
delete tail_;
|
||||
}
|
||||
|
@@ -30,7 +30,7 @@
|
||||
*/
|
||||
#include "cdt.h"
|
||||
|
||||
CDT::CDT(Point polyline[], const int& point_count) {
|
||||
CDT::CDT(Point** polyline, const int& point_count) {
|
||||
sweep_context_ = new SweepContext(polyline, point_count);
|
||||
sweep_ = new Sweep;
|
||||
}
|
||||
|
@@ -39,7 +39,7 @@ class CDT
|
||||
public:
|
||||
|
||||
/// Constructor
|
||||
CDT(Point poly_line[], const int& point_count);
|
||||
CDT(Point** poly_line, const int& point_count);
|
||||
/// Add a hole
|
||||
void AddHole(const Point poly_line[], const int point_count);
|
||||
/// Triangulate points
|
||||
|
@@ -57,11 +57,13 @@ void Sweep::SweepPoints(SweepContext& tcx ) {
|
||||
for(int i = 1; i < tcx.point_count(); i++ ) {
|
||||
|
||||
Point& point = *tcx.GetPoint(i);
|
||||
|
||||
Node& node = PointEvent(tcx, point);
|
||||
|
||||
|
||||
for(int i = 0; i < point.edge_list.size(); i++) {
|
||||
EdgeEvent(tcx, point.edge_list[i], node );
|
||||
EdgeEvent(tcx, point.edge_list[i], node);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -93,10 +95,10 @@ Node& Sweep::PointEvent(SweepContext& tcx, Point& point) {
|
||||
}
|
||||
|
||||
void Sweep::EdgeEvent(SweepContext& tcx, Edge& edge, Node& node) {
|
||||
|
||||
|
||||
tcx.edge_event.constrained_edge = &edge;
|
||||
tcx.edge_event.right = edge.p->x > edge.q->x;
|
||||
|
||||
|
||||
if(IsEdgeSideOfTriangle(*node.triangle, *edge.p, *edge.q)){
|
||||
return;
|
||||
}
|
||||
@@ -274,7 +276,7 @@ double Sweep::HoleAngle(Node& node) {
|
||||
double ay = node.next->point->y - node.point->y;
|
||||
double bx = node.prev->point->x - node.point->x;
|
||||
double by = node.prev->point->y - node.point->y;
|
||||
return atan2(ax*by - ay*bx, ax*bx + ay*by);
|
||||
return atan2(ax * by - ay * bx, ax * bx + ay * by);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -380,7 +382,7 @@ bool Sweep::Incircle(Point& pa, Point& pb, Point& pc, Point& pd) {
|
||||
double oabd = adxbdy - bdxady;
|
||||
|
||||
if( oabd <= 0 )
|
||||
return false;
|
||||
return false;
|
||||
|
||||
double cdx = pc.x - pd.x;
|
||||
double cdy = pc.y - pd.y;
|
||||
@@ -390,7 +392,7 @@ bool Sweep::Incircle(Point& pa, Point& pb, Point& pc, Point& pd) {
|
||||
double ocad = cdxady - adxcdy;
|
||||
|
||||
if( ocad <= 0 )
|
||||
return false;
|
||||
return false;
|
||||
|
||||
double bdxcdy = bdx * cdy;
|
||||
double cdxbdy = cdx * bdy;
|
||||
@@ -693,7 +695,7 @@ void Sweep::FillRightConcaveEdgeEvent(SweepContext& tcx, Edge& edge, Node& node)
|
||||
|
||||
void Sweep::FillRightConvexEdgeEvent(SweepContext& tcx, Edge& edge, Node& node) {
|
||||
|
||||
// Next concave or convex?
|
||||
// Next concave or convex?
|
||||
if(Orient2d(*node.next->point, *node.next->next->point, *node.next->next->next->point ) == CCW) {
|
||||
// Concave
|
||||
FillRightConcaveEdgeEvent(tcx, edge, *node.next);
|
||||
@@ -788,25 +790,25 @@ void Sweep::FlipEdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle& t,
|
||||
}
|
||||
|
||||
if(InScanArea(p, *t.PointCCW(p), *t.PointCW(p), op)) {
|
||||
// Lets rotate shared edge one vertex CW
|
||||
RotateTrianglePair(t, p, ot, op);
|
||||
tcx.MapTriangleToNodes(t);
|
||||
tcx.MapTriangleToNodes(ot);
|
||||
|
||||
if( p == eq && op == ep ) {
|
||||
if(eq == *tcx.edge_event.constrained_edge->q && ep == *tcx.edge_event.constrained_edge->p) {
|
||||
t.MarkConstrainedEdge(&ep, &eq);
|
||||
ot.MarkConstrainedEdge(&ep, &eq);
|
||||
Legalize(tcx, t);
|
||||
Legalize(tcx, ot);
|
||||
} else {
|
||||
// XXX: I think one of the triangles should be legalized here?
|
||||
}
|
||||
} else {
|
||||
Orientation o = Orient2d(eq, op, ep);
|
||||
t = NextFlipTriangle(tcx, (int) o, t, ot, p, op);
|
||||
FlipEdgeEvent(tcx, ep, eq, t, p);
|
||||
}
|
||||
// Lets rotate shared edge one vertex CW
|
||||
RotateTrianglePair(t, p, ot, op);
|
||||
tcx.MapTriangleToNodes(t);
|
||||
tcx.MapTriangleToNodes(ot);
|
||||
|
||||
if( p == eq && op == ep ) {
|
||||
if(eq == *tcx.edge_event.constrained_edge->q && ep == *tcx.edge_event.constrained_edge->p) {
|
||||
t.MarkConstrainedEdge(&ep, &eq);
|
||||
ot.MarkConstrainedEdge(&ep, &eq);
|
||||
Legalize(tcx, t);
|
||||
Legalize(tcx, ot);
|
||||
} else {
|
||||
// XXX: I think one of the triangles should be legalized here?
|
||||
}
|
||||
} else {
|
||||
Orientation o = Orient2d(eq, op, ep);
|
||||
t = NextFlipTriangle(tcx, (int) o, t, ot, p, op);
|
||||
FlipEdgeEvent(tcx, ep, eq, t, p);
|
||||
}
|
||||
} else {
|
||||
Point& newP = NextFlipPoint( ep, eq, ot, op);
|
||||
FlipScanEdgeEvent(tcx, ep, eq, t, ot, newP);
|
||||
@@ -816,36 +818,36 @@ void Sweep::FlipEdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle& t,
|
||||
|
||||
Triangle& Sweep::NextFlipTriangle(SweepContext& tcx, int o, Triangle& t, Triangle& ot, Point& p, Point& op) {
|
||||
|
||||
if(o == CCW ) {
|
||||
// ot is not crossing edge after flip
|
||||
int edge_index = ot.EdgeIndex(&p, &op);
|
||||
ot.delaunay_edge[edge_index] = true;
|
||||
Legalize(tcx, ot);
|
||||
ot.ClearDelunayEdges();
|
||||
return t;
|
||||
}
|
||||
|
||||
// t is not crossing edge after flip
|
||||
int edge_index = t.EdgeIndex(&p, &op);
|
||||
t.delaunay_edge[edge_index] = true;
|
||||
Legalize(tcx, t);
|
||||
t.ClearDelunayEdges();
|
||||
return ot;
|
||||
if(o == CCW) {
|
||||
// ot is not crossing edge after flip
|
||||
int edge_index = ot.EdgeIndex(&p, &op);
|
||||
ot.delaunay_edge[edge_index] = true;
|
||||
Legalize(tcx, ot);
|
||||
ot.ClearDelunayEdges();
|
||||
return t;
|
||||
}
|
||||
|
||||
// t is not crossing edge after flip
|
||||
int edge_index = t.EdgeIndex(&p, &op);
|
||||
t.delaunay_edge[edge_index] = true;
|
||||
Legalize(tcx, t);
|
||||
t.ClearDelunayEdges();
|
||||
return ot;
|
||||
}
|
||||
|
||||
Point& Sweep::NextFlipPoint(Point& ep, Point& eq, Triangle& ot, Point& op ) {
|
||||
Point& Sweep::NextFlipPoint(Point& ep, Point& eq, Triangle& ot, Point& op) {
|
||||
|
||||
Orientation o2d = Orient2d(eq, op, ep);
|
||||
if(o2d == CW) {
|
||||
// Right
|
||||
return *ot.PointCCW(op);
|
||||
} else if(o2d == CCW) {
|
||||
// Left
|
||||
return *ot.PointCW(op);
|
||||
} else {
|
||||
//throw new RuntimeException("[Unsupported] Opposing point on constrained edge");
|
||||
assert(0);
|
||||
}
|
||||
Orientation o2d = Orient2d(eq, op, ep);
|
||||
if(o2d == CW) {
|
||||
// Right
|
||||
return *ot.PointCCW(op);
|
||||
} else if(o2d == CCW) {
|
||||
// Left
|
||||
return *ot.PointCW(op);
|
||||
} else {
|
||||
//throw new RuntimeException("[Unsupported] Opposing point on constrained edge");
|
||||
assert(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@@ -37,8 +37,6 @@
|
||||
* "FlipScan" Constrained Edge Algorithm invented by Thomas <20>hl<68>n, thahlen@gmail.com
|
||||
*/
|
||||
|
||||
#include <list>
|
||||
|
||||
class SweepContext;
|
||||
struct Node;
|
||||
struct Point;
|
||||
|
@@ -4,16 +4,17 @@
|
||||
#include <GL/glfw.h>
|
||||
#include "advancing_front.h"
|
||||
|
||||
SweepContext::SweepContext(Point polyline[], const int& point_count) {
|
||||
SweepContext::SweepContext(Point** polyline, const int& point_count) {
|
||||
|
||||
basin = Basin();
|
||||
edge_event = EdgeEvent();
|
||||
|
||||
point_count_ = point_count;
|
||||
points_ = polyline;
|
||||
for(int i = 0; i < point_count; i++) {
|
||||
points_.push_back(**&polyline[i]);
|
||||
}
|
||||
|
||||
InitEdges(polyline, point_count);
|
||||
InitTriangulation();
|
||||
InitEdges();
|
||||
|
||||
}
|
||||
|
||||
@@ -27,7 +28,7 @@ void SweepContext::InitTriangulation() {
|
||||
double ymax(points_[0].y), ymin(points_[0].y);
|
||||
|
||||
// Calculate bounds.
|
||||
for(int i = 0; i < point_count_; i++) {
|
||||
for(int i = 0; i < points_.size(); i++) {
|
||||
Point p = points_[i];
|
||||
if(p.x > xmax)
|
||||
xmax = p.x;
|
||||
@@ -46,22 +47,17 @@ void SweepContext::InitTriangulation() {
|
||||
|
||||
// Sort points along y-axis
|
||||
double init_time = glfwGetTime();
|
||||
std::sort(points_, points_ + point_count_);
|
||||
std::sort(points_.begin(), points_.end());
|
||||
double dt = glfwGetTime() - init_time;
|
||||
printf("Sort time (secs) = %f\n", dt);
|
||||
|
||||
/*
|
||||
for(int i = 0; i < point_count_; i++) {
|
||||
printf("%i: %f, %f\n", i+1, points_[i].x, points_[i].y);
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
}
|
||||
|
||||
void SweepContext::InitEdges() {
|
||||
for(int i = 0; i < point_count_; i++) {
|
||||
int j = i < point_count_ - 1 ? i + 1 : 0;
|
||||
new Edge(points_[i], points_[j]);
|
||||
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]));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,7 +153,7 @@ void SweepContext::MeshCleanReq(Triangle& triangle ) {
|
||||
*/
|
||||
|
||||
SweepContext::~SweepContext() {
|
||||
delete [] points_;
|
||||
//delete [] points_;
|
||||
delete head_;
|
||||
delete tail_;
|
||||
delete front_;
|
||||
|
@@ -29,6 +29,7 @@
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include <list>
|
||||
#include <vector>
|
||||
|
||||
// Inital triangle factor, seed triangle will extend 30% of
|
||||
// PointSet width to both left and right.
|
||||
@@ -46,7 +47,7 @@ class SweepContext {
|
||||
public:
|
||||
|
||||
// Constructor
|
||||
SweepContext(Point polyline[], const int& point_count);
|
||||
SweepContext(Point** polyline, const int& point_count);
|
||||
// Destructor
|
||||
~SweepContext();
|
||||
|
||||
@@ -81,6 +82,7 @@ public:
|
||||
|
||||
std::list<Triangle*> GetTriangles();
|
||||
|
||||
std::vector<Edge*> edge_list;
|
||||
|
||||
struct Basin {
|
||||
|
||||
@@ -118,9 +120,7 @@ public:
|
||||
private:
|
||||
|
||||
std::list<Triangle*> tri_list_;
|
||||
|
||||
Point* points_;
|
||||
int point_count_;
|
||||
std::vector<Point> points_;
|
||||
|
||||
// Advancing front
|
||||
AdvancingFront* front_;
|
||||
@@ -132,7 +132,7 @@ private:
|
||||
//EdgeEvent edgeEvent = new EdgeEvent();
|
||||
|
||||
void InitTriangulation();
|
||||
void InitEdges();
|
||||
void InitEdges(Point** polyline, const int& point_count);
|
||||
|
||||
//void MeshCleanReq(Triangle& triangle )
|
||||
|
||||
@@ -147,7 +147,7 @@ private:
|
||||
|
||||
inline AdvancingFront* SweepContext::front() { return front_; }
|
||||
|
||||
inline int SweepContext::point_count() { return point_count_; }
|
||||
inline int SweepContext::point_count() { return points_.size(); }
|
||||
|
||||
inline void SweepContext::set_head(Point* p1) { head_ = p1; }
|
||||
|
||||
|
Reference in New Issue
Block a user