10_line.t 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Slic3r::XS;
  5. use Test::More tests => 40;
  6. use constant PI => 4 * atan2(1, 1);
  7. use constant EPSILON => 1E-4;
  8. my $points = [
  9. [100, 100],
  10. [200, 100],
  11. ];
  12. my $line = Slic3r::Line->new(@$points);
  13. is_deeply $line->pp, $points, 'line roundtrip';
  14. is ref($line->arrayref), 'ARRAY', 'line arrayref is unblessed';
  15. isa_ok $line->[0], 'Slic3r::Point::Ref', 'line point is blessed';
  16. {
  17. my $clone = $line->clone;
  18. $clone->reverse;
  19. is_deeply $clone->pp, [ reverse @$points ], 'reverse';
  20. }
  21. {
  22. my $line2 = Slic3r::Line->new($line->a->clone, $line->b->clone);
  23. is_deeply $line2->pp, $points, 'line roundtrip with cloned points';
  24. }
  25. {
  26. my $clone = $line->clone;
  27. $clone->translate(10, -5);
  28. is_deeply $clone->pp, [
  29. [110, 95],
  30. [210, 95],
  31. ], 'translate';
  32. }
  33. {
  34. ok +Slic3r::Line->new([0,0],[200,0])->parallel_to_line(Slic3r::Line->new([200,200],[0,200])), 'parallel_to';
  35. }
  36. foreach my $base_angle (0, PI/4, PI/2, PI) {
  37. my $line = Slic3r::Line->new([0,0], [100,0]);
  38. $line->rotate($base_angle, [0,0]);
  39. my $clone = $line->clone;
  40. ok $line->parallel_to_line($clone), 'line is parallel to self';
  41. $clone->reverse;
  42. ok $line->parallel_to_line($clone), 'line is parallel to self + PI';
  43. ok $line->parallel_to($line->direction), 'line is parallel to its direction';
  44. ok $line->parallel_to($line->direction + PI), 'line is parallel to its direction + PI';
  45. ok $line->parallel_to($line->direction - PI), 'line is parallel to its direction - PI';
  46. {
  47. my $line2 = $line->clone;
  48. $line2->reverse;
  49. ok $line->parallel_to_line($line2), 'line is parallel to its opposite';
  50. }
  51. {
  52. my $line2 = $line->clone;
  53. $line2->rotate(+(EPSILON)/2, [0,0]);
  54. ok $line->parallel_to_line($line2), 'line is parallel within epsilon';
  55. }
  56. {
  57. my $line2 = $line->clone;
  58. $line2->rotate(-(EPSILON)/2, [0,0]);
  59. ok $line->parallel_to_line($line2), 'line is parallel within epsilon';
  60. }
  61. }
  62. {
  63. my $a = Slic3r::Line->new([100, 0], [200, 0]);
  64. my $b = Slic3r::Line->new([300, 300], [300, 100]);
  65. my $r = $a->intersection_infinite($b);
  66. is_deeply $r->pp, [300, 0], 'intersection_infinite';
  67. }
  68. __END__