diff --git a/include/J3ML/Geometry/OBB.h b/include/J3ML/Geometry/OBB.h index 0fc43f3..1146e0a 100644 --- a/include/J3ML/Geometry/OBB.h +++ b/include/J3ML/Geometry/OBB.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include diff --git a/include/J3ML/Geometry/QuadTree.h b/include/J3ML/Geometry/QuadTree.h index fa42e35..2e3803a 100644 --- a/include/J3ML/Geometry/QuadTree.h +++ b/include/J3ML/Geometry/QuadTree.h @@ -6,10 +6,21 @@ #include "AABB2D.h" namespace Geometry { + + using LinearAlgebra::Vector2; template 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: struct Node { Node *parent; diff --git a/include/J3ML/Geometry/Ray.h b/include/J3ML/Geometry/Ray.h index 0e94161..a5cc415 100644 --- a/include/J3ML/Geometry/Ray.h +++ b/include/J3ML/Geometry/Ray.h @@ -8,6 +8,7 @@ namespace Geometry { + using LinearAlgebra::Vector3; class Ray { Vector3 Origin; diff --git a/include/J3ML/LinearAlgebra/Quaternion.h b/include/J3ML/LinearAlgebra/Quaternion.h index 89713ff..284763c 100644 --- a/include/J3ML/LinearAlgebra/Quaternion.h +++ b/include/J3ML/LinearAlgebra/Quaternion.h @@ -8,51 +8,64 @@ namespace LinearAlgebra { - class Quaternion : public Vector4 - { + class Quaternion : public Vector4 { public: Quaternion(); - Quaternion(const Quaternion& rhs) = default; - explicit Quaternion(const Matrix3x3& rotationMtrx); - explicit Quaternion(const Matrix4x4& rotationMtrx); + + Quaternion(const Quaternion &rhs) = default; + + 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. 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 // @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 Vector4& rotationAxis, float rotationAngleBetween) { SetFromAxisAngle(rotationAxis, rotationAngleBetween); } + Quaternion(const Vector3 &rotationAxis, float rotationAngleBetween) { + SetFromAxisAngle(rotationAxis, rotationAngleBetween); + } + + Quaternion(const Vector4 &rotationAxis, float rotationAngleBetween) { + SetFromAxisAngle(rotationAxis, rotationAngleBetween); + } //void Inverse(); explicit Quaternion(Vector4 vector4); void SetFromAxisAngle(const Vector3 &vector3, float between); + void SetFromAxisAngle(const Vector4 &vector4, float between); Quaternion Inverse() const; + Quaternion Conjugate() const; //void Normalize(); Vector3 GetWorldX() const; + Vector3 GetWorldY() const; + Vector3 GetWorldZ() const; - Vector3 GetAxis() const - { + Vector3 GetAxis() const { float rcpSinAngle = 1 - (std::sqrt(1 - w * w)); return Vector3(x, y, z) * rcpSinAngle; } - float GetAngle() const - { + float GetAngle() const { return std::acos(w) * 2.f; } - Matrix3x3 ToMatrix3x3() const; + Matrix4x4 ToMatrix4x4() const; + + Matrix4x4 ToMatrix4x4(const Vector3 &translation) const; + Vector3 Transform(const Vector3& vec) 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 @@ -89,7 +102,7 @@ namespace LinearAlgebra Quaternion operator - () 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; diff --git a/src/J3ML/LinearAlgebra/Quaternion.cpp b/src/J3ML/LinearAlgebra/Quaternion.cpp index b7de362..6fd2667 100644 --- a/src/J3ML/LinearAlgebra/Quaternion.cpp +++ b/src/J3ML/LinearAlgebra/Quaternion.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include namespace LinearAlgebra { @@ -163,4 +164,12 @@ namespace LinearAlgebra { 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}; + } } \ No newline at end of file diff --git a/src/J3ML/LinearAlgebra/Transform2D.cpp b/src/J3ML/LinearAlgebra/Transform2D.cpp index 8705120..151aa28 100644 --- a/src/J3ML/LinearAlgebra/Transform2D.cpp +++ b/src/J3ML/LinearAlgebra/Transform2D.cpp @@ -8,7 +8,7 @@ namespace LinearAlgebra { Vector2 Transform2D::Transform(const Vector2 &input) const { - return transformation * input; + return transformation.Transform(input); } Transform2D::Transform2D(const Matrix3x3 &transform) : transformation(transform) { } diff --git a/src/J3ML/LinearAlgebra/Vector2.cpp b/src/J3ML/LinearAlgebra/Vector2.cpp index b24294d..5949c05 100644 --- a/src/J3ML/LinearAlgebra/Vector2.cpp +++ b/src/J3ML/LinearAlgebra/Vector2.cpp @@ -22,7 +22,6 @@ namespace LinearAlgebra { if (index == 1) return y; return 0; } - bool Vector2::IsWithinMarginOfError(const Vector2& rhs, float margin) const { return this->Distance(rhs) <= margin; @@ -247,5 +246,11 @@ namespace LinearAlgebra { 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}; + } } \ No newline at end of file