fixed more pointer bugs

This commit is contained in:
zzzzrrr 2010-01-20 12:10:58 -05:00
parent d776b54e57
commit 0bb55b95b8
11 changed files with 145 additions and 148 deletions

View File

@ -36,11 +36,7 @@ Triangle::Triangle(Point& a, Point& b, Point& 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;
}
Triangle::~Triangle() {
printf("bye triangle\n");
} }
// Update neighbor pointers // Update neighbor pointers
@ -140,8 +136,7 @@ int Triangle::EdgeIndex(const Point* p1, const Point* p2) {
} 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) {
@ -162,16 +157,7 @@ void Triangle::MarkConstrainedEdge(const int index) {
} }
void Triangle::MarkConstrainedEdge(Edge& edge) { void Triangle::MarkConstrainedEdge(Edge& edge) {
MarkConstrainedEdge(edge.p, edge.q); MarkConstrainedEdge(edge.p, edge.q);
if((edge.q == points_[0] && edge.p == points_[1]) || (edge.q == points_[1] && edge.p == points_[0])) {
constrained_edge[2] = true;
} else if((edge.q == points_[0] && edge.p == points_[2]) || (edge.q == points_[2] && edge.p == points_[0])) {
constrained_edge[1] = true;
} else if(( edge.q == points_[1] && edge.p == points_[2]) || (edge.q == points_[2] && edge.p == points_[1])) {
constrained_edge[0] = true;
}
} }
// Mark edge as constrained // Mark edge as constrained

View File

@ -92,18 +92,6 @@ struct Point {
return len; return len;
} }
bool operator < (Point& b) {
if (y < b.y) {
return true;
} else if (y == b.y) {
// Make sure q is point with greater x value
if(x < b.x) {
return true;
}
}
return false;
}
}; };
// Represents a simple polygon's edge // Represents a simple polygon's edge
@ -128,7 +116,7 @@ struct Edge {
} }
q->edge_list.push_back(*this); q->edge_list.push_back(*this);
//printf("%i\n", q->edge_list.size());
} }
}; };
@ -142,15 +130,11 @@ public:
/// Constructor /// Constructor
Triangle(Point& a, Point& b, Point& c); Triangle(Point& a, Point& b, Point& c);
// Destroctor
~Triangle();
/// 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];
/// Has this triangle been marked as an interior triangle?
bool interior;
Point* GetPoint(const int& index); Point* GetPoint(const int& index);
Point* PointCW(Point& point); Point* PointCW(Point& point);
@ -187,6 +171,9 @@ public:
void ClearNeighbors(); void ClearNeighbors();
void ClearDelunayEdges(); void ClearDelunayEdges();
inline bool IsInterior();
inline void IsInterior(bool b);
Triangle& NeighborAcross(Point& opoint); Triangle& NeighborAcross(Point& opoint);
void DebugPrint(); void DebugPrint();
@ -198,14 +185,17 @@ private:
/// Neighbor list /// Neighbor list
Triangle* neighbors_[3]; Triangle* neighbors_[3];
/// Has this triangle been marked as an interior triangle?
bool interior_;
}; };
inline bool operator < (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;
} }
} }
@ -235,10 +225,6 @@ 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 Node& a, const Node& b) {
return a != b;
}
/// 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;
@ -281,5 +267,13 @@ inline bool Triangle::Contains(Point* p, Point* q) {
return Contains(p) && Contains(q); return Contains(p) && Contains(q);
} }
inline bool Triangle::IsInterior() {
return interior_;
}
inline void Triangle::IsInterior(bool b) {
interior_ = b;
}
#endif #endif

View File

@ -43,7 +43,7 @@ void CDT::Triangulate() {
sweep_->Triangulate(*sweep_context_); sweep_->Triangulate(*sweep_context_);
} }
std::list<Triangle*> CDT::GetTriangles() { std::vector<Triangle*> CDT::GetTriangles() {
return sweep_context_->GetTriangles(); return sweep_context_->GetTriangles();
} }

View File

@ -45,7 +45,7 @@ public:
/// Triangulate points /// Triangulate points
void Triangulate(); void Triangulate();
/// Get Delaunay triangles /// Get Delaunay triangles
std::list<Triangle*> GetTriangles(); std::vector<Triangle*> GetTriangles();
private: private:

View File

@ -37,27 +37,18 @@
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
/* FinalizationPolygon(tcx);
// Finalize triangulation
if( tcx.getTriangulationMode() == TriangulationMode.Polygon ) {
finalizationPolygon( tcx );
} else {
finalizationConvexHull( 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++ ) {
Point& point = *tcx.GetPoint(i); Point& point = *tcx.GetPoint(i);
Node& node = PointEvent(tcx, point); Node& node = PointEvent(tcx, point);
for(int i = 0; i < point.edge_list.size(); i++) { for(int i = 0; i < point.edge_list.size(); i++) {
@ -68,6 +59,19 @@ void Sweep::SweepPoints(SweepContext& tcx ) {
} }
void Sweep::FinalizationPolygon(SweepContext& tcx) {
// Get an Internal triangle to start with
Triangle* t = tcx.front()->head()->next->triangle;
Point* p = tcx.front()->head()->next->point;
while(!t->GetConstrainedEdgeCW(*p)) {
t = t->NeighborCCW(*p);
}
// Collect interior triangles constrained by edges
tcx.MeshClean(*t);
}
/** /**
* Find closes node to the left of the new point and * Find closes node to the left of the new point and
* create a new triangle. If needed new holes and basins * create a new triangle. If needed new holes and basins
@ -615,7 +619,7 @@ void Sweep::TurnAdvancingFrontConvex(SweepContext& tcx, Node& b, Node& c) {
Node& first = b; Node& first = b;
while(c != *tcx.front()->tail()) { while(&c != tcx.front()->tail()) {
if(Orient2d(*b.point, *c.point, *c.next->point) == CCW) { if(Orient2d(*b.point, *c.point, *c.next->point) == CCW) {
// [b,c,d] Concave - fill around c // [b,c,d] Concave - fill around c
@ -623,7 +627,7 @@ void Sweep::TurnAdvancingFrontConvex(SweepContext& tcx, Node& b, Node& c) {
c = *c.next; c = *c.next;
} else { } else {
// [b,c,d] Convex // [b,c,d] Convex
if(b != first && Orient2d(*b.prev->point, *b.point, *c.point) == CCW) { if(&b != &first && Orient2d(*b.prev->point, *b.point, *c.point) == CCW) {
// [a,b,c] Concave - fill around b // [a,b,c] Concave - fill around b
Fill(tcx, b); Fill(tcx, b);
b = *b.prev; b = *b.prev;

View File

@ -113,4 +113,6 @@ 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);
}; };

View File

@ -9,27 +9,26 @@ SweepContext::SweepContext(Point** polyline, const int& point_count) {
basin = Basin(); basin = Basin();
edge_event = EdgeEvent(); edge_event = EdgeEvent();
for(int i = 0; i < point_count; i++) { points_ = polyline;
points_.push_back(**&polyline[i]); point_count_ = point_count;
}
InitEdges(polyline, point_count); InitEdges(points_, point_count_);
InitTriangulation(); InitTriangulation();
} }
std::list<Triangle*> SweepContext::GetTriangles() { std::vector<Triangle*> SweepContext::GetTriangles() {
return tri_list_; return triangles_;
} }
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 < points_.size(); 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)
@ -47,26 +46,46 @@ void SweepContext::InitTriangulation() {
// Sort points along y-axis // Sort points along y-axis
double init_time = glfwGetTime(); double init_time = glfwGetTime();
std::sort(points_.begin(), points_.end()); 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");
for(int i = 0; i < point_count_; i++) {
printf("%p ", points_[i]);
printf("%f,%f\n", points_[i]->x, points_[i]->y);
}
printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
for(int i = 0; i < edge_list.size(); i++) {
printf("%p, %p\n", edge_list[i]->p, edge_list[i]->q);
}
*/
} }
void SweepContext::InitEdges(Point** polyline, const int& point_count) { 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 < points_.size() - 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++) {
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 ) {
tri_list_.push_back(triangle); map_.push_back(triangle);
} }
Node& SweepContext::LocateNode(Point& point) { Node& SweepContext::LocateNode(Point& point) {
@ -77,9 +96,9 @@ Node& SweepContext::LocateNode(Point& point) {
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_);
tri_list_.push_back(triangle); map_.push_back(triangle);
front_ = new AdvancingFront; front_ = new AdvancingFront;
@ -114,46 +133,22 @@ void SweepContext::MapTriangleToNodes(Triangle& t) {
} }
void SweepContext::RemoveFromMap(Triangle* triangle) { void SweepContext::RemoveFromMap(Triangle* triangle) {
tri_list_.remove(triangle); map_.remove(triangle);
} }
/* void SweepContext::MeshClean(Triangle& triangle ) {
void SweepContext::MeshClean(Triangle& triangle) {
pointset_.ClearTriangulation();
MeshCleanReq(triangle);
}
AFront SweepContext::front_() { if(&triangle != NULL && !triangle.IsInterior()) {
return front_;
}
void SweepContext::Clear() {
super.clear();
tri_list_.Clear();
}
Node* SweepContext::LocateNode(Point& point) {
// TODO implement tree
return front_.Locate(point.x);
}
/*
void SweepContext::MeshCleanReq(Triangle& triangle ) {
if(triangle != NULL && !triangle.isInterior()) {
triangle.IsInterior(true); triangle.IsInterior(true);
pointset_.AddTriangle(triangle); triangles_.push_back(&triangle);
for(int i = 0; i < 3; i++) { for(int i = 0; i < 3; i++) {
if(!triangle.cEdge[i]) if(!triangle.constrained_edge[i])
MeshCleanReq(triangle.neighbors[i]); MeshClean(*triangle.GetNeighbor(i));
} }
} }
} }
*/
SweepContext::~SweepContext() { SweepContext::~SweepContext() {
//delete [] points_;
delete head_; delete head_;
delete tail_; delete tail_;
delete front_; delete front_;

View File

@ -80,7 +80,9 @@ public:
AdvancingFront* front(); AdvancingFront* front();
std::list<Triangle*> GetTriangles(); void MeshClean(Triangle& triangle);
std::vector<Triangle*> GetTriangles();
std::vector<Edge*> edge_list; std::vector<Edge*> edge_list;
@ -119,8 +121,11 @@ public:
private: private:
std::list<Triangle*> tri_list_; std::vector<Triangle*> triangles_;
std::vector<Point> points_; std::list<Triangle*> map_;
Point** points_;
int point_count_;
// Advancing front // Advancing front
AdvancingFront* front_; AdvancingFront* front_;
@ -147,7 +152,7 @@ private:
inline AdvancingFront* SweepContext::front() { return front_; } inline AdvancingFront* SweepContext::front() { return front_; }
inline int SweepContext::point_count() { return points_.size(); } inline int SweepContext::point_count() { return point_count_; }
inline void SweepContext::set_head(Point* p1) { head_ = p1; } inline void SweepContext::set_head(Point* p1) { head_ = p1; }

View File

@ -46,12 +46,13 @@ void Init();
void ShutDown(int return_code); void ShutDown(int return_code);
void MainLoop(const double zoom); void MainLoop(const double zoom);
void Draw(const double zoom); void Draw(const double zoom);
void ConstrainedColor(bool constrain);
float rotate_y = 0, float rotate_y = 0,
rotate_z = 0; rotate_z = 0;
const float rotations_per_tick = .2; const float rotations_per_tick = .2;
list<Triangle*> triangles; vector<Triangle*> triangles;
double StringToDouble(const std::string& s) { double StringToDouble(const std::string& s) {
std::istringstream i(s); std::istringstream i(s);
@ -81,20 +82,11 @@ int main(int argc, char* argv[]) {
polyline[i] = Point(x, y); polyline[i] = Point(x, y);
} }
polyline[0] = Point(5, 5);
polyline[1] = Point(-5, 5);
polyline[2] = Point(-5, -5);
polyline[3] = Point(5, -5);
//Point foo[] = {Point(5, 5.1), Point(-5, 5.2), Point(-5, -5.3), Point(5, -5.4), Point(5.1, 5.5), Point(5, 5.5),
// Point(-5, 5.6), Point(-5, -5.7), Point(5, -5.8), Point(5, 5.9), Point(-5, 5.1),
// Point(-5, -5.11), Point(5, -5.12), Point(5, 5.13), Point(-5, 5.14), Point(-5, -5.15), Point(5, -5.16)};
*/ */
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);
@ -107,7 +99,7 @@ int main(int argc, char* argv[]) {
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]);
points.push_back(Point(x, y)); points.push_back(new Point(x, y));
} }
myfile.close(); myfile.close();
} else { } else {
@ -119,7 +111,7 @@ int main(int argc, char* argv[]) {
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();
@ -229,22 +221,41 @@ void Draw(const double zoom)
ResetZoom(zoom, center.x, center.y, 800, 600); ResetZoom(zoom, center.x, center.y, 800, 600);
list<Triangle*>::iterator it; for (int i = 0; i < triangles.size(); i++) {
for (it = triangles.begin(); it != triangles.end(); it++) {
Triangle* t = *it;
Point* a = t->GetPoint(0);
Point* b = t->GetPoint(1);
Point* c = t->GetPoint(2);
// Red Triangle& t = *triangles[i];
glColor3f(1, 0, 0); Point& a = *t.GetPoint(0);
Point& b = *t.GetPoint(1);
Point& c = *t.GetPoint(2);
glBegin(GL_LINE_LOOP); // Drawing Using Triangles ConstrainedColor(t.constrained_edge[2]);
glVertex2f(a->x, a->y); // Top glBegin(GL_LINES);
glVertex2f(b->x, b->y); // Bottom Left glVertex2f(a.x, a.y);
glVertex2f(c->x, c->y); // Bottom Right glVertex2f(b.x, b.y);
glEnd(); glEnd( );
ConstrainedColor(t.constrained_edge[0]);
glBegin(GL_LINES);
glVertex2f(b.x, b.y);
glVertex2f(c.x, c.y);
glEnd( );
ConstrainedColor(t.constrained_edge[1]);
glBegin(GL_LINES);
glVertex2f(c.x, c.y);
glVertex2f(a.x, a.y);
glEnd( );
} }
} }
void ConstrainedColor(bool constrain) {
if(constrain) {
// Green
glColor3f(0, 1, 0);
} else {
// Red
glColor3f(1, 0, 0);
}
}