geometry.t 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. use Test::More;
  2. use strict;
  3. use warnings;
  4. plan tests => 26;
  5. BEGIN {
  6. use FindBin;
  7. use lib "$FindBin::Bin/../lib";
  8. }
  9. use Slic3r;
  10. use Slic3r::Geometry qw(PI polyline_remove_parallel_continuous_edges
  11. polyline_remove_acute_vertices polygon_remove_acute_vertices
  12. polygon_remove_parallel_continuous_edges polygon_is_convex
  13. chained_path_points epsilon scale);
  14. #==========================================================
  15. my $line1 = [ [5, 15], [30, 15] ];
  16. my $line2 = [ [10, 20], [10, 10] ];
  17. is_deeply Slic3r::Geometry::line_intersection($line1, $line2, 1)->arrayref, [10, 15], 'line_intersection';
  18. #==========================================================
  19. $line1 = [ [73.6310778185108/0.0000001, 371.74239268924/0.0000001], [73.6310778185108/0.0000001, 501.74239268924/0.0000001] ];
  20. $line2 = [ [75/0.0000001, 437.9853/0.0000001], [62.7484/0.0000001, 440.4223/0.0000001] ];
  21. isnt Slic3r::Geometry::line_intersection($line1, $line2, 1), undef, 'line_intersection';
  22. #==========================================================
  23. {
  24. my $polygon = Slic3r::Polygon->new(
  25. [45919000, 515273900], [14726100, 461246400], [14726100, 348753500], [33988700, 315389800],
  26. [43749700, 343843000], [45422300, 352251500], [52362100, 362637800], [62748400, 369577600],
  27. [75000000, 372014700], [87251500, 369577600], [97637800, 362637800], [104577600, 352251500],
  28. [107014700, 340000000], [104577600, 327748400], [97637800, 317362100], [87251500, 310422300],
  29. [82789200, 309534700], [69846100, 294726100], [254081000, 294726100], [285273900, 348753500],
  30. [285273900, 461246400], [254081000, 515273900],
  31. );
  32. # this points belongs to $polyline
  33. # note: it's actually a vertex, while we should better check an intermediate point
  34. my $point = Slic3r::Point->new(104577600, 327748400);
  35. local $Slic3r::Geometry::epsilon = 1E-5;
  36. is_deeply Slic3r::Geometry::polygon_segment_having_point($polygon, $point)->pp,
  37. [ [107014700, 340000000], [104577600, 327748400] ],
  38. 'polygon_segment_having_point';
  39. }
  40. #==========================================================
  41. {
  42. my $point = Slic3r::Point->new(736310778.185108, 5017423926.8924);
  43. my $line = Slic3r::Line->new([627484000, 3695776000], [750000000, 3720147000]);
  44. is Slic3r::Geometry::point_in_segment($point, $line), 0, 'point_in_segment';
  45. }
  46. #==========================================================
  47. {
  48. my $point = Slic3r::Point->new(736310778.185108, 5017423926.8924);
  49. my $line = Slic3r::Line->new([627484000, 3695776000], [750000000, 3720147000]);
  50. is Slic3r::Geometry::point_in_segment($point, $line), 0, 'point_in_segment';
  51. }
  52. #==========================================================
  53. my $polygons = [
  54. Slic3r::Polygon->new( # contour, ccw
  55. [459190000, 5152739000], [147261000, 4612464000], [147261000, 3487535000], [339887000, 3153898000],
  56. [437497000, 3438430000], [454223000, 3522515000], [523621000, 3626378000], [627484000, 3695776000],
  57. [750000000, 3720147000], [872515000, 3695776000], [976378000, 3626378000], [1045776000, 3522515000],
  58. [1070147000, 3400000000], [1045776000, 3277484000], [976378000, 3173621000], [872515000, 3104223000],
  59. [827892000, 3095347000], [698461000, 2947261000], [2540810000, 2947261000], [2852739000, 3487535000],
  60. [2852739000, 4612464000], [2540810000, 5152739000],
  61. ),
  62. Slic3r::Polygon->new( # hole, cw
  63. [750000000, 5020147000], [872515000, 4995776000], [976378000, 4926378000], [1045776000, 4822515000],
  64. [1070147000, 4700000000], [1045776000, 4577484000], [976378000, 4473621000], [872515000, 4404223000],
  65. [750000000, 4379853000], [627484000, 4404223000], [523621000, 4473621000], [454223000, 4577484000],
  66. [429853000, 4700000000], [454223000, 4822515000], [523621000, 4926378000], [627484000, 4995776000],
  67. ),
  68. ];
  69. my $points = [
  70. Slic3r::Point->new(736310778.185108, 3717423926.892399788),
  71. Slic3r::Point->new(736310778.185108, 5017423926.8924),
  72. ];
  73. is Slic3r::Geometry::can_connect_points(@$points, $polygons), 0, 'can_connect_points';
  74. #==========================================================
  75. {
  76. my $p1 = [10, 10];
  77. my $p2 = [10, 20];
  78. my $p3 = [10, 30];
  79. my $p4 = [20, 20];
  80. my $p5 = [0, 20];
  81. is Slic3r::Geometry::angle3points($p2, $p3, $p1), PI(), 'angle3points';
  82. is Slic3r::Geometry::angle3points($p2, $p1, $p3), PI(), 'angle3points';
  83. is Slic3r::Geometry::angle3points($p2, $p3, $p4), PI()/2*3, 'angle3points';
  84. is Slic3r::Geometry::angle3points($p2, $p4, $p3), PI()/2, 'angle3points';
  85. is Slic3r::Geometry::angle3points($p2, $p1, $p4), PI()/2, 'angle3points';
  86. is Slic3r::Geometry::angle3points($p2, $p1, $p5), PI()/2*3, 'angle3points';
  87. }
  88. {
  89. my $p1 = [30, 30];
  90. my $p2 = [20, 20];
  91. my $p3 = [10, 10];
  92. my $p4 = [30, 10];
  93. is Slic3r::Geometry::angle3points($p2, $p1, $p3), PI(), 'angle3points';
  94. is Slic3r::Geometry::angle3points($p2, $p1, $p4), PI()/2*3, 'angle3points';
  95. is Slic3r::Geometry::angle3points($p2, $p1, $p1), 2*PI(), 'angle3points';
  96. }
  97. #==========================================================
  98. {
  99. my $polygon = [
  100. [2265447881, 7013509857], [2271869937, 7009802077], [2606221146, 6816764300], [1132221146, 4263721402],
  101. [1098721150, 4205697705], [1228411320, 4130821051], [1557501031, 3940821051], [1737340080, 3836990933],
  102. [1736886253, 3837252951], [1494771522, 3977037948], [2959638603, 6514262167], [3002271522, 6588104548],
  103. [3083252364, 6541350240], [2854283608, 6673545411], [2525193897, 6863545411],
  104. ];
  105. polygon_remove_parallel_continuous_edges($polygon);
  106. polygon_remove_acute_vertices($polygon);
  107. is scalar(@$polygon), 4, 'polygon_remove_acute_vertices';
  108. }
  109. #==========================================================
  110. {
  111. my $polygon = [
  112. [226.5447881,701.3509857], [260.6221146,681.67643], [109.872115,420.5697705], [149.4771522,397.7037948],
  113. [300.2271522,658.8104548], [308.3252364,654.135024],
  114. ];
  115. polyline_remove_acute_vertices($polygon);
  116. is scalar(@$polygon), 6, 'polyline_remove_acute_vertices';
  117. }
  118. #==========================================================
  119. {
  120. my $cw_square = [ [0,0], [0,10], [10,10], [10,0] ];
  121. is polygon_is_convex($cw_square), 0, 'cw square is not convex';
  122. is polygon_is_convex([ reverse @$cw_square ]), 1, 'ccw square is convex';
  123. my $convex1 = [ [0,0], [10,0], [10,10], [0,10], [0,6], [4,6], [4,4], [0,4] ];
  124. is polygon_is_convex($convex1), 0, 'concave polygon';
  125. }
  126. #==========================================================
  127. {
  128. my $polyline = Slic3r::Polyline->new([0, 0], [10, 0], [20, 0]);
  129. is_deeply [ map $_->pp, @{$polyline->lines} ], [
  130. [ [0, 0], [10, 0] ],
  131. [ [10, 0], [20, 0] ],
  132. ], 'polyline_lines';
  133. }
  134. #==========================================================
  135. {
  136. my $polygon = Slic3r::Polygon->new([0, 0], [10, 0], [5, 5]);
  137. my $result = $polygon->split_at_index(1);
  138. is ref($result), 'Slic3r::Polyline', 'split_at_index returns polyline';
  139. is_deeply $result->pp, [ [10, 0], [5, 5], [0, 0], [10, 0] ], 'split_at_index';
  140. }
  141. #==========================================================
  142. {
  143. my $bb = Slic3r::Geometry::BoundingBox->new_from_points([ map Slic3r::Point->new(@$_), [0, 1], [10, 2], [20, 2] ]);
  144. $bb->scale(2);
  145. is_deeply $bb->extents, [ [0,40], [2,4] ], 'bounding box is scaled correctly';
  146. }
  147. #==========================================================
  148. {
  149. my $line = Slic3r::Line->new([10,10], [20,10]);
  150. is +($line->grow(5))[0]->area, Slic3r::Polygon->new([5,5], [25,5], [25,15], [5,15])->area, 'grow line';
  151. }
  152. #==========================================================
  153. {
  154. # if chained_path() works correctly, these points should be joined with no diagonal paths
  155. # (thus 26 units long)
  156. my @points = map Slic3r::Point->new_scale(@$_), [26,26],[52,26],[0,26],[26,52],[26,0],[0,52],[52,52],[52,0];
  157. my @ordered = @{chained_path_points(\@points, $points[0])};
  158. ok !(grep { abs($ordered[$_]->distance_to($ordered[$_+1]) - scale 26) > epsilon } 0..$#ordered-1), 'chained_path';
  159. }
  160. #==========================================================