geometry.t 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. use Test::More;
  2. use strict;
  3. use warnings;
  4. plan tests => 42;
  5. BEGIN {
  6. use FindBin;
  7. use lib "$FindBin::Bin/../lib";
  8. use local::lib "$FindBin::Bin/../local-lib";
  9. }
  10. use Slic3r;
  11. use Slic3r::Geometry qw(PI polygon_is_convex
  12. chained_path_from epsilon scale);
  13. {
  14. # this test was failing on Windows (GH #1950)
  15. my $polygon = Slic3r::Polygon->new(
  16. [207802834,-57084522],[196528149,-37556190],[173626821,-25420928],[171285751,-21366123],
  17. [118673592,-21366123],[116332562,-25420928],[93431208,-37556191],[82156517,-57084523],
  18. [129714478,-84542120],[160244873,-84542120],
  19. );
  20. my $point = Slic3r::Point->new(95706562, -57294774);
  21. ok $polygon->contains_point($point), 'contains_point';
  22. }
  23. #==========================================================
  24. my $line1 = [ [5, 15], [30, 15] ];
  25. my $line2 = [ [10, 20], [10, 10] ];
  26. is_deeply Slic3r::Geometry::line_intersection($line1, $line2, 1)->arrayref, [10, 15], 'line_intersection';
  27. #==========================================================
  28. $line1 = [ [73.6310778185108/0.0000001, 371.74239268924/0.0000001], [73.6310778185108/0.0000001, 501.74239268924/0.0000001] ];
  29. $line2 = [ [75/0.0000001, 437.9853/0.0000001], [62.7484/0.0000001, 440.4223/0.0000001] ];
  30. isnt Slic3r::Geometry::line_intersection($line1, $line2, 1), undef, 'line_intersection';
  31. #==========================================================
  32. {
  33. my $polygon = Slic3r::Polygon->new(
  34. [45919000, 515273900], [14726100, 461246400], [14726100, 348753500], [33988700, 315389800],
  35. [43749700, 343843000], [45422300, 352251500], [52362100, 362637800], [62748400, 369577600],
  36. [75000000, 372014700], [87251500, 369577600], [97637800, 362637800], [104577600, 352251500],
  37. [107014700, 340000000], [104577600, 327748400], [97637800, 317362100], [87251500, 310422300],
  38. [82789200, 309534700], [69846100, 294726100], [254081000, 294726100], [285273900, 348753500],
  39. [285273900, 461246400], [254081000, 515273900],
  40. );
  41. # this points belongs to $polyline
  42. # note: it's actually a vertex, while we should better check an intermediate point
  43. my $point = Slic3r::Point->new(104577600, 327748400);
  44. local $Slic3r::Geometry::epsilon = 1E-5;
  45. is_deeply Slic3r::Geometry::polygon_segment_having_point($polygon, $point)->pp,
  46. [ [107014700, 340000000], [104577600, 327748400] ],
  47. 'polygon_segment_having_point';
  48. }
  49. #==========================================================
  50. {
  51. my $point = Slic3r::Point->new(736310778.185108, 5017423926.8924);
  52. my $line = Slic3r::Line->new([627484000, 3695776000], [750000000, 3720147000]);
  53. is Slic3r::Geometry::point_in_segment($point, $line), 0, 'point_in_segment';
  54. }
  55. #==========================================================
  56. {
  57. my $point = Slic3r::Point->new(736310778.185108, 5017423926.8924);
  58. my $line = Slic3r::Line->new([627484000, 3695776000], [750000000, 3720147000]);
  59. is Slic3r::Geometry::point_in_segment($point, $line), 0, 'point_in_segment';
  60. }
  61. #==========================================================
  62. my $polygons = [
  63. Slic3r::Polygon->new( # contour, ccw
  64. [45919000, 515273900], [14726100, 461246400], [14726100, 348753500], [33988700, 315389800],
  65. [43749700, 343843000], [45422300, 352251500], [52362100, 362637800], [62748400, 369577600],
  66. [75000000, 372014700], [87251500, 369577600], [97637800, 362637800], [104577600, 352251500],
  67. [107014700, 340000000], [104577600, 327748400], [97637800, 317362100], [87251500, 310422300],
  68. [82789200, 309534700], [69846100, 294726100], [254081000, 294726100], [285273900, 348753500],
  69. [285273900, 461246400], [254081000, 515273900],
  70. ),
  71. Slic3r::Polygon->new( # hole, cw
  72. [75000000, 502014700], [87251500, 499577600], [97637800, 492637800], [104577600, 482251500],
  73. [107014700, 470000000], [104577600, 457748400], [97637800, 447362100], [87251500, 440422300],
  74. [75000000, 437985300], [62748400, 440422300], [52362100, 447362100], [45422300, 457748400],
  75. [42985300, 470000000], [45422300, 482251500], [52362100, 492637800], [62748400, 499577600],
  76. ),
  77. ];
  78. #==========================================================
  79. {
  80. my $p1 = [10, 10];
  81. my $p2 = [10, 20];
  82. my $p3 = [10, 30];
  83. my $p4 = [20, 20];
  84. my $p5 = [0, 20];
  85. is Slic3r::Geometry::angle3points($p2, $p3, $p1), PI(), 'angle3points';
  86. is Slic3r::Geometry::angle3points($p2, $p1, $p3), PI(), 'angle3points';
  87. is Slic3r::Geometry::angle3points($p2, $p3, $p4), PI()/2*3, 'angle3points';
  88. is Slic3r::Geometry::angle3points($p2, $p4, $p3), PI()/2, 'angle3points';
  89. is Slic3r::Geometry::angle3points($p2, $p1, $p4), PI()/2, 'angle3points';
  90. is Slic3r::Geometry::angle3points($p2, $p1, $p5), PI()/2*3, 'angle3points';
  91. }
  92. {
  93. my $p1 = [30, 30];
  94. my $p2 = [20, 20];
  95. my $p3 = [10, 10];
  96. my $p4 = [30, 10];
  97. is Slic3r::Geometry::angle3points($p2, $p1, $p3), PI(), 'angle3points';
  98. is Slic3r::Geometry::angle3points($p2, $p1, $p4), PI()/2*3, 'angle3points';
  99. is Slic3r::Geometry::angle3points($p2, $p1, $p1), 2*PI(), 'angle3points';
  100. }
  101. #==========================================================
  102. {
  103. my $cw_square = [ [0,0], [0,10], [10,10], [10,0] ];
  104. is polygon_is_convex($cw_square), 0, 'cw square is not convex';
  105. is polygon_is_convex([ reverse @$cw_square ]), 1, 'ccw square is convex';
  106. my $convex1 = [ [0,0], [10,0], [10,10], [0,10], [0,6], [4,6], [4,4], [0,4] ];
  107. is polygon_is_convex($convex1), 0, 'concave polygon';
  108. }
  109. #==========================================================
  110. {
  111. my $polyline = Slic3r::Polyline->new([0, 0], [10, 0], [20, 0]);
  112. is_deeply [ map $_->pp, @{$polyline->lines} ], [
  113. [ [0, 0], [10, 0] ],
  114. [ [10, 0], [20, 0] ],
  115. ], 'polyline_lines';
  116. }
  117. #==========================================================
  118. {
  119. my $polygon = Slic3r::Polygon->new([0, 0], [10, 0], [5, 5]);
  120. my $result = $polygon->split_at_index(1);
  121. is ref($result), 'Slic3r::Polyline', 'split_at_index returns polyline';
  122. is_deeply $result->pp, [ [10, 0], [5, 5], [0, 0], [10, 0] ], 'split_at_index';
  123. }
  124. #==========================================================
  125. {
  126. my $bb = Slic3r::Geometry::BoundingBox->new_from_points([ map Slic3r::Point->new(@$_), [0, 1], [10, 2], [20, 2] ]);
  127. $bb->scale(2);
  128. is_deeply [ $bb->min_point->pp, $bb->max_point->pp ], [ [0,2], [40,4] ], 'bounding box is scaled correctly';
  129. }
  130. #==========================================================
  131. {
  132. my $line = Slic3r::Line->new([10,10], [20,10]);
  133. is $line->grow(5)->[0]->area, Slic3r::Polygon->new([10,5], [20,5], [20,15], [10,15])->area, 'grow line';
  134. }
  135. #==========================================================
  136. {
  137. # if chained_path() works correctly, these points should be joined with no diagonal paths
  138. # (thus 26 units long)
  139. my @points = map Slic3r::Point->new_scale(@$_), [26,26],[52,26],[0,26],[26,52],[26,0],[0,52],[52,52],[52,0];
  140. my @ordered = @points[@{chained_path_from(\@points, $points[0])}];
  141. ok !(grep { abs($ordered[$_]->distance_to($ordered[$_+1]) - scale 26) > epsilon } 0..$#ordered-1), 'chained_path';
  142. }
  143. #==========================================================
  144. {
  145. my $line = Slic3r::Line->new([0, 0], [20, 0]);
  146. is +Slic3r::Point->new(10, 10)->distance_to_line($line), 10, 'distance_to';
  147. is +Slic3r::Point->new(50, 0)->distance_to_line($line), 30, 'distance_to';
  148. is +Slic3r::Point->new(0, 0)->distance_to_line($line), 0, 'distance_to';
  149. is +Slic3r::Point->new(20, 0)->distance_to_line($line), 0, 'distance_to';
  150. is +Slic3r::Point->new(10, 0)->distance_to_line($line), 0, 'distance_to';
  151. }
  152. #==========================================================
  153. {
  154. my $square = Slic3r::Polygon->new_scale(
  155. [100,100],
  156. [200,100],
  157. [200,200],
  158. [100,200],
  159. );
  160. is scalar(@{$square->concave_points(PI*4/3)}), 0, 'no concave vertices detected in ccw square';
  161. is scalar(@{$square->convex_points(PI*2/3)}), 4, 'four convex vertices detected in ccw square';
  162. $square->make_clockwise;
  163. is scalar(@{$square->concave_points(PI*4/3)}), 4, 'fuor concave vertices detected in cw square';
  164. is scalar(@{$square->convex_points(PI*2/3)}), 0, 'no convex vertices detected in cw square';
  165. }
  166. {
  167. my $square = Slic3r::Polygon->new_scale(
  168. [150,100],
  169. [200,100],
  170. [200,200],
  171. [100,200],
  172. [100,100],
  173. );
  174. is scalar(@{$square->concave_points(PI*4/3)}), 0, 'no concave vertices detected in convex polygon';
  175. is scalar(@{$square->convex_points(PI*2/3)}), 4, 'four convex vertices detected in square';
  176. }
  177. {
  178. my $square = Slic3r::Polygon->new_scale(
  179. [200,200],
  180. [100,200],
  181. [100,100],
  182. [150,100],
  183. [200,100],
  184. );
  185. is scalar(@{$square->concave_points(PI*4/3)}), 0, 'no concave vertices detected in convex polygon';
  186. is scalar(@{$square->convex_points(PI*2/3)}), 4, 'four convex vertices detected in square';
  187. }
  188. {
  189. my $triangle = Slic3r::Polygon->new(
  190. [16000170,26257364], [714223,461012], [31286371,461008],
  191. );
  192. is scalar(@{$triangle->concave_points(PI*4/3)}), 0, 'no concave vertices detected in triangle';
  193. is scalar(@{$triangle->convex_points(PI*2/3)}), 3, 'three convex vertices detected in triangle';
  194. }
  195. {
  196. my $triangle = Slic3r::Polygon->new(
  197. [16000170,26257364], [714223,461012], [20000000,461012], [31286371,461012],
  198. );
  199. is scalar(@{$triangle->concave_points(PI*4/3)}), 0, 'no concave vertices detected in triangle having collinear point';
  200. is scalar(@{$triangle->convex_points(PI*2/3)}), 3, 'three convex vertices detected in triangle having collinear point';
  201. }
  202. {
  203. my $triangle = Slic3r::Polygon->new(
  204. [16000170,26257364], [714223,461012], [31286371,461008],
  205. );
  206. my $simplified = $triangle->simplify(250000)->[0];
  207. is scalar(@$simplified), 3, 'triangle is never simplified to less than 3 points';
  208. }
  209. __END__