Refactoring: move direction math into a single function. Includes some fixes and adjustments

This commit is contained in:
Alessandro Ranellucci 2014-05-02 18:46:22 +02:00
parent 8240f71d07
commit cb1527f7ef
9 changed files with 59 additions and 13 deletions

View file

@ -93,6 +93,14 @@ chained_path_items(Points &points, T &items, T &retval)
}
template void chained_path_items(Points &points, ClipperLib::PolyNodes &items, ClipperLib::PolyNodes &retval);
bool
directions_parallel(double angle1, double angle2, double max_diff)
{
double diff = fabs(angle1 - angle2);
max_diff += EPSILON;
return diff < max_diff || fabs(diff - PI) < max_diff;
}
Line
MedialAxis::edge_to_line(const VD::edge_type &edge) const
{

View file

@ -15,6 +15,7 @@ void convex_hull(Points &points, Polygon* hull);
void chained_path(Points &points, std::vector<Points::size_type> &retval, Point start_near);
void chained_path(Points &points, std::vector<Points::size_type> &retval);
template<class T> void chained_path_items(Points &points, T &items, T &retval);
bool directions_parallel(double angle1, double angle2, double max_diff = 0);
class MedialAxis {
public:

View file

@ -1,3 +1,4 @@
#include "Geometry.hpp"
#include "Line.hpp"
#include "Polyline.hpp"
#include <algorithm>
@ -113,8 +114,7 @@ Line::direction() const
bool
Line::parallel_to(double angle) const {
double diff = abs(this->direction() - angle);
return (diff < EPSILON) || (abs(diff - PI) < EPSILON);
return Slic3r::Geometry::directions_parallel(this->direction(), angle);
}
bool

View file

@ -54,12 +54,12 @@ foreach my $base_angle (0, PI/4, PI/2, PI) {
}
{
my $line2 = $line->clone;
$line2->rotate(+EPSILON/2, [0,0]);
$line2->rotate(+(EPSILON)/2, [0,0]);
ok $line->parallel_to_line($line2), 'line is parallel within epsilon';
}
{
my $line2 = $line->clone;
$line2->rotate(-EPSILON/2, [0,0]);
$line2->rotate(-(EPSILON)/2, [0,0]);
ok $line->parallel_to_line($line2), 'line is parallel within epsilon';
}
}

View file

@ -4,7 +4,9 @@ use strict;
use warnings;
use Slic3r::XS;
use Test::More tests => 2;
use Test::More tests => 8;
use constant PI => 4 * atan2(1, 1);
{
my @points = (
@ -19,4 +21,14 @@ use Test::More tests => 2;
is scalar(@$hull), 4, 'convex_hull returns the correct number of points';
}
# directions_parallel() and directions_parallel_within() are tested
# also with Slic3r::Line::parallel_to() tests in 10_line.t
{
ok Slic3r::Geometry::directions_parallel_within(0, 0, 0), 'directions_parallel_within';
ok Slic3r::Geometry::directions_parallel_within(0, PI, 0), 'directions_parallel_within';
ok Slic3r::Geometry::directions_parallel_within(0, 0, PI/180), 'directions_parallel_within';
ok Slic3r::Geometry::directions_parallel_within(0, PI, PI/180), 'directions_parallel_within';
ok !Slic3r::Geometry::directions_parallel_within(PI/2, PI, 0), 'directions_parallel_within';
ok !Slic3r::Geometry::directions_parallel_within(PI/2, PI, PI/180), 'directions_parallel_within';
}
__END__

View file

@ -11,6 +11,25 @@
%{
bool
directions_parallel(angle1, angle2)
double angle1
double angle2
CODE:
RETVAL = Slic3r::Geometry::directions_parallel(angle1, angle2);
OUTPUT:
RETVAL
bool
directions_parallel_within(angle1, angle2, max_diff)
double angle1
double angle2
double max_diff
CODE:
RETVAL = Slic3r::Geometry::directions_parallel(angle1, angle2, max_diff);
OUTPUT:
RETVAL
Polygon*
convex_hull(points)
Points points