From 4d5d003ba73678d141358690cb1259bef5bdbaa1 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Sat, 23 Nov 2013 19:41:40 +0100 Subject: [PATCH] Ported horizontal_projection() to XS --- lib/Slic3r/TriangleMesh.pm | 20 -------------------- xs/src/TriangleMesh.cpp | 23 +++++++++++++++++++++++ xs/src/TriangleMesh.hpp | 2 ++ xs/xsp/TriangleMesh.xsp | 2 ++ 4 files changed, 27 insertions(+), 20 deletions(-) diff --git a/lib/Slic3r/TriangleMesh.pm b/lib/Slic3r/TriangleMesh.pm index c0f37137c..838e1cd32 100644 --- a/lib/Slic3r/TriangleMesh.pm +++ b/lib/Slic3r/TriangleMesh.pm @@ -29,24 +29,4 @@ sub bounding_box { return Slic3r::Geometry::BoundingBox->new_from_bb($self->bb3); } -# this will return *scaled* expolygons, so it is expected to be run -# on unscaled meshes -sub horizontal_projection { - my $self = shift; - - my ($facets, $vertices) = ($self->facets, $self->vertices); - - my @f = (); - foreach my $facet (@$facets) { - push @f, Slic3r::Polygon->new( - map [ map $_ / &Slic3r::SCALING_FACTOR, @{$vertices->[$_]}[X,Y] ], @$facet - ); - } - - $_->make_counter_clockwise for @f; # do this after scaling, as winding order might change while doing that - - # the offset factor was tuned using groovemount.stl - return union_ex(offset(\@f, Slic3r::Geometry::scale 0.01), 1); -} - 1; diff --git a/xs/src/TriangleMesh.cpp b/xs/src/TriangleMesh.cpp index 962e92a7b..7c554b7e7 100644 --- a/xs/src/TriangleMesh.cpp +++ b/xs/src/TriangleMesh.cpp @@ -1,4 +1,5 @@ #include "TriangleMesh.hpp" +#include "ClipperUtils.hpp" #include #include #include @@ -575,6 +576,28 @@ TriangleMesh::merge(const TriangleMesh* mesh) stl_get_size(&this->stl); } +/* this will return scaled ExPolygons */ +void +TriangleMesh::horizontal_projection(ExPolygons &retval) const +{ + Polygons pp; + pp.reserve(this->stl.stats.number_of_facets); + for (int i = 0; i < this->stl.stats.number_of_facets; i++) { + stl_facet* facet = &this->stl.facet_start[i]; + Polygon p; + p.points.resize(3); + p.points[0] = Point(facet->vertex[0].x / SCALING_FACTOR, facet->vertex[0].y / SCALING_FACTOR); + p.points[1] = Point(facet->vertex[1].x / SCALING_FACTOR, facet->vertex[1].y / SCALING_FACTOR); + p.points[2] = Point(facet->vertex[2].x / SCALING_FACTOR, facet->vertex[2].y / SCALING_FACTOR); + p.make_counter_clockwise(); // do this after scaling, as winding order might change while doing that + pp.push_back(p); + } + + // the offset factor was tuned using groovemount.stl + offset(pp, pp, 0.01 / SCALING_FACTOR); + union_(pp, retval, true); +} + #ifdef SLIC3RXS SV* TriangleMesh::to_SV() { diff --git a/xs/src/TriangleMesh.hpp b/xs/src/TriangleMesh.hpp index cd0b5377d..74f1605c8 100644 --- a/xs/src/TriangleMesh.hpp +++ b/xs/src/TriangleMesh.hpp @@ -6,6 +6,7 @@ #include #include "Point.hpp" #include "Polygon.hpp" +#include "ExPolygon.hpp" namespace Slic3r { @@ -31,6 +32,7 @@ class TriangleMesh void slice(const std::vector &z, std::vector &layers); TriangleMeshPtrs split() const; void merge(const TriangleMesh* mesh); + void horizontal_projection(ExPolygons &retval) const; stl_file stl; bool repaired; diff --git a/xs/xsp/TriangleMesh.xsp b/xs/xsp/TriangleMesh.xsp index 8d6c42375..abee1f4ec 100644 --- a/xs/xsp/TriangleMesh.xsp +++ b/xs/xsp/TriangleMesh.xsp @@ -24,6 +24,8 @@ void rotate(double angle, Point* center); TriangleMeshPtrs split(); void merge(TriangleMesh* mesh); + ExPolygons horizontal_projection() + %code{% THIS->horizontal_projection(RETVAL); %}; %{ SV*