mirror of
https://github.com/jhasse/poly2tri.git
synced 2025-01-06 01:43:31 +01:00
uncrustified code
This commit is contained in:
parent
9202d205df
commit
732e0791e8
@ -1,4 +1,4 @@
|
|||||||
/*
|
/*
|
||||||
* Poly2Tri Copyright (c) 2009-2010, Mason Green
|
* Poly2Tri Copyright (c) 2009-2010, Mason Green
|
||||||
* http://code.google.com/p/poly2tri/
|
* http://code.google.com/p/poly2tri/
|
||||||
*
|
*
|
||||||
@ -30,35 +30,35 @@
|
|||||||
*/
|
*/
|
||||||
#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;
|
||||||
delaunay_edge[0] = delaunay_edge[1] = delaunay_edge[2] = false;
|
delaunay_edge[0] = delaunay_edge[1] = delaunay_edge[2] = false;
|
||||||
interior_ = false;
|
interior_ = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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]))
|
||||||
neighbors_[1] = t;
|
neighbors_[1] = t;
|
||||||
else if((p1 == points_[0] && p2 == points_[1]) || (p1 == points_[1] && p2 == points_[0]))
|
else if ((p1 == points_[0] && p2 == points_[1]) || (p1 == points_[1] && p2 == points_[0]))
|
||||||
neighbors_[2] = t;
|
neighbors_[2] = t;
|
||||||
else
|
else
|
||||||
assert(0);
|
assert(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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);
|
||||||
} else if(t.Contains(points_[0], points_[2])) {
|
} else if (t.Contains(points_[0], points_[2])) {
|
||||||
neighbors_[1] = &t;
|
neighbors_[1] = &t;
|
||||||
t.MarkNeighbor(points_[0], points_[2], this);
|
t.MarkNeighbor(points_[0], points_[2], this);
|
||||||
} else if (t.Contains(points_[0], points_[1])) {
|
} else if (t.Contains(points_[0], points_[1])) {
|
||||||
@ -67,39 +67,41 @@ 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;
|
||||||
x = p.x;
|
x = p.x;
|
||||||
y = p.y;
|
y = p.y;
|
||||||
Point* ham = PointCW(*cw);
|
Point* ham = PointCW(*cw);
|
||||||
return PointCW(*cw);
|
return PointCW(*cw);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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];
|
||||||
points_[2] = &npoint;
|
points_[2] = &npoint;
|
||||||
@ -111,211 +113,214 @@ void Triangle::Legalize(Point& opoint, Point& npoint) {
|
|||||||
points_[0] = points_[2];
|
points_[0] = points_[2];
|
||||||
points_[2] = points_[1];
|
points_[2] = points_[1];
|
||||||
points_[1] = &npoint;
|
points_[1] = &npoint;
|
||||||
} else {
|
} else {
|
||||||
assert(0);
|
assert(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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]) {
|
||||||
return 1;
|
return 1;
|
||||||
} else if(p == points_[2]) {
|
} else if (p == points_[2]) {
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
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;
|
||||||
} else if(points_[2] == p2){
|
} else if (points_[2] == p2) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
} else if(points_[1] == p1) {
|
} else if (points_[1] == p1) {
|
||||||
if(points_[2] == p2) {
|
if (points_[2] == p2) {
|
||||||
return 0;
|
return 0;
|
||||||
} else if(points_[0] == p2) {
|
} else if (points_[0] == p2) {
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
} else if(points_[2] == p1) {
|
} else if (points_[2] == p1) {
|
||||||
if(points_[0] == p2 ) {
|
if (points_[0] == p2) {
|
||||||
return 1;
|
return 1;
|
||||||
} else if(points_[1] == p2) {
|
} else if (points_[1] == p2) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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])) {
|
||||||
constrained_edge[1] = true;
|
constrained_edge[1] = true;
|
||||||
} 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
|
|
||||||
Point* Triangle::PointCW(Point& point) {
|
|
||||||
|
|
||||||
if(&point == points_[0]) {
|
|
||||||
return points_[2];
|
|
||||||
} else if(&point == points_[1]) {
|
|
||||||
return points_[0];
|
|
||||||
} else if(&point == points_[2]) {
|
|
||||||
return points_[1];
|
|
||||||
}
|
|
||||||
assert(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// The point counter-clockwise to given point
|
// The point counter-clockwise to given point
|
||||||
Point* Triangle::PointCCW(Point& point) {
|
Point* Triangle::PointCW(Point& point)
|
||||||
|
{
|
||||||
|
if (&point == points_[0]) {
|
||||||
|
return points_[2];
|
||||||
|
} else if (&point == points_[1]) {
|
||||||
|
return points_[0];
|
||||||
|
} else if (&point == points_[2]) {
|
||||||
|
return points_[1];
|
||||||
|
}
|
||||||
|
assert(0);
|
||||||
|
}
|
||||||
|
|
||||||
if(&point == points_[0]) {
|
// The point counter-clockwise to given point
|
||||||
return points_[1];
|
Point* Triangle::PointCCW(Point& point)
|
||||||
} else if(&point == points_[1]) {
|
{
|
||||||
return points_[2];
|
if (&point == points_[0]) {
|
||||||
} else if(&point == points_[2]) {
|
return points_[1];
|
||||||
return points_[0];
|
} else if (&point == points_[1]) {
|
||||||
}
|
return points_[2];
|
||||||
assert(0);
|
} else if (&point == points_[2]) {
|
||||||
|
return points_[0];
|
||||||
|
}
|
||||||
|
assert(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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]) {
|
||||||
return neighbors_[2];
|
return neighbors_[2];
|
||||||
}
|
}
|
||||||
return neighbors_[0];
|
return neighbors_[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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]) {
|
||||||
return neighbors_[0];
|
return neighbors_[0];
|
||||||
}
|
}
|
||||||
return neighbors_[1];
|
return neighbors_[1];
|
||||||
}
|
|
||||||
|
|
||||||
bool Triangle::GetConstrainedEdgeCCW(Point& p) {
|
|
||||||
|
|
||||||
if(&p == points_[0]) {
|
|
||||||
return constrained_edge[2];
|
|
||||||
} else if(&p == points_[1]) {
|
|
||||||
return constrained_edge[0];
|
|
||||||
}
|
|
||||||
return constrained_edge[1];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Triangle::GetConstrainedEdgeCW(Point& p) {
|
bool Triangle::GetConstrainedEdgeCCW(Point& p)
|
||||||
|
{
|
||||||
|
if (&p == points_[0]) {
|
||||||
|
return constrained_edge[2];
|
||||||
|
} else if (&p == points_[1]) {
|
||||||
|
return constrained_edge[0];
|
||||||
|
}
|
||||||
|
return constrained_edge[1];
|
||||||
|
}
|
||||||
|
|
||||||
if(&p == points_[0]) {
|
bool Triangle::GetConstrainedEdgeCW(Point& p)
|
||||||
|
{
|
||||||
|
if (&p == points_[0]) {
|
||||||
return constrained_edge[1];
|
return constrained_edge[1];
|
||||||
} else if(&p == points_[1]) {
|
} else if (&p == points_[1]) {
|
||||||
return constrained_edge[2];
|
return constrained_edge[2];
|
||||||
}
|
}
|
||||||
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]) {
|
||||||
constrained_edge[0] = ce;
|
constrained_edge[0] = ce;
|
||||||
} else {
|
} else {
|
||||||
constrained_edge[1] = ce;
|
constrained_edge[1] = 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]) {
|
||||||
constrained_edge[2] = ce;
|
constrained_edge[2] = ce;
|
||||||
} else {
|
} else {
|
||||||
constrained_edge[0] = ce;
|
constrained_edge[0] = ce;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
bool Triangle::GetDelunayEdgeCCW(Point& p) {
|
|
||||||
|
|
||||||
if(&p == points_[0]){
|
|
||||||
return delaunay_edge[2];
|
|
||||||
} else if(&p == points_[1]) {
|
|
||||||
return delaunay_edge[0];
|
|
||||||
}
|
|
||||||
return delaunay_edge[1];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Triangle::GetDelunayEdgeCW(Point& p) {
|
bool Triangle::GetDelunayEdgeCCW(Point& p)
|
||||||
|
{
|
||||||
if(&p == points_[0]) {
|
if (&p == points_[0]) {
|
||||||
return delaunay_edge[1];
|
return delaunay_edge[2];
|
||||||
} else if(&p == points_[1]) {
|
} else if (&p == points_[1]) {
|
||||||
return delaunay_edge[2];
|
return delaunay_edge[0];
|
||||||
}
|
}
|
||||||
return delaunay_edge[0];
|
return delaunay_edge[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
void Triangle::SetDelunayEdgeCCW(Point& p, bool e) {
|
bool Triangle::GetDelunayEdgeCW(Point& p)
|
||||||
|
{
|
||||||
if(&p == points_[0]) {
|
if (&p == points_[0]) {
|
||||||
delaunay_edge[2] = e;
|
return delaunay_edge[1];
|
||||||
} else if(&p == points_[1]) {
|
} else if (&p == points_[1]) {
|
||||||
delaunay_edge[0] = e;
|
return delaunay_edge[2];
|
||||||
} else {
|
}
|
||||||
delaunay_edge[1] = e;
|
return delaunay_edge[0];
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Triangle::SetDelunayEdgeCW(Point& p, bool e) {
|
void Triangle::SetDelunayEdgeCCW(Point& p, bool e)
|
||||||
|
{
|
||||||
|
if (&p == points_[0]) {
|
||||||
|
delaunay_edge[2] = e;
|
||||||
|
} else if (&p == points_[1]) {
|
||||||
|
delaunay_edge[0] = e;
|
||||||
|
} else {
|
||||||
|
delaunay_edge[1] = e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(&p == points_[0]) {
|
void Triangle::SetDelunayEdgeCW(Point& p, bool e)
|
||||||
delaunay_edge[1] = e;
|
{
|
||||||
} else if(&p == points_[1]) {
|
if (&p == points_[0]) {
|
||||||
delaunay_edge[2] = e;
|
delaunay_edge[1] = e;
|
||||||
} else {
|
} else if (&p == points_[1]) {
|
||||||
delaunay_edge[0] = e;
|
delaunay_edge[2] = e;
|
||||||
}
|
} else {
|
||||||
|
delaunay_edge[0] = 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]) {
|
{
|
||||||
return *neighbors_[0];
|
if (&opoint == points_[0]) {
|
||||||
} else if(&opoint == points_[1]) {
|
return *neighbors_[0];
|
||||||
return *neighbors_[1];
|
} else if (&opoint == points_[1]) {
|
||||||
}
|
return *neighbors_[1];
|
||||||
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 << " ";
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
/*
|
/*
|
||||||
* Poly2Tri Copyright (c) 2009-2010, Mason Green
|
* Poly2Tri Copyright (c) 2009-2010, Mason Green
|
||||||
* http://code.google.com/p/poly2tri/
|
* http://code.google.com/p/poly2tri/
|
||||||
*
|
*
|
||||||
@ -42,241 +42,271 @@
|
|||||||
struct Node;
|
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
|
||||||
|
{
|
||||||
/// Add a point to this point.
|
Point v; v.set(-x, -y); return v;
|
||||||
void operator += (const Point& v) {
|
}
|
||||||
x += v.x; y += v.y;
|
|
||||||
}
|
/// Add a point to this point.
|
||||||
|
void operator +=(const Point& v)
|
||||||
/// Subtract a point from this point.
|
{
|
||||||
void operator -= (const Point& v) {
|
x += v.x; y += v.y;
|
||||||
x -= v.x; y -= v.y;
|
}
|
||||||
}
|
|
||||||
|
/// Subtract a point from this point.
|
||||||
|
void operator -=(const Point& v)
|
||||||
|
{
|
||||||
|
x -= v.x; y -= v.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Multiply this point by a scalar.
|
||||||
|
void operator *=(double a)
|
||||||
|
{
|
||||||
|
x *= a; y *= a;
|
||||||
|
}
|
||||||
|
|
||||||
/// Multiply this point by a scalar.
|
|
||||||
void operator *= (double 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;
|
||||||
} else if(p1.y == p2.y) {
|
} else if (p1.y == p2.y) {
|
||||||
if(p1.x > p2.x) {
|
if (p1.x > p2.x) {
|
||||||
q = &p1;
|
q = &p1;
|
||||||
p = &p2;
|
p = &p2;
|
||||||
} else if(p1.x == p2.x) {
|
} else if (p1.x == p2.x) {
|
||||||
// Repeat points
|
// Repeat points
|
||||||
assert(false);
|
assert(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
Triangle(Point& a, Point& b, Point& c);
|
Triangle(Point& a, Point& b, Point& c);
|
||||||
|
|
||||||
/// Flags to determine if an edge is a Constrained edge
|
/// Flags to determine if an edge is a Constrained edge
|
||||||
bool constrained_edge[3];
|
bool constrained_edge[3];
|
||||||
/// Flags to determine if an edge is a Delauney edge
|
/// Flags to determine if an edge is a Delauney edge
|
||||||
bool delaunay_edge[3];
|
bool delaunay_edge[3];
|
||||||
|
|
||||||
Point* GetPoint(const int& index);
|
|
||||||
Point* PointCW(Point& point);
|
|
||||||
Point* PointCCW(Point& point);
|
|
||||||
Point* OppositePoint(Triangle& t, Point& p);
|
|
||||||
|
|
||||||
Triangle* GetNeighbor(const int& index);
|
|
||||||
void MarkNeighbor(Point* p1, Point* p2, Triangle* t);
|
|
||||||
void MarkNeighbor(Triangle& t);
|
|
||||||
|
|
||||||
void MarkConstrainedEdge(const int index);
|
|
||||||
void MarkConstrainedEdge(Edge& edge);
|
|
||||||
void MarkConstrainedEdge(Point* p, Point* q);
|
|
||||||
|
|
||||||
int Index(const Point* p);
|
Point* GetPoint(const int& index);
|
||||||
int EdgeIndex(const Point* p1, const Point* p2);
|
Point* PointCW(Point& point);
|
||||||
|
Point* PointCCW(Point& point);
|
||||||
|
Point* OppositePoint(Triangle& t, Point& p);
|
||||||
|
|
||||||
Triangle* NeighborCW(Point& point);
|
Triangle* GetNeighbor(const int& index);
|
||||||
Triangle* NeighborCCW(Point& point);
|
void MarkNeighbor(Point* p1, Point* p2, Triangle* t);
|
||||||
bool GetConstrainedEdgeCCW(Point& p);
|
void MarkNeighbor(Triangle& t);
|
||||||
bool GetConstrainedEdgeCW(Point& p);
|
|
||||||
void SetConstrainedEdgeCCW(Point& p, bool ce);
|
void MarkConstrainedEdge(const int index);
|
||||||
void SetConstrainedEdgeCW(Point& p, bool ce);
|
void MarkConstrainedEdge(Edge& edge);
|
||||||
bool GetDelunayEdgeCCW(Point& p);
|
void MarkConstrainedEdge(Point* p, Point* q);
|
||||||
bool GetDelunayEdgeCW(Point& p);
|
|
||||||
void SetDelunayEdgeCCW(Point& p, bool e);
|
int Index(const Point* p);
|
||||||
void SetDelunayEdgeCW(Point& p, bool e);
|
int EdgeIndex(const Point* p1, const Point* p2);
|
||||||
|
|
||||||
bool Contains(Point* p);
|
Triangle* NeighborCW(Point& point);
|
||||||
bool Contains(const Edge& e);
|
Triangle* NeighborCCW(Point& point);
|
||||||
bool Contains(Point* p, Point* q);
|
bool GetConstrainedEdgeCCW(Point& p);
|
||||||
void Legalize(Point& point);
|
bool GetConstrainedEdgeCW(Point& p);
|
||||||
void Legalize(Point& opoint, Point& npoint);
|
void SetConstrainedEdgeCCW(Point& p, bool ce);
|
||||||
void ClearNeighbors();
|
void SetConstrainedEdgeCW(Point& p, bool ce);
|
||||||
void ClearDelunayEdges();
|
bool GetDelunayEdgeCCW(Point& p);
|
||||||
|
bool GetDelunayEdgeCW(Point& p);
|
||||||
inline bool IsInterior();
|
void SetDelunayEdgeCCW(Point& p, bool e);
|
||||||
inline void IsInterior(bool b);
|
void SetDelunayEdgeCW(Point& p, bool e);
|
||||||
|
|
||||||
Triangle& NeighborAcross(Point& opoint);
|
bool Contains(Point* p);
|
||||||
|
bool Contains(const Edge& e);
|
||||||
|
bool Contains(Point* p, Point* q);
|
||||||
|
void Legalize(Point& point);
|
||||||
|
void Legalize(Point& opoint, Point& npoint);
|
||||||
|
void ClearNeighbors();
|
||||||
|
void ClearDelunayEdges();
|
||||||
|
|
||||||
|
inline bool IsInterior();
|
||||||
|
inline void IsInterior(bool b);
|
||||||
|
|
||||||
|
Triangle& NeighborAcross(Point& opoint);
|
||||||
|
|
||||||
|
void DebugPrint();
|
||||||
|
|
||||||
void DebugPrint();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
/// Triangle points
|
/// Triangle points
|
||||||
Point* points_[3];
|
Point* points_[3];
|
||||||
/// Neighbor list
|
/// Neighbor list
|
||||||
Triangle* neighbors_[3];
|
Triangle* neighbors_[3];
|
||||||
|
|
||||||
/// 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) {
|
||||||
// Make sure q is point with greater x value
|
// Make sure q is point with greater x value
|
||||||
if(a->x < b->x) {
|
if (a->x < b->x) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
/*
|
/*
|
||||||
* Poly2Tri Copyright (c) 2009-2010, Mason Green
|
* Poly2Tri Copyright (c) 2009-2010, Mason Green
|
||||||
* http://code.google.com/p/poly2tri/
|
* http://code.google.com/p/poly2tri/
|
||||||
*
|
*
|
||||||
@ -35,9 +35,12 @@
|
|||||||
#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;
|
||||||
|
|
||||||
enum Orientation { CW, CCW, COLLINEAR };
|
enum Orientation { CW, CCW, COLLINEAR };
|
||||||
@ -50,28 +53,27 @@ enum Orientation { CW, CCW, COLLINEAR };
|
|||||||
* <pre>
|
* <pre>
|
||||||
* A[P1,P2,P3] = (x1*y2 - y1*x2) + (x2*y3 - y2*x3) + (x3*y1 - y3*x1)
|
* A[P1,P2,P3] = (x1*y2 - y1*x2) + (x2*y3 - y2*x3) + (x3*y1 - y3*x1)
|
||||||
* = (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;
|
||||||
if( val > -EPSILON && val < EPSILON ) {
|
if (val > -EPSILON && val < EPSILON) {
|
||||||
return COLLINEAR;
|
return COLLINEAR;
|
||||||
} else if( val > 0 ) {
|
} else if (val > 0) {
|
||||||
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;
|
||||||
double ady = pa.y - pdy;
|
double ady = pa.y - pdy;
|
||||||
double bdx = pb.x - pdx;
|
double bdx = pb.x - pdx;
|
||||||
double bdy = pb.y - pdy;
|
double bdy = pb.y - pdy;
|
||||||
|
|
||||||
@ -79,7 +81,7 @@ bool InScanArea(Point& pa, Point& pb, Point& pc, Point& pd) {
|
|||||||
double bdxady = bdx * ady;
|
double bdxady = bdx * ady;
|
||||||
double oabd = adxbdy - bdxady;
|
double oabd = adxbdy - bdxady;
|
||||||
|
|
||||||
if(oabd <= EPSILON) {
|
if (oabd <= EPSILON) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,12 +92,11 @@ bool InScanArea(Point& pa, Point& pb, Point& pc, Point& pd) {
|
|||||||
double adxcdy = adx * cdy;
|
double adxcdy = adx * cdy;
|
||||||
double ocad = cdxady - adxcdy;
|
double ocad = cdxady - adxcdy;
|
||||||
|
|
||||||
if(ocad <= EPSILON) {
|
if (ocad <= EPSILON) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
/*
|
/*
|
||||||
* Poly2Tri Copyright (c) 2009-2010, Mason Green
|
* Poly2Tri Copyright (c) 2009-2010, Mason Green
|
||||||
* http://code.google.com/p/poly2tri/
|
* http://code.google.com/p/poly2tri/
|
||||||
*
|
*
|
||||||
@ -30,75 +30,78 @@
|
|||||||
*/
|
*/
|
||||||
#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) {
|
||||||
//printf("<: - %f,%f - %p\n", x, node->value, node->next);
|
//printf("<: - %f,%f - %p\n", x, node->value, node->next);
|
||||||
while((node = node->prev) != NULL) {
|
while ((node = node->prev) != NULL) {
|
||||||
if(x >= node->value) {
|
if (x >= node->value) {
|
||||||
search_node_ = node;
|
search_node_ = node;
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
//printf("%p - %p\n", node, node->next);
|
//printf("%p - %p\n", node, node->next);
|
||||||
//printf(">: %f - %f\n", x, node->value);
|
//printf(">: %f - %f\n", x, node->value);
|
||||||
while((node = node->next) != NULL) {
|
while ((node = node->next) != NULL) {
|
||||||
if(x < node->value) {
|
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)
|
||||||
// TODO: implement BST index
|
{
|
||||||
return search_node_;
|
// TODO: implement BST index
|
||||||
|
return search_node_;
|
||||||
}
|
}
|
||||||
|
|
||||||
Node* AdvancingFront::LocatePoint(Point* point) {
|
|
||||||
|
|
||||||
const double px = point->x;
|
Node* AdvancingFront::LocatePoint(Point* point)
|
||||||
Node* node = FindSearchNode(px);
|
{
|
||||||
const double nx = node->point->x;
|
const double px = point->x;
|
||||||
|
Node* node = FindSearchNode(px);
|
||||||
if(px == nx) {
|
const double nx = node->point->x;
|
||||||
if(point != node->point) {
|
|
||||||
// We might have two nodes with same x value for a short time
|
if (px == nx) {
|
||||||
if(point == node->prev->point) {
|
if (point != node->point) {
|
||||||
node = node->prev;
|
// We might have two nodes with same x value for a short time
|
||||||
} else if(point == node->next->point) {
|
if (point == node->prev->point) {
|
||||||
node = node->next;
|
node = node->prev;
|
||||||
} else {
|
} else if (point == node->next->point) {
|
||||||
assert(0);
|
node = node->next;
|
||||||
}
|
} else {
|
||||||
}
|
assert(0);
|
||||||
} else if(px < nx) {
|
}
|
||||||
while((node = node->prev) != NULL) {
|
}
|
||||||
if(point == node->point) {
|
} else if (px < nx) {
|
||||||
break;
|
while ((node = node->prev) != NULL) {
|
||||||
}
|
if (point == node->point) {
|
||||||
}
|
break;
|
||||||
} else {
|
}
|
||||||
while((node = node->next) != NULL) {
|
}
|
||||||
if(point == node->point)
|
} else {
|
||||||
break;
|
while ((node = node->next) != NULL) {
|
||||||
}
|
if (point == node->point)
|
||||||
}
|
break;
|
||||||
if(node) search_node_ = node;
|
}
|
||||||
return node;
|
}
|
||||||
|
if (node) search_node_ = node;
|
||||||
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
AdvancingFront::~AdvancingFront() {
|
AdvancingFront::~AdvancingFront()
|
||||||
delete head_;
|
{
|
||||||
delete search_node_;
|
delete head_;
|
||||||
|
delete search_node_;
|
||||||
delete tail_;
|
delete tail_;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
/*
|
/*
|
||||||
* Poly2Tri Copyright (c) 2009-2010, Mason Green
|
* Poly2Tri Copyright (c) 2009-2010, Mason Green
|
||||||
* http://code.google.com/p/poly2tri/
|
* http://code.google.com/p/poly2tri/
|
||||||
*
|
*
|
||||||
@ -34,66 +34,84 @@ struct Node;
|
|||||||
|
|
||||||
// Advancing front node
|
// Advancing front node
|
||||||
struct Node {
|
struct Node {
|
||||||
|
|
||||||
Point* point;
|
Point* point;
|
||||||
Triangle* triangle;
|
Triangle* triangle;
|
||||||
|
|
||||||
Node* next;
|
Node* next;
|
||||||
Node* prev;
|
Node* prev;
|
||||||
|
|
||||||
double value;
|
|
||||||
|
|
||||||
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),
|
double value;
|
||||||
next(NULL), prev(NULL) {}
|
|
||||||
|
Node(Point& p) : point(&p), triangle(NULL), value(p.x), next(NULL), prev(NULL)
|
||||||
/*
|
{
|
||||||
~Node() {
|
|
||||||
printf("going... ");
|
|
||||||
printf("bye node");
|
|
||||||
printf(" ... gone!\n");
|
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
Node(Point& p, Triangle& t) : point(&p), triangle(&t), value(p.x),
|
||||||
|
next(NULL), prev(NULL)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
~Node() {
|
||||||
|
printf("going... ");
|
||||||
|
printf("bye node");
|
||||||
|
printf(" ... gone!\n");
|
||||||
|
}
|
||||||
|
*/
|
||||||
};
|
};
|
||||||
|
|
||||||
// Advancing front
|
// Advancing front
|
||||||
class AdvancingFront {
|
class AdvancingFront {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
AdvancingFront();
|
AdvancingFront();
|
||||||
// Destructor
|
// Destructor
|
||||||
~AdvancingFront();
|
~AdvancingFront();
|
||||||
|
|
||||||
Node* head();
|
Node* head();
|
||||||
void set_head(Node* node);
|
void set_head(Node* node);
|
||||||
Node* tail();
|
Node* tail();
|
||||||
void set_tail(Node* node);
|
void set_tail(Node* node);
|
||||||
Node* search();
|
Node* search();
|
||||||
void set_search(Node* node);
|
void set_search(Node* node);
|
||||||
|
|
||||||
/// Locate insertion point along advancing front
|
/// Locate insertion point along advancing front
|
||||||
Node* Locate(const double& x);
|
Node* Locate(const double& x);
|
||||||
|
|
||||||
Node* LocatePoint(Point* point);
|
Node* LocatePoint(Point* point);
|
||||||
|
|
||||||
private:
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
/*
|
/*
|
||||||
* Poly2Tri Copyright (c) 2009-2010, Mason Green
|
* Poly2Tri Copyright (c) 2009-2010, Mason Green
|
||||||
* http://code.google.com/p/poly2tri/
|
* http://code.google.com/p/poly2tri/
|
||||||
*
|
*
|
||||||
@ -30,29 +30,34 @@
|
|||||||
*/
|
*/
|
||||||
#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_ = new Sweep;
|
sweep_context_ = new SweepContext(polyline, point_count);
|
||||||
|
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_;
|
delete sweep_context_;
|
||||||
|
delete sweep_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
/*
|
/*
|
||||||
* Poly2Tri Copyright (c) 2009-2010, Mason Green
|
* Poly2Tri Copyright (c) 2009-2010, Mason Green
|
||||||
* http://code.google.com/p/poly2tri/
|
* http://code.google.com/p/poly2tri/
|
||||||
*
|
*
|
||||||
@ -33,28 +33,26 @@
|
|||||||
#include "sweep_context.h"
|
#include "sweep_context.h"
|
||||||
#include "sweep.h"
|
#include "sweep.h"
|
||||||
|
|
||||||
class CDT
|
class CDT
|
||||||
{
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/// Constructor
|
/// Constructor
|
||||||
CDT(Point** poly_line, const int& point_count);
|
CDT(Point** poly_line, const int& point_count);
|
||||||
/// Add a hole
|
/// Add a hole
|
||||||
void AddHole(const Point poly_line[], const int point_count);
|
void AddHole(const Point poly_line[], const int point_count);
|
||||||
/// Triangulate points
|
/// Triangulate points
|
||||||
void Triangulate();
|
void Triangulate();
|
||||||
/// Get Delaunay triangles
|
/// Get Delaunay triangles
|
||||||
std::vector<Triangle*> GetTriangles();
|
std::vector<Triangle*> GetTriangles();
|
||||||
/// Get triangle map
|
/// Get triangle map
|
||||||
std::list<Triangle*> CDT::GetMap();
|
std::list<Triangle*> CDT::GetMap();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
SweepContext* sweep_context_;
|
SweepContext* sweep_context_;
|
||||||
Sweep* sweep_;
|
Sweep* sweep_;
|
||||||
|
|
||||||
/// Destructor
|
/// Destructor
|
||||||
~CDT();
|
~CDT();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
/*
|
/*
|
||||||
* Poly2Tri Copyright (c) 2009-2010, Mason Green
|
* Poly2Tri Copyright (c) 2009-2010, Mason Green
|
||||||
* http://code.google.com/p/poly2tri/
|
* http://code.google.com/p/poly2tri/
|
||||||
*
|
*
|
||||||
@ -34,16 +34,16 @@
|
|||||||
// Excludes exterior triangles outside constrained edges
|
// Excludes exterior triangles outside constrained edges
|
||||||
// Depth first search
|
// Depth first search
|
||||||
|
|
||||||
void Mesh::clean(Triangle& triangle)
|
void Mesh::clean(Triangle& triangle)
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
if(triangle != NULL && !triangle.interior)
|
if(triangle != NULL && !triangle.interior)
|
||||||
{
|
{
|
||||||
triangle.interior = true;
|
triangle.interior = true;
|
||||||
triangles += triangle;
|
triangles += triangle;
|
||||||
for(i <- 0 until 3)
|
for(i <- 0 until 3)
|
||||||
if(!triangle.edges(i))
|
if(!triangle.edges(i))
|
||||||
clean(triangle.neighbors(i));
|
clean(triangle.neighbors(i));
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
/*
|
/*
|
||||||
* Poly2Tri Copyright (c) 2009-2010, Mason Green
|
* Poly2Tri Copyright (c) 2009-2010, Mason Green
|
||||||
* http://code.google.com/p/poly2tri/
|
* http://code.google.com/p/poly2tri/
|
||||||
*
|
*
|
||||||
@ -33,18 +33,16 @@ using namespace std;
|
|||||||
|
|
||||||
class Triangle;
|
class Triangle;
|
||||||
|
|
||||||
class Mesh
|
class Mesh
|
||||||
{
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/// Triangles that constitute the mesh
|
/// Triangles that constitute the mesh
|
||||||
vector<Triangle> map;
|
vector<Triangle> map;
|
||||||
|
|
||||||
// Debug triangles
|
// Debug triangles
|
||||||
//val debug = new ArrayBuffer[Triangle]
|
//val debug = new ArrayBuffer[Triangle]
|
||||||
//val triangles = new ArrayBuffer[Triangle]
|
//val triangles = new ArrayBuffer[Triangle]
|
||||||
|
|
||||||
void clean(Triangle& triangle);
|
void clean(Triangle& triangle);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -1,4 +1,4 @@
|
|||||||
/*
|
/*
|
||||||
* Poly2Tri Copyright (c) 2009-2010, Mason Green
|
* Poly2Tri Copyright (c) 2009-2010, Mason Green
|
||||||
* http://code.google.com/p/poly2tri/
|
* http://code.google.com/p/poly2tri/
|
||||||
*
|
*
|
||||||
@ -33,8 +33,8 @@
|
|||||||
* Sweep-line, Constrained Delauney Triangulation (CDT) See: Domiter, V. and
|
* Sweep-line, Constrained Delauney Triangulation (CDT) See: Domiter, V. and
|
||||||
* Zalik, B.(2008)'Sweep-line algorithm for constrained Delaunay triangulation',
|
* Zalik, B.(2008)'Sweep-line algorithm for constrained Delaunay triangulation',
|
||||||
* International Journal of Geographical Information Science
|
* International Journal of Geographical Information Science
|
||||||
*
|
*
|
||||||
* "FlipScan" Constrained Edge Algorithm invented by Thomas Åhlén, thahlen@gmail.com
|
* "FlipScan" Constrained Edge Algorithm invented by Thomas Åhlén, thahlen@gmail.com
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class SweepContext;
|
class SweepContext;
|
||||||
@ -44,71 +44,69 @@ struct Edge;
|
|||||||
class Triangle;
|
class Triangle;
|
||||||
|
|
||||||
class Sweep {
|
class Sweep {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
void Triangulate(SweepContext& tcx);
|
void Triangulate(SweepContext& tcx);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
void SweepPoints(SweepContext& tcx);
|
|
||||||
|
|
||||||
Node& PointEvent(SweepContext& tcx, Point& point);
|
|
||||||
|
|
||||||
void EdgeEvent(SweepContext& tcx, Edge* edge, Node& node);
|
|
||||||
|
|
||||||
void EdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle* triangle, Point& point);
|
|
||||||
|
|
||||||
Node& NewFrontTriangle(SweepContext& tcx, Point& point, Node& node);
|
void SweepPoints(SweepContext& tcx);
|
||||||
|
|
||||||
void Fill(SweepContext& tcx, Node& node);
|
|
||||||
|
|
||||||
bool Legalize(SweepContext& tcx, Triangle& t);
|
|
||||||
|
|
||||||
bool Incircle(Point& pa, Point& pb, Point& pc, Point& pd);
|
|
||||||
|
|
||||||
void RotateTrianglePair(Triangle& t, Point& p, Triangle& ot, Point& op);
|
Node& PointEvent(SweepContext& tcx, Point& point);
|
||||||
|
|
||||||
void FillAdvancingFront(SweepContext& tcx, Node& n);
|
|
||||||
|
|
||||||
double HoleAngle(Node& node);
|
|
||||||
|
|
||||||
double BasinAngle(Node& node);
|
|
||||||
|
|
||||||
void FillBasin(SweepContext& tcx, Node& node);
|
|
||||||
|
|
||||||
void FillBasinReq(SweepContext& tcx, Node& node);
|
|
||||||
|
|
||||||
bool IsShallow(SweepContext& tcx, Node& node);
|
|
||||||
|
|
||||||
bool IsEdgeSideOfTriangle(Triangle& triangle, Point& ep, Point& eq);
|
|
||||||
|
|
||||||
void FillEdgeEvent(SweepContext& tcx, Edge* edge, Node& node);
|
|
||||||
|
|
||||||
void FillRightAboveEdgeEvent(SweepContext& tcx, Edge* edge, Node& node);
|
|
||||||
|
|
||||||
void FillRightBelowEdgeEvent(SweepContext& tcx, Edge* edge, Node& node);
|
void EdgeEvent(SweepContext& tcx, Edge* edge, Node& node);
|
||||||
|
|
||||||
void FillRightConcaveEdgeEvent(SweepContext& tcx, Edge* edge, Node& node);
|
void EdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle* triangle, Point& point);
|
||||||
|
|
||||||
void FillRightConvexEdgeEvent(SweepContext& tcx, Edge* edge, Node& node);
|
Node& NewFrontTriangle(SweepContext& tcx, Point& point, Node& node);
|
||||||
|
|
||||||
void FillLeftAboveEdgeEvent(SweepContext& tcx, Edge* edge, Node& node);
|
|
||||||
|
|
||||||
void FillLeftBelowEdgeEvent(SweepContext& tcx, Edge* edge, Node& node);
|
void Fill(SweepContext& tcx, Node& node);
|
||||||
|
|
||||||
void FillLeftConcaveEdgeEvent(SweepContext& tcx, Edge* edge, Node& node);
|
bool Legalize(SweepContext& tcx, Triangle& t);
|
||||||
|
|
||||||
void FillLeftConvexEdgeEvent(SweepContext& tcx, Edge* edge, Node& node);
|
bool Incircle(Point& pa, Point& pb, Point& pc, Point& pd);
|
||||||
|
|
||||||
void FlipEdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle& t, Point& p);
|
|
||||||
|
|
||||||
Triangle& NextFlipTriangle(SweepContext& tcx, int o, Triangle& t, Triangle& ot, Point& p, Point& op);
|
|
||||||
|
|
||||||
Point& NextFlipPoint(Point& ep, Point& eq, Triangle& ot, Point& op );
|
|
||||||
|
|
||||||
void FlipScanEdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle& flip_triangle, Triangle& t, Point& p);
|
|
||||||
|
|
||||||
void FinalizationPolygon(SweepContext& tcx);
|
void RotateTrianglePair(Triangle& t, Point& p, Triangle& ot, Point& op);
|
||||||
|
|
||||||
|
void FillAdvancingFront(SweepContext& tcx, Node& n);
|
||||||
|
|
||||||
|
double HoleAngle(Node& node);
|
||||||
|
|
||||||
|
double BasinAngle(Node& node);
|
||||||
|
|
||||||
|
void FillBasin(SweepContext& tcx, Node& node);
|
||||||
|
|
||||||
|
void FillBasinReq(SweepContext& tcx, Node& node);
|
||||||
|
|
||||||
|
bool IsShallow(SweepContext& tcx, Node& node);
|
||||||
|
|
||||||
|
bool IsEdgeSideOfTriangle(Triangle& triangle, Point& ep, Point& eq);
|
||||||
|
|
||||||
|
void FillEdgeEvent(SweepContext& tcx, Edge* edge, Node& node);
|
||||||
|
|
||||||
|
void FillRightAboveEdgeEvent(SweepContext& tcx, Edge* edge, Node& node);
|
||||||
|
|
||||||
|
void FillRightBelowEdgeEvent(SweepContext& tcx, Edge* edge, Node& node);
|
||||||
|
|
||||||
|
void FillRightConcaveEdgeEvent(SweepContext& tcx, Edge* edge, Node& node);
|
||||||
|
|
||||||
|
void FillRightConvexEdgeEvent(SweepContext& tcx, Edge* edge, Node& node);
|
||||||
|
|
||||||
|
void FillLeftAboveEdgeEvent(SweepContext& tcx, Edge* edge, Node& node);
|
||||||
|
|
||||||
|
void FillLeftBelowEdgeEvent(SweepContext& tcx, Edge* edge, Node& node);
|
||||||
|
|
||||||
|
void FillLeftConcaveEdgeEvent(SweepContext& tcx, Edge* edge, Node& node);
|
||||||
|
|
||||||
|
void FillLeftConvexEdgeEvent(SweepContext& tcx, Edge* edge, Node& node);
|
||||||
|
|
||||||
|
void FlipEdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle& t, Point& p);
|
||||||
|
|
||||||
|
Triangle& NextFlipTriangle(SweepContext& tcx, int o, Triangle& t, Triangle& ot, Point& p, Point& op);
|
||||||
|
|
||||||
|
Point& NextFlipPoint(Point& ep, Point& eq, Triangle& ot, Point& op);
|
||||||
|
|
||||||
|
void FlipScanEdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle& flip_triangle, Triangle& t, Point& p);
|
||||||
|
|
||||||
|
void FinalizationPolygon(SweepContext& tcx);
|
||||||
};
|
};
|
||||||
|
@ -4,157 +4,162 @@
|
|||||||
#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();
|
||||||
|
|
||||||
points_ = polyline;
|
points_ = polyline;
|
||||||
point_count_ = point_count;
|
point_count_ = 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);
|
||||||
|
|
||||||
// Calculate bounds.
|
// Calculate bounds.
|
||||||
for(int i = 0; i < point_count_; i++) {
|
for (int i = 0; i < point_count_; 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
double dx = kAlpha * ( xmax - xmin );
|
double dx = kAlpha * (xmax - xmin);
|
||||||
double dy = kAlpha * ( ymax - ymin );
|
double dy = kAlpha * (ymax - ymin);
|
||||||
head_ = new Point(xmax + dx, ymin - dy);
|
head_ = new Point(xmax + dx, ymin - dy);
|
||||||
tail_ = new Point(xmin - dx, ymin - dy);
|
tail_ = new Point(xmin - dx, ymin - dy);
|
||||||
|
|
||||||
// Sort points along y-axis
|
// Sort points along y-axis
|
||||||
double init_time = glfwGetTime();
|
double init_time = glfwGetTime();
|
||||||
std::sort(points_, points_ + point_count_, cmp);
|
std::sort(points_, points_ + point_count_, cmp);
|
||||||
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");
|
/*
|
||||||
for(int i = 0; i < edge_list.size(); i++) {
|
printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
|
||||||
edge_list[i]->p->DebugPrint(); edge_list[i]->q->DebugPrint();
|
for(int i = 0; i < edge_list.size(); i++) {
|
||||||
printf("%p, %p\n", edge_list[i]->p, edge_list[i]->q);
|
edge_list[i]->p->DebugPrint(); edge_list[i]->q->DebugPrint();
|
||||||
}
|
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]));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
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);
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
Point* SweepContext::GetPoint(const int& index) {
|
|
||||||
return points_[index];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SweepContext::AddToMap(Triangle* triangle ) {
|
Point* SweepContext::GetPoint(const int& index)
|
||||||
|
{
|
||||||
|
return points_[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
{
|
||||||
return *front_->Locate(point.x);
|
// TODO implement search tree
|
||||||
|
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_);
|
||||||
|
|
||||||
map_.push_back(triangle);
|
map_.push_back(triangle);
|
||||||
|
|
||||||
front_ = new AdvancingFront;
|
front_ = new AdvancingFront;
|
||||||
|
|
||||||
front_->set_head(new Node(*triangle->GetPoint(1)));
|
front_->set_head(new Node(*triangle->GetPoint(1)));
|
||||||
front_->head()->triangle = triangle;
|
front_->head()->triangle = triangle;
|
||||||
Node* middle = new Node(*triangle->GetPoint(0));
|
Node* middle = new Node(*triangle->GetPoint(0));
|
||||||
middle->triangle = triangle;
|
middle->triangle = triangle;
|
||||||
front_->set_tail(new Node(*triangle->GetPoint(2)));
|
front_->set_tail(new Node(*triangle->GetPoint(2)));
|
||||||
front_->set_search(middle);
|
front_->set_search(middle);
|
||||||
|
|
||||||
// TODO: More intuitive if head is middles next and not previous?
|
// TODO: More intuitive if head is middles next and not previous?
|
||||||
// so swap head and tail
|
// so swap head and tail
|
||||||
front_->head()->next = middle;
|
front_->head()->next = middle;
|
||||||
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++) {
|
{
|
||||||
if(t.GetNeighbor(i) == NULL) {
|
for (int i = 0; i < 3; i++) {
|
||||||
|
if (t.GetNeighbor(i) == NULL) {
|
||||||
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) {
|
|
||||||
map_.remove(triangle);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SweepContext::MeshClean(Triangle& triangle ) {
|
|
||||||
|
|
||||||
if(&triangle != NULL && !triangle.IsInterior()) {
|
|
||||||
triangle.IsInterior(true);
|
|
||||||
triangles_.push_back(&triangle);
|
|
||||||
for(int i = 0; i < 3; i++) {
|
|
||||||
if(!triangle.constrained_edge[i])
|
|
||||||
MeshClean(*triangle.GetNeighbor(i));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SweepContext::~SweepContext() {
|
void SweepContext::RemoveFromMap(Triangle* triangle)
|
||||||
|
{
|
||||||
|
map_.remove(triangle);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SweepContext::MeshClean(Triangle& triangle)
|
||||||
|
{
|
||||||
|
if (&triangle != NULL && !triangle.IsInterior()) {
|
||||||
|
triangle.IsInterior(true);
|
||||||
|
triangles_.push_back(&triangle);
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
if (!triangle.constrained_edge[i])
|
||||||
|
MeshClean(*triangle.GetNeighbor(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SweepContext::~SweepContext()
|
||||||
|
{
|
||||||
delete head_;
|
delete head_;
|
||||||
delete tail_;
|
delete tail_;
|
||||||
delete front_;
|
delete front_;
|
||||||
|
@ -31,7 +31,7 @@
|
|||||||
#include <list>
|
#include <list>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
// 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;
|
||||||
|
|
||||||
@ -41,124 +41,141 @@ struct Node;
|
|||||||
struct Edge;
|
struct Edge;
|
||||||
class AdvancingFront;
|
class AdvancingFront;
|
||||||
|
|
||||||
|
|
||||||
class SweepContext {
|
|
||||||
|
|
||||||
|
class SweepContext {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
SweepContext(Point** polyline, const int& point_count);
|
SweepContext(Point** polyline, const int& point_count);
|
||||||
// Destructor
|
// Destructor
|
||||||
~SweepContext();
|
~SweepContext();
|
||||||
|
|
||||||
//void MeshClean(Triangle& triangle);
|
|
||||||
// Get Advancing Front
|
|
||||||
//AdvancingFront front();
|
|
||||||
|
|
||||||
void set_head(Point* p1);
|
|
||||||
Point* head();
|
|
||||||
|
|
||||||
void set_tail(Point* p1 );
|
//void MeshClean(Triangle& triangle);
|
||||||
Point* tail();
|
// Get Advancing Front
|
||||||
|
//AdvancingFront front();
|
||||||
int point_count();
|
|
||||||
|
|
||||||
Node& LocateNode(Point& point);
|
|
||||||
void RemoveNode(Node* node);
|
|
||||||
|
|
||||||
void CreateAdvancingFront();
|
|
||||||
|
|
||||||
// Try to map a node to all sides of this triangle that don't have a neighbor
|
|
||||||
void MapTriangleToNodes(Triangle& t);
|
|
||||||
|
|
||||||
void AddToMap(Triangle* triangle);
|
|
||||||
|
|
||||||
Point* GetPoint(const int& index);
|
|
||||||
Point* GetPoints();
|
|
||||||
|
|
||||||
void RemoveFromMap(Triangle* triangle);
|
|
||||||
|
|
||||||
AdvancingFront* front();
|
|
||||||
|
|
||||||
void MeshClean(Triangle& triangle);
|
|
||||||
|
|
||||||
std::vector<Triangle*> GetTriangles();
|
|
||||||
std::list<Triangle*> GetMap();
|
|
||||||
|
|
||||||
std::vector<Edge*> edge_list;
|
|
||||||
|
|
||||||
struct Basin {
|
|
||||||
|
|
||||||
Node* left_node;
|
|
||||||
Node* bottom_node;
|
|
||||||
Node* right_node;
|
|
||||||
double width;
|
|
||||||
bool left_highest;
|
|
||||||
|
|
||||||
Basin() : left_node(NULL), bottom_node(NULL), right_node(NULL),
|
|
||||||
width(0.0), left_highest(false) {}
|
|
||||||
|
|
||||||
void Clear() {
|
|
||||||
left_node = NULL;
|
|
||||||
bottom_node = NULL;
|
|
||||||
right_node = NULL;
|
|
||||||
width = 0.0;
|
|
||||||
left_highest = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
struct EdgeEvent {
|
|
||||||
|
|
||||||
Edge* constrained_edge;
|
|
||||||
bool right;
|
|
||||||
|
|
||||||
EdgeEvent() : constrained_edge(NULL), right(false) {}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
Basin basin;
|
|
||||||
EdgeEvent edge_event;
|
|
||||||
|
|
||||||
private:
|
|
||||||
|
|
||||||
std::vector<Triangle*> triangles_;
|
void set_head(Point* p1);
|
||||||
std::list<Triangle*> map_;
|
Point* head();
|
||||||
|
|
||||||
Point** points_;
|
void set_tail(Point* p1);
|
||||||
int point_count_;
|
Point* tail();
|
||||||
|
|
||||||
// Advancing front
|
int point_count();
|
||||||
AdvancingFront* front_;
|
|
||||||
// head point used with advancing front
|
Node& LocateNode(Point& point);
|
||||||
Point* head_;
|
void RemoveNode(Node* node);
|
||||||
// tail point used with advancing front
|
|
||||||
Point* tail_;
|
void CreateAdvancingFront();
|
||||||
|
|
||||||
//EdgeEvent edgeEvent = new EdgeEvent();
|
// Try to map a node to all sides of this triangle that don't have a neighbor
|
||||||
|
void MapTriangleToNodes(Triangle& t);
|
||||||
void InitTriangulation();
|
|
||||||
void InitEdges(Point** polyline, const int& point_count);
|
void AddToMap(Triangle* triangle);
|
||||||
|
|
||||||
//void MeshCleanReq(Triangle& triangle )
|
Point* GetPoint(const int& index);
|
||||||
|
Point* GetPoints();
|
||||||
/*
|
|
||||||
class EdgeEvent {
|
void RemoveFromMap(Triangle* triangle);
|
||||||
Edge* constrainedEdge;
|
|
||||||
bool right;
|
AdvancingFront* front();
|
||||||
};
|
|
||||||
*/
|
void MeshClean(Triangle& triangle);
|
||||||
|
|
||||||
|
std::vector<Triangle*> GetTriangles();
|
||||||
|
std::list<Triangle*> GetMap();
|
||||||
|
|
||||||
|
std::vector<Edge*> edge_list;
|
||||||
|
|
||||||
|
struct Basin {
|
||||||
|
Node* left_node;
|
||||||
|
Node* bottom_node;
|
||||||
|
Node* right_node;
|
||||||
|
double width;
|
||||||
|
bool left_highest;
|
||||||
|
|
||||||
|
Basin() : left_node(NULL), bottom_node(NULL), right_node(NULL),
|
||||||
|
width(0.0), left_highest(false)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void Clear()
|
||||||
|
{
|
||||||
|
left_node = NULL;
|
||||||
|
bottom_node = NULL;
|
||||||
|
right_node = NULL;
|
||||||
|
width = 0.0;
|
||||||
|
left_highest = false;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
inline AdvancingFront* SweepContext::front() { return front_; }
|
struct EdgeEvent {
|
||||||
|
Edge* constrained_edge;
|
||||||
|
bool right;
|
||||||
|
|
||||||
inline int SweepContext::point_count() { return point_count_; }
|
EdgeEvent() : constrained_edge(NULL), right(false)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
inline void SweepContext::set_head(Point* p1) { head_ = p1; }
|
Basin basin;
|
||||||
|
EdgeEvent edge_event;
|
||||||
|
|
||||||
inline Point* SweepContext::head() { return head_; }
|
private:
|
||||||
|
|
||||||
inline void SweepContext::set_tail(Point* p1) { tail_ = p1; }
|
std::vector<Triangle*> triangles_;
|
||||||
|
std::list<Triangle*> map_;
|
||||||
|
|
||||||
inline Point* SweepContext::tail() { return tail_; }
|
Point** points_;
|
||||||
|
int point_count_;
|
||||||
|
|
||||||
|
// Advancing front
|
||||||
|
AdvancingFront* front_;
|
||||||
|
// head point used with advancing front
|
||||||
|
Point* head_;
|
||||||
|
// tail point used with advancing front
|
||||||
|
Point* tail_;
|
||||||
|
|
||||||
|
//EdgeEvent edgeEvent = new EdgeEvent();
|
||||||
|
|
||||||
|
void InitTriangulation();
|
||||||
|
void InitEdges(Point** polyline, const int& point_count);
|
||||||
|
|
||||||
|
//void MeshCleanReq(Triangle& triangle )
|
||||||
|
|
||||||
|
/*
|
||||||
|
class EdgeEvent {
|
||||||
|
Edge* constrainedEdge;
|
||||||
|
bool right;
|
||||||
|
};
|
||||||
|
*/
|
||||||
|
};
|
||||||
|
|
||||||
|
inline AdvancingFront* SweepContext::front()
|
||||||
|
{
|
||||||
|
return front_;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int SweepContext::point_count()
|
||||||
|
{
|
||||||
|
return point_count_;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void SweepContext::set_head(Point* p1)
|
||||||
|
{
|
||||||
|
head_ = p1;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Point* SweepContext::head()
|
||||||
|
{
|
||||||
|
return head_;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void SweepContext::set_tail(Point* p1)
|
||||||
|
{
|
||||||
|
tail_ = p1;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Point* SweepContext::tail()
|
||||||
|
{
|
||||||
|
return tail_;
|
||||||
|
}
|
||||||
|
209
testbed/main.cc
209
testbed/main.cc
@ -1,4 +1,4 @@
|
|||||||
/*
|
/*
|
||||||
* Poly2Tri Copyright (c) 2009-2010, Mason Green
|
* Poly2Tri Copyright (c) 2009-2010, Mason Green
|
||||||
* http://code.google.com/p/poly2tri/
|
* http://code.google.com/p/poly2tri/
|
||||||
*
|
*
|
||||||
@ -58,50 +58,51 @@ 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))
|
||||||
return 0;
|
return 0;
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
// initialize random seed:
|
// initialize random seed:
|
||||||
srand ( time(NULL) );
|
srand ( time(NULL) );
|
||||||
|
|
||||||
int a = 0;
|
int a = 0;
|
||||||
int b = 2000;
|
int b = 2000;
|
||||||
|
|
||||||
for(int i = 0; i < num_points; i++) {
|
for(int i = 0; i < num_points; i++) {
|
||||||
double x = rand() % (b - a - 1) + a + 1;
|
double x = rand() % (b - a - 1) + a + 1;
|
||||||
double y = rand() % (b - a - 1) + a + 1;
|
double y = rand() % (b - a - 1) + a + 1;
|
||||||
polyline[i] = Point(x, y);
|
polyline[i] = Point(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
string line;
|
string line;
|
||||||
ifstream myfile (argv[1]);
|
ifstream myfile(argv[1]);
|
||||||
vector<Point*> points;
|
vector<Point*> points;
|
||||||
if (myfile.is_open()) {
|
if (myfile.is_open()) {
|
||||||
while (!myfile.eof()) {
|
while (!myfile.eof()) {
|
||||||
getline (myfile,line);
|
getline(myfile, line);
|
||||||
if(line.size() == 0) {
|
if (line.size() == 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
istringstream iss(line);
|
istringstream iss(line);
|
||||||
vector<string> tokens;
|
vector<string> tokens;
|
||||||
copy(istream_iterator<string>(iss), istream_iterator<string>(),
|
copy(istream_iterator<string>(iss), istream_iterator<string>(),
|
||||||
back_inserter<vector<string> >(tokens));
|
back_inserter<vector<string> >(tokens));
|
||||||
double x = StringToDouble(tokens[0]);
|
double x = StringToDouble(tokens[0]);
|
||||||
double y = StringToDouble(tokens[1]);
|
double y = StringToDouble(tokens[1]);
|
||||||
@ -111,187 +112,179 @@ int main(int argc, char* argv[]) {
|
|||||||
} else {
|
} else {
|
||||||
cout << "File not opened" << endl;
|
cout << "File not opened" << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
int num_points = points.size();
|
int num_points = points.size();
|
||||||
cout << "Number of points = " << num_points << endl;
|
cout << "Number of points = " << num_points << endl;
|
||||||
|
|
||||||
Point** polyline = new Point *[num_points];
|
Point** polyline = new Point *[num_points];
|
||||||
for(int i = 0; i < num_points; i++) {
|
for (int i = 0; i < num_points; i++) {
|
||||||
polyline[i] = points[i];
|
polyline[i] = points[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
Init();
|
Init();
|
||||||
|
|
||||||
// Perform triangulation
|
// Perform triangulation
|
||||||
double init_time = glfwGetTime();
|
double init_time = glfwGetTime();
|
||||||
CDT * cdt = new CDT(polyline, num_points);
|
CDT * cdt = new CDT(polyline, num_points);
|
||||||
cdt->Triangulate();
|
cdt->Triangulate();
|
||||||
double dt = glfwGetTime() - init_time;
|
double dt = glfwGetTime() - init_time;
|
||||||
cout << "Elapsed time (secs) = " << dt << endl;
|
cout << "Elapsed time (secs) = " << dt << endl;
|
||||||
|
|
||||||
triangles = cdt->GetTriangles();
|
triangles = cdt->GetTriangles();
|
||||||
map = cdt->GetMap();
|
map = cdt->GetMap();
|
||||||
|
|
||||||
MainLoop(atof(argv[2]));
|
MainLoop(atof(argv[2]));
|
||||||
|
|
||||||
delete [] polyline;
|
delete [] polyline;
|
||||||
ShutDown(0);
|
ShutDown(0);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Init()
|
void Init()
|
||||||
{
|
{
|
||||||
const int window_width = 800,
|
const int window_width = 800,
|
||||||
window_height = 600;
|
window_height = 600;
|
||||||
|
|
||||||
if (glfwInit() != GL_TRUE)
|
if (glfwInit() != GL_TRUE)
|
||||||
ShutDown(1);
|
ShutDown(1);
|
||||||
// 800 x 600, 16 bit color, no depth, alpha or stencil buffers, windowed
|
// 800 x 600, 16 bit color, no depth, alpha or stencil buffers, windowed
|
||||||
if (glfwOpenWindow(window_width, window_height, 5, 6, 5, 0, 0, 0, GLFW_WINDOW) != GL_TRUE)
|
if (glfwOpenWindow(window_width, window_height, 5, 6, 5, 0, 0, 0, GLFW_WINDOW) != GL_TRUE)
|
||||||
ShutDown(1);
|
ShutDown(1);
|
||||||
|
|
||||||
glfwSetWindowTitle("Poly2Tri - C++");
|
glfwSetWindowTitle("Poly2Tri - C++");
|
||||||
|
|
||||||
glEnable(GL_BLEND);
|
glEnable(GL_BLEND);
|
||||||
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)
|
||||||
{
|
{
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
exit(return_code);
|
exit(return_code);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainLoop(const double zoom)
|
void MainLoop(const double zoom)
|
||||||
{
|
{
|
||||||
// the time of the previous frame
|
// the time of the previous frame
|
||||||
double old_time = glfwGetTime();
|
double old_time = glfwGetTime();
|
||||||
// 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;
|
||||||
old_time = current_time;
|
old_time = current_time;
|
||||||
|
|
||||||
// escape to quit, arrow keys to rotate view
|
// escape to quit, arrow keys to rotate view
|
||||||
// Check if ESC key was pressed or window was closed
|
// Check if ESC key was pressed or window was closed
|
||||||
running = !glfwGetKey( GLFW_KEY_ESC ) && glfwGetWindowParam( GLFW_OPENED );
|
running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam(GLFW_OPENED);
|
||||||
|
|
||||||
if (glfwGetKey(GLFW_KEY_LEFT) == GLFW_PRESS)
|
if (glfwGetKey(GLFW_KEY_LEFT) == GLFW_PRESS)
|
||||||
rotate_y += delta_rotate;
|
rotate_y += delta_rotate;
|
||||||
if (glfwGetKey(GLFW_KEY_RIGHT) == GLFW_PRESS)
|
if (glfwGetKey(GLFW_KEY_RIGHT) == GLFW_PRESS)
|
||||||
rotate_y -= delta_rotate;
|
rotate_y -= delta_rotate;
|
||||||
// z axis always rotates
|
// z axis always rotates
|
||||||
rotate_z += delta_rotate;
|
rotate_z += delta_rotate;
|
||||||
|
|
||||||
// Draw the scene
|
// Draw the scene
|
||||||
if(draw_map) {
|
if (draw_map) {
|
||||||
DrawMap(zoom);
|
DrawMap(zoom);
|
||||||
} else {
|
} else {
|
||||||
Draw(zoom);
|
Draw(zoom);
|
||||||
}
|
}
|
||||||
|
|
||||||
// swap back and front buffers
|
// swap back and front buffers
|
||||||
glfwSwapBuffers();
|
glfwSwapBuffers();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 right = width / zoom;
|
||||||
|
double bottom = -height / zoom;
|
||||||
|
double top = height / zoom;
|
||||||
|
|
||||||
double left = -width / zoom;
|
// Reset viewport
|
||||||
double right = width / zoom;
|
glLoadIdentity();
|
||||||
double bottom = -height / zoom;
|
glMatrixMode(GL_PROJECTION);
|
||||||
double top = height / zoom;
|
glLoadIdentity();
|
||||||
|
|
||||||
// Reset viewport
|
// Reset ortho view
|
||||||
glLoadIdentity();
|
glOrtho(left, right, bottom, top, 1, -1);
|
||||||
glMatrixMode(GL_PROJECTION);
|
glTranslatef(-cx, -cy, 0);
|
||||||
glLoadIdentity();
|
glMatrixMode(GL_MODELVIEW);
|
||||||
|
glDisable(GL_DEPTH_TEST);
|
||||||
// Reset ortho view
|
glLoadIdentity();
|
||||||
glOrtho(left, right, bottom, top, 1, -1);
|
|
||||||
glTranslatef(-cx, -cy, 0);
|
// Clear the screen
|
||||||
glMatrixMode(GL_MODELVIEW);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
glDisable(GL_DEPTH_TEST);
|
|
||||||
glLoadIdentity();
|
|
||||||
|
|
||||||
// Clear the screen
|
|
||||||
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);
|
||||||
Point& c = *t.GetPoint(2);
|
Point& c = *t.GetPoint(2);
|
||||||
|
|
||||||
// Red
|
// Red
|
||||||
glColor3f(1, 0, 0);
|
glColor3f(1, 0, 0);
|
||||||
|
|
||||||
glBegin(GL_LINE_LOOP);
|
|
||||||
glVertex2f(a.x, a.y);
|
|
||||||
glVertex2f(b.x, b.y);
|
|
||||||
glVertex2f(c.x, c.y);
|
|
||||||
glEnd();
|
|
||||||
|
|
||||||
|
glBegin(GL_LINE_LOOP);
|
||||||
|
glVertex2f(a.x, a.y);
|
||||||
|
glVertex2f(b.x, b.y);
|
||||||
|
glVertex2f(c.x, c.y);
|
||||||
|
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);
|
||||||
|
|
||||||
ResetZoom(zoom, center.x, center.y, 800, 600);
|
ResetZoom(zoom, center.x, center.y, 800, 600);
|
||||||
|
|
||||||
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);
|
||||||
Point& c = *t.GetPoint(2);
|
Point& c = *t.GetPoint(2);
|
||||||
|
|
||||||
ConstrainedColor(t.constrained_edge[2]);
|
ConstrainedColor(t.constrained_edge[2]);
|
||||||
glBegin(GL_LINES);
|
glBegin(GL_LINES);
|
||||||
glVertex2f(a.x, a.y);
|
glVertex2f(a.x, a.y);
|
||||||
glVertex2f(b.x, b.y);
|
glVertex2f(b.x, b.y);
|
||||||
glEnd( );
|
glEnd( );
|
||||||
|
|
||||||
ConstrainedColor(t.constrained_edge[0]);
|
ConstrainedColor(t.constrained_edge[0]);
|
||||||
glBegin(GL_LINES);
|
glBegin(GL_LINES);
|
||||||
glVertex2f(b.x, b.y);
|
glVertex2f(b.x, b.y);
|
||||||
glVertex2f(c.x, c.y);
|
glVertex2f(c.x, c.y);
|
||||||
glEnd( );
|
glEnd( );
|
||||||
|
|
||||||
ConstrainedColor(t.constrained_edge[1]);
|
ConstrainedColor(t.constrained_edge[1]);
|
||||||
glBegin(GL_LINES);
|
glBegin(GL_LINES);
|
||||||
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);
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user