Skip to content

Commit

Permalink
modernized geometry and utility classes
Browse files Browse the repository at this point in the history
Optimized the geometry and utility classes, enhancing the readability and maintenance of the code. Changes included the addition of explicit constructors, default and noexcept specifiers to methods, simplifying conditionals, and replaced old C++11 type traits with newer C++20 concepts where applicable.

Contribute to CURA-9830
  • Loading branch information
jellespijker committed May 3, 2024
1 parent c76d02e commit f533624
Show file tree
Hide file tree
Showing 13 changed files with 212 additions and 211 deletions.
47 changes: 21 additions & 26 deletions include/geometry/ClosedPolyline.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@ class ClosedPolyline : public Polyline
bool explicitely_closed_{ false };

public:
ClosedPolyline() = default;

/*!
* \brief Builds an empty closed polyline
* \param explicitely_closed Indicates whether the line will be explicitely closed
* \warning By default, the line is tagged as explicitely closed. We need this default
* constructor in various places, but be careful that the interpretation of the points
* added later will depend on this.
*/
ClosedPolyline(bool explicitely_closed = false)
: Polyline()
, explicitely_closed_(explicitely_closed)
explicit ClosedPolyline(const bool explicitely_closed)
: explicitely_closed_{ explicitely_closed }
{
}

Expand All @@ -56,8 +57,8 @@ class ClosedPolyline : public Polyline
* \param explicitely_closed Specify whether the given points form an explicitely closed line
*/
ClosedPolyline(const std::initializer_list<Point2LL>& initializer, bool explicitely_closed)
: Polyline(initializer)
, explicitely_closed_(explicitely_closed)
: Polyline{ initializer }
, explicitely_closed_{ explicitely_closed }
{
}

Expand All @@ -66,8 +67,8 @@ class ClosedPolyline : public Polyline
* \param explicitely_closed Specify whether the given points form an explicitely closed line
*/
explicit ClosedPolyline(const ClipperLib::Path& points, bool explicitely_closed)
: Polyline(points)
, explicitely_closed_(explicitely_closed)
: Polyline{ points }
, explicitely_closed_{ explicitely_closed }
{
}

Expand All @@ -76,36 +77,30 @@ class ClosedPolyline : public Polyline
* \param explicitely_closed Specify whether the given points form an explicitely closed line
*/
explicit ClosedPolyline(ClipperLib::Path&& points, bool explicitely_closed)
: Polyline(points)
, explicitely_closed_(explicitely_closed)
: Polyline{ std::move(points) }
, explicitely_closed_{ explicitely_closed }
{
}

~ClosedPolyline() override = default;

/*! @see Polyline::hasClosingSegment() */
virtual bool hasClosingSegment() const
[[nodiscard]] bool hasClosingSegment() const override
{
return ! explicitely_closed_;
}

/*! @see Polyline::addClosingSegment() */
virtual size_t segmentsCount() const override;
[[nodiscard]] size_t segmentsCount() const override;

/*! @see Polyline::isValid() */
virtual bool isValid() const override;
[[nodiscard]] bool isValid() const override;

ClosedPolyline& operator=(const ClosedPolyline& other)
{
Polyline::operator=(other);
return *this;
}
ClosedPolyline& operator=(const ClosedPolyline& other) = default;

ClosedPolyline& operator=(ClosedPolyline&& other)
{
Polyline::operator=(other);
return *this;
}
ClosedPolyline& operator=(ClosedPolyline&& other) = default;

bool isExplicitelyClosed() const
[[nodiscard]] bool isExplicitelyClosed() const
{
return explicitely_closed_;
}
Expand All @@ -126,9 +121,9 @@ class ClosedPolyline : public Polyline
*
* http://www.angusj.com/delphi/clipper/documentation/Docs/Units/ClipperLib/Functions/PointInPolygon.htm
*/
bool inside(const Point2LL& p, bool border_result = false) const;
[[nodiscard]] bool inside(const Point2LL& p, bool border_result = false) const;

bool inside(const ClipperLib::Path& polygon) const;
[[nodiscard]] bool inside(const ClipperLib::Path& polygon) const;

/*!
* \brief Converts the closed polyline to an open polyline which happens to have its end and start points at the same
Expand All @@ -137,7 +132,7 @@ class ClosedPolyline : public Polyline
* between open and closed polylines
* \return An open polyline instance, with the end point at the same position of the start point
*/
OpenPolyline toPseudoOpenPolyline() const;
[[nodiscard]] OpenPolyline toPseudoOpenPolyline() const;
};

} // namespace cura
Expand Down
32 changes: 12 additions & 20 deletions include/geometry/LinesSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ class LinesSet
LinesSet(LinesSet&& other) = default;

/*! \brief Constructor with an existing set of lines */
LinesSet(const std::vector<LineType>& lines)
explicit LinesSet(const std::vector<LineType>& lines)
: lines_(lines)
{
}

/*! \brief Constructor that takes ownership of the data from the given set of lines */
LinesSet(std::vector<LineType>&& lines)
explicit LinesSet(std::vector<LineType>&& lines)
: lines_(std::move(lines))
{
}
Expand All @@ -70,7 +70,7 @@ class LinesSet
* objects, because closed ones require an additional argument
*/
template<typename U = LineType>
requires std::is_same_v<U, OpenPolyline> LinesSet(ClipperLib::Paths&& paths)
requires std::is_same_v<U, OpenPolyline> explicit LinesSet(ClipperLib::Paths&& paths)
{
reserve(paths.size());
for (ClipperLib::Path& path : paths)
Expand Down Expand Up @@ -196,17 +196,9 @@ class LinesSet
return lines_.erase(first, last);
}

LinesSet& operator=(const LinesSet& other)
{
lines_ = other.lines_;
return *this;
}
LinesSet& operator=(const LinesSet& other) = default;

LinesSet& operator=(LinesSet&& other) noexcept
{
lines_ = std::move(other.lines_);
return *this;
}
LinesSet& operator=(LinesSet&& other) noexcept = default;

LineType& operator[](size_t index)
{
Expand All @@ -225,7 +217,7 @@ class LinesSet
}

/*! \brief Return the amount of points in all lines */
size_t pointCount() const;
[[nodiscard]] size_t pointCount() const;

/*!
* Remove a line from the list and move the last line to its place
Expand All @@ -237,15 +229,15 @@ class LinesSet
void addSegment(const Point2LL& from, const Point2LL& to);

/*! \brief Get the total length of all the lines */
coord_t length() const;
[[nodiscard]] coord_t length() const;

void splitIntoSegments(OpenLinesSet& result) const;
OpenLinesSet splitIntoSegments() const;
[[nodiscard]] OpenLinesSet splitIntoSegments() const;

/*! \brief Removes overlapping consecutive line segments which don't delimit a positive area */
void removeDegenerateVerts();

Shape offset(coord_t distance, ClipperLib::JoinType join_type = ClipperLib::jtMiter, double miter_limit = 1.2) const;
[[nodiscard]] Shape offset(coord_t distance, ClipperLib::JoinType join_type = ClipperLib::jtMiter, double miter_limit = 1.2) const;

/*!
* Utility method for creating the tube (or 'donut') of a shape.
Expand All @@ -257,21 +249,21 @@ class LinesSet
* shape. Comparable to normal offset.
* \return The resulting polygons.
*/
Shape createTubeShape(const coord_t inner_offset, const coord_t outer_offset) const;
[[nodiscard]] Shape createTubeShape(const coord_t inner_offset, const coord_t outer_offset) const;

void translate(const Point2LL& delta);

/*!
* \brief Utility method to add all the lines to a ClipperLib::Clipper object
* \note This method needs to be public but you shouldn't need to use it from outside
*/
void addPaths(ClipperLib::Clipper& clipper, ClipperLib::PolyType PolyTyp) const;
void addPaths(ClipperLib::Clipper& clipper, ClipperLib::PolyType poly_typ) const;

/*!
* \brief Utility method to add all the lines to a ClipperLib::ClipperOffset object
* \note This method needs to be public but you shouldn't need to use it from outside
*/
void addPaths(ClipperLib::ClipperOffset& clipper, ClipperLib::JoinType jointType, ClipperLib::EndType endType) const;
void addPaths(ClipperLib::ClipperOffset& clipper, ClipperLib::JoinType joint_type, ClipperLib::EndType end_type) const;

/*!
* \brief Display operator, useful for debugging/testing
Expand Down
4 changes: 2 additions & 2 deletions include/geometry/MixedLinesSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class MixedLinesSet : public std::vector<PolylinePtr>
* \return A shape containing the offsetted polylines. This may contain many unjoined polygons,
* but no overlapping ones.
*/
Shape offset(coord_t distance, ClipperLib::JoinType join_type = ClipperLib::jtMiter, double miter_limit = 1.2) const;
[[nodiscard]] Shape offset(coord_t distance, ClipperLib::JoinType join_type = ClipperLib::jtMiter, double miter_limit = 1.2) const;

/*! @brief Adds a copy of the given polyline to the set */
void push_back(const OpenPolyline& line);
Expand Down Expand Up @@ -72,7 +72,7 @@ class MixedLinesSet : public std::vector<PolylinePtr>
void push_back(ClosedLinesSet&& lines_set);

/*! \brief Computes the total lenght of all the polylines in the set */
coord_t length() const;
[[nodiscard]] coord_t length() const;
};

} // namespace cura
Expand Down
30 changes: 12 additions & 18 deletions include/geometry/OpenPolyline.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ class OpenPolyline : public Polyline
* \warning A copy of the points list is made, so this constructor is somehow "slow"
*/
OpenPolyline(const std::initializer_list<Point2LL>& initializer)
: Polyline(initializer)
: Polyline{ initializer }
{
}

/*!
* \brief Constructor with an existing list of points
* \warning A copy of the points list is made, so this constructor is somehow "slow"
*/
OpenPolyline(const ClipperLib::Path& points)
: Polyline(points)
explicit OpenPolyline(const ClipperLib::Path& points)
: Polyline{ points }
{
}

Expand All @@ -59,40 +59,34 @@ class OpenPolyline : public Polyline
* \warning This constructor is fast because it does not allocate data, but it will clear
* the source object
*/
OpenPolyline(ClipperLib::Path&& points)
: Polyline(std::move(points))
explicit OpenPolyline(ClipperLib::Path&& points)
: Polyline{ std::move(points) }
{
}

~OpenPolyline() override = default;

/*! @see Polyline::hasClosingSegment() */
virtual bool hasClosingSegment() const override
[[nodiscard]] bool hasClosingSegment() const override
{
return false; // Definitely not
}

/*! @see Polyline::segmentsCount() */
virtual size_t segmentsCount() const override
[[nodiscard]] size_t segmentsCount() const override
{
return size() > 1 ? size() - 1 : 0;
}

/*! @see Polyline::isValid() */
virtual bool isValid() const override
[[nodiscard]] bool isValid() const override
{
return size() >= 2;
}

OpenPolyline& operator=(OpenPolyline&& other)
{
Polyline::operator=(std::move(other));
return *this;
}
OpenPolyline& operator=(OpenPolyline&& other) noexcept = default;

OpenPolyline& operator=(const OpenPolyline& other)
{
Polyline::operator=(other);
return *this;
}
OpenPolyline& operator=(const OpenPolyline& other) = default;
};

} // namespace cura
Expand Down
16 changes: 13 additions & 3 deletions include/geometry/PartsView.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,21 @@ class PartsView : public std::vector<std::vector<size_t>>
public:
Shape& polygons_;

PartsView(Shape& polygons)
: polygons_(polygons)
PartsView() = delete;

explicit PartsView(Shape& polygons)
: polygons_{ polygons }
{
}

PartsView(PartsView&& parts_view) = default;
PartsView(const PartsView& parts_view) = default;

~PartsView() = default;

PartsView& operator=(PartsView&& parts_view) = delete;
PartsView& operator=(const PartsView& parts_view) = delete;

/*!
* Get the index of the SingleShape of which the polygon with index \p poly_idx is part.
*
Expand All @@ -50,7 +60,7 @@ class PartsView : public std::vector<std::vector<size_t>>
* \param part_idx The index of the part
* \return The SingleShape with index \p poly_idx
*/
SingleShape assemblePart(size_t part_idx) const;
[[nodiscard]] SingleShape assemblePart(size_t part_idx) const;
};

} // namespace cura
Expand Down
Loading

0 comments on commit f533624

Please sign in to comment.