Skip to content

Commit

Permalink
Code cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
wawanbreton committed Apr 11, 2024
1 parent 53e566b commit a25fada
Show file tree
Hide file tree
Showing 22 changed files with 140 additions and 1,891 deletions.
12 changes: 1 addition & 11 deletions include/geometry/closed_polyline.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,7 @@ class ClosedPolyline : public Polyline
return ! explicitely_closed_;
}

virtual size_t segmentsCount() const override
{
if (explicitely_closed_)
{
return size() >= 3 ? size() - 1 : 0;
}
else
{
return size() >= 2 ? size() : 0;
}
}
virtual size_t segmentsCount() const override;

ClosedPolyline& operator=(const ClosedPolyline& other)
{
Expand Down
53 changes: 43 additions & 10 deletions include/geometry/mixed_lines_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,70 @@ class Shape;
template<class LineType>
class LinesSet;

using PolylinePtr = std::shared_ptr<Polyline>;
using OpenPolylinePtr = std::shared_ptr<OpenPolyline>;

/*!
* \brief Convenience definition for a container that can hold either open or closed polylines.
* \brief Convenience definition for a container that can hold any type of polyline.
*/
class MixedLinesSet : public std::vector<std::shared_ptr<Polyline>>
class MixedLinesSet : public std::vector<PolylinePtr>
{
public:
/*!
* \brief Computes the offset of all the polylines contained in the set. The polylines may
* be of different types, and polylines are polygons are treated differently.
* \param distance The distance to increase the polylines from, or decrase if negative
* \param join_type The type of tip joint to be used (for open polylines only)
* \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;

/*! @brief Adds a copy of the given polyline to the set
@note As we have to copy the whole points data, this is not very efficient */
void push_back(const OpenPolyline& line);

/*! @brief Adds a copy of the given polyline to the set
@note As we have to copy the whole points data, this is not very efficient */
void push_back(const Polygon& line);

/*! @brief Adds a copy of the given polyline to the set
* @note As we can move the points data, this is much more efficient than the above methods */
void push_back(OpenPolyline&& line);

/*! @brief Adds a copy of the given polyline to the set
* @note As we can move the points data, this is much more efficient than the above methods */
void push_back(ClosedPolyline&& line);

void push_back(const Polygon& line);
/*! @brief Adds the given shared pointer to the set. The pointer reference count will be incremeted but no data is actually copied.
* @note The is even more efficient than the above methods because it only involves copying a pointer */
void push_back(const OpenPolylinePtr& line);

void push_back(const std::shared_ptr<OpenPolyline>& line);
/*! @brief Adds the given shared pointer to the set. The pointer reference count will be incremeted but no data is actually copied.
* @note The is even more efficient than the above methods because it only involves copying a pointer */
void push_back(const PolylinePtr& line);

void push_back(const std::shared_ptr<Polyline>& line);
/*! @brief Adds a copy of all the polygons contained in the shape
@note As we have to copy the whole points data, this is really not efficient */
void push_back(const Shape& shape);

void push_back(LinesSet<OpenPolyline>&& lines_set);
/*! @brief Adds a copy of all the polygons contained in the set
@note As we have to copy the whole points data, this is really not efficient */
void push_back(const LinesSet<Polygon>& lines_set);

/*! @brief Adds a copy of all the polylines contained in the set
@note As we have to copy the whole points data, this is really not efficient */
void push_back(const LinesSet<OpenPolyline>& lines_set);

void push_back(LinesSet<ClosedPolyline>&& lines_set);

void push_back(const LinesSet<Polygon>& lines_set);
/*! @brief Adds a copy of all the polylines contained in the set
* @note As we can move the points data, this is much more efficient than the above methods */
void push_back(LinesSet<OpenPolyline>&& lines_set);

void push_back(const Shape& shape);
/*! @brief Adds a copy of all the polylines contained in the set
* @note As we can move the points data, this is much more efficient than the above methods */
void push_back(LinesSet<ClosedPolyline>&& lines_set);

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

Expand Down
8 changes: 4 additions & 4 deletions include/geometry/points_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const static int clipper_init = (0);
class PointsSet
{
private:
std::vector<Point2LL> points_;
ClipperLib::Path points_;

public:
PointsSet() = default;
Expand All @@ -37,17 +37,17 @@ class PointsSet

PointsSet(ClipperLib::Path&& points);

const std::vector<Point2LL>& getPoints() const
const ClipperLib::Path& getPoints() const
{
return points_;
}

std::vector<Point2LL>& getPoints()
ClipperLib::Path& getPoints()
{
return points_;
}

void setPoints(std::vector<Point2LL>&& points)
void setPoints(ClipperLib::Path&& points)
{
points_ = points;
}
Expand Down
12 changes: 11 additions & 1 deletion include/geometry/polyline.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Polyline : public PointsSet
{
}

Polyline(const std::vector<Point2LL>& points)
Polyline(const ClipperLib::Path& points)
: PointsSet(points)
{
}
Expand All @@ -59,8 +59,18 @@ class Polyline : public PointsSet

virtual ~Polyline() = default;

/*!
* \brief Indicates whether this polyline has a virtual closing segment between the last point
* in the set and the first one
* \return True if a segment between the last and first point should be considered
*/
virtual bool addClosingSegment() const = 0;

/*!
* \brief Gets the total number of "full" segments in the polyline. Calling this is also safe if
* there are not enough points to make a valid polyline, so it can also be a good
* indicator of a "valid" polyline.
*/
virtual size_t segmentsCount() const = 0;

Polyline& operator=(const Polyline& other)
Expand Down
10 changes: 4 additions & 6 deletions include/geometry/shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#define GEOMETRY_SHAPE_H

#include "geometry/lines_set.h"
#include "geometry/polygon.h"
#include "settings/types/Angle.h"

namespace cura
Expand All @@ -15,6 +14,8 @@ class Polygon;
class Ratio;
class SingleShape;
class PartsView;
class PointMatrix;
class Point3Matrix;

class Shape : public LinesSet<Polygon>
{
Expand All @@ -29,10 +30,7 @@ class Shape : public LinesSet<Polygon>

Shape(Shape&& other) = default;

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

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

Expand Down Expand Up @@ -79,9 +77,9 @@ class Shape : public LinesSet<Polygon>
* \param restitch Whether to stitch the resulting segments into longer polylines, or leave every segment as a single segment
* \param max_stitch_distance The maximum distance for two polylines to be stitched together with a segment
* \return The resulting polylines limited to the area of this Polygons object
* \todo This should technically return a MixedLinesSet, because it can definitely contain open and closed polylines, but that is a heavy change
*/
template<class LineType>
#warning Technically this should return a MixedLinesSet
LinesSet<OpenPolyline> intersection(const LinesSet<LineType>& polylines, bool restitch = true, const coord_t max_stitch_distance = 10_mu) const;

/*!
Expand Down
1 change: 0 additions & 1 deletion include/geometry/single_shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#define GEOMETRY_SINGLE_SHAPE_H

#include "geometry/shape.h"
#include "point2ll.h"

namespace cura
{
Expand Down
2 changes: 1 addition & 1 deletion src/FffGcodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1393,7 +1393,7 @@ void FffGcodeWriter::processSkirtBrim(const SliceDataStorage& storage, LayerPlan
push_lines(offset.open_polylines);
*/

for (const std::shared_ptr<Polyline>& line : offset)
for (const PolylinePtr& line : offset)
{
if (line->segmentsCount() > 0)
{
Expand Down
2 changes: 1 addition & 1 deletion src/SkirtBrim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ coord_t SkirtBrim::generateOffset(const Offset& offset, Shape& covered_area, std
std::remove_if(
result.begin(),
result.end(),
[](const std::shared_ptr<Polyline>& line)
[](const PolylinePtr& line)
{
if (const std::shared_ptr<const OpenPolyline> open_line = dynamic_pointer_cast<const OpenPolyline>(line))
{
Expand Down
12 changes: 12 additions & 0 deletions src/geometry/closed_polyline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@
namespace cura
{

size_t ClosedPolyline::segmentsCount() const
{
if (explicitely_closed_)
{
return size() >= 3 ? size() - 1 : 0;
}
else
{
return size() >= 2 ? size() : 0;
}
}

bool ClosedPolyline::inside(const Point2LL& p, bool border_result) const
{
int res = ClipperLib::PointInPolygon(p, getPoints());
Expand Down
21 changes: 11 additions & 10 deletions src/geometry/mixed_lines_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <numeric>

#include "geometry/open_polyline.h"
#include "geometry/polygon.h"
#include "geometry/shape.h"


Expand All @@ -19,7 +20,7 @@ Shape MixedLinesSet::offset(coord_t distance, ClipperLib::JoinType join_type, do
// Return a shape that contains only actual polygons
Shape result;

for (const std::shared_ptr<Polyline>& line : (*this))
for (const PolylinePtr& line : (*this))
{
if (const std::shared_ptr<const Polygon> polygon = dynamic_pointer_cast<const Polygon>(line))
{
Expand All @@ -34,7 +35,7 @@ Shape MixedLinesSet::offset(coord_t distance, ClipperLib::JoinType join_type, do
Shape polygons;
ClipperLib::ClipperOffset clipper(miter_limit, 10.0);

for (const std::shared_ptr<Polyline>& line : (*this))
for (const PolylinePtr& line : (*this))
{
if (const std::shared_ptr<const Polygon> polygon = dynamic_pointer_cast<const Polygon>(line))
{
Expand Down Expand Up @@ -82,32 +83,32 @@ Shape MixedLinesSet::offset(coord_t distance, ClipperLib::JoinType join_type, do

void MixedLinesSet::push_back(const OpenPolyline& line)
{
std::vector<std::shared_ptr<Polyline>>::push_back(std::make_shared<OpenPolyline>(line));
std::vector<PolylinePtr>::push_back(std::make_shared<OpenPolyline>(line));
}

void MixedLinesSet::push_back(OpenPolyline&& line)
{
std::vector<std::shared_ptr<Polyline>>::push_back(std::make_shared<OpenPolyline>(std::move(line)));
std::vector<PolylinePtr>::push_back(std::make_shared<OpenPolyline>(std::move(line)));
}

void MixedLinesSet::push_back(ClosedPolyline&& line)
{
std::vector<std::shared_ptr<Polyline>>::push_back(std::make_shared<ClosedPolyline>(std::move(line)));
std::vector<PolylinePtr>::push_back(std::make_shared<ClosedPolyline>(std::move(line)));
}

void MixedLinesSet::push_back(const Polygon& line)
{
std::vector<std::shared_ptr<Polyline>>::push_back(std::make_shared<Polygon>(std::move(line)));
std::vector<PolylinePtr>::push_back(std::make_shared<Polygon>(std::move(line)));
}

void MixedLinesSet::push_back(const std::shared_ptr<OpenPolyline>& line)
{
std::vector<std::shared_ptr<Polyline>>::push_back(line);
std::vector<PolylinePtr>::push_back(line);
}

void MixedLinesSet::push_back(const std::shared_ptr<Polyline>& line)
void MixedLinesSet::push_back(const PolylinePtr& line)
{
std::vector<std::shared_ptr<Polyline>>::push_back(line);
std::vector<PolylinePtr>::push_back(line);
}

void MixedLinesSet::push_back(LinesSet<OpenPolyline>&& lines_set)
Expand Down Expand Up @@ -157,7 +158,7 @@ coord_t MixedLinesSet::length() const
begin(),
end(),
coord_t(0),
[](coord_t value, const std::shared_ptr<Polyline>& line)
[](coord_t value, const PolylinePtr& line)
{
return value + line->length();
});
Expand Down
1 change: 1 addition & 0 deletions src/geometry/parts_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "geometry/parts_view.h"

#include "geometry/polygon.h"
#include "geometry/single_shape.h"

namespace cura
Expand Down
1 change: 0 additions & 1 deletion src/geometry/points_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "geometry/points_set.h"

#include "geometry/point2ll.h"
#include "geometry/point3_matrix.h"
#include "geometry/point_matrix.h"

Expand Down
23 changes: 2 additions & 21 deletions src/geometry/polygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,11 @@

#include "geometry/polygon.h"

// #include <unordered_set>

// #include <boost/geometry/geometries/point_xy.hpp>
// #include <boost/geometry/geometries/polygon.hpp>
// #include <boost/geometry/io/wkt/read.hpp>
// #include <fmt/format.h>
// #include <range/v3/range/primitives.hpp>
// #include <range/v3/to_container.hpp>
// #include <range/v3/view/c_str.hpp>
// #include <range/v3/view/concat.hpp>
// #include <range/v3/view/filter.hpp>
// #include <range/v3/view/join.hpp>
// #include <range/v3/view/sliding.hpp>
// #include <range/v3/view/take.hpp>
// #include <range/v3/view/transform.hpp>
// #include <range/v3/view/zip.hpp>
// #include <spdlog/spdlog.h>
#include "utils/ListPolyIt.h"
#include "utils/linearAlg2D.h"

// #include "utils/PolylineStitcher.h"
#include "geometry/point3_matrix.h"
#include "geometry/point_matrix.h"
#include "geometry/shape.h"
#include "utils/ListPolyIt.h"
#include "utils/linearAlg2D.h"

namespace cura
{
Expand Down
Loading

0 comments on commit a25fada

Please sign in to comment.