18_motionplanner.t 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. BEGIN {
  5. use FindBin;
  6. use lib "$FindBin::Bin/../../lib";
  7. }
  8. use Slic3r::XS;
  9. use Test::More tests => 20;
  10. my $square = Slic3r::Polygon->new( # ccw
  11. [100, 100],
  12. [200, 100],
  13. [200, 200],
  14. [100, 200],
  15. );
  16. my $hole_in_square = Slic3r::Polygon->new( # cw
  17. [140, 140],
  18. [140, 160],
  19. [160, 160],
  20. [160, 140],
  21. );
  22. $_->scale(1/0.000001) for $square, $hole_in_square;
  23. my $expolygon = Slic3r::ExPolygon->new($square, $hole_in_square);
  24. {
  25. my $mp = Slic3r::MotionPlanner->new([ $expolygon ]);
  26. isa_ok $mp, 'Slic3r::MotionPlanner';
  27. my $from = Slic3r::Point->new(120, 120);
  28. my $to = Slic3r::Point->new(180,180);
  29. $_->scale(1/0.000001) for $from, $to;
  30. my $path = $mp->shortest_path($from, $to);
  31. ok $path->is_valid(), 'return path is valid';
  32. ok $path->length > Slic3r::Line->new($from, $to)->length, 'path length is greater than straight line';
  33. ok $path->first_point->coincides_with($from), 'first path point coincides with initial point';
  34. ok $path->last_point->coincides_with($to), 'last path point coincides with destination point';
  35. ok $expolygon->contains_polyline($path), 'path is fully contained in expolygon';
  36. }
  37. {
  38. my $mp = Slic3r::MotionPlanner->new([ $expolygon ]);
  39. isa_ok $mp, 'Slic3r::MotionPlanner';
  40. my $from = Slic3r::Point->new(80, 100);
  41. my $to = Slic3r::Point->new(220,200);
  42. $_->scale(1/0.000001) for $from, $to;
  43. my $path = $mp->shortest_path($from, $to);
  44. ok $path->is_valid(), 'return path is valid';
  45. ok $path->length > Slic3r::Line->new($from, $to)->length, 'path length is greater than straight line';
  46. ok $path->first_point->coincides_with($from), 'first path point coincides with initial point';
  47. ok $path->last_point->coincides_with($to), 'last path point coincides with destination point';
  48. is scalar(@{ Slic3r::Geometry::Clipper::intersection_pl([$path], [@$expolygon]) }), 0, 'path has no intersection with expolygon';
  49. }
  50. {
  51. my $expolygon2 = $expolygon->clone;
  52. $expolygon2->translate(300/0.000001, 0);
  53. my $mp = Slic3r::MotionPlanner->new([ $expolygon, $expolygon2 ]);
  54. isa_ok $mp, 'Slic3r::MotionPlanner';
  55. my $from = Slic3r::Point->new(120, 120);
  56. my $to = Slic3r::Point->new(120 + 300, 120);
  57. $_->scale(1/0.000001) for $from, $to;
  58. ok $expolygon->contains_point($from), 'start point is contained in first expolygon';
  59. ok $expolygon2->contains_point($to), 'end point is contained in second expolygon';
  60. my $path = $mp->shortest_path($from, $to);
  61. ok $path->is_valid(), 'return path is valid';
  62. }
  63. {
  64. my $expolygons = [
  65. Slic3r::ExPolygon->new([[123800962,89330311],[123959159,89699438],[124000004,89898430],[124000012,110116427],[123946510,110343065],[123767391,110701303],[123284087,111000001],[102585791,111000009],[102000004,110414223],[102000004,89585787],[102585790,89000000],[123300022,88999993]]),
  66. Slic3r::ExPolygon->new([[97800954,89330311],[97959151,89699438],[97999996,89898430],[98000004,110116427],[97946502,110343065],[97767383,110701303],[97284079,111000001],[76585783,111000009],[75999996,110414223],[75999996,89585787],[76585782,89000000],[97300014,88999993]]),
  67. ];
  68. my $mp = Slic3r::MotionPlanner->new($expolygons);
  69. isa_ok $mp, 'Slic3r::MotionPlanner';
  70. my $from = Slic3r::Point->new(79120520, 107839491);
  71. my $to = Slic3r::Point->new(104664164, 108335852);
  72. ok $expolygons->[1]->contains_point($from), 'start point is contained in second expolygon';
  73. ok $expolygons->[0]->contains_point($to), 'end point is contained in first expolygon';
  74. my $path = $mp->shortest_path($from, $to);
  75. ok $path->is_valid(), 'return path is valid';
  76. }
  77. __END__