#include "cafLine.h" //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- template caf::Line::Line() : m_start(cvf::Vector3::UNDEFINED) , m_end(cvf::Vector3::UNDEFINED) { } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- template caf::Line::Line(const cvf::Vector3& startPoint, const cvf::Vector3& endPoint) : m_start(startPoint) , m_end(endPoint) { } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- template caf::Line::Line(const Line& copyFrom) { m_start = copyFrom.start(); m_end = copyFrom.end(); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- template caf::Line& caf::Line::operator=(const Line& copyFrom) { m_start = copyFrom.start(); m_end = copyFrom.end(); return *this; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- template const cvf::Vector3& caf::Line::start() const { return m_start; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- template const cvf::Vector3& caf::Line::end() const { return m_end; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- template cvf::Vector3 caf::Line::vector() const { return m_end - m_start; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- template caf::Line caf::Line::findLineBetweenNearestPoints(const Line& otherLine, bool* withinLineSegments) { // Taken from Real-Time Collision Detection, Christer Ericson, 2005, p146-147 cvf::Vector3 d1 = vector(); cvf::Vector3 d2 = otherLine.vector(); S a = d1.dot(d1); S b = d1.dot(d2); S e = d2.dot(d2); S d = a * e - b * b; if (d < std::numeric_limits::epsilon()) { // Parallel lines. Choice of closest points is arbitrary. // Just use start to start. if (withinLineSegments) *withinLineSegments = true; return Line(start(), otherLine.start()); } cvf::Vector3 r = start() - otherLine.start(); S c = d1.dot(r); S f = d2.dot(r); S s = (b*f - c * e) / d; S t = (a*f - b * c) / d; if (withinLineSegments) { *withinLineSegments = s >= 0 && s <= 1 && t >= 0 && t <= 1; } return Line(start() + s * d1, otherLine.start() + t * d2); }