04_expolygon.t 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Slic3r::XS;
  5. use Test::More tests => 17;
  6. use constant PI => 4 * atan2(1, 1);
  7. my $square = [ # ccw
  8. [100, 100],
  9. [200, 100],
  10. [200, 200],
  11. [100, 200],
  12. ];
  13. my $hole_in_square = [ # cw
  14. [140, 140],
  15. [140, 160],
  16. [160, 160],
  17. [160, 140],
  18. ];
  19. my $expolygon = Slic3r::ExPolygon->new($square, $hole_in_square);
  20. is ref($expolygon->pp), 'ARRAY', 'expolygon pp is unblessed';
  21. is_deeply $expolygon->pp, [$square, $hole_in_square], 'expolygon roundtrip';
  22. is ref($expolygon->arrayref), 'ARRAY', 'expolygon arrayref is unblessed';
  23. isa_ok $expolygon->[0], 'Slic3r::Polygon', 'expolygon polygon is blessed';
  24. isa_ok $expolygon->[0][0], 'Slic3r::Point', 'expolygon point is blessed';
  25. {
  26. my $polygon = $expolygon->[0];
  27. $polygon->scale(2);
  28. isnt $expolygon->[0][0][0], $polygon->[0][0], 'a copy of polygons is returned';
  29. }
  30. is_deeply $expolygon->clone->pp, [$square, $hole_in_square], 'clone';
  31. # The following tests implicitely check that modifying clones
  32. # does not modify the original one.
  33. {
  34. my $expolygon2 = $expolygon->clone;
  35. $expolygon2->scale(2.5);
  36. is_deeply $expolygon2->pp, [
  37. [map [ 2.5*$_->[0], 2.5*$_->[1] ], @$square],
  38. [map [ 2.5*$_->[0], 2.5*$_->[1] ], @$hole_in_square]
  39. ], 'scale';
  40. }
  41. {
  42. my $expolygon2 = $expolygon->clone;
  43. $expolygon2->translate(10, -5);
  44. is_deeply $expolygon2->pp, [
  45. [map [ $_->[0]+10, $_->[1]-5 ], @$square],
  46. [map [ $_->[0]+10, $_->[1]-5 ], @$hole_in_square]
  47. ], 'translate';
  48. }
  49. {
  50. my $expolygon2 = $expolygon->clone;
  51. $expolygon2->rotate(PI/2, Slic3r::Point->new(150,150));
  52. is_deeply $expolygon2->pp, [
  53. [ @$square[1,2,3,0] ],
  54. [ @$hole_in_square[3,0,1,2] ]
  55. ], 'rotate around Point';
  56. }
  57. {
  58. my $expolygon2 = $expolygon->clone;
  59. $expolygon2->rotate(PI/2, [150,150]);
  60. is_deeply $expolygon2->pp, [
  61. [ @$square[1,2,3,0] ],
  62. [ @$hole_in_square[3,0,1,2] ]
  63. ], 'rotate around pure-Perl Point';
  64. }
  65. {
  66. my $expolygon2 = $expolygon->clone;
  67. $expolygon2->scale(2);
  68. my $collection = Slic3r::ExPolygon::Collection->new($expolygon->pp, $expolygon2->pp);
  69. is_deeply $collection->pp, [ $expolygon->pp, $expolygon2->pp ],
  70. 'expolygon collection (pure Perl) roundtrip';
  71. my $collection2 = Slic3r::ExPolygon::Collection->new($expolygon, $expolygon2);
  72. is_deeply $collection->pp, $collection2->pp,
  73. 'expolygon collection (XS) roundtrip';
  74. $collection->clear;
  75. is scalar(@$collection), 0, 'clear collection';
  76. $collection->append($expolygon);
  77. is scalar(@$collection), 1, 'append to collection';
  78. my $exp = $collection->[0];
  79. $exp->scale(3);
  80. ### we store a copy, not the original by reference
  81. ###is_deeply $expolygon->pp, $exp->pp, 'input is stored by reference in collection';
  82. is_deeply $collection->[0]->pp, $exp->pp, 'collection items are returned by reference';
  83. is_deeply $collection->[0]->clone->pp, $collection->[0]->pp, 'clone collection item';
  84. }
  85. __END__