2011-09-18 17:28:12 +00:00
|
|
|
package Slic3r::Geometry;
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
2011-10-07 19:19:18 +00:00
|
|
|
require Exporter;
|
|
|
|
our @ISA = qw(Exporter);
|
|
|
|
our @EXPORT_OK = qw(
|
2014-03-04 22:33:13 +00:00
|
|
|
PI X Y Z A B X1 Y1 X2 Y2 Z1 Z2 MIN MAX epsilon slope
|
2011-10-07 19:19:18 +00:00
|
|
|
line_point_belongs_to_segment points_coincide distance_between_points
|
2013-11-23 20:47:33 +00:00
|
|
|
normalize tan move_points_3D
|
2013-11-06 20:09:28 +00:00
|
|
|
point_in_polygon point_in_segment segment_in_segment
|
2013-11-22 20:43:35 +00:00
|
|
|
polyline_lines polygon_lines
|
2011-10-07 19:19:18 +00:00
|
|
|
point_along_segment polygon_segment_having_point polygon_has_subsegment
|
2013-11-21 13:15:38 +00:00
|
|
|
deg2rad rad2deg
|
|
|
|
rotate_points move_points
|
|
|
|
dot perp polygon_points_visibility
|
2013-11-06 22:08:03 +00:00
|
|
|
line_intersection bounding_box bounding_box_intersect
|
2014-03-04 22:33:13 +00:00
|
|
|
angle3points
|
2013-11-23 23:59:45 +00:00
|
|
|
chained_path chained_path_from collinear scale unscale
|
2012-02-25 13:46:21 +00:00
|
|
|
rad2deg_dir bounding_box_center line_intersects_any douglas_peucker
|
2012-02-25 15:14:59 +00:00
|
|
|
polyline_remove_short_segments normal triangle_normal polygon_is_convex
|
2012-11-19 16:39:16 +00:00
|
|
|
scaled_epsilon bounding_box_3D size_3D size_2D
|
2014-05-02 16:46:22 +00:00
|
|
|
convex_hull directions_parallel directions_parallel_within
|
2011-10-07 19:19:18 +00:00
|
|
|
);
|
|
|
|
|
2011-09-18 17:28:12 +00:00
|
|
|
|
2011-09-26 13:51:22 +00:00
|
|
|
use constant PI => 4 * atan2(1, 1);
|
2011-09-18 17:28:12 +00:00
|
|
|
use constant A => 0;
|
|
|
|
use constant B => 1;
|
|
|
|
use constant X => 0;
|
|
|
|
use constant Y => 1;
|
2011-11-11 21:01:27 +00:00
|
|
|
use constant Z => 2;
|
2011-11-13 17:14:02 +00:00
|
|
|
use constant X1 => 0;
|
|
|
|
use constant Y1 => 1;
|
|
|
|
use constant X2 => 2;
|
|
|
|
use constant Y2 => 3;
|
2013-09-11 09:55:08 +00:00
|
|
|
use constant Z1 => 4;
|
|
|
|
use constant Z2 => 5;
|
2011-12-03 17:31:31 +00:00
|
|
|
use constant MIN => 0;
|
|
|
|
use constant MAX => 1;
|
2012-09-27 13:14:54 +00:00
|
|
|
our $parallel_degrees_limit = abs(deg2rad(0.1));
|
2011-09-18 17:28:12 +00:00
|
|
|
|
2012-02-25 16:16:55 +00:00
|
|
|
sub epsilon () { 1E-4 }
|
2012-09-12 13:19:47 +00:00
|
|
|
sub scaled_epsilon () { epsilon / &Slic3r::SCALING_FACTOR }
|
2011-10-07 17:07:57 +00:00
|
|
|
|
2012-07-27 19:13:03 +00:00
|
|
|
sub scale ($) { $_[0] / &Slic3r::SCALING_FACTOR }
|
|
|
|
sub unscale ($) { $_[0] * &Slic3r::SCALING_FACTOR }
|
2011-11-27 10:40:03 +00:00
|
|
|
|
2013-05-17 12:14:33 +00:00
|
|
|
sub tan {
|
|
|
|
my ($angle) = @_;
|
|
|
|
return (sin $angle) / (cos $angle);
|
|
|
|
}
|
|
|
|
|
2011-09-18 17:28:12 +00:00
|
|
|
sub slope {
|
|
|
|
my ($line) = @_;
|
|
|
|
return undef if abs($line->[B][X] - $line->[A][X]) < epsilon; # line is vertical
|
|
|
|
return ($line->[B][Y] - $line->[A][Y]) / ($line->[B][X] - $line->[A][X]);
|
|
|
|
}
|
|
|
|
|
|
|
|
# this subroutine checks whether a given point may belong to a given
|
|
|
|
# segment given the hypothesis that it belongs to the line containing
|
|
|
|
# the segment
|
|
|
|
sub line_point_belongs_to_segment {
|
|
|
|
my ($point, $segment) = @_;
|
|
|
|
|
|
|
|
#printf " checking whether %f,%f may belong to segment %f,%f - %f,%f\n",
|
|
|
|
# @$point, map @$_, @$segment;
|
|
|
|
|
|
|
|
my @segment_extents = (
|
|
|
|
[ sort { $a <=> $b } map $_->[X], @$segment ],
|
|
|
|
[ sort { $a <=> $b } map $_->[Y], @$segment ],
|
|
|
|
);
|
|
|
|
|
|
|
|
return 0 if $point->[X] < ($segment_extents[X][0] - epsilon) || $point->[X] > ($segment_extents[X][1] + epsilon);
|
|
|
|
return 0 if $point->[Y] < ($segment_extents[Y][0] - epsilon) || $point->[Y] > ($segment_extents[Y][1] + epsilon);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub points_coincide {
|
|
|
|
my ($p1, $p2) = @_;
|
|
|
|
return 1 if abs($p2->[X] - $p1->[X]) < epsilon && abs($p2->[Y] - $p1->[Y]) < epsilon;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub distance_between_points {
|
|
|
|
my ($p1, $p2) = @_;
|
2011-10-05 19:25:17 +00:00
|
|
|
return sqrt((($p1->[X] - $p2->[X])**2) + ($p1->[Y] - $p2->[Y])**2);
|
2011-09-18 17:28:12 +00:00
|
|
|
}
|
|
|
|
|
2011-11-13 17:14:02 +00:00
|
|
|
# this will check whether a point is in a polygon regardless of polygon orientation
|
2011-09-18 17:28:12 +00:00
|
|
|
sub point_in_polygon {
|
|
|
|
my ($point, $polygon) = @_;
|
|
|
|
|
|
|
|
my ($x, $y) = @$point;
|
2012-06-16 07:24:27 +00:00
|
|
|
my $n = @$polygon;
|
2012-06-17 17:09:11 +00:00
|
|
|
my @x = map $_->[X], @$polygon;
|
|
|
|
my @y = map $_->[Y], @$polygon;
|
2011-09-18 17:28:12 +00:00
|
|
|
|
|
|
|
# Derived from the comp.graphics.algorithms FAQ,
|
|
|
|
# courtesy of Wm. Randolph Franklin
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# if point is not in polygon, let's check whether it belongs to the contour
|
2011-09-26 12:48:22 +00:00
|
|
|
if (!$side && 0) {
|
2011-10-06 10:20:25 +00:00
|
|
|
return 1 if polygon_segment_having_point($polygon, $point);
|
2011-09-18 17:28:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $side;
|
|
|
|
}
|
|
|
|
|
2011-10-04 20:27:45 +00:00
|
|
|
sub point_in_segment {
|
|
|
|
my ($point, $line) = @_;
|
|
|
|
|
|
|
|
my ($x, $y) = @$point;
|
2013-07-16 15:13:01 +00:00
|
|
|
my $line_p = $line->pp;
|
|
|
|
my @line_x = sort { $a <=> $b } $line_p->[A][X], $line_p->[B][X];
|
|
|
|
my @line_y = sort { $a <=> $b } $line_p->[A][Y], $line_p->[B][Y];
|
2011-10-04 20:27:45 +00:00
|
|
|
|
|
|
|
# check whether the point is in the segment bounding box
|
|
|
|
return 0 unless $x >= ($line_x[0] - epsilon) && $x <= ($line_x[1] + epsilon)
|
|
|
|
&& $y >= ($line_y[0] - epsilon) && $y <= ($line_y[1] + epsilon);
|
|
|
|
|
|
|
|
# if line is vertical, check whether point's X is the same as the line
|
2013-07-16 15:13:01 +00:00
|
|
|
if ($line_p->[A][X] == $line_p->[B][X]) {
|
|
|
|
return abs($x - $line_p->[A][X]) < epsilon ? 1 : 0;
|
2011-10-04 20:27:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# calculate the Y in line at X of the point
|
2013-07-16 15:13:01 +00:00
|
|
|
my $y3 = $line_p->[A][Y] + ($line_p->[B][Y] - $line_p->[A][Y])
|
|
|
|
* ($x - $line_p->[A][X]) / ($line_p->[B][X] - $line_p->[A][X]);
|
2011-10-04 20:27:45 +00:00
|
|
|
return abs($y3 - $y) < epsilon ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2011-10-07 17:07:57 +00:00
|
|
|
sub segment_in_segment {
|
|
|
|
my ($needle, $haystack) = @_;
|
|
|
|
|
|
|
|
# a segment is contained in another segment if its endpoints are contained
|
|
|
|
return point_in_segment($needle->[A], $haystack) && point_in_segment($needle->[B], $haystack);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub polyline_lines {
|
2013-07-15 10:14:22 +00:00
|
|
|
my ($polyline) = @_;
|
|
|
|
my @points = @$polyline;
|
|
|
|
return map Slic3r::Line->new(@points[$_, $_+1]), 0 .. $#points-1;
|
2011-09-18 17:28:12 +00:00
|
|
|
}
|
|
|
|
|
2011-10-07 17:07:57 +00:00
|
|
|
sub polygon_lines {
|
2012-05-19 19:03:24 +00:00
|
|
|
my ($polygon) = @_;
|
2011-10-07 17:07:57 +00:00
|
|
|
return polyline_lines([ @$polygon, $polygon->[0] ]);
|
|
|
|
}
|
|
|
|
|
2011-10-06 09:55:26 +00:00
|
|
|
# given a segment $p1-$p2, get the point at $distance from $p1 along segment
|
2011-09-25 21:15:45 +00:00
|
|
|
sub point_along_segment {
|
|
|
|
my ($p1, $p2, $distance) = @_;
|
|
|
|
|
|
|
|
my $point = [ @$p1 ];
|
|
|
|
|
|
|
|
my $line_length = sqrt( (($p2->[X] - $p1->[X])**2) + (($p2->[Y] - $p1->[Y])**2) );
|
|
|
|
for (X, Y) {
|
|
|
|
if ($p1->[$_] != $p2->[$_]) {
|
|
|
|
$point->[$_] = $p1->[$_] + ($p2->[$_] - $p1->[$_]) * $distance / $line_length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-16 15:13:01 +00:00
|
|
|
return Slic3r::Point->new(@$point);
|
2011-09-25 21:15:45 +00:00
|
|
|
}
|
|
|
|
|
2011-10-06 09:55:26 +00:00
|
|
|
# given a $polygon, return the (first) segment having $point
|
|
|
|
sub polygon_segment_having_point {
|
|
|
|
my ($polygon, $point) = @_;
|
|
|
|
|
2013-07-16 15:13:01 +00:00
|
|
|
foreach my $line (@{ $polygon->lines }) {
|
2011-10-06 09:55:26 +00:00
|
|
|
return $line if point_in_segment($point, $line);
|
|
|
|
}
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
2011-10-07 17:07:57 +00:00
|
|
|
# return true if the given segment is contained in any edge of the polygon
|
|
|
|
sub polygon_has_subsegment {
|
|
|
|
my ($polygon, $segment) = @_;
|
|
|
|
foreach my $line (polygon_lines($polygon)) {
|
|
|
|
return 1 if segment_in_segment($segment, $line);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-25 15:23:41 +00:00
|
|
|
# polygon must be simple (non complex) and ccw
|
2012-02-25 15:14:59 +00:00
|
|
|
sub polygon_is_convex {
|
|
|
|
my ($points) = @_;
|
|
|
|
for (my $i = 0; $i <= $#$points; $i++) {
|
|
|
|
my $angle = angle3points($points->[$i-1], $points->[$i-2], $points->[$i]);
|
|
|
|
return 0 if $angle < PI;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-09-26 13:51:22 +00:00
|
|
|
sub deg2rad {
|
|
|
|
my ($degrees) = @_;
|
|
|
|
return PI() * $degrees / 180;
|
|
|
|
}
|
|
|
|
|
2011-10-07 17:07:57 +00:00
|
|
|
sub rad2deg {
|
|
|
|
my ($rad) = @_;
|
|
|
|
return $rad / PI() * 180;
|
|
|
|
}
|
|
|
|
|
2011-12-02 22:35:39 +00:00
|
|
|
sub rad2deg_dir {
|
|
|
|
my ($rad) = @_;
|
|
|
|
$rad = ($rad < PI) ? (-$rad + PI/2) : ($rad + PI/2);
|
|
|
|
$rad += PI if $rad < 0;
|
|
|
|
return rad2deg($rad);
|
|
|
|
}
|
|
|
|
|
2011-09-26 13:51:22 +00:00
|
|
|
sub rotate_points {
|
|
|
|
my ($radians, $center, @points) = @_;
|
2013-07-11 14:48:44 +00:00
|
|
|
$center //= [0,0];
|
2011-09-26 13:51:22 +00:00
|
|
|
return map {
|
|
|
|
[
|
|
|
|
$center->[X] + cos($radians) * ($_->[X] - $center->[X]) - sin($radians) * ($_->[Y] - $center->[Y]),
|
|
|
|
$center->[Y] + cos($radians) * ($_->[Y] - $center->[Y]) + sin($radians) * ($_->[X] - $center->[X]),
|
|
|
|
]
|
|
|
|
} @points;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub move_points {
|
|
|
|
my ($shift, @points) = @_;
|
2013-07-15 14:04:49 +00:00
|
|
|
return map {
|
|
|
|
my @p = @$_;
|
|
|
|
Slic3r::Point->new($shift->[X] + $p[X], $shift->[Y] + $p[Y]);
|
|
|
|
} @points;
|
2011-09-26 13:51:22 +00:00
|
|
|
}
|
|
|
|
|
2013-05-18 14:48:26 +00:00
|
|
|
sub move_points_3D {
|
|
|
|
my ($shift, @points) = @_;
|
|
|
|
return map [
|
|
|
|
$shift->[X] + $_->[X],
|
|
|
|
$shift->[Y] + $_->[Y],
|
|
|
|
$shift->[Z] + $_->[Z],
|
|
|
|
], @points;
|
|
|
|
}
|
|
|
|
|
2012-02-19 14:45:27 +00:00
|
|
|
sub normal {
|
|
|
|
my ($line1, $line2) = @_;
|
|
|
|
|
|
|
|
return [
|
|
|
|
($line1->[Y] * $line2->[Z]) - ($line1->[Z] * $line2->[Y]),
|
|
|
|
-($line2->[Z] * $line1->[X]) + ($line2->[X] * $line1->[Z]),
|
|
|
|
($line1->[X] * $line2->[Y]) - ($line1->[Y] * $line2->[X]),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
sub triangle_normal {
|
|
|
|
my ($v1, $v2, $v3) = @_;
|
|
|
|
|
|
|
|
my $u = [ map +($v2->[$_] - $v1->[$_]), (X,Y,Z) ];
|
|
|
|
my $v = [ map +($v3->[$_] - $v1->[$_]), (X,Y,Z) ];
|
|
|
|
|
|
|
|
return normal($u, $v);
|
|
|
|
}
|
|
|
|
|
2013-05-17 12:14:33 +00:00
|
|
|
sub normalize {
|
|
|
|
my ($line) = @_;
|
|
|
|
|
|
|
|
my $len = sqrt( ($line->[X]**2) + ($line->[Y]**2) + ($line->[Z]**2) )
|
|
|
|
or return [0, 0, 0]; # to avoid illegal division by zero
|
|
|
|
return [ map $_ / $len, @$line ];
|
|
|
|
}
|
|
|
|
|
2011-10-04 18:06:17 +00:00
|
|
|
# 2D dot product
|
|
|
|
sub dot {
|
|
|
|
my ($u, $v) = @_;
|
|
|
|
return $u->[X] * $v->[X] + $u->[Y] * $v->[Y];
|
|
|
|
}
|
|
|
|
|
|
|
|
# 2D perp product
|
|
|
|
sub perp {
|
|
|
|
my ($u, $v) = @_;
|
|
|
|
return $u->[X] * $v->[Y] - $u->[Y] * $v->[X];
|
|
|
|
}
|
|
|
|
|
2011-10-06 09:55:26 +00:00
|
|
|
sub polygon_points_visibility {
|
|
|
|
my ($polygon, $p1, $p2) = @_;
|
|
|
|
|
|
|
|
my $our_line = [ $p1, $p2 ];
|
|
|
|
foreach my $line (polygon_lines($polygon)) {
|
2013-07-15 18:31:43 +00:00
|
|
|
my $intersection = line_intersection($our_line, $line, 1) // next;
|
2011-10-06 09:55:26 +00:00
|
|
|
next if grep points_coincide($intersection, $_), $p1, $p2;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-12-30 16:17:37 +00:00
|
|
|
sub line_intersects_any {
|
|
|
|
my ($line, $lines) = @_;
|
|
|
|
for (@$lines) {
|
|
|
|
return 1 if line_intersection($line, $_, 1);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-10-06 09:55:26 +00:00
|
|
|
sub line_intersection {
|
|
|
|
my ($line1, $line2, $require_crossing) = @_;
|
|
|
|
$require_crossing ||= 0;
|
|
|
|
|
|
|
|
my $intersection = _line_intersection(map @$_, @$line1, @$line2);
|
|
|
|
return (ref $intersection && $intersection->[1] == $require_crossing)
|
|
|
|
? $intersection->[0]
|
|
|
|
: undef;
|
|
|
|
}
|
|
|
|
|
2011-11-11 21:01:27 +00:00
|
|
|
sub collinear {
|
|
|
|
my ($line1, $line2, $require_overlapping) = @_;
|
|
|
|
my $intersection = _line_intersection(map @$_, @$line1, @$line2);
|
|
|
|
return 0 unless !ref($intersection)
|
|
|
|
&& ($intersection eq 'parallel collinear'
|
|
|
|
|| ($intersection eq 'parallel vertical' && abs($line1->[A][X] - $line2->[A][X]) < epsilon));
|
|
|
|
|
|
|
|
if ($require_overlapping) {
|
|
|
|
my @box_a = bounding_box([ $line1->[0], $line1->[1] ]);
|
|
|
|
my @box_b = bounding_box([ $line2->[0], $line2->[1] ]);
|
|
|
|
return 0 unless bounding_box_intersect( 2, @box_a, @box_b );
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-10-06 09:55:26 +00:00
|
|
|
sub _line_intersection {
|
2012-02-25 16:16:55 +00:00
|
|
|
my ( $x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3 ) = @_;
|
2011-10-06 09:55:26 +00:00
|
|
|
|
|
|
|
my ($x, $y); # The as-yet-undetermined intersection point.
|
|
|
|
|
|
|
|
my $dy10 = $y1 - $y0; # dyPQ, dxPQ are the coordinate differences
|
|
|
|
my $dx10 = $x1 - $x0; # between the points P and Q.
|
|
|
|
my $dy32 = $y3 - $y2;
|
|
|
|
my $dx32 = $x3 - $x2;
|
|
|
|
|
|
|
|
my $dy10z = abs( $dy10 ) < epsilon; # Is the difference $dy10 "zero"?
|
|
|
|
my $dx10z = abs( $dx10 ) < epsilon;
|
|
|
|
my $dy32z = abs( $dy32 ) < epsilon;
|
|
|
|
my $dx32z = abs( $dx32 ) < epsilon;
|
|
|
|
|
|
|
|
my $dyx10; # The slopes.
|
|
|
|
my $dyx32;
|
2012-02-26 10:18:42 +00:00
|
|
|
|
2011-10-06 09:55:26 +00:00
|
|
|
$dyx10 = $dy10 / $dx10 unless $dx10z;
|
|
|
|
$dyx32 = $dy32 / $dx32 unless $dx32z;
|
|
|
|
|
|
|
|
# Now we know all differences and the slopes;
|
|
|
|
# we can detect horizontal/vertical special cases.
|
|
|
|
# E.g., slope = 0 means a horizontal line.
|
|
|
|
|
|
|
|
unless ( defined $dyx10 or defined $dyx32 ) {
|
|
|
|
return "parallel vertical";
|
|
|
|
}
|
|
|
|
elsif ( $dy10z and not $dy32z ) { # First line horizontal.
|
|
|
|
$y = $y0;
|
|
|
|
$x = $x2 + ( $y - $y2 ) * $dx32 / $dy32;
|
|
|
|
}
|
|
|
|
elsif ( not $dy10z and $dy32z ) { # Second line horizontal.
|
|
|
|
$y = $y2;
|
|
|
|
$x = $x0 + ( $y - $y0 ) * $dx10 / $dy10;
|
|
|
|
}
|
|
|
|
elsif ( $dx10z and not $dx32z ) { # First line vertical.
|
|
|
|
$x = $x0;
|
|
|
|
$y = $y2 + $dyx32 * ( $x - $x2 );
|
|
|
|
}
|
|
|
|
elsif ( not $dx10z and $dx32z ) { # Second line vertical.
|
|
|
|
$x = $x2;
|
|
|
|
$y = $y0 + $dyx10 * ( $x - $x0 );
|
|
|
|
}
|
|
|
|
elsif ( abs( $dyx10 - $dyx32 ) < epsilon ) {
|
|
|
|
# The slopes are suspiciously close to each other.
|
|
|
|
# Either we have parallel collinear or just parallel lines.
|
|
|
|
|
|
|
|
# The bounding box checks have already weeded the cases
|
|
|
|
# "parallel horizontal" and "parallel vertical" away.
|
|
|
|
|
|
|
|
my $ya = $y0 - $dyx10 * $x0;
|
|
|
|
my $yb = $y2 - $dyx32 * $x2;
|
2011-11-11 21:01:27 +00:00
|
|
|
|
2011-10-06 09:55:26 +00:00
|
|
|
return "parallel collinear" if abs( $ya - $yb ) < epsilon;
|
|
|
|
return "parallel";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
# None of the special cases matched.
|
|
|
|
# We have a "honest" line intersection.
|
|
|
|
|
|
|
|
$x = ($y2 - $y0 + $dyx10*$x0 - $dyx32*$x2)/($dyx10 - $dyx32);
|
|
|
|
$y = $y0 + $dyx10 * ($x - $x0);
|
|
|
|
}
|
|
|
|
|
|
|
|
my $h10 = $dx10 ? ($x - $x0) / $dx10 : ($dy10 ? ($y - $y0) / $dy10 : 1);
|
|
|
|
my $h32 = $dx32 ? ($x - $x2) / $dx32 : ($dy32 ? ($y - $y2) / $dy32 : 1);
|
|
|
|
|
2011-10-20 16:11:59 +00:00
|
|
|
return [Slic3r::Point->new($x, $y), $h10 >= 0 && $h10 <= 1 && $h32 >= 0 && $h32 <= 1];
|
2011-10-06 09:55:26 +00:00
|
|
|
}
|
|
|
|
|
2012-02-26 10:18:42 +00:00
|
|
|
# http://paulbourke.net/geometry/lineline2d/
|
|
|
|
sub _line_intersection2 {
|
|
|
|
my ($line1, $line2) = @_;
|
|
|
|
|
|
|
|
my $denom = ($line2->[B][Y] - $line2->[A][Y]) * ($line1->[B][X] - $line1->[A][X])
|
|
|
|
- ($line2->[B][X] - $line2->[A][X]) * ($line1->[B][Y] - $line1->[A][Y]);
|
|
|
|
my $numerA = ($line2->[B][X] - $line2->[A][X]) * ($line1->[A][Y] - $line2->[A][Y])
|
|
|
|
- ($line2->[B][Y] - $line2->[A][Y]) * ($line1->[A][X] - $line2->[A][X]);
|
|
|
|
my $numerB = ($line1->[B][X] - $line1->[A][X]) * ($line1->[A][Y] - $line2->[A][Y])
|
|
|
|
- ($line1->[B][Y] - $line1->[A][Y]) * ($line1->[A][X] - $line2->[A][X]);
|
|
|
|
|
|
|
|
# are the lines coincident?
|
|
|
|
if (abs($numerA) < epsilon && abs($numerB) < epsilon && abs($denom) < epsilon) {
|
|
|
|
return Slic3r::Point->new(
|
|
|
|
($line1->[A][X] + $line1->[B][X]) / 2,
|
|
|
|
($line1->[A][Y] + $line1->[B][Y]) / 2,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
# are the lines parallel?
|
|
|
|
if (abs($denom) < epsilon) {
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
# is the intersection along the segments?
|
|
|
|
my $muA = $numerA / $denom;
|
|
|
|
my $muB = $numerB / $denom;
|
|
|
|
if ($muA < 0 || $muA > 1 || $muB < 0 || $muB > 1) {
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Slic3r::Point->new(
|
|
|
|
$line1->[A][X] + $muA * ($line1->[B][X] - $line1->[A][X]),
|
|
|
|
$line1->[A][Y] + $muA * ($line1->[B][Y] - $line1->[A][Y]),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-10-06 09:55:26 +00:00
|
|
|
# 2D
|
|
|
|
sub bounding_box {
|
|
|
|
my ($points) = @_;
|
2013-09-06 14:46:28 +00:00
|
|
|
|
2013-08-26 21:27:51 +00:00
|
|
|
my @x = map $_->x, @$points;
|
|
|
|
my @y = map $_->y, @$points; #,,
|
|
|
|
my @bb = (undef, undef, undef, undef);
|
|
|
|
for (0..$#x) {
|
|
|
|
$bb[X1] = $x[$_] if !defined $bb[X1] || $x[$_] < $bb[X1];
|
|
|
|
$bb[X2] = $x[$_] if !defined $bb[X2] || $x[$_] > $bb[X2];
|
|
|
|
$bb[Y1] = $y[$_] if !defined $bb[Y1] || $y[$_] < $bb[Y1];
|
|
|
|
$bb[Y2] = $y[$_] if !defined $bb[Y2] || $y[$_] > $bb[Y2];
|
2012-02-25 16:16:55 +00:00
|
|
|
}
|
2011-10-06 09:55:26 +00:00
|
|
|
|
2013-08-26 21:27:51 +00:00
|
|
|
return @bb[X1,Y1,X2,Y2];
|
2011-10-06 09:55:26 +00:00
|
|
|
}
|
|
|
|
|
2011-12-03 17:31:31 +00:00
|
|
|
sub bounding_box_center {
|
2013-04-18 15:34:21 +00:00
|
|
|
my ($bounding_box) = @_;
|
2011-12-03 17:31:31 +00:00
|
|
|
return Slic3r::Point->new(
|
2013-04-18 15:34:21 +00:00
|
|
|
($bounding_box->[X2] + $bounding_box->[X1]) / 2,
|
|
|
|
($bounding_box->[Y2] + $bounding_box->[Y1]) / 2,
|
2011-12-03 17:31:31 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-11-19 16:39:16 +00:00
|
|
|
sub size_2D {
|
|
|
|
my @bounding_box = bounding_box(@_);
|
|
|
|
return (
|
|
|
|
($bounding_box[X2] - $bounding_box[X1]),
|
|
|
|
($bounding_box[Y2] - $bounding_box[Y1]),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-10-06 09:55:26 +00:00
|
|
|
# bounding_box_intersect($d, @a, @b)
|
|
|
|
# Return true if the given bounding boxes @a and @b intersect
|
|
|
|
# in $d dimensions. Used by line_intersection().
|
|
|
|
sub bounding_box_intersect {
|
|
|
|
my ( $d, @bb ) = @_; # Number of dimensions and box coordinates.
|
|
|
|
my @aa = splice( @bb, 0, 2 * $d ); # The first box.
|
|
|
|
# (@bb is the second one.)
|
|
|
|
|
|
|
|
# Must intersect in all dimensions.
|
|
|
|
for ( my $i_min = 0; $i_min < $d; $i_min++ ) {
|
|
|
|
my $i_max = $i_min + $d; # The index for the maximum.
|
|
|
|
return 0 if ( $aa[ $i_max ] + epsilon ) < $bb[ $i_min ];
|
|
|
|
return 0 if ( $bb[ $i_max ] + epsilon ) < $aa[ $i_min ];
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-09-22 14:10:24 +00:00
|
|
|
# 3D
|
|
|
|
sub bounding_box_3D {
|
|
|
|
my ($points) = @_;
|
|
|
|
|
|
|
|
my @extents = (map [undef, undef], X,Y,Z);
|
|
|
|
foreach my $point (@$points) {
|
|
|
|
for (X,Y,Z) {
|
|
|
|
$extents[$_][MIN] = $point->[$_] if !defined $extents[$_][MIN] || $point->[$_] < $extents[$_][MIN];
|
|
|
|
$extents[$_][MAX] = $point->[$_] if !defined $extents[$_][MAX] || $point->[$_] > $extents[$_][MAX];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return @extents;
|
|
|
|
}
|
|
|
|
|
2012-09-22 14:12:54 +00:00
|
|
|
sub size_3D {
|
|
|
|
my ($points) = @_;
|
|
|
|
|
|
|
|
my @extents = bounding_box_3D($points);
|
|
|
|
return map $extents[$_][MAX] - $extents[$_][MIN], (X,Y,Z);
|
|
|
|
}
|
|
|
|
|
2012-12-05 10:31:35 +00:00
|
|
|
# this assumes a CCW rotation from $p2 to $p3 around $p1
|
2011-10-08 17:02:05 +00:00
|
|
|
sub angle3points {
|
|
|
|
my ($p1, $p2, $p3) = @_;
|
|
|
|
# p1 is the center
|
|
|
|
|
|
|
|
my $angle = atan2($p2->[X] - $p1->[X], $p2->[Y] - $p1->[Y])
|
|
|
|
- atan2($p3->[X] - $p1->[X], $p3->[Y] - $p1->[Y]);
|
|
|
|
|
|
|
|
# we only want to return only positive angles
|
|
|
|
return $angle <= 0 ? $angle + 2*PI() : $angle;
|
|
|
|
}
|
|
|
|
|
2011-12-30 16:17:37 +00:00
|
|
|
sub polyline_remove_short_segments {
|
|
|
|
my ($points, $min_length, $isPolygon) = @_;
|
|
|
|
for (my $i = $isPolygon ? 0 : 1; $i < $#$points; $i++) {
|
|
|
|
if (distance_between_points($points->[$i-1], $points->[$i]) < $min_length) {
|
|
|
|
# we can remove $points->[$i]
|
|
|
|
splice @$points, $i, 1;
|
|
|
|
$i--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-25 13:46:21 +00:00
|
|
|
sub douglas_peucker {
|
|
|
|
my ($points, $tolerance) = @_;
|
2012-06-24 14:37:59 +00:00
|
|
|
no warnings "recursion";
|
2012-02-25 13:46:21 +00:00
|
|
|
|
|
|
|
my $results = [];
|
|
|
|
my $dmax = 0;
|
|
|
|
my $index = 0;
|
|
|
|
for my $i (1..$#$points) {
|
2013-11-06 22:08:03 +00:00
|
|
|
my $d = $points->[$i]->distance_to(Slic3r::Line->new($points->[0], $points->[-1]));
|
2012-02-25 13:46:21 +00:00
|
|
|
if ($d > $dmax) {
|
|
|
|
$index = $i;
|
|
|
|
$dmax = $d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($dmax >= $tolerance) {
|
|
|
|
my $dp1 = douglas_peucker([ @$points[0..$index] ], $tolerance);
|
|
|
|
$results = [
|
|
|
|
@$dp1[0..($#$dp1-1)],
|
|
|
|
@{douglas_peucker([ @$points[$index..$#$points] ], $tolerance)},
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
$results = [ $points->[0], $points->[-1] ];
|
|
|
|
}
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub douglas_peucker2 {
|
|
|
|
my ($points, $tolerance) = @_;
|
|
|
|
|
|
|
|
my $anchor = 0;
|
|
|
|
my $floater = $#$points;
|
|
|
|
my @stack = ();
|
|
|
|
my %keep = ();
|
|
|
|
|
|
|
|
push @stack, [$anchor, $floater];
|
|
|
|
while (@stack) {
|
|
|
|
($anchor, $floater) = @{pop @stack};
|
|
|
|
|
|
|
|
# initialize line segment
|
|
|
|
my ($anchor_x, $anchor_y, $seg_len);
|
|
|
|
if (grep $points->[$floater][$_] != $points->[$anchor][$_], X, Y) {
|
|
|
|
$anchor_x = $points->[$floater][X] - $points->[$anchor][X];
|
|
|
|
$anchor_y = $points->[$floater][Y] - $points->[$anchor][Y];
|
|
|
|
$seg_len = sqrt(($anchor_x ** 2) + ($anchor_y ** 2));
|
|
|
|
# get the unit vector
|
|
|
|
$anchor_x /= $seg_len;
|
|
|
|
$anchor_y /= $seg_len;
|
|
|
|
} else {
|
|
|
|
$anchor_x = $anchor_y = $seg_len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
# inner loop:
|
|
|
|
my $max_dist = 0;
|
|
|
|
my $farthest = $anchor + 1;
|
|
|
|
for my $i (($anchor + 1) .. $floater) {
|
|
|
|
my $dist_to_seg = 0;
|
|
|
|
# compare to anchor
|
|
|
|
my $vecX = $points->[$i][X] - $points->[$anchor][X];
|
|
|
|
my $vecY = $points->[$i][Y] - $points->[$anchor][Y];
|
|
|
|
$seg_len = sqrt(($vecX ** 2) + ($vecY ** 2));
|
|
|
|
# dot product:
|
|
|
|
my $proj = $vecX * $anchor_x + $vecY * $anchor_y;
|
|
|
|
if ($proj < 0) {
|
|
|
|
$dist_to_seg = $seg_len;
|
|
|
|
} else {
|
|
|
|
# compare to floater
|
|
|
|
$vecX = $points->[$i][X] - $points->[$floater][X];
|
|
|
|
$vecY = $points->[$i][Y] - $points->[$floater][Y];
|
|
|
|
$seg_len = sqrt(($vecX ** 2) + ($vecY ** 2));
|
|
|
|
# dot product:
|
|
|
|
$proj = $vecX * (-$anchor_x) + $vecY * (-$anchor_y);
|
|
|
|
if ($proj < 0) {
|
|
|
|
$dist_to_seg = $seg_len
|
|
|
|
} else { # calculate perpendicular distance to line (pythagorean theorem):
|
|
|
|
$dist_to_seg = sqrt(abs(($seg_len ** 2) - ($proj ** 2)));
|
|
|
|
}
|
|
|
|
if ($max_dist < $dist_to_seg) {
|
|
|
|
$max_dist = $dist_to_seg;
|
|
|
|
$farthest = $i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($max_dist <= $tolerance) { # use line segment
|
|
|
|
$keep{$_} = 1 for $anchor, $floater;
|
|
|
|
} else {
|
|
|
|
push @stack, [$anchor, $farthest];
|
|
|
|
push @stack, [$farthest, $floater];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [ map $points->[$_], sort keys %keep ];
|
|
|
|
}
|
|
|
|
|
2012-04-30 12:56:01 +00:00
|
|
|
sub arrange {
|
2013-12-23 23:30:51 +00:00
|
|
|
my ($total_parts, $partx, $party, $dist, $bb) = @_;
|
2012-04-30 12:56:01 +00:00
|
|
|
|
|
|
|
my $linint = sub {
|
|
|
|
my ($value, $oldmin, $oldmax, $newmin, $newmax) = @_;
|
|
|
|
return ($value - $oldmin) * ($newmax - $newmin) / ($oldmax - $oldmin) + $newmin;
|
|
|
|
};
|
|
|
|
|
|
|
|
# use actual part size (the largest) plus separation distance (half on each side) in spacing algorithm
|
|
|
|
$partx += $dist;
|
|
|
|
$party += $dist;
|
|
|
|
|
2013-12-23 23:30:51 +00:00
|
|
|
my ($areax, $areay);
|
|
|
|
if (defined $bb) {
|
|
|
|
my $size = $bb->size;
|
|
|
|
($areax, $areay) = @$size[X,Y];
|
2012-04-30 12:56:01 +00:00
|
|
|
} else {
|
2013-12-23 23:30:51 +00:00
|
|
|
# bogus area size, large enough not to trigger the error below
|
|
|
|
$areax = $partx * $total_parts;
|
|
|
|
$areay = $party * $total_parts;
|
2012-04-30 12:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# this is how many cells we have available into which to put parts
|
2013-12-23 23:30:51 +00:00
|
|
|
my $cellw = int(($areax + $dist) / $partx);
|
|
|
|
my $cellh = int(($areay + $dist) / $party);
|
2012-04-30 12:56:01 +00:00
|
|
|
|
|
|
|
die "$total_parts parts won't fit in your print area!\n" if $total_parts > ($cellw * $cellh);
|
|
|
|
|
|
|
|
# width and height of space used by cells
|
|
|
|
my $w = $cellw * $partx;
|
|
|
|
my $h = $cellh * $party;
|
|
|
|
|
|
|
|
# left and right border positions of space used by cells
|
|
|
|
my $l = ($areax - $w) / 2;
|
|
|
|
my $r = $l + $w;
|
|
|
|
|
|
|
|
# top and bottom border positions
|
|
|
|
my $t = ($areay - $h) / 2;
|
|
|
|
my $b = $t + $h;
|
|
|
|
|
|
|
|
# list of cells, sorted by distance from center
|
|
|
|
my @cellsorder;
|
|
|
|
|
|
|
|
# work out distance for all cells, sort into list
|
|
|
|
for my $i (0..$cellw-1) {
|
|
|
|
for my $j (0..$cellh-1) {
|
|
|
|
my $cx = $linint->($i + 0.5, 0, $cellw, $l, $r);
|
|
|
|
my $cy = $linint->($j + 0.5, 0, $cellh, $t, $b);
|
|
|
|
|
|
|
|
my $xd = abs(($areax / 2) - $cx);
|
|
|
|
my $yd = abs(($areay / 2) - $cy);
|
|
|
|
|
|
|
|
my $c = {
|
|
|
|
location => [$cx, $cy],
|
|
|
|
index => [$i, $j],
|
|
|
|
distance => $xd * $xd + $yd * $yd - abs(($cellw / 2) - ($i + 0.5)),
|
|
|
|
};
|
|
|
|
|
|
|
|
BINARYINSERTIONSORT: {
|
|
|
|
my $index = $c->{distance};
|
|
|
|
my $low = 0;
|
|
|
|
my $high = @cellsorder;
|
|
|
|
while ($low < $high) {
|
|
|
|
my $mid = ($low + (($high - $low) / 2)) | 0;
|
|
|
|
my $midval = $cellsorder[$mid]->[0];
|
|
|
|
|
|
|
|
if ($midval < $index) {
|
|
|
|
$low = $mid + 1;
|
|
|
|
} elsif ($midval > $index) {
|
|
|
|
$high = $mid;
|
|
|
|
} else {
|
|
|
|
splice @cellsorder, $mid, 0, [$index, $c];
|
|
|
|
last BINARYINSERTIONSORT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
splice @cellsorder, $low, 0, [$index, $c];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# the extents of cells actually used by objects
|
|
|
|
my ($lx, $ty, $rx, $by) = (0, 0, 0, 0);
|
|
|
|
|
|
|
|
# now find cells actually used by objects, map out the extents so we can position correctly
|
|
|
|
for my $i (1..$total_parts) {
|
|
|
|
my $c = $cellsorder[$i - 1];
|
|
|
|
my $cx = $c->[1]->{index}->[0];
|
|
|
|
my $cy = $c->[1]->{index}->[1];
|
|
|
|
if ($i == 1) {
|
|
|
|
$lx = $rx = $cx;
|
|
|
|
$ty = $by = $cy;
|
|
|
|
} else {
|
|
|
|
$rx = $cx if $cx > $rx;
|
|
|
|
$lx = $cx if $cx < $lx;
|
|
|
|
$by = $cy if $cy > $by;
|
|
|
|
$ty = $cy if $cy < $ty;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# now we actually place objects into cells, positioned such that the left and bottom borders are at 0
|
|
|
|
my @positions = ();
|
|
|
|
for (1..$total_parts) {
|
|
|
|
my $c = shift @cellsorder;
|
|
|
|
my $cx = $c->[1]->{index}->[0] - $lx;
|
|
|
|
my $cy = $c->[1]->{index}->[1] - $ty;
|
|
|
|
|
|
|
|
push @positions, [$cx * $partx, $cy * $party];
|
|
|
|
}
|
2013-12-23 23:30:51 +00:00
|
|
|
|
|
|
|
if (defined $bb) {
|
|
|
|
$_->[X] += $bb->x_min for @positions;
|
|
|
|
$_->[Y] += $bb->y_min for @positions;
|
|
|
|
}
|
2012-04-30 12:56:01 +00:00
|
|
|
return @positions;
|
|
|
|
}
|
|
|
|
|
2011-09-18 17:28:12 +00:00
|
|
|
1;
|