slice.t 4.7 KB

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