Line.pm 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package Slic3r::Line;
  2. use strict;
  3. use warnings;
  4. use Boost::Geometry::Utils;
  5. use Slic3r::Geometry qw(A B X Y);
  6. sub new {
  7. my $class = shift;
  8. my $self;
  9. $self = [ @_ ];
  10. bless $self, $class;
  11. bless $_, 'Slic3r::Point' for @$self;
  12. return $self;
  13. }
  14. sub boost_linestring {
  15. my $self = shift;
  16. return Boost::Geometry::Utils::linestring($self);
  17. }
  18. sub coincides_with {
  19. my $self = shift;
  20. my ($line) = @_;
  21. return ($self->a->coincides_with($line->a) && $self->b->coincides_with($line->b))
  22. || ($self->a->coincides_with($line->b) && $self->b->coincides_with($line->a));
  23. }
  24. sub length {
  25. my $self = shift;
  26. return Boost::Geometry::Utils::linestring_length($self);
  27. }
  28. sub vector {
  29. my $self = shift;
  30. return (ref $self)->new([0,0], [map $self->[B][$_] - $self->[A][$_], X,Y]);
  31. }
  32. sub atan {
  33. my $self = shift;
  34. return Slic3r::Geometry::line_atan($self);
  35. }
  36. sub direction {
  37. my $self = shift;
  38. return Slic3r::Geometry::line_direction($self);
  39. }
  40. sub intersection {
  41. my $self = shift;
  42. my ($line, $require_crossing) = @_;
  43. return Slic3r::Geometry::line_intersection($self, $line, $require_crossing);
  44. }
  45. sub point_on_left {
  46. my $self = shift;
  47. my ($point) = @_;
  48. return Slic3r::Geometry::point_is_on_left_of_segment($point, $self);
  49. }
  50. sub midpoint {
  51. my $self = shift;
  52. return Slic3r::Point->new(
  53. ($self->[A][X] + $self->[B][X]) / 2,
  54. ($self->[A][Y] + $self->[B][Y]) / 2,
  55. );
  56. }
  57. sub reverse {
  58. my $self = shift;
  59. @$self = reverse @$self;
  60. }
  61. sub translate {
  62. my $self = shift;
  63. my ($x, $y) = @_;
  64. @$self = Slic3r::Geometry::move_points([$x, $y], @$self);
  65. bless $_, 'Slic3r::Point' for @$self;
  66. return $self;
  67. }
  68. 1;