Skip to content

Commit

Permalink
Fix of ThickPolyline::clip_end()
Browse files Browse the repository at this point in the history
  • Loading branch information
bubnikv committed Mar 9, 2023
1 parent 6734ad6 commit 9ecf1e4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
45 changes: 25 additions & 20 deletions src/libslic3r/Polyline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,29 +267,34 @@ ThickLines ThickPolyline::thicklines() const
// Removes the given distance from the end of the ThickPolyline
void ThickPolyline::clip_end(double distance)
{
while (distance > 0) {
Vec2d last_point = this->last_point().cast<double>();
coordf_t last_width = this->width.back();
this->points.pop_back();
this->width.pop_back();
if (this->points.empty())
break;

Vec2d vec = this->last_point().cast<double>() - last_point;
coordf_t width_diff = this->width.back() - last_width;
double vec_length_sqr = vec.squaredNorm();
if (vec_length_sqr > distance * distance) {
double t = (distance / std::sqrt(vec_length_sqr));
this->points.emplace_back((last_point + vec * t).cast<coord_t>());
this->width.emplace_back(last_width + width_diff * t);
assert(this->width.size() == (this->points.size() - 1) * 2);
return;
} else
if (! this->empty()) {
assert(this->width.size() == (this->points.size() - 1) * 2);
while (distance > 0) {
Vec2d last_point = this->last_point().cast<double>();
this->points.pop_back();
if (this->points.empty()) {
assert(this->width.empty());
break;
}
coordf_t last_width = this->width.back();
this->width.pop_back();

distance -= std::sqrt(vec_length_sqr);
Vec2d vec = this->last_point().cast<double>() - last_point;
coordf_t width_diff = this->width.back() - last_width;
double vec_length_sqr = vec.squaredNorm();
if (vec_length_sqr > distance * distance) {
double t = (distance / std::sqrt(vec_length_sqr));
this->points.emplace_back((last_point + vec * t).cast<coord_t>());
this->width.emplace_back(last_width + width_diff * t);
assert(this->width.size() == (this->points.size() - 1) * 2);
return;
} else
this->width.pop_back();

distance -= std::sqrt(vec_length_sqr);
}
}
assert(this->width.size() == (this->points.size() - 1) * 2);
assert(this->points.empty() ? this->width.empty() : this->width.size() == (this->points.size() - 1) * 2);
}

void ThickPolyline::start_at_index(int index)
Expand Down
1 change: 1 addition & 0 deletions src/libslic3r/Polyline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ struct ThickPolyline {
const Point& last_point() const { return this->points.back(); }
size_t size() const { return this->points.size(); }
bool is_valid() const { return this->points.size() >= 2; }
bool empty() const { return this->points.empty(); }
double length() const { return Slic3r::length(this->points); }

void clear() { this->points.clear(); this->width.clear(); }
Expand Down

0 comments on commit 9ecf1e4

Please sign in to comment.