From bd3fb21cfd54009be6b8404bed8605cdce573654 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Tue, 16 Jul 2013 20:36:27 +0200 Subject: [PATCH] Make offset_ex(), offset2_ex() and diff_ex() available to C++ code --- xs/src/ClipperUtils.hpp | 89 +++++++++++++++++++++++++++++++++++++++++ xs/xsp/Clipper.xsp | 72 ++------------------------------- 2 files changed, 92 insertions(+), 69 deletions(-) diff --git a/xs/src/ClipperUtils.hpp b/xs/src/ClipperUtils.hpp index fcfc64e42..ade09433f 100644 --- a/xs/src/ClipperUtils.hpp +++ b/xs/src/ClipperUtils.hpp @@ -96,6 +96,95 @@ scaleClipperPolygons(ClipperLib::Polygons &polygons, const double scale) } } +void +offset_ex(Slic3r::Polygons &polygons, Slic3r::ExPolygons &retval, const float delta, + double scale = 100000, ClipperLib::JoinType joinType = ClipperLib::jtMiter, + double miterLimit = 3) +{ + // read input + ClipperLib::Polygons* input = new ClipperLib::Polygons(); + Slic3rPolygons_to_ClipperPolygons(polygons, *input); + + // scale input + scaleClipperPolygons(*input, scale); + + // perform offset + ClipperLib::Polygons* output = new ClipperLib::Polygons(); + ClipperLib::OffsetPolygons(*input, *output, (delta*scale), joinType, miterLimit); + delete input; + + // unscale output + scaleClipperPolygons(*output, 1/scale); + + // convert into ExPolygons + ClipperPolygons_to_Slic3rExPolygons(*output, retval); + delete output; +} + +void +offset2_ex(Slic3r::Polygons &polygons, Slic3r::ExPolygons &retval, const float delta1, + const float delta2, double scale = 100000, ClipperLib::JoinType joinType = ClipperLib::jtMiter, + double miterLimit = 3) +{ + // read input + ClipperLib::Polygons* input = new ClipperLib::Polygons(); + Slic3rPolygons_to_ClipperPolygons(polygons, *input); + + // scale input + scaleClipperPolygons(*input, scale); + + // perform first offset + ClipperLib::Polygons* output1 = new ClipperLib::Polygons(); + ClipperLib::OffsetPolygons(*input, *output1, (delta1*scale), joinType, miterLimit); + delete input; + + // perform second offset + ClipperLib::Polygons* output2 = new ClipperLib::Polygons(); + ClipperLib::OffsetPolygons(*output1, *output2, (delta2*scale), joinType, miterLimit); + delete output1; + + // unscale output + scaleClipperPolygons(*output2, 1/scale); + + // convert into ExPolygons + ClipperPolygons_to_Slic3rExPolygons(*output2, retval); + delete output2; +} + +void +diff_ex(Slic3r::Polygons &subject, Slic3r::Polygons &clip, Slic3r::ExPolygons &retval, bool safety_offset) +{ + // read input + ClipperLib::Polygons* input_subject = new ClipperLib::Polygons(); + ClipperLib::Polygons* input_clip = new ClipperLib::Polygons(); + Slic3rPolygons_to_ClipperPolygons(subject, *input_subject); + Slic3rPolygons_to_ClipperPolygons(clip, *input_clip); + + // perform safety offset + if (safety_offset) { + // SafetyOffset(*input_clip); + } + + // init Clipper + ClipperLib::Clipper clipper; + clipper.Clear(); + + // add polygons + clipper.AddPolygons(*input_subject, ClipperLib::ptSubject); + delete input_subject; + clipper.AddPolygons(*input_clip, ClipperLib::ptClip); + delete input_clip; + + // perform operation + ClipperLib::PolyTree* polytree = new ClipperLib::PolyTree(); + clipper.Execute(ClipperLib::ctDifference, *polytree, ClipperLib::pftNonZero, ClipperLib::pftNonZero); + + // convert into ExPolygons + PolyTreeToExPolygons(*polytree, retval); + delete polytree; +} + + } diff --git a/xs/xsp/Clipper.xsp b/xs/xsp/Clipper.xsp index 019639925..08fdfa3d7 100644 --- a/xs/xsp/Clipper.xsp +++ b/xs/xsp/Clipper.xsp @@ -18,24 +18,7 @@ offset_ex(polygons, delta, scale = 100000, joinType = ClipperLib::jtMiter, miter ClipperLib::JoinType joinType double miterLimit CODE: - // read input - ClipperLib::Polygons* input = new ClipperLib::Polygons(); - Slic3rPolygons_to_ClipperPolygons(polygons, *input); - - // scale input - scaleClipperPolygons(*input, scale); - - // perform offset - ClipperLib::Polygons* output = new ClipperLib::Polygons(); - ClipperLib::OffsetPolygons(*input, *output, (delta*scale), joinType, miterLimit); - delete input; - - // unscale output - scaleClipperPolygons(*output, 1/scale); - - // convert into ExPolygons - ClipperPolygons_to_Slic3rExPolygons(*output, RETVAL); - delete output; + offset_ex(polygons, RETVAL, delta, scale, joinType, miterLimit); OUTPUT: RETVAL @@ -48,29 +31,7 @@ offset2_ex(polygons, delta1, delta2, scale = 100000, joinType = ClipperLib::jtMi ClipperLib::JoinType joinType double miterLimit CODE: - // read input - ClipperLib::Polygons* input = new ClipperLib::Polygons(); - Slic3rPolygons_to_ClipperPolygons(polygons, *input); - - // scale input - scaleClipperPolygons(*input, scale); - - // perform first offset - ClipperLib::Polygons* output1 = new ClipperLib::Polygons(); - ClipperLib::OffsetPolygons(*input, *output1, (delta1*scale), joinType, miterLimit); - delete input; - - // perform second offset - ClipperLib::Polygons* output2 = new ClipperLib::Polygons(); - ClipperLib::OffsetPolygons(*output1, *output2, (delta2*scale), joinType, miterLimit); - delete output1; - - // unscale output - scaleClipperPolygons(*output2, 1/scale); - - // convert into ExPolygons - ClipperPolygons_to_Slic3rExPolygons(*output2, RETVAL); - delete output2; + offset2_ex(polygons, RETVAL, delta1, delta2, scale, joinType, miterLimit); OUTPUT: RETVAL @@ -80,34 +41,7 @@ diff_ex(subject, clip, safety_offset = false) Polygons clip bool safety_offset CODE: - // read input - ClipperLib::Polygons* input_subject = new ClipperLib::Polygons(); - ClipperLib::Polygons* input_clip = new ClipperLib::Polygons(); - Slic3rPolygons_to_ClipperPolygons(subject, *input_subject); - Slic3rPolygons_to_ClipperPolygons(clip, *input_clip); - - // perform safety offset - if (safety_offset) { - // SafetyOffset(*input_clip); - } - - // init Clipper - ClipperLib::Clipper clipper; - clipper.Clear(); - - // add polygons - clipper.AddPolygons(*input_subject, ClipperLib::ptSubject); - delete input_subject; - clipper.AddPolygons(*input_clip, ClipperLib::ptClip); - delete input_clip; - - // perform operation - ClipperLib::PolyTree* polytree = new ClipperLib::PolyTree(); - clipper.Execute(ClipperLib::ctDifference, *polytree, ClipperLib::pftNonZero, ClipperLib::pftNonZero); - - // convert into ExPolygons - PolyTreeToExPolygons(*polytree, RETVAL); - delete polytree; + diff_ex(subject, clip, RETVAL, safety_offset); OUTPUT: RETVAL