New ExPolygon::triangulate() method

This commit is contained in:
Alessandro Ranellucci 2014-04-24 17:06:16 +02:00
parent bb0ce3cccd
commit 37c7b958d4
5 changed files with 26 additions and 3 deletions

View File

@ -176,6 +176,18 @@ ExPolygon::get_trapezoids(Polygons* polygons, double angle) const
polygon->rotate(-(PI/2 - angle), Point(0,0)); polygon->rotate(-(PI/2 - angle), Point(0,0));
} }
void
ExPolygon::triangulate(Polygons* polygons) const
{
// first make trapezoids
Polygons trapezoids;
this->get_trapezoids(&trapezoids);
// then triangulate each trapezoid
for (Polygons::iterator polygon = trapezoids.begin(); polygon != trapezoids.end(); ++polygon)
polygon->triangulate_convex(polygons);
}
#ifdef SLIC3RXS #ifdef SLIC3RXS
SV* SV*
ExPolygon::to_AV() { ExPolygon::to_AV() {

View File

@ -29,6 +29,7 @@ class ExPolygon
void medial_axis(double max_width, double min_width, Polylines* polylines) const; void medial_axis(double max_width, double min_width, Polylines* polylines) const;
void get_trapezoids(Polygons* polygons) const; void get_trapezoids(Polygons* polygons) const;
void get_trapezoids(Polygons* polygons, double angle) const; void get_trapezoids(Polygons* polygons, double angle) const;
void triangulate(Polygons* polygons) const;
#ifdef SLIC3RXS #ifdef SLIC3RXS
void from_SV(SV* poly_sv); void from_SV(SV* poly_sv);

View File

@ -168,7 +168,9 @@ Polygon::triangulate_convex(Polygons* polygons) const
p.points.push_back(this->points.front()); p.points.push_back(this->points.front());
p.points.push_back(*(it-1)); p.points.push_back(*(it-1));
p.points.push_back(*it); p.points.push_back(*it);
polygons->push_back(p);
// this should be replaced with a more efficient call to a merge_collinear_segments() method
if (p.area() > 0) polygons->push_back(p);
} }
} }

View File

@ -3,9 +3,9 @@
use strict; use strict;
use warnings; use warnings;
use List::Util qw(first); use List::Util qw(first sum);
use Slic3r::XS; use Slic3r::XS;
use Test::More tests => 31; use Test::More tests => 33;
use constant PI => 4 * atan2(1, 1); use constant PI => 4 * atan2(1, 1);
@ -133,4 +133,10 @@ is $expolygon->area, 100*100-20*20, 'area';
is scalar(grep { $_->area == 100*200 } @$polygons), 1, 'trapezoids have expected area'; is scalar(grep { $_->area == 100*200 } @$polygons), 1, 'trapezoids have expected area';
} }
{
my $triangles = $expolygon->triangulate;
is scalar(@$triangles), 8, 'expected number of triangles';
is sum(map $_->area, @$triangles), $expolygon->area, 'sum of triangles area equals original expolygon area';
}
__END__ __END__

View File

@ -31,6 +31,8 @@
%code{% THIS->medial_axis(max_width, min_width, &RETVAL); %}; %code{% THIS->medial_axis(max_width, min_width, &RETVAL); %};
Polygons get_trapezoids(double angle) Polygons get_trapezoids(double angle)
%code{% THIS->get_trapezoids(&RETVAL, angle); %}; %code{% THIS->get_trapezoids(&RETVAL, angle); %};
Polygons triangulate()
%code{% THIS->triangulate(&RETVAL); %};
%{ %{
ExPolygon* ExPolygon*