diff options
author | Alessandro Ranellucci <aar@cpan.org> | 2011-09-18 21:28:12 +0400 |
---|---|---|
committer | Alessandro Ranellucci <aar@cpan.org> | 2011-09-18 21:28:12 +0400 |
commit | 18c7aef1a74e4be3ffe1160e00d7d19455f4e62e (patch) | |
tree | e7db4ea2eea7e7b30c2b9c387da01454571e9ca3 /lib/Slic3r/Polyline | |
parent | 26b05ab155046c4c04fc326aa6d9d00ffc799270 (diff) |
Lots of changes and refactoring after testing with hollow objects
Diffstat (limited to 'lib/Slic3r/Polyline')
-rw-r--r-- | lib/Slic3r/Polyline/Closed.pm | 69 |
1 files changed, 20 insertions, 49 deletions
diff --git a/lib/Slic3r/Polyline/Closed.pm b/lib/Slic3r/Polyline/Closed.pm index 9c950626b..cc80cee0e 100644 --- a/lib/Slic3r/Polyline/Closed.pm +++ b/lib/Slic3r/Polyline/Closed.pm @@ -3,69 +3,40 @@ use Moo; extends 'Slic3r::Polyline'; -has 'contour_of' => ( - is => 'rw', - #isa => 'Slic3r::Surface', - weak_ref => 1, -); - -has 'hole_of' => ( - is => 'rw', - #isa => 'Slic3r::Surface', - weak_ref => 1, -); - -sub new_from_points { - my $class = shift; - my $polyline = $class->SUPER::new_from_points(@_); +sub lines { + my $self = shift; + my @lines = $self->SUPER::lines(@_); - # polylines must be always closed, otherwise it means that our object is not manifold! - die "Polylines must be closed! Object not manifold?\n" - if ($polyline->lines->[0]->a != $polyline->lines->[-1]->b); + # since this is a closed polyline, we just add a line at the end, + # connecting the last and the first point + push @lines, Slic3r::Line->new(points => [$self->points->[-1], $self->points->[0]]); + return @lines; +} + +# superclass doesn't check whether last line of our closed polyline +# is parallel to first one, so let's do it here +sub merge_continuous_lines { + my $self = shift; + $self->SUPER::merge_continuous_lines(@_); - return $polyline; + my @lines = $self->lines; + if ($lines[-1]->parallel_to($lines[0])) { + shift @{$self->points}; + } } sub encloses_point { my $self = shift; my ($point) = @_; - my @xy = map { $_->x, $_->y } $self->points; #}} - my ($x, $y) = ($point->x, $point->y); #)) - - # Derived from the comp.graphics.algorithms FAQ, - # courtesy of Wm. Randolph Franklin - my $n = @xy / 2; # Number of points in polygon - my @i = map { 2*$_ } 0..(@xy/2); # The even indices of @xy - my @x = map { $xy[$_] } @i; # Even indices: x-coordinates - my @y = map { $xy[$_ + 1] } @i; # Odd indices: y-coordinates - - my ($i, $j); - my $side = 0; # 0 = outside; 1 = inside - for ($i = 0, $j = $n - 1; $i < $n; $j = $i++) { - if ( - # If the y is between the (y-) borders... - ($y[$i] <= $y && $y < $y[$j]) || ($y[$j] <= $y && $y < $y[$i]) - and - # ...the (x,y) to infinity line crosses the edge - # from the ith point to the jth point... - ($x < ($x[$j] - $x[$i]) * ($y - $y[$i]) / ($y[$j] - $y[$i]) + $x[$i]) - ) { - $side = not $side; # Jump the fence - } - } - - return $side; + return Slic3r::Geometry::point_in_polygon($point->p, $self->p); } sub mgp_polygon { my $self = shift; - # we need a list of ordered points - my $points = $self->ordered_points; - my $p = Math::Geometry::Planar->new; - $p->points($points); + $p->points($self->points); return $p; } |