Fixed ThickPolyline clear(), which did not clear widths.

Fixed by refactoring ThickPolyline to compose Polyline instead
of being derived of it.
This commit is contained in:
Vojtech Bubnik 2023-01-10 12:18:06 +01:00
parent bb8a001963
commit 7cb3007558
5 changed files with 25 additions and 13 deletions

View File

@ -14,7 +14,7 @@
#include "../../../clipper/clipper_z.hpp"
namespace Slic3r {
class ThickPolyline;
struct ThickPolyline;
}
namespace Slic3r::Arachne

View File

@ -320,7 +320,9 @@ void ExPolygon::medial_axis(double min_width, double max_width, Polylines* polyl
{
ThickPolylines tp;
this->medial_axis(min_width, max_width, &tp);
polylines->insert(polylines->end(), tp.begin(), tp.end());
polylines->reserve(polylines->size() + tp.size());
for (auto &pl : tp)
polylines->emplace_back(pl.points);
}
Lines ExPolygon::lines() const

View File

@ -498,6 +498,7 @@ void MedialAxis::build(ThickPolylines* polylines)
polyline.width.emplace_back(seed_edge_data.width_end);
// Grow the polyline in a forward direction.
this->process_edge_neighbors(&*seed_edge, &polyline);
assert(polyline.width.size() == polyline.points.size() * 2 - 2);
// Grow the polyline in a backward direction.
reverse_polyline.clear();
@ -505,7 +506,6 @@ void MedialAxis::build(ThickPolylines* polylines)
polyline.points.insert(polyline.points.begin(), reverse_polyline.points.rbegin(), reverse_polyline.points.rend());
polyline.width.insert(polyline.width.begin(), reverse_polyline.width.rbegin(), reverse_polyline.width.rend());
polyline.endpoints.first = reverse_polyline.endpoints.second;
assert(polyline.width.size() == polyline.points.size() * 2 - 2);
// Prevent loop endpoints from being extended.
@ -538,7 +538,9 @@ void MedialAxis::build(Polylines* polylines)
{
ThickPolylines tp;
this->build(&tp);
polylines->insert(polylines->end(), tp.begin(), tp.end());
polylines->reserve(polylines->size() + tp.size());
for (auto &pl : tp)
polylines->emplace_back(pl.points);
}
void MedialAxis::process_edge_neighbors(const VD::edge_type *edge, ThickPolyline* polyline)

View File

@ -10,7 +10,7 @@
namespace Slic3r {
class Polyline;
class ThickPolyline;
struct ThickPolyline;
typedef std::vector<Polyline> Polylines;
typedef std::vector<ThickPolyline> ThickPolylines;
@ -160,20 +160,28 @@ bool remove_degenerate(Polylines &polylines);
// Returns index of a segment of a polyline and foot point of pt on polyline.
std::pair<int, Point> foot_pt(const Points &polyline, const Point &pt);
class ThickPolyline : public Polyline {
public:
ThickPolyline() : endpoints(std::make_pair(false, false)) {}
struct ThickPolyline {
ThickPolyline() = default;
ThickLines thicklines() const;
const Point& first_point() const { return this->points.front(); }
const Point& last_point() const { return this->points.back(); }
bool is_valid() const { return this->points.size() >= 2; }
double length() const { return Slic3r::length(this->points); }
void clear() { this->points.clear(); this->width.clear(); }
void reverse() {
Polyline::reverse();
std::reverse(this->points.begin(), this->points.end());
std::reverse(this->width.begin(), this->width.end());
std::swap(this->endpoints.first, this->endpoints.second);
}
void clip_end(double distance);
std::vector<coordf_t> width;
std::pair<bool,bool> endpoints;
Points points;
std::vector<coordf_t> width;
std::pair<bool,bool> endpoints { false, false };
};
inline ThickPolylines to_thick_polylines(Polylines &&polylines, const coordf_t width)

View File

@ -179,8 +179,8 @@ void SVG::draw(const ThickLines &thicklines, const std::string &fill, const std:
void SVG::draw(const ThickPolylines &polylines, const std::string &stroke, coordf_t stroke_width)
{
for (ThickPolylines::const_iterator it = polylines.begin(); it != polylines.end(); ++it)
this->draw((Polyline)*it, stroke, stroke_width);
for (const ThickPolyline &pl : polylines)
this->draw(Polyline(pl.points), stroke, stroke_width);
}
void SVG::draw(const ThickPolylines &thickpolylines, const std::string &fill, const std::string &stroke, coordf_t stroke_width)