Incomplete work for porting simplify() to XS
This commit is contained in:
parent
7475762a27
commit
518798beb3
24 changed files with 121 additions and 41 deletions
|
@ -10,9 +10,4 @@ our @EXPORT_OK = qw(EXTR_ROLE_PERIMETER EXTR_ROLE_EXTERNAL_PERIMETER
|
|||
EXTR_ROLE_INTERNALBRIDGE EXTR_ROLE_SKIRT EXTR_ROLE_SUPPORTMATERIAL EXTR_ROLE_GAPFILL);
|
||||
our %EXPORT_TAGS = (roles => \@EXPORT_OK);
|
||||
|
||||
sub simplify {
|
||||
my $self = shift;
|
||||
$self->polyline($self->polyline->simplify(@_));
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
|
@ -38,7 +38,7 @@ sub BUILD {
|
|||
my $crossing_edges = $self->_crossing_edges;
|
||||
|
||||
# simplify islands
|
||||
@{$self->islands} = map $_->simplify($self->_inner_margin), @{$self->islands};
|
||||
$_->simplify($self->_inner_margin) for @{$self->islands};
|
||||
|
||||
# process individual islands
|
||||
for my $i (0 .. $#{$self->islands}) {
|
||||
|
|
|
@ -1360,10 +1360,10 @@ sub make_thumbnail {
|
|||
# remove polygons with area <= 1mm
|
||||
my $area_threshold = Slic3r::Geometry::scale 1;
|
||||
$self->thumbnail->append(
|
||||
map $_->simplify(0.5),
|
||||
grep $_->area >= $area_threshold,
|
||||
@{ $mesh->horizontal_projection },
|
||||
);
|
||||
$self->thumbnail->simplify(0.5);
|
||||
} else {
|
||||
my $convex_hull = Slic3r::ExPolygon->new($self->convex_hull)->clone;
|
||||
$convex_hull->scale(1/&Slic3r::SCALING_FACTOR);
|
||||
|
|
|
@ -32,13 +32,6 @@ sub grow {
|
|||
return $self->split_at_first_point->grow(@_);
|
||||
}
|
||||
|
||||
# NOTE that this will turn the polygon to ccw regardless of its
|
||||
# original orientation
|
||||
sub simplify {
|
||||
my $self = shift;
|
||||
return @{Slic3r::Geometry::Clipper::simplify_polygons([ $self->SUPER::simplify(@_) ])};
|
||||
}
|
||||
|
||||
# this method subdivides the polygon segments to that no one of them
|
||||
# is longer than the length provided
|
||||
sub subdivide {
|
||||
|
|
|
@ -27,14 +27,6 @@ sub remove_acute_vertices {
|
|||
polyline_remove_acute_vertices($self);
|
||||
}
|
||||
|
||||
sub simplify {
|
||||
my $self = shift;
|
||||
my $tolerance = shift || 10;
|
||||
|
||||
my $simplified = Boost::Geometry::Utils::linestring_simplify($self->pp, $tolerance);
|
||||
return __PACKAGE__->new(@$simplified);
|
||||
}
|
||||
|
||||
sub bounding_box {
|
||||
my $self = shift;
|
||||
return Slic3r::Geometry::BoundingBox->new_from_points($self);
|
||||
|
|
|
@ -317,14 +317,8 @@ sub _simplify_slices {
|
|||
my ($distance) = @_;
|
||||
|
||||
foreach my $layer (map @{$_->layers}, @{$self->objects}) {
|
||||
my @new = map $_->simplify($distance), map $_->clone, @{$layer->slices};
|
||||
$layer->slices->clear;
|
||||
$layer->slices->append(@new);
|
||||
foreach my $layerm (@{$layer->regions}) {
|
||||
my @new = map $_->simplify($distance), map $_->clone, @{$layerm->slices};
|
||||
$layerm->slices->clear;
|
||||
$layerm->slices->append(@new);
|
||||
}
|
||||
$layer->slices->simplify($distance);
|
||||
$_->slices->simplify($distance) for @{$layer->regions};
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -38,11 +38,6 @@ sub offset {
|
|||
return [ map $self->clone(expolygon => $_), @{$self->expolygon->offset_ex(@_)} ];
|
||||
}
|
||||
|
||||
sub simplify {
|
||||
my $self = shift;
|
||||
return map $self->clone(expolygon => $_), $self->expolygon->simplify(@_);
|
||||
}
|
||||
|
||||
sub p {
|
||||
my $self = shift;
|
||||
return @{$self->polygons};
|
||||
|
|
|
@ -24,7 +24,7 @@ use Slic3r;
|
|||
my $polyline = Slic3r::Polyline->new(
|
||||
[0,0],[1,0],[2,0],[2,1],[2,2],[1,2],[0,2],[0,1],[0,0],
|
||||
);
|
||||
$polyline = $polyline->simplify(1);
|
||||
$polyline->simplify(1);
|
||||
is_deeply $polyline->pp, [ [0, 0], [2, 0], [2, 2], [0, 2], [0, 0] ], 'Douglas-Peucker';
|
||||
}
|
||||
|
||||
|
@ -32,7 +32,7 @@ use Slic3r;
|
|||
my $polyline = Slic3r::Polyline->new(
|
||||
[0,0], [50,50], [100,0], [125,-25], [150,50],
|
||||
);
|
||||
$polyline = $polyline->simplify(25);
|
||||
$polyline->simplify(25);
|
||||
is_deeply $polyline->pp, [ [0, 0], [50, 50], [125, -25], [150, 50] ], 'Douglas-Peucker';
|
||||
}
|
||||
|
||||
|
|
|
@ -82,6 +82,32 @@ ExPolygon::contains_point(const Point* point) const
|
|||
return true;
|
||||
}
|
||||
|
||||
Polygons
|
||||
ExPolygon::simplify(double tolerance) const
|
||||
{
|
||||
Polygons p;
|
||||
this->contour.simplify(tolerance, p);
|
||||
for (Polygons::const_iterator it = this->holes.begin(); it != this->holes.end(); ++it)
|
||||
it->simplify(tolerance, p);
|
||||
simplify_polygons(p, p);
|
||||
return p;
|
||||
}
|
||||
|
||||
ExPolygons
|
||||
ExPolygon::simplify(double tolerance) const
|
||||
{
|
||||
Polygons p = this->simplify(tolerance);
|
||||
return union_(p);
|
||||
}
|
||||
|
||||
void
|
||||
ExPolygon::simplify(double tolerance, ExPolygons &expolygons) const
|
||||
{
|
||||
ExPolygons ep = this->simplify(tolerance);
|
||||
expolygons.reserve(expolygons.size() + ep.size());
|
||||
expolygons.insert(expolygons.end(), ep.begin(), ep.end());
|
||||
}
|
||||
|
||||
#ifdef SLIC3RXS
|
||||
SV*
|
||||
ExPolygon::to_AV() {
|
||||
|
|
|
@ -19,6 +19,9 @@ class ExPolygon
|
|||
bool is_valid() const;
|
||||
bool contains_line(const Line* line) const;
|
||||
bool contains_point(const Point* point) const;
|
||||
Polygons simplify(double tolerance) const;
|
||||
ExPolygons simplify(double tolerance) const;
|
||||
void simplify(double tolerance, ExPolygons &expolygons) const;
|
||||
|
||||
#ifdef SLIC3RXS
|
||||
void from_SV(SV* poly_sv);
|
||||
|
|
|
@ -47,4 +47,14 @@ ExPolygonCollection::contains_point(const Point* point) const
|
|||
return false;
|
||||
}
|
||||
|
||||
void
|
||||
ExPolygonCollection::simplify(double tolerance)
|
||||
{
|
||||
ExPolygons t;
|
||||
for (ExPolygons::const_iterator it = this->expolygons.begin(); it != this->expolygons.end(); ++it) {
|
||||
it->simplify_and_append_to(tolerance, t);
|
||||
}
|
||||
this->expolygons = t;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ class ExPolygonCollection
|
|||
void translate(double x, double y);
|
||||
void rotate(double angle, Point* center);
|
||||
bool contains_point(const Point* point) const;
|
||||
void simplify(double tolerance);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -78,6 +78,12 @@ ExtrusionPath::clip_end(double distance)
|
|||
this->polyline.clip_end(distance);
|
||||
}
|
||||
|
||||
void
|
||||
ExtrusionPath::simplify(double tolerance)
|
||||
{
|
||||
this->polyline.simplify(tolerance);
|
||||
}
|
||||
|
||||
double
|
||||
ExtrusionPath::length() const
|
||||
{
|
||||
|
|
|
@ -54,6 +54,7 @@ class ExtrusionPath : public ExtrusionEntity
|
|||
ExtrusionEntityCollection* intersect_expolygons(ExPolygonCollection* collection) const;
|
||||
ExtrusionEntityCollection* subtract_expolygons(ExPolygonCollection* collection) const;
|
||||
void clip_end(double distance);
|
||||
void simplify(double tolerance);
|
||||
double length() const;
|
||||
private:
|
||||
ExtrusionEntityCollection* _inflate_collection(const Polylines &polylines) const;
|
||||
|
|
|
@ -55,6 +55,47 @@ MultiPoint::is_valid() const
|
|||
return this->points.size() >= 2;
|
||||
}
|
||||
|
||||
void
|
||||
MultiPoint::simplify(double tolerance)
|
||||
{
|
||||
this->points = MultiPoint::_douglas_peucker(this->points, tolerance);
|
||||
}
|
||||
|
||||
Points
|
||||
MultiPoint::_douglas_peucker(Points &points, double tolerance)
|
||||
{
|
||||
Points results;
|
||||
double dmax = 0;
|
||||
int index = 0;
|
||||
Line full(points.front(), points.back());
|
||||
for (Points::const_iterator it = points.begin() + 1; it != points.end(); ++it) {
|
||||
double d = it->distance_to(full);
|
||||
if (d > dmax) {
|
||||
index = it - points.begin();
|
||||
dmax = d;
|
||||
}
|
||||
}
|
||||
if (dmax >= tolerance) {
|
||||
Points dp0;
|
||||
dp0.reserve(index + 1);
|
||||
dp0.insert(dp0.end(), points.begin(), points.begin() + index + 1);
|
||||
Points dp1 = MultiPoint::_douglas_peucker(dp0, tolerance);
|
||||
results.reserve(results.size() + dp1.size() - 1);
|
||||
results.insert(results.end(), dp1.begin(), dp1.end() - 1);
|
||||
|
||||
dp0.clear();
|
||||
dp0.reserve(points.size() - index + 1);
|
||||
dp0.insert(dp0.end(), points.begin() + index, points.end());
|
||||
dp1 = MultiPoint::_douglas_peucker(dp0, tolerance);
|
||||
results.reserve(results.size() + dp1.size());
|
||||
results.insert(results.end(), dp1.begin(), dp1.end());
|
||||
} else {
|
||||
results.push_back(points.front());
|
||||
results.push_back(points.back());
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
#ifdef SLIC3RXS
|
||||
void
|
||||
MultiPoint::from_SV(SV* poly_sv)
|
||||
|
|
|
@ -21,6 +21,7 @@ class MultiPoint
|
|||
virtual Lines lines() const = 0;
|
||||
double length() const;
|
||||
bool is_valid() const;
|
||||
void simplify(double tolerance);
|
||||
|
||||
#ifdef SLIC3RXS
|
||||
void from_SV(SV* poly_sv);
|
||||
|
@ -28,6 +29,9 @@ class MultiPoint
|
|||
SV* to_AV();
|
||||
SV* to_SV_pureperl() const;
|
||||
#endif
|
||||
|
||||
private:
|
||||
static Points _douglas_peucker(Points &points, double tolerance);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -76,12 +76,18 @@ Point::distance_to(const Point* point) const
|
|||
double
|
||||
Point::distance_to(const Line* line) const
|
||||
{
|
||||
if (line->a.coincides_with(&line->b)) return this->distance_to(&line->a);
|
||||
return this->distance_to(*line);
|
||||
}
|
||||
|
||||
double
|
||||
Point::distance_to(const Line &line) const
|
||||
{
|
||||
if (line.a.coincides_with(&line.b)) return this->distance_to(&line.a);
|
||||
|
||||
double n = (line->b.x - line->a.x) * (line->a.y - this->y)
|
||||
- (line->a.x - this->x) * (line->b.y - line->a.y);
|
||||
double n = (line.b.x - line.a.x) * (line.a.y - this->y)
|
||||
- (line.a.x - this->x) * (line.b.y - line.a.y);
|
||||
|
||||
return abs(n) / line->length();
|
||||
return abs(n) / line.length();
|
||||
}
|
||||
|
||||
#ifdef SLIC3RXS
|
||||
|
|
|
@ -25,6 +25,7 @@ class Point
|
|||
Point* nearest_point(Points points) const;
|
||||
double distance_to(const Point* point) const;
|
||||
double distance_to(const Line* line) const;
|
||||
double distance_to(const Line &line) const;
|
||||
|
||||
#ifdef SLIC3RXS
|
||||
void from_SV(SV* point_sv);
|
||||
|
|
|
@ -125,6 +125,12 @@ Polygon::contains_point(const Point* point) const
|
|||
return result;
|
||||
}
|
||||
|
||||
Polygons
|
||||
Polygon::simplify(double tolerance) const
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#ifdef SLIC3RXS
|
||||
SV*
|
||||
Polygon::to_SV_ref() {
|
||||
|
|
|
@ -24,6 +24,8 @@ class Polygon : public MultiPoint {
|
|||
bool make_clockwise();
|
||||
bool is_valid() const;
|
||||
bool contains_point(const Point* point) const;
|
||||
Polygons simplify(double tolerance) const;
|
||||
void simplify(double tolerance, Polygons &polygons) const;
|
||||
|
||||
#ifdef SLIC3RXS
|
||||
SV* to_SV_ref();
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
int count()
|
||||
%code{% RETVAL = THIS->expolygons.size(); %};
|
||||
bool contains_point(Point* point);
|
||||
void simplify(double tolerance);
|
||||
%{
|
||||
|
||||
ExPolygonCollection*
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
ExtrusionEntityCollection* subtract_expolygons(ExPolygonCollection* collection)
|
||||
%code{% const char* CLASS = "Slic3r::ExtrusionPath::Collection"; RETVAL = THIS->subtract_expolygons(collection); %};
|
||||
void clip_end(double distance);
|
||||
void simplify(double tolerance);
|
||||
double length();
|
||||
bool is_perimeter();
|
||||
bool is_fill();
|
||||
|
|
|
@ -34,6 +34,7 @@
|
|||
Point* first_point()
|
||||
%code{% const char* CLASS = "Slic3r::Point"; RETVAL = THIS->first_point(); %};
|
||||
bool contains_point(Point* point);
|
||||
void simplify(double tolerance);
|
||||
%{
|
||||
|
||||
Polygon*
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
bool is_valid();
|
||||
void clip_end(double distance);
|
||||
void clip_start(double distance);
|
||||
void simplify(double tolerance);
|
||||
%{
|
||||
|
||||
Polyline*
|
||||
|
|
Loading…
Reference in a new issue