WIP Refactoring of Layers: LayerIslands filled in with perimeter

extrusions, gap fill extrusions and fill regions.
This commit is contained in:
Vojtech Bubnik 2022-11-02 12:59:31 +01:00
parent 2eb0417018
commit 409fae6183
13 changed files with 891 additions and 560 deletions

View File

@ -187,6 +187,8 @@ public:
friend BoundingBox get_extents_rotated(const Points &points, double angle);
};
using BoundingBoxes = std::vector<BoundingBox>;
class BoundingBox3 : public BoundingBox3Base<Vec3crd>
{
public:

View File

@ -432,6 +432,8 @@ Slic3r::ExPolygons offset_ex(const Slic3r::ExPolygons &expolygons, const float d
{ return PolyTreeToExPolygons(expolygons_offset_pt(expolygons, delta, joinType, miterLimit)); }
Slic3r::ExPolygons offset_ex(const Slic3r::Surfaces &surfaces, const float delta, ClipperLib::JoinType joinType, double miterLimit)
{ return PolyTreeToExPolygons(expolygons_offset_pt(surfaces, delta, joinType, miterLimit)); }
Slic3r::ExPolygons offset_ex(const Slic3r::SurfacesPtr &surfaces, const float delta, ClipperLib::JoinType joinType, double miterLimit)
{ return PolyTreeToExPolygons(expolygons_offset_pt(surfaces, delta, joinType, miterLimit)); }
Polygons offset2(const ExPolygons &expolygons, const float delta1, const float delta2, ClipperLib::JoinType joinType, double miterLimit)
{

View File

@ -326,6 +326,7 @@ Slic3r::ExPolygons offset_ex(const Slic3r::Polygons &polygons, const float delta
Slic3r::ExPolygons offset_ex(const Slic3r::ExPolygon &expolygon, const float delta, ClipperLib::JoinType joinType = DefaultJoinType, double miterLimit = DefaultMiterLimit);
Slic3r::ExPolygons offset_ex(const Slic3r::ExPolygons &expolygons, const float delta, ClipperLib::JoinType joinType = DefaultJoinType, double miterLimit = DefaultMiterLimit);
Slic3r::ExPolygons offset_ex(const Slic3r::Surfaces &surfaces, const float delta, ClipperLib::JoinType joinType = DefaultJoinType, double miterLimit = DefaultMiterLimit);
Slic3r::ExPolygons offset_ex(const Slic3r::SurfacesPtr &surfaces, const float delta, ClipperLib::JoinType joinType = DefaultJoinType, double miterLimit = DefaultMiterLimit);
inline Slic3r::Polygons union_safety_offset (const Slic3r::Polygons &polygons) { return offset (polygons, ClipperSafetyOffset); }
inline Slic3r::Polygons union_safety_offset (const Slic3r::ExPolygons &expolygons) { return offset (expolygons, ClipperSafetyOffset); }

View File

@ -306,6 +306,18 @@ Lines ExPolygon::lines() const
return lines;
}
// Do expolygons match? If they match, they must have the same topology,
// however their contours may be rotated.
bool expolygons_match(const ExPolygon &l, const ExPolygon &r)
{
if (l.holes.size() != r.holes.size() || ! polygons_match(l.contour, r.contour))
return false;
for (size_t hole_idx = 0; hole_idx < l.holes.size(); ++ hole_idx)
if (! polygons_match(l.holes[hole_idx], r.holes[hole_idx]))
return false;
return true;
}
BoundingBox get_extents(const ExPolygon &expolygon)
{
return get_extents(expolygon.contour);

View File

@ -391,6 +391,10 @@ inline ExPolygons expolygons_simplify(const ExPolygons &expolys, double toleranc
return out;
}
// Do expolygons match? If they match, they must have the same topology,
// however their contours may be rotated.
bool expolygons_match(const ExPolygon &l, const ExPolygon &r);
BoundingBox get_extents(const ExPolygon &expolygon);
BoundingBox get_extents(const ExPolygons &expolygons);
BoundingBox get_extents_rotated(const ExPolygon &poly, double angle);

View File

@ -381,24 +381,39 @@ void Layer::make_perimeters()
// keep track of regions whose perimeters we have already generated
std::vector<unsigned char> done(m_regions.size(), false);
std::vector<uint32_t> layer_region_ids;
std::vector<std::pair<ExtrusionRange, ExtrusionRange>> perimeter_and_gapfill_ranges;
ExPolygons fill_expolygons;
std::vector<ExPolygonRange> fill_expolygons_ranges;
SurfacesPtr surfaces_to_merge;
SurfacesPtr surfaces_to_merge_temp;
auto layer_region_reset_perimeters = [](LayerRegion &layerm) {
layerm.m_perimeters.clear();
layerm.m_fills.clear();
layerm.m_thin_fills.clear();
layerm.m_fill_expolygons.clear();
layerm.m_fill_expolygons_bboxes.clear();
layerm.m_fill_expolygons_composite.clear();
layerm.m_fill_expolygons_composite_bboxes.clear();
};
LayerRegionPtrs layerms;
for (LayerRegionPtrs::iterator layerm = m_regions.begin(); layerm != m_regions.end(); ++ layerm)
if ((*layerm)->slices().empty()) {
(*layerm)->m_perimeters.clear();
(*layerm)->m_fills.clear();
(*layerm)->m_thin_fills.clear();
} else {
size_t region_id = layerm - m_regions.begin();
if (done[region_id])
continue;
if (size_t region_id = layerm - m_regions.begin(); ! done[region_id]) {
layer_region_reset_perimeters(**layerm);
if (! (*layerm)->slices().empty()) {
BOOST_LOG_TRIVIAL(trace) << "Generating perimeters for layer " << this->id() << ", region " << region_id;
done[region_id] = true;
const PrintRegionConfig &config = (*layerm)->region().config();
perimeter_and_gapfill_ranges.clear();
fill_expolygons.clear();
fill_expolygons_ranges.clear();
surfaces_to_merge.clear();
// find compatible regions
layerms.clear();
layerms.push_back(*layerm);
layer_region_ids.clear();
layer_region_ids.push_back(region_id);
for (LayerRegionPtrs::const_iterator it = layerm + 1; it != m_regions.end(); ++it)
if (! (*it)->slices().empty()) {
LayerRegion* other_layerm = *it;
@ -418,52 +433,226 @@ void Layer::make_perimeters()
&& config.fuzzy_skin_thickness == other_config.fuzzy_skin_thickness
&& config.fuzzy_skin_point_dist == other_config.fuzzy_skin_point_dist)
{
other_layerm->m_perimeters.clear();
other_layerm->m_fills.clear();
other_layerm->m_thin_fills.clear();
layerms.push_back(other_layerm);
layer_region_reset_perimeters(*other_layerm);
layer_region_ids.push_back(it - m_regions.begin());
done[it - m_regions.begin()] = true;
}
}
ExPolygons fill_expolygons;
if (layerms.size() == 1) { // optimization
(*layerm)->m_fill_expolygons.clear();
(*layerm)->m_fill_surfaces.clear();
(*layerm)->make_perimeters((*layerm)->slices(), fill_expolygons);
(*layerm)->m_fill_expolygons = std::move(fill_expolygons);
if (layer_region_ids.size() == 1) { // optimization
(*layerm)->make_perimeters((*layerm)->slices(), perimeter_and_gapfill_ranges, fill_expolygons, fill_expolygons_ranges);
this->sort_perimeters_into_islands((*layerm)->slices(), region_id, perimeter_and_gapfill_ranges, std::move(fill_expolygons), fill_expolygons_ranges, layer_region_ids);
} else {
SurfaceCollection new_slices;
// Use the region with highest infill rate, as the make_perimeters() function below decides on the gap fill based on the infill existence.
LayerRegion *layerm_config = layerms.front();
LayerRegion *layerm_config = m_regions[layer_region_ids.front()];
{
// group slices (surfaces) according to number of extra perimeters
std::map<unsigned short, Surfaces> slices; // extra_perimeters => [ surface, surface... ]
for (LayerRegion *layerm : layerms) {
for (const Surface &surface : layerm->slices())
slices[surface.extra_perimeters].emplace_back(surface);
if (layerm->region().config().fill_density > layerm_config->region().config().fill_density)
layerm_config = layerm;
layerm->m_fill_surfaces.clear();
layerm->m_fill_expolygons.clear();
// Merge slices (surfaces) according to number of extra perimeters.
for (uint32_t region_id : layer_region_ids) {
LayerRegion &layerm = *m_regions[region_id];
for (const Surface &surface : layerm.slices())
surfaces_to_merge.emplace_back(&surface);
if (layerm.region().config().fill_density > layerm_config->region().config().fill_density)
layerm_config = &layerm;
}
std::sort(surfaces_to_merge.begin(), surfaces_to_merge.end(), [](const Surface *l, const Surface *r){ return l->extra_perimeters < r->extra_perimeters; });
for (size_t i = 0; i < surfaces_to_merge.size();) {
size_t j = i;
const Surface &first = *surfaces_to_merge[i];
size_t extra_perimeters = first.extra_perimeters;
for (; j < surfaces_to_merge.size() && surfaces_to_merge[j]->extra_perimeters == extra_perimeters; ++ j) ;
if (i + 1 == j)
// Nothing to merge, just copy.
new_slices.surfaces.emplace_back(*surfaces_to_merge[i]);
else {
surfaces_to_merge_temp.assign(surfaces_to_merge.begin() + i, surfaces_to_merge.begin() + j);
new_slices.append(offset_ex(surfaces_to_merge_temp, ClipperSafetyOffset), first);
}
i = j;
}
// merge the surfaces assigned to each group
for (std::pair<const unsigned short,Surfaces> &surfaces_with_extra_perimeters : slices)
new_slices.append(offset_ex(surfaces_with_extra_perimeters.second, ClipperSafetyOffset), surfaces_with_extra_perimeters.second.front());
}
// make perimeters
layerm_config->make_perimeters(new_slices, fill_expolygons);
// assign fill_surfaces to each layer
if (! fill_expolygons.empty()) {
// Separate the fill surfaces.
for (LayerRegion *l : layerms)
l->m_fill_expolygons = intersection_ex(l->slices().surfaces, fill_expolygons);
layerm_config->make_perimeters(new_slices, perimeter_and_gapfill_ranges, fill_expolygons, fill_expolygons_ranges);
this->sort_perimeters_into_islands(new_slices, region_id, perimeter_and_gapfill_ranges, std::move(fill_expolygons), fill_expolygons_ranges, layer_region_ids);
}
}
}
BOOST_LOG_TRIVIAL(trace) << "Generating perimeters for layer " << this->id() << " - Done";
}
void Layer::sort_perimeters_into_islands(
// Slices for which perimeters and fill_expolygons were just created.
// The slices may have been created by merging multiple source slices with the same perimeter parameters.
const SurfaceCollection &slices,
// Region where the perimeters, gap fills and fill expolygons are stored.
const uint32_t region_id,
// Perimeters and gap fills produced by the perimeter generator for the slices,
// sorted by the source slices.
const std::vector<std::pair<ExtrusionRange, ExtrusionRange>> &perimeter_and_gapfill_ranges,
// Fill expolygons produced for all source slices above.
ExPolygons &&fill_expolygons,
// Fill expolygon ranges sorted by the source slices.
const std::vector<ExPolygonRange> &fill_expolygons_ranges,
// If the current layer consists of multiple regions, then the fill_expolygons above are split by the source LayerRegion surfaces.
const std::vector<uint32_t> &layer_region_ids)
{
for (LayerSlice &lslice : this->lslices_ex)
lslice.islands.clear();
LayerRegion &this_layer_region = *m_regions[region_id];
// Bounding boxes of fill_expolygons.
BoundingBoxes fill_expolygons_bboxes;
fill_expolygons_bboxes.reserve(fill_expolygons.size());
for (const ExPolygon &expolygon : fill_expolygons)
fill_expolygons_bboxes.emplace_back(get_extents(expolygon));
// Map of source fill_expolygon into region and fill_expolygon of that region.
// -1: not set
std::vector<std::pair<int, int>> map_expolygon_to_region_and_fill;
// assign fill_surfaces to each layer
if (! fill_expolygons.empty()) {
if (layer_region_ids.size() == 1) {
this_layer_region.m_fill_expolygons = std::move(fill_expolygons);
this_layer_region.m_fill_expolygons_bboxes = std::move(fill_expolygons_bboxes);
} else {
// Sort the bounding boxes lexicographically.
std::vector<uint32_t> fill_expolygons_bboxes_sorted(fill_expolygons_bboxes.size());
std::iota(fill_expolygons_bboxes_sorted.begin(), fill_expolygons_bboxes_sorted.end(), 0);
std::sort(fill_expolygons_bboxes_sorted.begin(), fill_expolygons_bboxes_sorted.end(), [&fill_expolygons_bboxes](uint32_t lhs, uint32_t rhs){
const BoundingBox &bbl = fill_expolygons_bboxes[lhs];
const BoundingBox &bbr = fill_expolygons_bboxes[rhs];
return bbl.min < bbr.min || (bbl.min == bbr.min && bbl.max < bbr.max);
});
map_expolygon_to_region_and_fill.assign(fill_expolygons.size(), std::make_pair(-1, -1));
for (uint32_t region_idx : layer_region_ids) {
LayerRegion &l = *m_regions[region_idx];
l.m_fill_expolygons = intersection_ex(l.slices().surfaces, fill_expolygons);
l.m_fill_expolygons_bboxes.reserve(l.fill_expolygons().size());
for (const ExPolygon &expolygon : l.fill_expolygons()) {
BoundingBox bbox = get_extents(expolygon);
l.m_fill_expolygons_bboxes.emplace_back(bbox);
auto it_bbox = std::lower_bound(fill_expolygons_bboxes_sorted.begin(), fill_expolygons_bboxes_sorted.end(), bbox, [&fill_expolygons_bboxes](uint32_t lhs, const BoundingBox &bbr){
const BoundingBox &bbl = fill_expolygons_bboxes[lhs];
return bbl.min < bbr.min || (bbl.min == bbr.min && bbl.max < bbr.max);
});
if (it_bbox != fill_expolygons_bboxes_sorted.end())
if (uint32_t fill_id = *it_bbox; fill_expolygons_bboxes[fill_id] == bbox) {
// With a very high probability the two expolygons match exactly. Confirm that.
if (expolygons_match(expolygon, fill_expolygons[fill_id])) {
std::pair<int, int> &ref = map_expolygon_to_region_and_fill[fill_id];
// Only one expolygon produced by intersection with LayerRegion surface may match an expolygon of fill_expolygons.
assert(ref.first == -1);
ref.first = region_idx;
ref.second = int(&expolygon - l.fill_expolygons().data());
}
}
}
}
}
}
// Traverse the slices in an increasing order of bounding box size, so that the islands inside another islands are tested first,
// so we can just test a point inside ExPolygon::contour and we may skip testing the holes.
auto point_inside_surface = [this](const size_t lslice_idx, const Point &point) {
const BoundingBox &bbox = this->lslices_ex[lslice_idx].bbox;
return point.x() >= bbox.min.x() && point.x() < bbox.max.x() &&
point.y() >= bbox.min.y() && point.y() < bbox.max.y() &&
this->lslices[lslice_idx].contour.contains(point);
};
// Take one sample point for each source slice, to be used to sort source slices into layer slices.
// source slice index + its sample.
std::vector<std::pair<uint32_t, Point>> perimeter_slices_queue;
perimeter_slices_queue.reserve(slices.size());
for (uint32_t islice = 0; islice < uint32_t(slices.size()); ++ islice) {
const std::pair<ExtrusionRange, ExtrusionRange> &extrusions = perimeter_and_gapfill_ranges[islice];
Point sample;
bool sample_set = false;
if (! extrusions.first.empty()) {
sample = this_layer_region.perimeters().entities[*extrusions.first.begin()]->first_point();
sample_set = true;
} else if (! extrusions.second.empty()) {
sample = this_layer_region.thin_fills().entities[*extrusions.second.begin()]->first_point();
sample_set = true;
} else if (const ExPolygonRange &fill_expolygon_range = fill_expolygons_ranges[islice]; ! fill_expolygons.empty()) {
for (uint32_t iexpoly : fill_expolygon_range)
if (const ExPolygon &expoly = fill_expolygons[iexpoly]; ! expoly.empty()) {
sample = expoly.contour.points.front();
sample_set = true;
break;
}
}
if (sample_set)
perimeter_slices_queue.emplace_back(islice, sample);
}
// Sort perimeter extrusions, thin fill extrusions and fill expolygons into islands.
std::vector<uint32_t> region_fill_sorted_last;
for (int lslice_idx = int(this->lslices_ex.size()) - 1; lslice_idx >= 0 && ! perimeter_slices_queue.empty(); -- lslice_idx) {
for (auto it_source_slice = perimeter_slices_queue.begin(); it_source_slice != perimeter_slices_queue.end(); ++ it_source_slice)
if (point_inside_surface(lslice_idx, it_source_slice->second)) {
this->lslices_ex[lslice_idx].islands.push_back({});
LayerIsland &island = this->lslices_ex[lslice_idx].islands.back();
const uint32_t source_slice_idx = it_source_slice->first;
island.perimeters = LayerExtrusionRange(region_id, perimeter_and_gapfill_ranges[source_slice_idx].first);
island.thin_fills = perimeter_and_gapfill_ranges[source_slice_idx].second;
if (ExPolygonRange fill_range = fill_expolygons_ranges[source_slice_idx]; ! fill_range.empty()) {
if (layer_region_ids.size() == 1) {
// Layer island is made of one fill region only.
island.fill_expolygons = fill_range;
island.fill_region_id = region_id;
} else {
// Check whether the fill expolygons of this island were split into multiple regions.
island.fill_region_id = LayerIsland::fill_region_composite_id;
for (uint32_t fill_idx : fill_range) {
const std::pair<int, int> &kvp = map_expolygon_to_region_and_fill[fill_idx];
if (kvp.first == -1 || (island.fill_region_id != -1 && island.fill_region_id != kvp.second)) {
island.fill_region_id = LayerIsland::fill_region_composite_id;
break;
} else
island.fill_region_id = kvp.second;
}
if (island.fill_expolygons_composite()) {
// They were split, thus store the unsplit "composite" expolygons into the region of perimeters.
auto begin = uint32_t(this_layer_region.fill_expolygons_composite().size());
this_layer_region.m_fill_expolygons_composite.reserve(this_layer_region.fill_expolygons_composite().size() + fill_range.size());
std::move(fill_expolygons.begin() + *fill_range.begin(), fill_expolygons.begin() + *fill_range.end(), std::back_inserter(this_layer_region.m_fill_expolygons_composite));
this_layer_region.m_fill_expolygons_composite_bboxes.insert(this_layer_region.m_fill_expolygons_composite_bboxes.end(),
fill_expolygons_bboxes.begin() + *fill_range.begin(), fill_expolygons_bboxes.begin() + *fill_range.end());
island.fill_expolygons = ExPolygonRange(begin, uint32_t(this_layer_region.fill_expolygons_composite().size()));
} else {
if (region_fill_sorted_last.empty())
region_fill_sorted_last.assign(m_regions.size(), 0);
uint32_t &last = region_fill_sorted_last[island.fill_region_id];
// They were not split and they belong to the same region.
// Sort the region m_fill_expolygons to a continuous span.
uint32_t begin = last;
LayerRegion &layerm = *m_regions[island.fill_region_id];
for (uint32_t fill_id : fill_range) {
uint32_t region_fill_id = map_expolygon_to_region_and_fill[fill_id].second;
assert(region_fill_id >= last);
if (region_fill_id > last) {
std::swap(layerm.m_fill_expolygons[region_fill_id], layerm.m_fill_expolygons[last]);
std::swap(layerm.m_fill_expolygons_bboxes[region_fill_id], layerm.m_fill_expolygons_bboxes[last]);
}
++ last;
}
island.fill_expolygons = ExPolygonRange(begin, last);
}
}
}
if (std::next(it_source_slice) != perimeter_slices_queue.end()) {
// Remove the current slice & point pair from the queue.
*it_source_slice = perimeter_slices_queue.back();
perimeter_slices_queue.pop_back();
}
break;
}
}
}
void Layer::export_region_slices_to_svg(const char *path) const
{
BoundingBox bbox;

View File

@ -28,6 +28,71 @@ namespace FillLightning {
class Generator;
};
// Range of indices, providing support for range based loops.
template<typename T>
class IndexRange
{
private:
// Just a bare minimum functionality iterator required by range-for loop.
template<typename T>
class IteratorType {
public:
T operator*() const { return m_idx; }
bool operator!=(const IteratorType &rhs) const { return m_idx != rhs.m_idx; }
void operator++() { ++ m_idx; }
private:
friend class IndexRange<T>;
IteratorType(T idx) : m_idx(idx) {}
T m_idx;
};
// Index of the first extrusion in LayerRegion.
T m_begin { 0 };
// Index of the last extrusion in LayerRegion.
T m_end { 0 };
public:
IndexRange(T ibegin, T iend) : m_begin(ibegin), m_end(iend) {}
IndexRange() = default;
using Iterator = IteratorType<T>;
Iterator begin() const { assert(m_begin <= m_end); return Iterator(m_begin); };
Iterator end() const { assert(m_begin <= m_end); return Iterator(m_end); };
bool empty() const { assert(m_begin <= m_end); return m_begin >= m_end; }
T size() const { assert(m_begin <= m_end); return m_end - m_begin; }
};
using ExtrusionRange = IndexRange<uint32_t>;
using ExPolygonRange = IndexRange<uint32_t>;
// Range of extrusions, referencing the source region by an index.
class LayerExtrusionRange : public ExtrusionRange
{
public:
LayerExtrusionRange(uint32_t iregion, uint32_t ibegin, uint32_t iend) : m_region(iregion), ExtrusionRange(ibegin, iend) {}
LayerExtrusionRange(uint32_t iregion, ExtrusionRange extrusion_range) : m_region(iregion), ExtrusionRange(extrusion_range) {}
LayerExtrusionRange() = default;
// Index of LayerRegion in Layer.
uint32_t region() const { return m_region; };
private:
// Index of LayerRegion in Layer.
uint32_t m_region { 0 };
};
// Most likely one LayerIsland will be filled with maximum one fill type.
static constexpr const size_t LayerExtrusionRangesStaticSize = 1;
using LayerExtrusionRanges =
#ifdef NDEBUG
// To reduce memory allocation in release mode.
boost::container::small_vector<LayerExtrusionRange, LayerExtrusionRangesStaticSize>;
#else // NDEBUG
// To ease debugging.
std::vector<LayerExtrusionRange>;
#endif // NDEBUG
class LayerRegion
{
public:
@ -39,7 +104,16 @@ public:
// divided by type top/bottom/internal
[[nodiscard]] const SurfaceCollection& slices() const { return m_slices; }
// Unspecified fill polygons, used for overhang detection ("ensure vertical wall thickness feature")
// and for re-starting of infills.
[[nodiscard]] const ExPolygons& fill_expolygons() const { return m_fill_expolygons; }
// and their bounding boxes
[[nodiscard]] const BoundingBoxes& fill_expolygons_bboxes() const { return m_fill_expolygons_bboxes; }
// Storage for fill regions produced for a single LayerIsland, of which infill splits into multiple islands.
// Not used for a plain single material print with no infill modifiers.
[[nodiscard]] const ExPolygons& fill_expolygons_composite() const { return m_fill_expolygons_composite; }
// and their bounding boxes
[[nodiscard]] const BoundingBoxes& fill_expolygons_composite_bboxes() const { return m_fill_expolygons_composite_bboxes; }
// collection of surfaces generated by slicing the original geometry
// divided by type top/bottom/internal
@ -67,7 +141,17 @@ public:
void slices_to_fill_surfaces_clipped();
void prepare_fill_surfaces();
void make_perimeters(const SurfaceCollection &slices, ExPolygons &fill_expolygons);
// Produce perimeter extrusions, gap fill extrusions and fill polygons for input slices.
void make_perimeters(
// Input slices for which the perimeters, gap fills and fill expolygons are to be generated.
const SurfaceCollection &slices,
// Ranges of perimeter extrusions and gap fill extrusions per suface, referencing
// newly created extrusions stored at this LayerRegion.
std::vector<std::pair<ExtrusionRange, ExtrusionRange>> &perimeter_and_gapfill_ranges,
// All fill areas produced for all input slices above.
ExPolygons &fill_expolygons,
// Ranges of fill areas above per input slice.
std::vector<ExPolygonRange> &fill_expolygons_ranges);
void process_external_surfaces(const Layer *lower_layer, const Polygons *lower_layer_covered);
double infill_area_threshold() const;
// Trim surfaces by trimming polygons. Used by the elephant foot compensation at the 1st layer.
@ -116,11 +200,18 @@ private:
// Unspecified fill polygons, used for overhang detection ("ensure vertical wall thickness feature")
// and for re-starting of infills.
ExPolygons m_fill_expolygons;
// and their bounding boxes
BoundingBoxes m_fill_expolygons_bboxes;
// Storage for fill regions produced for a single LayerIsland, of which infill splits into multiple islands.
// Not used for a plain single material print with no infill modifiers.
ExPolygons m_fill_expolygons_composite;
// and their bounding boxes
BoundingBoxes m_fill_expolygons_composite_bboxes;
// collection of surfaces for infill generation
// Collection of surfaces for infill generation, created by splitting m_slices by m_fill_expolygons.
SurfaceCollection m_fill_surfaces;
// collection of extrusion paths/loops filling gaps
// Collection of extrusion paths/loops filling gaps
// These fills are generated by the perimeter generator.
// They are not printed on their own, but they are copied to this->fills during infill generation.
ExtrusionEntityCollection m_thin_fills;
@ -141,66 +232,31 @@ private:
// Polygons bridged;
};
// Range of two indices, providing support for range based loops.
template<typename T>
class IndexRange
{
public:
IndexRange(T ibegin, T iend) : m_begin(ibegin), m_end(iend) {}
IndexRange() = default;
T begin() const { assert(m_begin <= m_end); return m_begin; };
T end() const { assert(m_begin <= m_end); return m_end; };
bool empty() const { assert(m_begin <= m_end); return m_begin >= m_end; }
T size() const { assert(m_begin <= m_end); return m_end - m_begin; }
private:
// Index of the first extrusion in LayerRegion.
T m_begin { 0 };
// Index of the last extrusion in LayerRegion.
T m_end { 0 };
};
class LayerExtrusionRange : public IndexRange<uint32_t>
{
public:
LayerExtrusionRange(uint32_t iregion, uint32_t ibegin, uint32_t iend) : m_region(iregion), IndexRange<uint32_t>(ibegin, iend) {}
LayerExtrusionRange() = default;
// Index of LayerRegion in Layer.
uint32_t region() const { return m_region; };
private:
// Index of LayerRegion in Layer.
uint32_t m_region { 0 };
};
static constexpr const size_t LayerExtrusionRangesStaticSize = 1;
using LayerExtrusionRanges =
#ifdef NDEBUG
// To reduce memory allocation in release mode.
boost::container::small_vector<LayerExtrusionRange, LayerExtrusionRangesStaticSize>;
#else // NDEBUG
// To ease debugging.
std::vector<LayerExtrusionRange>;
#endif // NDEBUG
using ExPolygonRange = IndexRange<uint32_t>();
// LayerSlice contains one or more LayerIsland objects,
// each LayerIsland containing a set of perimeter extrusions extruded with one particular PrintRegionConfig parameters
// and one or multiple
struct LayerIsland
{
private:
friend class Layer;
static constexpr const uint32_t fill_region_composite_id = std::numeric_limits<uint32_t>::max();
public:
// Perimeter extrusions in LayerRegion belonging to this island.
LayerExtrusionRange perimeters;
// Thin fills of the same region as perimeters. Generated by classic perimeter generator, while Arachne puts them into perimeters.
ExtrusionRange thin_fills;
// Infill + gapfill extrusions in LayerRegion belonging to this island.
LayerExtrusionRanges fills;
// Region that is to be filled with the fills above.
// Region that is to be filled with the fills above (thin fills, regular fills).
// Pointing to either LayerRegion::fill_expolygons() or LayerRegion::fill_expolygons_composite()
// based on this->fill_expolygons_composite() flag.
ExPolygonRange fill_expolygons;
// Index of LayerRegion with LayerRegion::fill_expolygons() if not fill_expolygons_composite().
uint32_t fill_region_id;
bool fill_expolygons_composite() const { return this->fill_region_id == fill_region_composite_id; }
// Centroid of this island used for path planning.
Point centroid;
// Point centroid;
bool has_extrusions() const { return ! this->perimeters.empty() || ! this->fills.empty(); }
};
@ -215,7 +271,7 @@ using LayerIslands =
std::vector<LayerIsland>;
#endif // NDEBUG
//
// One connected island of a layer. LayerSlice may consist of one or more LayerIslands.
struct LayerSlice
{
struct Link {
@ -235,6 +291,10 @@ struct LayerSlice
BoundingBox bbox;
Links overlaps_above;
Links overlaps_below;
// One island for each region or region set that generates its own perimeters.
// For multi-material prints or prints with regions of different perimeter parameters,
// a LayerSlice may be split into multiple LayerIslands.
// For most prints there will be just one island.
LayerIslands islands;
bool has_extrusions() const { for (const LayerIsland &island : islands) if (island.has_extrusions()) return true; return false; }
@ -324,6 +384,22 @@ protected:
virtual ~Layer();
private:
void sort_perimeters_into_islands(
// Slices for which perimeters and fill_expolygons were just created.
// The slices may have been created by merging multiple source slices with the same perimeter parameters.
const SurfaceCollection &slices,
// Region where the perimeters, gap fills and fill expolygons are stored.
const uint32_t region_id,
// Perimeters and gap fills produced by the perimeter generator for the slices,
// sorted by the source slices.
const std::vector<std::pair<ExtrusionRange, ExtrusionRange>> &perimeter_and_gapfill_ranges,
// Fill expolygons produced for all source slices above.
ExPolygons &&fill_expolygons,
// Fill expolygon ranges sorted by the source slices.
const std::vector<ExPolygonRange> &fill_expolygons_ranges,
// If the current layer consists of multiple regions, then the fill_expolygons above are split by the source LayerRegion surfaces.
const std::vector<uint32_t> &layer_region_ids);
// Sequential index of layer, 0-based, offsetted by number of raft layers.
size_t m_id;
PrintObject *m_object;
@ -337,7 +413,7 @@ public:
// Used to suppress retraction if moving for a support extrusion over these support_islands.
ExPolygons support_islands;
// Slightly inflated bounding boxes of the above, for faster intersection query.
std::vector<BoundingBox> support_islands_bboxes;
BoundingBoxes support_islands_bboxes;
// Extrusion paths for the support base and for the support interface and contacts.
ExtrusionEntityCollection support_fills;

View File

@ -59,11 +59,26 @@ void LayerRegion::slices_to_fill_surfaces_clipped()
}
}
void LayerRegion::make_perimeters(const SurfaceCollection &slices, ExPolygons &fill_expolygons)
// Produce perimeter extrusions, gap fill extrusions and fill polygons for input slices.
void LayerRegion::make_perimeters(
// Input slices for which the perimeters, gap fills and fill expolygons are to be generated.
const SurfaceCollection &slices,
// Ranges of perimeter extrusions and gap fill extrusions per suface, referencing
// newly created extrusions stored at this LayerRegion.
std::vector<std::pair<ExtrusionRange, ExtrusionRange>> &perimeter_and_gapfill_ranges,
// All fill areas produced for all input slices above.
ExPolygons &fill_expolygons,
// Ranges of fill areas above per input slice.
std::vector<ExPolygonRange> &fill_expolygons_ranges)
{
m_perimeters.clear();
m_thin_fills.clear();
perimeter_and_gapfill_ranges.reserve(perimeter_and_gapfill_ranges.size() + slices.size());
// There may be more expolygons produced per slice, thus this reserve is conservative.
fill_expolygons.reserve(fill_expolygons.size() + slices.size());
fill_expolygons_ranges.reserve(fill_expolygons_ranges.size() + slices.size());
const PrintConfig &print_config = this->layer()->object()->print()->config();
const PrintRegionConfig &region_config = this->region().config();
// This needs to be in sync with PrintObject::_slice() slicing_mode_normal_below_layer!
@ -90,11 +105,15 @@ void LayerRegion::make_perimeters(const SurfaceCollection &slices, ExPolygons &f
// Cache for offsetted lower_slices
Polygons lower_layer_polygons_cache;
for (const Surface &surface : slices) {
auto perimeters_begin = uint32_t(m_perimeters.size());
auto gap_fills_begin = uint32_t(m_thin_fills.size());
auto fill_expolygons_begin = uint32_t(fill_expolygons.size());
if (this->layer()->object()->config().perimeter_generator.value == PerimeterGeneratorType::Arachne && !spiral_vase)
PerimeterGenerator::process_arachne(
// input:
params,
&slices,
surface,
lower_slices,
lower_layer_polygons_cache,
// output:
@ -105,13 +124,18 @@ void LayerRegion::make_perimeters(const SurfaceCollection &slices, ExPolygons &f
PerimeterGenerator::process_classic(
// input:
params,
&slices,
surface,
lower_slices,
lower_layer_polygons_cache,
// output:
m_perimeters,
m_thin_fills,
fill_expolygons);
perimeter_and_gapfill_ranges.emplace_back(
ExtrusionRange{ perimeters_begin, uint32_t(m_perimeters.size()) },
ExtrusionRange{ gap_fills_begin, uint32_t(m_thin_fills.size()) });
fill_expolygons_ranges.emplace_back(ExtrusionRange{ fill_expolygons_begin, uint32_t(fill_expolygons.size()) });
}
}
//#define EXTERNAL_SURFACES_OFFSET_PARAMETERS ClipperLib::jtMiter, 3.

View File

@ -601,7 +601,7 @@ static void export_perimeters_to_svg(const std::string &path, const Polygons &co
void PerimeterGenerator::process_arachne(
// Inputs:
const Parameters &params,
const SurfaceCollection *slices,
const Surface &surface,
const ExPolygons *lower_slices,
// Cache:
Polygons &lower_slices_polygons_cache,
@ -633,7 +633,6 @@ void PerimeterGenerator::process_arachne(
// we need to process each island separately because we might have different
// extra perimeters for each one
for (const Surface &surface : slices->surfaces) {
// detect how many perimeters must be generated for this island
int loop_number = params.config.perimeters + surface.extra_perimeters - 1; // 0-indexed loops
ExPolygons last = offset_ex(surface.expolygon.simplify_p(params.scaled_resolution), - float(ext_perimeter_width / 2. - ext_perimeter_spacing / 2.));
@ -822,13 +821,12 @@ void PerimeterGenerator::process_arachne(
union_ex(pp),
float(- min_perimeter_infill_spacing / 2.),
float(inset + min_perimeter_infill_spacing / 2.)));
}
}
void PerimeterGenerator::process_classic(
// Inputs:
const Parameters &params,
const SurfaceCollection *slices,
const Surface &surface,
const ExPolygons *lower_slices,
// Cache:
Polygons &lower_slices_polygons_cache,
@ -875,7 +873,6 @@ void PerimeterGenerator::process_classic(
// we need to process each island separately because we might have different
// extra perimeters for each one
for (const Surface &surface : slices->surfaces) {
// detect how many perimeters must be generated for this island
int loop_number = params.config.perimeters + surface.extra_perimeters - 1; // 0-indexed loops
ExPolygons last = union_ex(surface.expolygon.simplify_p(params.scaled_resolution));
@ -1098,7 +1095,6 @@ void PerimeterGenerator::process_classic(
union_ex(pp),
float(- inset - min_perimeter_infill_spacing / 2.),
float(min_perimeter_infill_spacing / 2.)));
} // for each island
}
}

View File

@ -67,7 +67,7 @@ private:
void process_classic(
// Inputs:
const Parameters &params,
const SurfaceCollection *slices,
const Surface &surface,
const ExPolygons *lower_slices,
// Cache:
Polygons &lower_slices_polygons_cache,
@ -82,7 +82,7 @@ void process_classic(
void process_arachne(
// Inputs:
const Parameters &params,
const SurfaceCollection *slices,
const Surface &surface,
const ExPolygons *lower_slices,
// Cache:
Polygons &lower_slices_polygons_cache,

View File

@ -516,6 +516,26 @@ void remove_collinear(Polygons &polys)
remove_collinear(poly);
}
// Do polygons match? If they match, they must have the same topology,
// however their contours may be rotated.
bool polygons_match(const Polygon &l, const Polygon &r)
{
if (l.size() != r.size())
return false;
auto it_l = std::find(l.points.begin(), l.points.end(), r.points.front());
if (it_l == l.points.end())
return false;
auto it_r = r.points.begin();
for (; it_l != l.points.end(); ++ it_l, ++ it_r)
if (*it_l != *it_r)
return false;
it_l = l.points.begin();
for (; it_r != r.points.end(); ++ it_l, ++ it_r)
if (*it_l != *it_r)
return false;
return true;
}
bool contains(const Polygons &polygons, const Point &p, bool border_result)
{
int poly_count_inside = 0;

View File

@ -250,6 +250,10 @@ inline Polygons to_polygons(std::vector<Points> &&paths)
return out;
}
// Do polygons match? If they match, they must have the same topology,
// however their contours may be rotated.
bool polygons_match(const Polygon &l, const Polygon &r);
// Returns true if inside. Returns border_result if on boundary.
bool contains(const Polygons& polygons, const Point& p, bool border_result = true);

View File

@ -55,6 +55,7 @@ SCENARIO("Perimeter nesting", "[Perimeters]")
static_cast<const PrintConfig&>(config),
false); // spiral_vase
Polygons lower_layer_polygons_cache;
for (const Surface &surface : slices)
// FIXME Lukas H.: Disable this test for Arachne because it is failing and needs more investigation.
// if (config.perimeter_generator == PerimeterGeneratorType::Arachne)
// PerimeterGenerator::process_arachne();
@ -62,7 +63,7 @@ SCENARIO("Perimeter nesting", "[Perimeters]")
PerimeterGenerator::process_classic(
// input:
perimeter_generator_params,
&slices,
surface,
nullptr,
// cache:
lower_layer_polygons_cache,