Skip to content

Commit

Permalink
Fixed missing union before offsetting, and some cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
wawanbreton committed Apr 16, 2024
1 parent 71718c3 commit 1e043b0
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 75 deletions.
7 changes: 6 additions & 1 deletion include/geometry/shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Shape : public LinesSet<Polygon>

Shape(Shape&& other) = default;

Shape(const std::initializer_list<Polygon>& initializer);
Shape(const std::vector<Polygon>& polygons);

explicit Shape(ClipperLib::Paths&& paths, bool explicitely_closed = clipper_explicitely_closed_);

Expand Down Expand Up @@ -68,6 +68,11 @@ class Shape : public LinesSet<Polygon>

Shape intersection(const Shape& other) const;

/*! @brief Overridden definition of offset()
* @note The behavior of this method is exactly the same, but it just exists because it allows
* for a performance optimization */
Shape offset(coord_t distance, ClipperLib::JoinType join_type = ClipperLib::jtMiter, double miter_limit = 1.2) const;

/*!
* Intersect polylines with the area covered by the shape.
*
Expand Down
4 changes: 2 additions & 2 deletions include/geometry/single_shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class SingleShape : public Shape

/*!
* Tests whether the given point is inside this polygon part.
* \param p The point to test whether it is inside.
* \param p The point to test whether it is insisinglehde.
* \param border_result If the point is exactly on the border, this will be
* returned instead.
*/
Expand All @@ -34,4 +34,4 @@ class SingleShape : public Shape

} // namespace cura

#endif // GEOMETRY_MONO_SHAPE_H
#endif // GEOMETRY_SINGLE_SHAPE_H
3 changes: 2 additions & 1 deletion src/WallsComputation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ void WallsComputation::generateWalls(SliceLayerPart* part, SectionType section_t
part->wall_toolpaths = wall_tool_paths.getToolPaths();
part->inner_area = wall_tool_paths.getInnerContour();
}
part->outline = SingleShape{ Simplify(settings_).polygon(part->outline) };

part->outline = SingleShape({ Simplify(settings_).polygon(part->outline) });
part->print_outline = part->outline;
}

Expand Down
74 changes: 5 additions & 69 deletions src/geometry/lines_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,11 @@ Shape LinesSet<Polygon>::offset(coord_t distance, ClipperLib::JoinType join_type
{
if (distance == 0)
{
Shape result;
result.push_back(getLines());
return result;
return Shape(getLines());
}
ClipperLib::Paths ret;
ClipperLib::ClipperOffset clipper(miter_limit, 10.0);
addPaths(clipper, join_type, ClipperLib::etClosedPolygon);
Shape(getLines()).unionPolygons().addPaths(clipper, join_type, ClipperLib::etClosedPolygon);
clipper.MiterLimit = miter_limit;
clipper.Execute(ret, static_cast<double>(distance));
return Shape(std::move(ret));
Expand Down Expand Up @@ -203,71 +201,6 @@ Shape LinesSet<OpenPolyline>::offset(coord_t distance, ClipperLib::JoinType join
return result;
}

#if 0
template<>
Shape LinesSet<ClosedPolyline>::offset(coord_t distance, ClipperLib::JoinType joinType, double miter_limit) const
{
#error Implement me if required
Shape result;

if (distance != 0)
{
Shape polygons;
ClipperLib::ClipperOffset clipper(miter_limit, 10.0);

for (const LineType& line : lines_)
{
if (const Polygon* polygon = dynamic_cast<const Polygon*>(&line))
{
// Union all polygons first and add them later
polygons.push_back(*polygon);

/*temp = Shape(asRawVector()).unionPolygons();
actual_polygons = &temp.asRawVector();
end_type = ClipperLib::etClosedPolygon;*/
}
else
{
ClipperLib::EndType end_type;

if (line.addClosingSegment())
{
end_type = ClipperLib::etClosedLine;
}
else if (joinType == ClipperLib::jtMiter)
{
end_type = ClipperLib::etOpenSquare;
}
else
{
end_type = ClipperLib::etOpenRound;
}

clipper.AddPath(line.getPoints(), joinType, end_type);
}
}

if (! polygons.empty())
{
polygons = polygons.unionPolygons();

for (const Polygon& polygon : polygons)
{
clipper.AddPath(polygon.getPoints(), joinType, ClipperLib::etClosedPolygon);
}
}

clipper.MiterLimit = miter_limit;

ClipperLib::Paths result;
clipper.Execute(result, static_cast<double>(distance));
return Shape(std::move(result));
}

return result;
}
#endif

template<class LineType>
void LinesSet<LineType>::removeDegenerateVerts()
{
Expand Down Expand Up @@ -372,6 +305,7 @@ template coord_t LinesSet<OpenPolyline>::length() const;
template Shape LinesSet<OpenPolyline>::tubeShape(const coord_t inner_offset, const coord_t outer_offset) const;
template void LinesSet<OpenPolyline>::removeDegenerateVerts();
template void LinesSet<OpenPolyline>::addPaths(ClipperLib::Clipper& clipper, ClipperLib::PolyType PolyTyp) const;
template void LinesSet<OpenPolyline>::addPaths(ClipperLib::ClipperOffset& clipper, ClipperLib::JoinType jointType, ClipperLib::EndType endType) const;
template void LinesSet<OpenPolyline>::push_back(const OpenPolyline& line, bool checkNonEmpty);
template void LinesSet<OpenPolyline>::push_back(OpenPolyline&& line, bool checkNonEmpty);
template void LinesSet<OpenPolyline>::push_back(LinesSet<OpenPolyline>&& lines_set);
Expand All @@ -384,6 +318,7 @@ template coord_t LinesSet<ClosedPolyline>::length() const;
template Shape LinesSet<ClosedPolyline>::tubeShape(const coord_t inner_offset, const coord_t outer_offset) const;
template void LinesSet<ClosedPolyline>::removeDegenerateVerts();
template void LinesSet<ClosedPolyline>::addPaths(ClipperLib::Clipper& clipper, ClipperLib::PolyType PolyTyp) const;
template void LinesSet<ClosedPolyline>::addPaths(ClipperLib::ClipperOffset& clipper, ClipperLib::JoinType jointType, ClipperLib::EndType endType) const;
template void LinesSet<ClosedPolyline>::push_back(const ClosedPolyline& line, bool checkNonEmpty);
template void LinesSet<ClosedPolyline>::push_back(ClosedPolyline&& line, bool checkNonEmpty);
template void LinesSet<ClosedPolyline>::push_back(LinesSet<ClosedPolyline>&& lines_set);
Expand All @@ -397,6 +332,7 @@ template coord_t LinesSet<Polygon>::length() const;
template Shape LinesSet<Polygon>::tubeShape(const coord_t inner_offset, const coord_t outer_offset) const;
template void LinesSet<Polygon>::removeDegenerateVerts();
template void LinesSet<Polygon>::addPaths(ClipperLib::Clipper& clipper, ClipperLib::PolyType PolyTyp) const;
template void LinesSet<Polygon>::addPaths(ClipperLib::ClipperOffset& clipper, ClipperLib::JoinType jointType, ClipperLib::EndType endType) const;
template void LinesSet<Polygon>::push_back(const Polygon& line, bool checkNonEmpty);
template void LinesSet<Polygon>::push_back(Polygon&& line, bool checkNonEmpty);
template void LinesSet<Polygon>::push_back(LinesSet<Polygon>&& lines_set);
Expand Down
18 changes: 16 additions & 2 deletions src/geometry/shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ Shape::Shape(ClipperLib::Paths&& paths, bool explicitely_closed)
emplace_back(std::move(paths), explicitely_closed);
}

Shape::Shape(const std::initializer_list<Polygon>& initializer)
: LinesSet<Polygon>(initializer)
Shape::Shape(const std::vector<Polygon>& polygons)
: LinesSet<Polygon>(polygons)
{
}

Expand Down Expand Up @@ -173,6 +173,20 @@ Shape Shape::intersection(const Shape& other) const
return Shape(std::move(ret));
}

Shape Shape::offset(coord_t distance, ClipperLib::JoinType join_type, double miter_limit) const
{
if (distance == 0)
{
return Shape(getLines());
}
ClipperLib::Paths ret;
ClipperLib::ClipperOffset clipper(miter_limit, 10.0);
unionPolygons().addPaths(clipper, join_type, ClipperLib::etClosedPolygon);
clipper.MiterLimit = miter_limit;
clipper.Execute(ret, static_cast<double>(distance));
return Shape(std::move(ret));
}

bool Shape::inside(const Point2LL& p, bool border_result) const
{
int poly_count_inside = 0;
Expand Down

0 comments on commit 1e043b0

Please sign in to comment.