summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/ray.cpp18
-rw-r--r--lib/ray.hpp2
2 files changed, 20 insertions, 0 deletions
diff --git a/lib/ray.cpp b/lib/ray.cpp
new file mode 100644
index 0000000..1e30ae4
--- /dev/null
+++ b/lib/ray.cpp
@@ -0,0 +1,18 @@
+#include "ray.hpp"
+
+float
+Ray::distanceToLine(const glm::vec3 & p1, const glm::vec3 & e1) const
+{
+ // https://en.wikipedia.org/wiki/Skew_lines
+ const auto diff = p1 - e1;
+ const auto d1 = glm::normalize(diff);
+ const auto &p2 = start, &d2 = direction;
+ const auto n = glm::cross(d1, d2);
+ const auto n2 = glm::cross(d2, n);
+ const auto c1 = p1 + (glm::dot((p2 - p1), n2) / glm::dot(d1, n2)) * d1;
+ const auto difflength = glm::length(diff);
+ if (glm::length(c1 - p1) > difflength || glm::length(c1 - e1) > difflength) {
+ return std::numeric_limits<float>::infinity();
+ }
+ return glm::abs(glm::dot(n, p1 - p2));
+}
diff --git a/lib/ray.hpp b/lib/ray.hpp
index 4c0710a..bce4a30 100644
--- a/lib/ray.hpp
+++ b/lib/ray.hpp
@@ -8,4 +8,6 @@ public:
glm::vec3 start;
glm::vec3 direction;
+
+ float distanceToLine(const glm::vec3 & a, const glm::vec3 & b) const;
};