diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/geometricPlane.cpp | 19 | ||||
-rw-r--r-- | lib/geometricPlane.h | 10 |
2 files changed, 29 insertions, 0 deletions
diff --git a/lib/geometricPlane.cpp b/lib/geometricPlane.cpp index cb52997..71216c1 100644 --- a/lib/geometricPlane.cpp +++ b/lib/geometricPlane.cpp @@ -1,5 +1,7 @@ #include "geometricPlane.h" +#include "ray.hpp" #include <glm/geometric.hpp> +#include <glm/gtx/intersect.hpp> GeometricPlane::PlaneRelation GeometricPlane::getRelation(glm::vec3 p) const @@ -7,3 +9,20 @@ GeometricPlane::getRelation(glm::vec3 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::DistAndPosition> +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 96816f2..dc8df50 100644 --- a/lib/geometricPlane.h +++ b/lib/geometricPlane.h @@ -1,12 +1,22 @@ #pragma once #include <glm/vec3.hpp> +#include <optional> + +class Ray; class GeometricPlane { public: + struct DistAndPosition { + float dist; + glm::vec3 position; + }; enum class PlaneRelation { Above, Below, On }; glm::vec3 origin, normal; PlaneRelation getRelation(glm::vec3 point) const; + std::optional<DistAndPosition> getRayIntersectPosition(const Ray &) const; + + static bool isIntersect(PlaneRelation a, PlaneRelation b); }; |