01_trianglemesh.t 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Slic3r::XS;
  5. use Test::More tests => 49;
  6. use constant Z => 2;
  7. is Slic3r::TriangleMesh::hello_world(), 'Hello world!',
  8. 'hello world';
  9. my $cube = {
  10. vertices => [ [20,20,0], [20,0,0], [0,0,0], [0,20,0], [20,20,20], [0,20,20], [0,0,20], [20,0,20] ],
  11. facets => [ [0,1,2], [0,2,3], [4,5,6], [4,6,7], [0,4,7], [0,7,1], [1,7,6], [1,6,2], [2,6,5], [2,5,3], [4,0,3], [4,3,5] ],
  12. };
  13. {
  14. my $m = Slic3r::TriangleMesh->new;
  15. $m->ReadFromPerl($cube->{vertices}, $cube->{facets});
  16. $m->repair;
  17. my ($vertices, $facets) = ($m->vertices, $m->facets);
  18. is_deeply $vertices, $cube->{vertices}, 'vertices arrayref roundtrip';
  19. is_deeply $facets, $cube->{facets}, 'facets arrayref roundtrip';
  20. is scalar(@{$m->normals}), scalar(@$facets), 'normals returns the right number of items';
  21. {
  22. my $m2 = $m->clone;
  23. is_deeply $m2->vertices, $cube->{vertices}, 'cloned vertices arrayref roundtrip';
  24. is_deeply $m2->facets, $cube->{facets}, 'cloned facets arrayref roundtrip';
  25. $m2->scale(3); # check that it does not affect $m
  26. }
  27. {
  28. my $stats = $m->stats;
  29. is $stats->{number_of_facets}, scalar(@{ $cube->{facets} }), 'stats.number_of_facets';
  30. ok abs($stats->{volume} - 20*20*20) < 1E-2, 'stats.volume';
  31. }
  32. $m->scale(2);
  33. ok abs($m->stats->{volume} - 40*40*40) < 1E-2, 'scale';
  34. $m->scale_xyz(Slic3r::Pointf3->new(2,1,1));
  35. ok abs($m->stats->{volume} - 2*40*40*40) < 1E-2, 'scale_xyz';
  36. $m->translate(5,10,0);
  37. is_deeply $m->vertices->[0], [85,50,0], 'translate';
  38. $m->align_to_origin;
  39. is_deeply $m->vertices->[2], [0,0,0], 'align_to_origin';
  40. is_deeply $m->size, [80,40,40], 'size';
  41. $m->scale_xyz(Slic3r::Pointf3->new(0.5,1,1));
  42. $m->rotate(45, Slic3r::Point->new(20,20));
  43. ok abs($m->size->[0] - sqrt(2)*40) < 1E-4, 'rotate';
  44. {
  45. my $meshes = $m->split;
  46. is scalar(@$meshes), 1, 'split';
  47. isa_ok $meshes->[0], 'Slic3r::TriangleMesh', 'split';
  48. is_deeply $m->bb3, $meshes->[0]->bb3, 'split populates stats';
  49. }
  50. my $m2 = Slic3r::TriangleMesh->new;
  51. $m2->ReadFromPerl($cube->{vertices}, $cube->{facets});
  52. $m2->repair;
  53. $m->merge($m2);
  54. $m->repair;
  55. is $m->stats->{number_of_facets}, 2 * $m2->stats->{number_of_facets}, 'merge';
  56. {
  57. my $meshes = $m->split;
  58. is scalar(@$meshes), 2, 'split';
  59. }
  60. }
  61. {
  62. my $m = Slic3r::TriangleMesh->new;
  63. $m->ReadFromPerl($cube->{vertices}, $cube->{facets});
  64. $m->repair;
  65. my @z = (0,2,4,8,6,8,10,12,14,16,18,20);
  66. my $result = $m->slice(\@z);
  67. my $SCALING_FACTOR = 0.000001;
  68. for my $i (0..$#z) {
  69. is scalar(@{$result->[$i]}), 1, "number of returned polygons per layer (z = " . $z[$i] . ")";
  70. is $result->[$i][0]->area, 20*20/($SCALING_FACTOR**2), 'size of returned polygon';
  71. }
  72. }
  73. {
  74. my $m = Slic3r::TriangleMesh->new;
  75. $m->ReadFromPerl(
  76. [ [0,0,0],[0,0,20],[0,5,0],[0,5,20],[50,0,0],[50,0,20],[15,5,0],[35,5,0],[15,20,0],[50,5,0],[35,20,0],[15,5,10],[50,5,20],[35,5,10],[35,20,10],[15,20,10] ],
  77. [ [0,1,2],[2,1,3],[1,0,4],[5,1,4],[0,2,4],[4,2,6],[7,6,8],[4,6,7],[9,4,7],[7,8,10],[2,3,6],[11,3,12],[7,12,9],[13,12,7],[6,3,11],[11,12,13],[3,1,5],[12,3,5],[5,4,9],[12,5,9],[13,7,10],[14,13,10],[8,15,10],[10,15,14],[6,11,8],[8,11,15],[15,11,13],[14,15,13] ],
  78. );
  79. $m->repair;
  80. {
  81. # at Z = 10 we have a top horizontal surface
  82. my $slices = $m->slice([ 5, 10 ]);
  83. is $slices->[0][0]->area, $slices->[1][0]->area, 'slicing a top tangent plane includes its area';
  84. }
  85. $m->mirror_z;
  86. {
  87. # this second test also checks that performing a second slice on a mesh after
  88. # a transformation works properly (shared_vertices is correctly invalidated);
  89. # at Z = -10 we have a bottom horizontal surface
  90. my $slices = $m->slice([ -5, -10 ]);
  91. is $slices->[0][0]->area, $slices->[1][0]->area, 'slicing a bottom tangent plane includes its area';
  92. }
  93. }
  94. {
  95. my $m = Slic3r::TriangleMesh->new;
  96. $m->ReadFromPerl($cube->{vertices}, $cube->{facets});
  97. $m->repair;
  98. {
  99. my $upper = Slic3r::TriangleMesh->new;
  100. my $lower = Slic3r::TriangleMesh->new;
  101. $m->cut(Z, 0, $upper, $lower);
  102. $upper->repair; $lower->repair;
  103. is $upper->facets_count, 12, 'upper mesh has all facets except those belonging to the slicing plane';
  104. is $lower->facets_count, 0, 'lower mesh has no facets';
  105. }
  106. {
  107. my $upper = Slic3r::TriangleMesh->new;
  108. my $lower = Slic3r::TriangleMesh->new;
  109. $m->cut(Z, 10, $upper, $lower);
  110. #$upper->repair; $lower->repair;
  111. # we expect:
  112. # 2 facets on external horizontal surfaces
  113. # 3 facets on each side = 12 facets
  114. # 6 facets on the triangulated side (8 vertices)
  115. is $upper->facets_count, 2+12+6, 'upper mesh has the expected number of facets';
  116. is $lower->facets_count, 2+12+6, 'lower mesh has the expected number of facets';
  117. }
  118. }
  119. __END__