Merge branch 'lh_lightning_infill'

This commit is contained in:
Lukáš Hejl 2022-06-01 21:38:14 +02:00
commit 9dbb9d472c
19 changed files with 523 additions and 232 deletions

View file

@ -11,6 +11,7 @@
#include "FillBase.hpp" #include "FillBase.hpp"
#include "FillRectilinear.hpp" #include "FillRectilinear.hpp"
#include "FillLightning.hpp"
namespace Slic3r { namespace Slic3r {
@ -318,7 +319,7 @@ void export_group_fills_to_svg(const char *path, const std::vector<SurfaceFill>
#endif #endif
// friend to Layer // friend to Layer
void Layer::make_fills(FillAdaptive::Octree* adaptive_fill_octree, FillAdaptive::Octree* support_fill_octree) void Layer::make_fills(FillAdaptive::Octree* adaptive_fill_octree, FillAdaptive::Octree* support_fill_octree, FillLightning::Generator* lightning_generator)
{ {
for (LayerRegion *layerm : m_regions) for (LayerRegion *layerm : m_regions)
layerm->fills.clear(); layerm->fills.clear();
@ -348,6 +349,9 @@ void Layer::make_fills(FillAdaptive::Octree* adaptive_fill_octree, FillAdaptive:
f->angle = surface_fill.params.angle; f->angle = surface_fill.params.angle;
f->adapt_fill_octree = (surface_fill.params.pattern == ipSupportCubic) ? support_fill_octree : adaptive_fill_octree; f->adapt_fill_octree = (surface_fill.params.pattern == ipSupportCubic) ? support_fill_octree : adaptive_fill_octree;
if (surface_fill.params.pattern == ipLightning)
dynamic_cast<FillLightning::Filler*>(f.get())->generator = lightning_generator;
// calculate flow spacing for infill pattern generation // calculate flow spacing for infill pattern generation
bool using_internal_flow = ! surface_fill.surface.is_solid() && ! surface_fill.params.bridge; bool using_internal_flow = ! surface_fill.surface.is_solid() && ! surface_fill.params.bridge;
double link_max_length = 0.; double link_max_length = 0.;

View file

@ -46,9 +46,7 @@ Fill* Fill::new_from_type(const InfillPattern type)
case ipAdaptiveCubic: return new FillAdaptive::Filler(); case ipAdaptiveCubic: return new FillAdaptive::Filler();
case ipSupportCubic: return new FillAdaptive::Filler(); case ipSupportCubic: return new FillAdaptive::Filler();
case ipSupportBase: return new FillSupportBase(); case ipSupportBase: return new FillSupportBase();
#if HAS_LIGHTNING_INFILL
case ipLightning: return new FillLightning::Filler(); case ipLightning: return new FillLightning::Filler();
#endif // HAS_LIGHTNING_INFILL
default: throw Slic3r::InvalidArgument("unknown type"); default: throw Slic3r::InvalidArgument("unknown type");
} }
} }

View file

@ -1,29 +1,34 @@
#include "../Print.hpp" #include "../Print.hpp"
#include "../ShortestPath.hpp"
#include "FillLightning.hpp" #include "FillLightning.hpp"
#include "Lightning/Generator.hpp" #include "Lightning/Generator.hpp"
#include "../Surface.hpp"
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <numeric>
namespace Slic3r::FillLightning { namespace Slic3r::FillLightning {
Polylines Filler::fill_surface(const Surface *surface, const FillParams &params) void Filler::_fill_surface_single(
const FillParams &params,
unsigned int thickness_layers,
const std::pair<float, Point> &direction,
ExPolygon expolygon,
Polylines &polylines_out)
{ {
const Layer &layer = generator->getTreesForLayer(this->layer_id); const Layer &layer = generator->getTreesForLayer(this->layer_id);
return layer.convertToLines(to_polygons(surface->expolygon), generator->infilll_extrusion_width()); Polylines fill_lines = layer.convertToLines(to_polygons(expolygon), scaled<coord_t>(0.5 * this->spacing - this->overlap));
if (params.dont_connect() || fill_lines.size() <= 1) {
append(polylines_out, chain_polylines(std::move(fill_lines)));
} else
connect_infill(std::move(fill_lines), expolygon, polylines_out, this->spacing, params);
} }
void GeneratorDeleter::operator()(Generator *p) { void GeneratorDeleter::operator()(Generator *p) {
delete p; delete p;
} }
GeneratorPtr build_generator(const PrintObject &print_object) GeneratorPtr build_generator(const PrintObject &print_object, const std::function<void()> &throw_on_cancel_callback)
{ {
return GeneratorPtr(new Generator(print_object)); return GeneratorPtr(new Generator(print_object, throw_on_cancel_callback));
} }
} // namespace Slic3r::FillAdaptive } // namespace Slic3r::FillAdaptive

View file

@ -14,7 +14,7 @@ class Generator;
struct GeneratorDeleter { void operator()(Generator *p); }; struct GeneratorDeleter { void operator()(Generator *p); };
using GeneratorPtr = std::unique_ptr<Generator, GeneratorDeleter>; using GeneratorPtr = std::unique_ptr<Generator, GeneratorDeleter>;
GeneratorPtr build_generator(const PrintObject &print_object); GeneratorPtr build_generator(const PrintObject &print_object, const std::function<void()> &throw_on_cancel_callback);
class Filler : public Slic3r::Fill class Filler : public Slic3r::Fill
{ {
@ -24,8 +24,13 @@ public:
Generator *generator { nullptr }; Generator *generator { nullptr };
protected: protected:
Fill* clone() const override { return new Filler(*this); } Fill* clone() const override { return new Filler(*this); }
// Perform the fill.
Polylines fill_surface(const Surface *surface, const FillParams &params) override; void _fill_surface_single(const FillParams &params,
unsigned int thickness_layers,
const std::pair<float, Point> &direction,
ExPolygon expolygon,
Polylines &polylines_out) override;
// Let the G-code export reoder the infill lines. // Let the G-code export reoder the infill lines.
bool no_sort() const override { return false; } bool no_sort() const override { return false; }
}; };

View file

@ -406,13 +406,15 @@ public:
// for the infill pattern, don't cut the corners. // for the infill pattern, don't cut the corners.
// default miterLimt = 3 // default miterLimt = 3
//double miterLimit = 10.; //double miterLimit = 10.;
assert(aoffset1 < 0); // FIXME: Resolve properly the cases when it is constructed with aoffset1 = 0 and aoffset2 = 0,
// that is used in sample_grid_pattern() for Lightning infill.
// assert(aoffset1 < 0);
assert(aoffset2 <= 0); assert(aoffset2 <= 0);
assert(aoffset2 == 0 || aoffset2 < aoffset1); // assert(aoffset2 == 0 || aoffset2 < aoffset1);
// bool sticks_removed = // bool sticks_removed =
remove_sticks(polygons_src); remove_sticks(polygons_src);
// if (sticks_removed) BOOST_LOG_TRIVIAL(error) << "Sticks removed!"; // if (sticks_removed) BOOST_LOG_TRIVIAL(error) << "Sticks removed!";
polygons_outer = offset(polygons_src, float(aoffset1), ClipperLib::jtMiter, miterLimit); polygons_outer = aoffset1 == 0 ? polygons_src : offset(polygons_src, float(aoffset1), ClipperLib::jtMiter, miterLimit);
if (aoffset2 < 0) if (aoffset2 < 0)
polygons_inner = shrink(polygons_outer, float(aoffset1 - aoffset2), ClipperLib::jtMiter, miterLimit); polygons_inner = shrink(polygons_outer, float(aoffset1 - aoffset2), ClipperLib::jtMiter, miterLimit);
// Filter out contours with zero area or small area, contours with 2 points only. // Filter out contours with zero area or small area, contours with 2 points only.

View file

@ -5,44 +5,60 @@
#include "../FillRectilinear.hpp" #include "../FillRectilinear.hpp"
#include "../../ClipperUtils.hpp" #include "../../ClipperUtils.hpp"
#include <tbb/parallel_for.h>
namespace Slic3r::FillLightning namespace Slic3r::FillLightning
{ {
constexpr coord_t radius_per_cell_size = 6; // The cell-size should be small compared to the radius, but not so small as to be inefficient. constexpr coord_t radius_per_cell_size = 6; // The cell-size should be small compared to the radius, but not so small as to be inefficient.
DistanceField::DistanceField(const coord_t& radius, const Polygons& current_outline, const Polygons& current_overhang) : DistanceField::DistanceField(const coord_t& radius, const Polygons& current_outline, const BoundingBox& current_outlines_bbox, const Polygons& current_overhang) :
m_cell_size(radius / radius_per_cell_size), m_cell_size(radius / radius_per_cell_size),
m_supporting_radius(radius) m_supporting_radius(radius),
m_unsupported_points_bbox(current_outlines_bbox)
{ {
m_supporting_radius2 = double(radius) * double(radius); m_supporting_radius2 = Slic3r::sqr(int64_t(radius));
// Sample source polygons with a regular grid sampling pattern. // Sample source polygons with a regular grid sampling pattern.
for (const ExPolygon &expoly : union_ex(current_outline)) { for (const ExPolygon &expoly : union_ex(current_overhang)) {
for (const Point &p : sample_grid_pattern(expoly, m_cell_size)) { const Points sampled_points = sample_grid_pattern(expoly, m_cell_size);
// Find a squared distance to the source expolygon boundary. const size_t unsupported_points_prev_size = m_unsupported_points.size();
double d2 = std::numeric_limits<double>::max(); m_unsupported_points.resize(unsupported_points_prev_size + sampled_points.size());
for (size_t icontour = 0; icontour <= expoly.holes.size(); ++ icontour) {
const Polygon &contour = icontour == 0 ? expoly.contour : expoly.holes[icontour - 1]; tbb::parallel_for(tbb::blocked_range<size_t>(0, sampled_points.size()), [&self = *this, &expoly = std::as_const(expoly), &sampled_points = std::as_const(sampled_points), &unsupported_points_prev_size = std::as_const(unsupported_points_prev_size)](const tbb::blocked_range<size_t> &range) -> void {
if (contour.size() > 2) { for (size_t sp_idx = range.begin(); sp_idx < range.end(); ++sp_idx) {
Point prev = contour.points.back(); const Point &sp = sampled_points[sp_idx];
for (const Point &p2 : contour.points) { // Find a squared distance to the source expolygon boundary.
d2 = std::min(d2, Line::distance_to_squared(p, prev, p2)); double d2 = std::numeric_limits<double>::max();
prev = p2; for (size_t icontour = 0; icontour <= expoly.holes.size(); ++icontour) {
const Polygon &contour = icontour == 0 ? expoly.contour : expoly.holes[icontour - 1];
if (contour.size() > 2) {
Point prev = contour.points.back();
for (const Point &p2 : contour.points) {
d2 = std::min(d2, Line::distance_to_squared(sp, prev, p2));
prev = p2;
}
} }
} }
self.m_unsupported_points[unsupported_points_prev_size + sp_idx] = {sp, coord_t(std::sqrt(d2))};
assert(self.m_unsupported_points_bbox.contains(sp));
} }
m_unsupported_points.emplace_back(p, sqrt(d2)); }); // end of parallel_for
}
} }
m_unsupported_points.sort([&radius](const UnsupportedCell &a, const UnsupportedCell &b) { std::stable_sort(m_unsupported_points.begin(), m_unsupported_points.end(), [&radius](const UnsupportedCell &a, const UnsupportedCell &b) {
constexpr coord_t prime_for_hash = 191; constexpr coord_t prime_for_hash = 191;
return std::abs(b.dist_to_boundary - a.dist_to_boundary) > radius ? return std::abs(b.dist_to_boundary - a.dist_to_boundary) > radius ?
a.dist_to_boundary < b.dist_to_boundary : a.dist_to_boundary < b.dist_to_boundary :
(PointHash{}(a.loc) % prime_for_hash) < (PointHash{}(b.loc) % prime_for_hash); (PointHash{}(a.loc) % prime_for_hash) < (PointHash{}(b.loc) % prime_for_hash);
}); });
for (auto it = m_unsupported_points.begin(); it != m_unsupported_points.end(); ++it) {
UnsupportedCell& cell = *it; m_unsupported_points_erased.resize(m_unsupported_points.size());
m_unsupported_points_grid.emplace(Point{ cell.loc.x() / m_cell_size, cell.loc.y() / m_cell_size }, it); std::fill(m_unsupported_points_erased.begin(), m_unsupported_points_erased.end(), false);
}
m_unsupported_points_grid.initialize(m_unsupported_points, [&self = std::as_const(*this)](const Point &p) -> Point { return self.to_grid_point(p); });
// Because the distance between two points is at least one axis equal to m_cell_size, every cell
// in m_unsupported_points_grid contains exactly one point.
assert(m_unsupported_points.size() == m_unsupported_points_grid.size());
} }
void DistanceField::update(const Point& to_node, const Point& added_leaf) void DistanceField::update(const Point& to_node, const Point& added_leaf)
@ -60,17 +76,24 @@ void DistanceField::update(const Point& to_node, const Point& added_leaf)
grid.merge(to_node + iextent); grid.merge(to_node + iextent);
grid.merge(added_leaf - iextent); grid.merge(added_leaf - iextent);
grid.merge(added_leaf + iextent); grid.merge(added_leaf + iextent);
grid.min /= m_cell_size;
grid.max /= m_cell_size; // Clip grid by m_unsupported_points_bbox. Mainly to ensure that grid.min is a non-negative value.
grid.min.x() = std::max(grid.min.x(), m_unsupported_points_bbox.min.x());
grid.min.y() = std::max(grid.min.y(), m_unsupported_points_bbox.min.y());
grid.max.x() = std::min(grid.max.x(), m_unsupported_points_bbox.max.x());
grid.max.y() = std::min(grid.max.y(), m_unsupported_points_bbox.max.y());
grid.min = this->to_grid_point(grid.min);
grid.max = this->to_grid_point(grid.max);
} }
Point grid_addr;
Point grid_loc; Point grid_loc;
for (coord_t row = grid.min.y(); row <= grid.max.y(); ++ row) { for (grid_addr.y() = grid.min.y(); grid_addr.y() <= grid.max.y(); ++grid_addr.y()) {
grid_loc.y() = row * m_cell_size; for (grid_addr.x() = grid.min.x(); grid_addr.x() <= grid.max.x(); ++grid_addr.x()) {
for (coord_t col = grid.min.x(); col <= grid.max.y(); ++ col) { grid_loc = this->from_grid_point(grid_addr);
grid_loc.x() = col * m_cell_size;
// Test inside a circle at the new leaf. // Test inside a circle at the new leaf.
if ((grid_loc - added_leaf).cast<double>().squaredNorm() > m_supporting_radius2) { if ((grid_loc - added_leaf).cast<int64_t>().squaredNorm() > m_supporting_radius2) {
// Not inside a circle at the end of the new leaf. // Not inside a circle at the end of the new leaf.
// Test inside a rotated rectangle. // Test inside a rotated rectangle.
Vec2d vx = (grid_loc - to_node).cast<double>(); Vec2d vx = (grid_loc - to_node).cast<double>();
@ -84,10 +107,29 @@ void DistanceField::update(const Point& to_node, const Point& added_leaf)
} }
// Inside a circle at the end of the new leaf, or inside a rotated rectangle. // Inside a circle at the end of the new leaf, or inside a rotated rectangle.
// Remove unsupported leafs at this grid location. // Remove unsupported leafs at this grid location.
if (auto it = m_unsupported_points_grid.find(grid_loc); it != m_unsupported_points_grid.end()) { if (const size_t cell_idx = m_unsupported_points_grid.find_cell_idx(grid_addr); cell_idx != std::numeric_limits<size_t>::max()) {
std::list<UnsupportedCell>::iterator& list_it = it->second; const UnsupportedCell &cell = m_unsupported_points[cell_idx];
UnsupportedCell& cell = *list_it; if ((cell.loc - added_leaf).cast<int64_t>().squaredNorm() <= m_supporting_radius2) {
if ((cell.loc - added_leaf).cast<double>().squaredNorm() <= m_supporting_radius2) { m_unsupported_points_erased[cell_idx] = true;
m_unsupported_points_grid.mark_erased(grid_addr);
}
}
}
}
}
#if 0
void DistanceField::update(const Point &to_node, const Point &added_leaf)
{
const Point supporting_radius_point(m_supporting_radius, m_supporting_radius);
const BoundingBox grid(this->to_grid_point(added_leaf - supporting_radius_point), this->to_grid_point(added_leaf + supporting_radius_point));
for (coord_t grid_y = grid.min.y(); grid_y <= grid.max.y(); ++grid_y) {
for (coord_t grid_x = grid.min.x(); grid_x <= grid.max.x(); ++grid_x) {
if (auto it = m_unsupported_points_grid.find({grid_x, grid_y}); it != m_unsupported_points_grid.end()) {
std::list<UnsupportedCell>::iterator &list_it = it->second;
UnsupportedCell &cell = *list_it;
if ((cell.loc - added_leaf).cast<int64_t>().squaredNorm() <= m_supporting_radius2) {
m_unsupported_points.erase(list_it); m_unsupported_points.erase(list_it);
m_unsupported_points_grid.erase(it); m_unsupported_points_grid.erase(it);
} }
@ -95,5 +137,6 @@ void DistanceField::update(const Point& to_node, const Point& added_leaf)
} }
} }
} }
#endif
} // namespace Slic3r::FillLightning } // namespace Slic3r::FillLightning

View file

@ -4,6 +4,7 @@
#ifndef LIGHTNING_DISTANCE_FIELD_H #ifndef LIGHTNING_DISTANCE_FIELD_H
#define LIGHTNING_DISTANCE_FIELD_H #define LIGHTNING_DISTANCE_FIELD_H
#include "../../BoundingBox.hpp"
#include "../../Point.hpp" #include "../../Point.hpp"
#include "../../Polygon.hpp" #include "../../Polygon.hpp"
@ -29,7 +30,7 @@ public:
* \param current_overhang The overhang that needs to be supported on this * \param current_overhang The overhang that needs to be supported on this
* layer. * layer.
*/ */
DistanceField(const coord_t& radius, const Polygons& current_outline, const Polygons& current_overhang); DistanceField(const coord_t& radius, const Polygons& current_outline, const BoundingBox& current_outlines_bbox, const Polygons& current_overhang);
/*! /*!
* Gets the next unsupported location to be supported by a new branch. * Gets the next unsupported location to be supported by a new branch.
@ -37,11 +38,17 @@ public:
* \return ``true`` if successful, or ``false`` if there are no more points * \return ``true`` if successful, or ``false`` if there are no more points
* to consider. * to consider.
*/ */
bool tryGetNextPoint(Point* p) const { bool tryGetNextPoint(Point *out_unsupported_location, size_t *out_unsupported_cell_idx, const size_t start_idx = 0) const
if (m_unsupported_points.empty()) {
return false; for (size_t point_idx = start_idx; point_idx < m_unsupported_points.size(); ++point_idx) {
*p = m_unsupported_points.front().loc; if (!m_unsupported_points_erased[point_idx]) {
return true; *out_unsupported_cell_idx = point_idx;
*out_unsupported_location = m_unsupported_points[point_idx].loc;
return true;
}
}
return false;
} }
/*! /*!
@ -69,14 +76,13 @@ protected:
* branch of a tree. * branch of a tree.
*/ */
coord_t m_supporting_radius; coord_t m_supporting_radius;
double m_supporting_radius2; int64_t m_supporting_radius2;
/*! /*!
* Represents a small discrete area of infill that needs to be supported. * Represents a small discrete area of infill that needs to be supported.
*/ */
struct UnsupportedCell struct UnsupportedCell
{ {
UnsupportedCell(Point loc, coord_t dist_to_boundary) : loc(loc), dist_to_boundary(dist_to_boundary) {}
// The position of the center of this cell. // The position of the center of this cell.
Point loc; Point loc;
// How far this cell is removed from the ``current_outline`` polygon, the edge of the infill area. // How far this cell is removed from the ``current_outline`` polygon, the edge of the infill area.
@ -86,13 +92,110 @@ protected:
/*! /*!
* Cells which still need to be supported at some point. * Cells which still need to be supported at some point.
*/ */
std::list<UnsupportedCell> m_unsupported_points; std::vector<UnsupportedCell> m_unsupported_points;
std::vector<bool> m_unsupported_points_erased;
/*!
* BoundingBox of all points in m_unsupported_points. Used for mapping of sign integer numbers to positive integer numbers.
*/
const BoundingBox m_unsupported_points_bbox;
/*! /*!
* Links the unsupported points to a grid point, so that we can quickly look * Links the unsupported points to a grid point, so that we can quickly look
* up the cell belonging to a certain position in the grid. * up the cell belonging to a certain position in the grid.
*/ */
std::unordered_map<Point, std::list<UnsupportedCell>::iterator, PointHash> m_unsupported_points_grid;
class UnsupportedPointsGrid
{
public:
UnsupportedPointsGrid() = default;
void initialize(const std::vector<UnsupportedCell> &unsupported_points, const std::function<Point(const Point &)> &map_cell_to_grid)
{
if (unsupported_points.empty())
return;
BoundingBox unsupported_points_bbox;
for (const UnsupportedCell &cell : unsupported_points)
unsupported_points_bbox.merge(cell.loc);
m_size = unsupported_points.size();
m_grid_range = BoundingBox(map_cell_to_grid(unsupported_points_bbox.min), map_cell_to_grid(unsupported_points_bbox.max));
m_grid_size = m_grid_range.size() + Point::Ones();
m_data.assign(m_grid_size.y() * m_grid_size.x(), std::numeric_limits<size_t>::max());
m_data_erased.assign(m_grid_size.y() * m_grid_size.x(), true);
for (size_t cell_idx = 0; cell_idx < unsupported_points.size(); ++cell_idx) {
const size_t flat_idx = map_to_flat_array(map_cell_to_grid(unsupported_points[cell_idx].loc));
assert(m_data[flat_idx] == std::numeric_limits<size_t>::max());
m_data[flat_idx] = cell_idx;
m_data_erased[flat_idx] = false;
}
}
size_t size() const { return m_size; }
size_t find_cell_idx(const Point &grid_addr)
{
if (!m_grid_range.contains(grid_addr))
return std::numeric_limits<size_t>::max();
if (const size_t flat_idx = map_to_flat_array(grid_addr); !m_data_erased[flat_idx]) {
assert(m_data[flat_idx] != std::numeric_limits<size_t>::max());
return m_data[flat_idx];
}
return std::numeric_limits<size_t>::max();
}
void mark_erased(const Point &grid_addr)
{
assert(m_grid_range.contains(grid_addr));
if (!m_grid_range.contains(grid_addr))
return;
const size_t flat_idx = map_to_flat_array(grid_addr);
assert(!m_data_erased[flat_idx] && m_data[flat_idx] != std::numeric_limits<size_t>::max());
assert(m_size != 0);
m_data_erased[flat_idx] = true;
--m_size;
}
private:
size_t m_size = 0;
BoundingBox m_grid_range;
Point m_grid_size;
std::vector<size_t> m_data;
std::vector<bool> m_data_erased;
inline size_t map_to_flat_array(const Point &loc) const
{
const Point offset_loc = loc - m_grid_range.min;
const size_t flat_idx = m_grid_size.x() * offset_loc.y() + offset_loc.x();
assert(offset_loc.x() >= 0 && offset_loc.y() >= 0);
assert(flat_idx < size_t(m_grid_size.y() * m_grid_size.x()));
return flat_idx;
}
};
UnsupportedPointsGrid m_unsupported_points_grid;
/*!
* Maps the point to the grid coordinates.
*/
Point to_grid_point(const Point &point) const {
return (point - m_unsupported_points_bbox.min) / m_cell_size;
}
/*!
* Maps the point to the grid coordinates.
*/
Point from_grid_point(const Point &point) const {
return point * m_cell_size + m_unsupported_points_bbox.min;
}
}; };
} // namespace Slic3r::FillLightning } // namespace Slic3r::FillLightning

View file

@ -7,7 +7,6 @@
#include "../../ClipperUtils.hpp" #include "../../ClipperUtils.hpp"
#include "../../Layer.hpp" #include "../../Layer.hpp"
#include "../../Print.hpp" #include "../../Print.hpp"
#include "../../Surface.hpp"
/* Possible future tasks/optimizations,etc.: /* Possible future tasks/optimizations,etc.:
* - Improve connecting heuristic to favor connecting to shorter trees * - Improve connecting heuristic to favor connecting to shorter trees
@ -25,7 +24,7 @@
namespace Slic3r::FillLightning { namespace Slic3r::FillLightning {
Generator::Generator(const PrintObject &print_object) Generator::Generator(const PrintObject &print_object, const std::function<void()> &throw_on_cancel_callback)
{ {
const PrintConfig &print_config = print_object.print()->config(); const PrintConfig &print_config = print_object.print()->config();
const PrintObjectConfig &object_config = print_object.config(); const PrintObjectConfig &object_config = print_object.config();
@ -35,38 +34,38 @@ Generator::Generator(const PrintObject &print_object)
// const int infill_extruder = region_config.infill_extruder.value; // const int infill_extruder = region_config.infill_extruder.value;
const double default_infill_extrusion_width = Flow::auto_extrusion_width(FlowRole::frInfill, float(max_nozzle_diameter)); const double default_infill_extrusion_width = Flow::auto_extrusion_width(FlowRole::frInfill, float(max_nozzle_diameter));
// Note: There's not going to be a layer below the first one, so the 'initial layer height' doesn't have to be taken into account. // Note: There's not going to be a layer below the first one, so the 'initial layer height' doesn't have to be taken into account.
const double layer_thickness = object_config.layer_height; const double layer_thickness = scaled<double>(object_config.layer_height.value);
m_infill_extrusion_width = scaled<float>(region_config.infill_extrusion_width.percent ? default_infill_extrusion_width * 0.01 * region_config.infill_extrusion_width : region_config.infill_extrusion_width); m_infill_extrusion_width = scaled<float>(region_config.infill_extrusion_width.percent ? default_infill_extrusion_width * 0.01 * region_config.infill_extrusion_width : region_config.infill_extrusion_width);
m_supporting_radius = scaled<coord_t>(m_infill_extrusion_width * 0.001 / region_config.fill_density); m_supporting_radius = coord_t(m_infill_extrusion_width) * 100 / coord_t(region_config.fill_density.value);
const double lightning_infill_overhang_angle = M_PI / 4; // 45 degrees const double lightning_infill_overhang_angle = M_PI / 4; // 45 degrees
const double lightning_infill_prune_angle = M_PI / 4; // 45 degrees const double lightning_infill_prune_angle = M_PI / 4; // 45 degrees
const double lightning_infill_straightening_angle = M_PI / 4; // 45 degrees const double lightning_infill_straightening_angle = M_PI / 4; // 45 degrees
m_wall_supporting_radius = layer_thickness * std::tan(lightning_infill_overhang_angle); m_wall_supporting_radius = coord_t(layer_thickness * std::tan(lightning_infill_overhang_angle));
m_prune_length = layer_thickness * std::tan(lightning_infill_prune_angle); m_prune_length = coord_t(layer_thickness * std::tan(lightning_infill_prune_angle));
m_straightening_max_distance = layer_thickness * std::tan(lightning_infill_straightening_angle); m_straightening_max_distance = coord_t(layer_thickness * std::tan(lightning_infill_straightening_angle));
generateInitialInternalOverhangs(print_object); generateInitialInternalOverhangs(print_object, throw_on_cancel_callback);
generateTrees(print_object); generateTrees(print_object, throw_on_cancel_callback);
} }
void Generator::generateInitialInternalOverhangs(const PrintObject &print_object) void Generator::generateInitialInternalOverhangs(const PrintObject &print_object, const std::function<void()> &throw_on_cancel_callback)
{ {
m_overhang_per_layer.resize(print_object.layers().size()); m_overhang_per_layer.resize(print_object.layers().size());
const float infill_wall_offset = - m_infill_extrusion_width;
Polygons infill_area_above; Polygons infill_area_above;
//Iterate from top to bottom, to subtract the overhang areas above from the overhang areas on the layer below, to get only overhang in the top layer where it is overhanging. //Iterate from top to bottom, to subtract the overhang areas above from the overhang areas on the layer below, to get only overhang in the top layer where it is overhanging.
for (int layer_nr = print_object.layers().size() - 1; layer_nr >= 0; layer_nr--) { for (int layer_nr = int(print_object.layers().size()) - 1; layer_nr >= 0; --layer_nr) {
throw_on_cancel_callback();
Polygons infill_area_here; Polygons infill_area_here;
for (const LayerRegion* layerm : print_object.get_layer(layer_nr)->regions()) for (const LayerRegion* layerm : print_object.get_layer(layer_nr)->regions())
for (const Surface& surface : layerm->fill_surfaces.surfaces) for (const Surface& surface : layerm->fill_surfaces.surfaces)
if (surface.surface_type == stInternal) if (surface.surface_type == stInternal || surface.surface_type == stInternalVoid)
append(infill_area_here, offset(surface.expolygon, infill_wall_offset)); infill_area_here.emplace_back(surface.expolygon);
//Remove the part of the infill area that is already supported by the walls. //Remove the part of the infill area that is already supported by the walls.
Polygons overhang = diff(offset(infill_area_here, -m_wall_supporting_radius), infill_area_above); Polygons overhang = diff(offset(infill_area_here, -float(m_wall_supporting_radius)), infill_area_above);
m_overhang_per_layer[layer_nr] = overhang; m_overhang_per_layer[layer_nr] = overhang;
infill_area_above = std::move(infill_area_here); infill_area_above = std::move(infill_area_here);
@ -79,19 +78,20 @@ const Layer& Generator::getTreesForLayer(const size_t& layer_id) const
return m_lightning_layers[layer_id]; return m_lightning_layers[layer_id];
} }
void Generator::generateTrees(const PrintObject &print_object) void Generator::generateTrees(const PrintObject &print_object, const std::function<void()> &throw_on_cancel_callback)
{ {
m_lightning_layers.resize(print_object.layers().size()); m_lightning_layers.resize(print_object.layers().size());
const coord_t infill_wall_offset = - m_infill_extrusion_width;
std::vector<Polygons> infill_outlines(print_object.layers().size(), Polygons()); std::vector<Polygons> infill_outlines(print_object.layers().size(), Polygons());
// For-each layer from top to bottom: // For-each layer from top to bottom:
for (int layer_id = print_object.layers().size() - 1; layer_id >= 0; layer_id--) for (int layer_id = int(print_object.layers().size()) - 1; layer_id >= 0; layer_id--) {
throw_on_cancel_callback();
for (const LayerRegion *layerm : print_object.get_layer(layer_id)->regions()) for (const LayerRegion *layerm : print_object.get_layer(layer_id)->regions())
for (const Surface &surface : layerm->fill_surfaces.surfaces) for (const Surface &surface : layerm->fill_surfaces.surfaces)
if (surface.surface_type == stInternal) if (surface.surface_type == stInternal || surface.surface_type == stInternalVoid)
append(infill_outlines[layer_id], offset(surface.expolygon, infill_wall_offset)); infill_outlines[layer_id].emplace_back(surface.expolygon);
}
// For various operations its beneficial to quickly locate nearby features on the polygon: // For various operations its beneficial to quickly locate nearby features on the polygon:
const size_t top_layer_id = print_object.layers().size() - 1; const size_t top_layer_id = print_object.layers().size() - 1;
@ -99,23 +99,31 @@ void Generator::generateTrees(const PrintObject &print_object)
outlines_locator.create(infill_outlines[top_layer_id], locator_cell_size); outlines_locator.create(infill_outlines[top_layer_id], locator_cell_size);
// For-each layer from top to bottom: // For-each layer from top to bottom:
for (int layer_id = top_layer_id; layer_id >= 0; layer_id--) for (int layer_id = int(top_layer_id); layer_id >= 0; layer_id--) {
{ throw_on_cancel_callback();
Layer& current_lightning_layer = m_lightning_layers[layer_id]; Layer &current_lightning_layer = m_lightning_layers[layer_id];
Polygons& current_outlines = infill_outlines[layer_id]; const Polygons &current_outlines = infill_outlines[layer_id];
const BoundingBox &current_outlines_bbox = get_extents(current_outlines);
// register all trees propagated from the previous layer as to-be-reconnected // register all trees propagated from the previous layer as to-be-reconnected
std::vector<NodeSPtr> to_be_reconnected_tree_roots = current_lightning_layer.tree_roots; std::vector<NodeSPtr> to_be_reconnected_tree_roots = current_lightning_layer.tree_roots;
current_lightning_layer.generateNewTrees(m_overhang_per_layer[layer_id], current_outlines, outlines_locator, m_supporting_radius, m_wall_supporting_radius); current_lightning_layer.generateNewTrees(m_overhang_per_layer[layer_id], current_outlines, current_outlines_bbox, outlines_locator, m_supporting_radius, m_wall_supporting_radius, throw_on_cancel_callback);
current_lightning_layer.reconnectRoots(to_be_reconnected_tree_roots, current_outlines, outlines_locator, m_supporting_radius, m_wall_supporting_radius); current_lightning_layer.reconnectRoots(to_be_reconnected_tree_roots, current_outlines, current_outlines_bbox, outlines_locator, m_supporting_radius, m_wall_supporting_radius);
// Initialize trees for next lower layer from the current one. // Initialize trees for next lower layer from the current one.
if (layer_id == 0) if (layer_id == 0)
return; return;
const Polygons& below_outlines = infill_outlines[layer_id - 1]; const Polygons &below_outlines = infill_outlines[layer_id - 1];
outlines_locator.set_bbox(get_extents(below_outlines).inflated(SCALED_EPSILON)); BoundingBox below_outlines_bbox = get_extents(below_outlines).inflated(SCALED_EPSILON);
if (const BoundingBox &outlines_locator_bbox = outlines_locator.bbox(); outlines_locator_bbox.defined)
below_outlines_bbox.merge(outlines_locator_bbox);
if (!current_lightning_layer.tree_roots.empty())
below_outlines_bbox.merge(get_extents(current_lightning_layer.tree_roots).inflated(SCALED_EPSILON));
outlines_locator.set_bbox(below_outlines_bbox);
outlines_locator.create(below_outlines, locator_cell_size); outlines_locator.create(below_outlines, locator_cell_size);
std::vector<NodeSPtr>& lower_trees = m_lightning_layers[layer_id - 1].tree_roots; std::vector<NodeSPtr>& lower_trees = m_lightning_layers[layer_id - 1].tree_roots;

View file

@ -43,9 +43,8 @@ public:
* This generator will pre-compute things in preparation of generating * This generator will pre-compute things in preparation of generating
* Lightning Infill for the infill areas in that mesh. The infill areas must * Lightning Infill for the infill areas in that mesh. The infill areas must
* already be calculated at this point. * already be calculated at this point.
* \param mesh The mesh to generate infill for.
*/ */
Generator(const PrintObject &print_object); explicit Generator(const PrintObject &print_object, const std::function<void()> &throw_on_cancel_callback);
/*! /*!
* Get a tree of paths generated for a certain layer of the mesh. * Get a tree of paths generated for a certain layer of the mesh.
@ -69,12 +68,12 @@ protected:
* only when support is generated. For this pattern, we also need to * only when support is generated. For this pattern, we also need to
* generate overhang areas for the inside of the model. * generate overhang areas for the inside of the model.
*/ */
void generateInitialInternalOverhangs(const PrintObject &print_object); void generateInitialInternalOverhangs(const PrintObject &print_object, const std::function<void()> &throw_on_cancel_callback);
/*! /*!
* Calculate the tree structure of all layers. * Calculate the tree structure of all layers.
*/ */
void generateTrees(const PrintObject &print_object); void generateTrees(const PrintObject &print_object, const std::function<void()> &throw_on_cancel_callback);
float m_infill_extrusion_width; float m_infill_extrusion_width;

View file

@ -3,12 +3,16 @@
#include "Layer.hpp" //The class we're implementing. #include "Layer.hpp" //The class we're implementing.
#include <iterator> // advance
#include "DistanceField.hpp" #include "DistanceField.hpp"
#include "TreeNode.hpp" #include "TreeNode.hpp"
#include "../../ClipperUtils.hpp"
#include "../../Geometry.hpp" #include "../../Geometry.hpp"
#include "Utils.hpp"
#include <tbb/parallel_for.h>
#include <tbb/blocked_range2d.h>
#include <mutex>
namespace Slic3r::FillLightning { namespace Slic3r::FillLightning {
@ -23,10 +27,15 @@ Point GroundingLocation::p() const
return tree_node ? tree_node->getLocation() : *boundary_location; return tree_node ? tree_node->getLocation() : *boundary_location;
} }
void Layer::fillLocator(SparseNodeGrid &tree_node_locator) inline static Point to_grid_point(const Point &point, const BoundingBox &bbox)
{ {
std::function<void(NodeSPtr)> add_node_to_locator_func = [&tree_node_locator](NodeSPtr node) { return (point - bbox.min) / locator_cell_size;
tree_node_locator.insert(std::make_pair(Point(node->getLocation().x() / locator_cell_size, node->getLocation().y() / locator_cell_size), node)); }
void Layer::fillLocator(SparseNodeGrid &tree_node_locator, const BoundingBox& current_outlines_bbox)
{
std::function<void(NodeSPtr)> add_node_to_locator_func = [&tree_node_locator, &current_outlines_bbox](const NodeSPtr &node) {
tree_node_locator.insert(std::make_pair(to_grid_point(node->getLocation(), current_outlines_bbox), node));
}; };
for (auto& tree : tree_roots) for (auto& tree : tree_roots)
tree->visitNodes(add_node_to_locator_func); tree->visitNodes(add_node_to_locator_func);
@ -36,38 +45,50 @@ void Layer::generateNewTrees
( (
const Polygons& current_overhang, const Polygons& current_overhang,
const Polygons& current_outlines, const Polygons& current_outlines,
const BoundingBox& current_outlines_bbox,
const EdgeGrid::Grid& outlines_locator, const EdgeGrid::Grid& outlines_locator,
const coord_t supporting_radius, const coord_t supporting_radius,
const coord_t wall_supporting_radius const coord_t wall_supporting_radius,
const std::function<void()> &throw_on_cancel_callback
) )
{ {
DistanceField distance_field(supporting_radius, current_outlines, current_overhang); DistanceField distance_field(supporting_radius, current_outlines, current_outlines_bbox, current_overhang);
throw_on_cancel_callback();
SparseNodeGrid tree_node_locator; SparseNodeGrid tree_node_locator;
fillLocator(tree_node_locator); fillLocator(tree_node_locator, current_outlines_bbox);
// Until no more points need to be added to support all: // Until no more points need to be added to support all:
// Determine next point from tree/outline areas via distance-field // Determine next point from tree/outline areas via distance-field
Point unsupported_location; size_t unsupported_cell_idx = 0;
while (distance_field.tryGetNextPoint(&unsupported_location)) { Point unsupported_location;
while (distance_field.tryGetNextPoint(&unsupported_location, &unsupported_cell_idx, unsupported_cell_idx)) {
throw_on_cancel_callback();
GroundingLocation grounding_loc = getBestGroundingLocation( GroundingLocation grounding_loc = getBestGroundingLocation(
unsupported_location, current_outlines, outlines_locator, supporting_radius, wall_supporting_radius, tree_node_locator); unsupported_location, current_outlines, current_outlines_bbox, outlines_locator, supporting_radius, wall_supporting_radius, tree_node_locator);
NodeSPtr new_parent; NodeSPtr new_parent;
NodeSPtr new_child; NodeSPtr new_child;
this->attach(unsupported_location, grounding_loc, new_child, new_parent); this->attach(unsupported_location, grounding_loc, new_child, new_parent);
tree_node_locator.insert(std::make_pair(Point(new_child->getLocation().x() / locator_cell_size, new_child->getLocation().y() / locator_cell_size), new_child)); tree_node_locator.insert(std::make_pair(to_grid_point(new_child->getLocation(), current_outlines_bbox), new_child));
if (new_parent) if (new_parent)
tree_node_locator.insert(std::make_pair(Point(new_parent->getLocation().x() / locator_cell_size, new_parent->getLocation().y() / locator_cell_size), new_parent)); tree_node_locator.insert(std::make_pair(to_grid_point(new_parent->getLocation(), current_outlines_bbox), new_parent));
// update distance field // update distance field
distance_field.update(grounding_loc.p(), unsupported_location); distance_field.update(grounding_loc.p(), unsupported_location);
} }
#ifdef LIGHTNING_TREE_NODE_DEBUG_OUTPUT
{
static int iRun = 0;
export_to_svg(debug_out_path("FillLightning-TreeNodes-%d.svg", iRun++), current_outlines, this->tree_roots);
}
#endif /* LIGHTNING_TREE_NODE_DEBUG_OUTPUT */
} }
static bool polygonCollidesWithLineSegment(const Point from, const Point to, const EdgeGrid::Grid &loc_to_line) static bool polygonCollidesWithLineSegment(const Point &from, const Point &to, const EdgeGrid::Grid &loc_to_line)
{ {
struct Visitor { struct Visitor {
explicit Visitor(const EdgeGrid::Grid &grid) : grid(grid) {} explicit Visitor(const EdgeGrid::Grid &grid, const Line &line) : grid(grid), line(line) {}
bool operator()(coord_t iy, coord_t ix) { bool operator()(coord_t iy, coord_t ix) {
// Called with a row and colum of the grid cell, which is intersected by a line. // Called with a row and colum of the grid cell, which is intersected by a line.
@ -87,7 +108,7 @@ static bool polygonCollidesWithLineSegment(const Point from, const Point to, con
const EdgeGrid::Grid& grid; const EdgeGrid::Grid& grid;
Line line; Line line;
bool intersect = false; bool intersect = false;
} visitor(loc_to_line); } visitor(loc_to_line, {from, to});
loc_to_line.visit_cells_intersecting_line(from, to, visitor); loc_to_line.visit_cells_intersecting_line(from, to, visitor);
return visitor.intersect; return visitor.intersect;
@ -97,6 +118,7 @@ GroundingLocation Layer::getBestGroundingLocation
( (
const Point& unsupported_location, const Point& unsupported_location,
const Polygons& current_outlines, const Polygons& current_outlines,
const BoundingBox& current_outlines_bbox,
const EdgeGrid::Grid& outline_locator, const EdgeGrid::Grid& outline_locator,
const coord_t supporting_radius, const coord_t supporting_radius,
const coord_t wall_supporting_radius, const coord_t wall_supporting_radius,
@ -112,9 +134,10 @@ GroundingLocation Layer::getBestGroundingLocation
if (contour.size() > 2) { if (contour.size() > 2) {
Point prev = contour.points.back(); Point prev = contour.points.back();
for (const Point &p2 : contour.points) { for (const Point &p2 : contour.points) {
if (double d = Line::distance_to_squared(unsupported_location, prev, p2); d < d2) { Point closest_point;
if (double d = line_alg::distance_to_squared(Line{prev, p2}, unsupported_location, &closest_point); d < d2) {
d2 = d; d2 = d;
node_location = Geometry::foot_pt({ prev, p2 }, unsupported_location).cast<coord_t>(); node_location = closest_point;
} }
prev = p2; prev = p2;
} }
@ -123,30 +146,52 @@ GroundingLocation Layer::getBestGroundingLocation
const auto within_dist = coord_t((node_location - unsupported_location).cast<double>().norm()); const auto within_dist = coord_t((node_location - unsupported_location).cast<double>().norm());
NodeSPtr sub_tree{ nullptr }; NodeSPtr sub_tree{nullptr};
coord_t current_dist = getWeightedDistance(node_location, unsupported_location); coord_t current_dist = getWeightedDistance(node_location, unsupported_location);
if (current_dist >= wall_supporting_radius) { // Only reconnect tree roots to other trees if they are not already close to the outlines. if (current_dist >= wall_supporting_radius) { // Only reconnect tree roots to other trees if they are not already close to the outlines.
const coord_t search_radius = std::min(current_dist, within_dist); const coord_t search_radius = std::min(current_dist, within_dist);
BoundingBox region(unsupported_location - Point(search_radius, search_radius), unsupported_location + Point(search_radius + locator_cell_size, search_radius + locator_cell_size)); BoundingBox region(unsupported_location - Point(search_radius, search_radius), unsupported_location + Point(search_radius + locator_cell_size, search_radius + locator_cell_size));
region.min /= locator_cell_size; region.min = to_grid_point(region.min, current_outlines_bbox);
region.max /= locator_cell_size; region.max = to_grid_point(region.max, current_outlines_bbox);
Point grid_addr;
for (grid_addr.y() = region.min.y(); grid_addr.y() < region.max.y(); ++ grid_addr.y()) Point current_dist_grid_addr{std::numeric_limits<coord_t>::lowest(), std::numeric_limits<coord_t>::lowest()};
for (grid_addr.x() = region.min.x(); grid_addr.x() < region.max.x(); ++ grid_addr.x()) { std::mutex current_dist_mutex;
auto it_range = tree_node_locator.equal_range(grid_addr); tbb::parallel_for(tbb::blocked_range2d<coord_t>(region.min.y(), region.max.y(), region.min.x(), region.max.x()), [&current_dist, current_dist_copy = current_dist, &current_dist_mutex, &sub_tree, &current_dist_grid_addr, &exclude_tree = std::as_const(exclude_tree), &outline_locator = std::as_const(outline_locator), &supporting_radius = std::as_const(supporting_radius), &tree_node_locator = std::as_const(tree_node_locator), &unsupported_location = std::as_const(unsupported_location)](const tbb::blocked_range2d<coord_t> &range) -> void {
for (auto it = it_range.first; it != it_range.second; ++ it) { for (coord_t grid_addr_y = range.rows().begin(); grid_addr_y < range.rows().end(); ++grid_addr_y)
auto candidate_sub_tree = it->second.lock(); for (coord_t grid_addr_x = range.cols().begin(); grid_addr_x < range.cols().end(); ++grid_addr_x) {
if ((candidate_sub_tree && candidate_sub_tree != exclude_tree) && const Point local_grid_addr{grid_addr_x, grid_addr_y};
!(exclude_tree && exclude_tree->hasOffspring(candidate_sub_tree)) && NodeSPtr local_sub_tree{nullptr};
!polygonCollidesWithLineSegment(unsupported_location, candidate_sub_tree->getLocation(), outline_locator)) { coord_t local_current_dist = current_dist_copy;
const coord_t candidate_dist = candidate_sub_tree->getWeightedDistance(unsupported_location, supporting_radius); const auto it_range = tree_node_locator.equal_range(local_grid_addr);
if (candidate_dist < current_dist) { for (auto it = it_range.first; it != it_range.second; ++it) {
current_dist = candidate_dist; const NodeSPtr candidate_sub_tree = it->second.lock();
sub_tree = candidate_sub_tree; if ((candidate_sub_tree && candidate_sub_tree != exclude_tree) &&
!(exclude_tree && exclude_tree->hasOffspring(candidate_sub_tree)) &&
!polygonCollidesWithLineSegment(unsupported_location, candidate_sub_tree->getLocation(), outline_locator)) {
if (const coord_t candidate_dist = candidate_sub_tree->getWeightedDistance(unsupported_location, supporting_radius); candidate_dist < local_current_dist) {
local_current_dist = candidate_dist;
local_sub_tree = candidate_sub_tree;
}
}
}
// To always get the same result in a parallel version as in a non-parallel version,
// we need to preserve that for the same current_dist, we select the same sub_tree
// as in the non-parallel version. For this purpose, inside the variable
// current_dist_grid_addr is stored from with 2D grid position assigned sub_tree comes.
// And when there are two sub_tree with the same current_dist, one which will be found
// the first in the non-parallel version is selected.
{
std::lock_guard<std::mutex> lock(current_dist_mutex);
if (local_current_dist < current_dist ||
(local_current_dist == current_dist && (grid_addr_y < current_dist_grid_addr.y() ||
(grid_addr_y == current_dist_grid_addr.y() && grid_addr_x < current_dist_grid_addr.x())))) {
current_dist = local_current_dist;
sub_tree = local_sub_tree;
current_dist_grid_addr = local_grid_addr;
} }
} }
} }
} }); // end of parallel_for
} }
return ! sub_tree ? return ! sub_tree ?
@ -176,6 +221,7 @@ void Layer::reconnectRoots
( (
std::vector<NodeSPtr>& to_be_reconnected_tree_roots, std::vector<NodeSPtr>& to_be_reconnected_tree_roots,
const Polygons& current_outlines, const Polygons& current_outlines,
const BoundingBox& current_outlines_bbox,
const EdgeGrid::Grid& outline_locator, const EdgeGrid::Grid& outline_locator,
const coord_t supporting_radius, const coord_t supporting_radius,
const coord_t wall_supporting_radius const coord_t wall_supporting_radius
@ -184,10 +230,10 @@ void Layer::reconnectRoots
constexpr coord_t tree_connecting_ignore_offset = 100; constexpr coord_t tree_connecting_ignore_offset = 100;
SparseNodeGrid tree_node_locator; SparseNodeGrid tree_node_locator;
fillLocator(tree_node_locator); fillLocator(tree_node_locator, current_outlines_bbox);
const coord_t within_max_dist = outline_locator.resolution() * 2; const coord_t within_max_dist = outline_locator.resolution() * 2;
for (auto root_ptr : to_be_reconnected_tree_roots) for (const auto &root_ptr : to_be_reconnected_tree_roots)
{ {
auto old_root_it = std::find(tree_roots.begin(), tree_roots.end(), root_ptr); auto old_root_it = std::find(tree_roots.begin(), tree_roots.end(), root_ptr);
@ -203,7 +249,7 @@ void Layer::reconnectRoots
root_ptr->addChild(new_root); root_ptr->addChild(new_root);
new_root->reroot(); new_root->reroot();
tree_node_locator.insert(std::make_pair(Point(new_root->getLocation().x() / locator_cell_size, new_root->getLocation().y() / locator_cell_size), new_root)); tree_node_locator.insert(std::make_pair(to_grid_point(new_root->getLocation(), current_outlines_bbox), new_root));
*old_root_it = std::move(new_root); // replace old root with new root *old_root_it = std::move(new_root); // replace old root with new root
continue; continue;
@ -217,6 +263,7 @@ void Layer::reconnectRoots
( (
root_ptr->getLocation(), root_ptr->getLocation(),
current_outlines, current_outlines,
current_outlines_bbox,
outline_locator, outline_locator,
supporting_radius, supporting_radius,
tree_connecting_ignore_width, tree_connecting_ignore_width,
@ -233,7 +280,7 @@ void Layer::reconnectRoots
attach_ptr->reroot(); attach_ptr->reroot();
new_root->addChild(attach_ptr); new_root->addChild(attach_ptr);
tree_node_locator.insert(std::make_pair(new_root->getLocation(), new_root)); tree_node_locator.insert(std::make_pair(to_grid_point(new_root->getLocation(), current_outlines_bbox), new_root));
*old_root_it = std::move(new_root); // replace old root with new root *old_root_it = std::move(new_root); // replace old root with new root
} }
@ -256,15 +303,26 @@ void Layer::reconnectRoots
} }
} }
/* #if 0
* Implementation assumes moving inside, but moving outside should just as well be possible. /*!
* Moves the point \p from onto the nearest polygon or leaves the point as-is, when the comb boundary is not within the root of \p max_dist2 distance.
* Given a \p distance more than zero, the point will end up inside, and conversely outside.
* When the point is already in/outside by more than \p distance, \p from is unaltered, but the polygon is returned.
* When the point is in/outside by less than \p distance, \p from is moved to the correct place.
* Implementation assumes moving inside, but moving outside should just as well be possible.
*
* \param polygons The polygons onto which to move the point
* \param from[in,out] The point to move.
* \param distance The distance by which to move the point.
* \param max_dist2 The squared maximal allowed distance from the point to the nearest polygon.
* \return The index to the polygon onto which we have moved the point.
*/ */
static unsigned int moveInside(const Polygons& polygons, Point& from, int distance, int64_t maxDist2) static unsigned int moveInside(const Polygons& polygons, Point& from, int distance, int64_t maxDist2)
{ {
Point ret = from; Point ret = from;
int64_t bestDist2 = std::numeric_limits<int64_t>::max(); int64_t bestDist2 = std::numeric_limits<int64_t>::max();
unsigned int bestPoly = static_cast<unsigned int>(-1); auto bestPoly = static_cast<unsigned int>(-1);
bool is_already_on_correct_side_of_boundary = false; // whether [from] is already on the right side of the boundary bool is_already_on_correct_side_of_boundary = false; // whether [from] is already on the right side of the boundary
for (unsigned int poly_idx = 0; poly_idx < polygons.size(); poly_idx++) for (unsigned int poly_idx = 0; poly_idx < polygons.size(); poly_idx++)
{ {
const Polygon &poly = polygons[poly_idx]; const Polygon &poly = polygons[poly_idx];
@ -333,7 +391,7 @@ static unsigned int moveInside(const Polygons& polygons, Point& from, int distan
else else
{ // x is projected to a point properly on the line segment (not onto a vertex). The case which looks like | . { // x is projected to a point properly on the line segment (not onto a vertex). The case which looks like | .
projected_p_beyond_prev_segment = false; projected_p_beyond_prev_segment = false;
Point x = a + ab * dot_prod / ab_length2; Point x = (a.cast<int64_t>() + ab.cast<int64_t>() * dot_prod / ab_length2).cast<coord_t>();
int64_t dist2 = (p - x).cast<int64_t>().squaredNorm(); int64_t dist2 = (p - x).cast<int64_t>().squaredNorm();
if (dist2 < bestDist2) if (dist2 < bestDist2)
@ -373,38 +431,18 @@ static unsigned int moveInside(const Polygons& polygons, Point& from, int distan
} }
return static_cast<unsigned int>(-1); return static_cast<unsigned int>(-1);
} }
#endif
// Returns 'added someting'. Polylines Layer::convertToLines(const Polygons& limit_to_outline, const coord_t line_overlap) const
Polylines Layer::convertToLines(const Polygons& limit_to_outline, const coord_t line_width) const
{ {
if (tree_roots.empty()) if (tree_roots.empty())
return {}; return {};
Polygons result_lines; Polylines result_lines;
for (const auto& tree : tree_roots) { for (const auto &tree : tree_roots)
// If even the furthest location in the tree is inside the polygon, the entire tree must be inside of the polygon. tree->convertToPolylines(result_lines, line_overlap);
// (Don't take the root as that may be on the edge and cause rounding errors to register as 'outside'.)
constexpr coord_t epsilon = 5;
Point should_be_inside = tree->getLocation();
moveInside(limit_to_outline, should_be_inside, epsilon, epsilon * epsilon);
if (inside(limit_to_outline, should_be_inside))
tree->convertToPolylines(result_lines, line_width);
}
// TODO: allow for polylines! return intersection_pl(result_lines, limit_to_outline);
Polylines split_lines;
for (Polygon &line : result_lines) {
if (line.size() <= 1)
continue;
Point last = line[0];
for (size_t point_idx = 1; point_idx < line.size(); point_idx++) {
Point here = line[point_idx];
split_lines.push_back({ last, here });
last = here;
}
}
return split_lines;
} }
} // namespace Slic3r::Lightning } // namespace Slic3r::Lightning

View file

@ -41,9 +41,11 @@ public:
( (
const Polygons& current_overhang, const Polygons& current_overhang,
const Polygons& current_outlines, const Polygons& current_outlines,
const BoundingBox& current_outlines_bbox,
const EdgeGrid::Grid& outline_locator, const EdgeGrid::Grid& outline_locator,
const coord_t supporting_radius, coord_t supporting_radius,
const coord_t wall_supporting_radius coord_t wall_supporting_radius,
const std::function<void()> &throw_on_cancel_callback
); );
/*! Determine & connect to connection point in tree/outline. /*! Determine & connect to connection point in tree/outline.
@ -53,9 +55,10 @@ public:
( (
const Point& unsupported_location, const Point& unsupported_location,
const Polygons& current_outlines, const Polygons& current_outlines,
const BoundingBox& current_outlines_bbox,
const EdgeGrid::Grid& outline_locator, const EdgeGrid::Grid& outline_locator,
const coord_t supporting_radius, coord_t supporting_radius,
const coord_t wall_supporting_radius, coord_t wall_supporting_radius,
const SparseNodeGrid& tree_node_locator, const SparseNodeGrid& tree_node_locator,
const NodeSPtr& exclude_tree = nullptr const NodeSPtr& exclude_tree = nullptr
); );
@ -71,16 +74,17 @@ public:
( (
std::vector<NodeSPtr>& to_be_reconnected_tree_roots, std::vector<NodeSPtr>& to_be_reconnected_tree_roots,
const Polygons& current_outlines, const Polygons& current_outlines,
const BoundingBox& current_outlines_bbox,
const EdgeGrid::Grid& outline_locator, const EdgeGrid::Grid& outline_locator,
const coord_t supporting_radius, coord_t supporting_radius,
const coord_t wall_supporting_radius coord_t wall_supporting_radius
); );
Polylines convertToLines(const Polygons& limit_to_outline, const coord_t line_width) const; Polylines convertToLines(const Polygons& limit_to_outline, coord_t line_overlap) const;
coord_t getWeightedDistance(const Point& boundary_loc, const Point& unsupported_location); coord_t getWeightedDistance(const Point& boundary_loc, const Point& unsupported_location);
void fillLocator(SparseNodeGrid& tree_node_locator); void fillLocator(SparseNodeGrid& tree_node_locator, const BoundingBox& current_outlines_bbox);
}; };
} // namespace Slic3r::FillLightning } // namespace Slic3r::FillLightning

View file

@ -4,7 +4,6 @@
#include "TreeNode.hpp" #include "TreeNode.hpp"
#include "../../Geometry.hpp" #include "../../Geometry.hpp"
#include "../../ClipperUtils.hpp"
namespace Slic3r::FillLightning { namespace Slic3r::FillLightning {
@ -107,7 +106,7 @@ NodeSPtr Node::deepCopy() const
return local_root; return local_root;
} }
void Node::reroot(NodeSPtr new_parent /*= nullptr*/) void Node::reroot(const NodeSPtr &new_parent)
{ {
if (! m_is_root) { if (! m_is_root) {
auto old_parent = m_parent.lock(); auto old_parent = m_parent.lock();
@ -142,7 +141,7 @@ NodeSPtr Node::closestNode(const Point& loc)
return result; return result;
} }
bool inside(const Polygons &polygons, const Point p) bool inside(const Polygons &polygons, const Point &p)
{ {
int poly_count_inside = 0; int poly_count_inside = 0;
for (const Polygon &poly : polygons) { for (const Polygon &poly : polygons) {
@ -181,7 +180,11 @@ bool lineSegmentPolygonsIntersection(const Point& a, const Point& b, const EdgeG
} visitor { outline_locator, a.cast<double>(), b.cast<double>() }; } visitor { outline_locator, a.cast<double>(), b.cast<double>() };
outline_locator.visit_cells_intersecting_line(a, b, visitor); outline_locator.visit_cells_intersecting_line(a, b, visitor);
return visitor.d2min < within_max_dist * within_max_dist; if (visitor.d2min < double(within_max_dist) * double(within_max_dist)) {
result = Point(visitor.intersection_pt);
return true;
}
return false;
} }
bool Node::realign(const Polygons& outlines, const EdgeGrid::Grid& outline_locator, std::vector<NodeSPtr>& rerooted_parts) bool Node::realign(const Polygons& outlines, const EdgeGrid::Grid& outline_locator, std::vector<NodeSPtr>& rerooted_parts)
@ -226,14 +229,14 @@ bool Node::realign(const Polygons& outlines, const EdgeGrid::Grid& outline_locat
void Node::straighten(const coord_t magnitude, const coord_t max_remove_colinear_dist) void Node::straighten(const coord_t magnitude, const coord_t max_remove_colinear_dist)
{ {
straighten(magnitude, m_p, 0, max_remove_colinear_dist * max_remove_colinear_dist); straighten(magnitude, m_p, 0, int64_t(max_remove_colinear_dist) * int64_t(max_remove_colinear_dist));
} }
Node::RectilinearJunction Node::straighten( Node::RectilinearJunction Node::straighten(
const coord_t magnitude, const coord_t magnitude,
const Point& junction_above, const Point& junction_above,
const coord_t accumulated_dist, const coord_t accumulated_dist,
const coord_t max_remove_colinear_dist2) const int64_t max_remove_colinear_dist2)
{ {
constexpr coord_t junction_magnitude_factor_numerator = 3; constexpr coord_t junction_magnitude_factor_numerator = 3;
constexpr coord_t junction_magnitude_factor_denominator = 4; constexpr coord_t junction_magnitude_factor_denominator = 4;
@ -245,13 +248,13 @@ Node::RectilinearJunction Node::straighten(
auto child_dist = coord_t((m_p - child_p->m_p).cast<double>().norm()); auto child_dist = coord_t((m_p - child_p->m_p).cast<double>().norm());
RectilinearJunction junction_below = child_p->straighten(magnitude, junction_above, accumulated_dist + child_dist, max_remove_colinear_dist2); RectilinearJunction junction_below = child_p->straighten(magnitude, junction_above, accumulated_dist + child_dist, max_remove_colinear_dist2);
coord_t total_dist_to_junction_below = junction_below.total_recti_dist; coord_t total_dist_to_junction_below = junction_below.total_recti_dist;
Point a = junction_above; const Point& a = junction_above;
Point b = junction_below.junction_loc; Point b = junction_below.junction_loc;
if (a != b) // should always be true! if (a != b) // should always be true!
{ {
Point ab = b - a; Point ab = b - a;
Point destination = a + ab * accumulated_dist / std::max(coord_t(1), total_dist_to_junction_below); Point destination = (a.cast<int64_t>() + ab.cast<int64_t>() * int64_t(accumulated_dist) / std::max(int64_t(1), int64_t(total_dist_to_junction_below))).cast<coord_t>();
if ((destination - m_p).cast<double>().squaredNorm() <= magnitude * magnitude) if ((destination - m_p).cast<int64_t>().squaredNorm() <= int64_t(magnitude) * int64_t(magnitude))
m_p = destination; m_p = destination;
else else
m_p += ((destination - m_p).cast<double>().normalized() * magnitude).cast<coord_t>(); m_p += ((destination - m_p).cast<double>().normalized() * magnitude).cast<coord_t>();
@ -262,7 +265,7 @@ Node::RectilinearJunction Node::straighten(
child_p = m_children.front(); //recursive call to straighten might have removed the child child_p = m_children.front(); //recursive call to straighten might have removed the child
const NodeSPtr& parent_node = m_parent.lock(); const NodeSPtr& parent_node = m_parent.lock();
if (parent_node && if (parent_node &&
(child_p->m_p - parent_node->m_p).cast<double>().squaredNorm() < max_remove_colinear_dist2 && (child_p->m_p - parent_node->m_p).cast<int64_t>().squaredNorm() < max_remove_colinear_dist2 &&
Line::distance_to_squared(m_p, parent_node->m_p, child_p->m_p) < close_enough * close_enough) { Line::distance_to_squared(m_p, parent_node->m_p, child_p->m_p) < close_enough * close_enough) {
child_p->m_parent = m_parent; child_p->m_parent = m_parent;
for (auto& sibling : parent_node->m_children) for (auto& sibling : parent_node->m_children)
@ -344,16 +347,16 @@ coord_t Node::prune(const coord_t& pruning_distance)
return max_distance_pruned; return max_distance_pruned;
} }
void Node::convertToPolylines(Polygons& output, const coord_t line_width) const void Node::convertToPolylines(Polylines &output, const coord_t line_overlap) const
{ {
Polygons result; Polylines result;
output.emplace_back(); result.emplace_back();
convertToPolylines(0, result); convertToPolylines(0, result);
removeJunctionOverlap(result, line_width); removeJunctionOverlap(result, line_overlap);
append(output, std::move(result)); append(output, std::move(result));
} }
void Node::convertToPolylines(size_t long_line_idx, Polygons& output) const void Node::convertToPolylines(size_t long_line_idx, Polylines &output) const
{ {
if (m_children.empty()) { if (m_children.empty()) {
output[long_line_idx].points.push_back(m_p); output[long_line_idx].points.push_back(m_p);
@ -373,11 +376,12 @@ void Node::convertToPolylines(size_t long_line_idx, Polygons& output) const
} }
} }
void Node::removeJunctionOverlap(Polygons& result_lines, const coord_t line_width) const void Node::removeJunctionOverlap(Polylines &result_lines, const coord_t line_overlap) const
{ {
const coord_t reduction = line_width / 2; // TODO make configurable? const coord_t reduction = line_overlap;
for (auto poly_it = result_lines.begin(); poly_it != result_lines.end(); ) { size_t res_line_idx = 0;
Polygon &polyline = *poly_it; while (res_line_idx < result_lines.size()) {
Polyline &polyline = result_lines[res_line_idx];
if (polyline.size() <= 1) { if (polyline.size() <= 1) {
polyline = std::move(result_lines.back()); polyline = std::move(result_lines.back());
result_lines.pop_back(); result_lines.pop_back();
@ -386,8 +390,8 @@ void Node::removeJunctionOverlap(Polygons& result_lines, const coord_t line_widt
coord_t to_be_reduced = reduction; coord_t to_be_reduced = reduction;
Point a = polyline.back(); Point a = polyline.back();
for (int point_idx = polyline.size() - 2; point_idx >= 0; point_idx--) { for (int point_idx = int(polyline.size()) - 2; point_idx >= 0; point_idx--) {
const Point b = polyline[point_idx]; const Point b = polyline.points[point_idx];
const Point ab = b - a; const Point ab = b - a;
const auto ab_len = coord_t(ab.cast<double>().norm()); const auto ab_len = coord_t(ab.cast<double>().norm());
if (ab_len >= to_be_reduced) { if (ab_len >= to_be_reduced) {
@ -404,8 +408,33 @@ void Node::removeJunctionOverlap(Polygons& result_lines, const coord_t line_widt
polyline = std::move(result_lines.back()); polyline = std::move(result_lines.back());
result_lines.pop_back(); result_lines.pop_back();
} else } else
++ poly_it; ++ res_line_idx;
} }
} }
#ifdef LIGHTNING_TREE_NODE_DEBUG_OUTPUT
void export_to_svg(const NodeSPtr &root_node, SVG &svg)
{
for (const NodeSPtr &children : root_node->m_children) {
svg.draw(Line(root_node->getLocation(), children->getLocation()), "red");
export_to_svg(children, svg);
}
}
void export_to_svg(const std::string &path, const Polygons &contour, const std::vector<NodeSPtr> &root_nodes) {
BoundingBox bbox = get_extents(contour);
bbox.offset(SCALED_EPSILON);
SVG svg(path, bbox);
svg.draw_outline(contour, "blue");
for (const NodeSPtr &root_node: root_nodes) {
for (const NodeSPtr &children: root_node->m_children) {
svg.draw(Line(root_node->getLocation(), children->getLocation()), "red");
export_to_svg(children, svg);
}
}
}
#endif /* LIGHTNING_TREE_NODE_DEBUG_OUTPUT */
} // namespace Slic3r::FillLightning } // namespace Slic3r::FillLightning

View file

@ -11,6 +11,9 @@
#include "../../EdgeGrid.hpp" #include "../../EdgeGrid.hpp"
#include "../../Polygon.hpp" #include "../../Polygon.hpp"
#include "SVG.hpp"
//#define LIGHTNING_TREE_NODE_DEBUG_OUTPUT
namespace Slic3r::FillLightning namespace Slic3r::FillLightning
{ {
@ -43,7 +46,7 @@ public:
{ {
struct EnableMakeShared : public Node struct EnableMakeShared : public Node
{ {
EnableMakeShared(Arg&&...arg) : Node(std::forward<Arg>(arg)...) {} explicit EnableMakeShared(Arg&&...arg) : Node(std::forward<Arg>(arg)...) {}
}; };
return std::make_shared<EnableMakeShared>(std::forward<Arg>(arg)...); return std::make_shared<EnableMakeShared>(std::forward<Arg>(arg)...);
} }
@ -99,9 +102,9 @@ public:
std::vector<NodeSPtr>& next_trees, std::vector<NodeSPtr>& next_trees,
const Polygons& next_outlines, const Polygons& next_outlines,
const EdgeGrid::Grid& outline_locator, const EdgeGrid::Grid& outline_locator,
const coord_t prune_distance, coord_t prune_distance,
const coord_t smooth_magnitude, coord_t smooth_magnitude,
const coord_t max_remove_colinear_dist coord_t max_remove_colinear_dist
) const; ) const;
/*! /*!
@ -156,7 +159,7 @@ public:
* This is then recursively bubbled up until it reaches the (former) root, which then will become a leaf. * This is then recursively bubbled up until it reaches the (former) root, which then will become a leaf.
* \param new_parent The (new) parent-node of the root, useful for recursing or immediately attaching the node to another tree. * \param new_parent The (new) parent-node of the root, useful for recursing or immediately attaching the node to another tree.
*/ */
void reroot(NodeSPtr new_parent = nullptr); void reroot(const NodeSPtr &new_parent = nullptr);
/*! /*!
* Retrieves the closest node to the specified location. * Retrieves the closest node to the specified location.
@ -176,16 +179,16 @@ public:
*/ */
bool hasOffspring(const NodeSPtr& to_be_checked) const; bool hasOffspring(const NodeSPtr& to_be_checked) const;
protected:
Node() = delete; // Don't allow empty contruction Node() = delete; // Don't allow empty contruction
protected:
/*! /*!
* Construct a new node, either for insertion in a tree or as root. * Construct a new node, either for insertion in a tree or as root.
* \param p The physical location in the 2D layer that this node represents. * \param p The physical location in the 2D layer that this node represents.
* Connecting other nodes to this node indicates that a line segment should * Connecting other nodes to this node indicates that a line segment should
* be drawn between those two physical positions. * be drawn between those two physical positions.
*/ */
Node(const Point& p, const std::optional<Point>& last_grounding_location = std::nullopt); explicit Node(const Point& p, const std::optional<Point>& last_grounding_location = std::nullopt);
/*! /*!
* Copy this node and its entire sub-tree. * Copy this node and its entire sub-tree.
@ -211,7 +214,7 @@ protected:
* \param magnitude The maximum allowed distance to move the node. * \param magnitude The maximum allowed distance to move the node.
* \param max_remove_colinear_dist Maximum distance of the (compound) line-segment from which a co-linear point may be removed. * \param max_remove_colinear_dist Maximum distance of the (compound) line-segment from which a co-linear point may be removed.
*/ */
void straighten(const coord_t magnitude, const coord_t max_remove_colinear_dist); void straighten(coord_t magnitude, coord_t max_remove_colinear_dist);
/*! Recursive part of \ref straighten(.) /*! Recursive part of \ref straighten(.)
* \param junction_above The last seen junction with multiple children above * \param junction_above The last seen junction with multiple children above
@ -219,7 +222,7 @@ protected:
* \param max_remove_colinear_dist2 Maximum distance _squared_ of the (compound) line-segment from which a co-linear point may be removed. * \param max_remove_colinear_dist2 Maximum distance _squared_ of the (compound) line-segment from which a co-linear point may be removed.
* \return the total distance along the tree from the last junction above to the first next junction below and the location of the next junction below * \return the total distance along the tree from the last junction above to the first next junction below and the location of the next junction below
*/ */
RectilinearJunction straighten(const coord_t magnitude, const Point& junction_above, const coord_t accumulated_dist, const coord_t max_remove_colinear_dist2); RectilinearJunction straighten(coord_t magnitude, const Point& junction_above, coord_t accumulated_dist, int64_t max_remove_colinear_dist2);
/*! Prune the tree from the extremeties (leaf-nodes) until the pruning distance is reached. /*! Prune the tree from the extremeties (leaf-nodes) until the pruning distance is reached.
* \return The distance that has been pruned. If less than \p distance, then the whole tree was puned away. * \return The distance that has been pruned. If less than \p distance, then the whole tree was puned away.
@ -236,7 +239,7 @@ public:
* *
* \param output all branches in this tree connected into polylines * \param output all branches in this tree connected into polylines
*/ */
void convertToPolylines(Polygons& output, const coord_t line_width) const; void convertToPolylines(Polylines &output, coord_t line_overlap) const;
/*! If this was ever a direct child of the root, it'll have a previous grounding location. /*! If this was ever a direct child of the root, it'll have a previous grounding location.
* *
@ -255,9 +258,9 @@ protected:
* \param long_line a reference to a polyline in \p output which to continue building on in the recursion * \param long_line a reference to a polyline in \p output which to continue building on in the recursion
* \param output all branches in this tree connected into polylines * \param output all branches in this tree connected into polylines
*/ */
void convertToPolylines(size_t long_line_idx, Polygons& output) const; void convertToPolylines(size_t long_line_idx, Polylines &output) const;
void removeJunctionOverlap(Polygons& polylines, const coord_t line_width) const; void removeJunctionOverlap(Polylines &polylines, coord_t line_overlap) const;
bool m_is_root; bool m_is_root;
Point m_p; Point m_p;
@ -265,10 +268,40 @@ protected:
std::vector<NodeSPtr> m_children; std::vector<NodeSPtr> m_children;
std::optional<Point> m_last_grounding_location; //<! The last known grounding location, see 'getLastGroundingLocation()'. std::optional<Point> m_last_grounding_location; //<! The last known grounding location, see 'getLastGroundingLocation()'.
friend BoundingBox get_extents(const NodeSPtr &root_node);
friend BoundingBox get_extents(const std::vector<NodeSPtr> &tree_roots);
#ifdef LIGHTNING_TREE_NODE_DEBUG_OUTPUT
friend void export_to_svg(const NodeSPtr &root_node, Slic3r::SVG &svg);
friend void export_to_svg(const std::string &path, const Polygons &contour, const std::vector<NodeSPtr> &root_nodes);
#endif /* LIGHTNING_TREE_NODE_DEBUG_OUTPUT */
}; };
bool inside(const Polygons &polygons, const Point p); bool inside(const Polygons &polygons, const Point &p);
bool lineSegmentPolygonsIntersection(const Point& a, const Point& b, const EdgeGrid::Grid& outline_locator, Point& result, const coord_t within_max_dist); bool lineSegmentPolygonsIntersection(const Point& a, const Point& b, const EdgeGrid::Grid& outline_locator, Point& result, coord_t within_max_dist);
inline BoundingBox get_extents(const NodeSPtr &root_node)
{
BoundingBox bbox;
for (const NodeSPtr &children : root_node->m_children)
bbox.merge(get_extents(children));
bbox.merge(root_node->getLocation());
return bbox;
}
inline BoundingBox get_extents(const std::vector<NodeSPtr> &tree_roots)
{
BoundingBox bbox;
for (const NodeSPtr &root_node : tree_roots)
bbox.merge(get_extents(root_node));
return bbox;
}
#ifdef LIGHTNING_TREE_NODE_DEBUG_OUTPUT
void export_to_svg(const NodeSPtr &root_node, SVG &svg);
void export_to_svg(const std::string &path, const Polygons &contour, const std::vector<NodeSPtr> &root_nodes);
#endif /* LIGHTNING_TREE_NODE_DEBUG_OUTPUT */
} // namespace Slic3r::FillLightning } // namespace Slic3r::FillLightning

View file

@ -20,6 +20,10 @@ namespace FillAdaptive {
struct Octree; struct Octree;
}; };
namespace FillLightning {
class Generator;
};
class LayerRegion class LayerRegion
{ {
public: public:
@ -151,8 +155,8 @@ public:
} }
void make_perimeters(); void make_perimeters();
// Phony version of make_fills() without parameters for Perl integration only. // Phony version of make_fills() without parameters for Perl integration only.
void make_fills() { this->make_fills(nullptr, nullptr); } void make_fills() { this->make_fills(nullptr, nullptr, nullptr); }
void make_fills(FillAdaptive::Octree* adaptive_fill_octree, FillAdaptive::Octree* support_fill_octree); void make_fills(FillAdaptive::Octree* adaptive_fill_octree, FillAdaptive::Octree* support_fill_octree, FillLightning::Generator* lightning_generator);
void make_ironing(); void make_ironing();
void export_region_slices_to_svg(const char *path) const; void export_region_slices_to_svg(const char *path) const;

View file

@ -177,6 +177,11 @@ inline bool operator<(const Point &l, const Point &r)
return l.x() < r.x() || (l.x() == r.x() && l.y() < r.y()); return l.x() < r.x() || (l.x() == r.x() && l.y() < r.y());
} }
inline Point operator* (const Point& l, const double &r)
{
return {coord_t(l.x() * r), coord_t(l.y() * r)};
}
inline bool is_approx(const Point &p1, const Point &p2, coord_t epsilon = coord_t(SCALED_EPSILON)) inline bool is_approx(const Point &p1, const Point &p2, coord_t epsilon = coord_t(SCALED_EPSILON))
{ {
Point d = (p2 - p1).cwiseAbs(); Point d = (p2 - p1).cwiseAbs();

View file

@ -35,7 +35,13 @@ namespace FillAdaptive {
struct Octree; struct Octree;
struct OctreeDeleter; struct OctreeDeleter;
using OctreePtr = std::unique_ptr<Octree, OctreeDeleter>; using OctreePtr = std::unique_ptr<Octree, OctreeDeleter>;
}; }; // namespace FillAdaptive
namespace FillLightning {
class Generator;
struct GeneratorDeleter;
using GeneratorPtr = std::unique_ptr<Generator, GeneratorDeleter>;
}; // namespace FillLightning
// Print step IDs for keeping track of the print state. // Print step IDs for keeping track of the print state.
// The Print steps are applied in this order. // The Print steps are applied in this order.
@ -382,6 +388,7 @@ private:
void combine_infill(); void combine_infill();
void _generate_support_material(); void _generate_support_material();
std::pair<FillAdaptive::OctreePtr, FillAdaptive::OctreePtr> prepare_adaptive_infill_data(); std::pair<FillAdaptive::OctreePtr, FillAdaptive::OctreePtr> prepare_adaptive_infill_data();
FillLightning::GeneratorPtr prepare_lightning_infill_data();
// XYZ in scaled coordinates // XYZ in scaled coordinates
Vec3crd m_size; Vec3crd m_size;

View file

@ -108,9 +108,7 @@ static t_config_enum_values s_keys_map_InfillPattern {
{ "octagramspiral", ipOctagramSpiral }, { "octagramspiral", ipOctagramSpiral },
{ "adaptivecubic", ipAdaptiveCubic }, { "adaptivecubic", ipAdaptiveCubic },
{ "supportcubic", ipSupportCubic }, { "supportcubic", ipSupportCubic },
#if HAS_LIGHTNING_INFILL
{ "lightning", ipLightning } { "lightning", ipLightning }
#endif // HAS_LIGHTNING_INFILL
}; };
CONFIG_OPTION_ENUM_DEFINE_STATIC_MAPS(InfillPattern) CONFIG_OPTION_ENUM_DEFINE_STATIC_MAPS(InfillPattern)
@ -1138,9 +1136,7 @@ void PrintConfigDef::init_fff_params()
def->enum_values.push_back("octagramspiral"); def->enum_values.push_back("octagramspiral");
def->enum_values.push_back("adaptivecubic"); def->enum_values.push_back("adaptivecubic");
def->enum_values.push_back("supportcubic"); def->enum_values.push_back("supportcubic");
#if HAS_LIGHTNING_INFILL
def->enum_values.push_back("lightning"); def->enum_values.push_back("lightning");
#endif // HAS_LIGHTNING_INFILL
def->enum_labels.push_back(L("Rectilinear")); def->enum_labels.push_back(L("Rectilinear"));
def->enum_labels.push_back(L("Aligned Rectilinear")); def->enum_labels.push_back(L("Aligned Rectilinear"));
def->enum_labels.push_back(L("Grid")); def->enum_labels.push_back(L("Grid"));
@ -1157,9 +1153,7 @@ void PrintConfigDef::init_fff_params()
def->enum_labels.push_back(L("Octagram Spiral")); def->enum_labels.push_back(L("Octagram Spiral"));
def->enum_labels.push_back(L("Adaptive Cubic")); def->enum_labels.push_back(L("Adaptive Cubic"));
def->enum_labels.push_back(L("Support Cubic")); def->enum_labels.push_back(L("Support Cubic"));
#if HAS_LIGHTNING_INFILL
def->enum_labels.push_back(L("Lightning")); def->enum_labels.push_back(L("Lightning"));
#endif // HAS_LIGHTNING_INFILL
def->set_default_value(new ConfigOptionEnum<InfillPattern>(ipStars)); def->set_default_value(new ConfigOptionEnum<InfillPattern>(ipStars));
def = this->add("first_layer_acceleration", coFloat); def = this->add("first_layer_acceleration", coFloat);

View file

@ -57,14 +57,10 @@ enum class FuzzySkinType {
All, All,
}; };
#define HAS_LIGHTNING_INFILL 0
enum InfillPattern : int { enum InfillPattern : int {
ipRectilinear, ipMonotonic, ipAlignedRectilinear, ipGrid, ipTriangles, ipStars, ipCubic, ipLine, ipConcentric, ipHoneycomb, ip3DHoneycomb, ipRectilinear, ipMonotonic, ipAlignedRectilinear, ipGrid, ipTriangles, ipStars, ipCubic, ipLine, ipConcentric, ipHoneycomb, ip3DHoneycomb,
ipGyroid, ipHilbertCurve, ipArchimedeanChords, ipOctagramSpiral, ipAdaptiveCubic, ipSupportCubic, ipSupportBase, ipGyroid, ipHilbertCurve, ipArchimedeanChords, ipOctagramSpiral, ipAdaptiveCubic, ipSupportCubic, ipSupportBase,
#if HAS_LIGHTNING_INFILL
ipLightning, ipLightning,
#endif // HAS_LIGHTNING_INFILL
ipCount, ipCount,
}; };

View file

@ -14,6 +14,7 @@
#include "TriangleMeshSlicer.hpp" #include "TriangleMeshSlicer.hpp"
#include "Utils.hpp" #include "Utils.hpp"
#include "Fill/FillAdaptive.hpp" #include "Fill/FillAdaptive.hpp"
#include "Fill/FillLightning.hpp"
#include "Format/STL.hpp" #include "Format/STL.hpp"
#include <float.h> #include <float.h>
@ -353,14 +354,15 @@ void PrintObject::infill()
if (this->set_started(posInfill)) { if (this->set_started(posInfill)) {
auto [adaptive_fill_octree, support_fill_octree] = this->prepare_adaptive_infill_data(); auto [adaptive_fill_octree, support_fill_octree] = this->prepare_adaptive_infill_data();
auto lightning_generator = this->prepare_lightning_infill_data();
BOOST_LOG_TRIVIAL(debug) << "Filling layers in parallel - start"; BOOST_LOG_TRIVIAL(debug) << "Filling layers in parallel - start";
tbb::parallel_for( tbb::parallel_for(
tbb::blocked_range<size_t>(0, m_layers.size()), tbb::blocked_range<size_t>(0, m_layers.size()),
[this, &adaptive_fill_octree = adaptive_fill_octree, &support_fill_octree = support_fill_octree](const tbb::blocked_range<size_t>& range) { [this, &adaptive_fill_octree = adaptive_fill_octree, &support_fill_octree = support_fill_octree, &lightning_generator](const tbb::blocked_range<size_t>& range) {
for (size_t layer_idx = range.begin(); layer_idx < range.end(); ++ layer_idx) { for (size_t layer_idx = range.begin(); layer_idx < range.end(); ++ layer_idx) {
m_print->throw_if_canceled(); m_print->throw_if_canceled();
m_layers[layer_idx]->make_fills(adaptive_fill_octree.get(), support_fill_octree.get()); m_layers[layer_idx]->make_fills(adaptive_fill_octree.get(), support_fill_octree.get(), lightning_generator.get());
} }
} }
); );
@ -453,6 +455,18 @@ std::pair<FillAdaptive::OctreePtr, FillAdaptive::OctreePtr> PrintObject::prepare
support_line_spacing ? build_octree(mesh, overhangs.front(), support_line_spacing, true) : OctreePtr()); support_line_spacing ? build_octree(mesh, overhangs.front(), support_line_spacing, true) : OctreePtr());
} }
FillLightning::GeneratorPtr PrintObject::prepare_lightning_infill_data()
{
bool has_lightning_infill = false;
for (size_t region_id = 0; region_id < this->num_printing_regions(); ++region_id)
if (const PrintRegionConfig &config = this->printing_region(region_id).config(); config.fill_density > 0 && config.fill_pattern == ipLightning) {
has_lightning_infill = true;
break;
}
return has_lightning_infill ? FillLightning::build_generator(std::as_const(*this), [this]() -> void { this->throw_if_canceled(); }) : FillLightning::GeneratorPtr();
}
void PrintObject::clear_layers() void PrintObject::clear_layers()
{ {
for (Layer *l : m_layers) for (Layer *l : m_layers)