04_expolygon.t 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use List::Util qw(first sum);
  5. use Slic3r::XS;
  6. use Test::More tests => 33;
  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 $hole_in_square = [ # cw
  15. [140, 140],
  16. [140, 160],
  17. [160, 160],
  18. [160, 140],
  19. ];
  20. my $expolygon = Slic3r::ExPolygon->new($square, $hole_in_square);
  21. ok $expolygon->is_valid, 'is_valid';
  22. is ref($expolygon->pp), 'ARRAY', 'expolygon pp is unblessed';
  23. is_deeply $expolygon->pp, [$square, $hole_in_square], 'expolygon roundtrip';
  24. is ref($expolygon->arrayref), 'ARRAY', 'expolygon arrayref is unblessed';
  25. isa_ok $expolygon->[0], 'Slic3r::Polygon::Ref', 'expolygon polygon is blessed';
  26. isa_ok $expolygon->contour, 'Slic3r::Polygon::Ref', 'expolygon contour is blessed';
  27. isa_ok $expolygon->holes->[0], 'Slic3r::Polygon::Ref', 'expolygon hole is blessed';
  28. isa_ok $expolygon->[0][0], 'Slic3r::Point::Ref', 'expolygon point is blessed';
  29. {
  30. my $expolygon2 = $expolygon->clone;
  31. my $polygon = $expolygon2->[0];
  32. $polygon->scale(2);
  33. is $expolygon2->[0][0][0], $polygon->[0][0], 'polygons are returned by reference';
  34. }
  35. is_deeply $expolygon->clone->pp, [$square, $hole_in_square], 'clone';
  36. is $expolygon->area, 100*100-20*20, 'area';
  37. {
  38. my $expolygon2 = $expolygon->clone;
  39. $expolygon2->scale(2.5);
  40. is_deeply $expolygon2->pp, [
  41. [map [ 2.5*$_->[0], 2.5*$_->[1] ], @$square],
  42. [map [ 2.5*$_->[0], 2.5*$_->[1] ], @$hole_in_square]
  43. ], 'scale';
  44. }
  45. {
  46. my $expolygon2 = $expolygon->clone;
  47. $expolygon2->translate(10, -5);
  48. is_deeply $expolygon2->pp, [
  49. [map [ $_->[0]+10, $_->[1]-5 ], @$square],
  50. [map [ $_->[0]+10, $_->[1]-5 ], @$hole_in_square]
  51. ], 'translate';
  52. }
  53. {
  54. my $expolygon2 = $expolygon->clone;
  55. $expolygon2->rotate(PI/2, Slic3r::Point->new(150,150));
  56. is_deeply $expolygon2->pp, [
  57. [ @$square[1,2,3,0] ],
  58. [ @$hole_in_square[3,0,1,2] ]
  59. ], 'rotate around Point';
  60. }
  61. {
  62. my $expolygon2 = $expolygon->clone;
  63. $expolygon2->rotate(PI/2, [150,150]);
  64. is_deeply $expolygon2->pp, [
  65. [ @$square[1,2,3,0] ],
  66. [ @$hole_in_square[3,0,1,2] ]
  67. ], 'rotate around pure-Perl Point';
  68. }
  69. {
  70. my $expolygon2 = $expolygon->clone;
  71. $expolygon2->scale(2);
  72. my $collection = Slic3r::ExPolygon::Collection->new($expolygon->pp, $expolygon2->pp);
  73. is_deeply $collection->pp, [ $expolygon->pp, $expolygon2->pp ],
  74. 'expolygon collection (pure Perl) roundtrip';
  75. my $collection2 = Slic3r::ExPolygon::Collection->new($expolygon, $expolygon2);
  76. is_deeply $collection->pp, $collection2->pp,
  77. 'expolygon collection (XS) roundtrip';
  78. $collection->clear;
  79. is scalar(@$collection), 0, 'clear collection';
  80. $collection->append($expolygon);
  81. is scalar(@$collection), 1, 'append to collection';
  82. my $exp = $collection->[0];
  83. $exp->scale(3);
  84. is $collection->[0][0][0][0], $exp->[0][0][0], 'collection items are returned by reference';
  85. is_deeply $collection->[0]->clone->pp, $collection->[0]->pp, 'clone collection item';
  86. }
  87. {
  88. my $expolygon = Slic3r::ExPolygon->new($square);
  89. my $polygons = $expolygon->get_trapezoids2(PI/2);
  90. is scalar(@$polygons), 1, 'correct number of trapezoids returned';
  91. is scalar(@{$polygons->[0]}), 4, 'trapezoid has 4 points';
  92. is $polygons->[0]->area, $expolygon->area, 'trapezoid has correct area';
  93. }
  94. {
  95. my $polygons = $expolygon->get_trapezoids2(PI/2);
  96. is scalar(@$polygons), 4, 'correct number of trapezoids returned';
  97. # trapezoid polygons might have more than 4 points in case of collinear segments
  98. $polygons = [ map @{$_->simplify(1)}, @$polygons ];
  99. ok !defined(first { @$_ != 4 } @$polygons), 'all trapezoids have 4 points';
  100. is scalar(grep { $_->area == 40*100 } @$polygons), 2, 'trapezoids have expected area';
  101. is scalar(grep { $_->area == 20*40 } @$polygons), 2, 'trapezoids have expected area';
  102. }
  103. {
  104. my $expolygon = Slic3r::ExPolygon->new([ [0,100],[100,0],[200,0],[300,100],[200,200],[100,200] ]);
  105. my $polygons = $expolygon->get_trapezoids2(PI/2);
  106. is scalar(@$polygons), 3, 'correct number of trapezoids returned';
  107. is scalar(grep { $_->area == 100*200/2 } @$polygons), 2, 'trapezoids have expected area';
  108. is scalar(grep { $_->area == 100*200 } @$polygons), 1, 'trapezoids have expected area';
  109. }
  110. {
  111. my $triangles = $expolygon->triangulate_pp;
  112. is scalar(@$triangles), 8, 'expected number of triangles';
  113. is sum(map $_->area, @$triangles), $expolygon->area, 'sum of triangles area equals original expolygon area';
  114. }
  115. __END__