diff --git a/CMakeLists.txt b/CMakeLists.txt index 5317f0f..347d5f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,7 +61,9 @@ add_library(J3ML SHARED ${J3ML_SRC} include/J3ML/Algorithm/RNG.h src/J3ML/Algorithm/RNG.cpp include/J3ML/Algorithm/Spring.h - include/J3ML/Algorithm/DifferentialSolvers.h) + include/J3ML/Algorithm/DifferentialSolvers.h + include/J3ML/Units.h + src/J3ML/J3ML.cpp) set_target_properties(J3ML PROPERTIES LINKER_LANGUAGE CXX) install(TARGETS ${PROJECT_NAME} DESTINATION lib/${PROJECT_NAME}) diff --git a/include/J3ML/Algorithm/DifferentialSolvers.h b/include/J3ML/Algorithm/DifferentialSolvers.h index d471100..c4b9c07 100644 --- a/include/J3ML/Algorithm/DifferentialSolvers.h +++ b/include/J3ML/Algorithm/DifferentialSolvers.h @@ -15,6 +15,12 @@ namespace J3ML::Algorithm return (x + y + x*y); } + // Accelleration = Velocity / Time + // Velocity = Position / Time + // Position = Vector3 + + // + float euler(float x0, float y, float h, float x) { float temp = -0.f; diff --git a/include/J3ML/Geometry/AABB.h b/include/J3ML/Geometry/AABB.h index 60982d3..a434c71 100644 --- a/include/J3ML/Geometry/AABB.h +++ b/include/J3ML/Geometry/AABB.h @@ -2,7 +2,7 @@ #include -#include +#include "J3ML/LinearAlgebra.h" #include #include diff --git a/include/J3ML/Geometry/Plane.h b/include/J3ML/Geometry/Plane.h index ab7d9ea..b7f1994 100644 --- a/include/J3ML/Geometry/Plane.h +++ b/include/J3ML/Geometry/Plane.h @@ -13,6 +13,5 @@ namespace J3ML::Geometry Vector3 Position; Vector3 Normal; float distance = 0.f; - }; } \ No newline at end of file diff --git a/include/J3ML/Geometry/Sphere.h b/include/J3ML/Geometry/Sphere.h index 386484e..7bad952 100644 --- a/include/J3ML/Geometry/Sphere.h +++ b/include/J3ML/Geometry/Sphere.h @@ -1,15 +1,70 @@ #pragma once -#include "J3ML/Geometry.h" +#include +#include +#include +#include +#include namespace J3ML::Geometry { + using J3ML::LinearAlgebra::Matrix3x3; + using J3ML::LinearAlgebra::Matrix4x4; + + // A mathematical representation of a 3-dimensional sphere class Sphere { public: - Sphere(const Vector3& pos, float radius) + Vector3 Position; + float Radius; + Sphere() {} + Sphere(const Vector3& pos, float radius) : Position(pos), Radius(radius) {} + void Translate(const Vector3& offset) + { + Position = Position + offset; + } + void Transform(const Matrix3x3& transform) + { + Position = transform * Position; + } + void Transform(const Matrix4x4& transform) + { + Position = transform * Position; + } + inline float Cube(float inp) const + { + return inp*inp*inp; + } + float Volume() const + { + return 4.f * M_PI * Cube(Radius) / 3.f; + } + float SurfaceArea() const + { + return 4.f * M_PI * Cube(Radius) / 3.f; + } + bool IsFinite() const + { + return Position.IsFinite() && std::isfinite(Radius); + } + bool IsDegenerate() + { + return !(Radius > 0.f) || !Position.IsFinite(); + } + bool Contains(const Vector3& point) const + { + return Position.DistanceSquared(point) <= Radius*Radius; + } + bool Contains(const Vector3& point, float epsilon) const + { + return Position.DistanceSquared(point) <= Radius*Radius + epsilon; + } + bool Contains(const LineSegment& lineseg) const { } + TriangleMesh GenerateUVSphere() const {} + TriangleMesh GenerateIcososphere() const {} + }; } \ No newline at end of file diff --git a/include/J3ML/J3ML.h b/include/J3ML/J3ML.h index 1bb06d6..0772bac 100644 --- a/include/J3ML/J3ML.h +++ b/include/J3ML/J3ML.h @@ -1,17 +1,241 @@ +#pragma once + + + // // Created by josh on 12/25/2023. // #include +#include +#include +#include +#include -namespace J3ML +namespace J3ML::SizedIntegralTypes { using u8 = uint8_t; using u16 = uint16_t; using u32 = uint32_t; using u64 = uint64_t; + using u128 = __uint128_t; using s8 = int8_t; using s16 = int16_t; using s32 = int32_t; using s64 = int64_t; + using s128 = __int128_t; +} + + + +namespace J3ML::SizedFloatTypes +{ + using f32 = float; + using f64 = double; + using f128 = long double; + +} + +using namespace J3ML::SizedIntegralTypes; +using namespace J3ML::SizedFloatTypes; + +namespace J3ML::Math +{ + + + // Coming soon: Units Namespace + // For Dimensional Analysis + /* + namespace Units + { + + struct Unit {}; + + struct Meters : public Unit { }; + struct ImperialInches : public Unit {}; + struct Time : public Unit {}; + struct Grams : public Unit {}; + + + struct MetersPerSecond : public Unit {}; + + + template + struct Quantity + { + public: + + float Value; + }; + + struct Mass : public Quantity {}; + struct Length : public Quantity { }; + + struct Velocity : public Quantity{ }; + + class MetrixPrefix + { + public: + std::string Prefix; + std::string Symbol; + int Power; + float InverseMultiply(float input) const + { + return std::pow(input, -Power); + } + float Multiply(float input) const + { + return std::pow(input, Power); + } + + }; + namespace Prefixes + { + static constexpr MetrixPrefix Tera {"tera", "T", 12}; + static constexpr MetrixPrefix Giga {"giga", "G", 9}; + static constexpr MetrixPrefix Mega {"mega", "M", 6}; + static constexpr MetrixPrefix Kilo {"kilo", "k", 3}; + static constexpr MetrixPrefix Hecto {"hecto", "h", 2}; + static constexpr MetrixPrefix Deca {"deca", "da", 1}; + static constexpr MetrixPrefix None {"", "", 0}; + static constexpr MetrixPrefix Deci {"", "", 0}; + static constexpr MetrixPrefix Centi {"", "", 0}; + static constexpr MetrixPrefix Milli {"", "", 0}; + static constexpr MetrixPrefix Micro {"", "", 0}; + static constexpr MetrixPrefix Nano {"", "", 0}; + static constexpr MetrixPrefix Pico {"", "", 0}; + } + + Length operator ""_meters(long double value) + { + return {(float)value}; + } + Length operator ""_m(long double value) + { + return {(float)value}; + } + constexpr Length operator ""_kilometers(long double value) + { + return Length {(float)value}; + } + Length operator ""_km(long double value) + { + return {(float)value}; + } + Length operator ""_centimeters(long double value) + { + return {(float)value}; + } + Length operator ""_cm(long double value) + { + return {(float)value}; + } + + Length operator ""_millimeters(long double value) + { + return {(float)value}; + } + Length operator ""_mm(long double value) + { + return {(float)value}; + } + + Velocity operator ""_mps(long double value) + { + return {(float)value}; + } + + Velocity operator ""_meters_per_second(long double value) + { + return {(float)value}; + } + + Velocity operator ""_kmps(long double value) + { + return {(float)value}; + } + }*/ + + +#pragma region Constants + static const float Pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679f; + static const float RecipSqrt2Pi = 0.3989422804014326779399460599343818684758586311649346576659258296706579258993018385012523339073069364f; + static const float GoldenRatio = 1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911375f; +#pragma endregion + +#pragma region Math Functions + + inline float Radians(float degrees); + inline float Degrees(float radians); + + + struct NumberRange + { + float LowerBound; + float UpperBound; + }; + + + float NormalizeToRange(float input, float fromLower, float fromUpper, float toLower, float toUpper); + float NormalizeToRange(float input, const NumberRange& from, const NumberRange& to); + // auto rotation_normalized = NormalizeToRange(inp, {0, 360}, {-1, 1}); + + inline float Lerp(float a, float b, float t); + /// Linearly interpolates from a to b, under the modulus mod. + /// This function takes evaluates a and b in the range [0, mod] and takes the shorter path to reach from a to b. + inline float LerpMod(float a, float b, float mod, float t); + /// Computes the lerp factor a and b have to be Lerp()ed to get x. + inline float InverseLerp(float a, float b, float x); + /// See http://msdn.microsoft.com/en-us/library/bb509665(v=VS.85).aspx + inline float Step(float y, float x); + /// See http://msdn.microsoft.com/en-us/library/bb509658(v=vs.85).aspx + inline float Ramp(float min, float max, float x); + + inline float PingPongMod(float x, float mod); + + inline float Sqrt(float x); + inline float FastSqrt(float x); + /// Returns 1/Sqrt(x). (The reciprocal of the square root of x) + inline float RSqrt(float x); + + inline float FastRSqrt(float x); + + +#pragma endregion + + + namespace BitTwiddling + { + /// Parses a string of form "011101010" to a u32 + u32 BinaryStringToValue(const char* s); + + /// Returns the number of 1's set in the given value. + inline int CountBitsSet(u32 value); + } + + namespace Interp + { + inline float SmoothStart(float t); + } + + struct Rotation + { + public: + Rotation(); + Rotation(float value); + float valueInRadians; + float ValueInRadians() const; + float ValueInDegrees() const; + Rotation operator+(const Rotation& rhs); + }; + + + Rotation operator ""_rad(long double rads); + + Rotation operator ""_radians(long double rads); + + Rotation operator ""_deg(long double rads); + + Rotation operator ""_degrees(long double rads); + } \ No newline at end of file diff --git a/include/J3ML/LinearAlgebra/Angle2D.h b/include/J3ML/LinearAlgebra/Angle2D.h index 31258cf..0a0c2e5 100644 --- a/include/J3ML/LinearAlgebra/Angle2D.h +++ b/include/J3ML/LinearAlgebra/Angle2D.h @@ -1,14 +1,22 @@ #pragma once -#include namespace J3ML::LinearAlgebra { + class Angle2D { public: float x; float y; + Angle2D(Math::Rotation rads); + Angle2D(float X, float Y) + { + x = X; + y = Y; + } + bool operator==(const Angle2D& rhs) const { return (this->x==rhs.x && this->y==rhs.y); } }; + } \ No newline at end of file diff --git a/include/J3ML/LinearAlgebra/AxisAngle.h b/include/J3ML/LinearAlgebra/AxisAngle.h index 0b4e697..1e54e1b 100644 --- a/include/J3ML/LinearAlgebra/AxisAngle.h +++ b/include/J3ML/LinearAlgebra/AxisAngle.h @@ -1,6 +1,8 @@ #pragma once -#include +#include +#include +#include #include namespace J3ML::LinearAlgebra diff --git a/include/J3ML/LinearAlgebra/Common.h b/include/J3ML/LinearAlgebra/Common.h new file mode 100644 index 0000000..74f245b --- /dev/null +++ b/include/J3ML/LinearAlgebra/Common.h @@ -0,0 +1,28 @@ +#pragma once + +// Forward Declarations for classes that include each other +namespace J3ML::LinearAlgebra +{ + class Vector2; // A type representing a position in a 2-dimensional coordinate space. + class Vector3; // A type representing a position in a 3-dimensional coordinate space. + class Vector4; // A type representing a position in a 4-dimensional coordinate space. + class Angle2D; // Uses x,y components to represent a 2D rotation. + class EulerAngle; // Uses pitch,yaw,roll components to represent a 3D orientation. + class AxisAngle; // + class CoordinateFrame; // + class Matrix2x2; + class Matrix3x3; + class Matrix4x4; + class Transform2D; + class Transform3D; + class Quaternion; + + + using Position = Vector3; +} + +// Methods required by LinearAlgebra types +namespace J3ML::LinearAlgebra +{ + +} \ No newline at end of file diff --git a/include/J3ML/LinearAlgebra/CoordinateFrame.h b/include/J3ML/LinearAlgebra/CoordinateFrame.h index f38f255..666b9d5 100644 --- a/include/J3ML/LinearAlgebra/CoordinateFrame.h +++ b/include/J3ML/LinearAlgebra/CoordinateFrame.h @@ -1,6 +1,6 @@ #pragma once -#include + #include namespace J3ML::LinearAlgebra diff --git a/include/J3ML/LinearAlgebra/EulerAngle.h b/include/J3ML/LinearAlgebra/EulerAngle.h index 54e2f22..54855f2 100644 --- a/include/J3ML/LinearAlgebra/EulerAngle.h +++ b/include/J3ML/LinearAlgebra/EulerAngle.h @@ -1,9 +1,13 @@ #pragma once -#include + #include +#include +#include namespace J3ML::LinearAlgebra { + class AxisAngle; + // Essential Reading: // http://www.essentialmath.com/GDC2012/GDC2012_JMV_Rotations.pdf class EulerAngle { diff --git a/include/J3ML/LinearAlgebra/Matrix2x2.h b/include/J3ML/LinearAlgebra/Matrix2x2.h index 1f7d2b7..e13d2da 100644 --- a/include/J3ML/LinearAlgebra/Matrix2x2.h +++ b/include/J3ML/LinearAlgebra/Matrix2x2.h @@ -1,6 +1,6 @@ #pragma once -#include + #include namespace J3ML::LinearAlgebra { diff --git a/include/J3ML/LinearAlgebra/Matrix3x3.h b/include/J3ML/LinearAlgebra/Matrix3x3.h index 28bb56a..ad0409c 100644 --- a/include/J3ML/LinearAlgebra/Matrix3x3.h +++ b/include/J3ML/LinearAlgebra/Matrix3x3.h @@ -1,11 +1,15 @@ #pragma once -#include + #include #include #include namespace J3ML::LinearAlgebra { + + + class Quaternion; + /// A 3-by-3 matrix for linear transformations of 3D geometry. /* This can represent any kind of linear transformations of 3D geometry, which include * rotation, scale, shear, mirroring, and orthographic projection. diff --git a/include/J3ML/LinearAlgebra/Matrix4x4.h b/include/J3ML/LinearAlgebra/Matrix4x4.h index a8dad01..33df237 100644 --- a/include/J3ML/LinearAlgebra/Matrix4x4.h +++ b/include/J3ML/LinearAlgebra/Matrix4x4.h @@ -1,8 +1,14 @@ #pragma once -#include +#include + +#include #include + +#include +#include + namespace J3ML::LinearAlgebra { /// A 4-by-4 matrix for affine transformations and perspective projections of 3D geometry. diff --git a/include/J3ML/LinearAlgebra/Quaternion.h b/include/J3ML/LinearAlgebra/Quaternion.h index 7c09e7d..a1277e3 100644 --- a/include/J3ML/LinearAlgebra/Quaternion.h +++ b/include/J3ML/LinearAlgebra/Quaternion.h @@ -1,13 +1,22 @@ #pragma once -#include + + +#include +#include #include #include -#include +//#include +#include + + namespace J3ML::LinearAlgebra { + + class Matrix3x3; + class Quaternion : public Vector4 { public: Quaternion(); @@ -23,13 +32,9 @@ namespace J3ML::LinearAlgebra // 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 Vector3 &rotationAxis, float rotationAngleBetween); - Quaternion(const Vector4 &rotationAxis, float rotationAngleBetween) { - SetFromAxisAngle(rotationAxis, rotationAngleBetween); - } + Quaternion(const Vector4 &rotationAxis, float rotationAngleBetween); //void Inverse(); explicit Quaternion(Vector4 vector4); @@ -49,15 +54,9 @@ namespace J3ML::LinearAlgebra Vector3 GetWorldZ() const; - Vector3 GetAxis() const { - float rcpSinAngle = 1 - (std::sqrt(1 - w * w)); + Vector3 GetAxis() const; - return Vector3(x, y, z) * rcpSinAngle; - } - - float GetAngle() const { - return std::acos(w) * 2.f; - } + float GetAngle() const; Matrix3x3 ToMatrix3x3() const; @@ -102,7 +101,7 @@ namespace J3ML::LinearAlgebra Quaternion operator - () const; float Dot(const Quaternion &quaternion) const; - float Angle() const { return std::acos(w) * 2.f;} + float Angle() const { return acos(w) * 2.f;} float AngleBetween(const Quaternion& target) const; diff --git a/include/J3ML/LinearAlgebra/Transform2D.h b/include/J3ML/LinearAlgebra/Transform2D.h index bdf4c33..ad8a6ce 100644 --- a/include/J3ML/LinearAlgebra/Transform2D.h +++ b/include/J3ML/LinearAlgebra/Transform2D.h @@ -1,6 +1,6 @@ #pragma once -#include + #include namespace J3ML::LinearAlgebra { diff --git a/include/J3ML/LinearAlgebra/Vector2.h b/include/J3ML/LinearAlgebra/Vector2.h index bac8b27..6996c9a 100644 --- a/include/J3ML/LinearAlgebra/Vector2.h +++ b/include/J3ML/LinearAlgebra/Vector2.h @@ -1,6 +1,7 @@ +#pragma clang diagnostic push +#pragma ide diagnostic ignored "modernize-use-nodiscard" #pragma once #include -#include #include namespace J3ML::LinearAlgebra { @@ -9,11 +10,17 @@ namespace J3ML::LinearAlgebra { /// A 2D (x, y) ordered pair. class Vector2 { public: + + enum {Dimensions = 2}; + /// Default Constructor - Initializes values to zero Vector2(); /// Constructs a new Vector2 with the value (X, Y) Vector2(float X, float Y); - Vector2(float* xyPtr); + /// Constructs this float2 from a C array, to the value (data[0], data[1]). + explicit Vector2(const float* data); + // Constructs a new Vector2 with the value {scalar, scalar} + explicit Vector2(float scalar); Vector2(const Vector2& rhs); // Copy Constructor //Vector2(Vector2&&) = default; // Move Constructor @@ -29,10 +36,26 @@ namespace J3ML::LinearAlgebra { void SetX(float newX); void SetY(float newY); - float* ptr() - { - return &x; - } + /// Casts this float2 to a C array. + /** This function does not allocate new memory or make a copy of this float2. This function simply + returns a C pointer view to this data structure. Use ptr()[0] to access the x component of this float2 + and ptr()[1] to access the y component. + @note Since the returned pointer points to this class, do not dereference the pointer after this + float2 has been deleted. You should never store a copy of the returned pointer. + @note This function is provided for compatibility with other APIs which require raw C pointer access + to vectors. Avoid using this function in general, and instead always use the operator [] + or the At() function to access the elements of this vector by index. + @return A pointer to the first float element of this class. The data is contiguous in memory. + @see operator [](), At(). */ + float* ptr(); + const float *ptr() const; + + float operator[](std::size_t index) const; + float &operator[](std::size_t index); + + const float At(std::size_t index) const; + + float &At(std::size_t index); Vector2 Abs() const; @@ -42,8 +65,7 @@ namespace J3ML::LinearAlgebra { bool IsZero(float epsilonSq = 1e-6f) const; bool IsPerpendicular(const Vector2& other, float epsilonSq=1e-5f) const; - float operator[](std::size_t index) const; - float &operator[](std::size_t index); + bool operator == (const Vector2& rhs) const; bool operator != (const Vector2& rhs) const; @@ -145,8 +167,9 @@ namespace J3ML::LinearAlgebra { Vector2 operator +() const; // TODO: Implement Vector2 operator -() const; /// Assigns a vector to another - Vector2& operator+=(const Vector2& rhs); // Adds a vector to this vector, in-place. - Vector2& operator-=(const Vector2& rhs); // Subtracts a vector from this vector, in-place + Vector2 &operator =(const Vector2 &rhs); + Vector2& operator+=(const Vector2& rhs); + Vector2& operator-=(const Vector2& rhs); Vector2& operator*=(float scalar); Vector2& operator/=(float scalar); @@ -160,4 +183,5 @@ namespace J3ML::LinearAlgebra { { return rhs * lhs; } -} \ No newline at end of file +} +#pragma clang diagnostic pop \ No newline at end of file diff --git a/include/J3ML/LinearAlgebra/Vector3.h b/include/J3ML/LinearAlgebra/Vector3.h index 85e1d93..9a624fa 100644 --- a/include/J3ML/LinearAlgebra/Vector3.h +++ b/include/J3ML/LinearAlgebra/Vector3.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -12,6 +13,8 @@ namespace J3ML::LinearAlgebra { class Vector3 { public: + enum {Dimensions = 3}; + // Default Constructor - Initializes to zero Vector3(); // Constructs a new Vector3 with the value (X, Y, Z) @@ -19,7 +22,7 @@ public: Vector3(const Vector3& rhs); // Copy Constructor Vector3(Vector3&&) = default; // Move Constructor Vector3& operator=(const Vector3& rhs); - + explicit Vector3(const float* data); static const Vector3 Zero; static const Vector3 Up; @@ -32,17 +35,9 @@ public: static const Vector3 Infinity; static const Vector3 NegativeInfinity; - float* ptr() - { - return &x; - } + float* ptr(); - static void Orthonormalize(Vector3& a, Vector3& b) - { - a = a.Normalize(); - b = b - b.ProjectToNorm(a); - b = b.Normalize(); - } + static void Orthonormalize(Vector3& a, Vector3& b); Vector3 Abs() const; @@ -51,25 +46,14 @@ public: static Vector3 Direction(const Vector3 &rhs) ; - static void Orthonormalize(Vector3& a, Vector3& b, Vector3& c) - { - a = a.Normalize(); - b = b - b.ProjectToNorm(a); - b = b.Normalize(); - c = c - c.ProjectToNorm(a); - c = c - c.ProjectToNorm(b); - c = c.Normalize(); - } + static void Orthonormalize(Vector3& a, Vector3& b, Vector3& c); bool AreOrthonormal(const Vector3& a, const Vector3& b, float epsilon) { } - Vector3 ProjectToNorm(const Vector3& direction) - { - return direction * this->Dot(direction); - } + Vector3 ProjectToNorm(const Vector3& direction) const; float GetX() const; float GetY() const; @@ -88,10 +72,7 @@ public: bool operator == (const Vector3& rhs) const; bool operator != (const Vector3& rhs) const; - bool IsFinite() const - { - return std::isfinite(x) && std::isfinite(y) && std::isfinite(z); - } + bool IsFinite() const; Vector3 Min(const Vector3& min) const; static Vector3 Min(const Vector3& lhs, const Vector3& rhs); @@ -106,6 +87,9 @@ public: float Distance(const Vector3& to) const; static float Distance(const Vector3& from, const Vector3& to); + float DistanceSquared(const Vector3& to) const; + static float DistanceSquared(const Vector3& from, const Vector3& to); + float Length() const; static float Length(const Vector3& of); diff --git a/include/J3ML/LinearAlgebra/Vector4.h b/include/J3ML/LinearAlgebra/Vector4.h index 3f228ed..9e1008d 100644 --- a/include/J3ML/LinearAlgebra/Vector4.h +++ b/include/J3ML/LinearAlgebra/Vector4.h @@ -1,6 +1,7 @@ #pragma once -#include + +#include namespace J3ML::LinearAlgebra { class Vector4 { diff --git a/include/J3ML/Units.h b/include/J3ML/Units.h new file mode 100644 index 0000000..60e0933 --- /dev/null +++ b/include/J3ML/Units.h @@ -0,0 +1,19 @@ +#pragma once + +namespace J3ML::Units +{ + + template + class Rotation + { + T GetDegrees() const; + T GetRadians() const; + void SetDegrees(T val); + void SetRadians(T val); + + }; + + using Rotationf = Rotation; + using Rotationd = Rotation; + +} \ No newline at end of file diff --git a/src/J3ML/Algorithm/RNG.cpp b/src/J3ML/Algorithm/RNG.cpp index 29b3dec..4f475c0 100644 --- a/src/J3ML/Algorithm/RNG.cpp +++ b/src/J3ML/Algorithm/RNG.cpp @@ -4,7 +4,7 @@ namespace J3ML::Algorithm { - void RNG::Seed(J3ML::u32 seed, J3ML::u32 multiplier, J3ML::u32 increment, J3ML::u32 modulus) { + void RNG::Seed(u32 seed, u32 multiplier, u32 increment, u32 modulus) { // If we have a pure multiplicative RNG, then can't have 0 starting seed, since that would generate a stream of all zeroes if (seed == 0 && increment == 0) seed = 1; diff --git a/src/J3ML/Geometry/Frustum.cpp b/src/J3ML/Geometry/Frustum.cpp index cf2e9e2..894024a 100644 --- a/src/J3ML/Geometry/Frustum.cpp +++ b/src/J3ML/Geometry/Frustum.cpp @@ -1,4 +1,5 @@ #include +#include namespace J3ML::Geometry { diff --git a/src/J3ML/Geometry/Sphere.cpp b/src/J3ML/Geometry/Sphere.cpp index 3676de6..685088c 100644 --- a/src/J3ML/Geometry/Sphere.cpp +++ b/src/J3ML/Geometry/Sphere.cpp @@ -1 +1,6 @@ -#include \ No newline at end of file +#include + +namespace J3ML::Geometry +{ + +} \ No newline at end of file diff --git a/src/J3ML/LinearAlgebra.cpp b/src/J3ML/LinearAlgebra.cpp index f2a9ce8..113e577 100644 --- a/src/J3ML/LinearAlgebra.cpp +++ b/src/J3ML/LinearAlgebra.cpp @@ -1,4 +1,4 @@ -#include +#include "J3ML/LinearAlgebra.h" #include namespace LinearAlgebra { diff --git a/src/J3ML/LinearAlgebra/Quaternion.cpp b/src/J3ML/LinearAlgebra/Quaternion.cpp index 27ee94f..8fb9b53 100644 --- a/src/J3ML/LinearAlgebra/Quaternion.cpp +++ b/src/J3ML/LinearAlgebra/Quaternion.cpp @@ -47,7 +47,18 @@ namespace J3ML::LinearAlgebra { void Quaternion::SetFromAxisAngle(const Vector3 &axis, float angle) { float sinz, cosz; + sinz = std::sin(angle*0.5f); + cosz = std::cos(angle*0.5f); + x = axis.x * sinz; + y = axis.y * sinz; + z = axis.z * sinz; + w = cosz; + } + + void Quaternion::SetFromAxisAngle(const Vector4 &axis, float angle) + { + SetFromAxisAngle(Vector3(axis.x, axis.y, axis.z), angle); } Quaternion Quaternion::operator*(float scalar) const { @@ -172,4 +183,22 @@ namespace J3ML::LinearAlgebra { Matrix4x4 Quaternion::ToMatrix4x4(const Vector3 &translation) const { return {*this, translation}; } + + float Quaternion::GetAngle() const { + return std::acos(w) * 2.f; + } + + Vector3 Quaternion::GetAxis() const { + float rcpSinAngle = 1 - (std::sqrt(1 - w * w)); + + return Vector3(x, y, z) * rcpSinAngle; + } + + Quaternion::Quaternion(const Vector3 &rotationAxis, float rotationAngleBetween) { + SetFromAxisAngle(rotationAxis, rotationAngleBetween); + } + + Quaternion::Quaternion(const Vector4 &rotationAxis, float rotationAngleBetween) { + SetFromAxisAngle(rotationAxis, rotationAngleBetween); + } } \ No newline at end of file diff --git a/src/J3ML/LinearAlgebra/Vector3.cpp b/src/J3ML/LinearAlgebra/Vector3.cpp index 918cc72..89b8f0a 100644 --- a/src/J3ML/LinearAlgebra/Vector3.cpp +++ b/src/J3ML/LinearAlgebra/Vector3.cpp @@ -5,8 +5,6 @@ namespace J3ML::LinearAlgebra { - - const Vector3 Vector3::Zero = {0,0,0}; const Vector3 Vector3::Up = {0, -1, 0}; const Vector3 Vector3::Down = {0, 1, 0}; @@ -322,5 +320,38 @@ namespace J3ML::LinearAlgebra { return {std::abs(x), std::abs(y), std::abs(z)}; } + float *Vector3::ptr() { + return &x; + } + + void Vector3::Orthonormalize(Vector3 &a, Vector3 &b) { + a = a.Normalize(); + b = b - b.ProjectToNorm(a); + b = b.Normalize(); + } + + void Vector3::Orthonormalize(Vector3 &a, Vector3 &b, Vector3 &c) { + a = a.Normalize(); + b = b - b.ProjectToNorm(a); + b = b.Normalize(); + c = c - c.ProjectToNorm(a); + c = c - c.ProjectToNorm(b); + c = c.Normalize(); + } + + Vector3 Vector3::ProjectToNorm(const Vector3 &direction) const { + return direction * this->Dot(direction); + } + + bool Vector3::IsFinite() const { + return std::isfinite(x) && std::isfinite(y) && std::isfinite(z); + } + + Vector3::Vector3(const float *data) { + x = data[0]; + y = data[1]; + z = data[2]; + } + } \ No newline at end of file