From dc41dcf520e6162e0032de56735b4a5ac1227aae Mon Sep 17 00:00:00 2001 From: josh Date: Wed, 20 Mar 2024 15:36:27 -0400 Subject: [PATCH] Implement Vector3::MinElement --- include/J3ML/Geometry/AABB.h | 2 +- include/J3ML/LinearAlgebra/Vector3.h | 15 ++------- src/J3ML/Geometry/AABB.cpp | 49 +++++++++------------------- src/J3ML/LinearAlgebra/Vector3.cpp | 24 ++++++++++++++ 4 files changed, 43 insertions(+), 47 deletions(-) diff --git a/include/J3ML/Geometry/AABB.h b/include/J3ML/Geometry/AABB.h index 5227517..047f9e5 100644 --- a/include/J3ML/Geometry/AABB.h +++ b/include/J3ML/Geometry/AABB.h @@ -144,6 +144,6 @@ namespace J3ML::Geometry void Enclose(const OBB &obb); - bool TestAxis(Vector3 axis, Vector3 v0, Vector3 v1, Vector3 v2) const; + bool TestAxis(const Vector3& axis, const Vector3& v0, const Vector3& v1, const Vector3& v2) const; }; } diff --git a/include/J3ML/LinearAlgebra/Vector3.h b/include/J3ML/LinearAlgebra/Vector3.h index ad5a421..c5ed413 100644 --- a/include/J3ML/LinearAlgebra/Vector3.h +++ b/include/J3ML/LinearAlgebra/Vector3.h @@ -6,7 +6,6 @@ #include #include - namespace J3ML::LinearAlgebra { // A 3D (x, y, z) ordered pair. @@ -44,13 +43,9 @@ public: /// Returns the DirectionVector for a given angle. static Vector3 Direction(const Vector3 &rhs) ; - static void Orthonormalize(Vector3& a, Vector3& b, Vector3& c); - bool AreOrthonormal(const Vector3& a, const Vector3& b, float epsilon) - { - - } + bool AreOrthonormal(const Vector3& a, const Vector3& b, float epsilon); Vector3 ProjectToNorm(const Vector3& direction) const; @@ -72,7 +67,6 @@ public: bool operator != (const Vector3& rhs) const; bool IsFinite() const; - float MinElement() const; static float MinElement(const Vector3& of); @@ -150,10 +144,7 @@ public: /// Multiplies this vector by a vector, element-wise /// @note Mathematically, the multiplication of two vectors is not defined in linear space structures, /// but this function is provided here for syntactical convenience. - Vector3 Mul(const Vector3& rhs) const - { - - } + Vector3 Mul(const Vector3& rhs) const; /// Divides this vector by a scalar Vector3 operator/(float rhs) const; @@ -164,7 +155,7 @@ public: /// Divides this vector by a vector, element-wise /// @note Mathematically, the multiplication of two vectors is not defined in linear space structures, /// but this function is provided here for syntactical convenience - Vector2 Div(const Vector2& v) const; + Vector3 Div(const Vector3& v) const; /// Unary + operator Vector3 operator+() const; // TODO: Implement diff --git a/src/J3ML/Geometry/AABB.cpp b/src/J3ML/Geometry/AABB.cpp index 0bb3cdc..6979470 100644 --- a/src/J3ML/Geometry/AABB.cpp +++ b/src/J3ML/Geometry/AABB.cpp @@ -297,7 +297,7 @@ namespace J3ML::Geometry { Vector3 u2 = Vector3(0.f, 0.f, 1.f); - bool AABB::TestAxis(Vector3 axis, Vector3 v0, Vector3 v1, Vector3 v2) const + bool AABB::TestAxis(const Vector3& axis, const Vector3& v0, const Vector3& v1, const Vector3& v2) const { Vector3 e = this->Size(); @@ -333,7 +333,6 @@ namespace J3ML::Geometry { bool AABB::Intersects(const Triangle &triangle) const { - // https://gdbooks.gitbooks.io/3dcollisions/content/Chapter4/aabb-triangle.html Vector3 v0 = triangle.V0; @@ -356,8 +355,6 @@ namespace J3ML::Geometry { Vector3 f1 = v2 - v1; // C - B Vector3 f2 = v0 - v2; // A - C - - // There are a total of 13 axes to test!!! // We first test against 9 axis, these axes are given by cross product combinations // of the edges of the triangle and the edges of the AABB. You need to get an axis testing each of the 3 sides @@ -367,51 +364,34 @@ namespace J3ML::Geometry { Vector3 axis_u0_f0 = Vector3::Cross(u0, f0); Vector3 axis_u0_f1 = Vector3::Cross(u0, f1); Vector3 axis_u0_f2 = Vector3::Cross(u0, f2); - Vector3 axis_u1_f0 = Vector3::Cross(u1, f0); Vector3 axis_u1_f1 = Vector3::Cross(u1, f1); Vector3 axis_u1_f2 = Vector3::Cross(u1, f2); - Vector3 axis_u2_f0 = Vector3::Cross(u1, f0); Vector3 axis_u2_f1 = Vector3::Cross(u1, f1); Vector3 axis_u2_f2 = Vector3::Cross(u1, f2); - if (TestAxis(axis_u0_f0, v0, v1, v2)) - return true; - if (TestAxis(axis_u0_f1, v0, v1, v2)) - return true; - if (TestAxis(axis_u0_f2, v0, v1, v2)) - return true; - - if (TestAxis(axis_u1_f0, v0, v1, v2)) - return true; - if (TestAxis(axis_u1_f1, v0, v1, v2)) - return true; - if (TestAxis(axis_u1_f2, v0, v1, v2)) - return true; - - if (TestAxis(axis_u2_f0, v0, v1, v2)) - return true; - if (TestAxis(axis_u2_f1, v0, v1, v2)) - return true; - if (TestAxis(axis_u2_f2, v0, v1, v2)) - return true; + if (TestAxis(axis_u0_f0, v0, v1, v2)) return true; + if (TestAxis(axis_u0_f1, v0, v1, v2)) return true; + if (TestAxis(axis_u0_f2, v0, v1, v2)) return true; + if (TestAxis(axis_u1_f0, v0, v1, v2)) return true; + if (TestAxis(axis_u1_f1, v0, v1, v2)) return true; + if (TestAxis(axis_u1_f2, v0, v1, v2)) return true; + if (TestAxis(axis_u2_f0, v0, v1, v2)) return true; + if (TestAxis(axis_u2_f1, v0, v1, v2)) return true; + if (TestAxis(axis_u2_f2, v0, v1, v2)) return true; // Next we have 3 face normals from the AABB // for these tests we are conceptually checking if the bounding box // of the triangle intersects the bounding box of the AABB // that is to say, the separating axis for all tests are axis aligned: // axis1: (1, 0, 0), axis2: (0, 1, 0), axis3: (0, 0, 1) - // Do the SAT given the 3 primary axes of the AABB // You already have two vectors for this: u0, u1, and u2 - if (TestAxis(u0, v0, v1, v2)) - return true; - if (TestAxis(u1, v0, v1, v2)) - return true; - if (TestAxis(u2, v0, v1, v2)) - return true; + if (TestAxis(u0, v0, v1, v2)) return true; + if (TestAxis(u1, v0, v1, v2)) return true; + if (TestAxis(u2, v0, v1, v2)) return true; // Finally we have one last axis to test, the face normal of the triangle // We can get the normal of the triangle by crossing the first two line segments @@ -419,9 +399,10 @@ namespace J3ML::Geometry { if (TestAxis(triangleNormal, u0, u1, u2)) return true; - // Passed testing for all 13 separating axes that exist return true; } + Vector3 AABB::AnyPointFast() const { return minPoint;} + } diff --git a/src/J3ML/LinearAlgebra/Vector3.cpp b/src/J3ML/LinearAlgebra/Vector3.cpp index d3cf824..762770b 100644 --- a/src/J3ML/LinearAlgebra/Vector3.cpp +++ b/src/J3ML/LinearAlgebra/Vector3.cpp @@ -381,5 +381,29 @@ namespace J3ML::LinearAlgebra { return *this; } + float Vector3::MinElement() const { + return std::min(x, std::min(y, z)); + } + + bool Vector3::AreOrthonormal(const Vector3 &a, const Vector3 &b, float epsilon) { + return a.IsPerpendicular(b, epsilon) && a.IsNormalized(epsilon*epsilon) && b.IsNormalized(epsilon*epsilon); + } + + Vector3 Vector3::Mul(const Vector3 &rhs) const { + return { + this->x * rhs.x, + this->y * rhs.y, + this->z * rhs.z + }; + } + + Vector3 Vector3::Div(const Vector3 &v) const { + return { + this->x/v.x, + this->y/v.y, + this->z/v.z + }; + } + } \ No newline at end of file