PrusaSlicer-NonPlainar/lib/Slic3r/Line.pm

72 lines
1.6 KiB
Perl
Raw Normal View History

2011-09-01 19:06:28 +00:00
package Slic3r::Line;
use Moo;
2011-09-01 19:06:28 +00:00
# arrayref of points
has 'points' => (
2011-09-01 19:06:28 +00:00
is => 'rw',
default => sub { [] },
required => 1,
2011-09-02 19:10:20 +00:00
);
sub cast {
my $class = shift;
my ($line, %args) = @_;
if (ref $line eq 'ARRAY') {
@$line == 2 or die "Line needs two points!";
return $class->new(points => [ map Slic3r::Point->cast($_), @$line ], %args);
} else {
return $line;
2011-09-01 19:06:28 +00:00
}
}
sub a { return $_[0]->points->[0] }
sub b { return $_[0]->points->[1] }
2011-09-01 19:06:28 +00:00
sub id {
my $self = shift;
return $self->a->id . "-" . $self->b->id;
}
2011-09-05 10:21:27 +00:00
sub coordinates {
my $self = shift;
return ($self->a->coordinates, $self->b->coordinates);
}
sub p {
my $self = shift;
return [ $self->a->p, $self->b->p ];
}
2011-09-01 19:06:28 +00:00
sub coincides_with {
my $self = shift;
my ($line) = @_;
return ($self->a->coincides_with($line->a) && $self->b->coincides_with($line->b))
|| ($self->a->coincides_with($line->b) && $self->b->coincides_with($line->a));
}
2011-09-02 19:10:20 +00:00
sub has_endpoint {
my $self = shift;
2011-09-05 10:21:27 +00:00
my ($point) = @_;
2011-09-02 19:10:20 +00:00
return $point->coincides_with($self->a) || $point->coincides_with($self->b);
}
sub has_segment {
my $self = shift;
my ($line) = @_;
$line = $line->p if $line->isa('Slic3r::Line');
# a segment belongs to another segment if its points belong to it
return Slic3r::Geometry::point_in_segment($line->[0], $self->p)
&& Slic3r::Geometry::point_in_segment($line->[1], $self->p);
}
sub parallel_to {
2011-09-01 19:06:28 +00:00
my $self = shift;
my ($line) = @_;
return Slic3r::Geometry::lines_parallel($self->p, $line->p);
2011-09-01 19:06:28 +00:00
}
1;