mirror of
https://github.com/SoftFever/OrcaSlicer.git
synced 2025-10-22 00:01:09 -06:00
Incomplete work for porting simplify() to XS
This commit is contained in:
parent
7475762a27
commit
518798beb3
24 changed files with 121 additions and 41 deletions
|
@ -82,6 +82,32 @@ ExPolygon::contains_point(const Point* point) const
|
|||
return true;
|
||||
}
|
||||
|
||||
Polygons
|
||||
ExPolygon::simplify(double tolerance) const
|
||||
{
|
||||
Polygons p;
|
||||
this->contour.simplify(tolerance, p);
|
||||
for (Polygons::const_iterator it = this->holes.begin(); it != this->holes.end(); ++it)
|
||||
it->simplify(tolerance, p);
|
||||
simplify_polygons(p, p);
|
||||
return p;
|
||||
}
|
||||
|
||||
ExPolygons
|
||||
ExPolygon::simplify(double tolerance) const
|
||||
{
|
||||
Polygons p = this->simplify(tolerance);
|
||||
return union_(p);
|
||||
}
|
||||
|
||||
void
|
||||
ExPolygon::simplify(double tolerance, ExPolygons &expolygons) const
|
||||
{
|
||||
ExPolygons ep = this->simplify(tolerance);
|
||||
expolygons.reserve(expolygons.size() + ep.size());
|
||||
expolygons.insert(expolygons.end(), ep.begin(), ep.end());
|
||||
}
|
||||
|
||||
#ifdef SLIC3RXS
|
||||
SV*
|
||||
ExPolygon::to_AV() {
|
||||
|
|
|
@ -19,6 +19,9 @@ class ExPolygon
|
|||
bool is_valid() const;
|
||||
bool contains_line(const Line* line) const;
|
||||
bool contains_point(const Point* point) const;
|
||||
Polygons simplify(double tolerance) const;
|
||||
ExPolygons simplify(double tolerance) const;
|
||||
void simplify(double tolerance, ExPolygons &expolygons) const;
|
||||
|
||||
#ifdef SLIC3RXS
|
||||
void from_SV(SV* poly_sv);
|
||||
|
|
|
@ -47,4 +47,14 @@ ExPolygonCollection::contains_point(const Point* point) const
|
|||
return false;
|
||||
}
|
||||
|
||||
void
|
||||
ExPolygonCollection::simplify(double tolerance)
|
||||
{
|
||||
ExPolygons t;
|
||||
for (ExPolygons::const_iterator it = this->expolygons.begin(); it != this->expolygons.end(); ++it) {
|
||||
it->simplify_and_append_to(tolerance, t);
|
||||
}
|
||||
this->expolygons = t;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ class ExPolygonCollection
|
|||
void translate(double x, double y);
|
||||
void rotate(double angle, Point* center);
|
||||
bool contains_point(const Point* point) const;
|
||||
void simplify(double tolerance);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -78,6 +78,12 @@ ExtrusionPath::clip_end(double distance)
|
|||
this->polyline.clip_end(distance);
|
||||
}
|
||||
|
||||
void
|
||||
ExtrusionPath::simplify(double tolerance)
|
||||
{
|
||||
this->polyline.simplify(tolerance);
|
||||
}
|
||||
|
||||
double
|
||||
ExtrusionPath::length() const
|
||||
{
|
||||
|
|
|
@ -54,6 +54,7 @@ class ExtrusionPath : public ExtrusionEntity
|
|||
ExtrusionEntityCollection* intersect_expolygons(ExPolygonCollection* collection) const;
|
||||
ExtrusionEntityCollection* subtract_expolygons(ExPolygonCollection* collection) const;
|
||||
void clip_end(double distance);
|
||||
void simplify(double tolerance);
|
||||
double length() const;
|
||||
private:
|
||||
ExtrusionEntityCollection* _inflate_collection(const Polylines &polylines) const;
|
||||
|
|
|
@ -55,6 +55,47 @@ MultiPoint::is_valid() const
|
|||
return this->points.size() >= 2;
|
||||
}
|
||||
|
||||
void
|
||||
MultiPoint::simplify(double tolerance)
|
||||
{
|
||||
this->points = MultiPoint::_douglas_peucker(this->points, tolerance);
|
||||
}
|
||||
|
||||
Points
|
||||
MultiPoint::_douglas_peucker(Points &points, double tolerance)
|
||||
{
|
||||
Points results;
|
||||
double dmax = 0;
|
||||
int index = 0;
|
||||
Line full(points.front(), points.back());
|
||||
for (Points::const_iterator it = points.begin() + 1; it != points.end(); ++it) {
|
||||
double d = it->distance_to(full);
|
||||
if (d > dmax) {
|
||||
index = it - points.begin();
|
||||
dmax = d;
|
||||
}
|
||||
}
|
||||
if (dmax >= tolerance) {
|
||||
Points dp0;
|
||||
dp0.reserve(index + 1);
|
||||
dp0.insert(dp0.end(), points.begin(), points.begin() + index + 1);
|
||||
Points dp1 = MultiPoint::_douglas_peucker(dp0, tolerance);
|
||||
results.reserve(results.size() + dp1.size() - 1);
|
||||
results.insert(results.end(), dp1.begin(), dp1.end() - 1);
|
||||
|
||||
dp0.clear();
|
||||
dp0.reserve(points.size() - index + 1);
|
||||
dp0.insert(dp0.end(), points.begin() + index, points.end());
|
||||
dp1 = MultiPoint::_douglas_peucker(dp0, tolerance);
|
||||
results.reserve(results.size() + dp1.size());
|
||||
results.insert(results.end(), dp1.begin(), dp1.end());
|
||||
} else {
|
||||
results.push_back(points.front());
|
||||
results.push_back(points.back());
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
#ifdef SLIC3RXS
|
||||
void
|
||||
MultiPoint::from_SV(SV* poly_sv)
|
||||
|
|
|
@ -21,6 +21,7 @@ class MultiPoint
|
|||
virtual Lines lines() const = 0;
|
||||
double length() const;
|
||||
bool is_valid() const;
|
||||
void simplify(double tolerance);
|
||||
|
||||
#ifdef SLIC3RXS
|
||||
void from_SV(SV* poly_sv);
|
||||
|
@ -28,6 +29,9 @@ class MultiPoint
|
|||
SV* to_AV();
|
||||
SV* to_SV_pureperl() const;
|
||||
#endif
|
||||
|
||||
private:
|
||||
static Points _douglas_peucker(Points &points, double tolerance);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -76,12 +76,18 @@ Point::distance_to(const Point* point) const
|
|||
double
|
||||
Point::distance_to(const Line* line) const
|
||||
{
|
||||
if (line->a.coincides_with(&line->b)) return this->distance_to(&line->a);
|
||||
return this->distance_to(*line);
|
||||
}
|
||||
|
||||
double
|
||||
Point::distance_to(const Line &line) const
|
||||
{
|
||||
if (line.a.coincides_with(&line.b)) return this->distance_to(&line.a);
|
||||
|
||||
double n = (line->b.x - line->a.x) * (line->a.y - this->y)
|
||||
- (line->a.x - this->x) * (line->b.y - line->a.y);
|
||||
double n = (line.b.x - line.a.x) * (line.a.y - this->y)
|
||||
- (line.a.x - this->x) * (line.b.y - line.a.y);
|
||||
|
||||
return abs(n) / line->length();
|
||||
return abs(n) / line.length();
|
||||
}
|
||||
|
||||
#ifdef SLIC3RXS
|
||||
|
|
|
@ -25,6 +25,7 @@ class Point
|
|||
Point* nearest_point(Points points) const;
|
||||
double distance_to(const Point* point) const;
|
||||
double distance_to(const Line* line) const;
|
||||
double distance_to(const Line &line) const;
|
||||
|
||||
#ifdef SLIC3RXS
|
||||
void from_SV(SV* point_sv);
|
||||
|
|
|
@ -125,6 +125,12 @@ Polygon::contains_point(const Point* point) const
|
|||
return result;
|
||||
}
|
||||
|
||||
Polygons
|
||||
Polygon::simplify(double tolerance) const
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#ifdef SLIC3RXS
|
||||
SV*
|
||||
Polygon::to_SV_ref() {
|
||||
|
|
|
@ -24,6 +24,8 @@ class Polygon : public MultiPoint {
|
|||
bool make_clockwise();
|
||||
bool is_valid() const;
|
||||
bool contains_point(const Point* point) const;
|
||||
Polygons simplify(double tolerance) const;
|
||||
void simplify(double tolerance, Polygons &polygons) const;
|
||||
|
||||
#ifdef SLIC3RXS
|
||||
SV* to_SV_ref();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue