09_polyline.t 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Slic3r::XS;
  5. use Test::More tests => 15;
  6. my $points = [
  7. [100, 100],
  8. [200, 100],
  9. [200, 200],
  10. ];
  11. my $polyline = Slic3r::Polyline->new(@$points);
  12. my $lines = $polyline->lines;
  13. is_deeply [ map $_->pp, @$lines ], [
  14. [ [100, 100], [200, 100] ],
  15. [ [200, 100], [200, 200] ],
  16. ], 'polyline lines';
  17. $polyline->append_polyline($polyline->clone);
  18. is_deeply $polyline->pp, [ @$points, @$points ], 'append_polyline';
  19. {
  20. my $len = $polyline->length;
  21. $polyline->clip_end($len/3);
  22. ok abs($polyline->length - ($len-($len/3))) < 1, 'clip_end';
  23. }
  24. {
  25. my $polyline = Slic3r::Polyline->new(
  26. [0,0], [20,0], [50,0], [80,0], [100,0],
  27. );
  28. $polyline->simplify(2);
  29. is_deeply $polyline->pp, [ [0,0], [100,0] ], 'Douglas-Peucker';
  30. }
  31. {
  32. my $polyline = Slic3r::Polyline->new(
  33. [0,0], [50,50], [100,0], [125,-25], [150,50],
  34. );
  35. $polyline->simplify(25);
  36. is_deeply $polyline->pp, [ [0, 0], [50, 50], [125, -25], [150, 50] ], 'Douglas-Peucker';
  37. }
  38. {
  39. my $polyline = Slic3r::Polyline->new(
  40. [0,0], [100,0], [50,10],
  41. );
  42. $polyline->simplify(25);
  43. is_deeply $polyline->pp, [ [0,0], [100,0], [50,10] ], 'Douglas-Peucker uses shortest distance instead of perpendicular distance';
  44. }
  45. {
  46. my $polyline = Slic3r::Polyline->new(@$points);
  47. is $polyline->length, 100*2, 'length';
  48. $polyline->extend_end(50);
  49. is $polyline->length, 100*2 + 50, 'extend_end';
  50. $polyline->extend_start(50);
  51. is $polyline->length, 100*2 + 50 + 50, 'extend_start';
  52. }
  53. {
  54. my $polyline = Slic3r::Polyline->new(@$points);
  55. my $p1 = Slic3r::Polyline->new;
  56. my $p2 = Slic3r::Polyline->new;
  57. my $point = Slic3r::Point->new(150, 100);
  58. $polyline->split_at($point, $p1, $p2);
  59. is scalar(@$p1), 2, 'split_at';
  60. is scalar(@$p2), 3, 'split_at';
  61. ok $p1->last_point->coincides_with($point), 'split_at';
  62. ok $p2->first_point->coincides_with($point), 'split_at';
  63. }
  64. {
  65. my $polyline = Slic3r::Polyline->new(@$points[0,1,2,0]);
  66. my $p1 = Slic3r::Polyline->new;
  67. my $p2 = Slic3r::Polyline->new;
  68. $polyline->split_at($polyline->first_point, $p1, $p2);
  69. is scalar(@$p1), 1, 'split_at';
  70. is scalar(@$p2), 4, 'split_at';
  71. }
  72. __END__