More unfinished work

This commit is contained in:
Alessandro Ranellucci 2013-11-22 02:16:10 +01:00
parent 518798beb3
commit df8d889481
16 changed files with 74 additions and 48 deletions

View File

@ -44,23 +44,6 @@ sub bounding_box {
return $self->contour->bounding_box; return $self->contour->bounding_box;
} }
sub simplify_as_polygons {
my $self = shift;
my ($tolerance) = @_;
# it would be nice to have a multilinestring_simplify method in B::G::U
return @{Slic3r::Geometry::Clipper::simplify_polygons(
[ map Boost::Geometry::Utils::linestring_simplify($_, $tolerance), @{$self->pp} ],
)};
}
sub simplify {
my $self = shift;
my ($tolerance) = @_;
return @{ Slic3r::Geometry::Clipper::union_ex([ $self->simplify_as_polygons($tolerance) ]) };
}
# this method only works for expolygons having only a contour or # this method only works for expolygons having only a contour or
# a contour and a hole, and not being thicker than the supplied # a contour and a hole, and not being thicker than the supplied
# width. it returns a polyline or a polygon # width. it returns a polyline or a polygon
@ -205,6 +188,7 @@ sub _medial_axis_voronoi {
} }
my @result = (); my @result = ();
my $simplify_tolerance = $width / 7;
foreach my $polyline (@polylines) { foreach my $polyline (@polylines) {
next unless @$polyline >= 2; next unless @$polyline >= 2;
@ -213,11 +197,11 @@ sub _medial_axis_voronoi {
if ($points[0]->coincides_with($points[-1])) { if ($points[0]->coincides_with($points[-1])) {
next if @points == 2; next if @points == 2;
push @result, Slic3r::Polygon->new(@points[0..$#points-1]); push @result, @{Slic3r::Polygon->new(@points[0..$#points-1])->simplify($simplify_tolerance)};
} else { } else {
push @result, Slic3r::Polyline->new(@points); push @result, Slic3r::Polyline->new(@points);
$result[-1]->simplify($simplify_tolerance);
} }
$result[-1]->simplify($width / 7);
} }
return @result; return @result;

View File

@ -38,7 +38,7 @@ sub BUILD {
my $crossing_edges = $self->_crossing_edges; my $crossing_edges = $self->_crossing_edges;
# simplify islands # simplify islands
$_->simplify($self->_inner_margin) for @{$self->islands}; @{$self->islands} = map @{$_->simplify($self->_inner_margin)}, @{$self->islands};
# process individual islands # process individual islands
for my $i (0 .. $#{$self->islands}) { for my $i (0 .. $#{$self->islands}) {

View File

@ -217,7 +217,7 @@ sub make_perimeters {
# non-collapsing regions # non-collapsing regions
$self->fill_surfaces->append( $self->fill_surfaces->append(
@{offset2_ex( @{offset2_ex(
[ map $_->simplify_as_polygons(&Slic3r::SCALED_RESOLUTION), @{union_ex(\@last)} ], [ map @{$_->simplify_p(&Slic3r::SCALED_RESOLUTION)}, @{union_ex(\@last)} ],
-($pspacing/2 + $ispacing/2), -($pspacing/2 + $ispacing/2),
+$ispacing/2, +$ispacing/2,
)} )}

View File

@ -83,21 +83,32 @@ ExPolygon::contains_point(const Point* point) const
} }
Polygons Polygons
ExPolygon::simplify(double tolerance) const ExPolygon::simplify_p(double tolerance) const
{ {
Polygons p; Polygons pp(this->holes.size() + 1);
this->contour.simplify(tolerance, p);
for (Polygons::const_iterator it = this->holes.begin(); it != this->holes.end(); ++it) // contour
it->simplify(tolerance, p); Polygon p = this->contour;
simplify_polygons(p, p); p.points = MultiPoint::_douglas_peucker(p.points, tolerance);
return p; pp.push_back(p);
// holes
for (Polygons::const_iterator it = this->holes.begin(); it != this->holes.end(); ++it) {
p = *it;
p.points = MultiPoint::_douglas_peucker(p.points, tolerance);
pp.push_back(p);
}
simplify_polygons(pp, pp);
return pp;
} }
ExPolygons ExPolygons
ExPolygon::simplify(double tolerance) const ExPolygon::simplify(double tolerance) const
{ {
Polygons p = this->simplify(tolerance); Polygons pp = this->simplify_p(tolerance);
return union_(p); ExPolygons expp;
union_(pp, expp);
return expp;
} }
void void

View File

@ -6,6 +6,9 @@
namespace Slic3r { namespace Slic3r {
class ExPolygon;
typedef std::vector<ExPolygon> ExPolygons;
class ExPolygon class ExPolygon
{ {
public: public:
@ -19,7 +22,7 @@ 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; Polygons simplify_p(double tolerance) const;
ExPolygons simplify(double tolerance) const; ExPolygons simplify(double tolerance) const;
void simplify(double tolerance, ExPolygons &expolygons) const; void simplify(double tolerance, ExPolygons &expolygons) const;
@ -33,8 +36,6 @@ class ExPolygon
#endif #endif
}; };
typedef std::vector<ExPolygon> ExPolygons;
} }
#endif #endif

View File

@ -50,11 +50,11 @@ ExPolygonCollection::contains_point(const Point* point) const
void void
ExPolygonCollection::simplify(double tolerance) ExPolygonCollection::simplify(double tolerance)
{ {
ExPolygons t; ExPolygons expp;
for (ExPolygons::const_iterator it = this->expolygons.begin(); it != this->expolygons.end(); ++it) { for (ExPolygons::const_iterator it = this->expolygons.begin(); it != this->expolygons.end(); ++it) {
it->simplify_and_append_to(tolerance, t); it->simplify(tolerance, expp);
} }
this->expolygons = t; this->expolygons = expp;
} }
} }

View File

@ -55,12 +55,6 @@ 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 Points
MultiPoint::_douglas_peucker(Points &points, double tolerance) MultiPoint::_douglas_peucker(Points &points, double tolerance)
{ {

View File

@ -21,7 +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); static Points _douglas_peucker(Points &points, double tolerance);
#ifdef SLIC3RXS #ifdef SLIC3RXS
void from_SV(SV* poly_sv); void from_SV(SV* poly_sv);
@ -29,9 +29,6 @@ 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);
}; };
} }

View File

@ -128,7 +128,21 @@ Polygon::contains_point(const Point* point) const
Polygons Polygons
Polygon::simplify(double tolerance) const Polygon::simplify(double tolerance) const
{ {
Polygon p = *this;
p.points = MultiPoint::_douglas_peucker(p.points, tolerance);
Polygons pp;
pp.push_back(p);
simplify_polygons(pp, pp);
return pp;
}
void
Polygon::simplify(double tolerance, Polygons &polygons) const
{
Polygons pp = this->simplify(tolerance);
polygons.reserve(polygons.size() + pp.size());
polygons.insert(polygons.end(), pp.begin(), pp.end());
} }
#ifdef SLIC3RXS #ifdef SLIC3RXS

View File

@ -9,6 +9,9 @@
namespace Slic3r { namespace Slic3r {
class Polygon;
typedef std::vector<Polygon> Polygons;
class Polygon : public MultiPoint { class Polygon : public MultiPoint {
public: public:
Point* last_point() const; Point* last_point() const;
@ -33,8 +36,6 @@ class Polygon : public MultiPoint {
#endif #endif
}; };
typedef std::vector<Polygon> Polygons;
} }
#endif #endif

View File

@ -87,6 +87,12 @@ Polyline::equally_spaced_points(double distance) const
return pts; return pts;
} }
void
Polyline::simplify(double tolerance)
{
this->points = MultiPoint::_douglas_peucker(this->points, tolerance);
}
#ifdef SLIC3RXS #ifdef SLIC3RXS
SV* SV*

View File

@ -17,6 +17,7 @@ class Polyline : public MultiPoint {
void clip_end(double distance); void clip_end(double distance);
void clip_start(double distance); void clip_start(double distance);
Points equally_spaced_points(double distance) const; Points equally_spaced_points(double distance) const;
void simplify(double tolerance);
#ifdef SLIC3RXS #ifdef SLIC3RXS
SV* to_SV_ref(); SV* to_SV_ref();

View File

@ -0,0 +1,13 @@
#include "SurfaceCollection.hpp"
namespace Slic3r {
void
simplify(double tolerance)
{
for (Surfaces::iterator it = this->surfaces.begin(); it != this->surfaces.end(); ++it) {
throw "Unimplemented";
}
}
}

View File

@ -9,6 +9,7 @@ class SurfaceCollection
{ {
public: public:
Surfaces surfaces; Surfaces surfaces;
void simplify(double tolerance);
}; };
} }

View File

@ -23,6 +23,8 @@
bool is_valid(); bool is_valid();
bool contains_line(Line* line); bool contains_line(Line* line);
bool contains_point(Point* point); bool contains_point(Point* point);
ExPolygons simplify(double tolerance);
Polygons simplify_p(double tolerance);
%{ %{
ExPolygon* ExPolygon*

View File

@ -11,6 +11,7 @@
%code{% THIS->surfaces.clear(); %}; %code{% THIS->surfaces.clear(); %};
int count() int count()
%code{% RETVAL = THIS->surfaces.size(); %}; %code{% RETVAL = THIS->surfaces.size(); %};
void simplify(double tolerance);
%{ %{
SurfaceCollection* SurfaceCollection*