diff --git a/src/libslic3r/Line.hpp b/src/libslic3r/Line.hpp index 781d8cb14..118a92135 100644 --- a/src/libslic3r/Line.hpp +++ b/src/libslic3r/Line.hpp @@ -54,11 +54,11 @@ double distance_to_squared(const L &line, const Vec, Scalar> &point, V // We find projection of this point onto the line. // It falls where t = [(this-a) . (b-a)] / |b-a|^2 const double t = va.dot(v) / l2; - if (t < 0.0) { + if (t <= 0.0) { // beyond the 'a' end of the segment *nearest_point = get_a(line); return va.squaredNorm(); - } else if (t > 1.0) { + } else if (t >= 1.0) { // beyond the 'b' end of the segment *nearest_point = get_b(line); return (point - get_b(line)).template cast().squaredNorm(); @@ -120,6 +120,33 @@ double distance_to_infinite(const L &line, const Vec, Scalar> &point) return std::sqrt(distance_to_infinite_squared(line, point)); } +template bool intersection(const L &l1, const L &l2, Vec, Scalar> *intersection_pt) +{ + using Floating = typename std::conditional>::value, Scalar, double>::type; + using VecType = const Vec, Floating>; + const VecType v1 = (l1.b - l1.a).template cast(); + const VecType v2 = (l2.b - l2.a).template cast(); + Floating denom = cross2(v1, v2); + if (fabs(denom) < EPSILON) +#if 0 + // Lines are collinear. Return true if they are coincident (overlappign). + return ! (fabs(nume_a) < EPSILON && fabs(nume_b) < EPSILON); +#else + return false; +#endif + const VecType v12 = (l1.a - l2.a).template cast(); + Floating nume_a = cross2(v2, v12); + Floating nume_b = cross2(v1, v12); + Floating t1 = nume_a / denom; + Floating t2 = nume_b / denom; + if (t1 >= 0 && t1 <= 1.0f && t2 >= 0 && t2 <= 1.0f) { + // Get the intersection point. + (*intersection_pt) = (l1.a.template cast() + t1 * v1).template cast>(); + return true; + } + return false; // not intersecting +} + } // namespace line_alg class Line