1
0
forked from josh/j3ml

Implement more methods

This commit is contained in:
2024-01-30 21:35:55 -05:00
parent 0c20e9bb21
commit 132b8a0a66
7 changed files with 55 additions and 15 deletions

View File

@@ -1,5 +1,6 @@
#pragma once #pragma once
#include <J3ML/Geometry.h>
#include <J3ML/Geometry/AABB.h> #include <J3ML/Geometry/AABB.h>
#include <J3ML/Geometry/LineSegment.h> #include <J3ML/Geometry/LineSegment.h>
#include <J3ML/Geometry/Polyhedron.h> #include <J3ML/Geometry/Polyhedron.h>

View File

@@ -6,10 +6,21 @@
#include "AABB2D.h" #include "AABB2D.h"
namespace Geometry { namespace Geometry {
using LinearAlgebra::Vector2; using LinearAlgebra::Vector2;
template<typename T> template<typename T>
class QuadTree { class QuadTree {
/// A fixed split rule for all QuadTrees: A QuadTree leaf node is only ever split if the leaf contains at least this many objects.
/// Leaves containing fewer than this many objects are always kept as leaves until the object count is exceeded.
constexpr static const int minQuadTreeNodeObjectCount = 16;
/// A fixed split limit rule for all QuadTrees: If the QuadTree node side length is smaller than this, the node will
/// never be split again into smaller subnodes. This provides a hard limit safety net for infinite/extra long recursion
/// in case multiple identical overlapping objects are placed into the tree.
constexpr static const float minQuadTreeQuadrantSize = 0.05f;
public: public:
struct Node { struct Node {
Node *parent; Node *parent;

View File

@@ -8,6 +8,7 @@
namespace Geometry namespace Geometry
{ {
using LinearAlgebra::Vector3;
class Ray class Ray
{ {
Vector3 Origin; Vector3 Origin;

View File

@@ -8,51 +8,64 @@
namespace LinearAlgebra namespace LinearAlgebra
{ {
class Quaternion : public Vector4 class Quaternion : public Vector4 {
{
public: public:
Quaternion(); Quaternion();
Quaternion(const Quaternion& rhs) = default;
explicit Quaternion(const Matrix3x3& rotationMtrx); Quaternion(const Quaternion &rhs) = default;
explicit Quaternion(const Matrix4x4& rotationMtrx);
explicit Quaternion(const Matrix3x3 &rotationMtrx);
explicit Quaternion(const Matrix4x4 &rotationMtrx);
// @note The input data is not normalized after construction, this has to be done manually. // @note The input data is not normalized after construction, this has to be done manually.
Quaternion(float X, float Y, float Z, float W); Quaternion(float X, float Y, float Z, float W);
// Constructs this quaternion by specifying a rotation axis and the amount of rotation to be performed about that axis // Constructs this quaternion by specifying a rotation axis and the amount of rotation to be performed about that axis
// @param rotationAxis The normalized rotation axis to rotate about. If using Vector4 version of the constructor, the w component of this vector must be 0. // @param rotationAxis The normalized rotation axis to rotate about. If using Vector4 version of the constructor, the w component of this vector must be 0.
Quaternion(const Vector3& rotationAxis, float rotationAngleBetween) { SetFromAxisAngle(rotationAxis, rotationAngleBetween); } Quaternion(const Vector3 &rotationAxis, float rotationAngleBetween) {
Quaternion(const Vector4& rotationAxis, float rotationAngleBetween) { SetFromAxisAngle(rotationAxis, rotationAngleBetween); } SetFromAxisAngle(rotationAxis, rotationAngleBetween);
}
Quaternion(const Vector4 &rotationAxis, float rotationAngleBetween) {
SetFromAxisAngle(rotationAxis, rotationAngleBetween);
}
//void Inverse(); //void Inverse();
explicit Quaternion(Vector4 vector4); explicit Quaternion(Vector4 vector4);
void SetFromAxisAngle(const Vector3 &vector3, float between); void SetFromAxisAngle(const Vector3 &vector3, float between);
void SetFromAxisAngle(const Vector4 &vector4, float between); void SetFromAxisAngle(const Vector4 &vector4, float between);
Quaternion Inverse() const; Quaternion Inverse() const;
Quaternion Conjugate() const; Quaternion Conjugate() const;
//void Normalize(); //void Normalize();
Vector3 GetWorldX() const; Vector3 GetWorldX() const;
Vector3 GetWorldY() const; Vector3 GetWorldY() const;
Vector3 GetWorldZ() const; Vector3 GetWorldZ() const;
Vector3 GetAxis() const Vector3 GetAxis() const {
{
float rcpSinAngle = 1 - (std::sqrt(1 - w * w)); float rcpSinAngle = 1 - (std::sqrt(1 - w * w));
return Vector3(x, y, z) * rcpSinAngle; return Vector3(x, y, z) * rcpSinAngle;
} }
float GetAngle() const float GetAngle() const {
{
return std::acos(w) * 2.f; return std::acos(w) * 2.f;
} }
Matrix3x3 ToMatrix3x3() const; Matrix3x3 ToMatrix3x3() const;
Matrix4x4 ToMatrix4x4() const;
Matrix4x4 ToMatrix4x4(const Vector3 &translation) const;
Vector3 Transform(const Vector3& vec) const; Vector3 Transform(const Vector3& vec) const;
Vector3 Transform(float X, float Y, float Z) const; Vector3 Transform(float X, float Y, float Z) const;
// Note: We only transform the x,y,z components of 4D vectors, w is left untouched // Note: We only transform the x,y,z components of 4D vectors, w is left untouched
@@ -89,7 +102,7 @@ namespace LinearAlgebra
Quaternion operator - () const; Quaternion operator - () const;
float Dot(const Quaternion &quaternion) const; float Dot(const Quaternion &quaternion) const;
float Angle() const; float Angle() const { return std::acos(w) * 2.f;}
float AngleBetween(const Quaternion& target) const; float AngleBetween(const Quaternion& target) const;

View File

@@ -1,6 +1,7 @@
#include <J3ML/LinearAlgebra/Vector3.h> #include <J3ML/LinearAlgebra/Vector3.h>
#include <J3ML/LinearAlgebra/Vector4.h> #include <J3ML/LinearAlgebra/Vector4.h>
#include <J3ML/LinearAlgebra/Matrix3x3.h> #include <J3ML/LinearAlgebra/Matrix3x3.h>
#include <J3ML/LinearAlgebra/Matrix4x4.h>
#include <J3ML/LinearAlgebra/Quaternion.h> #include <J3ML/LinearAlgebra/Quaternion.h>
namespace LinearAlgebra { namespace LinearAlgebra {
@@ -163,4 +164,12 @@ namespace LinearAlgebra {
x + rhs.x, y + rhs.y, z + rhs.z,w + rhs.w x + rhs.x, y + rhs.y, z + rhs.z,w + rhs.w
}; };
} }
Matrix4x4 Quaternion::ToMatrix4x4() const {
return Matrix4x4(*this);
}
Matrix4x4 Quaternion::ToMatrix4x4(const Vector3 &translation) const {
return {*this, translation};
}
} }

View File

@@ -8,7 +8,7 @@ namespace LinearAlgebra {
Vector2 Transform2D::Transform(const Vector2 &input) const { Vector2 Transform2D::Transform(const Vector2 &input) const {
return transformation * input; return transformation.Transform(input);
} }
Transform2D::Transform2D(const Matrix3x3 &transform) : transformation(transform) { } Transform2D::Transform2D(const Matrix3x3 &transform) : transformation(transform) { }

View File

@@ -22,7 +22,6 @@ namespace LinearAlgebra {
if (index == 1) return y; if (index == 1) return y;
return 0; return 0;
} }
bool Vector2::IsWithinMarginOfError(const Vector2& rhs, float margin) const bool Vector2::IsWithinMarginOfError(const Vector2& rhs, float margin) const
{ {
return this->Distance(rhs) <= margin; return this->Distance(rhs) <= margin;
@@ -247,5 +246,11 @@ namespace LinearAlgebra {
return {this->x*v.x, this->y*v.y}; return {this->x*v.x, this->y*v.y};
} }
bool Vector2::IsFinite() const {
return std::isfinite(x) && std::isfinite(y);
}
Vector2 Vector2::Div(const Vector2 &v) const {
return {this->x/v.x, this->y/v.y};
}
} }