From 7ad46a0e239c7e68c4436e632a77a0d8a14b6333 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 5 Jan 2024 20:08:26 +0000 Subject: Inline functions of GeometricPlane --- lib/geometricPlane.cpp | 28 ---------------------------- lib/geometricPlane.h | 30 +++++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 33 deletions(-) delete mode 100644 lib/geometricPlane.cpp (limited to 'lib') diff --git a/lib/geometricPlane.cpp b/lib/geometricPlane.cpp deleted file mode 100644 index 567f98a..0000000 --- a/lib/geometricPlane.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include "geometricPlane.h" -#include "ray.h" -#include -#include - -GeometricPlane::PlaneRelation -GeometricPlane::getRelation(Position3D p) const -{ - const auto d = glm::dot(normal, p - origin); - return d < 0.F ? PlaneRelation::Below : d > 0.F ? PlaneRelation::Above : PlaneRelation::On; -} - -bool -GeometricPlane::isIntersect(PlaneRelation a, PlaneRelation b) -{ - return ((a == PlaneRelation::Above && b == PlaneRelation::Below) - || (a == PlaneRelation::Below && b == PlaneRelation::Above)); -} - -std::optional -GeometricPlane::getRayIntersectPosition(const Ray & ray) const -{ - float dist {}; - if (!glm::intersectRayPlane(ray.start, ray.direction, origin, normal, dist)) { - return {}; - } - return DistAndPosition {dist, ray.start + (ray.direction * dist)}; -} diff --git a/lib/geometricPlane.h b/lib/geometricPlane.h index c74beff..5be76ec 100644 --- a/lib/geometricPlane.h +++ b/lib/geometricPlane.h @@ -1,11 +1,12 @@ #pragma once #include "config/types.h" +#include "ray.h" +#include +#include #include #include -class Ray; - class GeometricPlane { public: struct DistAndPosition { @@ -17,8 +18,27 @@ public: Position3D origin; Normal3D normal; - [[nodiscard]] PlaneRelation getRelation(Position3D point) const; - [[nodiscard]] std::optional getRayIntersectPosition(const Ray &) const; + [[nodiscard]] inline PlaneRelation + getRelation(Position3D point) const + { + const auto d = glm::dot(normal, point - origin); + return d < 0.F ? PlaneRelation::Below : d > 0.F ? PlaneRelation::Above : PlaneRelation::On; + } + + [[nodiscard]] inline std::optional + getRayIntersectPosition(const Ray & ray) const + { + float dist {}; + if (!glm::intersectRayPlane(ray.start, ray.direction, origin, normal, dist)) { + return {}; + } + return DistAndPosition {dist, ray.start + (ray.direction * dist)}; + } - static bool isIntersect(PlaneRelation a, PlaneRelation b); + inline static bool + isIntersect(PlaneRelation a, PlaneRelation b) + { + return ((a == PlaneRelation::Above && b == PlaneRelation::Below) + || (a == PlaneRelation::Below && b == PlaneRelation::Above)); + } }; -- cgit v1.2.3 From 335e9c6b4bbc5bbf1a861ac16abf612f5519dd5e Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 5 Jan 2024 20:20:14 +0000 Subject: Template GeometicPlane on Position type --- lib/geometricPlane.cpp | 8 ++++++++ lib/geometricPlane.h | 23 +++++++++++------------ 2 files changed, 19 insertions(+), 12 deletions(-) create mode 100644 lib/geometricPlane.cpp (limited to 'lib') diff --git a/lib/geometricPlane.cpp b/lib/geometricPlane.cpp new file mode 100644 index 0000000..7aadf6a --- /dev/null +++ b/lib/geometricPlane.cpp @@ -0,0 +1,8 @@ +#include "geometricPlane.h" + +bool +GeometricPlane::isIntersect(PlaneRelation a, PlaneRelation b) +{ + return ((a == PlaneRelation::Above && b == PlaneRelation::Below) + || (a == PlaneRelation::Below && b == PlaneRelation::Above)); +} diff --git a/lib/geometricPlane.h b/lib/geometricPlane.h index 5be76ec..fd3a1d7 100644 --- a/lib/geometricPlane.h +++ b/lib/geometricPlane.h @@ -8,18 +8,24 @@ #include class GeometricPlane { +public: + enum class PlaneRelation { Above, Below, On }; + + static bool isIntersect(PlaneRelation a, PlaneRelation b); +}; + +template class GeometricPlaneT : public GeometricPlane { public: struct DistAndPosition { - float dist; - Position3D position; + PositionType::value_type dist; + PositionType position; }; - enum class PlaneRelation { Above, Below, On }; - Position3D origin; + PositionType origin; Normal3D normal; [[nodiscard]] inline PlaneRelation - getRelation(Position3D point) const + getRelation(PositionType point) const { const auto d = glm::dot(normal, point - origin); return d < 0.F ? PlaneRelation::Below : d > 0.F ? PlaneRelation::Above : PlaneRelation::On; @@ -34,11 +40,4 @@ public: } return DistAndPosition {dist, ray.start + (ray.direction * dist)}; } - - inline static bool - isIntersect(PlaneRelation a, PlaneRelation b) - { - return ((a == PlaneRelation::Above && b == PlaneRelation::Below) - || (a == PlaneRelation::Below && b == PlaneRelation::Above)); - } }; -- cgit v1.2.3 From dcef947ea986f4ad4633189652abccebdd7aa3f6 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 6 Jan 2024 13:03:42 +0000 Subject: Helper to create a cubiod from dimensions --- lib/basicShapes.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 lib/basicShapes.h (limited to 'lib') diff --git a/lib/basicShapes.h b/lib/basicShapes.h new file mode 100644 index 0000000..183863e --- /dev/null +++ b/lib/basicShapes.h @@ -0,0 +1,19 @@ +#pragma once +#include +#include + +template +constexpr std::array, 8> +cuboidCorners(T lx, T ux, T ly, T uy, T lz, T uz) +{ + return {{ + {lx, uy, lz}, // LFB + {ux, uy, lz}, // RFB + {lx, uy, uz}, // LFT + {ux, uy, uz}, // RFT + {lx, ly, lz}, // LBB + {ux, ly, lz}, // RBB + {lx, ly, uz}, // LBT + {ux, ly, uz}, // RBT + }}; +} -- cgit v1.2.3 From 95eed94c9294f719724283e672cc7c83b2abd282 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 7 Jan 2024 01:10:26 +0000 Subject: Unified crossProduct --- lib/maths.h | 20 +++++++++++++++++--- lib/ray.cpp | 5 +++-- 2 files changed, 20 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/maths.h b/lib/maths.h index c1bf61a..b5af9ca 100644 --- a/lib/maths.h +++ b/lib/maths.h @@ -68,9 +68,9 @@ sq(T v) return v * v; } -template -inline constexpr glm::vec<3, T, Q> -crossInt(const glm::vec<3, T, Q> a, const glm::vec<3, T, Q> b) +template +inline constexpr glm::vec<3, int64_t, Q> +crossProduct(const glm::vec<3, int64_t, Q> a, const glm::vec<3, int64_t, Q> b) { return { (a.y * b.z) - (a.z * b.y), @@ -79,6 +79,20 @@ crossInt(const glm::vec<3, T, Q> a, const glm::vec<3, T, Q> b) }; } +template +inline constexpr glm::vec<3, T, Q> +crossProduct(const glm::vec<3, T, Q> a, const glm::vec<3, T, Q> b) +{ + return crossProduct(a, b); +} + +template +inline constexpr glm::vec<3, T, Q> +crossProduct(const glm::vec<3, T, Q> a, const glm::vec<3, T, Q> b) +{ + return glm::cross(a, b); +} + template inline constexpr auto ratio(Ta a, Tb b) diff --git a/lib/ray.cpp b/lib/ray.cpp index 9fb3648..254ad14 100644 --- a/lib/ray.cpp +++ b/lib/ray.cpp @@ -1,4 +1,5 @@ #include "ray.h" +#include "maths.h" #include Ray @@ -14,8 +15,8 @@ Ray::distanceToLine(const Position3D & p1, const Position3D & e1) const 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 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) { -- cgit v1.2.3 From 6a1df3dfbae98a05e74c646cc216fbc19ffdb6d6 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 7 Jan 2024 13:04:31 +0000 Subject: Template Ray on position type --- lib/geometricPlane.h | 2 +- lib/ray.cpp | 36 ------------------------ lib/ray.h | 78 +++++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 72 insertions(+), 44 deletions(-) delete mode 100644 lib/ray.cpp (limited to 'lib') 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 - getRayIntersectPosition(const Ray & ray) const + getRayIntersectPosition(const Ray & 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 - -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::infinity(); - } - return glm::abs(glm::dot(n, p1 - p2)); -} - -bool -Ray::passesCloseToEdges(const std::span 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(); -} diff --git a/lib/ray.h b/lib/ray.h index bc70c74..e1f43c3 100644 --- a/lib/ray.h +++ b/lib/ray.h @@ -1,20 +1,84 @@ #pragma once #include "config/types.h" +#include "maths.h" + +#include #include +#include #include -class Ray { +template 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 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::infinity(); + } + return static_cast(glm::abs(glm::dot(n, RelativePosition3D(p1 - start)))); + } + + [[nodiscard]] bool + passesCloseToEdges(const std::span 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) { + 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) { + 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(size), positionF, normal); + position = GlobalPosition3D(positionF) + start; + return r; + } + } +}; + +class RayFactory { +public: + template + static Ray + fromPoints(PositionType start, PositionType p) + { + return {start, glm::normalize(p - start)}; + } }; -- cgit v1.2.3