2013-07-16 19:04:14 +00:00
|
|
|
#include "ClipperUtils.hpp"
|
2013-11-23 22:21:59 +00:00
|
|
|
#include "Geometry.hpp"
|
2013-07-16 19:04:14 +00:00
|
|
|
|
2016-09-13 11:30:00 +00:00
|
|
|
// #define CLIPPER_UTILS_DEBUG
|
|
|
|
|
|
|
|
#ifdef CLIPPER_UTILS_DEBUG
|
|
|
|
#include "SVG.hpp"
|
|
|
|
#endif /* CLIPPER_UTILS_DEBUG */
|
|
|
|
|
2013-07-16 19:04:14 +00:00
|
|
|
namespace Slic3r {
|
|
|
|
|
|
|
|
//-----------------------------------------------------------
|
|
|
|
// legacy code from Clipper documentation
|
2014-11-15 22:44:03 +00:00
|
|
|
void AddOuterPolyNodeToExPolygons(ClipperLib::PolyNode& polynode, Slic3r::ExPolygons* expolygons)
|
2013-07-16 19:04:14 +00:00
|
|
|
{
|
2014-11-15 22:44:03 +00:00
|
|
|
size_t cnt = expolygons->size();
|
|
|
|
expolygons->resize(cnt + 1);
|
|
|
|
ClipperPath_to_Slic3rMultiPoint(polynode.Contour, &(*expolygons)[cnt].contour);
|
|
|
|
(*expolygons)[cnt].holes.resize(polynode.ChildCount());
|
2013-07-16 19:04:14 +00:00
|
|
|
for (int i = 0; i < polynode.ChildCount(); ++i)
|
|
|
|
{
|
2014-11-15 22:44:03 +00:00
|
|
|
ClipperPath_to_Slic3rMultiPoint(polynode.Childs[i]->Contour, &(*expolygons)[cnt].holes[i]);
|
2013-07-16 19:04:14 +00:00
|
|
|
//Add outer polygons contained by (nested within) holes ...
|
|
|
|
for (int j = 0; j < polynode.Childs[i]->ChildCount(); ++j)
|
|
|
|
AddOuterPolyNodeToExPolygons(*polynode.Childs[i]->Childs[j], expolygons);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-15 22:44:03 +00:00
|
|
|
void PolyTreeToExPolygons(ClipperLib::PolyTree& polytree, Slic3r::ExPolygons* expolygons)
|
2013-07-16 19:04:14 +00:00
|
|
|
{
|
2014-11-15 22:44:03 +00:00
|
|
|
expolygons->clear();
|
2013-07-16 19:04:14 +00:00
|
|
|
for (int i = 0; i < polytree.ChildCount(); ++i)
|
|
|
|
AddOuterPolyNodeToExPolygons(*polytree.Childs[i], expolygons);
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------
|
|
|
|
|
2013-11-21 13:15:38 +00:00
|
|
|
template <class T>
|
2013-07-16 19:04:14 +00:00
|
|
|
void
|
2014-11-15 22:44:03 +00:00
|
|
|
ClipperPath_to_Slic3rMultiPoint(const ClipperLib::Path &input, T* output)
|
2013-07-16 19:04:14 +00:00
|
|
|
{
|
2014-11-15 22:44:03 +00:00
|
|
|
output->points.clear();
|
2016-10-20 11:04:23 +00:00
|
|
|
output->points.reserve(input.size());
|
|
|
|
for (ClipperLib::Path::const_iterator pit = input.begin(); pit != input.end(); ++pit)
|
2014-11-15 22:44:03 +00:00
|
|
|
output->points.push_back(Slic3r::Point( (*pit).X, (*pit).Y ));
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|
2015-12-19 09:20:05 +00:00
|
|
|
template void ClipperPath_to_Slic3rMultiPoint<Slic3r::Polygon>(const ClipperLib::Path &input, Slic3r::Polygon* output);
|
2013-07-16 19:04:14 +00:00
|
|
|
|
2013-11-21 13:15:38 +00:00
|
|
|
template <class T>
|
2013-07-16 19:04:14 +00:00
|
|
|
void
|
2014-11-15 22:44:03 +00:00
|
|
|
ClipperPaths_to_Slic3rMultiPoints(const ClipperLib::Paths &input, T* output)
|
2013-07-16 19:04:14 +00:00
|
|
|
{
|
2014-11-15 22:44:03 +00:00
|
|
|
output->clear();
|
2016-10-20 11:04:23 +00:00
|
|
|
output->reserve(input.size());
|
2013-11-20 14:59:19 +00:00
|
|
|
for (ClipperLib::Paths::const_iterator it = input.begin(); it != input.end(); ++it) {
|
2013-11-21 13:15:38 +00:00
|
|
|
typename T::value_type p;
|
2014-11-15 22:44:03 +00:00
|
|
|
ClipperPath_to_Slic3rMultiPoint(*it, &p);
|
|
|
|
output->push_back(p);
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-11-15 22:44:03 +00:00
|
|
|
ClipperPaths_to_Slic3rExPolygons(const ClipperLib::Paths &input, Slic3r::ExPolygons* output)
|
2013-07-16 19:04:14 +00:00
|
|
|
{
|
|
|
|
// init Clipper
|
|
|
|
ClipperLib::Clipper clipper;
|
|
|
|
clipper.Clear();
|
|
|
|
|
|
|
|
// perform union
|
2013-11-20 14:59:19 +00:00
|
|
|
clipper.AddPaths(input, ClipperLib::ptSubject, true);
|
2014-05-08 13:00:49 +00:00
|
|
|
ClipperLib::PolyTree polytree;
|
|
|
|
clipper.Execute(ClipperLib::ctUnion, polytree, ClipperLib::pftEvenOdd, ClipperLib::pftEvenOdd); // offset results work with both EvenOdd and NonZero
|
2013-07-16 19:04:14 +00:00
|
|
|
|
|
|
|
// write to ExPolygons object
|
2014-11-15 22:44:03 +00:00
|
|
|
output->clear();
|
2014-05-08 13:00:49 +00:00
|
|
|
PolyTreeToExPolygons(polytree, output);
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|
|
|
|
|
2013-07-16 22:48:29 +00:00
|
|
|
void
|
2014-11-15 22:44:03 +00:00
|
|
|
Slic3rMultiPoint_to_ClipperPath(const Slic3r::MultiPoint &input, ClipperLib::Path* output)
|
2013-07-16 22:48:29 +00:00
|
|
|
{
|
2014-11-15 22:44:03 +00:00
|
|
|
output->clear();
|
2016-10-20 11:04:23 +00:00
|
|
|
output->reserve(input.points.size());
|
|
|
|
for (Slic3r::Points::const_iterator pit = input.points.begin(); pit != input.points.end(); ++pit)
|
|
|
|
output->push_back(ClipperLib::IntPoint( (*pit).x, (*pit).y ));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Slic3rMultiPoint_to_ClipperPath_reversed(const Slic3r::MultiPoint &input, ClipperLib::Path* output)
|
|
|
|
{
|
|
|
|
output->clear();
|
|
|
|
output->reserve(input.points.size());
|
|
|
|
for (Slic3r::Points::const_reverse_iterator pit = input.points.rbegin(); pit != input.points.rend(); ++pit)
|
2014-11-15 22:44:03 +00:00
|
|
|
output->push_back(ClipperLib::IntPoint( (*pit).x, (*pit).y ));
|
2013-07-16 22:48:29 +00:00
|
|
|
}
|
|
|
|
|
2013-11-20 10:35:58 +00:00
|
|
|
template <class T>
|
2013-07-16 22:48:29 +00:00
|
|
|
void
|
2014-11-15 22:44:03 +00:00
|
|
|
Slic3rMultiPoints_to_ClipperPaths(const T &input, ClipperLib::Paths* output)
|
2013-07-16 22:48:29 +00:00
|
|
|
{
|
2014-11-15 22:44:03 +00:00
|
|
|
output->clear();
|
2016-10-20 11:04:23 +00:00
|
|
|
output->reserve(input.size());
|
2013-11-20 10:35:58 +00:00
|
|
|
for (typename T::const_iterator it = input.begin(); it != input.end(); ++it) {
|
2016-10-20 11:04:23 +00:00
|
|
|
// std::vector< IntPoint >, IntPoint is a pair of int64_t
|
2013-11-20 14:59:19 +00:00
|
|
|
ClipperLib::Path p;
|
2014-11-15 22:44:03 +00:00
|
|
|
Slic3rMultiPoint_to_ClipperPath(*it, &p);
|
|
|
|
output->push_back(p);
|
2013-07-16 22:48:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-20 11:04:23 +00:00
|
|
|
void
|
|
|
|
scaleClipperPolygon(ClipperLib::Path &polygon, const double scale)
|
|
|
|
{
|
|
|
|
for (ClipperLib::Path::iterator pit = polygon.begin(); pit != polygon.end(); ++pit) {
|
|
|
|
//FIXME multiplication of int64_t by double!
|
|
|
|
// Replace by bit shifts?
|
|
|
|
(*pit).X *= scale;
|
|
|
|
(*pit).Y *= scale;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-16 19:04:14 +00:00
|
|
|
void
|
2013-11-20 14:59:19 +00:00
|
|
|
scaleClipperPolygons(ClipperLib::Paths &polygons, const double scale)
|
2013-07-16 19:04:14 +00:00
|
|
|
{
|
2013-11-20 14:59:19 +00:00
|
|
|
for (ClipperLib::Paths::iterator it = polygons.begin(); it != polygons.end(); ++it) {
|
|
|
|
for (ClipperLib::Path::iterator pit = (*it).begin(); pit != (*it).end(); ++pit) {
|
2016-10-20 11:04:23 +00:00
|
|
|
//FIXME multiplication of int64_t by double!
|
|
|
|
// Replace by bit shifts?
|
2013-07-16 19:04:14 +00:00
|
|
|
(*pit).X *= scale;
|
|
|
|
(*pit).Y *= scale;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-11-15 22:44:03 +00:00
|
|
|
offset(const Slic3r::Polygons &polygons, ClipperLib::Paths* retval, const float delta,
|
2013-07-16 19:04:14 +00:00
|
|
|
double scale, ClipperLib::JoinType joinType, double miterLimit)
|
|
|
|
{
|
|
|
|
// read input
|
2013-12-24 11:40:46 +00:00
|
|
|
ClipperLib::Paths input;
|
2014-11-15 22:44:03 +00:00
|
|
|
Slic3rMultiPoints_to_ClipperPaths(polygons, &input);
|
2013-07-16 19:04:14 +00:00
|
|
|
|
|
|
|
// scale input
|
2013-12-24 11:40:46 +00:00
|
|
|
scaleClipperPolygons(input, scale);
|
2013-07-16 19:04:14 +00:00
|
|
|
|
|
|
|
// perform offset
|
2013-12-24 11:40:46 +00:00
|
|
|
ClipperLib::ClipperOffset co;
|
|
|
|
if (joinType == jtRound) {
|
|
|
|
co.ArcTolerance = miterLimit;
|
|
|
|
} else {
|
|
|
|
co.MiterLimit = miterLimit;
|
|
|
|
}
|
|
|
|
co.AddPaths(input, joinType, ClipperLib::etClosedPolygon);
|
2014-11-15 22:44:03 +00:00
|
|
|
co.Execute(*retval, (delta*scale));
|
2013-07-16 19:04:14 +00:00
|
|
|
|
|
|
|
// unscale output
|
2014-11-15 22:44:03 +00:00
|
|
|
scaleClipperPolygons(*retval, 1/scale);
|
2013-07-16 22:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-11-15 22:44:03 +00:00
|
|
|
offset(const Slic3r::Polygons &polygons, Slic3r::Polygons* retval, const float delta,
|
2013-07-16 22:48:29 +00:00
|
|
|
double scale, ClipperLib::JoinType joinType, double miterLimit)
|
|
|
|
{
|
|
|
|
// perform offset
|
2013-12-24 11:40:46 +00:00
|
|
|
ClipperLib::Paths output;
|
2014-11-15 22:44:03 +00:00
|
|
|
offset(polygons, &output, delta, scale, joinType, miterLimit);
|
2013-07-16 22:48:29 +00:00
|
|
|
|
|
|
|
// convert into ExPolygons
|
2013-12-24 11:40:46 +00:00
|
|
|
ClipperPaths_to_Slic3rMultiPoints(output, retval);
|
2013-07-16 22:48:29 +00:00
|
|
|
}
|
|
|
|
|
2015-07-23 13:53:02 +00:00
|
|
|
Slic3r::Polygons
|
|
|
|
offset(const Slic3r::Polygons &polygons, const float delta,
|
|
|
|
double scale, ClipperLib::JoinType joinType, double miterLimit)
|
|
|
|
{
|
|
|
|
Slic3r::Polygons pp;
|
|
|
|
offset(polygons, &pp, delta, scale, joinType, miterLimit);
|
|
|
|
return pp;
|
|
|
|
}
|
|
|
|
|
2013-11-20 10:35:58 +00:00
|
|
|
void
|
2014-11-15 22:44:03 +00:00
|
|
|
offset(const Slic3r::Polylines &polylines, ClipperLib::Paths* retval, const float delta,
|
2013-11-20 10:35:58 +00:00
|
|
|
double scale, ClipperLib::JoinType joinType, double miterLimit)
|
|
|
|
{
|
|
|
|
// read input
|
2013-12-24 11:40:46 +00:00
|
|
|
ClipperLib::Paths input;
|
2014-11-15 22:44:03 +00:00
|
|
|
Slic3rMultiPoints_to_ClipperPaths(polylines, &input);
|
2013-11-20 10:35:58 +00:00
|
|
|
|
|
|
|
// scale input
|
2013-12-24 11:40:46 +00:00
|
|
|
scaleClipperPolygons(input, scale);
|
2013-11-20 10:35:58 +00:00
|
|
|
|
|
|
|
// perform offset
|
2013-12-24 11:40:46 +00:00
|
|
|
ClipperLib::ClipperOffset co;
|
|
|
|
if (joinType == jtRound) {
|
|
|
|
co.ArcTolerance = miterLimit;
|
|
|
|
} else {
|
|
|
|
co.MiterLimit = miterLimit;
|
|
|
|
}
|
|
|
|
co.AddPaths(input, joinType, ClipperLib::etOpenButt);
|
2014-11-15 22:44:03 +00:00
|
|
|
co.Execute(*retval, (delta*scale));
|
2013-11-20 10:35:58 +00:00
|
|
|
|
|
|
|
// unscale output
|
2014-11-15 22:44:03 +00:00
|
|
|
scaleClipperPolygons(*retval, 1/scale);
|
2013-11-20 10:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-11-15 22:44:03 +00:00
|
|
|
offset(const Slic3r::Polylines &polylines, Slic3r::Polygons* retval, const float delta,
|
2013-11-20 10:35:58 +00:00
|
|
|
double scale, ClipperLib::JoinType joinType, double miterLimit)
|
|
|
|
{
|
|
|
|
// perform offset
|
2014-05-08 13:00:49 +00:00
|
|
|
ClipperLib::Paths output;
|
2014-11-15 22:44:03 +00:00
|
|
|
offset(polylines, &output, delta, scale, joinType, miterLimit);
|
2013-11-20 10:35:58 +00:00
|
|
|
|
|
|
|
// convert into ExPolygons
|
2014-05-08 13:00:49 +00:00
|
|
|
ClipperPaths_to_Slic3rMultiPoints(output, retval);
|
2013-11-20 10:35:58 +00:00
|
|
|
}
|
|
|
|
|
2013-11-23 17:29:25 +00:00
|
|
|
void
|
2014-11-15 22:44:03 +00:00
|
|
|
offset(const Slic3r::Surface &surface, Slic3r::Surfaces* retval, const float delta,
|
2013-11-23 17:29:25 +00:00
|
|
|
double scale, ClipperLib::JoinType joinType, double miterLimit)
|
|
|
|
{
|
|
|
|
// perform offset
|
|
|
|
Slic3r::ExPolygons expp;
|
2014-11-15 22:44:03 +00:00
|
|
|
offset(surface.expolygon, &expp, delta, scale, joinType, miterLimit);
|
2013-11-23 17:29:25 +00:00
|
|
|
|
|
|
|
// clone the input surface for each expolygon we got
|
2014-11-15 22:44:03 +00:00
|
|
|
retval->clear();
|
|
|
|
retval->reserve(expp.size());
|
2013-11-23 17:29:25 +00:00
|
|
|
for (ExPolygons::iterator it = expp.begin(); it != expp.end(); ++it) {
|
|
|
|
Surface s = surface; // clone
|
|
|
|
s.expolygon = *it;
|
2014-11-15 22:44:03 +00:00
|
|
|
retval->push_back(s);
|
2013-11-23 17:29:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-16 22:48:29 +00:00
|
|
|
void
|
2014-11-15 22:44:03 +00:00
|
|
|
offset(const Slic3r::Polygons &polygons, Slic3r::ExPolygons* retval, const float delta,
|
2013-07-16 22:48:29 +00:00
|
|
|
double scale, ClipperLib::JoinType joinType, double miterLimit)
|
|
|
|
{
|
|
|
|
// perform offset
|
2014-05-08 13:00:49 +00:00
|
|
|
ClipperLib::Paths output;
|
2014-11-15 22:44:03 +00:00
|
|
|
offset(polygons, &output, delta, scale, joinType, miterLimit);
|
2013-07-16 19:04:14 +00:00
|
|
|
|
|
|
|
// convert into ExPolygons
|
2014-05-08 13:00:49 +00:00
|
|
|
ClipperPaths_to_Slic3rExPolygons(output, retval);
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|
|
|
|
|
2016-10-20 11:04:23 +00:00
|
|
|
// This is a safe variant of the polygon offset, tailored for a single ExPolygon:
|
|
|
|
// a single polygon with multiple non-overlapping holes.
|
|
|
|
// Each contour and hole is offsetted separately, then the holes are subtracted from the outer contours.
|
|
|
|
void offset(const Slic3r::ExPolygon &expolygon, ClipperLib::Paths* retval, const float delta,
|
|
|
|
double scale, ClipperLib::JoinType joinType, double miterLimit)
|
|
|
|
{
|
|
|
|
// printf("new ExPolygon offset\n");
|
|
|
|
// 1) Offset the outer contour.
|
|
|
|
const float delta_scaled = float(delta * scale);
|
|
|
|
ClipperLib::Paths contours;
|
|
|
|
{
|
|
|
|
ClipperLib::Path input;
|
|
|
|
Slic3rMultiPoint_to_ClipperPath(expolygon.contour, &input);
|
|
|
|
scaleClipperPolygon(input, scale);
|
|
|
|
ClipperLib::ClipperOffset co;
|
|
|
|
if (joinType == jtRound)
|
|
|
|
co.ArcTolerance = miterLimit;
|
|
|
|
else
|
|
|
|
co.MiterLimit = miterLimit;
|
|
|
|
co.AddPath(input, joinType, ClipperLib::etClosedPolygon);
|
|
|
|
co.Execute(contours, delta_scaled);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2) Offset the holes one by one, collect the results.
|
|
|
|
ClipperLib::Paths holes;
|
|
|
|
{
|
|
|
|
holes.reserve(expolygon.holes.size());
|
|
|
|
for (Polygons::const_iterator it_hole = expolygon.holes.begin(); it_hole != expolygon.holes.end(); ++ it_hole) {
|
|
|
|
ClipperLib::Path input;
|
|
|
|
Slic3rMultiPoint_to_ClipperPath_reversed(*it_hole, &input);
|
|
|
|
scaleClipperPolygon(input, scale);
|
|
|
|
ClipperLib::ClipperOffset co;
|
|
|
|
if (joinType == jtRound)
|
|
|
|
co.ArcTolerance = miterLimit;
|
|
|
|
else
|
|
|
|
co.MiterLimit = miterLimit;
|
|
|
|
co.AddPath(input, joinType, ClipperLib::etClosedPolygon);
|
|
|
|
ClipperLib::Paths out;
|
|
|
|
co.Execute(out, - delta_scaled);
|
|
|
|
holes.insert(holes.end(), out.begin(), out.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3) Subtract holes from the contours.
|
|
|
|
ClipperLib::Paths output;
|
|
|
|
{
|
|
|
|
ClipperLib::Clipper clipper;
|
|
|
|
clipper.Clear();
|
|
|
|
clipper.AddPaths(contours, ClipperLib::ptSubject, true);
|
|
|
|
clipper.AddPaths(holes, ClipperLib::ptClip, true);
|
|
|
|
clipper.Execute(ClipperLib::ctDifference, *retval, ClipperLib::pftNonZero, ClipperLib::pftNonZero);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 4) Unscale the output.
|
|
|
|
scaleClipperPolygons(*retval, 1/scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
Slic3r::Polygons offset(const Slic3r::ExPolygon &expolygon, const float delta,
|
|
|
|
double scale, ClipperLib::JoinType joinType, double miterLimit)
|
|
|
|
{
|
|
|
|
// perform offset
|
|
|
|
ClipperLib::Paths output;
|
|
|
|
offset(expolygon, &output, delta, scale, joinType, miterLimit);
|
|
|
|
|
|
|
|
// convert into ExPolygons
|
|
|
|
Slic3r::Polygons retval;
|
|
|
|
ClipperPaths_to_Slic3rMultiPoints(output, &retval);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2015-10-26 22:23:03 +00:00
|
|
|
Slic3r::ExPolygons
|
|
|
|
offset_ex(const Slic3r::Polygons &polygons, const float delta,
|
|
|
|
double scale, ClipperLib::JoinType joinType, double miterLimit)
|
|
|
|
{
|
|
|
|
Slic3r::ExPolygons expp;
|
|
|
|
offset(polygons, &expp, delta, scale, joinType, miterLimit);
|
|
|
|
return expp;
|
|
|
|
}
|
|
|
|
|
2013-07-16 19:04:14 +00:00
|
|
|
void
|
2014-11-15 22:44:03 +00:00
|
|
|
offset2(const Slic3r::Polygons &polygons, ClipperLib::Paths* retval, const float delta1,
|
2013-08-26 13:57:54 +00:00
|
|
|
const float delta2, const double scale, const ClipperLib::JoinType joinType, const double miterLimit)
|
2013-07-16 19:04:14 +00:00
|
|
|
{
|
2016-09-13 11:30:00 +00:00
|
|
|
#ifdef CLIPPER_UTILS_DEBUG
|
|
|
|
BoundingBox bbox = get_extents(polygons);
|
|
|
|
coordf_t stroke_width = scale_(0.005);
|
|
|
|
static int iRun = 0;
|
|
|
|
++ iRun;
|
|
|
|
bool flipY = false;
|
2016-10-21 08:18:01 +00:00
|
|
|
SVG svg(debug_out_path("offset2-%d.svg", iRun), bbox, scale_(1.), flipY);
|
2016-09-13 11:30:00 +00:00
|
|
|
for (Slic3r::Polygons::const_iterator it = polygons.begin(); it != polygons.end(); ++ it)
|
|
|
|
svg.draw(it->lines(), "gray", stroke_width);
|
|
|
|
#endif /* CLIPPER_UTILS_DEBUG */
|
|
|
|
|
2013-07-16 19:04:14 +00:00
|
|
|
// read input
|
2013-12-24 11:40:46 +00:00
|
|
|
ClipperLib::Paths input;
|
2014-11-15 22:44:03 +00:00
|
|
|
Slic3rMultiPoints_to_ClipperPaths(polygons, &input);
|
2013-07-16 19:04:14 +00:00
|
|
|
|
|
|
|
// scale input
|
2013-12-24 11:40:46 +00:00
|
|
|
scaleClipperPolygons(input, scale);
|
|
|
|
|
|
|
|
// prepare ClipperOffset object
|
|
|
|
ClipperLib::ClipperOffset co;
|
|
|
|
if (joinType == jtRound) {
|
|
|
|
co.ArcTolerance = miterLimit;
|
|
|
|
} else {
|
|
|
|
co.MiterLimit = miterLimit;
|
|
|
|
}
|
2013-07-16 19:04:14 +00:00
|
|
|
|
|
|
|
// perform first offset
|
2013-12-24 11:40:46 +00:00
|
|
|
ClipperLib::Paths output1;
|
|
|
|
co.AddPaths(input, joinType, ClipperLib::etClosedPolygon);
|
|
|
|
co.Execute(output1, (delta1*scale));
|
2016-09-13 11:30:00 +00:00
|
|
|
#ifdef CLIPPER_UTILS_DEBUG
|
|
|
|
svg.draw(output1, 1./CLIPPER_OFFSET_SCALE, "red", stroke_width);
|
|
|
|
#endif /* CLIPPER_UTILS_DEBUG */
|
2013-07-16 19:04:14 +00:00
|
|
|
|
|
|
|
// perform second offset
|
2013-12-24 11:40:46 +00:00
|
|
|
co.Clear();
|
|
|
|
co.AddPaths(output1, joinType, ClipperLib::etClosedPolygon);
|
2014-11-15 22:44:03 +00:00
|
|
|
co.Execute(*retval, (delta2*scale));
|
2016-09-13 11:30:00 +00:00
|
|
|
#ifdef CLIPPER_UTILS_DEBUG
|
|
|
|
svg.draw(*retval, 1./CLIPPER_OFFSET_SCALE, "green", stroke_width);
|
|
|
|
#endif /* CLIPPER_UTILS_DEBUG */
|
|
|
|
|
2013-07-16 19:04:14 +00:00
|
|
|
// unscale output
|
2014-11-15 22:44:03 +00:00
|
|
|
scaleClipperPolygons(*retval, 1/scale);
|
2013-07-16 22:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-11-15 22:44:03 +00:00
|
|
|
offset2(const Slic3r::Polygons &polygons, Slic3r::Polygons* retval, const float delta1,
|
2013-08-26 13:57:54 +00:00
|
|
|
const float delta2, const double scale, const ClipperLib::JoinType joinType, const double miterLimit)
|
2013-07-16 22:48:29 +00:00
|
|
|
{
|
|
|
|
// perform offset
|
2014-05-08 13:00:49 +00:00
|
|
|
ClipperLib::Paths output;
|
2014-11-15 22:44:03 +00:00
|
|
|
offset2(polygons, &output, delta1, delta2, scale, joinType, miterLimit);
|
2013-07-16 22:48:29 +00:00
|
|
|
|
|
|
|
// convert into ExPolygons
|
2014-05-08 13:00:49 +00:00
|
|
|
ClipperPaths_to_Slic3rMultiPoints(output, retval);
|
2013-07-16 22:48:29 +00:00
|
|
|
}
|
|
|
|
|
2015-07-23 13:53:02 +00:00
|
|
|
Slic3r::Polygons
|
|
|
|
offset2(const Slic3r::Polygons &polygons, const float delta1,
|
|
|
|
const float delta2, const double scale, const ClipperLib::JoinType joinType, const double miterLimit)
|
|
|
|
{
|
|
|
|
Slic3r::Polygons pp;
|
|
|
|
offset2(polygons, &pp, delta1, delta2, scale, joinType, miterLimit);
|
|
|
|
return pp;
|
|
|
|
}
|
|
|
|
|
2013-07-16 22:48:29 +00:00
|
|
|
void
|
2014-11-15 22:44:03 +00:00
|
|
|
offset2(const Slic3r::Polygons &polygons, Slic3r::ExPolygons* retval, const float delta1,
|
2013-08-26 13:57:54 +00:00
|
|
|
const float delta2, const double scale, const ClipperLib::JoinType joinType, const double miterLimit)
|
2013-07-16 22:48:29 +00:00
|
|
|
{
|
|
|
|
// perform offset
|
2014-05-08 13:00:49 +00:00
|
|
|
ClipperLib::Paths output;
|
2014-11-15 22:44:03 +00:00
|
|
|
offset2(polygons, &output, delta1, delta2, scale, joinType, miterLimit);
|
2013-07-16 19:04:14 +00:00
|
|
|
|
|
|
|
// convert into ExPolygons
|
2014-05-08 13:00:49 +00:00
|
|
|
ClipperPaths_to_Slic3rExPolygons(output, retval);
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|
|
|
|
|
2015-07-23 13:53:02 +00:00
|
|
|
Slic3r::ExPolygons
|
|
|
|
offset2_ex(const Slic3r::Polygons &polygons, const float delta1,
|
|
|
|
const float delta2, const double scale, const ClipperLib::JoinType joinType, const double miterLimit)
|
|
|
|
{
|
|
|
|
Slic3r::ExPolygons expp;
|
|
|
|
offset2(polygons, &expp, delta1, delta2, scale, joinType, miterLimit);
|
|
|
|
return expp;
|
|
|
|
}
|
|
|
|
|
2013-07-16 23:21:30 +00:00
|
|
|
template <class T>
|
2013-11-21 14:12:06 +00:00
|
|
|
void _clipper_do(const ClipperLib::ClipType clipType, const Slic3r::Polygons &subject,
|
2014-11-15 22:44:03 +00:00
|
|
|
const Slic3r::Polygons &clip, T* retval, const ClipperLib::PolyFillType fillType, const bool safety_offset_)
|
2013-07-16 19:04:14 +00:00
|
|
|
{
|
|
|
|
// read input
|
2014-05-08 13:00:49 +00:00
|
|
|
ClipperLib::Paths input_subject, input_clip;
|
2014-11-15 22:44:03 +00:00
|
|
|
Slic3rMultiPoints_to_ClipperPaths(subject, &input_subject);
|
|
|
|
Slic3rMultiPoints_to_ClipperPaths(clip, &input_clip);
|
2013-07-16 19:04:14 +00:00
|
|
|
|
|
|
|
// perform safety offset
|
2013-08-26 15:58:37 +00:00
|
|
|
if (safety_offset_) {
|
2013-07-16 22:29:09 +00:00
|
|
|
if (clipType == ClipperLib::ctUnion) {
|
2014-05-08 13:00:49 +00:00
|
|
|
safety_offset(&input_subject);
|
2013-07-16 22:29:09 +00:00
|
|
|
} else {
|
2014-05-08 13:00:49 +00:00
|
|
|
safety_offset(&input_clip);
|
2013-07-16 22:29:09 +00:00
|
|
|
}
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// init Clipper
|
|
|
|
ClipperLib::Clipper clipper;
|
|
|
|
clipper.Clear();
|
|
|
|
|
|
|
|
// add polygons
|
2014-05-08 13:00:49 +00:00
|
|
|
clipper.AddPaths(input_subject, ClipperLib::ptSubject, true);
|
|
|
|
clipper.AddPaths(input_clip, ClipperLib::ptClip, true);
|
2013-07-16 19:04:14 +00:00
|
|
|
|
2013-07-16 23:21:30 +00:00
|
|
|
// perform operation
|
2014-11-15 22:44:03 +00:00
|
|
|
clipper.Execute(clipType, *retval, fillType, fillType);
|
2013-07-16 23:21:30 +00:00
|
|
|
}
|
|
|
|
|
2013-11-21 14:12:06 +00:00
|
|
|
void _clipper_do(const ClipperLib::ClipType clipType, const Slic3r::Polylines &subject,
|
2014-11-15 22:44:03 +00:00
|
|
|
const Slic3r::Polygons &clip, ClipperLib::PolyTree* retval, const ClipperLib::PolyFillType fillType,
|
2014-05-08 09:07:37 +00:00
|
|
|
const bool safety_offset_)
|
2013-11-21 13:15:38 +00:00
|
|
|
{
|
|
|
|
// read input
|
2014-05-08 09:07:37 +00:00
|
|
|
ClipperLib::Paths input_subject, input_clip;
|
2014-11-15 22:44:03 +00:00
|
|
|
Slic3rMultiPoints_to_ClipperPaths(subject, &input_subject);
|
|
|
|
Slic3rMultiPoints_to_ClipperPaths(clip, &input_clip);
|
2014-05-08 09:07:37 +00:00
|
|
|
|
|
|
|
// perform safety offset
|
|
|
|
if (safety_offset_) safety_offset(&input_clip);
|
2013-11-21 13:15:38 +00:00
|
|
|
|
|
|
|
// init Clipper
|
|
|
|
ClipperLib::Clipper clipper;
|
|
|
|
clipper.Clear();
|
|
|
|
|
|
|
|
// add polygons
|
2014-05-08 09:07:37 +00:00
|
|
|
clipper.AddPaths(input_subject, ClipperLib::ptSubject, false);
|
|
|
|
clipper.AddPaths(input_clip, ClipperLib::ptClip, true);
|
2013-11-21 13:15:38 +00:00
|
|
|
|
|
|
|
// perform operation
|
2014-11-15 22:44:03 +00:00
|
|
|
clipper.Execute(clipType, *retval, fillType, fillType);
|
2013-11-21 13:15:38 +00:00
|
|
|
}
|
|
|
|
|
2013-11-21 14:12:06 +00:00
|
|
|
void _clipper(ClipperLib::ClipType clipType, const Slic3r::Polygons &subject,
|
2014-11-15 22:44:03 +00:00
|
|
|
const Slic3r::Polygons &clip, Slic3r::Polygons* retval, bool safety_offset_)
|
2013-07-16 23:21:30 +00:00
|
|
|
{
|
|
|
|
// perform operation
|
2014-05-08 13:00:49 +00:00
|
|
|
ClipperLib::Paths output;
|
2014-11-15 22:44:03 +00:00
|
|
|
_clipper_do<ClipperLib::Paths>(clipType, subject, clip, &output, ClipperLib::pftNonZero, safety_offset_);
|
2013-07-16 23:21:30 +00:00
|
|
|
|
|
|
|
// convert into Polygons
|
2014-05-08 13:00:49 +00:00
|
|
|
ClipperPaths_to_Slic3rMultiPoints(output, retval);
|
2013-07-16 23:21:30 +00:00
|
|
|
}
|
|
|
|
|
2013-11-21 14:12:06 +00:00
|
|
|
void _clipper(ClipperLib::ClipType clipType, const Slic3r::Polygons &subject,
|
2014-11-15 22:44:03 +00:00
|
|
|
const Slic3r::Polygons &clip, Slic3r::ExPolygons* retval, bool safety_offset_)
|
2013-07-16 23:21:30 +00:00
|
|
|
{
|
2013-07-16 19:04:14 +00:00
|
|
|
// perform operation
|
2014-05-08 09:07:37 +00:00
|
|
|
ClipperLib::PolyTree polytree;
|
2014-11-15 22:44:03 +00:00
|
|
|
_clipper_do<ClipperLib::PolyTree>(clipType, subject, clip, &polytree, ClipperLib::pftNonZero, safety_offset_);
|
2013-07-16 19:04:14 +00:00
|
|
|
|
|
|
|
// convert into ExPolygons
|
2014-05-08 09:07:37 +00:00
|
|
|
PolyTreeToExPolygons(polytree, retval);
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|
|
|
|
|
2013-11-21 14:12:06 +00:00
|
|
|
void _clipper(ClipperLib::ClipType clipType, const Slic3r::Polylines &subject,
|
2014-11-15 22:44:03 +00:00
|
|
|
const Slic3r::Polygons &clip, Slic3r::Polylines* retval, bool safety_offset_)
|
2013-11-21 13:15:38 +00:00
|
|
|
{
|
|
|
|
// perform operation
|
|
|
|
ClipperLib::PolyTree polytree;
|
2014-11-15 22:44:03 +00:00
|
|
|
_clipper_do(clipType, subject, clip, &polytree, ClipperLib::pftNonZero, safety_offset_);
|
2013-11-21 13:15:38 +00:00
|
|
|
|
2014-05-08 09:07:37 +00:00
|
|
|
// convert into Polylines
|
2013-11-21 13:15:38 +00:00
|
|
|
ClipperLib::Paths output;
|
|
|
|
ClipperLib::PolyTreeToPaths(polytree, output);
|
|
|
|
ClipperPaths_to_Slic3rMultiPoints(output, retval);
|
2014-05-21 13:03:31 +00:00
|
|
|
}
|
|
|
|
|
2014-11-15 21:41:22 +00:00
|
|
|
void _clipper(ClipperLib::ClipType clipType, const Slic3r::Lines &subject,
|
2014-11-15 22:44:03 +00:00
|
|
|
const Slic3r::Polygons &clip, Slic3r::Lines* retval, bool safety_offset_)
|
2014-11-15 21:41:22 +00:00
|
|
|
{
|
|
|
|
// convert Lines to Polylines
|
|
|
|
Slic3r::Polylines polylines;
|
|
|
|
polylines.reserve(subject.size());
|
|
|
|
for (Slic3r::Lines::const_iterator line = subject.begin(); line != subject.end(); ++line)
|
|
|
|
polylines.push_back(*line);
|
|
|
|
|
|
|
|
// perform operation
|
2014-11-15 22:44:03 +00:00
|
|
|
_clipper(clipType, polylines, clip, &polylines, safety_offset_);
|
2014-11-15 21:41:22 +00:00
|
|
|
|
|
|
|
// convert Polylines to Lines
|
|
|
|
for (Slic3r::Polylines::const_iterator polyline = polylines.begin(); polyline != polylines.end(); ++polyline)
|
2014-11-15 22:44:03 +00:00
|
|
|
retval->push_back(*polyline);
|
2014-11-15 21:41:22 +00:00
|
|
|
}
|
|
|
|
|
2014-05-21 13:03:31 +00:00
|
|
|
void _clipper(ClipperLib::ClipType clipType, const Slic3r::Polygons &subject,
|
2014-11-15 22:44:03 +00:00
|
|
|
const Slic3r::Polygons &clip, Slic3r::Polylines* retval, bool safety_offset_)
|
2014-05-21 13:03:31 +00:00
|
|
|
{
|
|
|
|
// transform input polygons into polylines
|
|
|
|
Slic3r::Polylines polylines;
|
|
|
|
polylines.reserve(subject.size());
|
|
|
|
for (Slic3r::Polygons::const_iterator polygon = subject.begin(); polygon != subject.end(); ++polygon)
|
|
|
|
polylines.push_back(*polygon); // implicit call to split_at_first_point()
|
|
|
|
|
|
|
|
// perform clipping
|
|
|
|
_clipper(clipType, polylines, clip, retval, safety_offset_);
|
2014-05-08 09:07:37 +00:00
|
|
|
|
|
|
|
/* If the split_at_first_point() call above happens to split the polygon inside the clipping area
|
|
|
|
we would get two consecutive polylines instead of a single one, so we go through them in order
|
|
|
|
to recombine continuous polylines. */
|
2014-11-15 22:44:03 +00:00
|
|
|
for (size_t i = 0; i < retval->size(); ++i) {
|
|
|
|
for (size_t j = i+1; j < retval->size(); ++j) {
|
|
|
|
if ((*retval)[i].points.back().coincides_with((*retval)[j].points.front())) {
|
2014-05-08 09:07:37 +00:00
|
|
|
/* If last point of i coincides with first point of j,
|
|
|
|
append points of j to i and delete j */
|
2014-11-15 22:44:03 +00:00
|
|
|
(*retval)[i].points.insert((*retval)[i].points.end(), (*retval)[j].points.begin()+1, (*retval)[j].points.end());
|
|
|
|
retval->erase(retval->begin() + j);
|
2014-05-08 09:07:37 +00:00
|
|
|
--j;
|
2014-11-15 22:44:03 +00:00
|
|
|
} else if ((*retval)[i].points.front().coincides_with((*retval)[j].points.back())) {
|
2014-05-08 09:07:37 +00:00
|
|
|
/* If first point of i coincides with last point of j,
|
|
|
|
prepend points of j to i and delete j */
|
2014-11-15 22:44:03 +00:00
|
|
|
(*retval)[i].points.insert((*retval)[i].points.begin(), (*retval)[j].points.begin(), (*retval)[j].points.end()-1);
|
|
|
|
retval->erase(retval->begin() + j);
|
2014-05-08 09:07:37 +00:00
|
|
|
--j;
|
2014-11-15 22:44:03 +00:00
|
|
|
} else if ((*retval)[i].points.front().coincides_with((*retval)[j].points.front())) {
|
2014-05-08 09:07:37 +00:00
|
|
|
/* Since Clipper does not preserve orientation of polylines,
|
|
|
|
also check the case when first point of i coincides with first point of j. */
|
2014-11-15 22:44:03 +00:00
|
|
|
(*retval)[j].reverse();
|
|
|
|
(*retval)[i].points.insert((*retval)[i].points.begin(), (*retval)[j].points.begin(), (*retval)[j].points.end()-1);
|
|
|
|
retval->erase(retval->begin() + j);
|
2014-05-08 09:07:37 +00:00
|
|
|
--j;
|
2014-11-15 22:44:03 +00:00
|
|
|
} else if ((*retval)[i].points.back().coincides_with((*retval)[j].points.back())) {
|
2014-05-08 09:07:37 +00:00
|
|
|
/* Since Clipper does not preserve orientation of polylines,
|
|
|
|
also check the case when last point of i coincides with last point of j. */
|
2014-11-15 22:44:03 +00:00
|
|
|
(*retval)[j].reverse();
|
|
|
|
(*retval)[i].points.insert((*retval)[i].points.end(), (*retval)[j].points.begin()+1, (*retval)[j].points.end());
|
|
|
|
retval->erase(retval->begin() + j);
|
2014-05-08 09:07:37 +00:00
|
|
|
--j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-16 22:29:09 +00:00
|
|
|
}
|
2013-07-16 19:04:14 +00:00
|
|
|
|
2014-05-08 09:07:37 +00:00
|
|
|
template <class SubjectType, class ResultType>
|
2014-11-15 22:44:03 +00:00
|
|
|
void diff(const SubjectType &subject, const Slic3r::Polygons &clip, ResultType* retval, bool safety_offset_)
|
2013-11-21 13:15:38 +00:00
|
|
|
{
|
2014-05-08 09:07:37 +00:00
|
|
|
_clipper(ClipperLib::ctDifference, subject, clip, retval, safety_offset_);
|
2013-11-21 13:15:38 +00:00
|
|
|
}
|
2014-11-15 22:44:03 +00:00
|
|
|
template void diff<Slic3r::Polygons, Slic3r::ExPolygons>(const Slic3r::Polygons &subject, const Slic3r::Polygons &clip, Slic3r::ExPolygons* retval, bool safety_offset_);
|
|
|
|
template void diff<Slic3r::Polygons, Slic3r::Polygons>(const Slic3r::Polygons &subject, const Slic3r::Polygons &clip, Slic3r::Polygons* retval, bool safety_offset_);
|
|
|
|
template void diff<Slic3r::Polygons, Slic3r::Polylines>(const Slic3r::Polygons &subject, const Slic3r::Polygons &clip, Slic3r::Polylines* retval, bool safety_offset_);
|
|
|
|
template void diff<Slic3r::Polylines, Slic3r::Polylines>(const Slic3r::Polylines &subject, const Slic3r::Polygons &clip, Slic3r::Polylines* retval, bool safety_offset_);
|
|
|
|
template void diff<Slic3r::Lines, Slic3r::Lines>(const Slic3r::Lines &subject, const Slic3r::Polygons &clip, Slic3r::Lines* retval, bool safety_offset_);
|
2013-11-21 13:15:38 +00:00
|
|
|
|
2014-12-24 09:20:55 +00:00
|
|
|
template <class SubjectType, class ResultType>
|
|
|
|
void diff(const SubjectType &subject, const Slic3r::ExPolygons &clip, ResultType* retval, bool safety_offset_)
|
|
|
|
{
|
|
|
|
Slic3r::Polygons pp;
|
|
|
|
for (Slic3r::ExPolygons::const_iterator ex = clip.begin(); ex != clip.end(); ++ex) {
|
|
|
|
Slic3r::Polygons ppp = *ex;
|
|
|
|
pp.insert(pp.end(), ppp.begin(), ppp.end());
|
|
|
|
}
|
|
|
|
diff(subject, pp, retval, safety_offset_);
|
|
|
|
}
|
|
|
|
template void diff<Slic3r::Polygons, Slic3r::ExPolygons>(const Slic3r::Polygons &subject, const Slic3r::ExPolygons &clip, Slic3r::ExPolygons* retval, bool safety_offset_);
|
|
|
|
|
2015-07-23 13:53:02 +00:00
|
|
|
Slic3r::Polygons
|
|
|
|
diff(const Slic3r::Polygons &subject, const Slic3r::Polygons &clip, bool safety_offset_)
|
|
|
|
{
|
|
|
|
Slic3r::Polygons pp;
|
|
|
|
diff(subject, clip, &pp, safety_offset_);
|
|
|
|
return pp;
|
|
|
|
}
|
|
|
|
|
2015-10-26 22:23:03 +00:00
|
|
|
template <class SubjectType, class ClipType>
|
2015-07-23 13:53:02 +00:00
|
|
|
Slic3r::ExPolygons
|
2015-10-26 22:23:03 +00:00
|
|
|
diff_ex(const SubjectType &subject, const ClipType &clip, bool safety_offset_)
|
2015-07-23 13:53:02 +00:00
|
|
|
{
|
|
|
|
Slic3r::ExPolygons expp;
|
|
|
|
diff(subject, clip, &expp, safety_offset_);
|
|
|
|
return expp;
|
|
|
|
}
|
2015-10-26 22:23:03 +00:00
|
|
|
template Slic3r::ExPolygons diff_ex<Slic3r::Polygons, Slic3r::Polygons>(const Slic3r::Polygons &subject, const Slic3r::Polygons &clip, bool safety_offset_);
|
|
|
|
template Slic3r::ExPolygons diff_ex<Slic3r::Polygons, Slic3r::ExPolygons>(const Slic3r::Polygons &subject, const Slic3r::ExPolygons &clip, bool safety_offset_);
|
2015-07-23 13:53:02 +00:00
|
|
|
|
2014-05-08 09:07:37 +00:00
|
|
|
template <class SubjectType, class ResultType>
|
2014-11-15 22:44:03 +00:00
|
|
|
void intersection(const SubjectType &subject, const Slic3r::Polygons &clip, ResultType* retval, bool safety_offset_)
|
2013-07-16 22:29:09 +00:00
|
|
|
{
|
2013-08-26 15:58:37 +00:00
|
|
|
_clipper(ClipperLib::ctIntersection, subject, clip, retval, safety_offset_);
|
2013-07-16 22:29:09 +00:00
|
|
|
}
|
2014-11-15 22:44:03 +00:00
|
|
|
template void intersection<Slic3r::Polygons, Slic3r::ExPolygons>(const Slic3r::Polygons &subject, const Slic3r::Polygons &clip, Slic3r::ExPolygons* retval, bool safety_offset_);
|
|
|
|
template void intersection<Slic3r::Polygons, Slic3r::Polygons>(const Slic3r::Polygons &subject, const Slic3r::Polygons &clip, Slic3r::Polygons* retval, bool safety_offset_);
|
|
|
|
template void intersection<Slic3r::Polygons, Slic3r::Polylines>(const Slic3r::Polygons &subject, const Slic3r::Polygons &clip, Slic3r::Polylines* retval, bool safety_offset_);
|
|
|
|
template void intersection<Slic3r::Polylines, Slic3r::Polylines>(const Slic3r::Polylines &subject, const Slic3r::Polygons &clip, Slic3r::Polylines* retval, bool safety_offset_);
|
|
|
|
template void intersection<Slic3r::Lines, Slic3r::Lines>(const Slic3r::Lines &subject, const Slic3r::Polygons &clip, Slic3r::Lines* retval, bool safety_offset_);
|
2013-11-21 13:15:38 +00:00
|
|
|
|
2015-12-21 13:46:35 +00:00
|
|
|
template <class SubjectType>
|
|
|
|
SubjectType intersection(const SubjectType &subject, const Slic3r::Polygons &clip, bool safety_offset_)
|
2015-07-23 13:53:02 +00:00
|
|
|
{
|
2015-12-21 13:46:35 +00:00
|
|
|
SubjectType pp;
|
2015-07-23 13:53:02 +00:00
|
|
|
intersection(subject, clip, &pp, safety_offset_);
|
|
|
|
return pp;
|
|
|
|
}
|
|
|
|
|
2015-12-21 13:46:35 +00:00
|
|
|
template Slic3r::Polygons intersection<Slic3r::Polygons>(const Slic3r::Polygons &subject, const Slic3r::Polygons &clip, bool safety_offset_);
|
|
|
|
template Slic3r::Polylines intersection<Slic3r::Polylines>(const Slic3r::Polylines &subject, const Slic3r::Polygons &clip, bool safety_offset_);
|
|
|
|
template Slic3r::Lines intersection<Slic3r::Lines>(const Slic3r::Lines &subject, const Slic3r::Polygons &clip, bool safety_offset_);
|
2015-07-23 13:53:02 +00:00
|
|
|
|
2015-10-26 22:23:03 +00:00
|
|
|
Slic3r::ExPolygons
|
|
|
|
intersection_ex(const Slic3r::Polygons &subject, const Slic3r::Polygons &clip, bool safety_offset_)
|
|
|
|
{
|
|
|
|
Slic3r::ExPolygons expp;
|
|
|
|
intersection(subject, clip, &expp, safety_offset_);
|
|
|
|
return expp;
|
|
|
|
}
|
|
|
|
|
2014-11-09 14:27:34 +00:00
|
|
|
template <class SubjectType>
|
|
|
|
bool intersects(const SubjectType &subject, const Slic3r::Polygons &clip, bool safety_offset_)
|
|
|
|
{
|
|
|
|
SubjectType retval;
|
2014-11-15 22:44:03 +00:00
|
|
|
intersection(subject, clip, &retval, safety_offset_);
|
2014-11-09 14:27:34 +00:00
|
|
|
return !retval.empty();
|
|
|
|
}
|
|
|
|
template bool intersects<Slic3r::Polygons>(const Slic3r::Polygons &subject, const Slic3r::Polygons &clip, bool safety_offset_);
|
|
|
|
template bool intersects<Slic3r::Polylines>(const Slic3r::Polylines &subject, const Slic3r::Polygons &clip, bool safety_offset_);
|
2014-11-15 21:41:22 +00:00
|
|
|
template bool intersects<Slic3r::Lines>(const Slic3r::Lines &subject, const Slic3r::Polygons &clip, bool safety_offset_);
|
2014-11-09 14:27:34 +00:00
|
|
|
|
2014-11-15 22:44:03 +00:00
|
|
|
void xor_(const Slic3r::Polygons &subject, const Slic3r::Polygons &clip, Slic3r::ExPolygons* retval,
|
2013-08-26 15:58:37 +00:00
|
|
|
bool safety_offset_)
|
2013-07-16 22:29:09 +00:00
|
|
|
{
|
2013-08-26 15:58:37 +00:00
|
|
|
_clipper(ClipperLib::ctXor, subject, clip, retval, safety_offset_);
|
2013-07-16 22:29:09 +00:00
|
|
|
}
|
|
|
|
|
2013-08-17 10:24:04 +00:00
|
|
|
template <class T>
|
2014-11-15 22:44:03 +00:00
|
|
|
void union_(const Slic3r::Polygons &subject, T* retval, bool safety_offset_)
|
2013-07-16 22:29:09 +00:00
|
|
|
{
|
|
|
|
Slic3r::Polygons p;
|
2013-08-26 15:58:37 +00:00
|
|
|
_clipper(ClipperLib::ctUnion, subject, p, retval, safety_offset_);
|
2013-07-16 22:29:09 +00:00
|
|
|
}
|
2014-11-15 22:44:03 +00:00
|
|
|
template void union_<Slic3r::ExPolygons>(const Slic3r::Polygons &subject, Slic3r::ExPolygons* retval, bool safety_offset_);
|
|
|
|
template void union_<Slic3r::Polygons>(const Slic3r::Polygons &subject, Slic3r::Polygons* retval, bool safety_offset_);
|
2013-07-16 19:04:14 +00:00
|
|
|
|
2015-07-23 13:53:02 +00:00
|
|
|
Slic3r::Polygons
|
|
|
|
union_(const Slic3r::Polygons &subject, bool safety_offset)
|
|
|
|
{
|
|
|
|
Polygons pp;
|
|
|
|
union_(subject, &pp, safety_offset);
|
|
|
|
return pp;
|
|
|
|
}
|
|
|
|
|
|
|
|
Slic3r::ExPolygons
|
|
|
|
union_ex(const Slic3r::Polygons &subject, bool safety_offset)
|
|
|
|
{
|
|
|
|
ExPolygons expp;
|
|
|
|
union_(subject, &expp, safety_offset);
|
|
|
|
return expp;
|
|
|
|
}
|
|
|
|
|
2015-12-02 18:32:57 +00:00
|
|
|
Slic3r::ExPolygons
|
|
|
|
union_ex(const Slic3r::Surfaces &subject, bool safety_offset)
|
|
|
|
{
|
|
|
|
Polygons pp;
|
|
|
|
for (Slic3r::Surfaces::const_iterator s = subject.begin(); s != subject.end(); ++s) {
|
|
|
|
Polygons spp = *s;
|
|
|
|
pp.insert(pp.end(), spp.begin(), spp.end());
|
|
|
|
}
|
|
|
|
return union_ex(pp, safety_offset);
|
|
|
|
}
|
|
|
|
|
2014-11-15 22:44:03 +00:00
|
|
|
void union_(const Slic3r::Polygons &subject1, const Slic3r::Polygons &subject2, Slic3r::Polygons* retval, bool safety_offset)
|
2014-11-09 14:27:34 +00:00
|
|
|
{
|
|
|
|
Polygons pp = subject1;
|
|
|
|
pp.insert(pp.end(), subject2.begin(), subject2.end());
|
|
|
|
union_(pp, retval, safety_offset);
|
|
|
|
}
|
|
|
|
|
2014-11-15 22:44:03 +00:00
|
|
|
void union_pt(const Slic3r::Polygons &subject, ClipperLib::PolyTree* retval, bool safety_offset_)
|
2013-08-26 16:37:19 +00:00
|
|
|
{
|
|
|
|
Slic3r::Polygons clip;
|
|
|
|
_clipper_do<ClipperLib::PolyTree>(ClipperLib::ctUnion, subject, clip, retval, ClipperLib::pftEvenOdd, safety_offset_);
|
|
|
|
}
|
|
|
|
|
2014-11-15 22:44:03 +00:00
|
|
|
void union_pt_chained(const Slic3r::Polygons &subject, Slic3r::Polygons* retval, bool safety_offset_)
|
2013-11-23 22:21:59 +00:00
|
|
|
{
|
|
|
|
ClipperLib::PolyTree pt;
|
2014-11-15 22:44:03 +00:00
|
|
|
union_pt(subject, &pt, safety_offset_);
|
2016-04-11 15:05:58 +00:00
|
|
|
if (&subject == retval)
|
|
|
|
// It is safe to use the same variable for input and output, because this function makes
|
|
|
|
// a temporary copy of the results.
|
|
|
|
retval->clear();
|
2013-11-23 22:21:59 +00:00
|
|
|
traverse_pt(pt.Childs, retval);
|
|
|
|
}
|
|
|
|
|
2014-11-15 22:44:03 +00:00
|
|
|
static void traverse_pt(ClipperLib::PolyNodes &nodes, Slic3r::Polygons* retval)
|
2013-11-23 22:21:59 +00:00
|
|
|
{
|
|
|
|
/* use a nearest neighbor search to order these children
|
|
|
|
TODO: supply start_near to chained_path() too? */
|
|
|
|
|
|
|
|
// collect ordering points
|
|
|
|
Points ordering_points;
|
|
|
|
ordering_points.reserve(nodes.size());
|
|
|
|
for (ClipperLib::PolyNodes::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
|
|
|
|
Point p((*it)->Contour.front().X, (*it)->Contour.front().Y);
|
|
|
|
ordering_points.push_back(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
// perform the ordering
|
|
|
|
ClipperLib::PolyNodes ordered_nodes;
|
|
|
|
Slic3r::Geometry::chained_path_items(ordering_points, nodes, ordered_nodes);
|
|
|
|
|
|
|
|
// push results recursively
|
|
|
|
for (ClipperLib::PolyNodes::iterator it = ordered_nodes.begin(); it != ordered_nodes.end(); ++it) {
|
|
|
|
// traverse the next depth
|
|
|
|
traverse_pt((*it)->Childs, retval);
|
|
|
|
|
|
|
|
Polygon p;
|
2014-11-15 22:44:03 +00:00
|
|
|
ClipperPath_to_Slic3rMultiPoint((*it)->Contour, &p);
|
|
|
|
retval->push_back(p);
|
|
|
|
if ((*it)->IsHole()) retval->back().reverse(); // ccw
|
2013-11-23 22:21:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-15 22:44:03 +00:00
|
|
|
void simplify_polygons(const Slic3r::Polygons &subject, Slic3r::Polygons* retval, bool preserve_collinear)
|
2013-08-08 00:10:34 +00:00
|
|
|
{
|
|
|
|
// convert into Clipper polygons
|
2014-05-08 13:00:49 +00:00
|
|
|
ClipperLib::Paths input_subject, output;
|
2014-11-15 22:44:03 +00:00
|
|
|
Slic3rMultiPoints_to_ClipperPaths(subject, &input_subject);
|
2014-05-01 08:37:38 +00:00
|
|
|
|
|
|
|
if (preserve_collinear) {
|
|
|
|
ClipperLib::Clipper c;
|
|
|
|
c.PreserveCollinear(true);
|
|
|
|
c.StrictlySimple(true);
|
2014-05-08 13:00:49 +00:00
|
|
|
c.AddPaths(input_subject, ClipperLib::ptSubject, true);
|
|
|
|
c.Execute(ClipperLib::ctUnion, output, ClipperLib::pftNonZero, ClipperLib::pftNonZero);
|
2014-05-01 08:37:38 +00:00
|
|
|
} else {
|
2014-05-08 13:00:49 +00:00
|
|
|
ClipperLib::SimplifyPolygons(input_subject, output, ClipperLib::pftNonZero);
|
2014-05-01 08:37:38 +00:00
|
|
|
}
|
|
|
|
|
2013-08-08 00:10:34 +00:00
|
|
|
// convert into Slic3r polygons
|
2014-05-08 13:00:49 +00:00
|
|
|
ClipperPaths_to_Slic3rMultiPoints(output, retval);
|
2013-08-08 00:10:34 +00:00
|
|
|
}
|
|
|
|
|
2014-11-15 22:44:03 +00:00
|
|
|
void simplify_polygons(const Slic3r::Polygons &subject, Slic3r::ExPolygons* retval, bool preserve_collinear)
|
2014-05-01 10:07:11 +00:00
|
|
|
{
|
|
|
|
if (!preserve_collinear) {
|
|
|
|
Polygons polygons;
|
2014-11-15 22:44:03 +00:00
|
|
|
simplify_polygons(subject, &polygons, preserve_collinear);
|
2014-05-01 10:07:11 +00:00
|
|
|
union_(polygons, retval);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert into Clipper polygons
|
|
|
|
ClipperLib::Paths input_subject;
|
2014-11-15 22:44:03 +00:00
|
|
|
Slic3rMultiPoints_to_ClipperPaths(subject, &input_subject);
|
2014-05-01 10:07:11 +00:00
|
|
|
|
|
|
|
ClipperLib::PolyTree polytree;
|
|
|
|
|
|
|
|
ClipperLib::Clipper c;
|
|
|
|
c.PreserveCollinear(true);
|
|
|
|
c.StrictlySimple(true);
|
|
|
|
c.AddPaths(input_subject, ClipperLib::ptSubject, true);
|
|
|
|
c.Execute(ClipperLib::ctUnion, polytree, ClipperLib::pftNonZero, ClipperLib::pftNonZero);
|
|
|
|
|
|
|
|
// convert into ExPolygons
|
|
|
|
PolyTreeToExPolygons(polytree, retval);
|
|
|
|
}
|
|
|
|
|
2014-05-08 09:07:37 +00:00
|
|
|
void safety_offset(ClipperLib::Paths* paths)
|
2013-08-26 15:58:37 +00:00
|
|
|
{
|
|
|
|
// scale input
|
2014-05-08 09:07:37 +00:00
|
|
|
scaleClipperPolygons(*paths, CLIPPER_OFFSET_SCALE);
|
2013-08-26 15:58:37 +00:00
|
|
|
|
|
|
|
// perform offset (delta = scale 1e-05)
|
2013-12-24 11:40:46 +00:00
|
|
|
ClipperLib::ClipperOffset co;
|
|
|
|
co.MiterLimit = 2;
|
2014-05-08 09:07:37 +00:00
|
|
|
co.AddPaths(*paths, ClipperLib::jtMiter, ClipperLib::etClosedPolygon);
|
|
|
|
co.Execute(*paths, 10.0 * CLIPPER_OFFSET_SCALE);
|
2013-08-26 15:58:37 +00:00
|
|
|
|
|
|
|
// unscale output
|
2014-05-08 09:07:37 +00:00
|
|
|
scaleClipperPolygons(*paths, 1.0/CLIPPER_OFFSET_SCALE);
|
2013-08-26 15:58:37 +00:00
|
|
|
}
|
|
|
|
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|