TriangleMesh.pm 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. package Slic3r::TriangleMesh;
  2. use Moo;
  3. use Slic3r::Geometry qw(X Y Z A B unscale same_point);
  4. # public
  5. has 'vertices' => (is => 'ro', required => 1); # id => [$x,$y,$z]
  6. has 'facets' => (is => 'ro', required => 1); # id => [ $v1_id, $v2_id, $v3_id ]
  7. # private
  8. has 'edges' => (is => 'ro', default => sub { [] }); # id => [ $v1_id, $v2_id ]
  9. has 'facets_edges' => (is => 'ro', default => sub { [] }); # id => [ $e1_id, $e2_id, $e3_id ]
  10. has 'edges_facets' => (is => 'ro', default => sub { [] }); # id => [ $f1_id, $f2_id, (...) ]
  11. use constant MIN => 0;
  12. use constant MAX => 1;
  13. use constant I_FMT => 'ffLLLLLc';
  14. use constant I_B => 0;
  15. use constant I_A_ID => 1;
  16. use constant I_B_ID => 2;
  17. use constant I_FACET_INDEX => 3;
  18. use constant I_PREV_FACET_INDEX => 4;
  19. use constant I_NEXT_FACET_INDEX => 5;
  20. use constant I_FACET_EDGE => 6;
  21. use constant FE_TOP => 0;
  22. use constant FE_BOTTOM => 1;
  23. # always make sure BUILD is idempotent
  24. sub BUILD {
  25. my $self = shift;
  26. @{$self->edges} = ();
  27. @{$self->facets_edges} = ();
  28. @{$self->edges_facets} = ();
  29. my %table = (); # edge_coordinates => edge_id
  30. for (my $facet_id = 0; $facet_id <= $#{$self->facets}; $facet_id++) {
  31. my $facet = $self->facets->[$facet_id];
  32. $self->facets_edges->[$facet_id] = [];
  33. # reorder vertices so that the first one is the one with lowest Z
  34. # this is needed to get all intersection lines in a consistent order
  35. # (external on the right of the line)
  36. {
  37. my @z_order = sort { $self->vertices->[$facet->[$a]][Z] <=> $self->vertices->[$facet->[$b]][Z] } -3..-1;
  38. @$facet[-3..-1] = (@$facet[$z_order[0]..-1], @$facet[-3..($z_order[0]-1)]);
  39. }
  40. # ignore the normal if provided
  41. my @vertices = @$facet[-3..-1];
  42. foreach my $edge ($self->_facet_edges($facet_id)) {
  43. my $edge_coordinates = join ';', sort @$edge;
  44. my $edge_id = $table{$edge_coordinates};
  45. if (!defined $edge_id) {
  46. # Note that the order of vertices in $self->edges is *casual* because it is only
  47. # good for one of the two adjacent facets. For this reason, it must not be used
  48. # when dealing with single facets.
  49. push @{$self->edges}, $edge;
  50. $edge_id = $#{$self->edges};
  51. $table{$edge_coordinates} = $edge_id;
  52. $self->edges_facets->[$edge_id] = [];
  53. }
  54. push @{$self->facets_edges->[$facet_id]}, $edge_id;
  55. push @{$self->edges_facets->[$edge_id]}, $facet_id;
  56. }
  57. }
  58. }
  59. sub clone {
  60. my $self = shift;
  61. return (ref $self)->new(
  62. vertices => [ map [ @$_ ], @{$self->vertices} ],
  63. facets => [ map [ @$_ ], @{$self->facets} ],
  64. );
  65. }
  66. sub _facet_edges {
  67. my $self = shift;
  68. my ($facet_id) = @_;
  69. my $facet = $self->facets->[$facet_id];
  70. return (
  71. [ $facet->[-3], $facet->[-2] ],
  72. [ $facet->[-2], $facet->[-1] ],
  73. [ $facet->[-1], $facet->[-3] ],
  74. );
  75. }
  76. # This method is supposed to remove narrow triangles, but it actually doesn't
  77. # work much; I'm committing it for future reference but I'm going to remove it later.
  78. # Note: a 'clean' method should actually take care of non-manifold facets and remove
  79. # them.
  80. sub clean {
  81. my $self = shift;
  82. # retrieve all edges shared by more than two facets;
  83. my @weird_edges = grep { @{$self->edge_facets->{$_}} != 2 } keys %{$self->edge_facets};
  84. # usually most of these facets are very narrow triangles whose two edges
  85. # are detected as collapsed, and thus added twice to the edge in edge_fasets table
  86. # let's identify these triangles
  87. my @narrow_facets_indexes = ();
  88. foreach my $edge_id (@weird_edges) {
  89. my %facet_count = ();
  90. $facet_count{$_}++ for @{$self->edge_facets->{$edge_id}};
  91. @{$self->edge_facets->{$edge_id}} = grep $facet_count{$_} == 1, keys %facet_count;
  92. push @narrow_facets_indexes, grep $facet_count{$_} > 1, keys %facet_count;
  93. }
  94. # remove identified narrow facets
  95. foreach my $facet_id (@narrow_facets_indexes) {last;
  96. splice @{$self->facets}, $facet_id, 1;
  97. splice @{$self->facets_edges}, $facet_id, 1;
  98. foreach my $facet_ides (values %{$self->edge_facets}) {
  99. @$facet_ides = map { $_ > $facet_id ? ($_-1) : $_ } @$facet_ides;
  100. }
  101. }
  102. Slic3r::debugf "%d narrow facets removed\n", scalar(@narrow_facets_indexes)
  103. if @narrow_facets_indexes;
  104. }
  105. sub check_manifoldness {
  106. my $self = shift;
  107. # look for edges not connected to exactly two facets
  108. if (grep { @$_ != 2 } @{$self->edges_facets}) {
  109. my ($first_bad_edge_id) = grep { @{ $self->edges_facets->[$_] } != 2 } 0..$#{$self->edges_facets};
  110. warn sprintf "Warning: The input file is not manifold near edge %f-%f. "
  111. . "You might want to check the resulting G-code before printing.\n",
  112. @{$self->edges->[$first_bad_edge_id]};
  113. }
  114. }
  115. sub unpack_line {
  116. my ($packed) = @_;
  117. my @data = unpack I_FMT, $packed;
  118. splice @data, 0, 2, [ @data[0,1] ];
  119. $data[$_] ||= undef for I_A_ID, I_B_ID, I_PREV_FACET_INDEX, I_NEXT_FACET_INDEX;
  120. $data[I_FACET_EDGE] = undef if $data[I_FACET_EDGE] == -1;
  121. return [@data];
  122. }
  123. sub make_loops {
  124. my ($layer) = @_;
  125. my @lines = map unpack_line($_), @{$layer->lines};
  126. # remove tangent edges
  127. {
  128. for (my $i = 0; $i <= $#lines; $i++) {
  129. next unless defined $lines[$i] && defined $lines[$i][I_FACET_EDGE];
  130. # if the line is a facet edge, find another facet edge
  131. # having the same endpoints but in reverse order
  132. for (my $j = $i+1; $j <= $#lines; $j++) {
  133. next unless defined $lines[$j] && defined $lines[$j][I_FACET_EDGE];
  134. # are these facets adjacent? (sharing a common edge on this layer)
  135. if ($lines[$i][I_A_ID] == $lines[$j][I_B_ID] && $lines[$i][I_B_ID] == $lines[$j][I_A_ID]) {
  136. # if they are both oriented upwards or downwards (like a 'V')
  137. # then we can remove both edges from this layer since it won't
  138. # affect the sliced shape
  139. if ($lines[$j][I_FACET_EDGE] == $lines[$i][I_FACET_EDGE]) {
  140. $lines[$i] = undef;
  141. $lines[$j] = undef;
  142. last;
  143. }
  144. # if one of them is oriented upwards and the other is oriented
  145. # downwards, let's only keep one of them (it doesn't matter which
  146. # one since all 'top' lines were reversed at slicing)
  147. if ($lines[$i][I_FACET_EDGE] == FE_TOP && $lines[$j][I_FACET_EDGE] == FE_BOTTOM) {
  148. $lines[$j] = undef;
  149. last;
  150. }
  151. }
  152. }
  153. }
  154. }
  155. @lines = grep $_, @lines;
  156. # count relationships
  157. my %prev_count = (); # how many lines have the same prev_facet_index
  158. my %a_count = (); # how many lines have the same a_id
  159. foreach my $line (@lines) {
  160. if (defined $line->[I_PREV_FACET_INDEX]) {
  161. $prev_count{$line->[I_PREV_FACET_INDEX]}++;
  162. }
  163. if (defined $line->[I_A_ID]) {
  164. $a_count{$line->[I_A_ID]}++;
  165. }
  166. }
  167. foreach my $point_id (grep $a_count{$_} > 1, keys %a_count) {
  168. my @lines_starting_here = grep defined $_->[I_A_ID] && $_->[I_A_ID] == $point_id, @lines;
  169. Slic3r::debugf "%d lines start at point %d\n", scalar(@lines_starting_here), $point_id;
  170. # if two lines start at this point, one being a 'top' facet edge and the other being a 'bottom' one,
  171. # then remove the top one and those following it (removing the top or the bottom one is an arbitrary
  172. # choice)
  173. # The "// ''" on the next line avoids uninitialized value errors mentioned in issue #357 but these
  174. # errors occur on fixed models so the root cause still needs to be found
  175. if (@lines_starting_here == 2 && join('', sort map $_->[I_FACET_EDGE] // '', @lines_starting_here) eq FE_TOP.FE_BOTTOM) { #/
  176. my @to_remove = grep $_->[I_FACET_EDGE] == FE_TOP, @lines_starting_here;
  177. while (!grep defined $_->[I_B_ID] && $_->[I_B_ID] == $to_remove[-1]->[I_B_ID] && $_ ne $to_remove[-1], @lines) {
  178. push @to_remove, grep defined $_->[I_A_ID] && $_->[I_A_ID] == $to_remove[-1]->[I_B_ID], @lines;
  179. }
  180. my %to_remove = map {$_ => 1} @to_remove;
  181. @lines = grep !$to_remove{$_}, @lines;
  182. } else {
  183. Slic3r::debugf " this shouldn't happen and should be further investigated\n";
  184. if (0) {
  185. require "Slic3r/SVG.pm";
  186. Slic3r::SVG::output(undef, "same_point.svg",
  187. lines => [ map $_->line, grep !defined $_->[I_FACET_EDGE], @lines ],
  188. red_lines => [ map $_->line, grep defined $_->[I_FACET_EDGE], @lines ],
  189. #points => [ $self->vertices->[$point_id] ],
  190. no_arrows => 0,
  191. );
  192. }
  193. }
  194. }
  195. # optimization: build indexes of lines
  196. my %by_facet_index = map { $lines[$_][I_FACET_INDEX] => $_ }
  197. grep defined $lines[$_][I_FACET_INDEX],
  198. (0..$#lines);
  199. my %by_a_id = map { $lines[$_][I_A_ID] => $_ }
  200. grep defined $lines[$_][I_A_ID],
  201. (0..$#lines);
  202. my (@polygons, %visited_lines) = ();
  203. CYCLE: for (my $i = 0; $i <= $#lines; $i++) {
  204. my $line = $lines[$i];
  205. next if $visited_lines{$line};
  206. my @points = ();
  207. my $first_facet_index = $line->[I_FACET_INDEX];
  208. do {
  209. my $next_line;
  210. if (defined $line->[I_NEXT_FACET_INDEX] && exists $by_facet_index{$line->[I_NEXT_FACET_INDEX]}) {
  211. $next_line = $lines[$by_facet_index{$line->[I_NEXT_FACET_INDEX]}];
  212. } elsif (defined $line->[I_B_ID] && exists $by_a_id{$line->[I_B_ID]}) {
  213. $next_line = $lines[$by_a_id{$line->[I_B_ID]}];
  214. } else {
  215. Slic3r::debugf " line has no next_facet_index or b_id\n";
  216. $layer->slicing_errors(1);
  217. next CYCLE;
  218. }
  219. if (!$next_line || $visited_lines{$next_line}) {
  220. Slic3r::debugf " failed to close this loop\n";
  221. $layer->slicing_errors(1);
  222. next CYCLE;
  223. } elsif (defined $next_line->[I_PREV_FACET_INDEX] && $next_line->[I_PREV_FACET_INDEX] != $line->[I_FACET_INDEX]) {
  224. Slic3r::debugf " wrong prev_facet_index\n";
  225. $layer->slicing_errors(1);
  226. next CYCLE;
  227. } elsif (defined $next_line->[I_A_ID] && $next_line->[I_A_ID] != $line->[I_B_ID]) {
  228. Slic3r::debugf " wrong a_id\n";
  229. $layer->slicing_errors(1);
  230. next CYCLE;
  231. }
  232. push @points, $next_line->[I_B];
  233. $visited_lines{$next_line} = 1;
  234. $line = $next_line;
  235. } while ($first_facet_index != $line->[I_FACET_INDEX]);
  236. push @polygons, Slic3r::Polygon->new(@points);
  237. Slic3r::debugf " Discovered %s polygon of %d points\n",
  238. ($polygons[-1]->is_counter_clockwise ? 'ccw' : 'cw'), scalar(@points)
  239. if $Slic3r::debug;
  240. }
  241. return [@polygons];
  242. }
  243. sub rotate {
  244. my $self = shift;
  245. my ($deg) = @_;
  246. return if $deg == 0;
  247. my $rad = Slic3r::Geometry::deg2rad($deg);
  248. # transform vertex coordinates
  249. foreach my $vertex (@{$self->vertices}) {
  250. @$vertex = (@{ +(Slic3r::Geometry::rotate_points($rad, undef, [ $vertex->[X], $vertex->[Y] ]))[0] }, $vertex->[Z]);
  251. }
  252. }
  253. sub scale {
  254. my $self = shift;
  255. my ($factor) = @_;
  256. return if $factor == 1;
  257. # transform vertex coordinates
  258. foreach my $vertex (@{$self->vertices}) {
  259. $vertex->[$_] *= $factor for X,Y,Z;
  260. }
  261. }
  262. sub move {
  263. my $self = shift;
  264. my (@shift) = @_;
  265. # transform vertex coordinates
  266. foreach my $vertex (@{$self->vertices}) {
  267. $vertex->[$_] += $shift[$_] || 0 for X,Y,Z;
  268. }
  269. }
  270. sub align_to_origin {
  271. my $self = shift;
  272. # calculate the displacements needed to
  273. # have lowest value for each axis at coordinate 0
  274. my @extents = $self->bounding_box;
  275. $self->move(map -$extents[$_][MIN], X,Y,Z);
  276. }
  277. sub duplicate {
  278. my $self = shift;
  279. my (@shifts) = @_;
  280. my @new_facets = ();
  281. foreach my $facet (@{$self->facets}) {
  282. # transform vertex coordinates
  283. my ($normal, @vertices) = @$facet;
  284. foreach my $shift (@shifts) {
  285. push @new_facets, [ $normal ];
  286. foreach my $vertex (@vertices) {
  287. push @{$self->vertices}, [ map $self->vertices->[$vertex][$_] + ($shift->[$_] || 0), (X,Y,Z) ];
  288. push @{$new_facets[-1]}, $#{$self->vertices};
  289. }
  290. }
  291. }
  292. push @{$self->facets}, @new_facets;
  293. $self->BUILD;
  294. }
  295. sub bounding_box {
  296. my $self = shift;
  297. my @extents = (map [undef, undef], X,Y,Z);
  298. foreach my $vertex (@{$self->vertices}) {
  299. for (X,Y,Z) {
  300. $extents[$_][MIN] = $vertex->[$_] if !defined $extents[$_][MIN] || $vertex->[$_] < $extents[$_][MIN];
  301. $extents[$_][MAX] = $vertex->[$_] if !defined $extents[$_][MAX] || $vertex->[$_] > $extents[$_][MAX];
  302. }
  303. }
  304. return @extents;
  305. }
  306. sub size {
  307. my $self = shift;
  308. my @extents = $self->bounding_box;
  309. return map $extents[$_][MAX] - $extents[$_][MIN], (X,Y,Z);
  310. }
  311. sub slice_facet {
  312. my $self = shift;
  313. my ($print_object, $facet_id) = @_;
  314. my @vertices = @{$self->facets->[$facet_id]}[-3..-1];
  315. Slic3r::debugf "\n==> FACET %d (%f,%f,%f - %f,%f,%f - %f,%f,%f):\n",
  316. $facet_id, map @{$self->vertices->[$_]}, @vertices
  317. if $Slic3r::debug;
  318. # find the vertical extents of the facet
  319. my ($min_z, $max_z) = (99999999999, -99999999999);
  320. foreach my $vertex (@vertices) {
  321. my $vertex_z = $self->vertices->[$vertex][Z];
  322. $min_z = $vertex_z if $vertex_z < $min_z;
  323. $max_z = $vertex_z if $vertex_z > $max_z;
  324. }
  325. Slic3r::debugf "z: min = %.0f, max = %.0f\n", $min_z, $max_z;
  326. if ($max_z == $min_z) {
  327. Slic3r::debugf "Facet is horizontal; ignoring\n";
  328. return;
  329. }
  330. # calculate the layer extents
  331. my $first_layer_height = $Slic3r::layer_height * $Slic3r::first_layer_height_ratio;
  332. my $min_layer = int((unscale($min_z) - ($first_layer_height + $Slic3r::layer_height / 2)) / $Slic3r::layer_height) - 2;
  333. $min_layer = 0 if $min_layer < 0;
  334. my $max_layer = int((unscale($max_z) - ($first_layer_height + $Slic3r::layer_height / 2)) / $Slic3r::layer_height) + 2;
  335. Slic3r::debugf "layers: min = %s, max = %s\n", $min_layer, $max_layer;
  336. my $lines = {}; # layer_id => [ lines ]
  337. for (my $layer_id = $min_layer; $layer_id <= $max_layer; $layer_id++) {
  338. my $layer = $print_object->layer($layer_id);
  339. $lines->{$layer_id} ||= [];
  340. push @{ $lines->{$layer_id} }, $self->intersect_facet($facet_id, $layer->slice_z);
  341. }
  342. return $lines;
  343. }
  344. sub intersect_facet {
  345. my $self = shift;
  346. my ($facet_id, $z) = @_;
  347. my @vertices_ids = @{$self->facets->[$facet_id]}[-3..-1];
  348. my @edge_ids = @{$self->facets_edges->[$facet_id]};
  349. my @edge_vertices_ids = $self->_facet_edges($facet_id);
  350. my (@lines, @points, @intersection_points, @points_on_layer) = ();
  351. for my $e (0..2) {
  352. my $edge_id = $edge_ids[$e];
  353. my ($a_id, $b_id) = @{$edge_vertices_ids[$e]};
  354. my ($a, $b) = map $self->vertices->[$_], ($a_id, $b_id);
  355. #printf "Az = %f, Bz = %f, z = %f\n", $a->[Z], $b->[Z], $z;
  356. if ($a->[Z] == $b->[Z] && $a->[Z] == $z) {
  357. # edge is horizontal and belongs to the current layer
  358. my $edge_type = (grep $self->vertices->[$_][Z] < $z, @vertices_ids) ? FE_TOP : FE_BOTTOM;
  359. if ($edge_type == FE_TOP) {
  360. ($a, $b) = ($b, $a);
  361. ($a_id, $b_id) = ($b_id, $a_id);
  362. }
  363. push @lines, pack I_FMT, (
  364. $b->[X], $b->[Y], # I_B
  365. $a_id, # I_A_ID
  366. $b_id, # I_B_ID
  367. $facet_id, # I_FACET_INDEX
  368. 0, # I_PREV_FACET_INDEX
  369. 0, # I_NEXT_FACET_INDEX
  370. $edge_type, # I_FACET_EDGE
  371. # Unused data:
  372. # a => [$a->[X], $a->[Y]],
  373. );
  374. #print "Horizontal edge at $z!\n";
  375. } elsif ($a->[Z] == $z) {
  376. #print "A point on plane $z!\n";
  377. push @points, [ $a->[X], $a->[Y], $a_id ];
  378. push @points_on_layer, $#points;
  379. } elsif ($b->[Z] == $z) {
  380. #print "B point on plane $z!\n";
  381. push @points, [ $b->[X], $b->[Y], $b_id ];
  382. push @points_on_layer, $#points;
  383. } elsif (($a->[Z] < $z && $b->[Z] > $z) || ($b->[Z] < $z && $a->[Z] > $z)) {
  384. # edge intersects the current layer; calculate intersection
  385. push @points, [
  386. $b->[X] + ($a->[X] - $b->[X]) * ($z - $b->[Z]) / ($a->[Z] - $b->[Z]),
  387. $b->[Y] + ($a->[Y] - $b->[Y]) * ($z - $b->[Z]) / ($a->[Z] - $b->[Z]),
  388. undef,
  389. $edge_id,
  390. ];
  391. push @intersection_points, $#points;
  392. #print "Intersects at $z!\n";
  393. }
  394. }
  395. return @lines if @lines;
  396. if (@points_on_layer == 2 && @intersection_points == 1) {
  397. $points[ $points_on_layer[1] ] = undef;
  398. @points = grep $_, @points;
  399. }
  400. if (@points_on_layer == 2 && @intersection_points == 0) {
  401. if (same_point(map $points[$_], @points_on_layer)) {
  402. return ();
  403. }
  404. }
  405. if (@points) {
  406. # defensive programming:
  407. die "Facets must intersect each plane 0 or 2 times" if @points != 2;
  408. # connect points:
  409. my ($prev_facet_index, $next_facet_index) = (undef, undef);
  410. $prev_facet_index = +(grep $_ != $facet_id, @{$self->edges_facets->[$points[B][3]]})[0]
  411. if defined $points[B][3];
  412. $next_facet_index = +(grep $_ != $facet_id, @{$self->edges_facets->[$points[A][3]]})[0]
  413. if defined $points[A][3];
  414. return pack I_FMT, (
  415. $points[A][X], $points[A][Y], # I_B
  416. $points[B][2] || 0, # I_A_ID
  417. $points[A][2] || 0, # I_B_ID
  418. $facet_id, # I_FACET_INDEX
  419. $prev_facet_index || 0, # I_PREV_FACET_INDEX
  420. $next_facet_index || 0, # I_NEXT_FACET_INDEX
  421. -1, # I_FACET_EDGE
  422. );
  423. #printf " intersection points at z = %f: %f,%f - %f,%f\n", $z, map @$_, @intersection_points;
  424. }
  425. return ();
  426. }
  427. sub get_connected_facets {
  428. my $self = shift;
  429. my ($facet_id) = @_;
  430. my %facets = ();
  431. foreach my $edge_id (@{$self->facets_edges->[$facet_id]}) {
  432. $facets{$_} = 1 for @{$self->edges_facets->[$edge_id]};
  433. }
  434. delete $facets{$facet_id};
  435. return keys %facets;
  436. }
  437. sub split_mesh {
  438. my $self = shift;
  439. my @meshes = ();
  440. # loop while we have remaining facets
  441. while (1) {
  442. # get the first facet
  443. my @facet_queue = ();
  444. my @facets = ();
  445. for (my $i = 0; $i <= $#{$self->facets}; $i++) {
  446. if (defined $self->facets->[$i]) {
  447. push @facet_queue, $i;
  448. last;
  449. }
  450. }
  451. last if !@facet_queue;
  452. while (defined (my $facet_id = shift @facet_queue)) {
  453. next unless defined $self->facets->[$facet_id];
  454. push @facets, map [ @$_ ], $self->facets->[$facet_id];
  455. push @facet_queue, $self->get_connected_facets($facet_id);
  456. $self->facets->[$facet_id] = undef;
  457. }
  458. my %vertices = map { $_ => 1 } map @$_[-3..-1], @facets;
  459. my @new_vertices = keys %vertices;
  460. my %new_vertices = map { $new_vertices[$_] => $_ } 0..$#new_vertices;
  461. foreach my $facet (@facets) {
  462. $facet->[$_] = $new_vertices{$facet->[$_]} for -3..-1;
  463. }
  464. push @meshes, Slic3r::TriangleMesh->new(
  465. facets => \@facets,
  466. vertices => [ map $self->vertices->[$_], keys %vertices ],
  467. );
  468. }
  469. return @meshes;
  470. }
  471. 1;