Skip to content

Commit

Permalink
Add polygon wkt reading/writing
Browse files Browse the repository at this point in the history
  • Loading branch information
casperlamboo committed Nov 20, 2023
1 parent 8f4caaf commit 6b6dd0f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 6 deletions.
15 changes: 15 additions & 0 deletions include/utils/polygon.h
Original file line number Diff line number Diff line change
Expand Up @@ -1509,6 +1509,21 @@ class Polygons
}

Polygons offset(const std::vector<coord_t>& offset_dists) const;

/*!
* @brief Export the polygon to a WKT string
*
* @param stream The stream to write to
*/
void writeWkt(std::ostream& stream) const;

/*!
* @brief Import the polygon from a WKT string
*
* @param wkt The WKT string to read from
* @return Polygons The polygons read from the stream
*/
static Polygons fromWkt(const std::string& wkt);
};

/*!
Expand Down
64 changes: 58 additions & 6 deletions src/utils/polygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@

#include "utils/polygon.h"

#include <numeric>
#include <unordered_set>

#include <range/v3/view/zip.hpp>
#include <range/v3/range/primitives.hpp>
#include <boost/geometry/io/wkt/read.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <fmt/format.h>
#include <range/v3/to_container.hpp>
#include <range/v3/view/c_str.hpp>
#include <range/v3/view/filter.hpp>

#include "utils/linearAlg2D.h" // pointLiesOnTheRightOfLine
#include <range/v3/view/join.hpp>
#include <range/v3/view/transform.hpp>

#include "utils/ListPolyIt.h"

#include "utils/PolylineStitcher.h"
#include "utils/linearAlg2D.h" // pointLiesOnTheRightOfLine

namespace cura
{
Expand Down Expand Up @@ -775,6 +778,55 @@ Polygons Polygons::toPolygons(ClipperLib::PolyTree& poly_tree)
return ret;
}

Polygons Polygons::fromWkt(const std::string& wkt)
{
typedef boost::geometry::model::d2::point_xy<double> point_type;
typedef boost::geometry::model::polygon<point_type> polygon_type;

polygon_type poly;
boost::geometry::read_wkt(wkt, poly);

Polygons ret;

Polygon outer;
for (const auto& point: poly.outer())
{
outer.add(Point(point.x(), point.y()));
}
ret.add(outer);

for (const auto& hole: poly.inners())
{
Polygon inner;
for (const auto& point: hole)
{
inner.add(Point(point.x(), point.y()));
}
ret.add(inner);
}

return ret;
}

void Polygons::writeWkt(std::ostream& stream) const
{
stream << "POLYGON (";
const auto paths_str = paths
| ranges::views::transform([](const auto& path) {
const auto path_str
= path
| ranges::views::transform([](const auto& point) { return fmt::format("{} {}", point.X, point.Y); })
| ranges::views::join(ranges::views::c_str(", "))
| ranges::to<std::string>();
return "(" + path_str + ")";
})
| ranges::views::join(ranges::views::c_str(" "))
| ranges::to<std::string>();

stream << paths_str;
stream << ")";
}

bool ConstPolygonRef::smooth_corner_complex(const Point p1, ListPolyIt& p0_it, ListPolyIt& p2_it, const int64_t shortcut_length)
{
// walk away from the corner until the shortcut > shortcut_length or it would smooth a piece inward
Expand Down

0 comments on commit 6b6dd0f

Please sign in to comment.