Support the new Clipper API

This commit is contained in:
Alessandro Ranellucci 2013-11-20 15:59:19 +01:00
parent c5f7318fc9
commit 1d6a18071a
4 changed files with 51 additions and 51 deletions

View file

@ -28,19 +28,19 @@ void PolyTreeToExPolygons(ClipperLib::PolyTree& polytree, Slic3r::ExPolygons& ex
//----------------------------------------------------------- //-----------------------------------------------------------
void void
ClipperPolygon_to_Slic3rPolygon(const ClipperLib::Polygon &input, Slic3r::Polygon &output) ClipperPolygon_to_Slic3rPolygon(const ClipperLib::Path &input, Slic3r::Polygon &output)
{ {
output.points.clear(); output.points.clear();
for (ClipperLib::Polygon::const_iterator pit = input.begin(); pit != input.end(); ++pit) { for (ClipperLib::Path::const_iterator pit = input.begin(); pit != input.end(); ++pit) {
output.points.push_back(Slic3r::Point( (*pit).X, (*pit).Y )); output.points.push_back(Slic3r::Point( (*pit).X, (*pit).Y ));
} }
} }
void void
ClipperPolygons_to_Slic3rPolygons(const ClipperLib::Polygons &input, Slic3r::Polygons &output) ClipperPolygons_to_Slic3rPolygons(const ClipperLib::Paths &input, Slic3r::Polygons &output)
{ {
output.clear(); output.clear();
for (ClipperLib::Polygons::const_iterator it = input.begin(); it != input.end(); ++it) { for (ClipperLib::Paths::const_iterator it = input.begin(); it != input.end(); ++it) {
Slic3r::Polygon p; Slic3r::Polygon p;
ClipperPolygon_to_Slic3rPolygon(*it, p); ClipperPolygon_to_Slic3rPolygon(*it, p);
output.push_back(p); output.push_back(p);
@ -48,14 +48,14 @@ ClipperPolygons_to_Slic3rPolygons(const ClipperLib::Polygons &input, Slic3r::Pol
} }
void void
ClipperPolygons_to_Slic3rExPolygons(const ClipperLib::Polygons &input, Slic3r::ExPolygons &output) ClipperPolygons_to_Slic3rExPolygons(const ClipperLib::Paths &input, Slic3r::ExPolygons &output)
{ {
// init Clipper // init Clipper
ClipperLib::Clipper clipper; ClipperLib::Clipper clipper;
clipper.Clear(); clipper.Clear();
// perform union // perform union
clipper.AddPolygons(input, ClipperLib::ptSubject); clipper.AddPaths(input, ClipperLib::ptSubject, true);
ClipperLib::PolyTree* polytree = new ClipperLib::PolyTree(); ClipperLib::PolyTree* polytree = new ClipperLib::PolyTree();
clipper.Execute(ClipperLib::ctUnion, *polytree, ClipperLib::pftEvenOdd, ClipperLib::pftEvenOdd); // offset results work with both EvenOdd and NonZero clipper.Execute(ClipperLib::ctUnion, *polytree, ClipperLib::pftEvenOdd, ClipperLib::pftEvenOdd); // offset results work with both EvenOdd and NonZero
@ -67,7 +67,7 @@ ClipperPolygons_to_Slic3rExPolygons(const ClipperLib::Polygons &input, Slic3r::E
} }
void void
Slic3rPolygon_to_ClipperPolygon(const Slic3r::MultiPoint &input, ClipperLib::Polygon &output) Slic3rPolygon_to_ClipperPolygon(const Slic3r::MultiPoint &input, ClipperLib::Path &output)
{ {
output.clear(); output.clear();
for (Slic3r::Points::const_iterator pit = input.points.begin(); pit != input.points.end(); ++pit) { for (Slic3r::Points::const_iterator pit = input.points.begin(); pit != input.points.end(); ++pit) {
@ -77,21 +77,21 @@ Slic3rPolygon_to_ClipperPolygon(const Slic3r::MultiPoint &input, ClipperLib::Pol
template <class T> template <class T>
void void
Slic3rPolygons_to_ClipperPolygons(const T &input, ClipperLib::Polygons &output) Slic3rPolygons_to_ClipperPolygons(const T &input, ClipperLib::Paths &output)
{ {
output.clear(); output.clear();
for (typename T::const_iterator it = input.begin(); it != input.end(); ++it) { for (typename T::const_iterator it = input.begin(); it != input.end(); ++it) {
ClipperLib::Polygon p; ClipperLib::Path p;
Slic3rPolygon_to_ClipperPolygon(*it, p); Slic3rPolygon_to_ClipperPolygon(*it, p);
output.push_back(p); output.push_back(p);
} }
} }
void void
scaleClipperPolygons(ClipperLib::Polygons &polygons, const double scale) scaleClipperPolygons(ClipperLib::Paths &polygons, const double scale)
{ {
for (ClipperLib::Polygons::iterator it = polygons.begin(); it != polygons.end(); ++it) { for (ClipperLib::Paths::iterator it = polygons.begin(); it != polygons.end(); ++it) {
for (ClipperLib::Polygon::iterator pit = (*it).begin(); pit != (*it).end(); ++pit) { for (ClipperLib::Path::iterator pit = (*it).begin(); pit != (*it).end(); ++pit) {
(*pit).X *= scale; (*pit).X *= scale;
(*pit).Y *= scale; (*pit).Y *= scale;
} }
@ -99,18 +99,18 @@ scaleClipperPolygons(ClipperLib::Polygons &polygons, const double scale)
} }
void void
offset(Slic3r::Polygons &polygons, ClipperLib::Polygons &retval, const float delta, offset(Slic3r::Polygons &polygons, ClipperLib::Paths &retval, const float delta,
double scale, ClipperLib::JoinType joinType, double miterLimit) double scale, ClipperLib::JoinType joinType, double miterLimit)
{ {
// read input // read input
ClipperLib::Polygons* input = new ClipperLib::Polygons(); ClipperLib::Paths* input = new ClipperLib::Paths();
Slic3rPolygons_to_ClipperPolygons(polygons, *input); Slic3rPolygons_to_ClipperPolygons(polygons, *input);
// scale input // scale input
scaleClipperPolygons(*input, scale); scaleClipperPolygons(*input, scale);
// perform offset // perform offset
ClipperLib::OffsetPolygons(*input, retval, (delta*scale), joinType, miterLimit); ClipperLib::OffsetPaths(*input, retval, (delta*scale), joinType, ClipperLib::etClosed, miterLimit);
delete input; delete input;
// unscale output // unscale output
@ -122,7 +122,7 @@ offset(Slic3r::Polygons &polygons, Slic3r::Polygons &retval, const float delta,
double scale, ClipperLib::JoinType joinType, double miterLimit) double scale, ClipperLib::JoinType joinType, double miterLimit)
{ {
// perform offset // perform offset
ClipperLib::Polygons* output = new ClipperLib::Polygons(); ClipperLib::Paths* output = new ClipperLib::Paths();
offset(polygons, *output, delta, scale, joinType, miterLimit); offset(polygons, *output, delta, scale, joinType, miterLimit);
// convert into ExPolygons // convert into ExPolygons
@ -131,11 +131,11 @@ offset(Slic3r::Polygons &polygons, Slic3r::Polygons &retval, const float delta,
} }
void void
offset(Slic3r::Polylines &polylines, ClipperLib::Polygons &retval, const float delta, offset(Slic3r::Polylines &polylines, ClipperLib::Paths &retval, const float delta,
double scale, ClipperLib::JoinType joinType, double miterLimit) double scale, ClipperLib::JoinType joinType, double miterLimit)
{ {
// read input // read input
ClipperLib::Polygons* input = new ClipperLib::Polygons(); ClipperLib::Paths* input = new ClipperLib::Paths();
Slic3rPolygons_to_ClipperPolygons(polylines, *input); Slic3rPolygons_to_ClipperPolygons(polylines, *input);
// scale input // scale input
@ -154,7 +154,7 @@ offset(Slic3r::Polylines &polylines, Slic3r::Polygons &retval, const float delta
double scale, ClipperLib::JoinType joinType, double miterLimit) double scale, ClipperLib::JoinType joinType, double miterLimit)
{ {
// perform offset // perform offset
ClipperLib::Polygons* output = new ClipperLib::Polygons(); ClipperLib::Paths* output = new ClipperLib::Paths();
offset(polylines, *output, delta, scale, joinType, miterLimit); offset(polylines, *output, delta, scale, joinType, miterLimit);
// convert into ExPolygons // convert into ExPolygons
@ -167,7 +167,7 @@ offset_ex(Slic3r::Polygons &polygons, Slic3r::ExPolygons &retval, const float de
double scale, ClipperLib::JoinType joinType, double miterLimit) double scale, ClipperLib::JoinType joinType, double miterLimit)
{ {
// perform offset // perform offset
ClipperLib::Polygons* output = new ClipperLib::Polygons(); ClipperLib::Paths* output = new ClipperLib::Paths();
offset(polygons, *output, delta, scale, joinType, miterLimit); offset(polygons, *output, delta, scale, joinType, miterLimit);
// convert into ExPolygons // convert into ExPolygons
@ -176,23 +176,23 @@ offset_ex(Slic3r::Polygons &polygons, Slic3r::ExPolygons &retval, const float de
} }
void void
offset2(Slic3r::Polygons &polygons, ClipperLib::Polygons &retval, const float delta1, offset2(Slic3r::Polygons &polygons, ClipperLib::Paths &retval, const float delta1,
const float delta2, const double scale, const ClipperLib::JoinType joinType, const double miterLimit) const float delta2, const double scale, const ClipperLib::JoinType joinType, const double miterLimit)
{ {
// read input // read input
ClipperLib::Polygons* input = new ClipperLib::Polygons(); ClipperLib::Paths* input = new ClipperLib::Paths();
Slic3rPolygons_to_ClipperPolygons(polygons, *input); Slic3rPolygons_to_ClipperPolygons(polygons, *input);
// scale input // scale input
scaleClipperPolygons(*input, scale); scaleClipperPolygons(*input, scale);
// perform first offset // perform first offset
ClipperLib::Polygons* output1 = new ClipperLib::Polygons(); ClipperLib::Paths* output1 = new ClipperLib::Paths();
ClipperLib::OffsetPolygons(*input, *output1, (delta1*scale), joinType, miterLimit); ClipperLib::OffsetPaths(*input, *output1, (delta1*scale), joinType, ClipperLib::etClosed, miterLimit);
delete input; delete input;
// perform second offset // perform second offset
ClipperLib::OffsetPolygons(*output1, retval, (delta2*scale), joinType, miterLimit); ClipperLib::OffsetPaths(*output1, retval, (delta2*scale), joinType, ClipperLib::etClosed, miterLimit);
delete output1; delete output1;
// unscale output // unscale output
@ -204,7 +204,7 @@ offset2(Slic3r::Polygons &polygons, Slic3r::Polygons &retval, const float delta1
const float delta2, const double scale, const ClipperLib::JoinType joinType, const double miterLimit) const float delta2, const double scale, const ClipperLib::JoinType joinType, const double miterLimit)
{ {
// perform offset // perform offset
ClipperLib::Polygons* output = new ClipperLib::Polygons(); ClipperLib::Paths* output = new ClipperLib::Paths();
offset2(polygons, *output, delta1, delta2, scale, joinType, miterLimit); offset2(polygons, *output, delta1, delta2, scale, joinType, miterLimit);
// convert into ExPolygons // convert into ExPolygons
@ -217,7 +217,7 @@ offset2_ex(Slic3r::Polygons &polygons, Slic3r::ExPolygons &retval, const float d
const float delta2, const double scale, const ClipperLib::JoinType joinType, const double miterLimit) const float delta2, const double scale, const ClipperLib::JoinType joinType, const double miterLimit)
{ {
// perform offset // perform offset
ClipperLib::Polygons* output = new ClipperLib::Polygons(); ClipperLib::Paths* output = new ClipperLib::Paths();
offset2(polygons, *output, delta1, delta2, scale, joinType, miterLimit); offset2(polygons, *output, delta1, delta2, scale, joinType, miterLimit);
// convert into ExPolygons // convert into ExPolygons
@ -230,8 +230,8 @@ void _clipper_do(const ClipperLib::ClipType clipType, Slic3r::Polygons &subject,
Slic3r::Polygons &clip, T &retval, const ClipperLib::PolyFillType fillType, const bool safety_offset_) Slic3r::Polygons &clip, T &retval, const ClipperLib::PolyFillType fillType, const bool safety_offset_)
{ {
// read input // read input
ClipperLib::Polygons* input_subject = new ClipperLib::Polygons(); ClipperLib::Paths* input_subject = new ClipperLib::Paths();
ClipperLib::Polygons* input_clip = new ClipperLib::Polygons(); ClipperLib::Paths* input_clip = new ClipperLib::Paths();
Slic3rPolygons_to_ClipperPolygons(subject, *input_subject); Slic3rPolygons_to_ClipperPolygons(subject, *input_subject);
Slic3rPolygons_to_ClipperPolygons(clip, *input_clip); Slic3rPolygons_to_ClipperPolygons(clip, *input_clip);
@ -249,9 +249,9 @@ void _clipper_do(const ClipperLib::ClipType clipType, Slic3r::Polygons &subject,
clipper.Clear(); clipper.Clear();
// add polygons // add polygons
clipper.AddPolygons(*input_subject, ClipperLib::ptSubject); clipper.AddPaths(*input_subject, ClipperLib::ptSubject, true);
delete input_subject; delete input_subject;
clipper.AddPolygons(*input_clip, ClipperLib::ptClip); clipper.AddPaths(*input_clip, ClipperLib::ptClip, true);
delete input_clip; delete input_clip;
// perform operation // perform operation
@ -262,8 +262,8 @@ void _clipper(ClipperLib::ClipType clipType, Slic3r::Polygons &subject,
Slic3r::Polygons &clip, Slic3r::Polygons &retval, bool safety_offset_) Slic3r::Polygons &clip, Slic3r::Polygons &retval, bool safety_offset_)
{ {
// perform operation // perform operation
ClipperLib::Polygons* output = new ClipperLib::Polygons(); ClipperLib::Paths* output = new ClipperLib::Paths();
_clipper_do<ClipperLib::Polygons>(clipType, subject, clip, *output, ClipperLib::pftNonZero, safety_offset_); _clipper_do<ClipperLib::Paths>(clipType, subject, clip, *output, ClipperLib::pftNonZero, safety_offset_);
// convert into Polygons // convert into Polygons
ClipperPolygons_to_Slic3rPolygons(*output, retval); ClipperPolygons_to_Slic3rPolygons(*output, retval);
@ -322,10 +322,10 @@ void union_pt(Slic3r::Polygons &subject, ClipperLib::PolyTree &retval, bool safe
void simplify_polygons(Slic3r::Polygons &subject, Slic3r::Polygons &retval) void simplify_polygons(Slic3r::Polygons &subject, Slic3r::Polygons &retval)
{ {
// convert into Clipper polygons // convert into Clipper polygons
ClipperLib::Polygons* input_subject = new ClipperLib::Polygons(); ClipperLib::Paths* input_subject = new ClipperLib::Paths();
Slic3rPolygons_to_ClipperPolygons(subject, *input_subject); Slic3rPolygons_to_ClipperPolygons(subject, *input_subject);
ClipperLib::Polygons* output = new ClipperLib::Polygons(); ClipperLib::Paths* output = new ClipperLib::Paths();
ClipperLib::SimplifyPolygons(*input_subject, *output, ClipperLib::pftNonZero); ClipperLib::SimplifyPolygons(*input_subject, *output, ClipperLib::pftNonZero);
delete input_subject; delete input_subject;
@ -334,14 +334,14 @@ void simplify_polygons(Slic3r::Polygons &subject, Slic3r::Polygons &retval)
delete output; delete output;
} }
void safety_offset(ClipperLib::Polygons* &subject) void safety_offset(ClipperLib::Paths* &subject)
{ {
// scale input // scale input
scaleClipperPolygons(*subject, CLIPPER_OFFSET_SCALE); scaleClipperPolygons(*subject, CLIPPER_OFFSET_SCALE);
// perform offset (delta = scale 1e-05) // perform offset (delta = scale 1e-05)
ClipperLib::Polygons* retval = new ClipperLib::Polygons(); ClipperLib::Paths* retval = new ClipperLib::Paths();
ClipperLib::OffsetPolygons(*subject, *retval, 10.0 * CLIPPER_OFFSET_SCALE, ClipperLib::jtMiter, 2); ClipperLib::OffsetPaths(*subject, *retval, 10.0 * CLIPPER_OFFSET_SCALE, ClipperLib::jtMiter, ClipperLib::etClosed, 2);
// unscale output // unscale output
scaleClipperPolygons(*retval, 1.0/CLIPPER_OFFSET_SCALE); scaleClipperPolygons(*retval, 1.0/CLIPPER_OFFSET_SCALE);

View file

@ -21,17 +21,17 @@ void AddOuterPolyNodeToExPolygons(ClipperLib::PolyNode& polynode, Slic3r::ExPoly
void PolyTreeToExPolygons(ClipperLib::PolyTree& polytree, Slic3r::ExPolygons& expolygons); void PolyTreeToExPolygons(ClipperLib::PolyTree& polytree, Slic3r::ExPolygons& expolygons);
//----------------------------------------------------------- //-----------------------------------------------------------
void Slic3rPolygon_to_ClipperPolygon(const Slic3r::MultiPoint &input, ClipperLib::Polygon &output); void Slic3rPolygon_to_ClipperPolygon(const Slic3r::MultiPoint &input, ClipperLib::Path &output);
template <class T> template <class T>
void Slic3rPolygons_to_ClipperPolygons(const T &input, ClipperLib::Polygons &output); void Slic3rPolygons_to_ClipperPolygons(const T &input, ClipperLib::Paths &output);
void ClipperPolygon_to_Slic3rPolygon(const ClipperLib::Polygon &input, Slic3r::Polygon &output); void ClipperPolygon_to_Slic3rPolygon(const ClipperLib::Path &input, Slic3r::Polygon &output);
void ClipperPolygons_to_Slic3rPolygons(const ClipperLib::Polygons &input, Slic3r::Polygons &output); void ClipperPolygons_to_Slic3rPolygons(const ClipperLib::Paths &input, Slic3r::Polygons &output);
void ClipperPolygons_to_Slic3rExPolygons(const ClipperLib::Polygons &input, Slic3r::ExPolygons &output); void ClipperPolygons_to_Slic3rExPolygons(const ClipperLib::Paths &input, Slic3r::ExPolygons &output);
void scaleClipperPolygons(ClipperLib::Polygons &polygons, const double scale); void scaleClipperPolygons(ClipperLib::Paths &polygons, const double scale);
// offset Polygons // offset Polygons
void offset(Slic3r::Polygons &polygons, ClipperLib::Polygons &retval, const float delta, void offset(Slic3r::Polygons &polygons, ClipperLib::Paths &retval, const float delta,
double scale = 100000, ClipperLib::JoinType joinType = ClipperLib::jtMiter, double scale = 100000, ClipperLib::JoinType joinType = ClipperLib::jtMiter,
double miterLimit = 3); double miterLimit = 3);
void offset(Slic3r::Polygons &polygons, Slic3r::Polygons &retval, const float delta, void offset(Slic3r::Polygons &polygons, Slic3r::Polygons &retval, const float delta,
@ -39,7 +39,7 @@ void offset(Slic3r::Polygons &polygons, Slic3r::Polygons &retval, const float de
double miterLimit = 3); double miterLimit = 3);
// offset Polylines // offset Polylines
void offset(Slic3r::Polylines &polylines, ClipperLib::Polygons &retval, const float delta, void offset(Slic3r::Polylines &polylines, ClipperLib::Paths &retval, const float delta,
double scale = 100000, ClipperLib::JoinType joinType = ClipperLib::jtSquare, double scale = 100000, ClipperLib::JoinType joinType = ClipperLib::jtSquare,
double miterLimit = 3); double miterLimit = 3);
void offset(Slic3r::Polylines &polylines, Slic3r::Polygons &retval, const float delta, void offset(Slic3r::Polylines &polylines, Slic3r::Polygons &retval, const float delta,
@ -50,7 +50,7 @@ void offset_ex(Slic3r::Polygons &polygons, Slic3r::ExPolygons &retval, const flo
double scale = 100000, ClipperLib::JoinType joinType = ClipperLib::jtMiter, double scale = 100000, ClipperLib::JoinType joinType = ClipperLib::jtMiter,
double miterLimit = 3); double miterLimit = 3);
void offset2(Slic3r::Polygons &polygons, ClipperLib::Polygons &retval, const float delta1, void offset2(Slic3r::Polygons &polygons, ClipperLib::Paths &retval, const float delta1,
const float delta2, double scale = 100000, ClipperLib::JoinType joinType = ClipperLib::jtMiter, const float delta2, double scale = 100000, ClipperLib::JoinType joinType = ClipperLib::jtMiter,
double miterLimit = 3); double miterLimit = 3);
void offset2(Slic3r::Polygons &polygons, Slic3r::Polygons &retval, const float delta1, void offset2(Slic3r::Polygons &polygons, Slic3r::Polygons &retval, const float delta1,
@ -84,7 +84,7 @@ void union_pt(Slic3r::Polygons &subject, ClipperLib::PolyTree &retval, bool safe
void simplify_polygons(Slic3r::Polygons &subject, Slic3r::Polygons &retval); void simplify_polygons(Slic3r::Polygons &subject, Slic3r::Polygons &retval);
void safety_offset(ClipperLib::Polygons* &subject); void safety_offset(ClipperLib::Paths* &subject);
///////////////// /////////////////

View file

@ -65,7 +65,7 @@ Polygon::equally_spaced_points(double distance) const
double double
Polygon::area() const Polygon::area() const
{ {
ClipperLib::Polygon p; ClipperLib::Path p;
Slic3rPolygon_to_ClipperPolygon(*this, p); Slic3rPolygon_to_ClipperPolygon(*this, p);
return ClipperLib::Area(p); return ClipperLib::Area(p);
} }
@ -73,7 +73,7 @@ Polygon::area() const
bool bool
Polygon::is_counter_clockwise() const Polygon::is_counter_clockwise() const
{ {
ClipperLib::Polygon* p = new ClipperLib::Polygon(); ClipperLib::Path* p = new ClipperLib::Path();
Slic3rPolygon_to_ClipperPolygon(*this, *p); Slic3rPolygon_to_ClipperPolygon(*this, *p);
bool orientation = ClipperLib::Orientation(*p); bool orientation = ClipperLib::Orientation(*p);
delete p; delete p;

View file

@ -49,7 +49,7 @@
//When enabled, code developed with earlier versions of Clipper //When enabled, code developed with earlier versions of Clipper
//(ie prior to ver 6) should compile without changes. //(ie prior to ver 6) should compile without changes.
//In a future update, this compatability code will be removed. //In a future update, this compatability code will be removed.
#define use_deprecated //#define use_deprecated
#include <vector> #include <vector>
#include <set> #include <set>