2011-09-01 19:06:28 +00:00
|
|
|
package Slic3r::Line;
|
2011-10-12 14:27:40 +00:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
2011-09-01 19:06:28 +00:00
|
|
|
|
2011-11-11 21:01:27 +00:00
|
|
|
use Slic3r::Geometry qw(A B X Y);
|
2011-10-20 16:11:59 +00:00
|
|
|
|
2011-10-12 14:27:40 +00:00
|
|
|
sub new {
|
2011-09-18 17:28:12 +00:00
|
|
|
my $class = shift;
|
2011-10-12 14:27:40 +00:00
|
|
|
my $self;
|
|
|
|
if (@_ == 2) {
|
2011-11-13 17:14:02 +00:00
|
|
|
$self = [ @_ ];
|
2011-10-12 14:27:40 +00:00
|
|
|
} elsif (ref $_[0] eq 'ARRAY') {
|
2011-11-13 17:14:02 +00:00
|
|
|
$self = [ $_[0][0], $_[0][1] ];
|
2011-10-12 14:27:40 +00:00
|
|
|
} elsif ($_[0]->isa(__PACKAGE__)) {
|
|
|
|
return $_[0];
|
2011-09-18 17:28:12 +00:00
|
|
|
} else {
|
2011-10-12 14:27:40 +00:00
|
|
|
die "Invalid argument for $class->new";
|
2011-09-01 19:06:28 +00:00
|
|
|
}
|
2011-10-12 14:27:40 +00:00
|
|
|
bless $self, $class;
|
2011-11-13 17:14:02 +00:00
|
|
|
bless $_, 'Slic3r::Point' for @$self;
|
2011-10-12 14:27:40 +00:00
|
|
|
return $self;
|
2011-09-01 19:06:28 +00:00
|
|
|
}
|
|
|
|
|
2011-11-13 17:14:02 +00:00
|
|
|
sub cast {
|
|
|
|
my $class = shift;
|
|
|
|
my ($line) = @_;
|
|
|
|
return $line if ref $line eq __PACKAGE__;
|
|
|
|
return $class->new($line);
|
|
|
|
}
|
|
|
|
|
2011-10-12 14:27:40 +00:00
|
|
|
sub a { $_[0][0] }
|
|
|
|
sub b { $_[0][1] }
|
2011-09-18 17:28:12 +00:00
|
|
|
|
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;
|
2011-10-12 14:27:40 +00:00
|
|
|
return join('-', sort map $_->id, @$self);
|
2011-10-07 17:07:57 +00:00
|
|
|
}
|
|
|
|
|
2011-09-05 10:21:27 +00:00
|
|
|
sub coordinates {
|
|
|
|
my $self = shift;
|
|
|
|
return ($self->a->coordinates, $self->b->coordinates);
|
|
|
|
}
|
|
|
|
|
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) = @_;
|
|
|
|
|
|
|
|
# a segment belongs to another segment if its points belong to it
|
2011-10-12 14:27:40 +00:00
|
|
|
return Slic3r::Geometry::point_in_segment($line->[0], $self)
|
|
|
|
&& Slic3r::Geometry::point_in_segment($line->[1], $self);
|
2011-10-04 20:27:45 +00:00
|
|
|
}
|
|
|
|
|
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) = @_;
|
2011-10-12 14:27:40 +00:00
|
|
|
return Slic3r::Geometry::lines_parallel($self, $line);
|
2011-09-01 19:06:28 +00:00
|
|
|
}
|
|
|
|
|
2011-10-15 09:36:05 +00:00
|
|
|
sub length {
|
|
|
|
my $self = shift;
|
|
|
|
return Slic3r::Geometry::line_length($self);
|
|
|
|
}
|
|
|
|
|
2011-10-20 16:11:59 +00:00
|
|
|
sub atan {
|
|
|
|
my $self = shift;
|
|
|
|
return Slic3r::Geometry::line_atan($self);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub intersection {
|
|
|
|
my $self = shift;
|
|
|
|
my ($line, $require_crossing) = @_;
|
|
|
|
return Slic3r::Geometry::line_intersection($self, $line, $require_crossing);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub point_on_left {
|
|
|
|
my $self = shift;
|
|
|
|
my ($point) = @_;
|
|
|
|
return Slic3r::Geometry::point_is_on_left_of_segment($point, $self);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub midpoint {
|
|
|
|
my $self = shift;
|
|
|
|
return Slic3r::Point->new(
|
|
|
|
($self->[A][X] + $self->[B][X]) / 2,
|
|
|
|
($self->[A][Y] + $self->[B][Y]) / 2,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-11-11 21:01:27 +00:00
|
|
|
sub reverse {
|
|
|
|
my $self = shift;
|
|
|
|
@$self = reverse @$self;
|
|
|
|
}
|
|
|
|
|
2011-09-01 19:06:28 +00:00
|
|
|
1;
|