From 660ec61ac0874129fced66dac05d1d8c6d059516 Mon Sep 17 00:00:00 2001 From: zzzzrrr Date: Wed, 10 Feb 2010 07:49:59 -0500 Subject: [PATCH] fixed include; updated API; updated comments --- poly2tri/common/utils.h | 5 +++++ poly2tri/poly2tri.h | 6 ++++++ poly2tri/sweep/advancing_front.h | 5 +++++ poly2tri/sweep/cdt.cc | 4 ++++ poly2tri/sweep/cdt.h | 9 ++++++++- poly2tri/sweep/sweep.h | 5 +++++ poly2tri/sweep/sweep_context.cc | 11 ++++++++--- poly2tri/sweep/sweep_context.h | 19 +++++++++++++++---- src/cdt.pxd | 1 + src/cdt.pxi | 4 ++++ test.py | 14 ++++++++++++-- 11 files changed, 73 insertions(+), 10 deletions(-) diff --git a/poly2tri/common/utils.h b/poly2tri/common/utils.h index 2fc3b5b..23c435d 100644 --- a/poly2tri/common/utils.h +++ b/poly2tri/common/utils.h @@ -28,6 +28,9 @@ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#ifndef UTILS_H +#define UTILS_H #include #include @@ -95,3 +98,5 @@ bool InScanArea(Point& pa, Point& pb, Point& pc, Point& pd) } +#endif + diff --git a/poly2tri/poly2tri.h b/poly2tri/poly2tri.h index eab8910..487755e 100644 --- a/poly2tri/poly2tri.h +++ b/poly2tri/poly2tri.h @@ -28,6 +28,12 @@ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#ifndef POLY2TRI_H +#define POLY2TRI_H + #include "common/shapes.h" #include "sweep/cdt.h" +#endif + diff --git a/poly2tri/sweep/advancing_front.h b/poly2tri/sweep/advancing_front.h index 5b9b299..4354905 100644 --- a/poly2tri/sweep/advancing_front.h +++ b/poly2tri/sweep/advancing_front.h @@ -28,6 +28,10 @@ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#ifndef ADVANCED_FRONT_H +#define ADVANCED_FRONT_H + #include "../common/shapes.h" namespace p2t { @@ -112,3 +116,4 @@ inline void AdvancingFront::set_search(Node* node) } +#endif diff --git a/poly2tri/sweep/cdt.cc b/poly2tri/sweep/cdt.cc index dcca815..f013e47 100644 --- a/poly2tri/sweep/cdt.cc +++ b/poly2tri/sweep/cdt.cc @@ -43,6 +43,10 @@ void CDT::AddHole(std::vector polyline) sweep_context_->AddHole(polyline); } +void CDT::AddPoint(Point* point) { + sweep_context_->AddPoint(point); +} + void CDT::Triangulate() { sweep_->Triangulate(*sweep_context_); diff --git a/poly2tri/sweep/cdt.h b/poly2tri/sweep/cdt.h index c0531ce..c428430 100644 --- a/poly2tri/sweep/cdt.h +++ b/poly2tri/sweep/cdt.h @@ -28,6 +28,10 @@ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#ifndef CDT_H +#define CDT_H + #include "advancing_front.h" #include "sweep_context.h" #include "sweep.h" @@ -42,9 +46,10 @@ class CDT CDT(std::vector polyline); /// Destructor ~CDT(); - /// Add a hole void AddHole(std::vector polyline); +/// Add a single point +void AddPoint(Point* point); /// Triangulate points void Triangulate(); /// Get Delaunay triangles @@ -60,3 +65,5 @@ Sweep* sweep_; }; } + +#endif diff --git a/poly2tri/sweep/sweep.h b/poly2tri/sweep/sweep.h index bf94d61..02bf9ab 100644 --- a/poly2tri/sweep/sweep.h +++ b/poly2tri/sweep/sweep.h @@ -35,6 +35,9 @@ * * "FlipScan" Constrained Edge Algorithm invented by Thomas Åhlén, thahlen@gmail.com */ + +#ifndef SWEEP_H +#define SWEEP_H namespace p2t { @@ -113,3 +116,5 @@ void FinalizationPolygon(SweepContext& tcx); }; } + +#endif diff --git a/poly2tri/sweep/sweep_context.cc b/poly2tri/sweep/sweep_context.cc index 0e6e1b8..f139244 100644 --- a/poly2tri/sweep/sweep_context.cc +++ b/poly2tri/sweep/sweep_context.cc @@ -28,11 +28,10 @@ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "sweep_context.h" #include -#include + +#include "sweep_context.h" #include "advancing_front.h" -#include namespace p2t { @@ -40,7 +39,9 @@ SweepContext::SweepContext(std::vector polyline) { basin = Basin(); edge_event = EdgeEvent(); + points_ = polyline; + InitEdges(points_); } @@ -52,6 +53,10 @@ void SweepContext::AddHole(std::vector polyline) } } +void SweepContext::AddPoint(Point* point) { + points_.push_back(point); +} + std::vector SweepContext::GetTriangles() { return triangles_; diff --git a/poly2tri/sweep/sweep_context.h b/poly2tri/sweep/sweep_context.h index 80cb3ea..b70e853 100644 --- a/poly2tri/sweep/sweep_context.h +++ b/poly2tri/sweep/sweep_context.h @@ -28,6 +28,10 @@ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#ifndef SWEEP_CONTEXT_H +#define SWEEP_CONTEXT_H + #include #include @@ -46,43 +50,48 @@ class AdvancingFront; class SweepContext { public: -// Constructor +/// Constructor SweepContext(std::vector polyline); -// Destructor +/// Destructor ~SweepContext(); void set_head(Point* p1); + Point* head(); void set_tail(Point* p1); + Point* tail(); 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 +/// 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); void AddHole(std::vector polyline); +void AddPoint(Point* point); + AdvancingFront* front(); void MeshClean(Triangle& triangle); std::vector GetTriangles(); std::list GetMap(); - std::vector edge_list; struct Basin { @@ -172,3 +181,5 @@ inline Point* SweepContext::tail() } } + +#endif diff --git a/src/cdt.pxd b/src/cdt.pxd index 96e3fe1..454823d 100644 --- a/src/cdt.pxd +++ b/src/cdt.pxd @@ -1,6 +1,7 @@ cdef extern from "../poly2tri/sweep/cdt.h": ctypedef struct c_CDT "p2t::CDT": void AddHole(point_vec polyline) + void AddPoint(c_Point* point) void Triangulate() triangle_vec GetTriangles() c_CDT *new_CDT "new p2t::CDT" (point_vec polyline) diff --git a/src/cdt.pxi b/src/cdt.pxi index 7dafa65..56cae43 100644 --- a/src/cdt.pxi +++ b/src/cdt.pxi @@ -26,6 +26,10 @@ cdef class CDT: for point in polyline: hole.push_back(new_Point(point.x, point.y)) self.me.AddHole(hole) + + def add_point(self, point): + cdef c_Point* p = new_Point(point.x, point.y) + self.me.AddPoint(p) def __dealloc__(self): del_CDT(self.me) \ No newline at end of file diff --git a/test.py b/test.py index 0b6ffc9..603b445 100644 --- a/test.py +++ b/test.py @@ -33,6 +33,8 @@ def main(file_name, translate, zoom): screen = pygame.display.set_mode(SCREEN_SIZE,0,8) pygame.display.set_caption('poly2tri demo') + pygame.mouse.set_visible(True) + black = Color(0,0,0) red = Color(255, 0, 0) green = Color(0, 255, 0) @@ -52,11 +54,13 @@ def main(file_name, translate, zoom): ## ## Step 1: Initialize + ## NOTE: polyline must be a simple polygon. The polyline's points + ## constitute constrained edges. No repeat points!!! ## cdt = CDT(polyline) ## - ## Step 2: Add holes if necessary + ## Step 2: Add holes and interior Steiner points if necessary ## if file_name == "data/dude.dat": hole = [] @@ -64,13 +68,19 @@ def main(file_name, translate, zoom): p[0] = p[0]*zoom + translate[0] p[1] = p[1]*zoom + translate[1] hole.append(Point(p[0],p[1])) + # Add a hole cdt.add_hole(hole) hole = [] for p in chest_hole: p[0] = p[0]*zoom + translate[0] p[1] = p[1]*zoom + translate[1] hole.append(Point(p[0],p[1])) + # Add a hole cdt.add_hole(hole) + # Add an interior Steiner point + x = 361*zoom + translate[0] + y = 381*zoom + translate[1] + cdt.add_point(Point(x, y)) ## ## Step 3: Triangulate @@ -134,7 +144,7 @@ def main(file_name, translate, zoom): break if( e.key == K_f ): pygame.display.toggle_fullscreen() - + return if __name__=="__main__":