Ported point_line_distance() and removed same_point()

This commit is contained in:
Alessandro Ranellucci 2013-11-06 23:08:03 +01:00
parent 09be25a156
commit c133a33ed2
6 changed files with 29 additions and 26 deletions

View File

@ -187,7 +187,7 @@ sub _medial_axis_voronoi {
# being longer than $width / 2
$polygon = $polygon->subdivide($width/2);
push @points, map $_->pp, @$polygon;
push @points, @{$polygon->pp};
}
$voronoi = Math::Geometry::Voronoi->new(points => \@points);
}
@ -200,10 +200,8 @@ sub _medial_axis_voronoi {
# ignore lines going to infinite
next if $edge->[1] == -1 || $edge->[2] == -1;
my ($a, $b);
$a = Slic3r::Point->new(@{$vertices->[$edge->[1]]});
$b = Slic3r::Point->new(@{$vertices->[$edge->[2]]});
next if !$self->encloses_point_quick($a) || !$self->encloses_point_quick($b);
next if !$self->encloses_point_quick(Slic3r::Point->new(@{$vertices->[$edge->[1]]}))
|| !$self->encloses_point_quick(Slic3r::Point->new(@{$vertices->[$edge->[2]]}));
push @skeleton_lines, [$edge->[1], $edge->[2]];
}
@ -244,7 +242,7 @@ sub _medial_axis_voronoi {
# cleanup
$polyline = Slic3r::Geometry::douglas_peucker($polyline, $width / 7);
if (Slic3r::Geometry::same_point($polyline->[0], $polyline->[-1])) {
if ($polyline->[0][X] == $polyline->[-1][X] && $polyline->[0][Y] == $polyline->[-1][Y]) {
next if @$polyline == 2;
push @result, Slic3r::Polygon->new(@$polyline[0..$#$polyline-1]);
} else {

View File

@ -14,7 +14,7 @@ our @EXPORT_OK = qw(
polygon_has_vertex can_connect_points deg2rad rad2deg
rotate_points move_points clip_segment_polygon
sum_vectors multiply_vector subtract_vectors dot perp polygon_points_visibility
line_intersection bounding_box bounding_box_intersect same_point
line_intersection bounding_box bounding_box_intersect
angle3points three_points_aligned line_direction
polyline_remove_parallel_continuous_edges polyline_remove_acute_vertices
polygon_remove_acute_vertices polygon_remove_parallel_continuous_edges
@ -107,29 +107,11 @@ sub points_coincide {
return 0;
}
sub same_point {
my ($p1, $p2) = @_;
return $p1->[X] == $p2->[X] && $p1->[Y] == $p2->[Y];
}
sub distance_between_points {
my ($p1, $p2) = @_;
return sqrt((($p1->[X] - $p2->[X])**2) + ($p1->[Y] - $p2->[Y])**2);
}
sub point_line_distance {
my ($point, $line) = @_;
return distance_between_points($point, $line->[A])
if same_point($line->[A], $line->[B]);
my $n = ($line->[B][X] - $line->[A][X]) * ($line->[A][Y] - $point->[Y])
- ($line->[A][X] - $point->[X]) * ($line->[B][Y] - $line->[A][Y]);
my $d = sqrt((($line->[B][X] - $line->[A][X]) ** 2) + (($line->[B][Y] - $line->[A][Y]) ** 2));
return abs($n) / $d;
}
# this will check whether a point is in a polygon regardless of polygon orientation
sub point_in_polygon {
my ($point, $polygon) = @_;
@ -795,7 +777,7 @@ sub douglas_peucker {
my $dmax = 0;
my $index = 0;
for my $i (1..$#$points) {
my $d = point_line_distance($points->[$i], [ $points->[0], $points->[-1] ]);
my $d = $points->[$i]->distance_to(Slic3r::Line->new($points->[0], $points->[-1]));
if ($d > $dmax) {
$index = $i;
$dmax = $d;

View File

@ -60,6 +60,12 @@ Line::coincides_with(const Line* line) const
return this->a.coincides_with(&line->a) && this->b.coincides_with(&line->b);
}
double
Line::distance_to(const Point* point) const
{
return point->distance_to(this);
}
#ifdef SLIC3RXS
void
Line::from_SV(SV* line_sv)

View File

@ -6,6 +6,8 @@
namespace Slic3r {
class Line;
class Line
{
public:
@ -21,6 +23,7 @@ class Line
Point* midpoint() const;
Point* point_at(double distance) const;
bool coincides_with(const Line* line) const;
double distance_to(const Point* point) const;
#ifdef SLIC3RXS
void from_SV(SV* line_sv);

View File

@ -1,4 +1,5 @@
#include "Point.hpp"
#include "Line.hpp"
#include <math.h>
namespace Slic3r {
@ -72,6 +73,17 @@ Point::distance_to(const Point* point) const
return sqrt(dx*dx + dy*dy);
}
double
Point::distance_to(const Line* line) const
{
if (line->a.coincides_with(&line->b)) return this->distance_to(&line->a);
double n = (line->b.x - line->a.x) * (line->a.y - this->y)
- (line->a.x - this->x) * (line->b.y - line->a.y);
return abs(n) / line->length();
}
#ifdef SLIC3RXS
SV*
Point::to_SV_ref() {

View File

@ -7,6 +7,7 @@
namespace Slic3r {
class Line;
class Point;
typedef std::vector<Point> Points;
@ -23,6 +24,7 @@ class Point
int nearest_point_index(const Points points) const;
Point* nearest_point(Points points) const;
double distance_to(const Point* point) const;
double distance_to(const Line* line) const;
#ifdef SLIC3RXS
void from_SV(SV* point_sv);