Finished implementation of TriangleMesh->split

This commit is contained in:
Alessandro Ranellucci 2013-09-09 22:27:58 +02:00
parent 2d4aa439ae
commit 27e7c6b9f7
18 changed files with 59 additions and 23 deletions

View file

@ -51,7 +51,7 @@ ExPolygon::is_valid() const
} }
SV* SV*
ExPolygon::to_SV() { ExPolygon::to_AV() {
const unsigned int num_holes = this->holes.size(); const unsigned int num_holes = this->holes.size();
AV* av = newAV(); AV* av = newAV();
av_extend(av, num_holes); // -1 +1 av_extend(av, num_holes); // -1 +1

View file

@ -13,7 +13,7 @@ class ExPolygon
Polygons holes; Polygons holes;
void from_SV(SV* poly_sv); void from_SV(SV* poly_sv);
void from_SV_check(SV* poly_sv); void from_SV_check(SV* poly_sv);
SV* to_SV(); SV* to_AV();
SV* to_SV_ref(); SV* to_SV_ref();
SV* to_SV_clone_ref() const; SV* to_SV_clone_ref() const;
SV* to_SV_pureperl() const; SV* to_SV_pureperl() const;

View file

@ -61,7 +61,7 @@ Line::from_SV_check(SV* line_sv)
} }
SV* SV*
Line::to_SV() { Line::to_AV() {
AV* av = newAV(); AV* av = newAV();
av_extend(av, 1); av_extend(av, 1);

View file

@ -15,7 +15,7 @@ class Line
explicit Line(Point _a, Point _b): a(_a), b(_b) {}; explicit Line(Point _a, Point _b): a(_a), b(_b) {};
void from_SV(SV* line_sv); void from_SV(SV* line_sv);
void from_SV_check(SV* line_sv); void from_SV_check(SV* line_sv);
SV* to_SV(); SV* to_AV();
SV* to_SV_ref(); SV* to_SV_ref();
SV* to_SV_clone_ref() const; SV* to_SV_clone_ref() const;
SV* to_SV_pureperl() const; SV* to_SV_pureperl() const;

View file

@ -62,7 +62,7 @@ MultiPoint::from_SV_check(SV* poly_sv)
} }
SV* SV*
MultiPoint::to_SV() { MultiPoint::to_AV() {
const unsigned int num_points = this->points.size(); const unsigned int num_points = this->points.size();
AV* av = newAV(); AV* av = newAV();
av_extend(av, num_points-1); av_extend(av, num_points-1);

View file

@ -13,7 +13,7 @@ class MultiPoint
Points points; Points points;
void from_SV(SV* poly_sv); void from_SV(SV* poly_sv);
void from_SV_check(SV* poly_sv); void from_SV_check(SV* poly_sv);
SV* to_SV(); SV* to_AV();
SV* to_SV_pureperl() const; SV* to_SV_pureperl() const;
void scale(double factor); void scale(double factor);
void translate(double x, double y); void translate(double x, double y);

View file

@ -16,6 +16,13 @@ TriangleMesh::~TriangleMesh() {
stl_close(&stl); stl_close(&stl);
} }
SV*
TriangleMesh::to_SV() {
SV* sv = newSV(0);
sv_setref_pv( sv, "Slic3r::TriangleMesh::XS", (void*)this );
return sv;
}
void void
TriangleMesh::ReadSTLFile(char* input_file) { TriangleMesh::ReadSTLFile(char* input_file) {
stl_open(&stl, input_file); stl_open(&stl, input_file);
@ -471,10 +478,10 @@ TriangleMesh::slice(const std::vector<double> &z)
return layers; return layers;
} }
std::vector<TriangleMesh> TriangleMeshPtrs
TriangleMesh::split() const TriangleMesh::split() const
{ {
std::vector<TriangleMesh> meshes; TriangleMeshPtrs meshes;
std::set<int> seen_facets; std::set<int> seen_facets;
// loop while we have remaining facets // loop while we have remaining facets
@ -483,7 +490,8 @@ TriangleMesh::split() const
std::queue<int> facet_queue; std::queue<int> facet_queue;
std::deque<int> facets; std::deque<int> facets;
for (int facet_idx = 0; facet_idx < this->stl.stats.number_of_facets; facet_idx++) { for (int facet_idx = 0; facet_idx < this->stl.stats.number_of_facets; facet_idx++) {
if (seen_facets.find(facet_idx) != seen_facets.end()) { if (seen_facets.find(facet_idx) == seen_facets.end()) {
// if facet was not seen put it into queue and start searching
facet_queue.push(facet_idx); facet_queue.push(facet_idx);
break; break;
} }
@ -493,7 +501,7 @@ TriangleMesh::split() const
while (!facet_queue.empty()) { while (!facet_queue.empty()) {
int facet_idx = facet_queue.front(); int facet_idx = facet_queue.front();
facet_queue.pop(); facet_queue.pop();
if (seen_facets.find(facet_idx) == seen_facets.end()) continue; if (seen_facets.find(facet_idx) != seen_facets.end()) continue;
facets.push_back(facet_idx); facets.push_back(facet_idx);
for (int j = 0; j <= 2; j++) { for (int j = 0; j <= 2; j++) {
facet_queue.push(this->stl.neighbors_start[facet_idx].neighbor[j]); facet_queue.push(this->stl.neighbors_start[facet_idx].neighbor[j]);
@ -501,8 +509,8 @@ TriangleMesh::split() const
seen_facets.insert(facet_idx); seen_facets.insert(facet_idx);
} }
meshes.resize(meshes.size()+1); TriangleMesh* mesh = new TriangleMesh;
TriangleMesh* mesh = &(meshes.back()); meshes.push_back(mesh);
stl_initialize(&mesh->stl); stl_initialize(&mesh->stl);
mesh->stl.stats.type = inmemory; mesh->stl.stats.type = inmemory;
mesh->stl.stats.number_of_facets = facets.size(); mesh->stl.stats.number_of_facets = facets.size();

View file

@ -9,11 +9,15 @@
namespace Slic3r { namespace Slic3r {
class TriangleMesh;
typedef std::vector<TriangleMesh*> TriangleMeshPtrs;
class TriangleMesh class TriangleMesh
{ {
public: public:
TriangleMesh(); TriangleMesh();
~TriangleMesh(); ~TriangleMesh();
SV* to_SV();
void ReadSTLFile(char* input_file); void ReadSTLFile(char* input_file);
void ReadFromPerl(SV* vertices, SV* facets); void ReadFromPerl(SV* vertices, SV* facets);
void Repair(); void Repair();
@ -23,7 +27,7 @@ class TriangleMesh
void align_to_origin(); void align_to_origin();
void rotate(double angle, Point* center); void rotate(double angle, Point* center);
std::vector<Polygons>* slice(const std::vector<double> &z); std::vector<Polygons>* slice(const std::vector<double> &z);
std::vector<TriangleMesh> split() const; TriangleMeshPtrs split() const;
stl_file stl; stl_file stl;
}; };

View file

@ -4,7 +4,7 @@ use strict;
use warnings; use warnings;
use Slic3r::XS; use Slic3r::XS;
use Test::More tests => 43; use Test::More tests => 45;
is Slic3r::TriangleMesh::XS::hello_world(), 'Hello world!', is Slic3r::TriangleMesh::XS::hello_world(), 'Hello world!',
'hello world'; 'hello world';
@ -42,6 +42,10 @@ my $cube = {
$m->rotate(45, Slic3r::Point->new(20,20)); $m->rotate(45, Slic3r::Point->new(20,20));
ok abs($m->size->[0] - sqrt(2)*40) < 1E-4, 'rotate'; ok abs($m->size->[0] - sqrt(2)*40) < 1E-4, 'rotate';
my $result = $m->split;
is scalar(@$result), 1, 'split';
isa_ok $result->[0], 'Slic3r::TriangleMesh::XS', 'split';
} }
{ {

View file

@ -10,7 +10,7 @@
ExPolygon* clone() ExPolygon* clone()
%code{% const char* CLASS = "Slic3r::ExPolygon"; RETVAL = new ExPolygon(*THIS); %}; %code{% const char* CLASS = "Slic3r::ExPolygon"; RETVAL = new ExPolygon(*THIS); %};
SV* arrayref() SV* arrayref()
%code{% RETVAL = THIS->to_SV(); %}; %code{% RETVAL = THIS->to_AV(); %};
SV* pp() SV* pp()
%code{% RETVAL = THIS->to_SV_pureperl(); %}; %code{% RETVAL = THIS->to_SV_pureperl(); %};
Polygon* contour() Polygon* contour()

View file

@ -8,7 +8,7 @@
%name{Slic3r::ExtrusionLoop} class ExtrusionLoop { %name{Slic3r::ExtrusionLoop} class ExtrusionLoop {
~ExtrusionLoop(); ~ExtrusionLoop();
SV* arrayref() SV* arrayref()
%code{% RETVAL = THIS->polygon.to_SV(); %}; %code{% RETVAL = THIS->polygon.to_AV(); %};
SV* pp() SV* pp()
%code{% RETVAL = THIS->polygon.to_SV_pureperl(); %}; %code{% RETVAL = THIS->polygon.to_SV_pureperl(); %};
void reverse() void reverse()

View file

@ -8,7 +8,7 @@
%name{Slic3r::ExtrusionPath} class ExtrusionPath { %name{Slic3r::ExtrusionPath} class ExtrusionPath {
~ExtrusionPath(); ~ExtrusionPath();
SV* arrayref() SV* arrayref()
%code{% RETVAL = THIS->polyline.to_SV(); %}; %code{% RETVAL = THIS->polyline.to_AV(); %};
SV* pp() SV* pp()
%code{% RETVAL = THIS->polyline.to_SV_pureperl(); %}; %code{% RETVAL = THIS->polyline.to_SV_pureperl(); %};
void pop_back() void pop_back()

View file

@ -10,7 +10,7 @@
Line* clone() Line* clone()
%code{% const char* CLASS = "Slic3r::Line"; RETVAL = new Line(*THIS); %}; %code{% const char* CLASS = "Slic3r::Line"; RETVAL = new Line(*THIS); %};
SV* arrayref() SV* arrayref()
%code{% RETVAL = THIS->to_SV(); %}; %code{% RETVAL = THIS->to_AV(); %};
SV* pp() SV* pp()
%code{% RETVAL = THIS->to_SV_pureperl(); %}; %code{% RETVAL = THIS->to_SV_pureperl(); %};
Point* a() Point* a()

View file

@ -10,7 +10,7 @@
Polygon* clone() Polygon* clone()
%code{% const char* CLASS = "Slic3r::Polygon"; RETVAL = new Polygon(*THIS); %}; %code{% const char* CLASS = "Slic3r::Polygon"; RETVAL = new Polygon(*THIS); %};
SV* arrayref() SV* arrayref()
%code{% RETVAL = THIS->to_SV(); %}; %code{% RETVAL = THIS->to_AV(); %};
SV* pp() SV* pp()
%code{% RETVAL = THIS->to_SV_pureperl(); %}; %code{% RETVAL = THIS->to_SV_pureperl(); %};
void scale(double factor); void scale(double factor);

View file

@ -10,7 +10,7 @@
Polyline* clone() Polyline* clone()
%code{% const char* CLASS = "Slic3r::Polyline"; RETVAL = new Polyline(*THIS); %}; %code{% const char* CLASS = "Slic3r::Polyline"; RETVAL = new Polyline(*THIS); %};
SV* arrayref() SV* arrayref()
%code{% RETVAL = THIS->to_SV(); %}; %code{% RETVAL = THIS->to_AV(); %};
SV* pp() SV* pp()
%code{% RETVAL = THIS->to_SV_pureperl(); %}; %code{% RETVAL = THIS->to_SV_pureperl(); %};
void scale(double factor); void scale(double factor);

View file

@ -16,6 +16,7 @@
void translate(float x, float y, float z); void translate(float x, float y, float z);
void align_to_origin(); void align_to_origin();
void rotate(double angle, Point* center); void rotate(double angle, Point* center);
TriangleMeshPtrs split();
%{ %{
SV* SV*

View file

@ -18,13 +18,22 @@ SurfaceType T_UV
ClipperLib::JoinType T_UV ClipperLib::JoinType T_UV
ClipperLib::PolyFillType T_UV ClipperLib::PolyFillType T_UV
# we return these types whenever we want the items to be cloned
Points T_ARRAYREF Points T_ARRAYREF
Lines T_ARRAYREF Lines T_ARRAYREF
Polygons T_ARRAYREF Polygons T_ARRAYREF
ExPolygons T_ARRAYREF ExPolygons T_ARRAYREF
# we return these types whenever we want the items to be returned
# by reference and marked ::Ref because they're contained in another
# Perl object
Polygons* T_ARRAYREF_PTR Polygons* T_ARRAYREF_PTR
# we return these types whenever we want the items to be returned
# by reference and not marked ::Ref because they're newly allocated
# and not referenced by any Perl object
TriangleMeshPtrs T_PTR_ARRAYREF
INPUT INPUT
T_ARRAYREF T_ARRAYREF
@ -52,7 +61,7 @@ T_ARRAYREF
av_extend(av, $var.size()-1); av_extend(av, $var.size()-1);
int i = 0; int i = 0;
for (${type}::const_iterator it = $var.begin(); it != $var.end(); ++it) { for (${type}::const_iterator it = $var.begin(); it != $var.end(); ++it) {
av_store(av, i++, (*it).to_SV_clone_ref()); av_store(av, i++, it->to_SV_clone_ref());
} }
$var.clear(); $var.clear();
@ -63,5 +72,15 @@ T_ARRAYREF_PTR
av_extend(av, $var->size()-1); av_extend(av, $var->size()-1);
int i = 0; int i = 0;
for (${ my $t = $type; $t =~ s/\*$//; \$t }::iterator it = $var->begin(); it != $var->end(); ++it) { for (${ my $t = $type; $t =~ s/\*$//; \$t }::iterator it = $var->begin(); it != $var->end(); ++it) {
av_store(av, i++, (*it).to_SV_ref()); av_store(av, i++, it->to_SV_ref());
}
T_PTR_ARRAYREF
AV* av = newAV();
$arg = newRV_noinc((SV*)av);
sv_2mortal($arg);
av_extend(av, $var.size()-1);
int i = 0;
for (${type}::iterator it = $var.begin(); it != $var.end(); ++it) {
av_store(av, i++, (*it)->to_SV());
} }

View file

@ -16,7 +16,7 @@
%typemap{Polygons}; %typemap{Polygons};
%typemap{ExPolygons}; %typemap{ExPolygons};
%typemap{Polygons*}; %typemap{Polygons*};
%typemap{Lines*}; %typemap{TriangleMeshPtrs};
%typemap{SurfaceType}{parsed}{ %typemap{SurfaceType}{parsed}{
%cpp_type{SurfaceType}; %cpp_type{SurfaceType};