09_polyline.t 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Slic3r::XS;
  5. use Test::More tests => 18;
  6. my $points = [
  7. [100, 100],
  8. [200, 100],
  9. [200, 200],
  10. ];
  11. my $polyline = Slic3r::Polyline->new(@$points);
  12. is_deeply $polyline->pp, $points, 'polyline roundtrip';
  13. is ref($polyline->arrayref), 'ARRAY', 'polyline arrayref is unblessed';
  14. isa_ok $polyline->[0], 'Slic3r::Point::Ref', 'polyline point is blessed';
  15. my $lines = $polyline->lines;
  16. is_deeply [ map $_->pp, @$lines ], [
  17. [ [100, 100], [200, 100] ],
  18. [ [200, 100], [200, 200] ],
  19. ], 'polyline lines';
  20. $polyline->append_polyline($polyline->clone);
  21. is_deeply $polyline->pp, [ @$points, @$points ], 'append_polyline';
  22. {
  23. my $len = $polyline->length;
  24. $polyline->clip_end($len/3);
  25. ok abs($polyline->length - ($len-($len/3))) < 1, 'clip_end';
  26. }
  27. {
  28. my $polyline = Slic3r::Polyline->new(
  29. [0,0], [20,0], [50,0], [80,0], [100,0],
  30. );
  31. $polyline->simplify(2);
  32. is_deeply $polyline->pp, [ [0,0], [100,0] ], 'Douglas-Peucker';
  33. }
  34. {
  35. my $polyline = Slic3r::Polyline->new(
  36. [0,0], [50,50], [100,0], [125,-25], [150,50],
  37. );
  38. $polyline->simplify(25);
  39. is_deeply $polyline->pp, [ [0, 0], [50, 50], [125, -25], [150, 50] ], 'Douglas-Peucker';
  40. }
  41. {
  42. my $polyline = Slic3r::Polyline->new(
  43. [0,0], [100,0], [50,10],
  44. );
  45. $polyline->simplify(25);
  46. is_deeply $polyline->pp, [ [0,0], [100,0], [50,10] ], 'Douglas-Peucker uses shortest distance instead of perpendicular distance';
  47. }
  48. {
  49. my $polyline = Slic3r::Polyline->new(@$points);
  50. is $polyline->length, 100*2, 'length';
  51. $polyline->extend_end(50);
  52. is $polyline->length, 100*2 + 50, 'extend_end';
  53. $polyline->extend_start(50);
  54. is $polyline->length, 100*2 + 50 + 50, 'extend_start';
  55. }
  56. {
  57. my $polyline = Slic3r::Polyline->new(@$points);
  58. my $p1 = Slic3r::Polyline->new;
  59. my $p2 = Slic3r::Polyline->new;
  60. my $point = Slic3r::Point->new(150, 100);
  61. $polyline->split_at($point, $p1, $p2);
  62. is scalar(@$p1), 2, 'split_at';
  63. is scalar(@$p2), 3, 'split_at';
  64. ok $p1->last_point->coincides_with($point), 'split_at';
  65. ok $p2->first_point->coincides_with($point), 'split_at';
  66. }
  67. {
  68. my $polyline = Slic3r::Polyline->new(@$points[0,1,2,0]);
  69. my $p1 = Slic3r::Polyline->new;
  70. my $p2 = Slic3r::Polyline->new;
  71. $polyline->split_at($polyline->first_point, $p1, $p2);
  72. is scalar(@$p1), 1, 'split_at';
  73. is scalar(@$p2), 4, 'split_at';
  74. }
  75. # disabled because we now use a more efficient but incomplete algorithm
  76. #if (0) {
  77. # my $polyline = Slic3r::Polyline->new(
  78. # map [$_,10], (0,10,20,30,40,50,60)
  79. # );
  80. # {
  81. # my $expolygon = Slic3r::ExPolygon->new(Slic3r::Polygon->new(
  82. # [25,0], [55,0], [55,30], [25,30],
  83. # ));
  84. # my $p = $polyline->clone;
  85. # $p->simplify_by_visibility($expolygon);
  86. # is_deeply $p->pp, [
  87. # map [$_,10], (0,10,20,30,50,60)
  88. # ], 'simplify_by_visibility()';
  89. # }
  90. # {
  91. # my $expolygon = Slic3r::ExPolygon->new(Slic3r::Polygon->new(
  92. # [-15,0], [75,0], [75,30], [-15,30],
  93. # ));
  94. # my $p = $polyline->clone;
  95. # $p->simplify_by_visibility($expolygon);
  96. # is_deeply $p->pp, [
  97. # map [$_,10], (0,60)
  98. # ], 'simplify_by_visibility()';
  99. # }
  100. # {
  101. # my $expolygon = Slic3r::ExPolygon->new(Slic3r::Polygon->new(
  102. # [-15,0], [25,0], [25,30], [-15,30],
  103. # ));
  104. # my $p = $polyline->clone;
  105. # $p->simplify_by_visibility($expolygon);
  106. # is_deeply $p->pp, [
  107. # map [$_,10], (0,20,30,40,50,60)
  108. # ], 'simplify_by_visibility()';
  109. # }
  110. #}
  111. __END__