diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/geometricPlane.h | 2 | ||||
-rw-r--r-- | lib/ray.cpp | 36 | ||||
-rw-r--r-- | lib/ray.h | 78 |
3 files changed, 72 insertions, 44 deletions
diff --git a/lib/geometricPlane.h b/lib/geometricPlane.h index fd3a1d7..3f95d3c 100644 --- a/lib/geometricPlane.h +++ b/lib/geometricPlane.h @@ -32,7 +32,7 @@ public: } [[nodiscard]] inline std::optional<DistAndPosition> - getRayIntersectPosition(const Ray & ray) const + getRayIntersectPosition(const Ray<PositionType> & ray) const { float dist {}; if (!glm::intersectRayPlane(ray.start, ray.direction, origin, normal, dist)) { diff --git a/lib/ray.cpp b/lib/ray.cpp deleted file mode 100644 index 254ad14..0000000 --- a/lib/ray.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include "ray.h" -#include "maths.h" -#include <algorithm> - -Ray -Ray::fromPoints(Position3D start, Position3D p) -{ - return {start, glm::normalize(p - start)}; -} - -float -Ray::distanceToLine(const Position3D & p1, const Position3D & 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 = crossProduct(d1, d2); - const auto n2 = crossProduct(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)); -} - -bool -Ray::passesCloseToEdges(const std::span<const Position3D> positions, float distance) const -{ - return std::adjacent_find(positions.begin(), positions.end(), - [this, distance](const Position3D & a, const Position3D & b) { - return distanceToLine(a, b) <= distance; - }) - != positions.end(); -} @@ -1,20 +1,84 @@ #pragma once #include "config/types.h" +#include "maths.h" + +#include <algorithm> #include <glm/glm.hpp> +#include <glm/gtx/intersect.hpp> #include <span> -class Ray { +template<typename PositionType> class Ray { public: #ifndef __cpp_aggregate_paren_init - Ray(Position3D start, Direction3D direction) : start {start}, direction {direction} { } + Ray(PositionType start, Direction3D direction) : start {start}, direction {direction} { } #endif - static Ray fromPoints(Position3D, Position3D); - - Position3D start; + PositionType start; Direction3D direction; - [[nodiscard]] float distanceToLine(const Position3D & a, const Position3D & b) const; - [[nodiscard]] bool passesCloseToEdges(const std::span<const Position3D> positions, float distance) const; + [[nodiscard]] PositionType::value_type + distanceToLine(const PositionType & p1, const PositionType & e1) const + { + // https://en.wikipedia.org/wiki/Skew_lines + const RelativePosition3D diff = p1 - e1; + const auto d1 = glm::normalize(diff); + const auto n = crossProduct(d1, direction); + const auto n2 = crossProduct(direction, n); + const auto c1 = p1 + PositionType((glm::dot(RelativePosition3D(start - p1), n2) / glm::dot(d1, n2)) * d1); + const auto difflength = glm::length(diff); + if (glm::length(RelativePosition3D(c1 - p1)) > difflength + || glm::length(RelativePosition3D(c1 - e1)) > difflength) { + return std::numeric_limits<typename PositionType::value_type>::infinity(); + } + return static_cast<PositionType::value_type>(glm::abs(glm::dot(n, RelativePosition3D(p1 - start)))); + } + + [[nodiscard]] bool + passesCloseToEdges(const std::span<const PositionType> positions, const PositionType::value_type distance) const + { + return std::adjacent_find(positions.begin(), positions.end(), [this, distance](const auto & a, const auto & b) { + return distanceToLine(a, b) <= distance; + }) != positions.end(); + } + + bool + intersectTriangle(const PositionType t0, const PositionType t1, const PositionType t2, BaryPosition & bary, + RelativeDistance & distance) const + { + if constexpr (std::is_floating_point_v<typename PositionType::value_type>) { + return glm::intersectRayTriangle(start, direction, t0, t1, t2, bary, distance); + } + else { + const RelativePosition3D t0r = t0 - start, t1r = t1 - start, t2r = t2 - start; + return glm::intersectRayTriangle({}, direction, t0r, t1r, t2r, bary, distance); + } + } + + bool + intersectSphere(const PositionType centre, const PositionType::value_type size, PositionType & position, + Normal3D & normal) const + { + if constexpr (std::is_floating_point_v<typename PositionType::value_type>) { + return glm::intersectRaySphere(start, direction, centre, size, position, normal); + } + else { + const RelativePosition3D cr = centre - start; + RelativePosition3D positionF {}; + const auto r = glm::intersectRaySphere( + {}, direction, cr, static_cast<RelativeDistance>(size), positionF, normal); + position = GlobalPosition3D(positionF) + start; + return r; + } + } +}; + +class RayFactory { +public: + template<typename PositionType> + static Ray<PositionType> + fromPoints(PositionType start, PositionType p) + { + return {start, glm::normalize(p - start)}; + } }; |