mirror of
https://github.com/jhasse/poly2tri.git
synced 2025-01-07 10:23:29 +01:00
uncrustified code
This commit is contained in:
parent
9202d205df
commit
732e0791e8
@ -30,8 +30,8 @@
|
|||||||
*/
|
*/
|
||||||
#include "shapes.h"
|
#include "shapes.h"
|
||||||
|
|
||||||
Triangle::Triangle(Point& a, Point& b, Point& c) {
|
Triangle::Triangle(Point& a, Point& b, Point& c)
|
||||||
|
{
|
||||||
points_[0] = &a; points_[1] = &b; points_[2] = &c;
|
points_[0] = &a; points_[1] = &b; points_[2] = &c;
|
||||||
neighbors_[0] = NULL; neighbors_[1] = NULL; neighbors_[2] = NULL;
|
neighbors_[0] = NULL; neighbors_[1] = NULL; neighbors_[2] = NULL;
|
||||||
constrained_edge[0] = constrained_edge[1] = constrained_edge[2] = false;
|
constrained_edge[0] = constrained_edge[1] = constrained_edge[2] = false;
|
||||||
@ -40,8 +40,8 @@ Triangle::Triangle(Point& a, Point& b, Point& c) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Update neighbor pointers
|
// Update neighbor pointers
|
||||||
void Triangle::MarkNeighbor(Point* p1, Point* p2, Triangle* t) {
|
void Triangle::MarkNeighbor(Point* p1, Point* p2, Triangle* t)
|
||||||
|
{
|
||||||
if ((p1 == points_[2] && p2 == points_[1]) || (p1 == points_[1] && p2 == points_[2]))
|
if ((p1 == points_[2] && p2 == points_[1]) || (p1 == points_[1] && p2 == points_[2]))
|
||||||
neighbors_[0] = t;
|
neighbors_[0] = t;
|
||||||
else if ((p1 == points_[0] && p2 == points_[2]) || (p1 == points_[2] && p2 == points_[0]))
|
else if ((p1 == points_[0] && p2 == points_[2]) || (p1 == points_[2] && p2 == points_[0]))
|
||||||
@ -53,8 +53,8 @@ void Triangle::MarkNeighbor(Point* p1, Point* p2, Triangle* t) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Exhaustive search to update neighbor pointers
|
// Exhaustive search to update neighbor pointers
|
||||||
void Triangle::MarkNeighbor(Triangle& t) {
|
void Triangle::MarkNeighbor(Triangle& t)
|
||||||
|
{
|
||||||
if (t.Contains(points_[1], points_[2])) {
|
if (t.Contains(points_[1], points_[2])) {
|
||||||
neighbors_[0] = &t;
|
neighbors_[0] = &t;
|
||||||
t.MarkNeighbor(points_[1], points_[2], this);
|
t.MarkNeighbor(points_[1], points_[2], this);
|
||||||
@ -67,18 +67,20 @@ void Triangle::MarkNeighbor(Triangle& t) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Triangle::ClearNeighbors() {
|
void Triangle::ClearNeighbors()
|
||||||
|
{
|
||||||
neighbors_[0] = NULL;
|
neighbors_[0] = NULL;
|
||||||
neighbors_[1] = NULL;
|
neighbors_[1] = NULL;
|
||||||
neighbors_[2] = NULL;
|
neighbors_[2] = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Triangle::ClearDelunayEdges() {
|
void Triangle::ClearDelunayEdges()
|
||||||
|
{
|
||||||
delaunay_edge[0] = delaunay_edge[1] = delaunay_edge[2] = false;
|
delaunay_edge[0] = delaunay_edge[1] = delaunay_edge[2] = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Point* Triangle::OppositePoint(Triangle& t, Point& p) {
|
Point* Triangle::OppositePoint(Triangle& t, Point& p)
|
||||||
|
{
|
||||||
Point *cw = t.PointCW(p);
|
Point *cw = t.PointCW(p);
|
||||||
double x = cw->x;
|
double x = cw->x;
|
||||||
double y = cw->y;
|
double y = cw->y;
|
||||||
@ -89,16 +91,16 @@ Point* Triangle::OppositePoint(Triangle& t, Point& p) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Legalized triangle by rotating clockwise around point(0)
|
// Legalized triangle by rotating clockwise around point(0)
|
||||||
void Triangle::Legalize(Point& point) {
|
void Triangle::Legalize(Point& point)
|
||||||
|
{
|
||||||
points_[1] = points_[0];
|
points_[1] = points_[0];
|
||||||
points_[0] = points_[2];
|
points_[0] = points_[2];
|
||||||
points_[2] = &point;
|
points_[2] = &point;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Legalize triagnle by rotating clockwise around oPoint
|
// Legalize triagnle by rotating clockwise around oPoint
|
||||||
void Triangle::Legalize(Point& opoint, Point& npoint) {
|
void Triangle::Legalize(Point& opoint, Point& npoint)
|
||||||
|
{
|
||||||
if (&opoint == points_[0]) {
|
if (&opoint == points_[0]) {
|
||||||
points_[1] = points_[0];
|
points_[1] = points_[0];
|
||||||
points_[0] = points_[2];
|
points_[0] = points_[2];
|
||||||
@ -116,8 +118,8 @@ void Triangle::Legalize(Point& opoint, Point& npoint) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int Triangle::Index(const Point* p) {
|
int Triangle::Index(const Point* p)
|
||||||
|
{
|
||||||
if (p == points_[0]) {
|
if (p == points_[0]) {
|
||||||
return 0;
|
return 0;
|
||||||
} else if (p == points_[1]) {
|
} else if (p == points_[1]) {
|
||||||
@ -128,8 +130,8 @@ int Triangle::Index(const Point* p) {
|
|||||||
assert(0);
|
assert(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int Triangle::EdgeIndex(const Point* p1, const Point* p2) {
|
int Triangle::EdgeIndex(const Point* p1, const Point* p2)
|
||||||
|
{
|
||||||
if (points_[0] == p1) {
|
if (points_[0] == p1) {
|
||||||
if (points_[1] == p2) {
|
if (points_[1] == p2) {
|
||||||
return 2;
|
return 2;
|
||||||
@ -152,17 +154,19 @@ int Triangle::EdgeIndex(const Point* p1, const Point* p2) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Triangle::MarkConstrainedEdge(const int index) {
|
void Triangle::MarkConstrainedEdge(const int index)
|
||||||
|
{
|
||||||
constrained_edge[index] = true;
|
constrained_edge[index] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Triangle::MarkConstrainedEdge(Edge& edge) {
|
void Triangle::MarkConstrainedEdge(Edge& edge)
|
||||||
|
{
|
||||||
MarkConstrainedEdge(edge.p, edge.q);
|
MarkConstrainedEdge(edge.p, edge.q);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mark edge as constrained
|
// Mark edge as constrained
|
||||||
void Triangle::MarkConstrainedEdge(Point* p, Point* q) {
|
void Triangle::MarkConstrainedEdge(Point* p, Point* q)
|
||||||
|
{
|
||||||
if ((q == points_[0] && p == points_[1]) || (q == points_[1] && p == points_[0])) {
|
if ((q == points_[0] && p == points_[1]) || (q == points_[1] && p == points_[0])) {
|
||||||
constrained_edge[2] = true;
|
constrained_edge[2] = true;
|
||||||
} else if ((q == points_[0] && p == points_[2]) || (q == points_[2] && p == points_[0])) {
|
} else if ((q == points_[0] && p == points_[2]) || (q == points_[2] && p == points_[0])) {
|
||||||
@ -170,12 +174,11 @@ void Triangle::MarkConstrainedEdge(Point* p, Point* q) {
|
|||||||
} else if ((q == points_[1] && p == points_[2]) || (q == points_[2] && p == points_[1])) {
|
} else if ((q == points_[1] && p == points_[2]) || (q == points_[2] && p == points_[1])) {
|
||||||
constrained_edge[0] = true;
|
constrained_edge[0] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// The point counter-clockwise to given point
|
// The point counter-clockwise to given point
|
||||||
Point* Triangle::PointCW(Point& point) {
|
Point* Triangle::PointCW(Point& point)
|
||||||
|
{
|
||||||
if (&point == points_[0]) {
|
if (&point == points_[0]) {
|
||||||
return points_[2];
|
return points_[2];
|
||||||
} else if (&point == points_[1]) {
|
} else if (&point == points_[1]) {
|
||||||
@ -187,8 +190,8 @@ Point* Triangle::PointCW(Point& point) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// The point counter-clockwise to given point
|
// The point counter-clockwise to given point
|
||||||
Point* Triangle::PointCCW(Point& point) {
|
Point* Triangle::PointCCW(Point& point)
|
||||||
|
{
|
||||||
if (&point == points_[0]) {
|
if (&point == points_[0]) {
|
||||||
return points_[1];
|
return points_[1];
|
||||||
} else if (&point == points_[1]) {
|
} else if (&point == points_[1]) {
|
||||||
@ -200,8 +203,8 @@ Point* Triangle::PointCCW(Point& point) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// The neighbor clockwise to given point
|
// The neighbor clockwise to given point
|
||||||
Triangle* Triangle::NeighborCW(Point& point) {
|
Triangle* Triangle::NeighborCW(Point& point)
|
||||||
|
{
|
||||||
if (&point == points_[0]) {
|
if (&point == points_[0]) {
|
||||||
return neighbors_[1];
|
return neighbors_[1];
|
||||||
} else if (&point == points_[1]) {
|
} else if (&point == points_[1]) {
|
||||||
@ -211,8 +214,8 @@ Triangle* Triangle::NeighborCW(Point& point) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// The neighbor counter-clockwise to given point
|
// The neighbor counter-clockwise to given point
|
||||||
Triangle* Triangle::NeighborCCW(Point& point) {
|
Triangle* Triangle::NeighborCCW(Point& point)
|
||||||
|
{
|
||||||
if (&point == points_[0]) {
|
if (&point == points_[0]) {
|
||||||
return neighbors_[2];
|
return neighbors_[2];
|
||||||
} else if (&point == points_[1]) {
|
} else if (&point == points_[1]) {
|
||||||
@ -221,8 +224,8 @@ Triangle* Triangle::NeighborCCW(Point& point) {
|
|||||||
return neighbors_[1];
|
return neighbors_[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Triangle::GetConstrainedEdgeCCW(Point& p) {
|
bool Triangle::GetConstrainedEdgeCCW(Point& p)
|
||||||
|
{
|
||||||
if (&p == points_[0]) {
|
if (&p == points_[0]) {
|
||||||
return constrained_edge[2];
|
return constrained_edge[2];
|
||||||
} else if (&p == points_[1]) {
|
} else if (&p == points_[1]) {
|
||||||
@ -231,8 +234,8 @@ bool Triangle::GetConstrainedEdgeCCW(Point& p) {
|
|||||||
return constrained_edge[1];
|
return constrained_edge[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Triangle::GetConstrainedEdgeCW(Point& p) {
|
bool Triangle::GetConstrainedEdgeCW(Point& p)
|
||||||
|
{
|
||||||
if (&p == points_[0]) {
|
if (&p == points_[0]) {
|
||||||
return constrained_edge[1];
|
return constrained_edge[1];
|
||||||
} else if (&p == points_[1]) {
|
} else if (&p == points_[1]) {
|
||||||
@ -241,8 +244,8 @@ bool Triangle::GetConstrainedEdgeCW(Point& p) {
|
|||||||
return constrained_edge[0];
|
return constrained_edge[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
void Triangle::SetConstrainedEdgeCCW(Point& p, bool ce) {
|
void Triangle::SetConstrainedEdgeCCW(Point& p, bool ce)
|
||||||
|
{
|
||||||
if (&p == points_[0]) {
|
if (&p == points_[0]) {
|
||||||
constrained_edge[2] = ce;
|
constrained_edge[2] = ce;
|
||||||
} else if (&p == points_[1]) {
|
} else if (&p == points_[1]) {
|
||||||
@ -252,8 +255,8 @@ void Triangle::SetConstrainedEdgeCCW(Point& p, bool ce) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Triangle::SetConstrainedEdgeCW(Point& p, bool ce) {
|
void Triangle::SetConstrainedEdgeCW(Point& p, bool ce)
|
||||||
|
{
|
||||||
if (&p == points_[0]) {
|
if (&p == points_[0]) {
|
||||||
constrained_edge[1] = ce;
|
constrained_edge[1] = ce;
|
||||||
} else if (&p == points_[1]) {
|
} else if (&p == points_[1]) {
|
||||||
@ -263,8 +266,8 @@ void Triangle::SetConstrainedEdgeCW(Point& p, bool ce) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Triangle::GetDelunayEdgeCCW(Point& p) {
|
bool Triangle::GetDelunayEdgeCCW(Point& p)
|
||||||
|
{
|
||||||
if (&p == points_[0]) {
|
if (&p == points_[0]) {
|
||||||
return delaunay_edge[2];
|
return delaunay_edge[2];
|
||||||
} else if (&p == points_[1]) {
|
} else if (&p == points_[1]) {
|
||||||
@ -273,8 +276,8 @@ bool Triangle::GetDelunayEdgeCCW(Point& p) {
|
|||||||
return delaunay_edge[1];
|
return delaunay_edge[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Triangle::GetDelunayEdgeCW(Point& p) {
|
bool Triangle::GetDelunayEdgeCW(Point& p)
|
||||||
|
{
|
||||||
if (&p == points_[0]) {
|
if (&p == points_[0]) {
|
||||||
return delaunay_edge[1];
|
return delaunay_edge[1];
|
||||||
} else if (&p == points_[1]) {
|
} else if (&p == points_[1]) {
|
||||||
@ -283,8 +286,8 @@ bool Triangle::GetDelunayEdgeCW(Point& p) {
|
|||||||
return delaunay_edge[0];
|
return delaunay_edge[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
void Triangle::SetDelunayEdgeCCW(Point& p, bool e) {
|
void Triangle::SetDelunayEdgeCCW(Point& p, bool e)
|
||||||
|
{
|
||||||
if (&p == points_[0]) {
|
if (&p == points_[0]) {
|
||||||
delaunay_edge[2] = e;
|
delaunay_edge[2] = e;
|
||||||
} else if (&p == points_[1]) {
|
} else if (&p == points_[1]) {
|
||||||
@ -294,8 +297,8 @@ void Triangle::SetDelunayEdgeCCW(Point& p, bool e) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Triangle::SetDelunayEdgeCW(Point& p, bool e) {
|
void Triangle::SetDelunayEdgeCW(Point& p, bool e)
|
||||||
|
{
|
||||||
if (&p == points_[0]) {
|
if (&p == points_[0]) {
|
||||||
delaunay_edge[1] = e;
|
delaunay_edge[1] = e;
|
||||||
} else if (&p == points_[1]) {
|
} else if (&p == points_[1]) {
|
||||||
@ -306,7 +309,8 @@ void Triangle::SetDelunayEdgeCW(Point& p, bool e) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// The neighbor across to given point
|
// The neighbor across to given point
|
||||||
Triangle& Triangle::NeighborAcross(Point& opoint) {
|
Triangle& Triangle::NeighborAcross(Point& opoint)
|
||||||
|
{
|
||||||
if (&opoint == points_[0]) {
|
if (&opoint == points_[0]) {
|
||||||
return *neighbors_[0];
|
return *neighbors_[0];
|
||||||
} else if (&opoint == points_[1]) {
|
} else if (&opoint == points_[1]) {
|
||||||
@ -315,7 +319,8 @@ Triangle& Triangle::NeighborAcross(Point& opoint) {
|
|||||||
return *neighbors_[2];
|
return *neighbors_[2];
|
||||||
}
|
}
|
||||||
|
|
||||||
void Triangle::DebugPrint() {
|
void Triangle::DebugPrint()
|
||||||
|
{
|
||||||
using namespace std;
|
using namespace std;
|
||||||
cout << points_[0]->x << "," << points_[0]->y << " ";
|
cout << points_[0]->x << "," << points_[0]->y << " ";
|
||||||
cout << points_[1]->x << "," << points_[1]->y << " ";
|
cout << points_[1]->x << "," << points_[1]->y << " ";
|
||||||
|
@ -43,69 +43,86 @@ struct Node;
|
|||||||
struct Edge;
|
struct Edge;
|
||||||
|
|
||||||
struct Point {
|
struct Point {
|
||||||
|
|
||||||
double x, y;
|
double x, y;
|
||||||
|
|
||||||
/// Default constructor does nothing (for performance).
|
/// Default constructor does nothing (for performance).
|
||||||
Point() { x = 0.0; y = 0.0; }
|
Point()
|
||||||
|
{
|
||||||
|
x = 0.0; y = 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
/// The edges this point constitutes an upper ending point
|
/// The edges this point constitutes an upper ending 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 x, double y) : x(x), y(y)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
/// Set this point to all zeros.
|
/// Set this point to all zeros.
|
||||||
void set_zero() { x = 0.0f; y = 0.0f; }
|
void set_zero()
|
||||||
|
{
|
||||||
|
x = 0.0f; y = 0.0f;
|
||||||
|
}
|
||||||
|
|
||||||
/// Set this point to some specified coordinates.
|
/// Set this point to some specified coordinates.
|
||||||
void set(double x_, double y_) { x = x_; y = y_; }
|
void set(double x_, double y_)
|
||||||
|
{
|
||||||
|
x = x_; y = y_;
|
||||||
|
}
|
||||||
|
|
||||||
/// Negate this point.
|
/// Negate this point.
|
||||||
Point operator -() const { Point v; v.set(-x, -y); return v; }
|
Point operator -() const
|
||||||
|
{
|
||||||
|
Point v; v.set(-x, -y); return v;
|
||||||
|
}
|
||||||
|
|
||||||
/// Add a point to this point.
|
/// Add a point to this point.
|
||||||
void operator += (const Point& v) {
|
void operator +=(const Point& v)
|
||||||
|
{
|
||||||
x += v.x; y += v.y;
|
x += v.x; y += v.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Subtract a point from this point.
|
/// Subtract a point from this point.
|
||||||
void operator -= (const Point& v) {
|
void operator -=(const Point& v)
|
||||||
|
{
|
||||||
x -= v.x; y -= v.y;
|
x -= v.x; y -= v.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Multiply this point by a scalar.
|
/// Multiply this point by a scalar.
|
||||||
void operator *= (double a) {
|
void operator *=(double a)
|
||||||
|
{
|
||||||
x *= a; y *= a;
|
x *= a; y *= a;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the length of this point (the norm).
|
/// Get the length of this point (the norm).
|
||||||
double Length() const {
|
double Length() const
|
||||||
|
{
|
||||||
return sqrt(x * x + y * y);
|
return sqrt(x * x + y * y);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert this point into a unit point. Returns the Length.
|
/// Convert this point into a unit point. Returns the Length.
|
||||||
double Normalize() {
|
double Normalize()
|
||||||
|
{
|
||||||
double len = Length();
|
double len = Length();
|
||||||
x /= len;
|
x /= len;
|
||||||
y /= len;
|
y /= len;
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DebugPrint() {
|
void DebugPrint()
|
||||||
|
{
|
||||||
printf("%f,%f ", x, y);
|
printf("%f,%f ", x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Represents a simple polygon's edge
|
// Represents a simple polygon's edge
|
||||||
struct Edge {
|
struct Edge {
|
||||||
|
|
||||||
Point* p, *q;
|
Point* p, *q;
|
||||||
|
|
||||||
/// Constructor
|
/// Constructor
|
||||||
Edge(Point& p1, Point& p2) : p(&p1), q(&p2) {
|
Edge(Point& p1, Point& p2) : p(&p1), q(&p2)
|
||||||
|
{
|
||||||
if (p1.y > p2.y) {
|
if (p1.y > p2.y) {
|
||||||
q = &p1;
|
q = &p1;
|
||||||
p = &p2;
|
p = &p2;
|
||||||
@ -120,16 +137,13 @@ struct Edge {
|
|||||||
}
|
}
|
||||||
|
|
||||||
q->edge_list.push_back(this);
|
q->edge_list.push_back(this);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Triangle-based data structures are know to have better performance than quad-edge structures
|
// Triangle-based data structures are know to have better performance than quad-edge structures
|
||||||
// See: J. Shewchuk, "Triangle: Engineering a 2D Quality Mesh Generator and Delaunay Triangulator"
|
// See: J. Shewchuk, "Triangle: Engineering a 2D Quality Mesh Generator and Delaunay Triangulator"
|
||||||
// "Triangulations in CGAL"
|
// "Triangulations in CGAL"
|
||||||
class Triangle {
|
class Triangle {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/// Constructor
|
/// Constructor
|
||||||
@ -191,10 +205,10 @@ private:
|
|||||||
|
|
||||||
/// Has this triangle been marked as an interior triangle?
|
/// Has this triangle been marked as an interior triangle?
|
||||||
bool interior_;
|
bool interior_;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
inline bool cmp (const Point* a, const Point* b) {
|
inline bool cmp(const Point* a, const Point* b)
|
||||||
|
{
|
||||||
if (a->y < b->y) {
|
if (a->y < b->y) {
|
||||||
return true;
|
return true;
|
||||||
} else if (a->y == b->y) {
|
} else if (a->y == b->y) {
|
||||||
@ -207,75 +221,91 @@ inline bool cmp (const Point* a, const Point* b) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Add two points_ component-wise.
|
/// Add two points_ component-wise.
|
||||||
inline Point operator + (const Point& a, const Point& b) {
|
inline Point operator +(const Point& a, const Point& b)
|
||||||
|
{
|
||||||
return Point(a.x + b.x, a.y + b.y);
|
return Point(a.x + b.x, a.y + b.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Subtract two points_ component-wise.
|
/// Subtract two points_ component-wise.
|
||||||
inline Point operator - (const Point& a, const Point& b) {
|
inline Point operator -(const Point& a, const Point& b)
|
||||||
|
{
|
||||||
return Point(a.x - b.x, a.y - b.y);
|
return Point(a.x - b.x, a.y - b.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Multiply point by scalar
|
/// Multiply point by scalar
|
||||||
inline Point operator * (double s, const Point& a) {
|
inline Point operator *(double s, const Point& a)
|
||||||
|
{
|
||||||
return Point(s * a.x, s * a.y);
|
return Point(s * a.x, s * a.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool operator == (const Point& a, const Point& b) {
|
inline bool operator ==(const Point& a, const Point& b)
|
||||||
|
{
|
||||||
return a.x == b.x && a.y == b.y;
|
return a.x == b.x && a.y == b.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool operator != (const Point& a, const Point& b) {
|
inline bool operator !=(const Point& a, const Point& b)
|
||||||
|
{
|
||||||
return a.x != b.x && a.y != b.y;
|
return a.x != b.x && a.y != b.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Peform the dot product on two vectors.
|
/// Peform the dot product on two vectors.
|
||||||
inline double Dot(const Point& a, const Point& b) {
|
inline double Dot(const Point& a, const Point& b)
|
||||||
|
{
|
||||||
return a.x * b.x + a.y * b.y;
|
return a.x * b.x + a.y * b.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Perform the cross product on two vectors. In 2D this produces a scalar.
|
/// Perform the cross product on two vectors. In 2D this produces a scalar.
|
||||||
inline double Cross(const Point& a, const Point& b) {
|
inline double Cross(const Point& a, const Point& b)
|
||||||
|
{
|
||||||
return a.x * b.y - a.y * b.x;
|
return a.x * b.y - a.y * b.x;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Perform the cross product on a point and a scalar. In 2D this produces
|
/// Perform the cross product on a point and a scalar. In 2D this produces
|
||||||
/// a point.
|
/// a point.
|
||||||
inline Point Cross(const Point& a, double s) {
|
inline Point Cross(const Point& a, double s)
|
||||||
|
{
|
||||||
return Point(s * a.y, -s * a.x);
|
return Point(s * a.y, -s * a.x);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Perform the cross product on a scalar and a point. In 2D this produces
|
/// Perform the cross product on a scalar and a point. In 2D this produces
|
||||||
/// a point.
|
/// a point.
|
||||||
inline Point Cross(const double s, const Point& a) {
|
inline Point Cross(const double s, const Point& a)
|
||||||
|
{
|
||||||
return Point(-s * a.y, s * a.x);
|
return Point(-s * a.y, s * a.x);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Point* Triangle::GetPoint(const int& index) {
|
inline Point* Triangle::GetPoint(const int& index)
|
||||||
|
{
|
||||||
return points_[index];
|
return points_[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Triangle* Triangle::GetNeighbor(const int& index) {
|
inline Triangle* Triangle::GetNeighbor(const int& index)
|
||||||
|
{
|
||||||
return neighbors_[index];
|
return neighbors_[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool Triangle::Contains(Point* p) {
|
inline bool Triangle::Contains(Point* p)
|
||||||
|
{
|
||||||
return p == points_[0] || p == points_[1] || p == points_[2];
|
return p == points_[0] || p == points_[1] || p == points_[2];
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool Triangle::Contains(const Edge& e) {
|
inline bool Triangle::Contains(const Edge& e)
|
||||||
|
{
|
||||||
return Contains(e.p) && Contains(e.q);
|
return Contains(e.p) && Contains(e.q);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool Triangle::Contains(Point* p, Point* q) {
|
inline bool Triangle::Contains(Point* p, Point* q)
|
||||||
|
{
|
||||||
return Contains(p) && Contains(q);
|
return Contains(p) && Contains(q);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool Triangle::IsInterior() {
|
inline bool Triangle::IsInterior()
|
||||||
|
{
|
||||||
return interior_;
|
return interior_;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Triangle::IsInterior(bool b) {
|
inline void Triangle::IsInterior(bool b)
|
||||||
|
{
|
||||||
interior_ = b;
|
interior_ = b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,7 +35,10 @@
|
|||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
template<typename T, int size>
|
template<typename T, int size>
|
||||||
int array_length(T(&)[size]){return size;}
|
int array_length(T(&)[size])
|
||||||
|
{
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
const double PI_3div4 = 3 * M_PI / 4;
|
const double PI_3div4 = 3 * M_PI / 4;
|
||||||
const double EPSILON = 1e-12;
|
const double EPSILON = 1e-12;
|
||||||
@ -52,8 +55,8 @@ enum Orientation { CW, CCW, COLLINEAR };
|
|||||||
* = (x1-x3)*(y2-y3) - (y1-y3)*(x2-x3)
|
* = (x1-x3)*(y2-y3) - (y1-y3)*(x2-x3)
|
||||||
* </pre>
|
* </pre>
|
||||||
*/
|
*/
|
||||||
Orientation Orient2d(Point& pa, Point& pb, Point& pc ) {
|
Orientation Orient2d(Point& pa, Point& pb, Point& pc)
|
||||||
|
{
|
||||||
double detleft = (pa.x - pc.x) * (pb.y - pc.y);
|
double detleft = (pa.x - pc.x) * (pb.y - pc.y);
|
||||||
double detright = (pa.y - pc.y) * (pb.x - pc.x);
|
double detright = (pa.y - pc.y) * (pb.x - pc.x);
|
||||||
double val = detleft - detright;
|
double val = detleft - detright;
|
||||||
@ -63,11 +66,10 @@ Orientation Orient2d(Point& pa, Point& pb, Point& pc ) {
|
|||||||
return CCW;
|
return CCW;
|
||||||
}
|
}
|
||||||
return CW;
|
return CW;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool InScanArea(Point& pa, Point& pb, Point& pc, Point& pd) {
|
bool InScanArea(Point& pa, Point& pb, Point& pc, Point& pd)
|
||||||
|
{
|
||||||
double pdx = pd.x;
|
double pdx = pd.x;
|
||||||
double pdy = pd.y;
|
double pdy = pd.y;
|
||||||
double adx = pa.x - pdx;
|
double adx = pa.x - pdx;
|
||||||
@ -95,7 +97,6 @@ bool InScanArea(Point& pa, Point& pb, Point& pc, Point& pd) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -30,12 +30,13 @@
|
|||||||
*/
|
*/
|
||||||
#include "advancing_front.h"
|
#include "advancing_front.h"
|
||||||
|
|
||||||
AdvancingFront::AdvancingFront() {
|
AdvancingFront::AdvancingFront()
|
||||||
|
{
|
||||||
head_ = tail_ = search_node_ = NULL;
|
head_ = tail_ = search_node_ = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
Node* AdvancingFront::Locate(const double& x) {
|
Node* AdvancingFront::Locate(const double& x)
|
||||||
|
{
|
||||||
Node* node = search_node_;
|
Node* node = search_node_;
|
||||||
|
|
||||||
if (x < node->value) {
|
if (x < node->value) {
|
||||||
@ -59,13 +60,14 @@ Node* AdvancingFront::Locate(const double& x) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
Node* AdvancingFront::FindSearchNode(const double& x) {
|
Node* AdvancingFront::FindSearchNode(const double& x)
|
||||||
|
{
|
||||||
// TODO: implement BST index
|
// TODO: implement BST index
|
||||||
return search_node_;
|
return search_node_;
|
||||||
}
|
}
|
||||||
|
|
||||||
Node* AdvancingFront::LocatePoint(Point* point) {
|
Node* AdvancingFront::LocatePoint(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;
|
||||||
@ -97,7 +99,8 @@ Node* AdvancingFront::LocatePoint(Point* point) {
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
AdvancingFront::~AdvancingFront() {
|
AdvancingFront::~AdvancingFront()
|
||||||
|
{
|
||||||
delete head_;
|
delete head_;
|
||||||
delete search_node_;
|
delete search_node_;
|
||||||
delete tail_;
|
delete tail_;
|
||||||
|
@ -34,7 +34,6 @@ struct Node;
|
|||||||
|
|
||||||
// Advancing front node
|
// Advancing front node
|
||||||
struct Node {
|
struct Node {
|
||||||
|
|
||||||
Point* point;
|
Point* point;
|
||||||
Triangle* triangle;
|
Triangle* triangle;
|
||||||
|
|
||||||
@ -43,10 +42,14 @@ struct Node {
|
|||||||
|
|
||||||
double value;
|
double value;
|
||||||
|
|
||||||
Node(Point& p) : point(&p), triangle(NULL), value(p.x), next(NULL), prev(NULL) {}
|
Node(Point& p) : point(&p), triangle(NULL), value(p.x), next(NULL), prev(NULL)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
Node(Point& p, Triangle& t) : point(&p), triangle(&t), value(p.x),
|
Node(Point& p, Triangle& t) : point(&p), triangle(&t), value(p.x),
|
||||||
next(NULL), prev(NULL) {}
|
next(NULL), prev(NULL)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
~Node() {
|
~Node() {
|
||||||
@ -55,12 +58,10 @@ struct Node {
|
|||||||
printf(" ... gone!\n");
|
printf(" ... gone!\n");
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Advancing front
|
// Advancing front
|
||||||
class AdvancingFront {
|
class AdvancingFront {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
AdvancingFront();
|
AdvancingFront();
|
||||||
@ -84,16 +85,33 @@ private:
|
|||||||
Node* head_, *tail_, *search_node_;
|
Node* head_, *tail_, *search_node_;
|
||||||
|
|
||||||
Node* FindSearchNode(const double& x);
|
Node* FindSearchNode(const double& x);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
inline Node* AdvancingFront::head() { return head_; }
|
inline Node* AdvancingFront::head()
|
||||||
inline void AdvancingFront::set_head(Node* node) { head_ = node; }
|
{
|
||||||
|
return head_;
|
||||||
|
}
|
||||||
|
inline void AdvancingFront::set_head(Node* node)
|
||||||
|
{
|
||||||
|
head_ = node;
|
||||||
|
}
|
||||||
|
|
||||||
inline Node* AdvancingFront::tail() { return tail_; }
|
inline Node* AdvancingFront::tail()
|
||||||
inline void AdvancingFront::set_tail(Node* node) { tail_ = node; }
|
{
|
||||||
|
return tail_;
|
||||||
|
}
|
||||||
|
inline void AdvancingFront::set_tail(Node* node)
|
||||||
|
{
|
||||||
|
tail_ = node;
|
||||||
|
}
|
||||||
|
|
||||||
inline Node* AdvancingFront::search() { return search_node_; }
|
inline Node* AdvancingFront::search()
|
||||||
|
{
|
||||||
|
return search_node_;
|
||||||
|
}
|
||||||
|
|
||||||
inline void AdvancingFront::set_search(Node* node) { search_node_ = node; }
|
inline void AdvancingFront::set_search(Node* node)
|
||||||
|
{
|
||||||
|
search_node_ = node;
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -30,28 +30,33 @@
|
|||||||
*/
|
*/
|
||||||
#include "cdt.h"
|
#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_context_ = new SweepContext(polyline, point_count);
|
||||||
sweep_ = new Sweep;
|
sweep_ = new Sweep;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CDT::AddHole(const Point poly_line[], const int point_count) {
|
void CDT::AddHole(const Point poly_line[], const int point_count)
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void CDT::Triangulate() {
|
void CDT::Triangulate()
|
||||||
|
{
|
||||||
sweep_->Triangulate(*sweep_context_);
|
sweep_->Triangulate(*sweep_context_);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<Triangle*> CDT::GetTriangles() {
|
std::vector<Triangle*> CDT::GetTriangles()
|
||||||
|
{
|
||||||
return sweep_context_->GetTriangles();
|
return sweep_context_->GetTriangles();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::list<Triangle*> CDT::GetMap() {
|
std::list<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_;
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,6 @@
|
|||||||
|
|
||||||
class CDT
|
class CDT
|
||||||
{
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/// Constructor
|
/// Constructor
|
||||||
@ -56,5 +55,4 @@ private:
|
|||||||
|
|
||||||
/// Destructor
|
/// Destructor
|
||||||
~CDT();
|
~CDT();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -35,7 +35,6 @@ class Triangle;
|
|||||||
|
|
||||||
class Mesh
|
class Mesh
|
||||||
{
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/// Triangles that constitute the mesh
|
/// Triangles that constitute the mesh
|
||||||
@ -46,5 +45,4 @@ public:
|
|||||||
//val triangles = new ArrayBuffer[Triangle]
|
//val triangles = new ArrayBuffer[Triangle]
|
||||||
|
|
||||||
void clean(Triangle& triangle);
|
void clean(Triangle& triangle);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -34,35 +34,31 @@
|
|||||||
#include "../common/utils.h"
|
#include "../common/utils.h"
|
||||||
|
|
||||||
// Triangulate simple polygon with holes
|
// Triangulate simple polygon with holes
|
||||||
void Sweep::Triangulate(SweepContext& tcx) {
|
void Sweep::Triangulate(SweepContext& tcx)
|
||||||
|
{
|
||||||
tcx.CreateAdvancingFront();
|
tcx.CreateAdvancingFront();
|
||||||
// Sweep points; build mesh
|
// Sweep points; build mesh
|
||||||
SweepPoints(tcx);
|
SweepPoints(tcx);
|
||||||
// Clean up
|
// Clean up
|
||||||
//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 i = 1; i < tcx.point_count(); i++) {
|
||||||
|
printf("%i = ", i);
|
||||||
//printf("%i = ",i);
|
|
||||||
Point& point = *tcx.GetPoint(i);
|
Point& point = *tcx.GetPoint(i);
|
||||||
//printf("size = %i\n", point.edge_list.size());
|
printf("%f,%f\n", point.x, point.y);
|
||||||
Node& node = PointEvent(tcx, point);
|
Node& node = PointEvent(tcx, point);
|
||||||
|
|
||||||
for (int i = 0; i < point.edge_list.size(); i++) {
|
for (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;
|
||||||
@ -83,8 +79,8 @@ void Sweep::FinalizationPolygon(SweepContext& tcx) {
|
|||||||
* @param point
|
* @param point
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
Node& Sweep::PointEvent(SweepContext& tcx, Point& point) {
|
Node& Sweep::PointEvent(SweepContext& tcx, Point& point)
|
||||||
|
{
|
||||||
Node& node = tcx.LocateNode(point);
|
Node& node = tcx.LocateNode(point);
|
||||||
Node& new_node = NewFrontTriangle(tcx, point, node);
|
Node& new_node = NewFrontTriangle(tcx, point, node);
|
||||||
|
|
||||||
@ -100,8 +96,8 @@ 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;
|
||||||
|
|
||||||
@ -114,11 +110,10 @@ void Sweep::EdgeEvent(SweepContext& tcx, Edge* edge, Node& node) {
|
|||||||
// but for now this avoid the issue with cases that needs both flips and fills
|
// but for now this avoid the issue with cases that needs both flips and fills
|
||||||
FillEdgeEvent(tcx, edge, node);
|
FillEdgeEvent(tcx, edge, 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;
|
||||||
}
|
}
|
||||||
@ -150,11 +145,10 @@ void Sweep::EdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle* triangl
|
|||||||
// 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) {
|
||||||
@ -168,8 +162,8 @@ bool Sweep::IsEdgeSideOfTriangle(Triangle& triangle, Point& ep, Point& eq) {
|
|||||||
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);
|
||||||
|
|
||||||
triangle->MarkNeighbor(*node.triangle);
|
triangle->MarkNeighbor(*node.triangle);
|
||||||
@ -193,10 +187,9 @@ Node& Sweep::NewFrontTriangle(SweepContext& tcx, Point& point, Node& node ) {
|
|||||||
* @param tcx
|
* @param tcx
|
||||||
* @param node - middle node, that is the bottom of the hole
|
* @param node - middle node, that is the bottom of the hole
|
||||||
*/
|
*/
|
||||||
void Sweep::Fill(SweepContext& tcx, Node& node) {
|
void Sweep::Fill(SweepContext& tcx, Node& node)
|
||||||
|
{
|
||||||
Triangle* triangle = new Triangle(*node.prev->point, *node.point,
|
Triangle* triangle = new Triangle(*node.prev->point, *node.point, *node.next->point);
|
||||||
*node.next->point);
|
|
||||||
|
|
||||||
// TODO: should copy the constrained_edge value from neighbor triangles
|
// TODO: should copy the constrained_edge value from neighbor triangles
|
||||||
// for now constrained_edge values are copied during the legalize
|
// for now constrained_edge values are copied during the legalize
|
||||||
@ -216,7 +209,6 @@ void Sweep::Fill(SweepContext& tcx, Node& node) {
|
|||||||
|
|
||||||
// TODO: delete node from memory
|
// TODO: delete node from memory
|
||||||
//tcx.RemoveNode(node);
|
//tcx.RemoveNode(node);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -226,8 +218,8 @@ void Sweep::Fill(SweepContext& tcx, Node& node) {
|
|||||||
* @param tcx
|
* @param tcx
|
||||||
* @param n
|
* @param n
|
||||||
*/
|
*/
|
||||||
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;
|
||||||
|
|
||||||
@ -255,10 +247,10 @@ void Sweep::FillAdvancingFront(SweepContext& tcx, Node& n) {
|
|||||||
FillBasin(tcx, n);
|
FillBasin(tcx, n);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
@ -269,8 +261,8 @@ double Sweep::BasinAngle(Node& node) {
|
|||||||
* @param node - middle node
|
* @param node - middle node
|
||||||
* @return the angle between 3 front nodes
|
* @return the angle between 3 front nodes
|
||||||
*/
|
*/
|
||||||
double Sweep::HoleAngle(Node& node) {
|
double Sweep::HoleAngle(Node& node)
|
||||||
|
{
|
||||||
/* 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)
|
||||||
@ -289,19 +281,17 @@ double Sweep::HoleAngle(Node& node) {
|
|||||||
/**
|
/**
|
||||||
* Returns true if triangle was legalized
|
* Returns true if triangle was legalized
|
||||||
*/
|
*/
|
||||||
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);
|
||||||
@ -316,7 +306,6 @@ bool Sweep::Legalize(SweepContext& tcx, Triangle& t) {
|
|||||||
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;
|
||||||
@ -377,8 +366,8 @@ bool Sweep::Legalize(SweepContext& tcx, Triangle& t) {
|
|||||||
* @param d - point opposite a
|
* @param d - point opposite a
|
||||||
* @return true if d is inside circle, false if on circle edge
|
* @return true if d is inside circle, false if on circle edge
|
||||||
*/
|
*/
|
||||||
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;
|
||||||
double ady = pa.y - pd.y;
|
double ady = pa.y - pd.y;
|
||||||
double bdx = pb.x - pd.x;
|
double bdx = pb.x - pd.x;
|
||||||
@ -427,8 +416,8 @@ bool Sweep::Incircle(Point& pa, Point& pb, Point& pc, Point& pd) {
|
|||||||
* n4 n4
|
* n4 n4
|
||||||
* </pre>
|
* </pre>
|
||||||
*/
|
*/
|
||||||
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);
|
||||||
@ -485,8 +474,8 @@ void Sweep::RotateTrianglePair(Triangle& t, Point& p, Triangle& ot, Point& op) {
|
|||||||
* @param tcx
|
* @param tcx
|
||||||
* @param node - starting node, this or next node will be left node
|
* @param node - starting node, this or next node will be left node
|
||||||
*/
|
*/
|
||||||
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 {
|
||||||
@ -518,7 +507,6 @@ void Sweep::FillBasin(SweepContext& tcx, Node& node) {
|
|||||||
tcx.basin.left_highest = tcx.basin.left_node->point->y > tcx.basin.right_node->point->y;
|
tcx.basin.left_highest = tcx.basin.left_node->point->y > tcx.basin.right_node->point->y;
|
||||||
|
|
||||||
FillBasinReq(tcx, *tcx.basin.bottom_node);
|
FillBasinReq(tcx, *tcx.basin.bottom_node);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -528,8 +516,8 @@ void Sweep::FillBasin(SweepContext& tcx, Node& node) {
|
|||||||
* @param node - bottom_node
|
* @param node - bottom_node
|
||||||
* @param cnt - counter used to alternate on even and odd numbers
|
* @param cnt - counter used to alternate on even and odd numbers
|
||||||
*/
|
*/
|
||||||
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;
|
||||||
@ -561,11 +549,10 @@ 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) {
|
||||||
@ -581,18 +568,17 @@ bool Sweep::IsShallow(SweepContext& tcx, Node& node) {
|
|||||||
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) {
|
||||||
@ -601,11 +587,10 @@ void Sweep::FillRightAboveEdgeEvent(SweepContext& tcx, Edge* edge, Node& node) {
|
|||||||
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
|
||||||
@ -617,11 +602,10 @@ void Sweep::FillRightBelowEdgeEvent(SweepContext& tcx, Edge* edge, Node& node) {
|
|||||||
FillRightBelowEdgeEvent(tcx, edge, node);
|
FillRightBelowEdgeEvent(tcx, edge, 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?
|
||||||
@ -635,11 +619,10 @@ void Sweep::FillRightConcaveEdgeEvent(SweepContext& tcx, Edge* edge, Node& node)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
@ -654,11 +637,10 @@ void Sweep::FillRightConvexEdgeEvent(SweepContext& tcx, Edge* edge, Node& node)
|
|||||||
// 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) {
|
||||||
@ -667,11 +649,10 @@ void Sweep::FillLeftAboveEdgeEvent(SweepContext& tcx, Edge* edge, Node& node) {
|
|||||||
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
|
||||||
@ -682,12 +663,11 @@ void Sweep::FillLeftBelowEdgeEvent(SweepContext& tcx, Edge* edge, Node& node) {
|
|||||||
// Retry this one
|
// Retry this one
|
||||||
FillLeftBelowEdgeEvent(tcx, edge, node);
|
FillLeftBelowEdgeEvent(tcx, edge, 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
|
||||||
@ -704,8 +684,8 @@ void Sweep::FillLeftConvexEdgeEvent(SweepContext& tcx, Edge* edge, Node& node) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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?
|
||||||
@ -719,11 +699,10 @@ void Sweep::FillLeftConcaveEdgeEvent(SweepContext& tcx, Edge* edge, Node& node)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
@ -761,8 +740,8 @@ 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) {
|
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);
|
||||||
@ -780,8 +759,8 @@ Triangle& Sweep::NextFlipTriangle(SweepContext& tcx, int o, Triangle& t, Triang
|
|||||||
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
|
||||||
@ -793,12 +772,11 @@ Point& Sweep::NextFlipPoint(Point& ep, Point& eq, Triangle& ot, Point& op) {
|
|||||||
//throw new RuntimeException("[Unsupported] Opposing point on constrained edge");
|
//throw new RuntimeException("[Unsupported] Opposing point on constrained edge");
|
||||||
assert(0);
|
assert(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
@ -823,7 +801,6 @@ void Sweep::FlipScanEdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle&
|
|||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -44,7 +44,6 @@ struct Edge;
|
|||||||
class Triangle;
|
class Triangle;
|
||||||
|
|
||||||
class Sweep {
|
class Sweep {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
void Triangulate(SweepContext& tcx);
|
void Triangulate(SweepContext& tcx);
|
||||||
@ -110,5 +109,4 @@ private:
|
|||||||
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);
|
void FinalizationPolygon(SweepContext& tcx);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -4,8 +4,8 @@
|
|||||||
#include <GL/glfw.h>
|
#include <GL/glfw.h>
|
||||||
#include "advancing_front.h"
|
#include "advancing_front.h"
|
||||||
|
|
||||||
SweepContext::SweepContext(Point** polyline, const int& point_count) {
|
SweepContext::SweepContext(Point** polyline, const int& point_count)
|
||||||
|
{
|
||||||
basin = Basin();
|
basin = Basin();
|
||||||
edge_event = EdgeEvent();
|
edge_event = EdgeEvent();
|
||||||
|
|
||||||
@ -14,19 +14,20 @@ SweepContext::SweepContext(Point** polyline, const int& point_count) {
|
|||||||
|
|
||||||
InitEdges(points_, point_count_);
|
InitEdges(points_, point_count_);
|
||||||
InitTriangulation();
|
InitTriangulation();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
@ -54,24 +55,23 @@ void SweepContext::InitTriangulation() {
|
|||||||
double dt = glfwGetTime() - init_time;
|
double dt = glfwGetTime() - init_time;
|
||||||
printf("Sort time (secs) = %f\n", dt);
|
printf("Sort time (secs) = %f\n", dt);
|
||||||
|
|
||||||
/*
|
|
||||||
printf("*************************\n");
|
printf("*************************\n");
|
||||||
for (int i = 0; i < point_count_; i++) {
|
for (int i = 0; i < point_count_; i++) {
|
||||||
printf("%f,%f ", points_[i]->x, points_[i]->y);
|
printf("%f,%f ", points_[i]->x, points_[i]->y);
|
||||||
printf("%p\n", points_[i]);
|
printf("%p\n", points_[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
|
printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
|
||||||
for(int i = 0; i < edge_list.size(); i++) {
|
for(int i = 0; i < edge_list.size(); i++) {
|
||||||
edge_list[i]->p->DebugPrint(); edge_list[i]->q->DebugPrint();
|
edge_list[i]->p->DebugPrint(); edge_list[i]->q->DebugPrint();
|
||||||
printf("%p, %p\n", edge_list[i]->p, edge_list[i]->q);
|
printf("%p, %p\n", edge_list[i]->p, edge_list[i]->q);
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SweepContext::InitEdges(Point** polyline, const int& point_count) {
|
void SweepContext::InitEdges(Point** polyline, const int& point_count)
|
||||||
|
{
|
||||||
for (int i = 0; i < point_count; i++) {
|
for (int i = 0; i < point_count; i++) {
|
||||||
int j = i < point_count - 1 ? i + 1 : 0;
|
int j = i < point_count - 1 ? i + 1 : 0;
|
||||||
edge_list.push_back(new Edge(*polyline[i], *polyline[j]));
|
edge_list.push_back(new Edge(*polyline[i], *polyline[j]));
|
||||||
@ -83,24 +83,26 @@ void SweepContext::InitEdges(Point** polyline, const int& point_count) {
|
|||||||
printf("%p, %p\n", edge_list[i]->p, edge_list[i]->q);
|
printf("%p, %p\n", edge_list[i]->p, edge_list[i]->q);
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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_->Locate(point.x);
|
return *front_->Locate(point.x);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SweepContext::CreateAdvancingFront() {
|
void SweepContext::CreateAdvancingFront()
|
||||||
|
{
|
||||||
// Initial triangle
|
// Initial triangle
|
||||||
Triangle* triangle = new Triangle(*points_[0], *tail_, *head_);
|
Triangle* triangle = new Triangle(*points_[0], *tail_, *head_);
|
||||||
|
|
||||||
@ -121,14 +123,15 @@ void SweepContext::CreateAdvancingFront() {
|
|||||||
middle->next = front_->tail();
|
middle->next = front_->tail();
|
||||||
middle->prev = front_->head();
|
middle->prev = front_->head();
|
||||||
front_->tail()->prev = middle;
|
front_->tail()->prev = 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) == NULL) {
|
if (t.GetNeighbor(i) == NULL) {
|
||||||
Node* n = front_->LocatePoint(t.PointCW(*t.GetPoint(i)));
|
Node* n = front_->LocatePoint(t.PointCW(*t.GetPoint(i)));
|
||||||
@ -138,12 +141,13 @@ void SweepContext::MapTriangleToNodes(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)
|
||||||
|
{
|
||||||
if (&triangle != NULL && !triangle.IsInterior()) {
|
if (&triangle != NULL && !triangle.IsInterior()) {
|
||||||
triangle.IsInterior(true);
|
triangle.IsInterior(true);
|
||||||
triangles_.push_back(&triangle);
|
triangles_.push_back(&triangle);
|
||||||
@ -154,7 +158,8 @@ void SweepContext::MeshClean(Triangle& triangle ) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SweepContext::~SweepContext() {
|
SweepContext::~SweepContext()
|
||||||
|
{
|
||||||
delete head_;
|
delete head_;
|
||||||
delete tail_;
|
delete tail_;
|
||||||
delete front_;
|
delete front_;
|
||||||
|
@ -43,7 +43,6 @@ class AdvancingFront;
|
|||||||
|
|
||||||
|
|
||||||
class SweepContext {
|
class SweepContext {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
@ -88,7 +87,6 @@ public:
|
|||||||
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;
|
||||||
@ -96,25 +94,27 @@ public:
|
|||||||
bool left_highest;
|
bool left_highest;
|
||||||
|
|
||||||
Basin() : left_node(NULL), bottom_node(NULL), right_node(NULL),
|
Basin() : left_node(NULL), bottom_node(NULL), right_node(NULL),
|
||||||
width(0.0), left_highest(false) {}
|
width(0.0), left_highest(false)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
void Clear() {
|
void Clear()
|
||||||
|
{
|
||||||
left_node = NULL;
|
left_node = NULL;
|
||||||
bottom_node = NULL;
|
bottom_node = NULL;
|
||||||
right_node = NULL;
|
right_node = NULL;
|
||||||
width = 0.0;
|
width = 0.0;
|
||||||
left_highest = false;
|
left_highest = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct EdgeEvent {
|
struct EdgeEvent {
|
||||||
|
|
||||||
Edge* constrained_edge;
|
Edge* constrained_edge;
|
||||||
bool right;
|
bool right;
|
||||||
|
|
||||||
EdgeEvent() : constrained_edge(NULL), right(false) {}
|
EdgeEvent() : constrained_edge(NULL), right(false)
|
||||||
|
{
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Basin basin;
|
Basin basin;
|
||||||
@ -148,17 +148,34 @@ private:
|
|||||||
bool right;
|
bool right;
|
||||||
};
|
};
|
||||||
*/
|
*/
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
inline AdvancingFront* SweepContext::front() { return front_; }
|
inline AdvancingFront* SweepContext::front()
|
||||||
|
{
|
||||||
|
return front_;
|
||||||
|
}
|
||||||
|
|
||||||
inline int SweepContext::point_count() { return point_count_; }
|
inline int SweepContext::point_count()
|
||||||
|
{
|
||||||
|
return point_count_;
|
||||||
|
}
|
||||||
|
|
||||||
inline void SweepContext::set_head(Point* p1) { head_ = p1; }
|
inline void SweepContext::set_head(Point* p1)
|
||||||
|
{
|
||||||
|
head_ = p1;
|
||||||
|
}
|
||||||
|
|
||||||
inline Point* SweepContext::head() { return head_; }
|
inline Point* SweepContext::head()
|
||||||
|
{
|
||||||
|
return head_;
|
||||||
|
}
|
||||||
|
|
||||||
inline void SweepContext::set_tail(Point* p1) { tail_ = p1; }
|
inline void SweepContext::set_tail(Point* p1)
|
||||||
|
{
|
||||||
|
tail_ = p1;
|
||||||
|
}
|
||||||
|
|
||||||
inline Point* SweepContext::tail() { return tail_; }
|
inline Point* SweepContext::tail()
|
||||||
|
{
|
||||||
|
return tail_;
|
||||||
|
}
|
||||||
|
@ -58,7 +58,8 @@ vector<Triangle*> triangles;
|
|||||||
/// Triangle map
|
/// Triangle map
|
||||||
list<Triangle*> map;
|
list<Triangle*> map;
|
||||||
|
|
||||||
double StringToDouble(const std::string& s) {
|
double StringToDouble(const std::string& s)
|
||||||
|
{
|
||||||
std::istringstream i(s);
|
std::istringstream i(s);
|
||||||
double x;
|
double x;
|
||||||
if (!(i >> x))
|
if (!(i >> x))
|
||||||
@ -68,8 +69,8 @@ double StringToDouble(const std::string& s) {
|
|||||||
|
|
||||||
bool draw_map = true;
|
bool draw_map = true;
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
if (argc != 3) {
|
if (argc != 3) {
|
||||||
cout << "Usage: p2t filename zoom" << endl;
|
cout << "Usage: p2t filename zoom" << endl;
|
||||||
return 1;
|
return 1;
|
||||||
@ -156,7 +157,6 @@ void Init()
|
|||||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
glClearColor(0.0, 0.0, 0.0, 0.0);
|
glClearColor(0.0, 0.0, 0.0, 0.0);
|
||||||
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
|
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ShutDown(int return_code)
|
void ShutDown(int return_code)
|
||||||
@ -172,8 +172,7 @@ void MainLoop(const double zoom)
|
|||||||
// this just loops as long as the program runs
|
// this just loops as long as the program runs
|
||||||
bool running = true;
|
bool running = true;
|
||||||
|
|
||||||
while(running)
|
while (running) {
|
||||||
{
|
|
||||||
// calculate time elapsed, and the amount by which stuff rotates
|
// calculate time elapsed, and the amount by which stuff rotates
|
||||||
double current_time = glfwGetTime(),
|
double current_time = glfwGetTime(),
|
||||||
delta_rotate = (current_time - old_time) * rotations_per_tick * 360;
|
delta_rotate = (current_time - old_time) * rotations_per_tick * 360;
|
||||||
@ -202,8 +201,8 @@ void MainLoop(const double zoom)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ResetZoom(double zoom, double cx, double cy, double width, double height) {
|
void ResetZoom(double zoom, double cx, double cy, double width, double height)
|
||||||
|
{
|
||||||
double left = -width / zoom;
|
double left = -width / zoom;
|
||||||
double right = width / zoom;
|
double right = width / zoom;
|
||||||
double bottom = -height / zoom;
|
double bottom = -height / zoom;
|
||||||
@ -223,18 +222,16 @@ void ResetZoom(double zoom, double cx, double cy, double width, double height) {
|
|||||||
|
|
||||||
// Clear the screen
|
// Clear the screen
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Draw(const double zoom) {
|
void Draw(const double zoom)
|
||||||
|
{
|
||||||
// reset zoom
|
// reset zoom
|
||||||
Point center = Point(0, 0);
|
Point center = Point(0, 0);
|
||||||
|
|
||||||
ResetZoom(zoom, center.x, center.y, 800, 600);
|
ResetZoom(zoom, center.x, center.y, 800, 600);
|
||||||
|
|
||||||
for (int i = 0; i < triangles.size(); i++) {
|
for (int i = 0; i < triangles.size(); i++) {
|
||||||
|
|
||||||
Triangle& t = *triangles[i];
|
Triangle& t = *triangles[i];
|
||||||
Point& a = *t.GetPoint(0);
|
Point& a = *t.GetPoint(0);
|
||||||
Point& b = *t.GetPoint(1);
|
Point& b = *t.GetPoint(1);
|
||||||
@ -248,13 +245,11 @@ void Draw(const double zoom) {
|
|||||||
glVertex2f(b.x, b.y);
|
glVertex2f(b.x, b.y);
|
||||||
glVertex2f(c.x, c.y);
|
glVertex2f(c.x, c.y);
|
||||||
glEnd();
|
glEnd();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
void DrawMap(const double zoom)
|
||||||
|
{
|
||||||
void DrawMap(const double zoom) {
|
|
||||||
|
|
||||||
// reset zoom
|
// reset zoom
|
||||||
Point center = Point(0, 0);
|
Point center = Point(0, 0);
|
||||||
|
|
||||||
@ -262,7 +257,6 @@ void DrawMap(const double zoom) {
|
|||||||
|
|
||||||
list<Triangle*>::iterator it;
|
list<Triangle*>::iterator it;
|
||||||
for (it = map.begin(); it != map.end(); it++) {
|
for (it = map.begin(); it != map.end(); it++) {
|
||||||
|
|
||||||
Triangle& t = **it;
|
Triangle& t = **it;
|
||||||
Point& a = *t.GetPoint(0);
|
Point& a = *t.GetPoint(0);
|
||||||
Point& b = *t.GetPoint(1);
|
Point& b = *t.GetPoint(1);
|
||||||
@ -285,12 +279,11 @@ void DrawMap(const double zoom) {
|
|||||||
glVertex2f(c.x, c.y);
|
glVertex2f(c.x, c.y);
|
||||||
glVertex2f(a.x, a.y);
|
glVertex2f(a.x, a.y);
|
||||||
glEnd( );
|
glEnd( );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
void ConstrainedColor(bool constrain)
|
||||||
|
{
|
||||||
void ConstrainedColor(bool constrain) {
|
|
||||||
if (constrain) {
|
if (constrain) {
|
||||||
// Green
|
// Green
|
||||||
glColor3f(0, 1, 0);
|
glColor3f(0, 1, 0);
|
||||||
|
Loading…
Reference in New Issue
Block a user