Line.pm 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package Slic3r::Line;
  2. use Moo;
  3. # arrayref of points
  4. has 'points' => (
  5. is => 'rw',
  6. default => sub { [] },
  7. required => 1,
  8. );
  9. sub cast {
  10. my $class = shift;
  11. my ($line) = @_;
  12. if (ref $line eq 'ARRAY') {
  13. @$line == 2 or die "Line needs two points!";
  14. return Slic3r::Line->new(points => [ map Slic3r::Point->cast($_), @$line ]);
  15. } else {
  16. return $line;
  17. }
  18. }
  19. sub a { return $_[0]->points->[0] }
  20. sub b { return $_[0]->points->[1] }
  21. sub id {
  22. my $self = shift;
  23. return $self->a->id . "-" . $self->b->id;
  24. }
  25. sub coordinates {
  26. my $self = shift;
  27. return ($self->a->coordinates, $self->b->coordinates);
  28. }
  29. sub p {
  30. my $self = shift;
  31. return [ $self->a->p, $self->b->p ];
  32. }
  33. sub coincides_with {
  34. my $self = shift;
  35. my ($line) = @_;
  36. return ($self->a->coincides_with($line->a) && $self->b->coincides_with($line->b))
  37. || ($self->a->coincides_with($line->b) && $self->b->coincides_with($line->a));
  38. }
  39. sub has_endpoint {
  40. my $self = shift;
  41. my ($point) = @_;
  42. return $point->coincides_with($self->a) || $point->coincides_with($self->b);
  43. }
  44. sub parallel_to {
  45. my $self = shift;
  46. my ($line) = @_;
  47. return Slic3r::Geometry::lines_parallel($self->p, $line->p);
  48. }
  49. 1;