BambuStudio/src/libslic3r/ExPolygon.cpp

600 lines
20 KiB
C++
Raw Normal View History

#include "BoundingBox.hpp"
#include "ExPolygon.hpp"
#include "Exception.hpp"
#include "Geometry/MedialAxis.hpp"
#include "Polygon.hpp"
#include "Line.hpp"
#include "ClipperUtils.hpp"
#include "SVG.hpp"
#include <algorithm>
#include <cassert>
#include <list>
namespace Slic3r {
void ExPolygon::scale(double factor)
{
contour.scale(factor);
for (Polygon &hole : holes)
hole.scale(factor);
}
void ExPolygon::scale(double factor_x, double factor_y)
{
contour.scale(factor_x, factor_y);
for (Polygon &hole : holes)
hole.scale(factor_x, factor_y);
}
void ExPolygon::translate(const Point &p)
{
contour.translate(p);
for (Polygon &hole : holes)
hole.translate(p);
}
void ExPolygon::rotate(double angle)
{
contour.rotate(angle);
for (Polygon &hole : holes)
hole.rotate(angle);
}
void ExPolygon::rotate(double angle, const Point &center)
{
contour.rotate(angle, center);
for (Polygon &hole : holes)
hole.rotate(angle, center);
}
double ExPolygon::area() const
{
double a = this->contour.area();
for (const Polygon &hole : holes)
a -= - hole.area(); // holes have negative area
return a;
}
bool ExPolygon::is_valid() const
{
if (!this->contour.is_valid() || !this->contour.is_counter_clockwise()) return false;
for (Polygons::const_iterator it = this->holes.begin(); it != this->holes.end(); ++it) {
if (!(*it).is_valid() || (*it).is_counter_clockwise()) return false;
}
return true;
}
void ExPolygon::douglas_peucker(double tolerance)
{
this->contour.douglas_peucker(tolerance);
for (Polygon &poly : this->holes)
poly.douglas_peucker(tolerance);
}
bool ExPolygon::contains(const Line &line) const
{
return this->contains(Polyline(line.a, line.b));
}
bool ExPolygon::contains(const Polyline &polyline) const
{
BoundingBox bbox1 = get_extents(*this);
BoundingBox bbox2 = get_extents(polyline);
bbox2.inflated(1);
if (!bbox1.overlap(bbox2))
return false;
return diff_pl(polyline, *this).empty();
}
bool ExPolygon::contains(const Polylines &polylines) const
{
#if 0
BoundingBox bbox = get_extents(polylines);
bbox.merge(get_extents(*this));
SVG svg(debug_out_path("ExPolygon_contains.svg"), bbox);
svg.draw(*this);
svg.draw_outline(*this);
svg.draw(polylines, "blue");
#endif
Polylines pl_out = diff_pl(polylines, *this);
#if 0
svg.draw(pl_out, "red");
#endif
return pl_out.empty();
}
bool ExPolygon::contains(const Point &point, bool border_result /* = true */) const
{
if (! Slic3r::contains(contour, point, border_result))
// Outside the outer contour, not on the contour boundary.
return false;
for (const Polygon &hole : this->holes)
if (Slic3r::contains(hole, point, ! border_result))
// Inside a hole, not on the hole boundary.
return false;
return true;
}
bool ExPolygon::on_boundary(const Point &point, double eps) const
{
if (this->contour.on_boundary(point, eps))
return true;
for (const Polygon &hole : this->holes)
if (hole.on_boundary(point, eps))
return true;
return false;
}
// Projection of a point onto the polygon.
Point ExPolygon::point_projection(const Point &point) const
{
if (this->holes.empty()) {
return this->contour.point_projection(point);
} else {
double dist_min2 = std::numeric_limits<double>::max();
Point closest_pt_min;
for (size_t i = 0; i < this->num_contours(); ++ i) {
Point closest_pt = this->contour_or_hole(i).point_projection(point);
double d2 = (closest_pt - point).cast<double>().squaredNorm();
if (d2 < dist_min2) {
dist_min2 = d2;
closest_pt_min = closest_pt;
}
}
return closest_pt_min;
}
}
void ExPolygon::symmetric_y(const coord_t &y_axis)
{
this->contour.symmetric_y(y_axis);
for (Polygon &hole : holes)
hole.symmetric_y(y_axis);
}
bool ExPolygon::overlaps(const ExPolygon &other) const
{
if (this->empty() || other.empty())
return false;
#if 0
BoundingBox bbox = get_extents(other);
bbox.merge(get_extents(*this));
static int iRun = 0;
SVG svg(debug_out_path("ExPolygon_overlaps-%d.svg", iRun ++), bbox);
svg.draw(*this);
svg.draw_outline(*this);
svg.draw_outline(other, "blue");
#endif
Polylines pl_out = intersection_pl(to_polylines(other), *this);
#if 0
svg.draw(pl_out, "red");
#endif
// See unit test SCENARIO("Clipper diff with polyline", "[Clipper]")
// for in which case the intersection_pl produces any intersection.
return ! pl_out.empty() ||
// If *this is completely inside other, then pl_out is empty, but the expolygons overlap. Test for that situation.
other.contains(this->contour.points.front());
}
bool overlaps(const ExPolygons& expolys1, const ExPolygons& expolys2)
{
for (const ExPolygon& expoly1 : expolys1) {
for (const ExPolygon& expoly2 : expolys2) {
if (expoly1.overlaps(expoly2))
return true;
}
}
return false;
}
bool overlaps(const ExPolygons& expolys, const ExPolygon& expoly)
{
for (const ExPolygon& el : expolys) {
if (el.overlaps(expoly))
return true;
}
return false;
}
Point projection_onto(const ExPolygons& polygons, const Point& from)
{
Point projected_pt;
double min_dist = std::numeric_limits<double>::max();
for (const auto& poly : polygons) {
for (int i = 0; i < poly.num_contours(); i++) {
Point p = from.projection_onto(poly.contour_or_hole(i));
double dist = (from - p).cast<double>().squaredNorm();
if (dist < min_dist) {
projected_pt = p;
min_dist = dist;
}
}
}
return projected_pt;
}
void ExPolygon::simplify_p(double tolerance, Polygons* polygons) const
{
Polygons pp = this->simplify_p(tolerance);
polygons->insert(polygons->end(), pp.begin(), pp.end());
}
Polygons ExPolygon::simplify_p(double tolerance) const
{
Polygons pp;
pp.reserve(this->holes.size() + 1);
// contour
{
Polygon p = this->contour;
p.points.push_back(p.points.front());
p.points = MultiPoint::_douglas_peucker(p.points, tolerance);
p.points.pop_back();
pp.emplace_back(std::move(p));
}
// holes
for (Polygon p : this->holes) {
p.points.push_back(p.points.front());
p.points = MultiPoint::_douglas_peucker(p.points, tolerance);
p.points.pop_back();
pp.emplace_back(std::move(p));
}
return simplify_polygons(pp);
}
ExPolygons ExPolygon::simplify(double tolerance) const
{
return union_ex(this->simplify_p(tolerance));
}
void ExPolygon::simplify(double tolerance, ExPolygons* expolygons) const
{
append(*expolygons, this->simplify(tolerance));
}
void ExPolygon::medial_axis(double min_width, double max_width, ThickPolylines* polylines) const
{
// init helper object
Slic3r::Geometry::MedialAxis ma(min_width, max_width, *this);
// compute the Voronoi diagram and extract medial axis polylines
ThickPolylines pp;
ma.build(&pp);
/*
SVG svg("medial_axis.svg");
svg.draw(*this);
svg.draw(pp);
svg.Close();
*/
/* Find the maximum width returned; we're going to use this for validating and
filtering the output segments. */
double max_w = 0;
for (ThickPolylines::const_iterator it = pp.begin(); it != pp.end(); ++it)
max_w = fmaxf(max_w, *std::max_element(it->width.begin(), it->width.end()));
/* Loop through all returned polylines in order to extend their endpoints to the
expolygon boundaries */
bool removed = false;
for (size_t i = 0; i < pp.size(); ++i) {
ThickPolyline& polyline = pp[i];
// extend initial and final segments of each polyline if they're actual endpoints
/* We assign new endpoints to temporary variables because in case of a single-line
polyline, after we extend the start point it will be caught by the intersection()
call, so we keep the inner point until we perform the second intersection() as well */
Point new_front = polyline.points.front();
Point new_back = polyline.points.back();
if (polyline.endpoints.first && !this->on_boundary(new_front, SCALED_EPSILON)) {
Vec2d p1 = polyline.points.front().cast<double>();
Vec2d p2 = polyline.points[1].cast<double>();
// prevent the line from touching on the other side, otherwise intersection() might return that solution
if (polyline.points.size() == 2)
p2 = (p1 + p2) * 0.5;
// Extend the start of the segment.
p1 -= (p2 - p1).normalized() * max_width;
this->contour.intersection(Line(p1.cast<coord_t>(), p2.cast<coord_t>()), &new_front);
}
if (polyline.endpoints.second && !this->on_boundary(new_back, SCALED_EPSILON)) {
Vec2d p1 = (polyline.points.end() - 2)->cast<double>();
Vec2d p2 = polyline.points.back().cast<double>();
// prevent the line from touching on the other side, otherwise intersection() might return that solution
if (polyline.points.size() == 2)
p1 = (p1 + p2) * 0.5;
// Extend the start of the segment.
p2 += (p2 - p1).normalized() * max_width;
this->contour.intersection(Line(p1.cast<coord_t>(), p2.cast<coord_t>()), &new_back);
}
polyline.points.front() = new_front;
polyline.points.back() = new_back;
/* remove too short polylines
(we can't do this check before endpoints extension and clipping because we don't
know how long will the endpoints be extended since it depends on polygon thickness
which is variable - extension will be <= max_width/2 on each side) */
if ((polyline.endpoints.first || polyline.endpoints.second)
&& polyline.length() < max_w*2) {
pp.erase(pp.begin() + i);
--i;
removed = true;
continue;
}
}
/* If we removed any short polylines we now try to connect consecutive polylines
in order to allow loop detection. Note that this algorithm is greedier than
MedialAxis::process_edge_neighbors() as it will connect random pairs of
polylines even when more than two start from the same point. This has no
drawbacks since we optimize later using nearest-neighbor which would do the
same, but should we use a more sophisticated optimization algorithm we should
not connect polylines when more than two meet. */
if (removed) {
for (size_t i = 0; i < pp.size(); ++i) {
ThickPolyline& polyline = pp[i];
if (polyline.endpoints.first && polyline.endpoints.second) continue; // optimization
// find another polyline starting here
for (size_t j = i+1; j < pp.size(); ++j) {
ThickPolyline& other = pp[j];
if (polyline.last_point() == other.last_point()) {
other.reverse();
} else if (polyline.first_point() == other.last_point()) {
polyline.reverse();
other.reverse();
} else if (polyline.first_point() == other.first_point()) {
polyline.reverse();
} else if (polyline.last_point() != other.first_point()) {
continue;
}
polyline.points.insert(polyline.points.end(), other.points.begin() + 1, other.points.end());
polyline.width.insert(polyline.width.end(), other.width.begin(), other.width.end());
polyline.endpoints.second = other.endpoints.second;
assert(polyline.width.size() == polyline.points.size()*2 - 2);
pp.erase(pp.begin() + j);
j = i; // restart search from i+1
}
}
}
polylines->insert(polylines->end(), pp.begin(), pp.end());
}
void ExPolygon::medial_axis(double min_width, double max_width, Polylines* polylines) const
{
ThickPolylines tp;
this->medial_axis(min_width, max_width, &tp);
polylines->reserve(polylines->size() + tp.size());
for (auto &pl : tp)
polylines->emplace_back(pl.points);
}
ENH: improve tree support ENH: [tree support] use 2 walls if necessary Add impact_strength_z for filaments. Use 2 walls more for weaker filaments. jira: none Change-Id: I0c7cfcff31f1026d57630283775e7aeb6c311146 ENH: [tree support] ease removal of large area supports Use xy_expansion to expand overhangs for better removal of large area supports jira: none Change-Id: Ief59a811204478e0e0a63ae10a9d63f24512fcfb ENH: [tree support]shrink polygon support jira: none Change-Id: I621aa30191d235329156015ef98d260c5b1d9ca8 FIX: fix organic tree, remove ePolygon support more easily 1. add protection to organic tree when xy_distance is set to 0. 2. add detailed classification of nodes(BigFlat && Thinplate && SharpTail && Cantilever && Normal) 3. modify the corresponding generation logic when drawing ePolygon node circles Change-Id: I5c52522b9adad832c9542a400ebf4e1e27a61754 FIX: dont set overhang with large areas to 'SharpTail' Change-Id: Ib118abc54edcb0024be5b0d5d6a51b4bd7b5cb47 FIX: merge support interfaces into a continuous large one jira: STUDIO-8611 github: #5132 Change-Id: I12ee4a9f88a78304a98f354bfaa92e2a05f19ec2 ENH: dont expand sharp tails Also add interface below sharp tails when using soluble supports. Change-Id: Ibf3af1fe466dd7f02f00357c49ad639249efab16 FIX:split holes to help remove when one overhang area contains holes, split it into 4 pieces with one hole's center. when choosing this center hole, consider if it's collided in lower layers, then choose the bigger hole. new: add xy_expansion to gap_width to balance the offset later in draw_circles Change-Id: Ib2452a5f5afdc56e219e69caf8c373d9d9ce8cbf FIX: sync printer preset when sync extruder info jira: none Change-Id: I0b856d38f75816036e428844aabe6fb4c58471f2 FIX: wall count=1 not working Change-Id: Ib9c174b49f217cc76b42c6ecdb8acdec4f7051b7 ENH: merge circle nodes into polygon overhangs nearby AND set the number of support_roof_layers below interface to 2, which developing the interface quality. Change-Id: I3675d37a12321317672a096dc63a9e51e2b1e984 FIX: smooth, floor embedding, delete useless codes Change-Id: I3a5e2634e35e2ed1c310e89fd22b1d75d9cde682 FIX: remove include "Brim.cpp" Change-Id: Id99ff4495f6d0f78d8a865d14d997a5cd9aa0791 FIX: fix sharptail detection in organic tree Change-Id: Ic1b9a0ebf595c814f7d58356270b2a740980c3b4 (cherry picked from commit f9bbc160ae44f3126ae10c4b964fa730d7deb03f)
2024-12-31 06:05:55 +00:00
ExPolygons ExPolygon::split_expoly_with_holes(coord_t gap_width, const ExPolygons& collision) const
{
ExPolygons sub_overhangs;
Polygon max_hole;
coordf_t max_area = 0;
bool is_collided = false;
for (const auto &hole : this->holes) {
if (!is_collided && Slic3r::overlaps({ExPolygon(hole)}, collision)) {
max_area = abs(hole.area());
max_hole = hole;
is_collided = true;
} else if (is_collided && Slic3r::overlaps({ExPolygon(hole)}, collision) && abs(hole.area()) > max_area) {
max_area = abs(hole.area());
max_hole = hole;
} else if (!is_collided && !Slic3r::overlaps({ExPolygon(hole)}, collision) && abs(hole.area()) > max_area) {
max_area = abs(hole.area());
max_hole = hole;
}
}
Point cent;
if (max_hole.size() > 0) {
auto overhang_bbx = get_extents(*this);
cent = max_hole.centroid();
append(sub_overhangs, intersection_ex(ExPolygon(BoundingBox(overhang_bbx.min, Point(cent.x() - gap_width, cent.y() - gap_width)).polygon()), *this));
append(sub_overhangs, intersection_ex(ExPolygon(BoundingBox(Point(cent.x() + gap_width, cent.y() + gap_width), overhang_bbx.max).polygon()), *this));
append(sub_overhangs,
intersection_ex(ExPolygon(BoundingBox(Point(overhang_bbx.min(0), cent.y() + gap_width), Point(cent.x() - gap_width, overhang_bbx.max(1))).polygon()), *this));
append(sub_overhangs,
intersection_ex(ExPolygon(BoundingBox(Point(cent.x() + gap_width, overhang_bbx.min(1)), Point(overhang_bbx.max(0), cent.y() - gap_width)).polygon()), *this));
}
return sub_overhangs;
}
Lines ExPolygon::lines() const
{
Lines lines = this->contour.lines();
for (Polygons::const_iterator h = this->holes.begin(); h != this->holes.end(); ++h) {
Lines hole_lines = h->lines();
lines.insert(lines.end(), hole_lines.begin(), hole_lines.end());
}
return lines;
}
bool ExPolygon::remove_colinear_points() {
bool removed = this->contour.remove_colinear_points();
if (contour.size() < 3) {
contour.points.clear();
holes.clear();
return true;
}
for (Polygon &hole : this->holes)
removed |= hole.remove_colinear_points();
return removed;
}
// 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);
}
BoundingBox get_extents(const ExPolygons &expolygons)
{
BoundingBox bbox;
if (! expolygons.empty()) {
for (size_t i = 0; i < expolygons.size(); ++ i)
if (! expolygons[i].contour.points.empty())
bbox.merge(get_extents(expolygons[i]));
}
return bbox;
}
BoundingBox get_extents_rotated(const ExPolygon &expolygon, double angle)
{
return get_extents_rotated(expolygon.contour, angle);
}
BoundingBox get_extents_rotated(const ExPolygons &expolygons, double angle)
{
BoundingBox bbox;
if (! expolygons.empty()) {
bbox = get_extents_rotated(expolygons.front().contour, angle);
for (size_t i = 1; i < expolygons.size(); ++ i)
bbox.merge(get_extents_rotated(expolygons[i].contour, angle));
}
return bbox;
}
extern std::vector<BoundingBox> get_extents_vector(const ExPolygons &polygons)
{
std::vector<BoundingBox> out;
out.reserve(polygons.size());
for (ExPolygons::const_iterator it = polygons.begin(); it != polygons.end(); ++ it)
out.push_back(get_extents(*it));
return out;
}
bool has_duplicate_points(const ExPolygon &expoly)
{
#if 1
// Check globally.
size_t cnt = expoly.contour.points.size();
for (const Polygon &hole : expoly.holes)
cnt += hole.points.size();
std::vector<Point> allpts;
allpts.reserve(cnt);
allpts.insert(allpts.begin(), expoly.contour.points.begin(), expoly.contour.points.end());
for (const Polygon &hole : expoly.holes)
allpts.insert(allpts.end(), hole.points.begin(), hole.points.end());
return has_duplicate_points(std::move(allpts));
#else
// Check per contour.
if (has_duplicate_points(expoly.contour))
return true;
for (const Polygon &hole : expoly.holes)
if (has_duplicate_points(hole))
return true;
return false;
#endif
}
bool has_duplicate_points(const ExPolygons &expolys)
{
#if 1
// Check globally.
size_t cnt = 0;
for (const ExPolygon &expoly : expolys) {
cnt += expoly.contour.points.size();
for (const Polygon &hole : expoly.holes)
cnt += hole.points.size();
}
std::vector<Point> allpts;
allpts.reserve(cnt);
for (const ExPolygon &expoly : expolys) {
allpts.insert(allpts.begin(), expoly.contour.points.begin(), expoly.contour.points.end());
for (const Polygon &hole : expoly.holes)
allpts.insert(allpts.end(), hole.points.begin(), hole.points.end());
}
return has_duplicate_points(std::move(allpts));
#else
// Check per contour.
for (const ExPolygon &expoly : expolys)
if (has_duplicate_points(expoly))
return true;
return false;
#endif
}
bool remove_same_neighbor(ExPolygons &expolygons)
{
if (expolygons.empty()) return false;
bool remove_from_holes = false;
bool remove_from_contour = false;
for (ExPolygon &expoly : expolygons) {
remove_from_contour |= remove_same_neighbor(expoly.contour);
remove_from_holes |= remove_same_neighbor(expoly.holes);
}
// Removing of expolygons without contour
if (remove_from_contour)
expolygons.erase(std::remove_if(expolygons.begin(), expolygons.end(), [](const ExPolygon &p) { return p.contour.points.size() <= 2; }), expolygons.end());
return remove_from_holes || remove_from_contour;
}
bool remove_sticks(ExPolygon &poly)
{
return remove_sticks(poly.contour) || remove_sticks(poly.holes);
}
bool remove_small_and_small_holes(ExPolygons &expolygons, double min_area)
{
bool modified = false;
size_t free_idx = 0;
for (size_t expoly_idx = 0; expoly_idx < expolygons.size(); ++expoly_idx) {
if (std::abs(expolygons[expoly_idx].area()) >= min_area) {
// Expolygon is big enough, so also check all its holes
modified |= remove_small(expolygons[expoly_idx].holes, min_area);
if (free_idx < expoly_idx) {
std::swap(expolygons[expoly_idx].contour, expolygons[free_idx].contour);
std::swap(expolygons[expoly_idx].holes, expolygons[free_idx].holes);
}
++free_idx;
} else
modified = true;
}
if (free_idx < expolygons.size())
expolygons.erase(expolygons.begin() + free_idx, expolygons.end());
return modified;
}
void keep_largest_contour_only(ExPolygons &polygons)
{
if (polygons.size() > 1) {
double max_area = 0.;
ExPolygon* max_area_polygon = nullptr;
for (ExPolygon& p : polygons) {
double a = p.contour.area();
if (a > max_area) {
max_area = a;
max_area_polygon = &p;
}
}
assert(max_area_polygon != nullptr);
ExPolygon p(std::move(*max_area_polygon));
polygons.clear();
polygons.emplace_back(std::move(p));
}
}
} // namespace Slic3r