Skip to content

Commit

Permalink
Ported horizontal_projection() to XS
Browse files Browse the repository at this point in the history
  • Loading branch information
alranel committed Nov 23, 2013
1 parent e75dbf3 commit 4d5d003
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 20 deletions.
20 changes: 0 additions & 20 deletions lib/Slic3r/TriangleMesh.pm
Expand Up @@ -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;
23 changes: 23 additions & 0 deletions xs/src/TriangleMesh.cpp
@@ -1,4 +1,5 @@
#include "TriangleMesh.hpp"
#include "ClipperUtils.hpp"
#include <queue>
#include <deque>
#include <set>
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 2 additions & 0 deletions xs/src/TriangleMesh.hpp
Expand Up @@ -6,6 +6,7 @@
#include <vector>
#include "Point.hpp"
#include "Polygon.hpp"
#include "ExPolygon.hpp"

namespace Slic3r {

Expand All @@ -31,6 +32,7 @@ class TriangleMesh
void slice(const std::vector<double> &z, std::vector<Polygons> &layers);
TriangleMeshPtrs split() const;
void merge(const TriangleMesh* mesh);
void horizontal_projection(ExPolygons &retval) const;
stl_file stl;
bool repaired;

Expand Down
2 changes: 2 additions & 0 deletions xs/xsp/TriangleMesh.xsp
Expand Up @@ -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*
Expand Down

0 comments on commit 4d5d003

Please sign in to comment.