From e749f6040f3ff9e81b19de47800cac1a90d549e5 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Fri, 16 Jan 2015 16:25:39 +0100 Subject: [PATCH] New +Line::intersection_infinite() method --- xs/src/libslic3r/Line.cpp | 17 +++++++++++++++++ xs/src/libslic3r/Line.hpp | 1 + xs/t/10_line.t | 9 ++++++++- xs/xsp/Line.xsp | 7 +++++++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/xs/src/libslic3r/Line.cpp b/xs/src/libslic3r/Line.cpp index b98208325..15771ea7c 100644 --- a/xs/src/libslic3r/Line.cpp +++ b/xs/src/libslic3r/Line.cpp @@ -89,6 +89,23 @@ Line::point_at(double distance) const return p; } +bool +Line::intersection_infinite(const Line &other, Point* point) const +{ + Vector x = this->a.vector_to(other.a); + Vector d1 = this->vector(); + Vector d2 = other.vector(); + + double cross = d1.x * d2.y - d1.y * d2.x; + if (std::fabs(cross) < EPSILON) + return false; + + double t1 = (x.x * d2.y - x.y * d2.x)/cross; + point->x = this->a.x + d1.x * t1; + point->y = this->a.y + d1.y * t1; + return true; +} + bool Line::coincides_with(const Line &line) const { diff --git a/xs/src/libslic3r/Line.hpp b/xs/src/libslic3r/Line.hpp index 0b9cc6302..c64288cf6 100644 --- a/xs/src/libslic3r/Line.hpp +++ b/xs/src/libslic3r/Line.hpp @@ -29,6 +29,7 @@ class Line Point* midpoint() const; void point_at(double distance, Point* point) const; Point point_at(double distance) const; + bool intersection_infinite(const Line &other, Point* point) const; bool coincides_with(const Line &line) const; double distance_to(const Point &point) const; bool parallel_to(double angle) const; diff --git a/xs/t/10_line.t b/xs/t/10_line.t index 67c3cd353..8f82e988c 100644 --- a/xs/t/10_line.t +++ b/xs/t/10_line.t @@ -4,7 +4,7 @@ use strict; use warnings; use Slic3r::XS; -use Test::More tests => 39; +use Test::More tests => 40; use constant PI => 4 * atan2(1, 1); use constant EPSILON => 1E-4; @@ -71,4 +71,11 @@ foreach my $base_angle (0, PI/4, PI/2, PI) { } } +{ + my $a = Slic3r::Line->new([100, 0], [200, 0]); + my $b = Slic3r::Line->new([300, 300], [300, 100]); + my $r = $a->intersection_infinite($b); + is_deeply $r->pp, [300, 0], 'intersection_infinite'; +} + __END__ diff --git a/xs/xsp/Line.xsp b/xs/xsp/Line.xsp index 004f9fae2..e5bc63c28 100644 --- a/xs/xsp/Line.xsp +++ b/xs/xsp/Line.xsp @@ -30,6 +30,13 @@ %code{% RETVAL = THIS->parallel_to(*line); %}; Point* midpoint(); Clone point_at(double distance); + Clone intersection_infinite(Line* other) + %code{% + Point p; + bool res = THIS->intersection_infinite(*other, &p); + if (!res) CONFESS("Intersection failed"); + RETVAL = p; + %}; Polyline* as_polyline() %code{% RETVAL = new Polyline(*THIS); %}; Clone normal();