gyroid & 3Dhoneycomb: now 'connected lines' follow the perimeters

This commit is contained in:
supermerill 2019-01-23 10:08:42 +01:00 committed by Yuri D'Elia
parent 8e4f777bd3
commit 86fbb9a095
5 changed files with 377 additions and 77 deletions

View file

@ -161,43 +161,38 @@ void Fill3DHoneycomb::_fill_surface_single(
for (Polylines::iterator it = polylines.begin(); it != polylines.end(); ++ it)
it->translate(bb.min(0), bb.min(1));
// clip pattern to boundaries
polylines = intersection_pl(polylines, (Polygons)expolygon);
// connect lines
if (! params.dont_connect && ! polylines.empty()) { // prevent calling leftmost_point() on empty collections
ExPolygon expolygon_off;
{
ExPolygons expolygons_off = offset_ex(expolygon, SCALED_EPSILON);
if (! expolygons_off.empty()) {
// When expanding a polygon, the number of islands could only shrink. Therefore the offset_ex shall generate exactly one expanded island for one input island.
assert(expolygons_off.size() == 1);
std::swap(expolygon_off, expolygons_off.front());
// clip pattern to boundaries, keeping the polyline order & ordering the fragment to be able to join them easily
Polylines polylines_chained;
for (size_t idx_polyline = 0; idx_polyline < polylines.size(); ++idx_polyline) {
Polyline &poly_to_cut = polylines[idx_polyline];
Polylines polylines_to_sort = intersection_pl(Polylines() = { poly_to_cut }, (Polygons)expolygon);
for (Polyline &polyline : polylines_to_sort) {
//TODO: replace by closest_index_point()
if (poly_to_cut.points.front().distance_to_square(polyline.points.front()) > poly_to_cut.points.front().distance_to_square(polyline.points.back())) {
polyline.reverse();
}
}
Polylines chained = PolylineCollection::chained_path_from(
std::move(polylines),
PolylineCollection::leftmost_point(polylines), false); // reverse allowed
bool first = true;
for (Polylines::iterator it_polyline = chained.begin(); it_polyline != chained.end(); ++ it_polyline) {
if (! first) {
// Try to connect the lines.
Points &pts_end = polylines_out.back().points;
const Point &first_point = it_polyline->points.front();
const Point &last_point = pts_end.back();
// TODO: we should also check that both points are on a fill_boundary to avoid
// connecting paths on the boundaries of internal regions
if ((last_point - first_point).cast<double>().norm() <= 1.5 * distance &&
expolygon_off.contains(Line(last_point, first_point))) {
// Append the polyline.
pts_end.insert(pts_end.end(), it_polyline->points.begin(), it_polyline->points.end());
continue;
if (polylines_to_sort.size() > 1) {
Point nearest = poly_to_cut.points.front();
//Bubble sort
for (size_t idx_sort = polylines_to_sort.size() - 1; idx_sort > 0; idx_sort--) {
for (size_t idx_bubble = 0; idx_bubble < idx_sort; idx_bubble++) {
if (polylines_to_sort[idx_bubble + 1].points.front().distance_to_square(nearest) < polylines_to_sort[idx_bubble].points.front().distance_to_square(nearest)) {
iter_swap(polylines_to_sort.begin() + idx_bubble, polylines_to_sort.begin() + idx_bubble + 1);
}
}
}
// The lines cannot be connected.
polylines_out.emplace_back(std::move(*it_polyline));
first = false;
}
polylines_chained.insert(polylines_chained.end(), polylines_to_sort.begin(), polylines_to_sort.end());
}
// connect lines if needed
if (!polylines_chained.empty()) {
if (params.dont_connect) {
polylines_out.insert(polylines_out.end(), polylines_chained.begin(), polylines_chained.end());
} else {
this->connect_infill(polylines_chained, expolygon, polylines_out);
}
}
}