2011-09-01 19:06:28 +00:00
|
|
|
package Slic3r::Line;
|
2011-09-06 09:50:43 +00:00
|
|
|
use Moo;
|
2011-09-01 19:06:28 +00:00
|
|
|
|
2011-09-18 17:28:12 +00:00
|
|
|
# arrayref of points
|
|
|
|
has 'points' => (
|
2011-09-01 19:06:28 +00:00
|
|
|
is => 'rw',
|
2011-09-18 17:28:12 +00:00
|
|
|
default => sub { [] },
|
|
|
|
required => 1,
|
2011-09-02 19:10:20 +00:00
|
|
|
);
|
|
|
|
|
2011-09-18 17:28:12 +00:00
|
|
|
sub cast {
|
|
|
|
my $class = shift;
|
2011-10-04 20:27:45 +00:00
|
|
|
my ($line, %args) = @_;
|
2011-09-18 17:28:12 +00:00
|
|
|
if (ref $line eq 'ARRAY') {
|
|
|
|
@$line == 2 or die "Line needs two points!";
|
2011-10-12 12:54:49 +00:00
|
|
|
return $class->new(points => [ map Slic3r::Point->new($_), @$line ], %args);
|
2011-09-18 17:28:12 +00:00
|
|
|
} else {
|
|
|
|
return $line;
|
2011-09-01 19:06:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-18 17:28:12 +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-10-07 17:07:57 +00:00
|
|
|
sub ordered_id {
|
|
|
|
my $self = shift;
|
|
|
|
return join('-', sort map $_->id, @{$self->points});
|
|
|
|
}
|
|
|
|
|
2011-09-05 10:21:27 +00:00
|
|
|
sub coordinates {
|
|
|
|
my $self = shift;
|
|
|
|
return ($self->a->coordinates, $self->b->coordinates);
|
|
|
|
}
|
|
|
|
|
2011-09-18 17:28:12 +00:00
|
|
|
sub p {
|
|
|
|
my $self = shift;
|
2011-10-12 12:54:49 +00:00
|
|
|
return [ $self->a, $self->b ];
|
2011-09-18 17:28:12 +00:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-10-04 20:27:45 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-09-18 17:28:12 +00:00
|
|
|
sub parallel_to {
|
2011-09-01 19:06:28 +00:00
|
|
|
my $self = shift;
|
2011-09-18 17:28:12 +00:00
|
|
|
my ($line) = @_;
|
|
|
|
return Slic3r::Geometry::lines_parallel($self->p, $line->p);
|
2011-09-01 19:06:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|