From 93cdd830055525729a6d9c34696ff1ff312bdc41 Mon Sep 17 00:00:00 2001 From: scorpioblood <77296181+scorpioblood@users.noreply.github.com> Date: Wed, 22 May 2024 21:55:03 +0200 Subject: [PATCH] Add ray. --- Engine/src/Runtime/Core/public/Math/Ray.hpp | 127 ++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 Engine/src/Runtime/Core/public/Math/Ray.hpp diff --git a/Engine/src/Runtime/Core/public/Math/Ray.hpp b/Engine/src/Runtime/Core/public/Math/Ray.hpp new file mode 100644 index 0000000..7ea1c79 --- /dev/null +++ b/Engine/src/Runtime/Core/public/Math/Ray.hpp @@ -0,0 +1,127 @@ +#pragma once + +#include "Core/public/Math/Boilerplate.h" +#include "Core/public/Math/MathFwd.h" + +#include "Core/public/Math/Vector3.hpp" + +namespace Phanes::Core::Math +{ + + // Ray with origin and direction (L = p + t * v) + + template + struct TRay + { + public: + using Real = T; + + TVector3 origin; + TVector3 direction; + + public: + /** Default constructor */ + TRay() = default; + + /** Copy constructor */ + TRay(const TRay& r) : direction(r.direction), origin(r.origin) {}; + + /** Move constructor */ + TRay(TRay&& r) : direction(std::move(r.direction)), origin(std::move(r.origin)) {}; + + /** + * Construct ray from origin and direction. + * + * @param(direction) Direction + * @param(origin) Origin + */ + + TRay(const TVector3& direction, const TVector3& origin) : direction(direction), origin(origin) {}; + + }; + + // ================== // + // TRay operators // + // ================== // + + + /** + * Tests two rays for equality + * + * @param(r1) Ray one + * @param(r2) Ray two + * + * @return True, if same and false, if not. + */ + + template + FORCEINLINE bool operator== (const TRay& r1, const TRay& r2) + { + return (r1.origin == r2.origin && r1.direction == r2.direction); + } + + /** + * Tests two rays for inequality + * + * @param(r1) Ray one + * @param(r2) Ray two + * + * @return True, if not same and false, if same. + */ + + template + FORCEINLINE bool operator== (const TRay& r1, const TRay& r2) + { + return (r1.origin != r2.origin || r1.direction != r2.direction); + } + + // ================== // + // TRay functions // + // ================== // + + + /** + * Gets the point of the ray at a given parameter + * + * @param(r1) Ray + * @param(t) Parameter + * + * @return Point at t + */ + + template + TVector3 PointAt(const TRay& r1, T t) + { + return r1.origin + r1.direction * t; + } + + /** + * Gets parameter necessary to travel to query point on line. + * + * @param(r1) Ray + * @param(p1) Query point + * + * @return parameter t + */ + + template + TVector3 GetParameter(const TRay& r1, const TVector3& p1) + { + return DotP((p1 - r1.origin), r1.direction); + } + + /** + * Tests wether two ray point in the same direction (Not if origin). + * + * @param(r1) Ray one + * @param(r2) Ray two + * + * @return True, if both rays point in the same direction, false if not. + */ + + template + inline bool SameDirection(const TRay& r1, const TRay& r2) + { + return (r1.direction == r1.direction); + } +} \ No newline at end of file