Ported Polygon->is_valid() and ExPolygon->is_valid()

This commit is contained in:
Alessandro Ranellucci 2013-08-26 23:27:51 +02:00
parent da0b85c0d9
commit fe42427a54
13 changed files with 37 additions and 54 deletions

View file

@ -40,6 +40,16 @@ ExPolygon::area() const
return a;
}
bool
ExPolygon::is_valid() const
{
if (!this->contour.is_valid() || !this->contour.is_counter_clockwise()) return false;
for (Polygons::const_iterator it = this->holes.begin(); it != this->holes.end(); ++it) {
if (!(*it).is_valid() || (*it).is_counter_clockwise()) return false;
}
return true;
}
SV*
ExPolygon::to_SV() {
const unsigned int num_holes = this->holes.size();

View file

@ -20,6 +20,7 @@ class ExPolygon
void translate(double x, double y);
void rotate(double angle, Point* center);
double area() const;
bool is_valid() const;
};
typedef std::vector<ExPolygon> ExPolygons;

View file

@ -92,4 +92,10 @@ Polygon::make_clockwise()
return false;
}
bool
Polygon::is_valid() const
{
return this->points.size() >= 3;
}
}

View file

@ -20,6 +20,7 @@ class Polygon : public MultiPoint {
bool is_counter_clockwise() const;
bool make_counter_clockwise();
bool make_clockwise();
bool is_valid() const;
};
typedef std::vector<Polygon> Polygons;

View file

@ -4,7 +4,7 @@ use strict;
use warnings;
use Slic3r::XS;
use Test::More tests => 20;
use Test::More tests => 21;
use constant PI => 4 * atan2(1, 1);
@ -23,6 +23,7 @@ my $hole_in_square = [ # cw
my $expolygon = Slic3r::ExPolygon->new($square, $hole_in_square);
ok $expolygon->is_valid, 'is_valid';
is ref($expolygon->pp), 'ARRAY', 'expolygon pp is unblessed';
is_deeply $expolygon->pp, [$square, $hole_in_square], 'expolygon roundtrip';

View file

@ -4,7 +4,7 @@ use strict;
use warnings;
use Slic3r::XS;
use Test::More tests => 12;
use Test::More tests => 13;
my $square = [ # ccw
[100, 100],
@ -14,6 +14,7 @@ my $square = [ # ccw
];
my $polygon = Slic3r::Polygon->new(@$square);
ok $polygon->is_valid, 'is_valid';
is_deeply $polygon->pp, $square, 'polygon roundtrip';
is ref($polygon->arrayref), 'ARRAY', 'polygon arrayref is unblessed';

View file

@ -20,6 +20,7 @@
void scale(double factor);
void translate(double x, double y);
double area();
bool is_valid();
%{
ExPolygon*

View file

@ -27,6 +27,7 @@
bool is_counter_clockwise();
bool make_counter_clockwise();
bool make_clockwise();
bool is_valid();
%{
Polygon*