diff options
author | Vojtech Bubnik <bubnikv@gmail.com> | 2016-05-03 22:47:01 +0300 |
---|---|---|
committer | Vojtech Bubnik <bubnikv@gmail.com> | 2016-05-03 22:47:01 +0300 |
commit | 0102e16de1bd7614dc83ec7d8b3fb2e47b235cf3 (patch) | |
tree | e6b94a1b14a00003d447eb74935c41795327e17a | |
parent | 79c5e0a52d3a797967756a807989dd7c0f4b94c3 (diff) |
Some improvements on the infills.version_1.2.31
28 files changed, 798 insertions, 277 deletions
diff --git a/slic3r.sublime-project b/slic3r.sublime-project index 9deb0e273..0ef692e81 100644 --- a/slic3r.sublime-project +++ b/slic3r.sublime-project @@ -11,24 +11,24 @@ "name": "Run", "working_dir": "$project_path", "file_regex": " at (.*?) line ([0-9]*)", - "shell_cmd": "perl slic3r.pl --gui \"..\\Slic3r-tests\\gap fill torture 20 -rt.stl\"" + "shell_cmd": "chdir & perl slic3r.pl --gui \"..\\Slic3r-tests\\gap fill torture 20 -rt.stl\"" }, { "name": "full", "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$", - "shell_cmd": "perl Build.pl" + "shell_cmd": "chdir & perl Build.pl" }, { "name": "xs", "working_dir": "$project_path/xs", "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$", - "shell_cmd": "perl Build install" + "shell_cmd": "chdir & perl Build install" }, { "name": "xs & run", "working_dir": "$project_path/xs", "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$", - "shell_cmd": "perl Build install & cd .. & perl slic3r.pl --gui \"..\\Slic3r-tests\\star3-big2.stl\"" + "shell_cmd": "chdir & perl Build install & cd .. & perl slic3r.pl --gui \"..\\Slic3r-tests\\star3-big2.stl\"" } ], "folders": diff --git a/xs/src/libslic3r/ClipperUtils.cpp b/xs/src/libslic3r/ClipperUtils.cpp index a1297e574..ca7822c42 100644 --- a/xs/src/libslic3r/ClipperUtils.cpp +++ b/xs/src/libslic3r/ClipperUtils.cpp @@ -1,6 +1,12 @@ #include "ClipperUtils.hpp" #include "Geometry.hpp" +#define CLIPPER_UTILS_DEBUG + +#ifdef CLIPPER_UTILS_DEBUG +#include "SVG.hpp" +#endif /* CLIPPER_UTILS_DEBUG */ + namespace Slic3r { //----------------------------------------------------------- @@ -226,6 +232,19 @@ void offset2(const Slic3r::Polygons &polygons, ClipperLib::Paths* retval, const float delta1, const float delta2, const double scale, const ClipperLib::JoinType joinType, const double miterLimit) { +#ifdef CLIPPER_UTILS_DEBUG + BoundingBox bbox = get_extents(polygons); + coordf_t stroke_width = scale_(0.005); + static int iRun = 0; + ++ iRun; + char path[2048]; + sprintf(path, "out\\offset2-%d.svg", iRun); + bool flipY = false; + SVG svg(path, bbox, scale_(1.), flipY); + for (Slic3r::Polygons::const_iterator it = polygons.begin(); it != polygons.end(); ++ it) + svg.draw(it->lines(), "gray", stroke_width); +#endif /* CLIPPER_UTILS_DEBUG */ + // read input ClipperLib::Paths input; Slic3rMultiPoints_to_ClipperPaths(polygons, &input); @@ -245,12 +264,18 @@ offset2(const Slic3r::Polygons &polygons, ClipperLib::Paths* retval, const float ClipperLib::Paths output1; co.AddPaths(input, joinType, ClipperLib::etClosedPolygon); co.Execute(output1, (delta1*scale)); +#ifdef CLIPPER_UTILS_DEBUG + svg.draw(output1, 1./CLIPPER_OFFSET_SCALE, "red", stroke_width); +#endif /* CLIPPER_UTILS_DEBUG */ // perform second offset co.Clear(); co.AddPaths(output1, joinType, ClipperLib::etClosedPolygon); co.Execute(*retval, (delta2*scale)); - +#ifdef CLIPPER_UTILS_DEBUG + svg.draw(*retval, 1./CLIPPER_OFFSET_SCALE, "green", stroke_width); +#endif /* CLIPPER_UTILS_DEBUG */ + // unscale output scaleClipperPolygons(*retval, 1/scale); } diff --git a/xs/src/libslic3r/ClipperUtils.hpp b/xs/src/libslic3r/ClipperUtils.hpp index e7396af76..ccdf75588 100644 --- a/xs/src/libslic3r/ClipperUtils.hpp +++ b/xs/src/libslic3r/ClipperUtils.hpp @@ -14,6 +14,11 @@ using ClipperLib::jtSquare; namespace Slic3r { +// Factor to convert from coord_t (which is int32) to an int64 type used by the Clipper library. +//FIXME Vojtech: Better to use a power of 2 coefficient and to use bit shifts for scaling. +// How about 2^17=131072? +// By the way, is the scalling needed at all? Cura runs all the computation with a fixed point precision of 1um, while Slic3r scales to 1nm, +// further scaling by 10e5 brings us to #define CLIPPER_OFFSET_SCALE 100000.0 //----------------------------------------------------------- diff --git a/xs/src/libslic3r/Fill/Fill3DHoneycomb.cpp b/xs/src/libslic3r/Fill/Fill3DHoneycomb.cpp index ce3fa3c57..85fa66c6f 100644 --- a/xs/src/libslic3r/Fill/Fill3DHoneycomb.cpp +++ b/xs/src/libslic3r/Fill/Fill3DHoneycomb.cpp @@ -139,9 +139,14 @@ static Polylines makeGrid(coord_t z, coord_t gridSize, size_t gridWidth, size_t return result; } -Polylines Fill3DHoneycomb::fill_surface(const Surface *surface, const FillParams ¶ms) +void Fill3DHoneycomb::_fill_surface_single( + const FillParams ¶ms, + unsigned int thickness_layers, + const std::pair<float, Point> &direction, + ExPolygon &expolygon, + Polylines &polylines_out) { - ExPolygon expolygon = surface->expolygon; + // no rotation is supported for this infill pattern BoundingBox bb = expolygon.contour.bounding_box(); Point size = bb.size(); coord_t distance = coord_t(scale_(this->spacing) / params.density); @@ -159,7 +164,7 @@ Polylines Fill3DHoneycomb::fill_surface(const Surface *surface, const FillParams distance, ceil(size.x / distance) + 1, ceil(size.y / distance) + 1, - ((this->layer_id / surface->thickness_layers) % 2) + 1); + ((this->layer_id/thickness_layers) % 2) + 1); // move pattern in place for (Polylines::iterator it = polylines.begin(); it != polylines.end(); ++ it) @@ -186,15 +191,11 @@ Polylines Fill3DHoneycomb::fill_surface(const Surface *surface, const FillParams polylines, #endif PolylineCollection::leftmost_point(polylines), false); // reverse allowed -#if SLIC3R_CPPVER >= 11 - assert(polylines.empty()); -#else - polylines.clear(); -#endif + bool first = true; for (Polylines::iterator it_polyline = chained.begin(); it_polyline != chained.end(); ++ it_polyline) { - if (! polylines.empty()) { + if (! first) { // Try to connect the lines. - Points &pts_end = polylines.back().points; + Points &pts_end = polylines_out.back().points; const Point &first_point = it_polyline->points.front(); const Point &last_point = pts_end.back(); // TODO: we should also check that both points are on a fill_boundary to avoid @@ -208,16 +209,14 @@ Polylines Fill3DHoneycomb::fill_surface(const Surface *surface, const FillParams } // The lines cannot be connected. #if SLIC3R_CPPVER >= 11 - polylines.push_back(std::move(*it_polyline)); + polylines_out.push_back(std::move(*it_polyline)); #else - polylines.push_back(Polyline()); - std::swap(polylines.back(), *it_polyline); + polylines_out.push_back(Polyline()); + std::swap(polylines_out.back(), *it_polyline); #endif + first = false; } } - - // TODO: return ExtrusionLoop objects to get better chained paths - return polylines; } } // namespace Slic3r diff --git a/xs/src/libslic3r/Fill/Fill3DHoneycomb.hpp b/xs/src/libslic3r/Fill/Fill3DHoneycomb.hpp index 1410f5b55..fae5b3c60 100644 --- a/xs/src/libslic3r/Fill/Fill3DHoneycomb.hpp +++ b/xs/src/libslic3r/Fill/Fill3DHoneycomb.hpp @@ -9,14 +9,21 @@ namespace Slic3r { -class Fill3DHoneycomb : public FillWithDirection +class Fill3DHoneycomb : public Fill { public: virtual ~Fill3DHoneycomb() {} - virtual Polylines fill_surface(const Surface *surface, const FillParams ¶ms); - + // require bridge flow since most of this pattern hangs in air virtual bool use_bridge_flow() const { return true; } + +protected: + virtual void _fill_surface_single( + const FillParams ¶ms, + unsigned int thickness_layers, + const std::pair<float, Point> &direction, + ExPolygon &expolygon, + Polylines &polylines_out); }; } // namespace Slic3r diff --git a/xs/src/libslic3r/Fill/FillBase.cpp b/xs/src/libslic3r/Fill/FillBase.cpp index c17f57185..96947da25 100644 --- a/xs/src/libslic3r/Fill/FillBase.cpp +++ b/xs/src/libslic3r/Fill/FillBase.cpp @@ -1,3 +1,4 @@ +#include "../ClipperUtils.hpp" #include "../Surface.hpp" #include "FillBase.hpp" @@ -24,7 +25,8 @@ Fill* Fill::new_from_type(const std::string &type) if (type == "line") return new FillLine(); if (type == "grid") - return new FillGrid(); +// return new FillGrid(); + return new FillGrid2(); if (type == "archimedeanchords") return new FillArchimedeanChords(); if (type == "hilbertcurve") @@ -35,16 +37,33 @@ Fill* Fill::new_from_type(const std::string &type) return NULL; } -coord_t Fill::adjust_solid_spacing(const coord_t width, const coord_t distance) +Polylines Fill::fill_surface(const Surface *surface, const FillParams ¶ms) +{ + // Perform offset. + Slic3r::ExPolygons expp; + offset(surface->expolygon, &expp, -0.5*scale_(this->spacing)); + // Create the infills for each of the regions. + Polylines polylines_out; + for (size_t i = 0; i < expp.size(); ++ i) + _fill_surface_single( + params, + surface->thickness_layers, + _infill_direction(surface), + expp[i], + polylines_out); + return polylines_out; +} + +coord_t Fill::_adjust_solid_spacing(const coord_t width, const coord_t distance) { coord_t number_of_lines = coord_t(coordf_t(width) / coordf_t(distance)) + 1; coord_t extra_space = width % distance; return (number_of_lines <= 1) ? - distance : - distance + extra_space / (number_of_lines - 1); + distance : + distance + extra_space / (number_of_lines - 1); } -std::pair<float, Point> FillWithDirection::infill_direction(const Surface *surface) const +std::pair<float, Point> Fill::_infill_direction(const Surface *surface) const { // set infill angle float out_angle = this->angle; diff --git a/xs/src/libslic3r/Fill/FillBase.hpp b/xs/src/libslic3r/Fill/FillBase.hpp index 8fa2bee42..1d465039f 100644 --- a/xs/src/libslic3r/Fill/FillBase.hpp +++ b/xs/src/libslic3r/Fill/FillBase.hpp @@ -1,6 +1,7 @@ #ifndef slic3r_FillBase_hpp_ #define slic3r_FillBase_hpp_ +#include <memory.h> #include <float.h> #include "../libslic3r.h" @@ -61,7 +62,7 @@ public: virtual bool no_sort() const { return false; } // Perform the fill. - virtual Polylines fill_surface(const Surface *surface, const FillParams ¶ms) = 0; + virtual Polylines fill_surface(const Surface *surface, const FillParams ¶ms); protected: Fill() : @@ -75,7 +76,22 @@ protected: bounding_box(Point(0, 0), Point(-1, -1)) {} - static coord_t adjust_solid_spacing(const coord_t width, const coord_t distance); + // The expolygon may be modified by the method to avoid a copy. + virtual void _fill_surface_single( + const FillParams ¶ms, + unsigned int thickness_layers, + const std::pair<float, Point> &direction, + ExPolygon &expolygon, + Polylines &polylines_out) {} + + static coord_t _adjust_solid_spacing(const coord_t width, const coord_t distance); + + virtual float _layer_angle(size_t idx) const { + bool odd = idx & 1; + return (idx & 1) ? float(M_PI/2.) : 0; + } + + virtual std::pair<float, Point> _infill_direction(const Surface *surface) const; }; // An interface class to Perl, aggregating an instance of a Fill and a FillData. @@ -88,16 +104,6 @@ public: FillParams params; }; -class FillWithDirection : public Fill -{ -public: - virtual float _layer_angle(size_t idx) const { - bool odd = idx & 1; - return (idx & 1) ? float(M_PI/2.) : 0; - } - virtual std::pair<float, Point> infill_direction(const Surface *surface) const ; -}; - } // namespace Slic3r #endif // slic3r_FillBase_hpp_ diff --git a/xs/src/libslic3r/Fill/FillConcentric.cpp b/xs/src/libslic3r/Fill/FillConcentric.cpp index 30b34456d..27914866d 100644 --- a/xs/src/libslic3r/Fill/FillConcentric.cpp +++ b/xs/src/libslic3r/Fill/FillConcentric.cpp @@ -6,17 +6,21 @@ namespace Slic3r { -Polylines FillConcentric::fill_surface(const Surface *surface, const FillParams ¶ms) +void FillConcentric::_fill_surface_single( + const FillParams ¶ms, + unsigned int thickness_layers, + const std::pair<float, Point> &direction, + ExPolygon &expolygon, + Polylines &polylines_out) { // no rotation is supported for this infill pattern - ExPolygon expolygon = surface->expolygon; BoundingBox bounding_box = expolygon.contour.bounding_box(); coord_t min_spacing = scale_(this->spacing); coord_t distance = coord_t(min_spacing / params.density); if (params.density > 0.9999f && !params.dont_adjust) { - distance = this->adjust_solid_spacing(bounding_box.size().x, distance); + distance = this->_adjust_solid_spacing(bounding_box.size().x, distance); this->spacing = unscale(distance); } @@ -32,29 +36,27 @@ Polylines FillConcentric::fill_surface(const Surface *surface, const FillParams union_pt_chained(loops, &loops, false); // split paths using a nearest neighbor search - Polylines paths; + size_t iPathFirst = polylines_out.size(); Point last_pos(0, 0); for (Polygons::const_iterator it_loop = loops.begin(); it_loop != loops.end(); ++ it_loop) { - paths.push_back(it_loop->split_at_index(last_pos.nearest_point_index(*it_loop))); - last_pos = paths.back().last_point(); + polylines_out.push_back(it_loop->split_at_index(last_pos.nearest_point_index(*it_loop))); + last_pos = polylines_out.back().last_point(); } // clip the paths to prevent the extruder from getting exactly on the first point of the loop // Keep valid paths only. - size_t j = 0; - for (size_t i = 0; i < paths.size(); ++ i) { - paths[i].clip_end(this->loop_clipping); - if (paths[i].is_valid()) { + size_t j = iPathFirst; + for (size_t i = iPathFirst; i < polylines_out.size(); ++ i) { + polylines_out[i].clip_end(this->loop_clipping); + if (polylines_out[i].is_valid()) { if (j < i) - std::swap(paths[j], paths[i]); + std::swap(polylines_out[j], polylines_out[i]); ++ j; } } - if (j < paths.size()) - paths.erase(paths.begin() + j, paths.end()); - + if (j < polylines_out.size()) + polylines_out.erase(polylines_out.begin() + j, polylines_out.end()); // TODO: return ExtrusionLoop objects to get better chained paths - return paths; } } // namespace Slic3r diff --git a/xs/src/libslic3r/Fill/FillConcentric.hpp b/xs/src/libslic3r/Fill/FillConcentric.hpp index 6d58ec089..91f14908a 100644 --- a/xs/src/libslic3r/Fill/FillConcentric.hpp +++ b/xs/src/libslic3r/Fill/FillConcentric.hpp @@ -9,9 +9,15 @@ class FillConcentric : public Fill { public: virtual ~FillConcentric() {} - virtual Polylines fill_surface(const Surface *surface, const FillParams ¶ms); protected: + virtual void _fill_surface_single( + const FillParams ¶ms, + unsigned int thickness_layers, + const std::pair<float, Point> &direction, + ExPolygon &expolygon, + Polylines &polylines_out); + virtual bool no_sort() const { return true; } }; diff --git a/xs/src/libslic3r/Fill/FillHoneycomb.cpp b/xs/src/libslic3r/Fill/FillHoneycomb.cpp index 408a5fa81..af7d25e13 100644 --- a/xs/src/libslic3r/Fill/FillHoneycomb.cpp +++ b/xs/src/libslic3r/Fill/FillHoneycomb.cpp @@ -6,10 +6,13 @@ namespace Slic3r { -Polylines FillHoneycomb::fill_surface(const Surface *surface, const FillParams ¶ms) +void FillHoneycomb::_fill_surface_single( + const FillParams ¶ms, + unsigned int thickness_layers, + const std::pair<float, Point> &direction, + ExPolygon &expolygon, + Polylines &polylines_out) { - std::pair<float, Point> rotate_vector = this->infill_direction(surface); - // cache hexagons math CacheID cache_id(params.density, this->spacing); Cache::iterator it_m = this->cache.find(cache_id); @@ -38,11 +41,11 @@ Polylines FillHoneycomb::fill_surface(const Surface *surface, const FillParams & // adjust actual bounding box to the nearest multiple of our hex pattern // and align it so that it matches across layers - BoundingBox bounding_box = surface->expolygon.contour.bounding_box(); + BoundingBox bounding_box = expolygon.contour.bounding_box(); { // rotate bounding box according to infill direction Polygon bb_polygon = bounding_box.polygon(); - bb_polygon.rotate(rotate_vector.first, m.hex_center); + bb_polygon.rotate(direction.first, m.hex_center); bounding_box = bb_polygon.bounding_box(); // extend bounding box so that our pattern will be aligned with other layers @@ -70,27 +73,27 @@ Polylines FillHoneycomb::fill_surface(const Surface *surface, const FillParams & std::swap(ax[0], ax[1]); // draw symmetrical pattern x += m.distance; } - p.rotate(-rotate_vector.first, m.hex_center); + p.rotate(-direction.first, m.hex_center); polygons.push_back(p); } } - Polylines paths; if (params.complete || true) { // we were requested to complete each loop; // in this case we don't try to make more continuous paths - Polygons polygons_trimmed = intersection((Polygons)*surface, polygons); + Polygons polygons_trimmed = intersection((Polygons)expolygon, polygons); for (Polygons::iterator it = polygons_trimmed.begin(); it != polygons_trimmed.end(); ++ it) - paths.push_back(it->split_at_first_point()); + polylines_out.push_back(it->split_at_first_point()); } else { // consider polygons as polylines without re-appending the initial point: // this cuts the last segment on purpose, so that the jump to the next // path is more straight + Polylines paths; { Polylines p; for (Polygons::iterator it = polygons.begin(); it != polygons.end(); ++ it) p.push_back((Polyline)(*it)); - paths = intersection(p, (Polygons)*surface); + intersection(p, (Polygons)expolygon, &paths); } // connect paths @@ -119,15 +122,13 @@ Polylines FillHoneycomb::fill_surface(const Surface *surface, const FillParams & } // clip paths again to prevent connection segments from crossing the expolygon boundaries - Polylines paths_trimmed = intersection(paths, to_polygons(offset_ex(surface->expolygon, SCALED_EPSILON))); -#if SLIC3R_CPPVER >= 11 - paths = std::move(paths_trimmed); -#else - std::swap(paths, paths_trimmed); -#endif + intersection(paths, to_polygons(offset_ex(expolygon, SCALED_EPSILON)), &paths); + // Move the polylines to the output, avoid a deep copy. + size_t j = polylines_out.size(); + polylines_out.resize(j + paths.size(), Polyline()); + for (size_t i = 0; i < paths.size(); ++ i) + std::swap(polylines_out[j ++], paths[i]); } - - return paths; } } // namespace Slic3r diff --git a/xs/src/libslic3r/Fill/FillHoneycomb.hpp b/xs/src/libslic3r/Fill/FillHoneycomb.hpp index 63fe1f6f4..d0b45a530 100644 --- a/xs/src/libslic3r/Fill/FillHoneycomb.hpp +++ b/xs/src/libslic3r/Fill/FillHoneycomb.hpp @@ -9,13 +9,19 @@ namespace Slic3r { -class FillHoneycomb : public FillWithDirection +class FillHoneycomb : public Fill { public: virtual ~FillHoneycomb() {} - virtual Polylines fill_surface(const Surface *surface, const FillParams ¶ms); protected: + virtual void _fill_surface_single( + const FillParams ¶ms, + unsigned int thickness_layers, + const std::pair<float, Point> &direction, + ExPolygon &expolygon, + Polylines &polylines_out); + // Caching the struct CacheID { diff --git a/xs/src/libslic3r/Fill/FillPlanePath.cpp b/xs/src/libslic3r/Fill/FillPlanePath.cpp index 0a271cbd8..d9d7db31a 100644 --- a/xs/src/libslic3r/Fill/FillPlanePath.cpp +++ b/xs/src/libslic3r/Fill/FillPlanePath.cpp @@ -6,17 +6,20 @@ namespace Slic3r { -Polylines FillPlanePath::fill_surface(const Surface *surface, const FillParams ¶ms) +void FillPlanePath::_fill_surface_single( + const FillParams ¶ms, + unsigned int thickness_layers, + const std::pair<float, Point> &direction, + ExPolygon &expolygon, + Polylines &polylines_out) { - ExPolygon expolygon = surface->expolygon; - std::pair<float, Point> rotate_vector = this->infill_direction(surface); - expolygon.rotate(- rotate_vector.first); + expolygon.rotate(- direction.first); coord_t distance_between_lines = scale_(this->spacing) / params.density; // align infill across layers using the object's bounding box Polygon bb_polygon = this->bounding_box.polygon(); - bb_polygon.rotate(- rotate_vector.first); + bb_polygon.rotate(- direction.first); BoundingBox bounding_box = bb_polygon.bounding_box(); Point shift = this->_centered() ? @@ -61,11 +64,15 @@ Polylines FillPlanePath::fill_surface(const Surface *surface, const FillParams & // paths must be repositioned and rotated back for (Polylines::iterator it = polylines.begin(); it != polylines.end(); ++ it) { it->translate(shift.x, shift.y); - it->rotate(rotate_vector.first); + it->rotate(direction.first); } } - - return polylines; + + // Move the polylines to the output, avoid a deep copy. + size_t j = polylines_out.size(); + polylines_out.resize(j + polylines.size(), Polyline()); + for (size_t i = 0; i < polylines.size(); ++ i) + std::swap(polylines_out[j ++], polylines[i]); } // Follow an Archimedean spiral, in polar coordinates: r=a+b\theta diff --git a/xs/src/libslic3r/Fill/FillPlanePath.hpp b/xs/src/libslic3r/Fill/FillPlanePath.hpp index 79e5522a9..6e9f45616 100644 --- a/xs/src/libslic3r/Fill/FillPlanePath.hpp +++ b/xs/src/libslic3r/Fill/FillPlanePath.hpp @@ -13,13 +13,19 @@ namespace Slic3r { // http://user42.tuxfamily.org/math-planepath/ // http://user42.tuxfamily.org/math-planepath/gallery.html -class FillPlanePath : public FillWithDirection +class FillPlanePath : public Fill { public: virtual ~FillPlanePath() {} - virtual Polylines fill_surface(const Surface *surface, const FillParams ¶ms); protected: + virtual void _fill_surface_single( + const FillParams ¶ms, + unsigned int thickness_layers, + const std::pair<float, Point> &direction, + ExPolygon &expolygon, + Polylines &polylines_out); + virtual float _layer_angle(size_t idx) const { return 0.f; } virtual bool _centered() const = 0; virtual Pointfs _generate(coord_t min_x, coord_t min_y, coord_t max_x, coord_t max_y) = 0; diff --git a/xs/src/libslic3r/Fill/FillRectilinear.cpp b/xs/src/libslic3r/Fill/FillRectilinear.cpp index e2f996e2a..8eb5302e5 100644 --- a/xs/src/libslic3r/Fill/FillRectilinear.cpp +++ b/xs/src/libslic3r/Fill/FillRectilinear.cpp @@ -7,15 +7,18 @@ namespace Slic3r { -Polylines FillRectilinear::fill_surface(const Surface *surface, const FillParams ¶ms) +void FillRectilinear::_fill_surface_single( + const FillParams ¶ms, + unsigned int thickness_layers, + const std::pair<float, Point> &direction, + ExPolygon &expolygon, + Polylines &polylines_out) { // rotate polygons so that we can work with vertical lines here - ExPolygon expolygon = surface->expolygon; - std::pair<float, Point> rotate_vector = this->infill_direction(surface); - expolygon.rotate(- rotate_vector.first); + expolygon.rotate(- direction.first); // No need to translate the polygon anyhow for the infill. // The infill will be performed inside a bounding box of the expolygon and its absolute position does not matter. -// expolygon.translate(rotate_vector.second.x, rotate_vector.second.y); +// expolygon.translate(direction.second.x, direction.second.y); this->_min_spacing = scale_(this->spacing); assert(params.density > 0.0001f && params.density <= 1.f); @@ -26,7 +29,7 @@ Polylines FillRectilinear::fill_surface(const Surface *surface, const FillParams // define flow spacing according to requested density if (params.density > 0.9999f && !params.dont_adjust) { - this->_line_spacing = this->adjust_solid_spacing(bounding_box.size().x, this->_line_spacing); + this->_line_spacing = this->_adjust_solid_spacing(bounding_box.size().x, this->_line_spacing); this->spacing = unscale(this->_line_spacing); } else { // extend bounding box so that our pattern will be aligned with other layers @@ -65,6 +68,7 @@ Polylines FillRectilinear::fill_surface(const Surface *surface, const FillParams // FIXME Vojtech: This is only performed for horizontal lines, not for the vertical lines! const float INFILL_OVERLAP_OVER_SPACING = 0.3f; + // How much to extend an infill path from expolygon outside? coord_t extra = coord_t(floor(this->_min_spacing * INFILL_OVERLAP_OVER_SPACING + 0.5f)); for (Polylines::iterator it_polyline = polylines.begin(); it_polyline != polylines.end(); ++ it_polyline) { Point *first_point = &it_polyline->points.front(); @@ -75,6 +79,8 @@ Polylines FillRectilinear::fill_surface(const Surface *surface, const FillParams last_point->y += extra; } + size_t n_polylines_out_old = polylines_out.size(); + // connect lines if (! params.dont_connect && ! polylines.empty()) { // prevent calling leftmost_point() on empty collections // offset the expolygon by max(min_spacing/2, extra) @@ -94,15 +100,11 @@ Polylines FillRectilinear::fill_surface(const Surface *surface, const FillParams polylines, #endif PolylineCollection::leftmost_point(polylines), false); // reverse allowed -#if SLIC3R_CPPVER >= 11 - assert(polylines.empty()); -#else - polylines.clear(); -#endif + bool first = true; for (Polylines::iterator it_polyline = chained.begin(); it_polyline != chained.end(); ++ it_polyline) { - if (! polylines.empty()) { + if (! first) { // Try to connect the lines. - Points &pts_end = polylines.back().points; + Points &pts_end = polylines_out.back().points; const Point &first_point = it_polyline->points.front(); const Point &last_point = pts_end.back(); // Distance in X, Y. @@ -118,21 +120,21 @@ Polylines FillRectilinear::fill_surface(const Surface *surface, const FillParams } // The lines cannot be connected. #if SLIC3R_CPPVER >= 11 - polylines.push_back(std::move(*it_polyline)); + polylines_out.push_back(std::move(*it_polyline)); #else - polylines.push_back(Polyline()); - std::swap(polylines.back(), *it_polyline); + polylines_out.push_back(Polyline()); + std::swap(polylines_out.back(), *it_polyline); #endif + first = false; } } // paths must be rotated back - for (Polylines::iterator it = polylines.begin(); it != polylines.end(); ++ it) { + for (Polylines::iterator it = polylines_out.begin() + n_polylines_out_old; it != polylines_out.end(); ++ it) { // No need to translate, the absolute position is irrelevant. - // it->translate(- rotate_vector.second.x, - rotate_vector.second.y); - it->rotate(rotate_vector.first); + // it->translate(- direction.second.x, - direction.second.y); + it->rotate(direction.first); } - return polylines; } } // namespace Slic3r diff --git a/xs/src/libslic3r/Fill/FillRectilinear.hpp b/xs/src/libslic3r/Fill/FillRectilinear.hpp index d10c4ea65..8c13b8bc1 100644 --- a/xs/src/libslic3r/Fill/FillRectilinear.hpp +++ b/xs/src/libslic3r/Fill/FillRectilinear.hpp @@ -9,13 +9,19 @@ namespace Slic3r { class Surface; -class FillRectilinear : public FillWithDirection +class FillRectilinear : public Fill { public: virtual ~FillRectilinear() {} - virtual Polylines fill_surface(const Surface *surface, const FillParams ¶ms); protected: + virtual void _fill_surface_single( + const FillParams ¶ms, + unsigned int thickness_layers, + const std::pair<float, Point> &direction, + ExPolygon &expolygon, + Polylines &polylines_out); + coord_t _min_spacing; coord_t _line_spacing; // distance threshold for allowing the horizontal infill lines to be connected into a continuous path diff --git a/xs/src/libslic3r/Fill/FillRectilinear2.cpp b/xs/src/libslic3r/Fill/FillRectilinear2.cpp index 1b9019305..a46e04354 100644 --- a/xs/src/libslic3r/Fill/FillRectilinear2.cpp +++ b/xs/src/libslic3r/Fill/FillRectilinear2.cpp @@ -1,3 +1,6 @@ +//FIXME +#define SLIC3R_DEBUG + #include <assert.h> #include <stdint.h> @@ -17,25 +20,6 @@ #include "SVG.hpp" #endif -#if defined(SLIC3R_DEBUG) and defined(_WIN32) -#include <Windows.h> -#pragma comment(lib, "user32.lib") - static inline void assert_fail(const char *assertion, const char *file, unsigned line, const char *function) - { - printf("Assert: %s in function %s\nfile %s:%d\n", assertion, function, file, line); - if (IsDebuggerPresent()) { - DebugBreak(); - } else { - ExitProcess(-1); - } - } - #undef assert - #define assert(expr) \ - ((expr) \ - ? static_cast<void>(0) \ - : assert_fail (#expr, __FILE__, __LINE__, __FUNCTION__)) -#endif /* SLIC3R_DEBUG */ - namespace Slic3r { #ifndef clamp @@ -262,45 +246,65 @@ public: struct ExPolygonWithOffset { public: - ExPolygonWithOffset(const ExPolygon &aexpolygon, coord_t aoffset) : expolygon(aexpolygon) + ExPolygonWithOffset( + const ExPolygon &expolygon, + float angle, + coord_t aoffset1, + coord_t aoffset2) { - polygons_inner = offset((Polygons)expolygon, aoffset, - CLIPPER_OFFSET_SCALE, - ClipperLib::jtMiter, - // for the infill pattern, don't cut the corners. - // default miterLimt = 3 - 10.); - n_contours_outer = 1 + expolygon.holes.size(); + // Copy and rotate the source polygons. + polygons_src = (Polygons)expolygon; + for (Polygons::iterator it = polygons_src.begin(); it != polygons_src.end(); ++ it) + it->rotate(angle); + + double mitterLimit = 3.; + // for the infill pattern, don't cut the corners. + // default miterLimt = 3 + //double mitterLimit = 10.; + assert(aoffset1 < 0); + assert(aoffset2 < 0); + assert(aoffset2 < aoffset1); + polygons_outer = offset(polygons_src, aoffset1, + CLIPPER_OFFSET_SCALE, + ClipperLib::jtMiter, + mitterLimit); + polygons_inner = offset(polygons_src, aoffset2, + CLIPPER_OFFSET_SCALE, + ClipperLib::jtMiter, + mitterLimit); + n_contours_outer = polygons_outer.size(); n_contours_inner = polygons_inner.size(); n_contours = n_contours_outer + n_contours_inner; - polygons_inner_ccw.assign(polygons_inner.size(), false); - for (size_t i = 0; i < polygons_inner.size(); ++ i) - polygons_inner_ccw[i] = is_ccw(polygons_inner[i]); - #ifdef SLIC3R_DEBUG - // Verify orientation of the expolygon. - assert(is_ccw(expolygon.contour)); - for (size_t i = 0; i < expolygon.holes.size(); ++ i) - assert(is_ccw(expolygon.holes[i])); - #endif /* SLIC3R_DEBUG */ + polygons_ccw.assign(n_contours, false); + for (size_t i = 0; i < n_contours; ++ i) { + contour(i).remove_duplicate_points(); + assert(! contour(i).has_duplicate_points()); + polygons_ccw[i] = is_ccw(contour(i)); + } } - // Outer contour of the expolygon. - bool is_contour_external(size_t idx) const { return idx == 0; } - // Any contour of the expolygon. - bool is_contour_outer(size_t idx) const { return idx < n_contours_inner; } - // Contour of the shrunk expolygon. - bool is_contour_inner(size_t idx) const { return idx >= n_contours_inner; } + // Any contour with offset1 + bool is_contour_outer(size_t idx) const { return idx < n_contours_outer; } + // Any contour with offset2 + bool is_contour_inner(size_t idx) const { return idx >= n_contours_outer; } - const Polygon& contour(size_t idx) const { - return is_contour_external(idx) ? expolygon.contour : - (is_contour_outer(idx) ? expolygon.holes[idx - 1] : polygons_inner[idx - n_contours_inner]); - } + const Polygon& contour(size_t idx) const + { return is_contour_outer(idx) ? polygons_outer[idx] : polygons_inner[idx - n_contours_outer]; } - bool is_contour_ccw(size_t idx) const { - return is_contour_external(idx) || (is_contour_inner(idx) && polygons_inner_ccw[idx - n_contours_inner]); - } + Polygon& contour(size_t idx) + { return is_contour_outer(idx) ? polygons_outer[idx] : polygons_inner[idx - n_contours_outer]; } + + bool is_contour_ccw(size_t idx) const { return polygons_ccw[idx]; } - const ExPolygon &expolygon; + BoundingBox bounding_box_src() const + { return _bounding_box_polygons(polygons_src); } + BoundingBox bounding_box_outer() const + { return _bounding_box_polygons(polygons_outer); } + BoundingBox bounding_box_inner() const + { return _bounding_box_polygons(polygons_inner); } + + Polygons polygons_src; + Polygons polygons_outer; Polygons polygons_inner; size_t n_contours_outer; @@ -308,11 +312,30 @@ public: size_t n_contours; protected: - // For each polygon of polygons_inner, remember its orientation. - std::vector<unsigned char> polygons_inner_ccw; + std::vector<unsigned char> polygons_ccw; + + static BoundingBox _bounding_box_polygons(const Polygons &poly) { + BoundingBox bbox; + if (! poly.empty()) { + bbox = poly.front().bounding_box(); + for (size_t i = 1; i < poly.size(); ++ i) + bbox.merge(poly[i]); + } + return bbox; + } }; +static inline int distance_of_segmens(const Polygon &poly, size_t seg1, size_t seg2, bool forward) +{ + int d = int(seg2) - int(seg1); + if (! forward) + d = - d; + if (d < 0) + d += int(poly.points.size()); + return d; +} + // For a vertical line, an inner contour and an intersection point, // find an intersection point on the previous resp. next vertical line. // The intersection point is connected with the prev resp. next intersection point with iInnerContour. @@ -339,7 +362,8 @@ static inline int intersection_on_prev_next_vertical_line( const SegmentIntersection &itsct = il.intersections[iIntersection]; const SegmentedIntersectionLine &il2 = segs[iVerticalLineOther]; const Polygon &poly = poly_with_offset.contour(iInnerContour); - const bool ccw = poly_with_offset.is_contour_ccw(iInnerContour); +// const bool ccw = poly_with_offset.is_contour_ccw(iInnerContour); + const bool forward = itsct.is_low() == dir_is_next; // Resulting index of an intersection point on il2. int out = -1; // Find an intersection point on iVerticalLineOther, intersecting iInnerContour @@ -349,13 +373,30 @@ static inline int intersection_on_prev_next_vertical_line( for (size_t i = 0; i < il2.intersections.size(); ++ i) { const SegmentIntersection &itsct2 = il2.intersections[i]; if (itsct.iContour == itsct2.iContour && itsct.type == itsct2.type) { + /* + if (itsct.is_low()) { + assert(itsct.type == SegmentIntersection::INNER_LOW); + assert(iIntersection > 0); + assert(il.intersections[iIntersection-1].type == SegmentIntersection::OUTER_LOW); + assert(i > 0); + if (il2.intersections[i-1].is_inner()) + // Take only the lowest inner intersection point. + continue; + assert(il2.intersections[i-1].type == SegmentIntersection::OUTER_LOW); + } else { + assert(itsct.type == SegmentIntersection::INNER_HIGH); + assert(iIntersection+1 < il.intersections.size()); + assert(il.intersections[iIntersection+1].type == SegmentIntersection::OUTER_HIGH); + assert(i+1 < il2.intersections.size()); + if (il2.intersections[i+1].is_inner()) + // Take only the highest inner intersection point. + continue; + assert(il2.intersections[i+1].type == SegmentIntersection::OUTER_HIGH); + } + */ // The intersection points lie on the same contour and have the same orientation. // Find the intersection point with a shortest path in the direction of the contour. - int d = int(itsct2.iSegment) - int(itsct.iSegment); - if (ccw != dir_is_next) - d = - d; - if (d < 0) - d += int(poly.points.size()); + int d = distance_of_segmens(poly, itsct.iSegment, itsct2.iSegment, forward); if (d < dmin) { out = i; dmin = d; @@ -395,17 +436,26 @@ static inline int intersection_unused_on_prev_next_vertical_line( size_t iIntersection, bool dir_is_next) { + //FIXME This routine will propose a connecting line even if the connecting perimeter segment intersects + // iVertical line multiple times before reaching iIntersectionOther. int iIntersectionOther = intersection_on_prev_next_vertical_line(poly_with_offset, segs, iVerticalLine, iInnerContour, iIntersection, dir_is_next); if (iIntersectionOther == -1) return -1; - //FIXME this routine will propose a connecting line even if the connecting perimeter segment intersects iVertical line multiple times before reaching iIntersectionOther. assert(dir_is_next ? (iVerticalLine + 1 < segs.size()) : (iVerticalLine > 0)); const SegmentedIntersectionLine &il_this = segs[iVerticalLine]; const SegmentIntersection &itsct_this = il_this.intersections[iIntersection]; const SegmentedIntersectionLine &il_other = segs[dir_is_next ? (iVerticalLine+1) : (iVerticalLine-1)]; const SegmentIntersection &itsct_other = il_other.intersections[iIntersectionOther]; assert(itsct_other.is_inner()); - assert(itsct_other.is_low() || iIntersectionOther > 1); + assert(iIntersectionOther > 0); + assert(iIntersectionOther + 1 < il_other.intersections.size()); + // Is iIntersectionOther at the boundary of a vertical segment? + const SegmentIntersection &itsct_other2 = il_other.intersections[itsct_other.is_low() ? iIntersectionOther - 1 : iIntersectionOther + 1]; + if (itsct_other2.is_inner()) + // Cannot follow a perimeter segment into the middle of another vertical segment. + // Only perimeter segments connecting to the end of a vertical segment are followed. + return -1; + assert(itsct_other.is_low() == itsct_other2.is_low()); if (dir_is_next ? itsct_this.consumed_perimeter_right : itsct_other.consumed_perimeter_right) // This perimeter segment was already consumed. return -1; @@ -460,11 +510,11 @@ static inline coordf_t measure_perimeter_prev_next_segment_length( const SegmentedIntersectionLine &il2 = segs[iVerticalLineOther]; const SegmentIntersection &itsct2 = il2.intersections[iIntersection2]; const Polygon &poly = poly_with_offset.contour(iInnerContour); - const bool ccw = poly_with_offset.is_contour_ccw(iInnerContour); +// const bool ccw = poly_with_offset.is_contour_ccw(iInnerContour); assert(itsct.type == itsct2.type); assert(itsct.iContour == itsct2.iContour); assert(itsct.is_inner()); - const bool forward = (itsct.is_low() == ccw) == dir_is_next; + const bool forward = itsct.is_low() == dir_is_next; Point p1(il.pos, itsct.pos); Point p2(il2.pos, itsct2.pos); @@ -522,11 +572,11 @@ static inline void emit_perimeter_prev_next_segment( const SegmentedIntersectionLine &il2 = segs[iVerticalLineOther]; const SegmentIntersection &itsct2 = il2.intersections[iIntersection2]; const Polygon &poly = poly_with_offset.contour(iInnerContour); - const bool ccw = poly_with_offset.is_contour_ccw(iInnerContour); +// const bool ccw = poly_with_offset.is_contour_ccw(iInnerContour); assert(itsct.type == itsct2.type); assert(itsct.iContour == itsct2.iContour); assert(itsct.is_inner()); - const bool forward = (itsct.is_low() == ccw) == dir_is_next; + const bool forward = itsct.is_low() == dir_is_next; // Do not append the first point. // out.points.push_back(Point(il.pos, itsct.pos)); if (forward) @@ -537,26 +587,43 @@ static inline void emit_perimeter_prev_next_segment( out.points.push_back(Point(il2.pos, itsct2.pos)); } -Polylines FillRectilinear2::fill_surface(const Surface *surface, const FillParams ¶ms) +void FillRectilinear2::fill_surface_by_lines(const Surface *surface, const FillParams ¶ms, float angleBase, Polylines &polylines_out) { - // rotate polygons so that we can work with vertical lines here - ExPolygon expolygon = surface->expolygon; - std::pair<float, Point> rotate_vector = this->infill_direction(surface); - expolygon.rotate(- rotate_vector.first); - // No need to translate the polygon anyhow for the infill. - // The infill will be performed inside a bounding box of the expolygon and its absolute position does not matter. -// expolygon.translate(rotate_vector.second.x, rotate_vector.second.y); + // At the end, only the new polylines will be rotated back. + size_t n_polylines_out_initial = polylines_out.size(); + + // Shrink the input polygon a bit first to not push the infill lines out of the perimeters. +// const float INFILL_OVERLAP_OVER_SPACING = 0.3f; + const float INFILL_OVERLAP_OVER_SPACING = 0.45f; + assert(INFILL_OVERLAP_OVER_SPACING > 0 && INFILL_OVERLAP_OVER_SPACING < 0.5f); + + // Rotate polygons so that we can work with vertical lines here + std::pair<float, Point> rotate_vector = this->_infill_direction(surface); + rotate_vector.first += angleBase; this->_min_spacing = scale_(this->spacing); assert(params.density > 0.0001f && params.density <= 1.f); this->_line_spacing = coord_t(coordf_t(this->_min_spacing) / params.density); this->_diagonal_distance = this->_line_spacing * 2; - BoundingBox bounding_box = expolygon.contour.bounding_box(); - + + // On the polygons of poly_with_offset, the infill lines will be connected. + ExPolygonWithOffset poly_with_offset( + surface->expolygon, + - rotate_vector.first, + scale_(- (0.5 - INFILL_OVERLAP_OVER_SPACING) * this->spacing), + scale_(- 0.5 * this->spacing)); + if (poly_with_offset.n_contours_inner == 0) { + //FIXME maybe one shall trigger the gap fill here? + return; + } + + BoundingBox bounding_box = poly_with_offset.bounding_box_outer(); + // define flow spacing according to requested density - if (params.density > 0.9999f && !params.dont_adjust) { - this->_line_spacing = this->adjust_solid_spacing(bounding_box.size().x, this->_line_spacing); - this->spacing = unscale(this->_line_spacing); + bool full_infill = params.density > 0.9999f; + if (full_infill && !params.dont_adjust) { +// this->_min_spacing = this->_line_spacing = this->_adjust_solid_spacing(bounding_box.size().x, this->_line_spacing); +// this->spacing = unscale(this->_line_spacing); } else { // extend bounding box so that our pattern will be aligned with other layers bounding_box.merge(Point( @@ -567,41 +634,42 @@ Polylines FillRectilinear2::fill_surface(const Surface *surface, const FillParam // Intersect a set of euqally spaced vertical lines wiht expolygon. size_t n_vlines = (bounding_box.max.x - bounding_box.min.x + SCALED_EPSILON) / this->_line_spacing; coord_t x0 = bounding_box.min.x + this->_line_spacing; - // On these polygons the infill lines will be connected. - ExPolygonWithOffset poly_with_offset(expolygon, - _min_spacing / 2); #ifdef SLIC3R_DEBUG char path[2048]; static int iRun = 0; sprintf(path, "out/FillRectilinear2-%d.svg", iRun); - BoundingBox bbox_svg = expolygon.contour.bounding_box(); - bbox_svg.min.x -= coord_t(1. / SCALING_FACTOR); - bbox_svg.min.y -= coord_t(1. / SCALING_FACTOR); - bbox_svg.max.x += coord_t(1. / SCALING_FACTOR); - bbox_svg.max.y += coord_t(1. / SCALING_FACTOR); - ::Slic3r::SVG svg(path, bbox_svg); - svg.draw(expolygon.lines()); - svg.draw(poly_with_offset.polygons_inner); + BoundingBox bbox_svg = poly_with_offset.bounding_box_outer(); + ::Slic3r::SVG svg(path, bbox_svg); // , scale_(1.)); + for (size_t i = 0; i < poly_with_offset.polygons_src.size(); ++ i) + svg.draw(poly_with_offset.polygons_src[i].lines()); + for (size_t i = 0; i < poly_with_offset.polygons_outer.size(); ++ i) + svg.draw(poly_with_offset.polygons_outer[i].lines(), "green"); + for (size_t i = 0; i < poly_with_offset.polygons_inner.size(); ++ i) + svg.draw(poly_with_offset.polygons_inner[i].lines(), "brown"); { char path2[2048]; sprintf(path2, "out/FillRectilinear2-initial-%d.svg", iRun); - ::Slic3r::SVG svg(path2, bbox_svg); - svg.draw(expolygon.lines()); - svg.draw(poly_with_offset.polygons_inner); - svg.Close(); + ::Slic3r::SVG svg(path2, bbox_svg); // , scale_(1.)); + for (size_t i = 0; i < poly_with_offset.polygons_src.size(); ++ i) + svg.draw(poly_with_offset.polygons_src[i].lines()); + for (size_t i = 0; i < poly_with_offset.polygons_outer.size(); ++ i) + svg.draw(poly_with_offset.polygons_outer[i].lines(), "green"); + for (size_t i = 0; i < poly_with_offset.polygons_inner.size(); ++ i) + svg.draw(poly_with_offset.polygons_inner[i].lines(), "brown"); } iRun ++; #endif /* SLIC3R_DEBUG */ // For each contour - // Allocate the storage for the segments. + // Allocate storage for the segments. std::vector<SegmentedIntersectionLine> segs(n_vlines, SegmentedIntersectionLine()); for (size_t i = 0; i < n_vlines; ++ i) { segs[i].idx = i; segs[i].pos = x0 + i * this->_line_spacing; } for (size_t iContour = 0; iContour < poly_with_offset.n_contours; ++ iContour) { - const Points &contour = poly_with_offset.contour(iContour); + const Points &contour = poly_with_offset.contour(iContour).points; if (contour.size() < 2) continue; // For each segment @@ -630,6 +698,10 @@ Polylines FillRectilinear2::fill_surface(const Surface *surface, const FillParam assert(ir >= 0 && ir < segs.size()); if (l == r) { // The segment is vertical. + // Don't insert vertical segments at all. + // If the contour is not degenerate, then this vertical line will be crossed + // by the non-vertical segments preceding resp. succeeding this vertical segment. + /* SegmentIntersection is; is.iContour = iContour; is.iSegment = iSegment; @@ -637,6 +709,7 @@ Polylines FillRectilinear2::fill_surface(const Surface *surface, const FillParam segs[il].intersections.push_back(is); is.pos = p2.y; segs[il].intersections.push_back(is); + */ continue; } for (int i = il; i <= ir; ++ i) { @@ -674,8 +747,8 @@ Polylines FillRectilinear2::fill_surface(const Surface *surface, const FillParam for (size_t i = 1; i < sil.intersections.size(); ++ i) { size_t iContour1 = sil.intersections[i-1].iContour; size_t iContour2 = sil.intersections[i].iContour; - const Points &contour1 = poly_with_offset.contour(iContour1); - const Points &contour2 = poly_with_offset.contour(iContour2); + const Points &contour1 = poly_with_offset.contour(iContour1).points; + const Points &contour2 = poly_with_offset.contour(iContour2).points; size_t iSegment1 = sil.intersections[i-1].iSegment; size_t iPrev1 = ((iSegment1 == 0) ? contour1.size() : iSegment1) - 1; size_t iSegment2 = sil.intersections[i].iSegment; @@ -706,17 +779,24 @@ Polylines FillRectilinear2::fill_surface(const Surface *surface, const FillParam const Point x2(sil.pos, sil.intersections[i ].pos); bool successive = false; #endif /* SLIC3R_DEBUG */ + // Sort the points in the two segments by x. if (a->x > b->x) std::swap(a, b); if (c->x > d->x) std::swap(c, d); + assert(a->x <= sil.pos); + assert(c->x <= sil.pos); + assert(b->x >= sil.pos); + assert(d->x >= sil.pos); + // Sort the two segments, so the segment <a,b> will be on the left of <c,d>. bool upper_more_left = false; if (a->x > c->x) { upper_more_left = true; std::swap(a, c); std::swap(b, d); } - if (a == c || b == c) { + if (a == c) { + // The segments iSegment1 and iSegment2 are directly connected. assert(iContour1 == iContour2); assert(iSegment1 == iPrev2 || iPrev1 == iSegment2); std::swap(c, d); @@ -725,8 +805,17 @@ Polylines FillRectilinear2::fill_surface(const Surface *surface, const FillParam successive = true; #endif /* SLIC3R_DEBUG */ } +#ifdef SLIC3R_DEBUG + else if (b == d) { + // The segments iSegment1 and iSegment2 are directly connected. + assert(iContour1 == iContour2); + assert(iSegment1 == iPrev2 || iPrev1 == iSegment2); + assert(a != c && b != c); + successive = true; + } +#endif /* SLIC3R_DEBUG */ Orientation o = orient(*a, *b, *c); - assert(! ORIENTATION_COLINEAR); + assert(o != ORIENTATION_COLINEAR); swap = upper_more_left != (o == ORIENTATION_CW); #ifdef SLIC3R_DEBUG if (swap) @@ -737,7 +826,7 @@ Polylines FillRectilinear2::fill_surface(const Surface *surface, const FillParam } } if (swap) { - // Swap the intersection points, but keep the original positions, so they are sorted. + // Swap the intersection points, but keep the original positions, so they stay sorted by the y axis. std::swap(sil.intersections[i-1], sil.intersections[i]); std::swap(sil.intersections[i-1].pos, sil.intersections[i].pos); modified = true; @@ -745,19 +834,40 @@ Polylines FillRectilinear2::fill_surface(const Surface *surface, const FillParam } } while (modified); // Assign the intersection types. + size_t j = 0; for (size_t i = 0; i < sil.intersections.size(); ++ i) { // What is the orientation of the segment at the intersection point? size_t iContour = sil.intersections[i].iContour; - const Points &contour = poly_with_offset.contour(iContour); + const Points &contour = poly_with_offset.contour(iContour).points; size_t iSegment = sil.intersections[i].iSegment; size_t iPrev = ((iSegment == 0) ? contour.size() : iSegment) - 1; coord_t dir = contour[iSegment].x - contour[iPrev].x; - bool ccw = poly_with_offset.is_contour_ccw(iContour); - bool low = (dir > 0) == ccw; + // bool ccw = poly_with_offset.is_contour_ccw(iContour); + // bool low = (dir > 0) == ccw; + bool low = dir > 0; sil.intersections[i].type = poly_with_offset.is_contour_outer(iContour) ? (low ? SegmentIntersection::OUTER_LOW : SegmentIntersection::OUTER_HIGH) : (low ? SegmentIntersection::INNER_LOW : SegmentIntersection::INNER_HIGH); + if (j > 0 && + sil.intersections[i].pos == sil.intersections[j-1].pos && + sil.intersections[i].type == sil.intersections[j-1].type && + sil.intersections[i].iContour == sil.intersections[j-1].iContour) { + // This has to be a corner point crossing the vertical line. + // Remove the second intersection point. +#ifdef SLIC3R_DEBUG + size_t iSegment2 = sil.intersections[j-1].iSegment; + size_t iPrev2 = ((iSegment2 == 0) ? contour.size() : iSegment2) - 1; + assert(iSegment == iPrev2 || iSegment2 == iPrev); +#endif /* SLIC3R_DEBUG */ + } else { + if (j < i) + sil.intersections[j] = sil.intersections[i]; + ++ j; + } } + // Shrink the list of intersections, if any of the intersection was removed during the classification. + if (j < sil.intersections.size()) + sil.intersections.erase(sil.intersections.begin() + j, sil.intersections.end()); } #ifdef SLIC3R_DEBUG @@ -788,6 +898,26 @@ Polylines FillRectilinear2::fill_surface(const Surface *surface, const FillParam } } svg.Close(); + + // Verify the segments & paint them. + for (size_t i_seg = 0; i_seg < segs.size(); ++ i_seg) { + SegmentedIntersectionLine &sil = segs[i_seg]; + // The intersection points have to be even. + assert((sil.intersections.size() & 1) == 0); + for (size_t i = 0; i < sil.intersections.size();) { + // An intersection segment crossing the bigger contour may cross the inner offsetted contour even number of times. + assert(sil.intersections[i].type == SegmentIntersection::OUTER_LOW); + size_t j = i + 1; + assert(j < sil.intersections.size()); + assert(sil.intersections[j].type == SegmentIntersection::INNER_LOW || sil.intersections[j].type == SegmentIntersection::OUTER_HIGH); + for (; j < sil.intersections.size() && sil.intersections[j].is_inner(); ++ j) ; + assert(j < sil.intersections.size()); + assert((j & 1) == 1); + assert(sil.intersections[j].type == SegmentIntersection::OUTER_HIGH); + assert(i + 1 == j || sil.intersections[j - 1].type == SegmentIntersection::INNER_HIGH); + i = j + 1; + } + } #endif /* SLIC3R_DEBUG */ // Now construct a graph. @@ -798,8 +928,9 @@ Polylines FillRectilinear2::fill_surface(const Surface *surface, const FillParam // Follow the line, connect the lines into a graph. // Until no new line could be added to the output path: Point pointLast; - Polylines polylines_out; Polyline *polyline_current = NULL; + if (! polylines_out.empty()) + pointLast = polylines_out.back().points.back(); for (;;) { if (i_intersection == size_t(-1)) { // The path has been interrupted. Find a next starting point, closest to the previous extruder position. @@ -901,31 +1032,81 @@ Polylines FillRectilinear2::fill_surface(const Surface *surface, const FillParam int iPrev = intersection_unused_on_prev_vertical_line(poly_with_offset, segs, i_vline, intrsctn->iContour, i_intersection); int iNext = intersection_unused_on_next_vertical_line(poly_with_offset, segs, i_vline, intrsctn->iContour, i_intersection); if (iPrev != -1 || iNext != -1) { - // Zig zag - coord_t distPrev = (iPrev == -1) ? std::numeric_limits<coord_t>::max() : - measure_perimeter_prev_segment_length(poly_with_offset, segs, i_vline, intrsctn->iContour, i_intersection, iPrev); - coord_t distNext = (iNext == -1) ? std::numeric_limits<coord_t>::max() : - measure_perimeter_next_segment_length(poly_with_offset, segs, i_vline, intrsctn->iContour, i_intersection, iNext); - // Take the shorter path. - bool take_next = (iPrev != -1 && iNext != -1) ? (distNext < distPrev) : distNext != -1; - assert(intrsctn->is_inner()); - polyline_current->points.push_back(Point(seg.pos, intrsctn->pos)); - emit_perimeter_prev_next_segment(poly_with_offset, segs, i_vline, intrsctn->iContour, i_intersection, take_next ? iNext : iPrev, *polyline_current, take_next); - // Mark both the left and right connecting segment as consumed, because one cannot go to this intersection point as it has been consumed. - if (iPrev != -1) - segs[i_vline-1].intersections[iPrev].consumed_perimeter_right = true; - if (iNext != -1) - intrsctn->consumed_perimeter_right = true; - //FIXME consume the left / right connecting segments at the other end of this line? Currently it is not critical because a perimeter segment is not followed if the vertical segment at the other side has already been consumed. - // Advance to the neighbor line. - if (take_next) { - ++ i_vline; - i_intersection = iNext; - } else { - -- i_vline; - i_intersection = iPrev; + // Does the perimeter intersect the current vertical line? + SegmentIntersection::SegmentIntersectionType type_crossing = (intrsctn->type == SegmentIntersection::INNER_LOW) ? + SegmentIntersection::INNER_HIGH : SegmentIntersection::INNER_LOW; + // Does the perimeter intersect the current vertical line above intrsctn? + int iSegAbove = -1; + for (size_t i = i_intersection + 1; i + 1 < seg.intersections.size(); ++ i) + if (seg.intersections[i].iContour == intrsctn->iContour && + seg.intersections[i].type == type_crossing) { + iSegAbove = seg.intersections[i].iSegment; + break; + } + // Does the perimeter intersect the current vertical line below intrsctn? + int iSegBelow = -1; + for (size_t i = i_intersection - 1; i > 0; -- i) + if (seg.intersections[i].iContour == intrsctn->iContour && + seg.intersections[i].type == type_crossing) { + iSegBelow = seg.intersections[i].iSegment; + break; + } + if (iSegBelow != -1 || iSegAbove != -1) { + // Invalidate iPrev resp. iNext, if the perimeter crosses the current vertical line earlier than iPrev resp. iNext. + // The perimeter contour orientation. + const bool forward = intrsctn->is_low(); // == poly_with_offset.is_contour_ccw(intrsctn->iContour); + const Polygon &poly = poly_with_offset.contour(intrsctn->iContour); + if (iPrev != -1) { + int d1 = distance_of_segmens(poly, segs[i_vline-1].intersections[iPrev].iSegment, intrsctn->iSegment, forward); + int d2 = (iSegBelow == -1) ? std::numeric_limits<int>::max() : + distance_of_segmens(poly, iSegBelow, intrsctn->iSegment, forward); + if (iSegAbove != -1) + d2 = std::min(d2, distance_of_segmens(poly, iSegAbove, intrsctn->iSegment, forward)); + if (d2 < d1) + // The vertical crossing comes eralier than the prev crossing. + // Disable the perimeter going back. + iPrev = -1; + } + if (iNext != -1) { + int d1 = distance_of_segmens(poly, intrsctn->iSegment, segs[i_vline+1].intersections[iNext].iSegment, forward); + int d2 = (iSegBelow == -1) ? std::numeric_limits<int>::max() : + distance_of_segmens(poly, intrsctn->iSegment, iSegBelow, forward); + if (iSegAbove != -1) + d2 = std::min(d2, distance_of_segmens(poly, intrsctn->iSegment, iSegAbove, forward)); + if (d2 < d1) + // The vertical crossing comes eralier than the prev crossing. + // Disable the perimeter going forward. + iNext = -1; + } + } + if (iPrev != -1 || iNext != -1) { + // Zig zag + coord_t distPrev = (iPrev == -1) ? std::numeric_limits<coord_t>::max() : + measure_perimeter_prev_segment_length(poly_with_offset, segs, i_vline, intrsctn->iContour, i_intersection, iPrev); + coord_t distNext = (iNext == -1) ? std::numeric_limits<coord_t>::max() : + measure_perimeter_next_segment_length(poly_with_offset, segs, i_vline, intrsctn->iContour, i_intersection, iNext); + // Take the shorter path. + bool take_next = (iPrev != -1 && iNext != -1) ? (distNext < distPrev) : iNext != -1; + assert(intrsctn->is_inner()); + pointLast = Point(seg.pos, intrsctn->pos); + polyline_current->points.push_back(pointLast); + emit_perimeter_prev_next_segment(poly_with_offset, segs, i_vline, intrsctn->iContour, i_intersection, take_next ? iNext : iPrev, *polyline_current, take_next); + // Mark both the left and right connecting segment as consumed, because one cannot go to this intersection point as it has been consumed. + if (iPrev != -1) + segs[i_vline-1].intersections[iPrev].consumed_perimeter_right = true; + if (iNext != -1) + intrsctn->consumed_perimeter_right = true; + //FIXME consume the left / right connecting segments at the other end of this line? Currently it is not critical because a perimeter segment is not followed if the vertical segment at the other side has already been consumed. + // Advance to the neighbor line. + if (take_next) { + ++ i_vline; + i_intersection = iNext; + } else { + -- i_vline; + i_intersection = iPrev; + } + continue; } - continue; } // Take the complete line up to the outer contour. if (going_up) @@ -939,17 +1120,65 @@ Polylines FillRectilinear2::fill_surface(const Surface *surface, const FillParam assert(intrsctn->is_high() == going_up); pointLast = Point(seg.pos, intrsctn->pos); polyline_current->points.push_back(pointLast); + // Handle duplicate points and zero length segments. + polyline_current->remove_duplicate_points(); + assert(! polyline_current->has_duplicate_points()); + // Handle nearly zero length edges. + if (polyline_current->points.size() <= 1 || + (polyline_current->points.size() == 2 && + std::abs(polyline_current->points.front().x - polyline_current->points.back().x) < SCALED_EPSILON && + std::abs(polyline_current->points.front().y - polyline_current->points.back().y) < SCALED_EPSILON)) + polylines_out.pop_back(); intrsctn = NULL; i_intersection = -1; polyline_current = NULL; } +#ifdef SLIC3R_DEBUG + { + sprintf(path, "out/FillRectilinear2-final-%d.svg", iRun); + ::Slic3r::SVG svg(path, bbox_svg); // , scale_(1.)); + for (size_t i = 0; i < poly_with_offset.polygons_src.size(); ++ i) + svg.draw(poly_with_offset.polygons_src[i].lines()); + for (size_t i = 0; i < poly_with_offset.polygons_outer.size(); ++ i) + svg.draw(poly_with_offset.polygons_outer[i].lines(), "green"); + for (size_t i = 0; i < poly_with_offset.polygons_inner.size(); ++ i) + svg.draw(poly_with_offset.polygons_inner[i].lines(), "brown"); + for (size_t i = n_polylines_out_initial; i < polylines_out.size(); ++ i) + svg.draw(polylines_out[i].lines(), "black"); + } +#endif /* SLIC3R_DEBUG */ + // paths must be rotated back - for (Polylines::iterator it = polylines_out.begin(); it != polylines_out.end(); ++ it) { + for (Polylines::iterator it = polylines_out.begin() + n_polylines_out_initial; it != polylines_out.end(); ++ it) { // No need to translate, the absolute position is irrelevant. // it->translate(- rotate_vector.second.x, - rotate_vector.second.y); + assert(! it->has_duplicate_points()); it->rotate(rotate_vector.first); + //FIXME rather simplify the paths to avoid very short edges? + //assert(! it->has_duplicate_points()); + it->remove_duplicate_points(); } + +#ifdef SLIC3R_DEBUG + // Verify, that there are no duplicate points in the sequence. + for (Polylines::iterator it = polylines_out.begin(); it != polylines_out.end(); ++ it) + assert(! it->has_duplicate_points()); +#endif /* SLIC3R_DEBUG */ +} + +Polylines FillRectilinear2::fill_surface(const Surface *surface, const FillParams ¶ms) +{ + Polylines polylines_out; + fill_surface_by_lines(surface, params, 0.f, polylines_out); + return polylines_out; +} + +Polylines FillGrid2::fill_surface(const Surface *surface, const FillParams ¶ms) +{ + Polylines polylines_out; + fill_surface_by_lines(surface, params, 0.f, polylines_out); + fill_surface_by_lines(surface, params, float(M_PI / 2.), polylines_out); return polylines_out; } diff --git a/xs/src/libslic3r/Fill/FillRectilinear2.hpp b/xs/src/libslic3r/Fill/FillRectilinear2.hpp index 0c4473e4a..196563a2b 100644 --- a/xs/src/libslic3r/Fill/FillRectilinear2.hpp +++ b/xs/src/libslic3r/Fill/FillRectilinear2.hpp @@ -9,13 +9,15 @@ namespace Slic3r { class Surface; -class FillRectilinear2 : public FillWithDirection +class FillRectilinear2 : public Fill { public: virtual ~FillRectilinear2() {} virtual Polylines fill_surface(const Surface *surface, const FillParams ¶ms); protected: + void fill_surface_by_lines(const Surface *surface, const FillParams ¶ms, float angleBase, Polylines &polylines_out); + coord_t _min_spacing; coord_t _line_spacing; // distance threshold for allowing the horizontal infill lines to be connected into a continuous path @@ -26,6 +28,7 @@ class FillGrid2 : public FillRectilinear2 { public: virtual ~FillGrid2() {} + virtual Polylines fill_surface(const Surface *surface, const FillParams ¶ms); protected: // The grid fill will keep the angle constant between the layers, see the implementation of Slic3r::Fill::Base. diff --git a/xs/src/libslic3r/Geometry.cpp b/xs/src/libslic3r/Geometry.cpp index a6171881a..ac2557374 100644 --- a/xs/src/libslic3r/Geometry.cpp +++ b/xs/src/libslic3r/Geometry.cpp @@ -593,12 +593,7 @@ static inline void dump_voronoi_to_svg(const Lines &lines, /* const */ voronoi_d const bool primaryEdgesOnly = false; BoundingBox bbox = BoundingBox(lines); - bbox.min.x -= coord_t(1. / SCALING_FACTOR); - bbox.min.y -= coord_t(1. / SCALING_FACTOR); - bbox.max.x += coord_t(1. / SCALING_FACTOR); - bbox.max.y += coord_t(1. / SCALING_FACTOR); - - ::Slic3r::SVG svg(path, bbox); + ::Slic3r::SVG svg(path, bbox, scale_(1.)); if (polylines != NULL) svg.draw(*polylines, "lime", "lime", voronoiLineWidth); diff --git a/xs/src/libslic3r/MultiPoint.cpp b/xs/src/libslic3r/MultiPoint.cpp index 627420628..919eaedf9 100644 --- a/xs/src/libslic3r/MultiPoint.cpp +++ b/xs/src/libslic3r/MultiPoint.cpp @@ -46,8 +46,13 @@ MultiPoint::rotate(double angle) void MultiPoint::rotate(double angle, const Point ¢er) { + double s = sin(angle); + double c = cos(angle); for (Points::iterator it = points.begin(); it != points.end(); ++it) { - (*it).rotate(angle, center); + double cur_x = (double)it->x; + double cur_y = (double)it->y; + it->x = (coord_t)round((double)center.x + c * (cur_x - (double)center.x) - s * (cur_y - (double)center.y)); + it->y = (coord_t)round((double)center.y + c * (cur_y - (double)center.y) + s * (cur_x - (double)center.x)); } } @@ -195,4 +200,9 @@ MultiPoint::_douglas_peucker(const Points &points, const double tolerance) return results; } +BoundingBox get_extents(const MultiPoint &mp) +{ + return mp.bounding_box(); +} + } diff --git a/xs/src/libslic3r/MultiPoint.hpp b/xs/src/libslic3r/MultiPoint.hpp index e6fe4f2f2..6464a68c0 100644 --- a/xs/src/libslic3r/MultiPoint.hpp +++ b/xs/src/libslic3r/MultiPoint.hpp @@ -45,6 +45,8 @@ class MultiPoint static Points _douglas_peucker(const Points &points, const double tolerance); }; +extern BoundingBox get_extents(const MultiPoint &mp); + } #endif diff --git a/xs/src/libslic3r/PerimeterGenerator.cpp b/xs/src/libslic3r/PerimeterGenerator.cpp index d7fdcbfae..689c179bc 100644 --- a/xs/src/libslic3r/PerimeterGenerator.cpp +++ b/xs/src/libslic3r/PerimeterGenerator.cpp @@ -1,14 +1,32 @@ +#include "BoundingBox.hpp" +#include "Polygon.hpp" #include "PerimeterGenerator.hpp" #include "ClipperUtils.hpp" #include "ExtrusionEntityCollection.hpp" #include <cmath> #include <cassert> +// #define PRECISE_INFILL + +#define PERIMETER_GENERATOR_DEBUG + +#ifdef PERIMETER_GENERATOR_DEBUG +#include "SVG.hpp" +#endif /* PERIMETER_GENERATOR_DEBUG */ + namespace Slic3r { + + void PerimeterGenerator::process() { +#ifdef PERIMETER_GENERATOR_DEBUG + static int iRunPerimeter = 0; + ++ iRunPerimeter; + char path[2048]; +#endif /* PERIMETER_GENERATOR_DEBUG */ + // other perimeters this->_mm3_per_mm = this->perimeter_flow.mm3_per_mm(); coord_t pwidth = this->perimeter_flow.scaled_width(); @@ -53,6 +71,11 @@ PerimeterGenerator::process() // extra perimeters for each one for (Surfaces::const_iterator surface = this->slices->surfaces.begin(); surface != this->slices->surfaces.end(); ++surface) { +#ifdef PERIMETER_GENERATOR_DEBUG + static int iSurface = 0; + ++ iSurface; +#endif /* PERIMETER_GENERATOR_DEBUG */ + // detect how many perimeters must be generated for this island signed short loop_number = this->config->perimeters + surface->extra_perimeters; loop_number--; // 0-indexed loops @@ -60,6 +83,15 @@ PerimeterGenerator::process() Polygons gaps; Polygons last = surface->expolygon.simplify_p(SCALED_RESOLUTION); +#ifdef PERIMETER_GENERATOR_DEBUG + sprintf(path, "out\\perimeter-%d-%d-initial.svg", iRunPerimeter, iSurface); + BoundingBox bbox = get_extents(last); + { + SVG svg(path, bbox, scale_(1.), true); + svg.draw_outline(last); + } +#endif /* PERIMETER_GENERATOR_DEBUG */ + if (loop_number >= 0) { // no loops = -1 std::vector<PerimeterGeneratorLoops> contours(loop_number+1); // depth => loops @@ -67,7 +99,12 @@ PerimeterGenerator::process() ThickPolylines thin_walls; // we loop one time more than needed in order to find gaps after the last perimeter was applied +//FIXME do we want to have a precise infill, or have an approximate infill and fill the gaps? +#ifdef PRECISE_INFILL + for (signed short i = 0; i <= loop_number; ++i) { // outer loop is 0 +#else for (signed short i = 0; i <= loop_number+1; ++i) { // outer loop is 0 +#endif Polygons offsets; if (i == 0) { // the minimum thickness of a single loop is: @@ -143,6 +180,15 @@ PerimeterGenerator::process() gaps.insert(gaps.end(), diff_pp.begin(), diff_pp.end()); } } + +#ifdef PERIMETER_GENERATOR_DEBUG + sprintf(path, "out\\perimeter-%d-%d-loop-%d.svg", iRunPerimeter, iSurface, i); + BoundingBox bbox = get_extents(last); + { + SVG svg(path, bbox, scale_(1.), true); + svg.draw_outline(offsets); + } +#endif /* PERIMETER_GENERATOR_DEBUG */ if (offsets.empty()) break; if (i > loop_number) break; // we were only looking for gaps this time @@ -299,6 +345,9 @@ PerimeterGenerator::process() for (ExPolygons::const_iterator ex = expp.begin(); ex != expp.end(); ++ex) ex->simplify_p(SCALED_RESOLUTION, &pp); + #ifdef PRECISE_INFILL + expp = offset_ex(pp, -inset); + #else // collapse too narrow infill areas coord_t min_perimeter_infill_spacing = ispacing * (1 - INSET_OVERLAP_TOLERANCE); expp = offset2_ex( @@ -306,7 +355,8 @@ PerimeterGenerator::process() -inset -min_perimeter_infill_spacing/2, +min_perimeter_infill_spacing/2 ); - + #endif + // append infill areas to fill_surfaces for (ExPolygons::const_iterator ex = expp.begin(); ex != expp.end(); ++ex) this->fill_surfaces->surfaces.push_back(Surface(stInternal, *ex)); // use a bogus surface type diff --git a/xs/src/libslic3r/PerimeterGenerator.hpp b/xs/src/libslic3r/PerimeterGenerator.hpp index eb3fa0fcb..68926d148 100644 --- a/xs/src/libslic3r/PerimeterGenerator.hpp +++ b/xs/src/libslic3r/PerimeterGenerator.hpp @@ -2,6 +2,7 @@ #define slic3r_PerimeterGenerator_hpp_ #include "libslic3r.h" +// #include "clipper.hpp" #include <vector> #include "ExPolygonCollection.hpp" #include "Flow.hpp" diff --git a/xs/src/libslic3r/Polygon.cpp b/xs/src/libslic3r/Polygon.cpp index 102838809..4ce7c9701 100644 --- a/xs/src/libslic3r/Polygon.cpp +++ b/xs/src/libslic3r/Polygon.cpp @@ -1,3 +1,4 @@ +#include "BoundingBox.hpp" #include "ClipperUtils.hpp" #include "Polygon.hpp" #include "Polyline.hpp" @@ -265,4 +266,20 @@ Polygon::convex_points(double angle) const return points; } +BoundingBox get_extents(const Polygon &poly) +{ + return poly.bounding_box(); +} + +BoundingBox get_extents(const Polygons &polygons) +{ + BoundingBox bb; + if (! polygons.empty()) { + bb = polygons.front().bounding_box(); + for (size_t i = 1; i < polygons.size(); ++ i) + bb.merge(polygons[i]); + } + return bb; +} + } diff --git a/xs/src/libslic3r/Polygon.hpp b/xs/src/libslic3r/Polygon.hpp index ccde4a740..3ccc0b8e1 100644 --- a/xs/src/libslic3r/Polygon.hpp +++ b/xs/src/libslic3r/Polygon.hpp @@ -44,6 +44,9 @@ class Polygon : public MultiPoint { Points convex_points(double angle = PI) const; }; +extern BoundingBox get_extents(const Polygon &poly); +extern BoundingBox get_extents(const Polygons &polygons); + } // start Boost diff --git a/xs/src/libslic3r/SVG.cpp b/xs/src/libslic3r/SVG.cpp index 259c6eacf..af4947e51 100644 --- a/xs/src/libslic3r/SVG.cpp +++ b/xs/src/libslic3r/SVG.cpp @@ -1,12 +1,12 @@ #include "SVG.hpp" #include <iostream> -#define COORD(x) ((float)unscale(x)*10) +#define COORD(x) ((float)unscale((x))*10) namespace Slic3r { SVG::SVG(const char* filename) - : arrows(false), fill("grey"), stroke("black"), filename(filename) + : arrows(false), fill("grey"), stroke("black"), filename(filename), flipY(false) { this->f = fopen(filename, "w"); fprintf(this->f, @@ -19,12 +19,12 @@ SVG::SVG(const char* filename) ); } -SVG::SVG(const char* filename, const BoundingBox &bbox) - : arrows(false), fill("grey"), stroke("black"), filename(filename), origin(bbox.min) +SVG::SVG(const char* filename, const BoundingBox &bbox, const coord_t bbox_offset, bool aflipY) + : arrows(false), fill("grey"), stroke("black"), filename(filename), origin(bbox.min - Point(bbox_offset, bbox_offset)), flipY(aflipY) { this->f = fopen(filename, "w"); - float w = COORD(bbox.max.x - bbox.min.x); - float h = COORD(bbox.max.y - bbox.min.y); + float w = COORD(bbox.max.x - bbox.min.x + 2 * bbox_offset); + float h = COORD(bbox.max.y - bbox.min.y + 2 * bbox_offset); fprintf(this->f, "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.0//EN\" \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\n" @@ -36,7 +36,7 @@ SVG::SVG(const char* filename, const BoundingBox &bbox) } void -SVG::draw(const Line &line, std::string stroke, coord_t stroke_width) +SVG::draw(const Line &line, std::string stroke, coordf_t stroke_width) { fprintf(this->f, " <line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" style=\"stroke: %s; stroke-width: %f\"", @@ -46,7 +46,7 @@ SVG::draw(const Line &line, std::string stroke, coord_t stroke_width) fprintf(this->f, "/>\n"); } -void SVG::draw(const ThickLine &line, const std::string &fill, const std::string &stroke, coord_t stroke_width) +void SVG::draw(const ThickLine &line, const std::string &fill, const std::string &stroke, coordf_t stroke_width) { Pointf dir(line.b.x-line.a.x, line.b.y-line.a.y); Pointf perp(-dir.y, dir.x); @@ -68,10 +68,10 @@ void SVG::draw(const ThickLine &line, const std::string &fill, const std::string } void -SVG::draw(const Lines &lines, std::string stroke) +SVG::draw(const Lines &lines, std::string stroke, coordf_t stroke_width) { for (Lines::const_iterator it = lines.begin(); it != lines.end(); ++it) - this->draw(*it, stroke); + this->draw(*it, stroke, stroke_width); } void @@ -91,7 +91,7 @@ SVG::draw(const ExPolygon &expolygon, std::string fill) for (Polygons::const_iterator p = pp.begin(); p != pp.end(); ++p) { d += this->get_path_d(*p, true) + " "; } - this->path(d, true); + this->path(d, true, 0); } void @@ -105,7 +105,7 @@ void SVG::draw(const Polygon &polygon, std::string fill) { this->fill = fill; - this->path(this->get_path_d(polygon, true), !fill.empty()); + this->path(this->get_path_d(polygon, true), !fill.empty(), 0); } void @@ -116,34 +116,34 @@ SVG::draw(const Polygons &polygons, std::string fill) } void -SVG::draw(const Polyline &polyline, std::string stroke, coord_t stroke_width) +SVG::draw(const Polyline &polyline, std::string stroke, coordf_t stroke_width) { this->stroke = stroke; this->path(this->get_path_d(polyline, false), false, stroke_width); } void -SVG::draw(const Polylines &polylines, std::string stroke, coord_t stroke_width) +SVG::draw(const Polylines &polylines, std::string stroke, coordf_t stroke_width) { for (Polylines::const_iterator it = polylines.begin(); it != polylines.end(); ++it) this->draw(*it, fill, stroke_width); } -void SVG::draw(const ThickLines &thicklines, const std::string &fill, const std::string &stroke, coord_t stroke_width) +void SVG::draw(const ThickLines &thicklines, const std::string &fill, const std::string &stroke, coordf_t stroke_width) { for (ThickLines::const_iterator it = thicklines.begin(); it != thicklines.end(); ++it) this->draw(*it, fill, stroke, stroke_width); } void -SVG::draw(const ThickPolylines &polylines, const std::string &stroke, coord_t stroke_width) +SVG::draw(const ThickPolylines &polylines, const std::string &stroke, coordf_t stroke_width) { for (ThickPolylines::const_iterator it = polylines.begin(); it != polylines.end(); ++it) this->draw((Polyline)*it, stroke, stroke_width); } void -SVG::draw(const ThickPolylines &thickpolylines, const std::string &fill, const std::string &stroke, coord_t stroke_width) +SVG::draw(const ThickPolylines &thickpolylines, const std::string &fill, const std::string &stroke, coordf_t stroke_width) { for (ThickPolylines::const_iterator it = thickpolylines.begin(); it != thickpolylines.end(); ++ it) draw(it->thicklines(), fill, stroke, stroke_width); @@ -168,8 +168,36 @@ SVG::draw(const Points &points, std::string fill, coord_t radius) this->draw(*it, fill, radius); } +void +SVG::draw(const ClipperLib::Path &polygon, double scale, std::string stroke, coordf_t stroke_width) +{ + this->stroke = stroke; + this->path(this->get_path_d(polygon, scale, true), false, stroke_width); +} + +void +SVG::draw(const ClipperLib::Paths &polygons, double scale, std::string stroke, coordf_t stroke_width) +{ + for (ClipperLib::Paths::const_iterator it = polygons.begin(); it != polygons.end(); ++ it) + draw(*it, scale, stroke, stroke_width); +} + +void +SVG::draw_outline(const Polygon &polygon, std::string stroke, coordf_t stroke_width) +{ + this->stroke = stroke; + this->path(this->get_path_d(polygon, true), false, stroke_width); +} + +void +SVG::draw_outline(const Polygons &polygons, std::string stroke, coordf_t stroke_width) +{ + for (Polygons::const_iterator it = polygons.begin(); it != polygons.end(); ++ it) + draw_outline(*it, stroke, stroke_width); +} + void -SVG::path(const std::string &d, bool fill, coord_t stroke_width) +SVG::path(const std::string &d, bool fill, coordf_t stroke_width) { float lineWidth = 0.f; if (! fill) @@ -199,6 +227,19 @@ SVG::get_path_d(const MultiPoint &mp, bool closed) const return d.str(); } +std::string +SVG::get_path_d(const ClipperLib::Path &path, double scale, bool closed) const +{ + std::ostringstream d; + d << "M "; + for (ClipperLib::Path::const_iterator p = path.begin(); p != path.end(); ++p) { + d << COORD(scale * p->X - origin.x) << " "; + d << COORD(scale * p->Y - origin.y) << " "; + } + if (closed) d << "z"; + return d.str(); +} + void SVG::Close() { diff --git a/xs/src/libslic3r/SVG.hpp b/xs/src/libslic3r/SVG.hpp index aa08c0e20..5388b1b03 100644 --- a/xs/src/libslic3r/SVG.hpp +++ b/xs/src/libslic3r/SVG.hpp @@ -2,6 +2,7 @@ #define slic3r_SVG_hpp_ #include "libslic3r.h" +#include "clipper.hpp" #include "ExPolygon.hpp" #include "Line.hpp" #include "TriangleMesh.hpp" @@ -14,34 +15,43 @@ class SVG bool arrows; std::string fill, stroke; Point origin; + bool flipY; SVG(const char* filename); - SVG(const char* filename, const BoundingBox &bbox); + SVG(const char* filename, const BoundingBox &bbox, const coord_t bbox_offset = scale_(1.), bool flipY = false); ~SVG() { if (f != NULL) Close(); } - void draw(const Line &line, std::string stroke = "black", coord_t stroke_width = 0); - void draw(const ThickLine &line, const std::string &fill, const std::string &stroke, coord_t stroke_width = 0); - void draw(const Lines &lines, std::string stroke = "black"); + void draw(const Line &line, std::string stroke = "black", coordf_t stroke_width = 0); + void draw(const ThickLine &line, const std::string &fill, const std::string &stroke, coordf_t stroke_width = 0); + void draw(const Lines &lines, std::string stroke = "black", coordf_t stroke_width = 0); void draw(const IntersectionLines &lines, std::string stroke = "black"); void draw(const ExPolygon &expolygon, std::string fill = "grey"); void draw(const ExPolygons &expolygons, std::string fill = "grey"); void draw(const Polygon &polygon, std::string fill = "grey"); + void draw_outline(const Polygon &polygon, std::string stroke = "black", coordf_t stroke_width = 0); void draw(const Polygons &polygons, std::string fill = "grey"); - void draw(const Polyline &polyline, std::string stroke = "black", coord_t stroke_width = 0); - void draw(const Polylines &polylines, std::string stroke = "black", coord_t stroke_width = 0); - void draw(const ThickLines &thicklines, const std::string &fill = "lime", const std::string &stroke = "black", coord_t stroke_width = 0); - void draw(const ThickPolylines &polylines, const std::string &stroke = "black", coord_t stroke_width = 0); - void draw(const ThickPolylines &thickpolylines, const std::string &fill, const std::string &stroke, coord_t stroke_width); + void draw_outline(const Polygons &polygons, std::string stroke = "black", coordf_t stroke_width = 0); + void draw(const Polyline &polyline, std::string stroke = "black", coordf_t stroke_width = 0); + void draw(const Polylines &polylines, std::string stroke = "black", coordf_t stroke_width = 0); + void draw(const ThickLines &thicklines, const std::string &fill = "lime", const std::string &stroke = "black", coordf_t stroke_width = 0); + void draw(const ThickPolylines &polylines, const std::string &stroke = "black", coordf_t stroke_width = 0); + void draw(const ThickPolylines &thickpolylines, const std::string &fill, const std::string &stroke, coordf_t stroke_width); void draw(const Point &point, std::string fill = "black", coord_t radius = 0); void draw(const Points &points, std::string fill = "black", coord_t radius = 0); + + // Support for rendering the ClipperLib paths + void draw(const ClipperLib::Path &polygon, double scale, std::string fill = "grey", coordf_t stroke_width = 0); + void draw(const ClipperLib::Paths &polygons, double scale, std::string fill = "grey", coordf_t stroke_width = 0); + void Close(); private: std::string filename; FILE* f; - void path(const std::string &d, bool fill, coord_t stroke_width = 0); + void path(const std::string &d, bool fill, coordf_t stroke_width); std::string get_path_d(const MultiPoint &mp, bool closed = false) const; + std::string get_path_d(const ClipperLib::Path &mp, double scale, bool closed = false) const; }; } diff --git a/xs/src/libslic3r/libslic3r.h b/xs/src/libslic3r/libslic3r.h index 6f6e1fdb6..76e354102 100644 --- a/xs/src/libslic3r/libslic3r.h +++ b/xs/src/libslic3r/libslic3r.h @@ -8,12 +8,22 @@ #define SLIC3R_VERSION "1.3.0-dev" +//FIXME This epsilon value is used for many non-related purposes: +// For a threshold of a squared Euclidean distance, +// for a trheshold in a difference of radians, +// for a threshold of a cross product of two non-normalized vectors etc. #define EPSILON 1e-4 +// Scaling factor for a conversion from coord_t to coordf_t: 10e-6 +// This scaling generates a following fixed point representation with for a 32bit integer: +// 0..4294mm with 1nm resolution #define SCALING_FACTOR 0.000001 +// RESOLUTION, SCALED_RESOLUTION: Used as an error threshold for a Douglas-Peucker polyline simplification algorithm. #define RESOLUTION 0.0125 #define SCALED_RESOLUTION (RESOLUTION / SCALING_FACTOR) #define PI 3.141592653589793238 +// When extruding a closed loop, the loop is interrupted and shortened a bit to reduce the seam. #define LOOP_CLIPPING_LENGTH_OVER_NOZZLE_DIAMETER 0.15 +// Maximum perimeter length for the loop to apply the small perimeter speed. #define SMALL_PERIMETER_LENGTH (6.5 / SCALING_FACTOR) * 2 * PI #define INSET_OVERLAP_TOLERANCE 0.4 #define EXTERNAL_INFILL_MARGIN 3 @@ -31,10 +41,62 @@ enum Axis { X, Y, Z }; } using namespace Slic3r; -/* Implementation of CONFESS("foo"): */ -#define CONFESS(...) confess_at(__FILE__, __LINE__, __func__, __VA_ARGS__) -void confess_at(const char *file, int line, const char *func, const char *pat, ...); -/* End implementation of CONFESS("foo"): */ +#if defined(SLIC3R_DEBUG) and defined(_WIN32) + #ifdef __cplusplus + extern "C" { + #endif + __declspec(dllimport) void __stdcall DebugBreak(); + __declspec(dllimport) int __stdcall IsDebuggerPresent(); + __declspec(dllimport) void __stdcall ExitProcess(unsigned int); + #ifdef __cplusplus + } + #endif + + #pragma comment(lib, "user32.lib") + + #include <stdarg.h> + + static inline void assert_fail(const char *assertion, const char *file, unsigned line, const char *function) + { + printf("Assert: %s in function %s\nfile %s:%d\n", assertion, function, file, line); + if (IsDebuggerPresent()) { + DebugBreak(); + } else { + ExitProcess(-1); + } + } + + #undef assert + + #define assert(expr) \ + ((expr) \ + ? static_cast<void>(0) \ + : assert_fail (#expr, __FILE__, __LINE__, __FUNCTION__)) + + static inline void my_confess_at(const char *file, unsigned line, const char *function, const char* format, ...) + { + fprintf(stderr, "Confess: "); + va_list argptr; + va_start(argptr, format); + vfprintf(stderr, format, argptr); + va_end(argptr); + fprintf(stderr, " in function %s\nfile %s:%d\n", function, file, line); + if (IsDebuggerPresent()) { + DebugBreak(); + } else { + ExitProcess(-1); + } + } + + #define CONFESS(...) my_confess_at(__FILE__, __LINE__, __func__, __VA_ARGS__) + +#else + /* Implementation of CONFESS("foo"): */ + #define CONFESS(...) confess_at(__FILE__, __LINE__, __func__, __VA_ARGS__) + void confess_at(const char *file, int line, const char *func, const char *pat, ...); + /* End implementation of CONFESS("foo"): */ +#endif /* SLIC3R_DEBUG */ + // Which C++ version is supported? // For example, could optimized functions with move semantics be used? diff --git a/xs/xsp/Filler.xsp b/xs/xsp/Filler.xsp index b7871664f..d03113bb1 100644 --- a/xs/xsp/Filler.xsp +++ b/xs/xsp/Filler.xsp @@ -3,6 +3,7 @@ %{ #include <xsinit.h> #include "libslic3r/Fill/FillBase.hpp" +#include "libslic3r/PolylineCollection.hpp" %} %name{Slic3r::Filler} class Filler { |