From 28b043e9a900120a7cf0db554c537d7b70a3d274 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Mon, 9 Sep 2013 23:09:56 +0200 Subject: [PATCH] Add unit test for split() --- xs/lib/Slic3r/XS.pm | 1 + xs/src/TriangleMesh.cpp | 11 +++++++++-- xs/src/TriangleMesh.hpp | 3 ++- xs/t/01_trianglemesh.t | 17 +++++++++++++---- xs/xsp/TriangleMesh.xsp | 4 ++++ 5 files changed, 29 insertions(+), 7 deletions(-) diff --git a/xs/lib/Slic3r/XS.pm b/xs/lib/Slic3r/XS.pm index 5cac751b4..9f761ba9a 100644 --- a/xs/lib/Slic3r/XS.pm +++ b/xs/lib/Slic3r/XS.pm @@ -4,6 +4,7 @@ use strict; our $VERSION = '0.01'; +use Carp qw(); use XSLoader; XSLoader::load(__PACKAGE__, $VERSION); diff --git a/xs/src/TriangleMesh.cpp b/xs/src/TriangleMesh.cpp index b1abae55b..664c7c26d 100644 --- a/xs/src/TriangleMesh.cpp +++ b/xs/src/TriangleMesh.cpp @@ -11,7 +11,6 @@ namespace Slic3r { -TriangleMesh::TriangleMesh() {} TriangleMesh::~TriangleMesh() { stl_close(&stl); } @@ -112,6 +111,8 @@ TriangleMesh::Repair() { // neighbors stl_verify_neighbors(&stl); + + this->repaired = true; } void @@ -484,6 +485,9 @@ TriangleMesh::split() const TriangleMeshPtrs meshes; std::set seen_facets; + // we need neighbors + if (!this->repaired) CONFESS("split() requires Repair()"); + // loop while we have remaining facets while (1) { // get the first facet @@ -530,8 +534,8 @@ TriangleMesh::merge(const TriangleMesh* mesh) { // reset stats and metadata int number_of_facets = this->stl.stats.number_of_facets; - stl_initialize(&this->stl); stl_invalidate_shared_vertices(&this->stl); + this->repaired = false; // update facet count and allocate more memory this->stl.stats.number_of_facets = number_of_facets + mesh->stl.stats.number_of_facets; @@ -542,6 +546,9 @@ TriangleMesh::merge(const TriangleMesh* mesh) for (int i = 0; i < mesh->stl.stats.number_of_facets; i++) { this->stl.facet_start[number_of_facets + i] = mesh->stl.facet_start[i]; } + + // update size + stl_get_size(&this->stl); } } diff --git a/xs/src/TriangleMesh.hpp b/xs/src/TriangleMesh.hpp index 77ef6a4d4..e6a1a8a08 100644 --- a/xs/src/TriangleMesh.hpp +++ b/xs/src/TriangleMesh.hpp @@ -15,7 +15,7 @@ typedef std::vector TriangleMeshPtrs; class TriangleMesh { public: - TriangleMesh(); + TriangleMesh() : repaired(false) {}; ~TriangleMesh(); SV* to_SV(); void ReadSTLFile(char* input_file); @@ -30,6 +30,7 @@ class TriangleMesh TriangleMeshPtrs split() const; void merge(const TriangleMesh* mesh); stl_file stl; + bool repaired; }; enum FacetEdgeType { feNone, feTop, feBottom }; diff --git a/xs/t/01_trianglemesh.t b/xs/t/01_trianglemesh.t index 6c4b062e1..24be62fbc 100644 --- a/xs/t/01_trianglemesh.t +++ b/xs/t/01_trianglemesh.t @@ -4,7 +4,7 @@ use strict; use warnings; use Slic3r::XS; -use Test::More tests => 46; +use Test::More tests => 47; is Slic3r::TriangleMesh::XS::hello_world(), 'Hello world!', 'hello world'; @@ -43,14 +43,23 @@ my $cube = { $m->rotate(45, Slic3r::Point->new(20,20)); 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'; + { + my $meshes = $m->split; + is scalar(@$meshes), 1, 'split'; + isa_ok $meshes->[0], 'Slic3r::TriangleMesh::XS', 'split'; + } my $m2 = Slic3r::TriangleMesh::XS->new; $m2->ReadFromPerl($cube->{vertices}, $cube->{facets}); + $m2->Repair; $m->merge($m2); + $m->Repair; is $m->stats->{number_of_facets}, 2 * $m2->stats->{number_of_facets}, 'merge'; + + { + my $meshes = $m->split; + is scalar(@$meshes), 2, 'split'; + } } { diff --git a/xs/xsp/TriangleMesh.xsp b/xs/xsp/TriangleMesh.xsp index 02f2f637d..433e63508 100644 --- a/xs/xsp/TriangleMesh.xsp +++ b/xs/xsp/TriangleMesh.xsp @@ -41,6 +41,8 @@ TriangleMesh::stats() SV* TriangleMesh::vertices() CODE: + if (!THIS->repaired) CONFESS("vertices() requires Repair()"); + if (THIS->stl.v_shared == NULL) stl_generate_shared_vertices(&(THIS->stl)); @@ -63,6 +65,8 @@ TriangleMesh::vertices() SV* TriangleMesh::facets() CODE: + if (!THIS->repaired) CONFESS("facets() requires Repair()"); + if (THIS->stl.v_shared == NULL) stl_generate_shared_vertices(&(THIS->stl));