ソースを参照

Refactor and cleanup math

Kajetan Johannes Hammerle 2 週間 前
コミット
d8c126c771

+ 0 - 1
CMakeLists.txt

@@ -12,7 +12,6 @@ set(SRC
     "src/Clock.cpp"
     "src/Random.cpp"
     "src/BitArray.cpp"
-    "src/Math.cpp"
     "src/Vector.cpp"
     "src/Quaternion.cpp"
     "src/Matrix.cpp"

+ 3 - 6
include/core/math/Frustum.hpp

@@ -25,12 +25,9 @@ namespace Core {
         bool isInside(const Vector3& pos, float radius) const;
 
         void toString(BufferString& s) const {
-            s.append("(tan = ");
-            s.append(tan);
-            s.append(", nearClip = ");
-            s.append(nearClip);
-            s.append(", farClip = ");
-            s.append(farClip);
+            s.append("(tan = ").append(tan);
+            s.append(", nearClip = ").append(nearClip);
+            s.append(", farClip = ").append(farClip);
             s.append(')');
         }
     };

+ 0 - 7
include/core/math/Math.hpp

@@ -56,11 +56,6 @@ namespace Core::Math {
 
     constexpr float PI = 3.14159265358979323846f;
 
-    float sin(float f);
-    float cos(float f);
-    float tan(float f);
-    void sinCos(float a, float& s, float& c);
-
     constexpr float radianToDegree(float radians) {
         return radians * (180.0f / PI);
     }
@@ -68,8 +63,6 @@ namespace Core::Math {
     constexpr float degreeToRadian(float degrees) {
         return degrees * (PI / 180.0f);
     }
-
-    float squareRoot(float f);
 }
 
 #endif

+ 1 - 17
include/core/math/Matrix.hpp

@@ -10,41 +10,25 @@ namespace Core {
 
     public:
         Matrix();
-
         Matrix& unit();
-
         Matrix& set(size_t index, const Vector4& v);
-
         Matrix transpose();
-
         const float* getValues() const;
-
         Matrix& operator*=(const Matrix& other);
         Matrix operator*(const Matrix& other) const;
         Vector3 operator*(const Vector3& v) const;
-
         Matrix& scale(const Vector3& v);
         Matrix& scale(float f);
-
         Matrix& translate(const Vector3& v);
         Matrix& translateX(float tx);
         Matrix& translateY(float ty);
         Matrix& translateZ(float tz);
         Matrix& translateTo(const Vector3& v);
-
         Matrix& rotateX(float degrees);
         Matrix& rotateY(float degrees);
         Matrix& rotateZ(float degrees);
         Matrix& rotate(const Quaternion& q);
-
-        void toString(BufferString& s) const {
-            s.append('[');
-            s.append(data[0]).append(", ");
-            s.append(data[1]).append(", ");
-            s.append(data[2]).append(", ");
-            s.append(data[3]);
-            s.append("]");
-        }
+        void toString(BufferString& s) const;
 
     private:
         Matrix& rotate(float degrees, int a, int b);

+ 1 - 12
include/core/math/Plane.hpp

@@ -13,18 +13,7 @@ namespace Core {
         Plane();
         Plane(const Vector3& a, const Vector3& b, const Vector3& c);
         float getSignedDistance(const Vector3& v) const;
-
-        void toString(BufferString& s) const {
-            s.append("(");
-            s.append(abc[0]);
-            s.append(" x + ");
-            s.append(abc[1]);
-            s.append(" y + ");
-            s.append(abc[2]);
-            s.append(" z + ");
-            s.append(d);
-            s.append(')');
-        }
+        void toString(BufferString& s) const;
     };
 }
 

+ 1 - 13
include/core/math/Quaternion.hpp

@@ -12,23 +12,11 @@ namespace Core {
     public:
         Quaternion();
         Quaternion(const Vector3& axis, float angle);
-
         Quaternion lerp(float f, const Quaternion& other) const;
         Quaternion& operator*=(const Quaternion& other);
         Quaternion operator*(const Quaternion& other) const;
         Vector3 operator*(const Vector3& v) const;
-
-        void toString(BufferString& s) const {
-            s.append("(");
-            s.append(xyz[0]);
-            s.append(" i + ");
-            s.append(xyz[1]);
-            s.append(" j + ");
-            s.append(xyz[2]);
-            s.append(" k + ");
-            s.append(w);
-            s.append(')');
-        }
+        void toString(BufferString& s) const;
     };
 }
 

+ 12 - 9
include/core/math/Vector.hpp

@@ -1,6 +1,8 @@
 #ifndef CORE_VECTOR_HPP
 #define CORE_VECTOR_HPP
 
+#include <math.h>
+
 #include "core/math/Math.hpp"
 #include "core/utils/ArrayString.hpp"
 
@@ -126,7 +128,7 @@ namespace Core {
         }
 
         float length() const {
-            return Math::squareRoot(static_cast<float>(squareLength()));
+            return sqrtf(static_cast<float>(squareLength()));
         }
 
         Vector& normalize() {
@@ -146,20 +148,21 @@ namespace Core {
             return values[index];
         }
 
-        Vector<N, int> toInt() const {
-            Vector<N, int> cast;
+        template<typename C>
+        Vector<N, C> to() const {
+            Vector<N, C> cast;
             for(size_t i = 0; i < N; i++) {
-                cast[i] = static_cast<int>(values[i]);
+                cast[i] = static_cast<C>(values[i]);
             }
             return cast;
         }
 
+        Vector<N, int> toInt() const {
+            return to<int>();
+        }
+
         Vector<N, float> toFloat() const {
-            Vector<N, float> cast;
-            for(size_t i = 0; i < N; i++) {
-                cast[i] = static_cast<float>(values[i]);
-            }
-            return cast;
+            return to<float>();
         }
 
         void toString(BufferString& s) const {

+ 0 - 1
include/core/math/View.hpp

@@ -14,7 +14,6 @@ namespace Core {
         void updateDirections(float lengthAngle, float widthAngle);
         void updateDirections(const Quaternion& q);
         const Matrix& updateMatrix(const Vector3& pos);
-
         Vector3 getUp() const;
         Vector3 getDown() const;
         Vector3 getLeft() const;

+ 1 - 1
src/Frustum.cpp

@@ -2,7 +2,7 @@
 
 Core::Frustum::Frustum(float fieldOfView, float nearClip_, float farClip_)
     : projection(), planes(),
-      tan(Core::Math::tan(Core::Math::degreeToRadian(fieldOfView) * 0.5f)),
+      tan(tanf(Core::Math::degreeToRadian(fieldOfView) * 0.5f)),
       nearClip(nearClip_), farClip(farClip_) {
     float diff = 1.0f / (nearClip - farClip);
     projection.set(1, Vector4(0.0f, 1.0f / tan, 0.0f, 0.0f));

+ 0 - 28
src/Math.cpp

@@ -1,28 +0,0 @@
-#include "core/math/Math.hpp"
-
-#include <math.h>
-
-float Core::Math::sin(float f) {
-    return sinf(f);
-}
-
-float Core::Math::cos(float f) {
-    return cosf(f);
-}
-
-float Core::Math::tan(float f) {
-    return tanf(f);
-}
-
-void Core::Math::sinCos(float a, float& s, float& c) {
-#ifdef _GNU_SOURCE
-    sincosf(a, &s, &c);
-#else
-    s = sinf(a);
-    c = cosf(a);
-#endif
-}
-
-float Core::Math::squareRoot(float f) {
-    return sqrtf(f);
-}

+ 10 - 1
src/Matrix.cpp

@@ -94,7 +94,7 @@ Core::Matrix& Core::Matrix::translateTo(const Vector3& v) {
 Core::Matrix& Core::Matrix::rotate(float degrees, int a, int b) {
     float sin = 0.0f;
     float cos = 0.0f;
-    Math::sinCos(Core::Math::degreeToRadian(degrees), sin, cos);
+    sincosf(Core::Math::degreeToRadian(degrees), &sin, &cos);
     Vector4 v = data[a];
     data[a] = cos * data[a] - sin * data[b];
     data[b] = sin * v + cos * data[b];
@@ -123,3 +123,12 @@ Core::Matrix& Core::Matrix::rotate(const Quaternion& q) {
     set(2, Vector4(a[2], b[2], c[2], d[2]));
     return *this;
 }
+
+void Core::Matrix::toString(BufferString& s) const {
+    s.append('[');
+    s.append(data[0]).append(", ");
+    s.append(data[1]).append(", ");
+    s.append(data[2]).append(", ");
+    s.append(data[3]);
+    s.append("]");
+}

+ 9 - 0
src/Plane.cpp

@@ -10,3 +10,12 @@ Core::Plane::Plane(const Vector3& a, const Vector3& b, const Vector3& c)
 float Core::Plane::getSignedDistance(const Vector3& v) const {
     return abc.dot(v) + d;
 }
+
+void Core::Plane::toString(BufferString& s) const {
+    s.append("(");
+    s.append(abc[0]).append(" x + ");
+    s.append(abc[1]).append(" y + ");
+    s.append(abc[2]).append(" z + ");
+    s.append(d);
+    s.append(')');
+}

+ 11 - 3
src/Quaternion.cpp

@@ -7,7 +7,7 @@ Core::Quaternion::Quaternion(const Vector3& axis, float angle)
     : xyz(axis), w(1.0f) {
     xyz.normalize();
     float factor = 0.0f;
-    Core::Math::sinCos(Core::Math::degreeToRadian(angle) * 0.5f, factor, w);
+    sincosf(Core::Math::degreeToRadian(angle) * 0.5f, &factor, &w);
     xyz *= factor;
 }
 
@@ -16,8 +16,7 @@ Core::Quaternion Core::Quaternion::lerp(float f,
     Quaternion q;
     q.xyz = xyz * (1.0f - f) + other.xyz * f;
     q.w = w * (1.0f - f) + other.w * f;
-    float iLength =
-        1.0f / Core::Math::squareRoot(q.xyz.squareLength() + q.w * q.w);
+    float iLength = 1.0f / sqrtf(q.xyz.squareLength() + q.w * q.w);
     q.xyz *= iLength;
     q.w *= iLength;
     return q;
@@ -41,3 +40,12 @@ Core::Vector3 Core::Quaternion::operator*(const Vector3& v) const {
     Vector3 qvq = xyz * xyz.dot(v) + qv * w - qv.cross(xyz);
     return qvq;
 }
+
+void Core::Quaternion::toString(BufferString& s) const {
+    s.append("(");
+    s.append(xyz[0]).append(" i + ");
+    s.append(xyz[1]).append(" j + ");
+    s.append(xyz[2]).append(" k + ");
+    s.append(w);
+    s.append(')');
+}

+ 2 - 2
src/Vector.cpp

@@ -4,11 +4,11 @@ template<>
 Core::Vector3& Core::Vector3::setAngles(float lengthAngle, float widthAngle) {
     float sWidth = 0.0f;
     float cWidth = 0.0f;
-    Core::Math::sinCos(Math::degreeToRadian(widthAngle), sWidth, cWidth);
+    sincosf(Math::degreeToRadian(widthAngle), &sWidth, &cWidth);
 
     float sLength = 0.0f;
     float cLength = 0.0f;
-    Core::Math::sinCos(Math::degreeToRadian(lengthAngle), sLength, cLength);
+    sincosf(Math::degreeToRadian(lengthAngle), &sLength, &cLength);
 
     return *this = Vector3(cWidth * cLength, sWidth, -sLength * cWidth);
 }

+ 0 - 11
test/modules/MathTests.cpp

@@ -67,16 +67,6 @@ static void testClamp() {
     CORE_TEST_EQUAL(10, CMath::clamp(20, 10, 5));
 }
 
-static void testSinCos() {
-    for(float f = -10.0f; f < 10.0f; f += 0.1f) {
-        float rSin = 0.0f;
-        float rCos = 0.0f;
-        CMath::sinCos(f, rSin, rCos);
-        CORE_TEST_FLOAT(CMath::sin(f), rSin, eps);
-        CORE_TEST_FLOAT(CMath::cos(f), rCos, eps);
-    }
-}
-
 static void testRadianToDegree() {
     CORE_TEST_FLOAT(45.0f, CMath::radianToDegree(CMath::PI * 0.25f), eps);
     CORE_TEST_FLOAT(90.0f, CMath::radianToDegree(CMath::PI * 0.5f), eps);
@@ -98,7 +88,6 @@ void Core::testMath() {
     testMin();
     testMax();
     testClamp();
-    testSinCos();
     testRadianToDegree();
     testDegreeToRadian();
 }

+ 1 - 1
test/modules/VectorTests.cpp

@@ -33,7 +33,7 @@ static void testInitAndRead() {
 }
 
 static void testSetAngles() {
-    float root = Core::Math::squareRoot(2.0f) * 0.5f;
+    float root = sqrtf(2.0f) * 0.5f;
     CORE_TEST_VECTOR(V3(1.0f, 0.0f, 0.0f), V3().setAngles(0.0f, 0.0f));
     CORE_TEST_VECTOR(V3(root, 0.0f, -root), V3().setAngles(45.0f, 0.0f));
     CORE_TEST_VECTOR(V3(0.0f, 0.0f, -1.0f), V3().setAngles(90.0f, 0.0f));