slice.t 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. use Test::More;
  2. use strict;
  3. use warnings;
  4. plan skip_all => 'temporarily disabled';
  5. plan tests => 16;
  6. BEGIN {
  7. use FindBin;
  8. use lib "$FindBin::Bin/../lib";
  9. }
  10. # temporarily disable compilation errors due to constant not being exported anymore
  11. sub Slic3r::TriangleMesh::I_B {}
  12. sub Slic3r::TriangleMesh::I_FACET_EDGE {}
  13. sub Slic3r::TriangleMesh::FE_BOTTOM {
  14. sub Slic3r::TriangleMesh::FE_TOP {}}
  15. use Slic3r;
  16. use Slic3r::Geometry qw(X Y Z A B);
  17. my @lines;
  18. my $z = 20;
  19. my @points = ([3, 4], [8, 5], [1, 9]); # XY coordinates of the facet vertices
  20. # NOTE:
  21. # the first point of the intersection lines is replaced by -1 because TriangleMesh.pm
  22. # is saving memory and doesn't store point A anymore since it's not actually needed.
  23. # We disable this test because intersect_facet() now assumes we never feed a horizontal
  24. # facet to it.
  25. # is_deeply lines(20, 20, 20), [
  26. # [ -1, $points[1] ], # $points[0]
  27. # [ -1, $points[2] ], # $points[1]
  28. # [ -1, $points[0] ], # $points[2]
  29. # ], 'horizontal';
  30. is_deeply lines(22, 20, 20), [ [ -1, $points[2] ] ], 'lower edge on layer'; # $points[1]
  31. is_deeply lines(20, 20, 22), [ [ -1, $points[1] ] ], 'lower edge on layer'; # $points[0]
  32. is_deeply lines(20, 22, 20), [ [ -1, $points[0] ] ], 'lower edge on layer'; # $points[2]
  33. is_deeply lines(20, 20, 10), [ [ -1, $points[0] ] ], 'upper edge on layer'; # $points[1]
  34. is_deeply lines(10, 20, 20), [ [ -1, $points[1] ] ], 'upper edge on layer'; # $points[2]
  35. is_deeply lines(20, 10, 20), [ [ -1, $points[2] ] ], 'upper edge on layer'; # $points[0]
  36. is_deeply lines(20, 15, 10), [ ], 'upper vertex on layer';
  37. is_deeply lines(28, 20, 30), [ ], 'lower vertex on layer';
  38. {
  39. my @z = (24, 10, 16);
  40. is_deeply lines(@z), [
  41. [
  42. -1, # line_plane_intersection([ vertices(@z)->[0], vertices(@z)->[1] ]),
  43. line_plane_intersection([ vertices(@z)->[2], vertices(@z)->[0] ]),
  44. ]
  45. ], 'two edges intersect';
  46. }
  47. {
  48. my @z = (16, 24, 10);
  49. is_deeply lines(@z), [
  50. [
  51. -1, # line_plane_intersection([ vertices(@z)->[1], vertices(@z)->[2] ]),
  52. line_plane_intersection([ vertices(@z)->[0], vertices(@z)->[1] ]),
  53. ]
  54. ], 'two edges intersect';
  55. }
  56. {
  57. my @z = (10, 16, 24);
  58. is_deeply lines(@z), [
  59. [
  60. -1, # line_plane_intersection([ vertices(@z)->[2], vertices(@z)->[0] ]),
  61. line_plane_intersection([ vertices(@z)->[1], vertices(@z)->[2] ]),
  62. ]
  63. ], 'two edges intersect';
  64. }
  65. {
  66. my @z = (24, 10, 20);
  67. is_deeply lines(@z), [
  68. [
  69. -1, # line_plane_intersection([ vertices(@z)->[0], vertices(@z)->[1] ]),
  70. $points[2],
  71. ]
  72. ], 'one vertex on plane and one edge intersects';
  73. }
  74. {
  75. my @z = (10, 20, 24);
  76. is_deeply lines(@z), [
  77. [
  78. -1, # line_plane_intersection([ vertices(@z)->[2], vertices(@z)->[0] ]),
  79. $points[1],
  80. ]
  81. ], 'one vertex on plane and one edge intersects';
  82. }
  83. {
  84. my @z = (20, 24, 10);
  85. is_deeply lines(@z), [
  86. [
  87. -1, # line_plane_intersection([ vertices(@z)->[1], vertices(@z)->[2] ]),
  88. $points[0],
  89. ]
  90. ], 'one vertex on plane and one edge intersects';
  91. }
  92. my @lower = intersect(22, 20, 20);
  93. my @upper = intersect(20, 20, 10);
  94. is $lower[0][Slic3r::TriangleMesh::I_FACET_EDGE], Slic3r::TriangleMesh::FE_BOTTOM, 'bottom edge on layer';
  95. is $upper[0][Slic3r::TriangleMesh::I_FACET_EDGE], Slic3r::TriangleMesh::FE_TOP, 'upper edge on layer';
  96. my $mesh;
  97. sub intersect {
  98. $mesh = Slic3r::TriangleMesh->new(
  99. facets => [],
  100. vertices => [],
  101. );
  102. push @{$mesh->facets}, [ [0,0,0], @{vertices(@_)} ];
  103. $mesh->analyze;
  104. return map Slic3r::TriangleMesh::unpack_line($_), $mesh->intersect_facet($#{$mesh->facets}, $z);
  105. }
  106. sub vertices {
  107. push @{$mesh->vertices}, map [ @{$points[$_]}, $_[$_] ], 0..2;
  108. [ ($#{$mesh->vertices}-2) .. $#{$mesh->vertices} ]
  109. }
  110. sub lines {
  111. my @lines = intersect(@_);
  112. #$_->a->[X] = sprintf('%.0f', $_->a->[X]) for @lines;
  113. #$_->a->[Y] = sprintf('%.0f', $_->a->[Y]) for @lines;
  114. $_->[Slic3r::TriangleMesh::I_B][X] = sprintf('%.0f', $_->[Slic3r::TriangleMesh::I_B][X]) for @lines;
  115. $_->[Slic3r::TriangleMesh::I_B][Y] = sprintf('%.0f', $_->[Slic3r::TriangleMesh::I_B][Y]) for @lines;
  116. return [ map [ -1, $_->[Slic3r::TriangleMesh::I_B] ], @lines ];
  117. }
  118. sub line_plane_intersection {
  119. my ($line) = @_;
  120. @$line = map $mesh->vertices->[$_], @$line;
  121. return [
  122. map sprintf('%.0f', $_),
  123. map +($line->[B][$_] + ($line->[A][$_] - $line->[B][$_]) * ($z - $line->[B][Z]) / ($line->[A][Z] - $line->[B][Z])),
  124. (X,Y)
  125. ];
  126. }
  127. __END__