mirror of
https://github.com/SoftFever/OrcaSlicer.git
synced 2025-07-17 03:37:54 -06:00
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);
|
EXTR_ROLE_INTERNALBRIDGE EXTR_ROLE_SKIRT EXTR_ROLE_SUPPORTMATERIAL EXTR_ROLE_GAPFILL);
|
||||||
our %EXPORT_TAGS = (roles => \@EXPORT_OK);
|
our %EXPORT_TAGS = (roles => \@EXPORT_OK);
|
||||||
|
|
||||||
sub simplify {
|
|
||||||
my $self = shift;
|
|
||||||
$self->polyline($self->polyline->simplify(@_));
|
|
||||||
}
|
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
|
|
@ -38,7 +38,7 @@ sub BUILD {
|
||||||
my $crossing_edges = $self->_crossing_edges;
|
my $crossing_edges = $self->_crossing_edges;
|
||||||
|
|
||||||
# simplify islands
|
# simplify islands
|
||||||
@{$self->islands} = map $_->simplify($self->_inner_margin), @{$self->islands};
|
$_->simplify($self->_inner_margin) for @{$self->islands};
|
||||||
|
|
||||||
# process individual islands
|
# process individual islands
|
||||||
for my $i (0 .. $#{$self->islands}) {
|
for my $i (0 .. $#{$self->islands}) {
|
||||||
|
|
|
@ -1360,10 +1360,10 @@ sub make_thumbnail {
|
||||||
# remove polygons with area <= 1mm
|
# remove polygons with area <= 1mm
|
||||||
my $area_threshold = Slic3r::Geometry::scale 1;
|
my $area_threshold = Slic3r::Geometry::scale 1;
|
||||||
$self->thumbnail->append(
|
$self->thumbnail->append(
|
||||||
map $_->simplify(0.5),
|
|
||||||
grep $_->area >= $area_threshold,
|
grep $_->area >= $area_threshold,
|
||||||
@{ $mesh->horizontal_projection },
|
@{ $mesh->horizontal_projection },
|
||||||
);
|
);
|
||||||
|
$self->thumbnail->simplify(0.5);
|
||||||
} else {
|
} else {
|
||||||
my $convex_hull = Slic3r::ExPolygon->new($self->convex_hull)->clone;
|
my $convex_hull = Slic3r::ExPolygon->new($self->convex_hull)->clone;
|
||||||
$convex_hull->scale(1/&Slic3r::SCALING_FACTOR);
|
$convex_hull->scale(1/&Slic3r::SCALING_FACTOR);
|
||||||
|
|
|
@ -32,13 +32,6 @@ sub grow {
|
||||||
return $self->split_at_first_point->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
|
# this method subdivides the polygon segments to that no one of them
|
||||||
# is longer than the length provided
|
# is longer than the length provided
|
||||||
sub subdivide {
|
sub subdivide {
|
||||||
|
|
|
@ -27,14 +27,6 @@ sub remove_acute_vertices {
|
||||||
polyline_remove_acute_vertices($self);
|
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 {
|
sub bounding_box {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
return Slic3r::Geometry::BoundingBox->new_from_points($self);
|
return Slic3r::Geometry::BoundingBox->new_from_points($self);
|
||||||
|
|
|
@ -317,14 +317,8 @@ sub _simplify_slices {
|
||||||
my ($distance) = @_;
|
my ($distance) = @_;
|
||||||
|
|
||||||
foreach my $layer (map @{$_->layers}, @{$self->objects}) {
|
foreach my $layer (map @{$_->layers}, @{$self->objects}) {
|
||||||
my @new = map $_->simplify($distance), map $_->clone, @{$layer->slices};
|
$layer->slices->simplify($distance);
|
||||||
$layer->slices->clear;
|
$_->slices->simplify($distance) for @{$layer->regions};
|
||||||
$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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -38,11 +38,6 @@ sub offset {
|
||||||
return [ map $self->clone(expolygon => $_), @{$self->expolygon->offset_ex(@_)} ];
|
return [ map $self->clone(expolygon => $_), @{$self->expolygon->offset_ex(@_)} ];
|
||||||
}
|
}
|
||||||
|
|
||||||
sub simplify {
|
|
||||||
my $self = shift;
|
|
||||||
return map $self->clone(expolygon => $_), $self->expolygon->simplify(@_);
|
|
||||||
}
|
|
||||||
|
|
||||||
sub p {
|
sub p {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
return @{$self->polygons};
|
return @{$self->polygons};
|
||||||
|
|
|
@ -24,7 +24,7 @@ use Slic3r;
|
||||||
my $polyline = Slic3r::Polyline->new(
|
my $polyline = Slic3r::Polyline->new(
|
||||||
[0,0],[1,0],[2,0],[2,1],[2,2],[1,2],[0,2],[0,1],[0,0],
|
[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';
|
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(
|
my $polyline = Slic3r::Polyline->new(
|
||||||
[0,0], [50,50], [100,0], [125,-25], [150,50],
|
[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';
|
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;
|
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
|
#ifdef SLIC3RXS
|
||||||
SV*
|
SV*
|
||||||
ExPolygon::to_AV() {
|
ExPolygon::to_AV() {
|
||||||
|
|
|
@ -19,6 +19,9 @@ class ExPolygon
|
||||||
bool is_valid() const;
|
bool is_valid() const;
|
||||||
bool contains_line(const Line* line) const;
|
bool contains_line(const Line* line) const;
|
||||||
bool contains_point(const Point* point) 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
|
#ifdef SLIC3RXS
|
||||||
void from_SV(SV* poly_sv);
|
void from_SV(SV* poly_sv);
|
||||||
|
|
|
@ -47,4 +47,14 @@ ExPolygonCollection::contains_point(const Point* point) const
|
||||||
return false;
|
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 translate(double x, double y);
|
||||||
void rotate(double angle, Point* center);
|
void rotate(double angle, Point* center);
|
||||||
bool contains_point(const Point* point) const;
|
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);
|
this->polyline.clip_end(distance);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
ExtrusionPath::simplify(double tolerance)
|
||||||
|
{
|
||||||
|
this->polyline.simplify(tolerance);
|
||||||
|
}
|
||||||
|
|
||||||
double
|
double
|
||||||
ExtrusionPath::length() const
|
ExtrusionPath::length() const
|
||||||
{
|
{
|
||||||
|
|
|
@ -54,6 +54,7 @@ class ExtrusionPath : public ExtrusionEntity
|
||||||
ExtrusionEntityCollection* intersect_expolygons(ExPolygonCollection* collection) const;
|
ExtrusionEntityCollection* intersect_expolygons(ExPolygonCollection* collection) const;
|
||||||
ExtrusionEntityCollection* subtract_expolygons(ExPolygonCollection* collection) const;
|
ExtrusionEntityCollection* subtract_expolygons(ExPolygonCollection* collection) const;
|
||||||
void clip_end(double distance);
|
void clip_end(double distance);
|
||||||
|
void simplify(double tolerance);
|
||||||
double length() const;
|
double length() const;
|
||||||
private:
|
private:
|
||||||
ExtrusionEntityCollection* _inflate_collection(const Polylines &polylines) const;
|
ExtrusionEntityCollection* _inflate_collection(const Polylines &polylines) const;
|
||||||
|
|
|
@ -55,6 +55,47 @@ MultiPoint::is_valid() const
|
||||||
return this->points.size() >= 2;
|
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
|
#ifdef SLIC3RXS
|
||||||
void
|
void
|
||||||
MultiPoint::from_SV(SV* poly_sv)
|
MultiPoint::from_SV(SV* poly_sv)
|
||||||
|
|
|
@ -21,6 +21,7 @@ class MultiPoint
|
||||||
virtual Lines lines() const = 0;
|
virtual Lines lines() const = 0;
|
||||||
double length() const;
|
double length() const;
|
||||||
bool is_valid() const;
|
bool is_valid() const;
|
||||||
|
void simplify(double tolerance);
|
||||||
|
|
||||||
#ifdef SLIC3RXS
|
#ifdef SLIC3RXS
|
||||||
void from_SV(SV* poly_sv);
|
void from_SV(SV* poly_sv);
|
||||||
|
@ -28,6 +29,9 @@ class MultiPoint
|
||||||
SV* to_AV();
|
SV* to_AV();
|
||||||
SV* to_SV_pureperl() const;
|
SV* to_SV_pureperl() const;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
private:
|
||||||
|
static Points _douglas_peucker(Points &points, double tolerance);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,12 +76,18 @@ Point::distance_to(const Point* point) const
|
||||||
double
|
double
|
||||||
Point::distance_to(const Line* line) const
|
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 n = (line->b.x - line->a.x) * (line->a.y - this->y)
|
double
|
||||||
- (line->a.x - this->x) * (line->b.y - line->a.y);
|
Point::distance_to(const Line &line) const
|
||||||
|
{
|
||||||
|
if (line.a.coincides_with(&line.b)) return this->distance_to(&line.a);
|
||||||
|
|
||||||
return abs(n) / line->length();
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef SLIC3RXS
|
#ifdef SLIC3RXS
|
||||||
|
|
|
@ -25,6 +25,7 @@ class Point
|
||||||
Point* nearest_point(Points points) const;
|
Point* nearest_point(Points points) const;
|
||||||
double distance_to(const Point* point) const;
|
double distance_to(const Point* point) const;
|
||||||
double distance_to(const Line* line) const;
|
double distance_to(const Line* line) const;
|
||||||
|
double distance_to(const Line &line) const;
|
||||||
|
|
||||||
#ifdef SLIC3RXS
|
#ifdef SLIC3RXS
|
||||||
void from_SV(SV* point_sv);
|
void from_SV(SV* point_sv);
|
||||||
|
|
|
@ -125,6 +125,12 @@ Polygon::contains_point(const Point* point) const
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Polygons
|
||||||
|
Polygon::simplify(double tolerance) const
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef SLIC3RXS
|
#ifdef SLIC3RXS
|
||||||
SV*
|
SV*
|
||||||
Polygon::to_SV_ref() {
|
Polygon::to_SV_ref() {
|
||||||
|
|
|
@ -24,6 +24,8 @@ class Polygon : public MultiPoint {
|
||||||
bool make_clockwise();
|
bool make_clockwise();
|
||||||
bool is_valid() const;
|
bool is_valid() const;
|
||||||
bool contains_point(const Point* point) const;
|
bool contains_point(const Point* point) const;
|
||||||
|
Polygons simplify(double tolerance) const;
|
||||||
|
void simplify(double tolerance, Polygons &polygons) const;
|
||||||
|
|
||||||
#ifdef SLIC3RXS
|
#ifdef SLIC3RXS
|
||||||
SV* to_SV_ref();
|
SV* to_SV_ref();
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
int count()
|
int count()
|
||||||
%code{% RETVAL = THIS->expolygons.size(); %};
|
%code{% RETVAL = THIS->expolygons.size(); %};
|
||||||
bool contains_point(Point* point);
|
bool contains_point(Point* point);
|
||||||
|
void simplify(double tolerance);
|
||||||
%{
|
%{
|
||||||
|
|
||||||
ExPolygonCollection*
|
ExPolygonCollection*
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
ExtrusionEntityCollection* subtract_expolygons(ExPolygonCollection* collection)
|
ExtrusionEntityCollection* subtract_expolygons(ExPolygonCollection* collection)
|
||||||
%code{% const char* CLASS = "Slic3r::ExtrusionPath::Collection"; RETVAL = THIS->subtract_expolygons(collection); %};
|
%code{% const char* CLASS = "Slic3r::ExtrusionPath::Collection"; RETVAL = THIS->subtract_expolygons(collection); %};
|
||||||
void clip_end(double distance);
|
void clip_end(double distance);
|
||||||
|
void simplify(double tolerance);
|
||||||
double length();
|
double length();
|
||||||
bool is_perimeter();
|
bool is_perimeter();
|
||||||
bool is_fill();
|
bool is_fill();
|
||||||
|
|
|
@ -34,6 +34,7 @@
|
||||||
Point* first_point()
|
Point* first_point()
|
||||||
%code{% const char* CLASS = "Slic3r::Point"; RETVAL = THIS->first_point(); %};
|
%code{% const char* CLASS = "Slic3r::Point"; RETVAL = THIS->first_point(); %};
|
||||||
bool contains_point(Point* point);
|
bool contains_point(Point* point);
|
||||||
|
void simplify(double tolerance);
|
||||||
%{
|
%{
|
||||||
|
|
||||||
Polygon*
|
Polygon*
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
bool is_valid();
|
bool is_valid();
|
||||||
void clip_end(double distance);
|
void clip_end(double distance);
|
||||||
void clip_start(double distance);
|
void clip_start(double distance);
|
||||||
|
void simplify(double tolerance);
|
||||||
%{
|
%{
|
||||||
|
|
||||||
Polyline*
|
Polyline*
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue