Very minor fixes: Uncrustify a few files and rename a shadowed var.

This commit is contained in:
jean-pierre charras 2016-07-22 08:59:17 +02:00
parent 78e4787297
commit 146a78a8fb
7 changed files with 1277 additions and 998 deletions

View File

@ -56,7 +56,7 @@ struct Point
std::vector<Edge*> edge_list; std::vector<Edge*> edge_list;
/// Construct using coordinates. /// Construct using coordinates.
Point( double x, double y ) : x( x ), y( y ) {} Point( double ax, double ay ) : x( ax ), y( ay ) {}
/// Set this point to all zeros. /// Set this point to all zeros.
void set_zero() void set_zero()

View File

@ -31,7 +31,6 @@
#include "advancing_front.h" #include "advancing_front.h"
namespace p2t { namespace p2t {
AdvancingFront::AdvancingFront( Node& head, Node& tail ) AdvancingFront::AdvancingFront( Node& head, Node& tail )
{ {
head_ = &head; head_ = &head;
@ -39,28 +38,38 @@ AdvancingFront::AdvancingFront(Node& head, Node& tail)
search_node_ = &head; search_node_ = &head;
} }
Node* AdvancingFront::LocateNode( const double& x ) Node* AdvancingFront::LocateNode( const double& x )
{ {
Node* node = search_node_; Node* node = search_node_;
if (x < node->value) { if( x < node->value )
while ((node = node->prev) != NULL) { {
if (x >= node->value) { while( (node = node->prev) != NULL )
{
if( x >= node->value )
{
search_node_ = node; search_node_ = node;
return node; return node;
} }
} }
} else { }
while ((node = node->next) != NULL) { else
if (x < node->value) { {
while( (node = node->next) != NULL )
{
if( x < node->value )
{
search_node_ = node->prev; search_node_ = node->prev;
return node->prev; return node->prev;
} }
} }
} }
return NULL; return NULL;
} }
Node* AdvancingFront::FindSearchNode( const double& x ) Node* AdvancingFront::FindSearchNode( const double& x )
{ {
(void) x; // suppress compiler warnings "unused parameter 'x'" (void) x; // suppress compiler warnings "unused parameter 'x'"
@ -68,42 +77,59 @@ Node* AdvancingFront::FindSearchNode(const double& x)
return search_node_; return search_node_;
} }
Node* AdvancingFront::LocatePoint( const Point* point ) Node* AdvancingFront::LocatePoint( const Point* point )
{ {
const double px = point->x; const double px = point->x;
Node* node = FindSearchNode( px ); Node* node = FindSearchNode( px );
const double nx = node->point->x; const double nx = node->point->x;
if (px == nx) { if( px == nx )
if (point != node->point) { {
if( point != node->point )
{
// We might have two nodes with same x value for a short time // We might have two nodes with same x value for a short time
if (point == node->prev->point) { if( point == node->prev->point )
{
node = node->prev; node = node->prev;
} else if (point == node->next->point) { }
else if( point == node->next->point )
{
node = node->next; node = node->next;
} else { }
else
{
assert( 0 ); assert( 0 );
} }
} }
} else if (px < nx) { }
while ((node = node->prev) != NULL) { else if( px < nx )
if (point == node->point) { {
while( (node = node->prev) != NULL )
{
if( point == node->point )
{
break; break;
} }
} }
} else { }
while ((node = node->next) != NULL) { else
{
while( (node = node->next) != NULL )
{
if( point == node->point ) if( point == node->point )
break; break;
} }
} }
if(node) search_node_ = node;
if( node )
search_node_ = node;
return node; return node;
} }
AdvancingFront::~AdvancingFront() AdvancingFront::~AdvancingFront()
{ {
} }
} }

View File

@ -35,11 +35,11 @@
#include "../common/shapes.h" #include "../common/shapes.h"
namespace p2t { namespace p2t {
struct Node; struct Node;
// Advancing front node // Advancing front node
struct Node { struct Node
{
Point* point; Point* point;
Triangle* triangle; Triangle* triangle;
@ -52,14 +52,15 @@ struct Node {
{ {
} }
Node(Point& p, Triangle& t) : point(&p), triangle(&t), next(NULL), prev(NULL), value(p.x) Node( Point& p, Triangle& t ) : point( &p ), triangle( &t ), next( NULL ), prev( NULL ), value(
p.x )
{ {
} }
}; };
// Advancing front // Advancing front
class AdvancingFront { class AdvancingFront
{
public: public:
AdvancingFront( Node& head, Node& tail ); AdvancingFront( Node& head, Node& tail );
@ -89,30 +90,36 @@ inline Node* AdvancingFront::head()
{ {
return head_; return head_;
} }
inline void AdvancingFront::set_head( Node* node ) inline void AdvancingFront::set_head( Node* node )
{ {
head_ = node; head_ = node;
} }
inline Node* AdvancingFront::tail() inline Node* AdvancingFront::tail()
{ {
return tail_; return tail_;
} }
inline void AdvancingFront::set_tail( Node* node ) inline void AdvancingFront::set_tail( Node* node )
{ {
tail_ = node; tail_ = node;
} }
inline Node* AdvancingFront::search() inline Node* AdvancingFront::search()
{ {
return search_node_; return search_node_;
} }
inline void AdvancingFront::set_search( Node* node ) inline void AdvancingFront::set_search( Node* node )
{ {
search_node_ = node; search_node_ = node;
} }
} }
#endif #endif

View File

@ -31,42 +31,46 @@
#include "cdt.h" #include "cdt.h"
namespace p2t { namespace p2t {
CDT::CDT( std::vector<Point*> polyline ) CDT::CDT( std::vector<Point*> polyline )
{ {
sweep_context_ = new SweepContext( polyline ); sweep_context_ = new SweepContext( polyline );
sweep_ = new Sweep; sweep_ = new Sweep;
} }
void CDT::AddHole( std::vector<Point*> polyline ) void CDT::AddHole( std::vector<Point*> polyline )
{ {
sweep_context_->AddHole( polyline ); sweep_context_->AddHole( polyline );
} }
void CDT::AddPoint(Point* point) {
void CDT::AddPoint( Point* point )
{
sweep_context_->AddPoint( point ); sweep_context_->AddPoint( point );
} }
void CDT::Triangulate() void CDT::Triangulate()
{ {
sweep_->Triangulate( *sweep_context_ ); sweep_->Triangulate( *sweep_context_ );
} }
std::vector<p2t::Triangle*> CDT::GetTriangles() std::vector<p2t::Triangle*> CDT::GetTriangles()
{ {
return sweep_context_->GetTriangles(); return sweep_context_->GetTriangles();
} }
std::list<p2t::Triangle*> CDT::GetMap() std::list<p2t::Triangle*> CDT::GetMap()
{ {
return sweep_context_->GetMap(); return sweep_context_->GetMap();
} }
CDT::~CDT() CDT::~CDT()
{ {
delete sweep_context_; delete sweep_context_;
delete sweep_; delete sweep_;
} }
} }

View File

@ -35,7 +35,6 @@
#include "../common/utils.h" #include "../common/utils.h"
namespace p2t { namespace p2t {
// Triangulate simple polygon with holes // Triangulate simple polygon with holes
void Sweep::Triangulate( SweepContext& tcx ) void Sweep::Triangulate( SweepContext& tcx )
{ {
@ -47,23 +46,30 @@ void Sweep::Triangulate(SweepContext& tcx)
FinalizationPolygon( tcx ); FinalizationPolygon( tcx );
} }
void Sweep::SweepPoints( SweepContext& tcx ) void Sweep::SweepPoints( SweepContext& tcx )
{ {
for (int i = 1; i < tcx.point_count(); i++) { for( int jj = 1; jj < tcx.point_count(); jj++ )
Point& point = *tcx.GetPoint(i); {
Point& point = *tcx.GetPoint( jj );
Node* node = &PointEvent( tcx, point ); Node* node = &PointEvent( tcx, point );
for (unsigned int i = 0; i < point.edge_list.size(); i++) {
for( unsigned int i = 0; i < point.edge_list.size(); i++ )
{
EdgeEvent( tcx, point.edge_list[i], node ); EdgeEvent( tcx, point.edge_list[i], node );
} }
} }
} }
void Sweep::FinalizationPolygon( SweepContext& tcx ) void Sweep::FinalizationPolygon( SweepContext& tcx )
{ {
// Get an Internal triangle to start with // Get an Internal triangle to start with
Triangle* t = tcx.front()->head()->next->triangle; Triangle* t = tcx.front()->head()->next->triangle;
Point* p = tcx.front()->head()->next->point; Point* p = tcx.front()->head()->next->point;
while (!t->GetConstrainedEdgeCW(*p)) {
while( !t->GetConstrainedEdgeCW( *p ) )
{
t = t->NeighborCCW( *p ); t = t->NeighborCCW( *p );
} }
@ -71,6 +77,7 @@ void Sweep::FinalizationPolygon(SweepContext& tcx)
tcx.MeshClean( *t ); tcx.MeshClean( *t );
} }
Node& Sweep::PointEvent( SweepContext& tcx, Point& point ) Node& Sweep::PointEvent( SweepContext& tcx, Point& point )
{ {
Node& node = tcx.LocateNode( point ); Node& node = tcx.LocateNode( point );
@ -78,7 +85,8 @@ Node& Sweep::PointEvent(SweepContext& tcx, Point& point)
// Only need to check +epsilon since point never have smaller // Only need to check +epsilon since point never have smaller
// x value than node due to how we fetch nodes from the front // x value than node due to how we fetch nodes from the front
if (point.x <= node.point->x + EPSILON) { if( point.x <= node.point->x + EPSILON )
{
Fill( tcx, node ); Fill( tcx, node );
} }
@ -88,12 +96,14 @@ Node& Sweep::PointEvent(SweepContext& tcx, Point& point)
return new_node; return new_node;
} }
void Sweep::EdgeEvent( SweepContext& tcx, Edge* edge, Node* node ) void Sweep::EdgeEvent( SweepContext& tcx, Edge* edge, Node* node )
{ {
tcx.edge_event.constrained_edge = edge; tcx.edge_event.constrained_edge = edge;
tcx.edge_event.right = (edge->p->x > edge->q->x); tcx.edge_event.right = (edge->p->x > edge->q->x);
if (IsEdgeSideOfTriangle(*node->triangle, *edge->p, *edge->q)) { if( IsEdgeSideOfTriangle( *node->triangle, *edge->p, *edge->q ) )
{
return; return;
} }
@ -104,76 +114,104 @@ void Sweep::EdgeEvent(SweepContext& tcx, Edge* edge, Node* node)
EdgeEvent( tcx, *edge->p, *edge->q, node->triangle, *edge->q ); EdgeEvent( tcx, *edge->p, *edge->q, node->triangle, *edge->q );
} }
void Sweep::EdgeEvent( SweepContext& tcx, Point& ep, Point& eq, Triangle* triangle, Point& point ) void Sweep::EdgeEvent( SweepContext& tcx, Point& ep, Point& eq, Triangle* triangle, Point& point )
{ {
if (IsEdgeSideOfTriangle(*triangle, ep, eq)) { if( IsEdgeSideOfTriangle( *triangle, ep, eq ) )
{
return; return;
} }
Point* p1 = triangle->PointCCW( point ); Point* p1 = triangle->PointCCW( point );
Orientation o1 = Orient2d( eq, *p1, ep ); Orientation o1 = Orient2d( eq, *p1, ep );
if (o1 == COLLINEAR) {
if( triangle->Contains(&eq, p1)) { if( o1 == COLLINEAR )
{
if( triangle->Contains( &eq, p1 ) )
{
triangle->MarkConstrainedEdge( &eq, p1 ); triangle->MarkConstrainedEdge( &eq, p1 );
// We are modifying the constraint maybe it would be better to // We are modifying the constraint maybe it would be better to
// not change the given constraint and just keep a variable for the new constraint // not change the given constraint and just keep a variable for the new constraint
tcx.edge_event.constrained_edge->q = p1; tcx.edge_event.constrained_edge->q = p1;
triangle = &triangle->NeighborAcross( point ); triangle = &triangle->NeighborAcross( point );
EdgeEvent( tcx, ep, *p1, triangle, *p1 ); EdgeEvent( tcx, ep, *p1, triangle, *p1 );
} else { }
else
{
std::runtime_error( "EdgeEvent - collinear points not supported" ); std::runtime_error( "EdgeEvent - collinear points not supported" );
assert( 0 ); assert( 0 );
} }
return; return;
} }
Point* p2 = triangle->PointCW( point ); Point* p2 = triangle->PointCW( point );
Orientation o2 = Orient2d( eq, *p2, ep ); Orientation o2 = Orient2d( eq, *p2, ep );
if (o2 == COLLINEAR) {
if( triangle->Contains(&eq, p2)) { if( o2 == COLLINEAR )
{
if( triangle->Contains( &eq, p2 ) )
{
triangle->MarkConstrainedEdge( &eq, p2 ); triangle->MarkConstrainedEdge( &eq, p2 );
// We are modifying the constraint maybe it would be better to // We are modifying the constraint maybe it would be better to
// not change the given constraint and just keep a variable for the new constraint // not change the given constraint and just keep a variable for the new constraint
tcx.edge_event.constrained_edge->q = p2; tcx.edge_event.constrained_edge->q = p2;
triangle = &triangle->NeighborAcross( point ); triangle = &triangle->NeighborAcross( point );
EdgeEvent( tcx, ep, *p2, triangle, *p2 ); EdgeEvent( tcx, ep, *p2, triangle, *p2 );
} else { }
else
{
std::runtime_error( "EdgeEvent - collinear points not supported" ); std::runtime_error( "EdgeEvent - collinear points not supported" );
assert( 0 ); assert( 0 );
} }
return; return;
} }
if (o1 == o2) { if( o1 == o2 )
{
// Need to decide if we are rotating CW or CCW to get to a triangle // Need to decide if we are rotating CW or CCW to get to a triangle
// that will cross edge // that will cross edge
if (o1 == CW) { if( o1 == CW )
{
triangle = triangle->NeighborCCW( point ); triangle = triangle->NeighborCCW( point );
} else{ }
else
{
triangle = triangle->NeighborCW( point ); triangle = triangle->NeighborCW( point );
} }
EdgeEvent( tcx, ep, eq, triangle, point ); EdgeEvent( tcx, ep, eq, triangle, point );
} else { }
else
{
// This triangle crosses constraint so lets flippin start! // This triangle crosses constraint so lets flippin start!
FlipEdgeEvent( tcx, ep, eq, triangle, point ); FlipEdgeEvent( tcx, ep, eq, triangle, point );
} }
} }
bool Sweep::IsEdgeSideOfTriangle( Triangle& triangle, Point& ep, Point& eq ) bool Sweep::IsEdgeSideOfTriangle( Triangle& triangle, Point& ep, Point& eq )
{ {
int index = triangle.EdgeIndex( &ep, &eq ); int index = triangle.EdgeIndex( &ep, &eq );
if (index != -1) { if( index != -1 )
{
triangle.MarkConstrainedEdge( index ); triangle.MarkConstrainedEdge( index );
Triangle* t = triangle.GetNeighbor( index ); Triangle* t = triangle.GetNeighbor( index );
if (t) {
if( t )
{
t->MarkConstrainedEdge( &ep, &eq ); t->MarkConstrainedEdge( &ep, &eq );
} }
return true; return true;
} }
return false; return false;
} }
Node& Sweep::NewFrontTriangle( SweepContext& tcx, Point& point, Node& node ) Node& Sweep::NewFrontTriangle( SweepContext& tcx, Point& point, Node& node )
{ {
Triangle* triangle = new Triangle( point, *node.point, *node.next->point ); Triangle* triangle = new Triangle( point, *node.point, *node.next->point );
@ -189,13 +227,15 @@ Node& Sweep::NewFrontTriangle(SweepContext& tcx, Point& point, Node& node)
node.next->prev = new_node; node.next->prev = new_node;
node.next = new_node; node.next = new_node;
if (!Legalize(tcx, *triangle)) { if( !Legalize( tcx, *triangle ) )
{
tcx.MapTriangleToNodes( *triangle ); tcx.MapTriangleToNodes( *triangle );
} }
return *new_node; return *new_node;
} }
void Sweep::Fill( SweepContext& tcx, Node& node ) void Sweep::Fill( SweepContext& tcx, Node& node )
{ {
Triangle* triangle = new Triangle( *node.prev->point, *node.point, *node.next->point ); Triangle* triangle = new Triangle( *node.prev->point, *node.point, *node.next->point );
@ -212,21 +252,24 @@ void Sweep::Fill(SweepContext& tcx, Node& node)
node.next->prev = node.prev; node.next->prev = node.prev;
// If it was legalized the triangle has already been mapped // If it was legalized the triangle has already been mapped
if (!Legalize(tcx, *triangle)) { if( !Legalize( tcx, *triangle ) )
{
tcx.MapTriangleToNodes( *triangle ); tcx.MapTriangleToNodes( *triangle );
} }
} }
void Sweep::FillAdvancingFront( SweepContext& tcx, Node& n ) void Sweep::FillAdvancingFront( SweepContext& tcx, Node& n )
{ {
// Fill right holes // Fill right holes
Node* node = n.next; Node* node = n.next;
while (node->next) { while( node->next )
{
// if HoleAngle exceeds 90 degrees then break. // if HoleAngle exceeds 90 degrees then break.
if (LargeHole_DontFill(node)) break; if( LargeHole_DontFill( node ) )
break;
Fill( tcx, *node ); Fill( tcx, *node );
node = node->next; node = node->next;
} }
@ -234,57 +277,79 @@ void Sweep::FillAdvancingFront(SweepContext& tcx, Node& n)
// Fill left holes // Fill left holes
node = n.prev; node = n.prev;
while (node->prev) { while( node->prev )
{
// if HoleAngle exceeds 90 degrees then break. // if HoleAngle exceeds 90 degrees then break.
if (LargeHole_DontFill(node)) break; if( LargeHole_DontFill( node ) )
break;
Fill( tcx, *node ); Fill( tcx, *node );
node = node->prev; node = node->prev;
} }
// Fill right basins // Fill right basins
if (n.next && n.next->next) { if( n.next && n.next->next )
{
double angle = BasinAngle( n ); double angle = BasinAngle( n );
if (angle < PI_3div4) {
if( angle < PI_3div4 )
{
FillBasin( tcx, n ); FillBasin( tcx, n );
} }
} }
} }
// True if HoleAngle exceeds 90 degrees.
bool Sweep::LargeHole_DontFill(Node* node) {
// True if HoleAngle exceeds 90 degrees.
bool Sweep::LargeHole_DontFill( Node* node )
{
Node* nextNode = node->next; Node* nextNode = node->next;
Node* prevNode = node->prev; Node* prevNode = node->prev;
if( !AngleExceeds90Degrees( node->point, nextNode->point, prevNode->point ) ) if( !AngleExceeds90Degrees( node->point, nextNode->point, prevNode->point ) )
return false; return false;
// Check additional points on front. // Check additional points on front.
Node* next2Node = nextNode->next; Node* next2Node = nextNode->next;
// "..Plus.." because only want angles on same side as point being added. // "..Plus.." because only want angles on same side as point being added.
if ((next2Node != NULL) && !AngleExceedsPlus90DegreesOrIsNegative(node->point, next2Node->point, prevNode->point)) if( (next2Node != NULL)
&& !AngleExceedsPlus90DegreesOrIsNegative( node->point, next2Node->point,
prevNode->point ) )
return false; return false;
Node* prev2Node = prevNode->prev; Node* prev2Node = prevNode->prev;
// "..Plus.." because only want angles on same side as point being added. // "..Plus.." because only want angles on same side as point being added.
if ((prev2Node != NULL) && !AngleExceedsPlus90DegreesOrIsNegative(node->point, nextNode->point, prev2Node->point)) if( (prev2Node != NULL)
&& !AngleExceedsPlus90DegreesOrIsNegative( node->point, nextNode->point,
prev2Node->point ) )
return false; return false;
return true; return true;
} }
bool Sweep::AngleExceeds90Degrees(Point* origin, Point* pa, Point* pb) {
bool Sweep::AngleExceeds90Degrees( Point* origin, Point* pa, Point* pb )
{
double angle = Angle( *origin, *pa, *pb ); double angle = Angle( *origin, *pa, *pb );
bool exceeds90Degrees = ( (angle > PI_div2) || (angle < -PI_div2) ); bool exceeds90Degrees = ( (angle > PI_div2) || (angle < -PI_div2) );
return exceeds90Degrees; return exceeds90Degrees;
} }
bool Sweep::AngleExceedsPlus90DegreesOrIsNegative(Point* origin, Point* pa, Point* pb) {
bool Sweep::AngleExceedsPlus90DegreesOrIsNegative( Point* origin, Point* pa, Point* pb )
{
double angle = Angle( *origin, *pa, *pb ); double angle = Angle( *origin, *pa, *pb );
bool exceedsPlus90DegreesOrIsNegative = (angle > PI_div2) || (angle < 0); bool exceedsPlus90DegreesOrIsNegative = (angle > PI_div2) || (angle < 0);
return exceedsPlus90DegreesOrIsNegative; return exceedsPlus90DegreesOrIsNegative;
} }
double Sweep::Angle(Point& origin, Point& pa, Point& pb) {
double Sweep::Angle( Point& origin, Point& pa, Point& pb )
{
/* Complex plane /* Complex plane
* ab = cosA +i*sinA * ab = cosA +i*sinA
* ab = (ax + ay*i)(bx + by*i) = (ax*bx + ay*by) + i(ax*by-ay*bx) * ab = (ax + ay*i)(bx + by*i) = (ax*bx + ay*by) + i(ax*by-ay*bx)
@ -302,16 +367,20 @@ double Sweep::Angle(Point& origin, Point& pa, Point& pb) {
double x = ax * by - ay * bx; double x = ax * by - ay * bx;
double y = ax * bx + ay * by; double y = ax * bx + ay * by;
double angle = atan2( x, y ); double angle = atan2( x, y );
return angle; return angle;
} }
double Sweep::BasinAngle( Node& node ) double Sweep::BasinAngle( Node& node )
{ {
double ax = node.point->x - node.next->next->point->x; double ax = node.point->x - node.next->next->point->x;
double ay = node.point->y - node.next->next->point->y; double ay = node.point->y - node.next->next->point->y;
return atan2( ay, ax ); return atan2( ay, ax );
} }
double Sweep::HoleAngle( Node& node ) double Sweep::HoleAngle( Node& node )
{ {
/* Complex plane /* Complex plane
@ -326,34 +395,40 @@ double Sweep::HoleAngle(Node& node)
double ay = node.next->point->y - node.point->y; double ay = node.next->point->y - node.point->y;
double bx = node.prev->point->x - node.point->x; double bx = node.prev->point->x - node.point->x;
double by = node.prev->point->y - node.point->y; 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 );
} }
bool Sweep::Legalize( SweepContext& tcx, Triangle& t ) bool Sweep::Legalize( SweepContext& tcx, Triangle& t )
{ {
// To legalize a triangle we start by finding if any of the three edges // To legalize a triangle we start by finding if any of the three edges
// violate the Delaunay condition // violate the Delaunay condition
for (int i = 0; i < 3; i++) { for( int i = 0; i < 3; i++ )
{
if( t.delaunay_edge[i] ) if( t.delaunay_edge[i] )
continue; continue;
Triangle* ot = t.GetNeighbor( i ); Triangle* ot = t.GetNeighbor( i );
if (ot) { if( ot )
{
Point* p = t.GetPoint( i ); Point* p = t.GetPoint( i );
Point* op = ot->OppositePoint( t, *p ); Point* op = ot->OppositePoint( t, *p );
int oi = ot->Index( op ); int oi = ot->Index( op );
// If this is a Constrained Edge or a Delaunay Edge(only during recursive legalization) // If this is a Constrained Edge or a Delaunay Edge(only during recursive legalization)
// then we should not try to legalize // then we should not try to legalize
if (ot->constrained_edge[oi] || ot->delaunay_edge[oi]) { if( ot->constrained_edge[oi] || ot->delaunay_edge[oi] )
{
t.constrained_edge[i] = ot->constrained_edge[oi]; t.constrained_edge[i] = ot->constrained_edge[oi];
continue; continue;
} }
bool inside = Incircle( *p, *t.PointCCW( *p ), *t.PointCW( *p ), *op ); bool inside = Incircle( *p, *t.PointCCW( *p ), *t.PointCW( *p ), *op );
if (inside) { if( inside )
{
// Lets mark this shared edge as Delaunay // Lets mark this shared edge as Delaunay
t.delaunay_edge[i] = true; t.delaunay_edge[i] = true;
ot->delaunay_edge[oi] = true; ot->delaunay_edge[oi] = true;
@ -366,11 +441,14 @@ bool Sweep::Legalize(SweepContext& tcx, Triangle& t)
// Make sure that triangle to node mapping is done only one time for a specific triangle // Make sure that triangle to node mapping is done only one time for a specific triangle
bool not_legalized = !Legalize( tcx, t ); bool not_legalized = !Legalize( tcx, t );
if (not_legalized) {
if( not_legalized )
{
tcx.MapTriangleToNodes( t ); tcx.MapTriangleToNodes( t );
} }
not_legalized = !Legalize( tcx, *ot ); not_legalized = !Legalize( tcx, *ot );
if( not_legalized ) if( not_legalized )
tcx.MapTriangleToNodes( *ot ); tcx.MapTriangleToNodes( *ot );
@ -387,9 +465,11 @@ bool Sweep::Legalize(SweepContext& tcx, Triangle& t)
} }
} }
} }
return false; return false;
} }
bool Sweep::Incircle( Point& pa, Point& pb, Point& pc, Point& pd ) bool Sweep::Incircle( Point& pa, Point& pb, Point& pc, Point& pd )
{ {
double adx = pa.x - pd.x; double adx = pa.x - pd.x;
@ -426,9 +506,11 @@ bool Sweep::Incircle(Point& pa, Point& pb, Point& pc, Point& pd)
return det > 0; return det > 0;
} }
void Sweep::RotateTrianglePair( Triangle& t, Point& p, Triangle& ot, Point& op ) void Sweep::RotateTrianglePair( Triangle& t, Point& p, Triangle& ot, Point& op )
{ {
Triangle* n1, * n2, * n3, * n4; Triangle* n1, * n2, * n3, * n4;
n1 = t.NeighborCCW( p ); n1 = t.NeighborCCW( p );
n2 = t.NeighborCW( p ); n2 = t.NeighborCW( p );
n3 = ot.NeighborCCW( op ); n3 = ot.NeighborCCW( op );
@ -468,38 +550,59 @@ void Sweep::RotateTrianglePair(Triangle& t, Point& p, Triangle& ot, Point& op)
// the right side. // the right side.
t.ClearNeighbors(); t.ClearNeighbors();
ot.ClearNeighbors(); ot.ClearNeighbors();
if (n1) ot.MarkNeighbor(*n1);
if (n2) t.MarkNeighbor(*n2); if( n1 )
if (n3) t.MarkNeighbor(*n3); ot.MarkNeighbor( *n1 );
if (n4) ot.MarkNeighbor(*n4);
if( n2 )
t.MarkNeighbor( *n2 );
if( n3 )
t.MarkNeighbor( *n3 );
if( n4 )
ot.MarkNeighbor( *n4 );
t.MarkNeighbor( ot ); t.MarkNeighbor( ot );
} }
void Sweep::FillBasin( SweepContext& tcx, Node& node ) void Sweep::FillBasin( SweepContext& tcx, Node& node )
{ {
if (Orient2d(*node.point, *node.next->point, *node.next->next->point) == CCW) { if( Orient2d( *node.point, *node.next->point, *node.next->next->point ) == CCW )
{
tcx.basin.left_node = node.next->next; tcx.basin.left_node = node.next->next;
} else { }
else
{
tcx.basin.left_node = node.next; tcx.basin.left_node = node.next;
} }
// Find the bottom and right node // Find the bottom and right node
tcx.basin.bottom_node = tcx.basin.left_node; tcx.basin.bottom_node = tcx.basin.left_node;
while( tcx.basin.bottom_node->next while( tcx.basin.bottom_node->next
&& tcx.basin.bottom_node->point->y >= tcx.basin.bottom_node->next->point->y) { && tcx.basin.bottom_node->point->y >= tcx.basin.bottom_node->next->point->y )
{
tcx.basin.bottom_node = tcx.basin.bottom_node->next; tcx.basin.bottom_node = tcx.basin.bottom_node->next;
} }
if (tcx.basin.bottom_node == tcx.basin.left_node) {
if( tcx.basin.bottom_node == tcx.basin.left_node )
{
// No valid basin // No valid basin
return; return;
} }
tcx.basin.right_node = tcx.basin.bottom_node; tcx.basin.right_node = tcx.basin.bottom_node;
while( tcx.basin.right_node->next while( tcx.basin.right_node->next
&& tcx.basin.right_node->point->y < tcx.basin.right_node->next->point->y) { && tcx.basin.right_node->point->y < tcx.basin.right_node->next->point->y )
{
tcx.basin.right_node = tcx.basin.right_node->next; tcx.basin.right_node = tcx.basin.right_node->next;
} }
if (tcx.basin.right_node == tcx.basin.bottom_node) {
if( tcx.basin.right_node == tcx.basin.bottom_node )
{
// No valid basins // No valid basins
return; return;
} }
@ -510,34 +613,52 @@ void Sweep::FillBasin(SweepContext& tcx, Node& node)
FillBasinReq( tcx, tcx.basin.bottom_node ); FillBasinReq( tcx, tcx.basin.bottom_node );
} }
void Sweep::FillBasinReq( SweepContext& tcx, Node* node ) void Sweep::FillBasinReq( SweepContext& tcx, Node* node )
{ {
// if shallow stop filling // if shallow stop filling
if (IsShallow(tcx, *node)) { if( IsShallow( tcx, *node ) )
{
return; return;
} }
Fill( tcx, *node ); Fill( tcx, *node );
if (node->prev == tcx.basin.left_node && node->next == tcx.basin.right_node) { if( node->prev == tcx.basin.left_node && node->next == tcx.basin.right_node )
{
return; return;
} else if (node->prev == tcx.basin.left_node) { }
else if( node->prev == tcx.basin.left_node )
{
Orientation o = Orient2d( *node->point, *node->next->point, *node->next->next->point ); Orientation o = Orient2d( *node->point, *node->next->point, *node->next->next->point );
if (o == CW) {
if( o == CW )
{
return; return;
} }
node = node->next; node = node->next;
} else if (node->next == tcx.basin.right_node) { }
else if( node->next == tcx.basin.right_node )
{
Orientation o = Orient2d( *node->point, *node->prev->point, *node->prev->prev->point ); Orientation o = Orient2d( *node->point, *node->prev->point, *node->prev->prev->point );
if (o == CCW) {
if( o == CCW )
{
return; return;
} }
node = node->prev; node = node->prev;
} else { }
else
{
// Continue with the neighbor node with lowest Y value // Continue with the neighbor node with lowest Y value
if (node->prev->point->y < node->next->point->y) { if( node->prev->point->y < node->next->point->y )
{
node = node->prev; node = node->prev;
} else { }
else
{
node = node->next; node = node->next;
} }
} }
@ -545,51 +666,71 @@ void Sweep::FillBasinReq(SweepContext& tcx, Node* node)
FillBasinReq( tcx, node ); FillBasinReq( tcx, node );
} }
bool Sweep::IsShallow( SweepContext& tcx, Node& node ) bool Sweep::IsShallow( SweepContext& tcx, Node& node )
{ {
double height; double height;
if (tcx.basin.left_highest) { if( tcx.basin.left_highest )
{
height = tcx.basin.left_node->point->y - node.point->y; height = tcx.basin.left_node->point->y - node.point->y;
} else { }
else
{
height = tcx.basin.right_node->point->y - node.point->y; height = tcx.basin.right_node->point->y - node.point->y;
} }
// if shallow stop filling // if shallow stop filling
if (tcx.basin.width > height) { if( tcx.basin.width > height )
{
return true; return true;
} }
return false; return false;
} }
void Sweep::FillEdgeEvent( SweepContext& tcx, Edge* edge, Node* node ) void Sweep::FillEdgeEvent( SweepContext& tcx, Edge* edge, Node* node )
{ {
if (tcx.edge_event.right) { if( tcx.edge_event.right )
{
FillRightAboveEdgeEvent( tcx, edge, node ); FillRightAboveEdgeEvent( tcx, edge, node );
} else { }
else
{
FillLeftAboveEdgeEvent( tcx, edge, node ); FillLeftAboveEdgeEvent( tcx, edge, node );
} }
} }
void Sweep::FillRightAboveEdgeEvent( SweepContext& tcx, Edge* edge, Node* node ) void Sweep::FillRightAboveEdgeEvent( SweepContext& tcx, Edge* edge, Node* node )
{ {
while (node->next->point->x < edge->p->x) { while( node->next->point->x < edge->p->x )
{
// Check if next node is below the edge // Check if next node is below the edge
if (Orient2d(*edge->q, *node->next->point, *edge->p) == CCW) { if( Orient2d( *edge->q, *node->next->point, *edge->p ) == CCW )
{
FillRightBelowEdgeEvent( tcx, edge, *node ); FillRightBelowEdgeEvent( tcx, edge, *node );
} else { }
else
{
node = node->next; node = node->next;
} }
} }
} }
void Sweep::FillRightBelowEdgeEvent( SweepContext& tcx, Edge* edge, Node& node ) void Sweep::FillRightBelowEdgeEvent( SweepContext& tcx, Edge* edge, Node& node )
{ {
if (node.point->x < edge->p->x) { if( node.point->x < edge->p->x )
if (Orient2d(*node.point, *node.next->point, *node.next->next->point) == CCW) { {
if( Orient2d( *node.point, *node.next->point, *node.next->next->point ) == CCW )
{
// Concave // Concave
FillRightConcaveEdgeEvent( tcx, edge, node ); FillRightConcaveEdgeEvent( tcx, edge, node );
} else{ }
else
{
// Convex // Convex
FillRightConvexEdgeEvent( tcx, edge, node ); FillRightConvexEdgeEvent( tcx, edge, node );
// Retry this one // Retry this one
@ -598,61 +739,85 @@ void Sweep::FillRightBelowEdgeEvent(SweepContext& tcx, Edge* edge, Node& node)
} }
} }
void Sweep::FillRightConcaveEdgeEvent( SweepContext& tcx, Edge* edge, Node& node ) void Sweep::FillRightConcaveEdgeEvent( SweepContext& tcx, Edge* edge, Node& node )
{ {
Fill( tcx, *node.next ); Fill( tcx, *node.next );
if (node.next->point != edge->p) {
if( node.next->point != edge->p )
{
// Next above or below edge? // Next above or below edge?
if (Orient2d(*edge->q, *node.next->point, *edge->p) == CCW) { if( Orient2d( *edge->q, *node.next->point, *edge->p ) == CCW )
{
// Below // Below
if (Orient2d(*node.point, *node.next->point, *node.next->next->point) == CCW) { if( Orient2d( *node.point, *node.next->point, *node.next->next->point ) == CCW )
{
// Next is concave // Next is concave
FillRightConcaveEdgeEvent( tcx, edge, node ); FillRightConcaveEdgeEvent( tcx, edge, node );
} else { }
else
{
// Next is convex // Next is convex
} }
} }
} }
} }
void Sweep::FillRightConvexEdgeEvent( 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) { if( Orient2d( *node.next->point, *node.next->next->point,
*node.next->next->next->point ) == CCW )
{
// Concave // Concave
FillRightConcaveEdgeEvent( tcx, edge, *node.next ); FillRightConcaveEdgeEvent( tcx, edge, *node.next );
} else{ }
else
{
// Convex // Convex
// Next above or below edge? // Next above or below edge?
if (Orient2d(*edge->q, *node.next->next->point, *edge->p) == CCW) { if( Orient2d( *edge->q, *node.next->next->point, *edge->p ) == CCW )
{
// Below // Below
FillRightConvexEdgeEvent( tcx, edge, *node.next ); FillRightConvexEdgeEvent( tcx, edge, *node.next );
} else{ }
else
{
// Above // Above
} }
} }
} }
void Sweep::FillLeftAboveEdgeEvent( SweepContext& tcx, Edge* edge, Node* node ) void Sweep::FillLeftAboveEdgeEvent( SweepContext& tcx, Edge* edge, Node* node )
{ {
while (node->prev->point->x > edge->p->x) { while( node->prev->point->x > edge->p->x )
{
// Check if next node is below the edge // Check if next node is below the edge
if (Orient2d(*edge->q, *node->prev->point, *edge->p) == CW) { if( Orient2d( *edge->q, *node->prev->point, *edge->p ) == CW )
{
FillLeftBelowEdgeEvent( tcx, edge, *node ); FillLeftBelowEdgeEvent( tcx, edge, *node );
} else { }
else
{
node = node->prev; node = node->prev;
} }
} }
} }
void Sweep::FillLeftBelowEdgeEvent( SweepContext& tcx, Edge* edge, Node& node ) void Sweep::FillLeftBelowEdgeEvent( SweepContext& tcx, Edge* edge, Node& node )
{ {
if (node.point->x > edge->p->x) { if( node.point->x > edge->p->x )
if (Orient2d(*node.point, *node.prev->point, *node.prev->prev->point) == CW) { {
if( Orient2d( *node.point, *node.prev->point, *node.prev->prev->point ) == CW )
{
// Concave // Concave
FillLeftConcaveEdgeEvent( tcx, edge, node ); FillLeftConcaveEdgeEvent( tcx, edge, node );
} else { }
else
{
// Convex // Convex
FillLeftConvexEdgeEvent( tcx, edge, node ); FillLeftConvexEdgeEvent( tcx, edge, node );
// Retry this one // Retry this one
@ -661,84 +826,117 @@ void Sweep::FillLeftBelowEdgeEvent(SweepContext& tcx, Edge* edge, Node& node)
} }
} }
void Sweep::FillLeftConvexEdgeEvent( SweepContext& tcx, Edge* edge, Node& node ) void Sweep::FillLeftConvexEdgeEvent( SweepContext& tcx, Edge* edge, Node& node )
{ {
// Next concave or convex? // Next concave or convex?
if (Orient2d(*node.prev->point, *node.prev->prev->point, *node.prev->prev->prev->point) == CW) { if( Orient2d( *node.prev->point, *node.prev->prev->point,
*node.prev->prev->prev->point ) == CW )
{
// Concave // Concave
FillLeftConcaveEdgeEvent( tcx, edge, *node.prev ); FillLeftConcaveEdgeEvent( tcx, edge, *node.prev );
} else{ }
else
{
// Convex // Convex
// Next above or below edge? // Next above or below edge?
if (Orient2d(*edge->q, *node.prev->prev->point, *edge->p) == CW) { if( Orient2d( *edge->q, *node.prev->prev->point, *edge->p ) == CW )
{
// Below // Below
FillLeftConvexEdgeEvent( tcx, edge, *node.prev ); FillLeftConvexEdgeEvent( tcx, edge, *node.prev );
} else{ }
else
{
// Above // Above
} }
} }
} }
void Sweep::FillLeftConcaveEdgeEvent( SweepContext& tcx, Edge* edge, Node& node ) void Sweep::FillLeftConcaveEdgeEvent( SweepContext& tcx, Edge* edge, Node& node )
{ {
Fill( tcx, *node.prev ); Fill( tcx, *node.prev );
if (node.prev->point != edge->p) {
if( node.prev->point != edge->p )
{
// Next above or below edge? // Next above or below edge?
if (Orient2d(*edge->q, *node.prev->point, *edge->p) == CW) { if( Orient2d( *edge->q, *node.prev->point, *edge->p ) == CW )
{
// Below // Below
if (Orient2d(*node.point, *node.prev->point, *node.prev->prev->point) == CW) { if( Orient2d( *node.point, *node.prev->point, *node.prev->prev->point ) == CW )
{
// Next is concave // Next is concave
FillLeftConcaveEdgeEvent( tcx, edge, node ); FillLeftConcaveEdgeEvent( tcx, edge, node );
} else{ }
else
{
// Next is convex // Next is convex
} }
} }
} }
} }
void Sweep::FlipEdgeEvent( SweepContext& tcx, Point& ep, Point& eq, Triangle* t, Point& p ) void Sweep::FlipEdgeEvent( SweepContext& tcx, Point& ep, Point& eq, Triangle* t, Point& p )
{ {
Triangle& ot = t->NeighborAcross( p ); Triangle& ot = t->NeighborAcross( p );
Point& op = *ot.OppositePoint( *t, p ); Point& op = *ot.OppositePoint( *t, p );
if (&ot == NULL) { if( &ot == NULL )
{
// If we want to integrate the fillEdgeEvent do it here // If we want to integrate the fillEdgeEvent do it here
// With current implementation we should never get here // With current implementation we should never get here
// throw new RuntimeException( "[BUG:FIXME] FLIP failed due to missing triangle"); // throw new RuntimeException( "[BUG:FIXME] FLIP failed due to missing triangle");
assert( 0 ); assert( 0 );
} }
if (InScanArea(p, *t->PointCCW(p), *t->PointCW(p), op)) { if( InScanArea( p, *t->PointCCW( p ), *t->PointCW( p ), op ) )
{
// Lets rotate shared edge one vertex CW // Lets rotate shared edge one vertex CW
RotateTrianglePair( *t, p, ot, op ); RotateTrianglePair( *t, p, ot, op );
tcx.MapTriangleToNodes( *t ); tcx.MapTriangleToNodes( *t );
tcx.MapTriangleToNodes( ot ); tcx.MapTriangleToNodes( ot );
if (p == eq && op == ep) { if( p == eq && op == ep )
if (eq == *tcx.edge_event.constrained_edge->q && ep == *tcx.edge_event.constrained_edge->p) { {
if( eq == *tcx.edge_event.constrained_edge->q
&& ep == *tcx.edge_event.constrained_edge->p )
{
t->MarkConstrainedEdge( &ep, &eq ); t->MarkConstrainedEdge( &ep, &eq );
ot.MarkConstrainedEdge( &ep, &eq ); ot.MarkConstrainedEdge( &ep, &eq );
Legalize( tcx, *t ); Legalize( tcx, *t );
Legalize( tcx, ot ); Legalize( tcx, ot );
} else { }
else
{
// XXX: I think one of the triangles should be legalized here? // XXX: I think one of the triangles should be legalized here?
} }
} else { }
else
{
Orientation o = Orient2d( eq, op, ep ); Orientation o = Orient2d( eq, op, ep );
t = &NextFlipTriangle( tcx, (int) o, *t, ot, p, op ); t = &NextFlipTriangle( tcx, (int) o, *t, ot, p, op );
FlipEdgeEvent( tcx, ep, eq, t, p ); FlipEdgeEvent( tcx, ep, eq, t, p );
} }
} else { }
else
{
Point& newP = NextFlipPoint( ep, eq, ot, op ); Point& newP = NextFlipPoint( ep, eq, ot, op );
FlipScanEdgeEvent( tcx, ep, eq, *t, ot, newP ); FlipScanEdgeEvent( tcx, ep, eq, *t, ot, newP );
EdgeEvent( tcx, ep, eq, t, p ); EdgeEvent( tcx, ep, eq, t, p );
} }
} }
Triangle& Sweep::NextFlipTriangle(SweepContext& tcx, int o, Triangle& t, Triangle& ot, Point& p, Point& op)
Triangle& Sweep::NextFlipTriangle( SweepContext& tcx,
int o,
Triangle& t,
Triangle& ot,
Point& p,
Point& op )
{
if( o == CCW )
{ {
if (o == CCW) {
// ot is not crossing edge after flip // ot is not crossing edge after flip
int edge_index = ot.EdgeIndex( &p, &op ); int edge_index = ot.EdgeIndex( &p, &op );
ot.delaunay_edge[edge_index] = true; ot.delaunay_edge[edge_index] = true;
@ -756,13 +954,18 @@ Triangle& Sweep::NextFlipTriangle(SweepContext& tcx, int o, Triangle& t, Triangl
return ot; 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 ); Orientation o2d = Orient2d( eq, op, ep );
if (o2d == CW) {
if( o2d == CW )
{
// Right // Right
return *ot.PointCCW( op ); return *ot.PointCCW( op );
} else if (o2d == CCW) { }
else if( o2d == CCW )
{
// Left // Left
return *ot.PointCW( op ); return *ot.PointCW( op );
} }
@ -774,20 +977,23 @@ Point& Sweep::NextFlipPoint(Point& ep, Point& eq, Triangle& ot, Point& op)
return ep; return ep;
} }
void Sweep::FlipScanEdgeEvent( SweepContext& tcx, Point& ep, Point& eq, Triangle& flip_triangle, void Sweep::FlipScanEdgeEvent( SweepContext& tcx, Point& ep, Point& eq, Triangle& flip_triangle,
Triangle& t, Point& p ) Triangle& t, Point& p )
{ {
Triangle& ot = t.NeighborAcross( p ); Triangle& ot = t.NeighborAcross( p );
Point& op = *ot.OppositePoint( t, p ); Point& op = *ot.OppositePoint( t, p );
if (&t.NeighborAcross(p) == NULL) { if( &t.NeighborAcross( p ) == NULL )
{
// If we want to integrate the fillEdgeEvent do it here // If we want to integrate the fillEdgeEvent do it here
// With current implementation we should never get here // With current implementation we should never get here
// throw new RuntimeException( "[BUG:FIXME] FLIP failed due to missing triangle"); // throw new RuntimeException( "[BUG:FIXME] FLIP failed due to missing triangle");
assert( 0 ); assert( 0 );
} }
if (InScanArea(eq, *flip_triangle.PointCCW(eq), *flip_triangle.PointCW(eq), op)) { if( InScanArea( eq, *flip_triangle.PointCCW( eq ), *flip_triangle.PointCW( eq ), op ) )
{
// flip with new edge op->eq // flip with new edge op->eq
FlipEdgeEvent( tcx, eq, op, &ot, op ); FlipEdgeEvent( tcx, eq, op, &ot, op );
// TODO: Actually I just figured out that it should be possible to // TODO: Actually I just figured out that it should be possible to
@ -797,21 +1003,21 @@ void Sweep::FlipScanEdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle&
// also need to set a new flip_triangle first // also need to set a new flip_triangle first
// Turns out at first glance that this is somewhat complicated // Turns out at first glance that this is somewhat complicated
// so it will have to wait. // so it will have to wait.
} else{ }
else
{
Point& newP = NextFlipPoint( ep, eq, ot, op ); Point& newP = NextFlipPoint( ep, eq, ot, op );
FlipScanEdgeEvent( tcx, ep, eq, flip_triangle, ot, newP ); FlipScanEdgeEvent( tcx, ep, eq, flip_triangle, ot, newP );
} }
} }
Sweep::~Sweep() {
Sweep::~Sweep()
{
// Clean up memory // Clean up memory
for( unsigned i = 0; i < nodes_.size(); i++ ) for( unsigned i = 0; i < nodes_.size(); i++ )
{ {
delete nodes_[i]; delete nodes_[i];
} }
} }
} }

View File

@ -33,7 +33,6 @@
#include "advancing_front.h" #include "advancing_front.h"
namespace p2t { namespace p2t {
SweepContext::SweepContext( std::vector<Point*> polyline ) : SweepContext::SweepContext( std::vector<Point*> polyline ) :
front_( 0 ), front_( 0 ),
head_( 0 ), head_( 0 ),
@ -50,42 +49,55 @@ SweepContext::SweepContext(std::vector<Point*> polyline) :
InitEdges( points_ ); InitEdges( points_ );
} }
void SweepContext::AddHole( std::vector<Point*> polyline ) void SweepContext::AddHole( std::vector<Point*> polyline )
{ {
InitEdges( polyline ); InitEdges( polyline );
for(unsigned int i = 0; i < polyline.size(); i++) {
for( unsigned int i = 0; i < polyline.size(); i++ )
{
points_.push_back( polyline[i] ); points_.push_back( polyline[i] );
} }
} }
void SweepContext::AddPoint(Point* point) {
void SweepContext::AddPoint( Point* point )
{
points_.push_back( point ); points_.push_back( point );
} }
std::vector<Triangle*> SweepContext::GetTriangles() std::vector<Triangle*> SweepContext::GetTriangles()
{ {
return triangles_; return triangles_;
} }
std::list<Triangle*> SweepContext::GetMap() std::list<Triangle*> SweepContext::GetMap()
{ {
return map_; return map_;
} }
void SweepContext::InitTriangulation() void SweepContext::InitTriangulation()
{ {
double xmax( points_[0]->x ), xmin( points_[0]->x ); double xmax( points_[0]->x ), xmin( points_[0]->x );
double ymax( points_[0]->y ), ymin( points_[0]->y ); double ymax( points_[0]->y ), ymin( points_[0]->y );
// Calculate bounds. // Calculate bounds.
for (unsigned int i = 0; i < points_.size(); i++) { for( unsigned 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 )
xmin = p.x; xmin = p.x;
if( p.y > ymax ) if( p.y > ymax )
ymax = p.y; ymax = p.y;
if( p.y < ymin ) if( p.y < ymin )
ymin = p.y; ymin = p.y;
} }
@ -97,37 +109,42 @@ void SweepContext::InitTriangulation()
// Sort points along y-axis // Sort points along y-axis
std::sort( points_.begin(), points_.end(), cmp ); std::sort( points_.begin(), points_.end(), cmp );
} }
void SweepContext::InitEdges( std::vector<Point*> polyline ) void SweepContext::InitEdges( std::vector<Point*> polyline )
{ {
int num_points = polyline.size(); int num_points = polyline.size();
for (int i = 0; i < num_points; i++) {
for( int i = 0; i < num_points; i++ )
{
int j = i < num_points - 1 ? i + 1 : 0; 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] ) );
} }
} }
Point* SweepContext::GetPoint( const int& index ) Point* SweepContext::GetPoint( const int& index )
{ {
return points_[index]; return points_[index];
} }
void SweepContext::AddToMap( Triangle* triangle ) void SweepContext::AddToMap( Triangle* triangle )
{ {
map_.push_back( triangle ); map_.push_back( triangle );
} }
Node& SweepContext::LocateNode( Point& point ) Node& SweepContext::LocateNode( Point& point )
{ {
// TODO implement search tree // TODO implement search tree
return *front_->LocateNode( point.x ); return *front_->LocateNode( point.x );
} }
void SweepContext::CreateAdvancingFront( std::vector<Node*> nodes ) void SweepContext::CreateAdvancingFront( std::vector<Node*> nodes )
{ {
(void) nodes; (void) nodes;
// Initial triangle // Initial triangle
Triangle* triangle = new Triangle( *points_[0], *tail_, *head_ ); Triangle* triangle = new Triangle( *points_[0], *tail_, *head_ );
@ -147,40 +164,52 @@ void SweepContext::CreateAdvancingFront(std::vector<Node*> nodes)
af_tail_->prev = af_middle_; af_tail_->prev = af_middle_;
} }
void SweepContext::RemoveNode( Node* node ) void SweepContext::RemoveNode( Node* node )
{ {
delete node; delete node;
} }
void SweepContext::MapTriangleToNodes( Triangle& t ) void SweepContext::MapTriangleToNodes( Triangle& t )
{ {
for (int i = 0; i < 3; i++) { for( int i = 0; i < 3; i++ )
if (!t.GetNeighbor(i)) { {
if( !t.GetNeighbor( i ) )
{
Node* n = front_->LocatePoint( t.PointCW( *t.GetPoint( i ) ) ); Node* n = front_->LocatePoint( t.PointCW( *t.GetPoint( i ) ) );
if( n ) if( n )
n->triangle = &t; n->triangle = &t;
} }
} }
} }
void SweepContext::RemoveFromMap( Triangle* triangle ) void SweepContext::RemoveFromMap( Triangle* triangle )
{ {
map_.remove( triangle ); map_.remove( triangle );
} }
void SweepContext::MeshClean( Triangle& triangle ) void SweepContext::MeshClean( Triangle& triangle )
{ {
std::vector<Triangle*> triangles; std::vector<Triangle*> triangles;
triangles.push_back( &triangle ); triangles.push_back( &triangle );
while(!triangles.empty()){ while( !triangles.empty() )
{
Triangle* t = triangles.back(); Triangle* t = triangles.back();
triangles.pop_back(); triangles.pop_back();
if (t != NULL && !t->IsInterior()) { if( t != NULL && !t->IsInterior() )
{
t->IsInterior( true ); t->IsInterior( true );
triangles_.push_back( t ); triangles_.push_back( t );
for (int i = 0; i < 3; i++) {
for( int i = 0; i < 3; i++ )
{
if( !t->constrained_edge[i] ) if( !t->constrained_edge[i] )
triangles.push_back( t->GetNeighbor( i ) ); triangles.push_back( t->GetNeighbor( i ) );
} }
@ -188,9 +217,9 @@ void SweepContext::MeshClean(Triangle& triangle)
} }
} }
SweepContext::~SweepContext() SweepContext::~SweepContext()
{ {
// Clean up memory // Clean up memory
delete head_; delete head_;
@ -202,15 +231,15 @@ SweepContext::~SweepContext()
typedef std::list<Triangle*> type_list; typedef std::list<Triangle*> type_list;
for(type_list::iterator iter = map_.begin(); iter != map_.end(); ++iter) { for( type_list::iterator iter = map_.begin(); iter != map_.end(); ++iter )
{
Triangle* ptr = *iter; Triangle* ptr = *iter;
delete ptr; delete ptr;
} }
for(unsigned int i = 0; i < edge_list.size(); i++) { for( unsigned int i = 0; i < edge_list.size(); i++ )
{
delete edge_list[i]; delete edge_list[i];
} }
} }
} }

View File

@ -37,7 +37,6 @@
#include <cstddef> #include <cstddef>
namespace p2t { namespace p2t {
// Inital triangle factor, seed triangle will extend 30% of // Inital triangle factor, seed triangle will extend 30% of
// PointSet width to both left and right. // PointSet width to both left and right.
const double kAlpha = 0.3; const double kAlpha = 0.3;
@ -48,7 +47,8 @@ struct Node;
struct Edge; struct Edge;
class AdvancingFront; class AdvancingFront;
class SweepContext { class SweepContext
{
public: public:
/// Constructor /// Constructor
@ -92,18 +92,21 @@ AdvancingFront* front();
void MeshClean( Triangle& triangle ); void MeshClean( Triangle& triangle );
std::vector<Triangle*> GetTriangles(); std::vector<Triangle*> GetTriangles();
std::list<Triangle*> GetMap(); std::list<Triangle*> GetMap();
std::vector<Edge*> edge_list; std::vector<Edge*> edge_list;
struct Basin { struct Basin
{
Node* left_node; Node* left_node;
Node* bottom_node; Node* bottom_node;
Node* right_node; Node* right_node;
double width; double width;
bool left_highest; bool left_highest;
Basin() : left_node(NULL), bottom_node(NULL), right_node(NULL), width(0.0), left_highest(false) Basin() : left_node( NULL ), bottom_node( NULL ), right_node( NULL ), width( 0.0 ),
left_highest( false )
{ {
} }
@ -117,7 +120,8 @@ struct Basin {
} }
}; };
struct EdgeEvent { struct EdgeEvent
{
Edge* constrained_edge; Edge* constrained_edge;
bool right; bool right;
@ -148,7 +152,6 @@ Node *af_head_, *af_middle_, *af_tail_;
void InitTriangulation(); void InitTriangulation();
void InitEdges( std::vector<Point*> polyline ); void InitEdges( std::vector<Point*> polyline );
}; };
inline AdvancingFront* SweepContext::front() inline AdvancingFront* SweepContext::front()
@ -156,31 +159,35 @@ inline AdvancingFront* SweepContext::front()
return front_; return front_;
} }
inline int SweepContext::point_count() inline int SweepContext::point_count()
{ {
return points_.size(); return points_.size();
} }
inline void SweepContext::set_head( Point* p1 ) inline void SweepContext::set_head( Point* p1 )
{ {
head_ = p1; head_ = p1;
} }
inline Point* SweepContext::head() inline Point* SweepContext::head()
{ {
return head_; return head_;
} }
inline void SweepContext::set_tail( Point* p1 ) inline void SweepContext::set_tail( Point* p1 )
{ {
tail_ = p1; tail_ = p1;
} }
inline Point* SweepContext::tail() inline Point* SweepContext::tail()
{ {
return tail_; return tail_;
} }
} }
#endif #endif