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
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
|
|
|
//-----------------------------------------------------------
|
|
|
|
// legacy code from Clipper documentation
|
|
|
|
void AddOuterPolyNodeToExPolygons(ClipperLib::PolyNode& polynode, Slic3r::ExPolygons& expolygons)
|
|
|
|
{
|
|
|
|
size_t cnt = expolygons.size();
|
|
|
|
expolygons.resize(cnt + 1);
|
2013-11-21 13:15:38 +00:00
|
|
|
ClipperPath_to_Slic3rMultiPoint(polynode.Contour, expolygons[cnt].contour);
|
2013-07-16 19:04:14 +00:00
|
|
|
expolygons[cnt].holes.resize(polynode.ChildCount());
|
|
|
|
for (int i = 0; i < polynode.ChildCount(); ++i)
|
|
|
|
{
|
2013-11-21 13:15:38 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PolyTreeToExPolygons(ClipperLib::PolyTree& polytree, Slic3r::ExPolygons& expolygons)
|
|
|
|
{
|
|
|
|
expolygons.clear();
|
|
|
|
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
|
2013-11-21 13:15:38 +00:00
|
|
|
ClipperPath_to_Slic3rMultiPoint(const ClipperLib::Path &input, T &output)
|
2013-07-16 19:04:14 +00:00
|
|
|
{
|
2013-07-16 22:48:29 +00:00
|
|
|
output.points.clear();
|
2013-11-20 14:59:19 +00:00
|
|
|
for (ClipperLib::Path::const_iterator pit = input.begin(); pit != input.end(); ++pit) {
|
2013-07-16 22:48:29 +00:00
|
|
|
output.points.push_back(Slic3r::Point( (*pit).X, (*pit).Y ));
|
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
|
2013-11-21 13:15:38 +00:00
|
|
|
ClipperPaths_to_Slic3rMultiPoints(const ClipperLib::Paths &input, T &output)
|
2013-07-16 19:04:14 +00:00
|
|
|
{
|
|
|
|
output.clear();
|
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;
|
|
|
|
ClipperPath_to_Slic3rMultiPoint(*it, p);
|
2013-07-16 19:04:14 +00:00
|
|
|
output.push_back(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-11-21 13:15:38 +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);
|
2013-07-16 19:04:14 +00:00
|
|
|
ClipperLib::PolyTree* polytree = new ClipperLib::PolyTree();
|
|
|
|
clipper.Execute(ClipperLib::ctUnion, *polytree, ClipperLib::pftEvenOdd, ClipperLib::pftEvenOdd); // offset results work with both EvenOdd and NonZero
|
|
|
|
|
|
|
|
// write to ExPolygons object
|
|
|
|
output.clear();
|
|
|
|
PolyTreeToExPolygons(*polytree, output);
|
|
|
|
|
|
|
|
delete polytree;
|
|
|
|
}
|
|
|
|
|
2013-07-16 22:48:29 +00:00
|
|
|
void
|
2013-11-21 13:15:38 +00:00
|
|
|
Slic3rMultiPoint_to_ClipperPath(const Slic3r::MultiPoint &input, ClipperLib::Path &output)
|
2013-07-16 22:48:29 +00:00
|
|
|
{
|
|
|
|
output.clear();
|
2013-08-26 16:37:19 +00:00
|
|
|
for (Slic3r::Points::const_iterator pit = input.points.begin(); pit != input.points.end(); ++pit) {
|
2013-07-16 22:48:29 +00:00
|
|
|
output.push_back(ClipperLib::IntPoint( (*pit).x, (*pit).y ));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-20 10:35:58 +00:00
|
|
|
template <class T>
|
2013-07-16 22:48:29 +00:00
|
|
|
void
|
2013-11-21 13:15:38 +00:00
|
|
|
Slic3rMultiPoints_to_ClipperPaths(const T &input, ClipperLib::Paths &output)
|
2013-07-16 22:48:29 +00:00
|
|
|
{
|
|
|
|
output.clear();
|
2013-11-20 10:35:58 +00:00
|
|
|
for (typename T::const_iterator it = input.begin(); it != input.end(); ++it) {
|
2013-11-20 14:59:19 +00:00
|
|
|
ClipperLib::Path p;
|
2013-11-21 13:15:38 +00:00
|
|
|
Slic3rMultiPoint_to_ClipperPath(*it, p);
|
2013-07-16 22:48:29 +00:00
|
|
|
output.push_back(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2013-07-16 19:04:14 +00:00
|
|
|
(*pit).X *= scale;
|
|
|
|
(*pit).Y *= scale;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-11-21 14:12:06 +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;
|
|
|
|
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);
|
|
|
|
co.Execute(retval, (delta*scale));
|
2013-07-16 19:04:14 +00:00
|
|
|
|
|
|
|
// unscale output
|
2013-07-16 22:48:29 +00:00
|
|
|
scaleClipperPolygons(retval, 1/scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-11-21 14:12:06 +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;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-11-20 10:35:58 +00:00
|
|
|
void
|
2013-11-21 14:12:06 +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;
|
|
|
|
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);
|
|
|
|
co.Execute(retval, (delta*scale));
|
2013-11-20 10:35:58 +00:00
|
|
|
|
|
|
|
// unscale output
|
|
|
|
scaleClipperPolygons(retval, 1/scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-11-21 14:12:06 +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
|
2013-11-20 14:59:19 +00:00
|
|
|
ClipperLib::Paths* output = new ClipperLib::Paths();
|
2013-11-20 10:35:58 +00:00
|
|
|
offset(polylines, *output, delta, scale, joinType, miterLimit);
|
|
|
|
|
|
|
|
// convert into ExPolygons
|
2013-11-21 13:15:38 +00:00
|
|
|
ClipperPaths_to_Slic3rMultiPoints(*output, retval);
|
2013-11-20 10:35:58 +00:00
|
|
|
delete output;
|
|
|
|
}
|
|
|
|
|
2013-11-23 17:29:25 +00:00
|
|
|
void
|
|
|
|
offset(const Slic3r::Surface &surface, Slic3r::Surfaces &retval, const float delta,
|
|
|
|
double scale, ClipperLib::JoinType joinType, double miterLimit)
|
|
|
|
{
|
|
|
|
// perform offset
|
|
|
|
Slic3r::ExPolygons expp;
|
|
|
|
offset_ex(surface.expolygon, expp, delta, scale, joinType, miterLimit);
|
|
|
|
|
|
|
|
// clone the input surface for each expolygon we got
|
|
|
|
retval.clear();
|
|
|
|
retval.reserve(expp.size());
|
|
|
|
for (ExPolygons::iterator it = expp.begin(); it != expp.end(); ++it) {
|
|
|
|
Surface s = surface; // clone
|
|
|
|
s.expolygon = *it;
|
|
|
|
retval.push_back(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-16 22:48:29 +00:00
|
|
|
void
|
2013-11-21 14:12:06 +00:00
|
|
|
offset_ex(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
|
2013-11-20 14:59:19 +00:00
|
|
|
ClipperLib::Paths* output = new ClipperLib::Paths();
|
2013-07-16 22:48:29 +00:00
|
|
|
offset(polygons, *output, delta, scale, joinType, miterLimit);
|
2013-07-16 19:04:14 +00:00
|
|
|
|
|
|
|
// convert into ExPolygons
|
2013-11-21 13:15:38 +00:00
|
|
|
ClipperPaths_to_Slic3rExPolygons(*output, retval);
|
2013-07-16 19:04:14 +00:00
|
|
|
delete output;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-11-21 14:12:06 +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
|
|
|
{
|
|
|
|
// read input
|
2013-12-24 11:40:46 +00:00
|
|
|
ClipperLib::Paths input;
|
|
|
|
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));
|
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);
|
|
|
|
co.Execute(retval, (delta2*scale));
|
2013-07-16 19:04:14 +00:00
|
|
|
|
|
|
|
// unscale output
|
2013-07-16 22:48:29 +00:00
|
|
|
scaleClipperPolygons(retval, 1/scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-11-21 14:12:06 +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
|
2013-11-20 14:59:19 +00:00
|
|
|
ClipperLib::Paths* output = new ClipperLib::Paths();
|
2013-07-16 22:48:29 +00:00
|
|
|
offset2(polygons, *output, delta1, delta2, scale, joinType, miterLimit);
|
|
|
|
|
|
|
|
// convert into ExPolygons
|
2013-11-21 13:15:38 +00:00
|
|
|
ClipperPaths_to_Slic3rMultiPoints(*output, retval);
|
2013-07-16 22:48:29 +00:00
|
|
|
delete output;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-11-21 14:12:06 +00:00
|
|
|
offset2_ex(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
|
2013-11-20 14:59:19 +00:00
|
|
|
ClipperLib::Paths* output = new ClipperLib::Paths();
|
2013-07-16 22:48:29 +00:00
|
|
|
offset2(polygons, *output, delta1, delta2, scale, joinType, miterLimit);
|
2013-07-16 19:04:14 +00:00
|
|
|
|
|
|
|
// convert into ExPolygons
|
2013-11-21 13:15:38 +00:00
|
|
|
ClipperPaths_to_Slic3rExPolygons(*output, retval);
|
2013-07-16 22:48:29 +00:00
|
|
|
delete output;
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
const Slic3r::Polygons &clip, T &retval, const ClipperLib::PolyFillType fillType, const bool safety_offset_)
|
2013-07-16 19:04:14 +00:00
|
|
|
{
|
|
|
|
// read input
|
2013-11-20 14:59:19 +00:00
|
|
|
ClipperLib::Paths* input_subject = new ClipperLib::Paths();
|
|
|
|
ClipperLib::Paths* input_clip = new ClipperLib::Paths();
|
2013-11-21 13:15:38 +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) {
|
2013-08-26 15:58:37 +00:00
|
|
|
safety_offset(input_subject);
|
2013-07-16 22:29:09 +00:00
|
|
|
} else {
|
2013-08-26 15:58:37 +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
|
2013-11-20 14:59:19 +00:00
|
|
|
clipper.AddPaths(*input_subject, ClipperLib::ptSubject, true);
|
2013-07-16 19:04:14 +00:00
|
|
|
delete input_subject;
|
2013-11-20 14:59:19 +00:00
|
|
|
clipper.AddPaths(*input_clip, ClipperLib::ptClip, true);
|
2013-07-16 19:04:14 +00:00
|
|
|
delete input_clip;
|
|
|
|
|
2013-07-16 23:21:30 +00:00
|
|
|
// perform operation
|
2013-08-26 16:37:19 +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,
|
|
|
|
const Slic3r::Polygons &clip, ClipperLib::PolyTree &retval, const ClipperLib::PolyFillType fillType)
|
2013-11-21 13:15:38 +00:00
|
|
|
{
|
|
|
|
// read input
|
|
|
|
ClipperLib::Paths* input_subject = new ClipperLib::Paths();
|
|
|
|
ClipperLib::Paths* input_clip = new ClipperLib::Paths();
|
|
|
|
Slic3rMultiPoints_to_ClipperPaths(subject, *input_subject);
|
|
|
|
Slic3rMultiPoints_to_ClipperPaths(clip, *input_clip);
|
|
|
|
|
|
|
|
// init Clipper
|
|
|
|
ClipperLib::Clipper clipper;
|
|
|
|
clipper.Clear();
|
|
|
|
|
|
|
|
// add polygons
|
|
|
|
clipper.AddPaths(*input_subject, ClipperLib::ptSubject, false);
|
|
|
|
delete input_subject;
|
|
|
|
clipper.AddPaths(*input_clip, ClipperLib::ptClip, true);
|
|
|
|
delete input_clip;
|
|
|
|
|
|
|
|
// perform operation
|
|
|
|
clipper.Execute(clipType, retval, fillType, fillType);
|
|
|
|
}
|
|
|
|
|
2013-11-21 14:12:06 +00:00
|
|
|
void _clipper(ClipperLib::ClipType clipType, const Slic3r::Polygons &subject,
|
|
|
|
const Slic3r::Polygons &clip, Slic3r::Polygons &retval, bool safety_offset_)
|
2013-07-16 23:21:30 +00:00
|
|
|
{
|
|
|
|
// perform operation
|
2013-11-20 14:59:19 +00:00
|
|
|
ClipperLib::Paths* output = new ClipperLib::Paths();
|
|
|
|
_clipper_do<ClipperLib::Paths>(clipType, subject, clip, *output, ClipperLib::pftNonZero, safety_offset_);
|
2013-07-16 23:21:30 +00:00
|
|
|
|
|
|
|
// convert into Polygons
|
2013-11-21 13:15:38 +00:00
|
|
|
ClipperPaths_to_Slic3rMultiPoints(*output, retval);
|
2013-07-16 23:21:30 +00:00
|
|
|
delete output;
|
|
|
|
}
|
|
|
|
|
2013-11-21 14:12:06 +00:00
|
|
|
void _clipper(ClipperLib::ClipType clipType, const Slic3r::Polygons &subject,
|
|
|
|
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
|
|
|
|
ClipperLib::PolyTree* polytree = new ClipperLib::PolyTree();
|
2013-08-26 16:37:19 +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
|
|
|
|
PolyTreeToExPolygons(*polytree, retval);
|
|
|
|
delete polytree;
|
|
|
|
}
|
|
|
|
|
2013-11-21 14:12:06 +00:00
|
|
|
void _clipper(ClipperLib::ClipType clipType, const Slic3r::Polylines &subject,
|
|
|
|
const Slic3r::Polygons &clip, Slic3r::Polylines &retval)
|
2013-11-21 13:15:38 +00:00
|
|
|
{
|
|
|
|
// perform operation
|
|
|
|
ClipperLib::PolyTree polytree;
|
|
|
|
_clipper_do(clipType, subject, clip, polytree, ClipperLib::pftNonZero);
|
|
|
|
|
|
|
|
// convert into Polygons
|
|
|
|
ClipperLib::Paths output;
|
|
|
|
ClipperLib::PolyTreeToPaths(polytree, output);
|
|
|
|
ClipperPaths_to_Slic3rMultiPoints(output, retval);
|
|
|
|
}
|
|
|
|
|
2013-07-16 23:21:30 +00:00
|
|
|
template <class T>
|
2013-11-21 14:12:06 +00:00
|
|
|
void diff(const Slic3r::Polygons &subject, const Slic3r::Polygons &clip, T &retval, bool safety_offset_)
|
2013-07-16 22:29:09 +00:00
|
|
|
{
|
2013-08-26 15:58:37 +00:00
|
|
|
_clipper(ClipperLib::ctDifference, subject, clip, retval, safety_offset_);
|
2013-07-16 22:29:09 +00:00
|
|
|
}
|
2013-11-21 14:12:06 +00:00
|
|
|
template void diff<Slic3r::ExPolygons>(const Slic3r::Polygons &subject, const Slic3r::Polygons &clip, Slic3r::ExPolygons &retval, bool safety_offset_);
|
|
|
|
template void diff<Slic3r::Polygons>(const Slic3r::Polygons &subject, const Slic3r::Polygons &clip, Slic3r::Polygons &retval, bool safety_offset_);
|
2013-07-16 19:04:14 +00:00
|
|
|
|
2013-11-21 14:12:06 +00:00
|
|
|
void diff(const Slic3r::Polylines &subject, const Slic3r::Polygons &clip, Slic3r::Polylines &retval)
|
2013-11-21 13:15:38 +00:00
|
|
|
{
|
|
|
|
_clipper(ClipperLib::ctDifference, subject, clip, retval);
|
|
|
|
}
|
|
|
|
|
2013-07-16 23:21:30 +00:00
|
|
|
template <class T>
|
2013-11-21 14:12:06 +00:00
|
|
|
void intersection(const Slic3r::Polygons &subject, const Slic3r::Polygons &clip, T &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
|
|
|
}
|
2013-11-21 14:12:06 +00:00
|
|
|
template void intersection<Slic3r::ExPolygons>(const Slic3r::Polygons &subject, const Slic3r::Polygons &clip, Slic3r::ExPolygons &retval, bool safety_offset_);
|
|
|
|
template void intersection<Slic3r::Polygons>(const Slic3r::Polygons &subject, const Slic3r::Polygons &clip, Slic3r::Polygons &retval, bool safety_offset_);
|
2013-07-16 22:29:09 +00:00
|
|
|
|
2013-11-21 14:12:06 +00:00
|
|
|
void intersection(const Slic3r::Polylines &subject, const Slic3r::Polygons &clip, Slic3r::Polylines &retval)
|
2013-11-21 13:15:38 +00:00
|
|
|
{
|
|
|
|
_clipper(ClipperLib::ctIntersection, subject, clip, retval);
|
|
|
|
}
|
|
|
|
|
2013-11-21 14:12:06 +00:00
|
|
|
void xor_ex(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>
|
2013-11-21 14:12:06 +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
|
|
|
}
|
2013-11-21 14:12:06 +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
|
|
|
|
2013-11-21 14:12:06 +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_);
|
|
|
|
}
|
|
|
|
|
2013-11-23 22:21:59 +00:00
|
|
|
void union_pt_chained(const Slic3r::Polygons &subject, Slic3r::Polygons &retval, bool safety_offset_)
|
|
|
|
{
|
|
|
|
ClipperLib::PolyTree pt;
|
|
|
|
union_pt(subject, pt, safety_offset_);
|
|
|
|
traverse_pt(pt.Childs, retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void traverse_pt(ClipperLib::PolyNodes &nodes, Slic3r::Polygons &retval)
|
|
|
|
{
|
|
|
|
/* 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;
|
|
|
|
ClipperPath_to_Slic3rMultiPoint((*it)->Contour, p);
|
|
|
|
retval.push_back(p);
|
|
|
|
if ((*it)->IsHole()) retval.back().reverse(); // ccw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-01 08:37:38 +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
|
2013-11-20 14:59:19 +00:00
|
|
|
ClipperLib::Paths* input_subject = new ClipperLib::Paths();
|
2013-11-21 13:15:38 +00:00
|
|
|
Slic3rMultiPoints_to_ClipperPaths(subject, *input_subject);
|
2013-08-08 00:10:34 +00:00
|
|
|
|
2013-11-20 14:59:19 +00:00
|
|
|
ClipperLib::Paths* output = new ClipperLib::Paths();
|
2014-05-01 08:37:38 +00:00
|
|
|
|
|
|
|
if (preserve_collinear) {
|
|
|
|
ClipperLib::Clipper c;
|
|
|
|
c.PreserveCollinear(true);
|
|
|
|
c.StrictlySimple(true);
|
|
|
|
c.AddPaths(*input_subject, ClipperLib::ptSubject, true);
|
|
|
|
c.Execute(ClipperLib::ctUnion, *output, ClipperLib::pftNonZero, ClipperLib::pftNonZero);
|
|
|
|
} else {
|
|
|
|
ClipperLib::SimplifyPolygons(*input_subject, *output, ClipperLib::pftNonZero);
|
|
|
|
}
|
|
|
|
|
2013-08-08 00:10:34 +00:00
|
|
|
delete input_subject;
|
|
|
|
|
|
|
|
// convert into Slic3r polygons
|
2013-11-21 13:15:38 +00:00
|
|
|
ClipperPaths_to_Slic3rMultiPoints(*output, retval);
|
2013-08-08 00:10:34 +00:00
|
|
|
delete output;
|
|
|
|
}
|
|
|
|
|
2013-11-20 14:59:19 +00:00
|
|
|
void safety_offset(ClipperLib::Paths* &subject)
|
2013-08-26 15:58:37 +00:00
|
|
|
{
|
|
|
|
// scale input
|
|
|
|
scaleClipperPolygons(*subject, CLIPPER_OFFSET_SCALE);
|
|
|
|
|
|
|
|
// perform offset (delta = scale 1e-05)
|
2013-11-20 14:59:19 +00:00
|
|
|
ClipperLib::Paths* retval = new ClipperLib::Paths();
|
2013-12-24 11:40:46 +00:00
|
|
|
ClipperLib::ClipperOffset co;
|
|
|
|
co.MiterLimit = 2;
|
|
|
|
co.AddPaths(*subject, ClipperLib::jtMiter, ClipperLib::etClosedPolygon);
|
|
|
|
co.Execute(*retval, 10.0 * CLIPPER_OFFSET_SCALE);
|
2013-08-26 15:58:37 +00:00
|
|
|
|
|
|
|
// unscale output
|
|
|
|
scaleClipperPolygons(*retval, 1.0/CLIPPER_OFFSET_SCALE);
|
|
|
|
|
|
|
|
// delete original data and switch pointer
|
|
|
|
delete subject;
|
|
|
|
subject = retval;
|
|
|
|
}
|
|
|
|
|
2013-08-26 16:37:19 +00:00
|
|
|
///////////////////////
|
|
|
|
|
2013-09-13 12:48:40 +00:00
|
|
|
#ifdef SLIC3RXS
|
2013-08-26 16:37:19 +00:00
|
|
|
SV*
|
|
|
|
polynode_children_2_perl(const ClipperLib::PolyNode& node)
|
|
|
|
{
|
|
|
|
AV* av = newAV();
|
|
|
|
const unsigned int len = node.ChildCount();
|
|
|
|
av_extend(av, len-1);
|
|
|
|
for (int i = 0; i < len; ++i) {
|
|
|
|
av_store(av, i, polynode2perl(*node.Childs[i]));
|
|
|
|
}
|
|
|
|
return (SV*)newRV_noinc((SV*)av);
|
|
|
|
}
|
|
|
|
|
|
|
|
SV*
|
|
|
|
polynode2perl(const ClipperLib::PolyNode& node)
|
|
|
|
{
|
|
|
|
HV* hv = newHV();
|
|
|
|
Slic3r::Polygon p;
|
2013-11-21 13:15:38 +00:00
|
|
|
ClipperPath_to_Slic3rMultiPoint(node.Contour, p);
|
2013-08-26 16:37:19 +00:00
|
|
|
if (node.IsHole()) {
|
2013-09-02 18:22:20 +00:00
|
|
|
(void)hv_stores( hv, "hole", p.to_SV_clone_ref() );
|
2013-08-26 16:37:19 +00:00
|
|
|
} else {
|
2013-09-02 18:22:20 +00:00
|
|
|
(void)hv_stores( hv, "outer", p.to_SV_clone_ref() );
|
2013-08-26 16:37:19 +00:00
|
|
|
}
|
|
|
|
(void)hv_stores( hv, "children", polynode_children_2_perl(node) );
|
|
|
|
return (SV*)newRV_noinc((SV*)hv);
|
|
|
|
}
|
2013-09-13 12:48:40 +00:00
|
|
|
#endif
|
2013-08-26 16:37:19 +00:00
|
|
|
|
2013-07-16 19:04:14 +00:00
|
|
|
}
|