Remove self-intersections before supplying polygon data to polyPartition and rename triangulate2() to triangulate_pp()

This commit is contained in:
Alessandro Ranellucci 2014-05-01 10:37:38 +02:00
parent 9734a40647
commit 60f640f100
7 changed files with 53 additions and 30 deletions

View File

@ -442,14 +442,24 @@ static void traverse_pt(ClipperLib::PolyNodes &nodes, Slic3r::Polygons &retval)
}
}
void simplify_polygons(const Slic3r::Polygons &subject, Slic3r::Polygons &retval)
void simplify_polygons(const Slic3r::Polygons &subject, Slic3r::Polygons &retval, bool preserve_collinear)
{
// convert into Clipper polygons
ClipperLib::Paths* input_subject = new ClipperLib::Paths();
Slic3rMultiPoints_to_ClipperPaths(subject, *input_subject);
ClipperLib::Paths* output = new ClipperLib::Paths();
if (preserve_collinear) {
ClipperLib::Clipper c;
c.PreserveCollinear(true);
c.StrictlySimple(true);
c.AddPaths(*input_subject, ClipperLib::ptSubject, true);
c.Execute(ClipperLib::ctUnion, *output, ClipperLib::pftNonZero, ClipperLib::pftNonZero);
} else {
ClipperLib::SimplifyPolygons(*input_subject, *output, ClipperLib::pftNonZero);
}
delete input_subject;
// convert into Slic3r polygons

View File

@ -98,7 +98,7 @@ void union_pt(const Slic3r::Polygons &subject, ClipperLib::PolyTree &retval, boo
void union_pt_chained(const Slic3r::Polygons &subject, Slic3r::Polygons &retval, bool safety_offset_ = false);
static void traverse_pt(ClipperLib::PolyNodes &nodes, Slic3r::Polygons &retval);
void simplify_polygons(const Slic3r::Polygons &subject, Slic3r::Polygons &retval);
void simplify_polygons(const Slic3r::Polygons &subject, Slic3r::Polygons &retval, bool preserve_collinear = false);
void safety_offset(ClipperLib::Paths* &subject);

View File

@ -239,6 +239,8 @@ ExPolygon::get_trapezoids2(Polygons* polygons, double angle) const
polygon->rotate(-(PI/2 - angle), Point(0,0));
}
// While this triangulates successfully, it's NOT a constrained triangulation
// as it will create more vertices on the boundaries than the ones supplied.
void
ExPolygon::triangulate(Polygons* polygons) const
{
@ -252,34 +254,45 @@ ExPolygon::triangulate(Polygons* polygons) const
}
void
ExPolygon::triangulate2(Polygons* polygons) const
ExPolygon::triangulate_pp(Polygons* polygons) const
{
// convert polygons
std::list<TPPLPoly> input;
Polygons pp = *this;
simplify_polygons(pp, pp, true);
ExPolygons expp;
union_(pp, expp);
for (ExPolygons::const_iterator ex = expp.begin(); ex != expp.end(); ++ex) {
// contour
{
TPPLPoly p;
p.Init(this->contour.points.size());
for (Points::const_iterator point = this->contour.points.begin(); point != this->contour.points.end(); ++point) {
p[ point-this->contour.points.begin() ].x = point->x;
p[ point-this->contour.points.begin() ].y = point->y;
p.Init(ex->contour.points.size());
//printf("%zu\n0\n", ex->contour.points.size());
for (Points::const_iterator point = ex->contour.points.begin(); point != ex->contour.points.end(); ++point) {
p[ point-ex->contour.points.begin() ].x = point->x;
p[ point-ex->contour.points.begin() ].y = point->y;
//printf("%ld %ld\n", point->x, point->y);
}
p.SetHole(false);
input.push_back(p);
}
// holes
for (Polygons::const_iterator hole = this->holes.begin(); hole != this->holes.end(); ++hole) {
for (Polygons::const_iterator hole = ex->holes.begin(); hole != ex->holes.end(); ++hole) {
TPPLPoly p;
p.Init(hole->points.size());
//printf("%zu\n1\n", hole->points.size());
for (Points::const_iterator point = hole->points.begin(); point != hole->points.end(); ++point) {
p[ point-hole->points.begin() ].x = point->x;
p[ point-hole->points.begin() ].y = point->y;
//printf("%ld %ld\n", point->x, point->y);
}
p.SetHole(true);
input.push_back(p);
}
}
// perform triangulation
std::list<TPPLPoly> output;

View File

@ -32,7 +32,7 @@ class ExPolygon
void get_trapezoids2(Polygons* polygons) const;
void get_trapezoids2(Polygons* polygons, double angle) const;
void triangulate(Polygons* polygons) const;
void triangulate2(Polygons* polygons) const;
void triangulate_pp(Polygons* polygons) const;
#ifdef SLIC3RXS
void from_SV(SV* poly_sv);

View File

@ -923,7 +923,7 @@ TriangleMeshSlicer::cut(float z, TriangleMesh* upper, TriangleMesh* lower)
// triangulate section
Polygons triangles;
for (ExPolygons::const_iterator expolygon = section.begin(); expolygon != section.end(); ++expolygon)
expolygon->triangulate2(&triangles);
expolygon->triangulate_pp(&triangles);
// convert triangles to facets and append them to mesh
for (Polygons::const_iterator polygon = triangles.begin(); polygon != triangles.end(); ++polygon) {
@ -951,7 +951,7 @@ TriangleMeshSlicer::cut(float z, TriangleMesh* upper, TriangleMesh* lower)
// triangulate section
Polygons triangles;
for (ExPolygons::const_iterator expolygon = section.begin(); expolygon != section.end(); ++expolygon)
expolygon->triangulate2(&triangles);
expolygon->triangulate_pp(&triangles);
// convert triangles to facets and append them to mesh
for (Polygons::const_iterator polygon = triangles.begin(); polygon != triangles.end(); ++polygon) {

View File

@ -134,7 +134,7 @@ is $expolygon->area, 100*100-20*20, 'area';
}
{
my $triangles = $expolygon->triangulate2;
my $triangles = $expolygon->triangulate_pp;
is scalar(@$triangles), 8, 'expected number of triangles';
is sum(map $_->area, @$triangles), $expolygon->area, 'sum of triangles area equals original expolygon area';
}

View File

@ -36,8 +36,8 @@
%code{% THIS->get_trapezoids2(&RETVAL, angle); %};
Polygons triangulate()
%code{% THIS->triangulate(&RETVAL); %};
Polygons triangulate2()
%code{% THIS->triangulate2(&RETVAL); %};
Polygons triangulate_pp()
%code{% THIS->triangulate_pp(&RETVAL); %};
%{
ExPolygon*