06_polygon.t 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use List::Util qw(first);
  5. use Slic3r::XS;
  6. use Test::More tests => 21;
  7. use constant PI => 4 * atan2(1, 1);
  8. my $square = [ # ccw
  9. [100, 100],
  10. [200, 100],
  11. [200, 200],
  12. [100, 200],
  13. ];
  14. my $polygon = Slic3r::Polygon->new(@$square);
  15. my $cw_polygon = $polygon->clone;
  16. $cw_polygon->reverse;
  17. ok $polygon->is_valid, 'is_valid';
  18. is_deeply $polygon->pp, $square, 'polygon roundtrip';
  19. is ref($polygon->arrayref), 'ARRAY', 'polygon arrayref is unblessed';
  20. isa_ok $polygon->[0], 'Slic3r::Point::Ref', 'polygon point is blessed';
  21. my $lines = $polygon->lines;
  22. is_deeply [ map $_->pp, @$lines ], [
  23. [ [100, 100], [200, 100] ],
  24. [ [200, 100], [200, 200] ],
  25. [ [200, 200], [100, 200] ],
  26. [ [100, 200], [100, 100] ],
  27. ], 'polygon lines';
  28. is_deeply $polygon->split_at_first_point->pp, [ @$square[0,1,2,3,0] ], 'split_at_first_point';
  29. is_deeply $polygon->split_at_index(2)->pp, [ @$square[2,3,0,1,2] ], 'split_at_index';
  30. is_deeply $polygon->split_at_vertex(Slic3r::Point->new(@{$square->[2]}))->pp, [ @$square[2,3,0,1,2] ], 'split_at';
  31. is $polygon->area, 100*100, 'area';
  32. ok $polygon->is_counter_clockwise, 'is_counter_clockwise';
  33. ok !$cw_polygon->is_counter_clockwise, 'is_counter_clockwise';
  34. {
  35. my $clone = $polygon->clone;
  36. $clone->reverse;
  37. ok !$clone->is_counter_clockwise, 'is_counter_clockwise';
  38. $clone->make_counter_clockwise;
  39. ok $clone->is_counter_clockwise, 'make_counter_clockwise';
  40. $clone->make_counter_clockwise;
  41. ok $clone->is_counter_clockwise, 'make_counter_clockwise';
  42. }
  43. ok ref($polygon->first_point) eq 'Slic3r::Point', 'first_point';
  44. ok $polygon->contains_point(Slic3r::Point->new(150,150)), 'ccw contains_point';
  45. ok $cw_polygon->contains_point(Slic3r::Point->new(150,150)), 'cw contains_point';
  46. {
  47. my @points = (Slic3r::Point->new(100,0));
  48. foreach my $i (1..5) {
  49. my $point = $points[0]->clone;
  50. $point->rotate(PI/3*$i, [0,0]);
  51. push @points, $point;
  52. }
  53. my $hexagon = Slic3r::Polygon->new(@points);
  54. my $triangles = $hexagon->triangulate_convex;
  55. is scalar(@$triangles), 4, 'right number of triangles';
  56. ok !(defined first { $_->is_clockwise } @$triangles), 'all triangles are ccw';
  57. }
  58. {
  59. is_deeply $polygon->centroid->pp, [150,150], 'centroid';
  60. }
  61. {
  62. my $polygon = Slic3r::Polygon->new(
  63. [50000000, 100000000],
  64. [300000000, 102000000],
  65. [50000000, 104000000],
  66. );
  67. my $line = Slic3r::Line->new([175992032,102000000], [47983964,102000000]);
  68. my $intersection = $polygon->intersection($line);
  69. is_deeply $intersection->pp, [50000000, 102000000], 'polygon-line intersection';
  70. }
  71. # this is not a test: this just demonstrates bad usage, where $polygon->clone gets
  72. # DESTROY'ed before the derived object ($point), causing bad memory access
  73. if (0) {
  74. my $point;
  75. {
  76. $point = $polygon->clone->[0];
  77. }
  78. $point->scale(2);
  79. }
  80. __END__