summaryrefslogtreecommitdiff
path: root/lib/geometricPlane.h
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2024-01-07 13:23:27 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2024-01-07 13:23:27 +0000
commit108bf5b2424da06aa2e1735ffd10a2b713834db8 (patch)
treeaf0918a6a266aa7e6ee1d8129df6d5ef9fc4f8ab /lib/geometricPlane.h
parentRemove more use of legacy types and unnecessary pointers from selectable inte... (diff)
parentRemove Position3D from RailVehicle (diff)
downloadilt-108bf5b2424da06aa2e1735ffd10a2b713834db8.tar.bz2
ilt-108bf5b2424da06aa2e1735ffd10a2b713834db8.tar.xz
ilt-108bf5b2424da06aa2e1735ffd10a2b713834db8.zip
Merge branch 'template-types'
Diffstat (limited to 'lib/geometricPlane.h')
-rw-r--r--lib/geometricPlane.h37
1 files changed, 28 insertions, 9 deletions
diff --git a/lib/geometricPlane.h b/lib/geometricPlane.h
index c74beff..3f95d3c 100644
--- a/lib/geometricPlane.h
+++ b/lib/geometricPlane.h
@@ -1,24 +1,43 @@
#pragma once
#include "config/types.h"
+#include "ray.h"
+#include <glm/geometric.hpp>
+#include <glm/gtx/intersect.hpp>
#include <glm/vec3.hpp>
#include <optional>
-class Ray;
-
class GeometricPlane {
public:
+ enum class PlaneRelation { Above, Below, On };
+
+ static bool isIntersect(PlaneRelation a, PlaneRelation b);
+};
+
+template<typename PositionType> 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]] PlaneRelation getRelation(Position3D point) const;
- [[nodiscard]] std::optional<DistAndPosition> getRayIntersectPosition(const Ray &) const;
+ [[nodiscard]] inline PlaneRelation
+ 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;
+ }
- static bool isIntersect(PlaneRelation a, PlaneRelation b);
+ [[nodiscard]] inline std::optional<DistAndPosition>
+ getRayIntersectPosition(const Ray<PositionType> & ray) const
+ {
+ float dist {};
+ if (!glm::intersectRayPlane(ray.start, ray.direction, origin, normal, dist)) {
+ return {};
+ }
+ return DistAndPosition {dist, ray.start + (ray.direction * dist)};
+ }
};