From bb0ce3cccd932a80c9ad33c7002b80d613ec6c5e Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Thu, 24 Apr 2014 16:59:36 +0200 Subject: [PATCH] New Polygon::triangulate_convex() method --- xs/src/Polygon.cpp | 14 ++++++++++++++ xs/src/Polygon.hpp | 1 + xs/t/06_polygon.t | 18 +++++++++++++++++- xs/xsp/Polygon.xsp | 2 ++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/xs/src/Polygon.cpp b/xs/src/Polygon.cpp index 5efbd56ec..eff8a9b96 100644 --- a/xs/src/Polygon.cpp +++ b/xs/src/Polygon.cpp @@ -158,6 +158,20 @@ Polygon::simplify(double tolerance, Polygons &polygons) const polygons.insert(polygons.end(), pp.begin(), pp.end()); } +// Only call this on convex polygons or it will return invalid results +void +Polygon::triangulate_convex(Polygons* polygons) const +{ + for (Points::const_iterator it = this->points.begin() + 2; it != this->points.end(); ++it) { + Polygon p; + p.points.reserve(3); + p.points.push_back(this->points.front()); + p.points.push_back(*(it-1)); + p.points.push_back(*it); + polygons->push_back(p); + } +} + #ifdef SLIC3RXS SV* Polygon::to_SV_ref() { diff --git a/xs/src/Polygon.hpp b/xs/src/Polygon.hpp index 4a3f9242e..e96fb9736 100644 --- a/xs/src/Polygon.hpp +++ b/xs/src/Polygon.hpp @@ -31,6 +31,7 @@ class Polygon : public MultiPoint { bool contains_point(const Point &point) const; Polygons simplify(double tolerance) const; void simplify(double tolerance, Polygons &polygons) const; + void triangulate_convex(Polygons* polygons) const; #ifdef SLIC3RXS void from_SV_check(SV* poly_sv); diff --git a/xs/t/06_polygon.t b/xs/t/06_polygon.t index a9d9321cc..f58aa539d 100644 --- a/xs/t/06_polygon.t +++ b/xs/t/06_polygon.t @@ -3,8 +3,11 @@ use strict; use warnings; +use List::Util qw(first); use Slic3r::XS; -use Test::More tests => 17; +use Test::More tests => 19; + +use constant PI => 4 * atan2(1, 1); my $square = [ # ccw [100, 100], @@ -53,6 +56,19 @@ ok ref($polygon->first_point) eq 'Slic3r::Point', 'first_point'; ok $polygon->contains_point(Slic3r::Point->new(150,150)), 'ccw contains_point'; ok $cw_polygon->contains_point(Slic3r::Point->new(150,150)), 'cw contains_point'; +{ + my @points = (Slic3r::Point->new(100,0)); + foreach my $i (1..5) { + my $point = $points[0]->clone; + $point->rotate(PI/3*$i, [0,0]); + push @points, $point; + } + my $hexagon = Slic3r::Polygon->new(@points); + my $triangles = $hexagon->triangulate_convex; + is scalar(@$triangles), 4, 'right number of triangles'; + ok !(defined first { $_->is_clockwise } @$triangles), 'all triangles are ccw'; +} + # this is not a test: this just demonstrates bad usage, where $polygon->clone gets # DESTROY'ed before the derived object ($point), causing bad memory access if (0) { diff --git a/xs/xsp/Polygon.xsp b/xs/xsp/Polygon.xsp index 8a32f29e4..65c826f3a 100644 --- a/xs/xsp/Polygon.xsp +++ b/xs/xsp/Polygon.xsp @@ -36,6 +36,8 @@ bool contains_point(Point* point) %code{% RETVAL = THIS->contains_point(*point); %}; Polygons simplify(double tolerance); + Polygons triangulate_convex() + %code{% THIS->triangulate_convex(&RETVAL); %}; %{ Polygon*