Implement Vector3::MinElement

This commit is contained in:
2024-03-20 15:36:27 -04:00
parent f4c6337f12
commit dc41dcf520
4 changed files with 43 additions and 47 deletions

View File

@@ -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;
};
}

View File

@@ -6,7 +6,6 @@
#include <cstdlib>
#include <J3ML/LinearAlgebra/Angle2D.h>
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

View File

@@ -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;}
}

View File

@@ -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
};
}
}