Просмотр исходного кода

New Polygon::triangulate_convex() method

Alessandro Ranellucci 11 лет назад
Родитель
Сommit
bb0ce3cccd
4 измененных файлов с 34 добавлено и 1 удалено
  1. 14 0
      xs/src/Polygon.cpp
  2. 1 0
      xs/src/Polygon.hpp
  3. 17 1
      xs/t/06_polygon.t
  4. 2 0
      xs/xsp/Polygon.xsp

+ 14 - 0
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() {

+ 1 - 0
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);

+ 17 - 1
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) {

+ 2 - 0
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*