New Polygon::triangulate_convex() method

This commit is contained in:
Alessandro Ranellucci 2014-04-24 16:59:36 +02:00
parent ca4d4211c9
commit bb0ce3cccd
4 changed files with 34 additions and 1 deletions

View File

@ -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() {

View File

@ -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);

View File

@ -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) {

View File

@ -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*