Browse Source

removal of size class, int vectors

Kajetan Johannes Hammerle 2 years ago
parent
commit
9f4ed235c8

+ 4 - 4
math/Frustum.cpp

@@ -4,10 +4,10 @@ Frustum::Frustum(float fieldOfView, float nearClip, float farClip)
     : fieldOfView(fieldOfView), nearClip(nearClip), farClip(farClip) {
 }
 
-Matrix& Frustum::updateProjection(const Size& size) {
+Matrix& Frustum::updateProjection(const IntVector2& size) {
     float tan = tanf(fieldOfView * (0.5f * M_PI / 180.0f));
     float q = 1.0f / tan;
-    float aspect = static_cast<float>(size.width) / size.height;
+    float aspect = static_cast<float>(size[0]) / size[1];
     float diff = 1.0f / (nearClip - farClip);
 
     projection.set(0, Vector4(q / aspect, 0.0f, 0.0f, 0.0f));
@@ -20,9 +20,9 @@ Matrix& Frustum::updateProjection(const Size& size) {
 
 void Frustum::updatePlanes(const Vector3& pos, const Vector3& right,
                            const Vector3& up, const Vector3& front,
-                           const Size& size) {
+                           const IntVector2& size) {
     float tan = tanf(fieldOfView * (0.5f * M_PI / 180.0f));
-    float aspect = static_cast<float>(size.width) / size.height;
+    float aspect = static_cast<float>(size[0]) / size[1];
 
     float halfNearHeight = tan * nearClip;
     float halfNearWidth = halfNearHeight * aspect;

+ 2 - 3
math/Frustum.h

@@ -4,7 +4,6 @@
 #include "math/Matrix.h"
 #include "math/Plane.h"
 #include "utils/Array.h"
-#include "utils/Size.h"
 #include "utils/StringBuffer.h"
 
 class Frustum final {
@@ -17,10 +16,10 @@ public:
     float farClip;
 
     Frustum(float fieldOfView, float nearClip, float farClip);
-    Matrix& updateProjection(const Size& size);
+    Matrix& updateProjection(const IntVector2& size);
     void updatePlanes(const Vector3& pos, const Vector3& right,
                       const Vector3& up, const Vector3& front,
-                      const Size& size);
+                      const IntVector2& size);
 
     bool isInside(const Vector3& pos) const;
     bool isInside(const Vector3& pos, float radius) const;

+ 1 - 1
math/Plane.cpp

@@ -4,7 +4,7 @@ Plane::Plane() : d(0) {
 }
 
 Plane::Plane(const Vector3& a, const Vector3& b, const Vector3& c)
-    : abc(static_cast<Vector3>(b - a).cross(c - a).normalize()), d(-abc.dot(b)) {
+    : abc((b - a).cross(c - a).normalize()), d(-abc.dot(b)) {
 }
 
 float Plane::getSignedDistance(const Vector3& v) const {

+ 1 - 2
math/Quaternion.cpp

@@ -4,10 +4,9 @@ Quaternion::Quaternion() : w(1.0f) {
 }
 
 Quaternion::Quaternion(const Vector3& axis, float angle) : xyz(axis) {
-    angle *= (M_PI / 360.0f);
     xyz.normalize();
     float factor = 0.0f;
-    sincosf(angle, &factor, &w);
+    sincosf(angle * (M_PI / 360.0f), &factor, &w);
     xyz *= factor;
 }
 

+ 12 - 16
math/Vector.cpp

@@ -1,25 +1,21 @@
 #include "math/Vector.h"
 
 template<>
-Vector<3>& Vector<3>::setAngles(float lengthAngle, float widthAngle) {
-    lengthAngle *= (M_PI / 180.0f);
-    widthAngle *= (M_PI / 180.0f);
+Vector3& Vector3::setAngles(float lengthAngle, float widthAngle) {
+    float sWidth = 0.0f;
+    float cWidth = 0.0f;
+    sincosf(widthAngle * (M_PI / 180.0f), &sWidth, &cWidth);
 
-    float sinWidth = 0.0f;
-    float cosWidth = 0.0f;
-    sincosf(widthAngle, &sinWidth, &cosWidth);
+    float sLength = 0.0f;
+    float cLength = 0.0f;
+    sincosf(lengthAngle * (M_PI / 180.0f), &sLength, &cLength);
 
-    float sinLength = 0.0f;
-    float cosLength = 0.0f;
-    sincosf(lengthAngle, &sinLength, &cosLength);
-
-    return *this =
-               Vector<3>(cosWidth * cosLength, sinWidth, -sinLength * cosWidth);
+    return *this = Vector3(cWidth * cLength, sWidth, -sLength * cWidth);
 }
 
 template<>
-Vector<3> Vector<3>::cross(const Vector<3>& other) const {
-    return Vector<3>(values[1] * other.values[2] - values[2] * other.values[1],
-                     values[2] * other.values[0] - values[0] * other.values[2],
-                     values[0] * other.values[1] - values[1] * other.values[0]);
+Vector3 Vector3::cross(const Vector3& other) const {
+    return Vector3(values[1] * other.values[2] - values[2] * other.values[1],
+                   values[2] * other.values[0] - values[0] * other.values[2],
+                   values[0] * other.values[1] - values[1] * other.values[0]);
 }

+ 28 - 24
math/Vector.h

@@ -4,19 +4,19 @@
 #include "math/Math.h"
 #include "utils/StringBuffer.h"
 
-template<int N>
+template<int N, typename T>
 class Vector final {
-    float values[N];
+    T values[N];
 
 public:
     Vector() {
         for(int i = 0; i < N; i++) {
-            values[i] = 0.0f;
+            values[i] = static_cast<T>(0);
         }
     }
 
     template<typename... Args>
-    Vector(float a, Args&&... args) {
+    Vector(T a, Args&&... args) {
         init<0>(a, args...);
     }
 
@@ -26,7 +26,7 @@ private:
         static_assert(I == N, "vector parameters do not match its size");
     }
 
-    template<int I, typename T, typename... Args>
+    template<int I, typename... Args>
     void init(T a, Args&&... args) {
         values[I] = a;
         init<I + 1>(args...);
@@ -70,54 +70,54 @@ public:
         return v;
     }
 
-    Vector& operator*=(float factor) {
+    Vector& operator*=(T factor) {
         for(int i = 0; i < N; i++) {
             values[i] *= factor;
         }
         return *this;
     }
 
-    Vector& operator*=(const Vector<N>& other) {
+    Vector& operator*=(const Vector& other) {
         for(int i = 0; i < N; i++) {
             values[i] *= other.values[i];
         }
         return *this;
     }
 
-    Vector operator*(float factor) const {
+    Vector operator*(T factor) const {
         Vector v = *this;
         v *= factor;
         return v;
     }
 
-    Vector operator*(const Vector<N>& other) const {
+    Vector operator*(const Vector& other) const {
         Vector v = *this;
         v *= other;
         return v;
     }
 
-    Vector& operator/=(float factor) {
+    Vector& operator/=(T factor) {
         for(int i = 0; i < N; i++) {
             values[i] /= factor;
         }
         return *this;
     }
 
-    Vector operator/(float factor) const {
+    Vector operator/(T factor) const {
         Vector v = *this;
         v /= factor;
         return v;
     }
 
-    float dot(const Vector& v) const {
-        float length = 0.0f;
+    T dot(const Vector& v) const {
+        T length = 0.0f;
         for(int i = 0; i < N; i++) {
             length += values[i] * v.values[i];
         }
         return length;
     }
 
-    float squareLength() const {
+    T squareLength() const {
         return dot(*this);
     }
 
@@ -130,11 +130,11 @@ public:
         return *this;
     }
 
-    float& operator[](int index) {
+    T& operator[](int index) {
         return values[index];
     }
 
-    const float& operator[](int index) const {
+    const T& operator[](int index) const {
         return values[index];
     }
 
@@ -152,19 +152,23 @@ public:
     }
 };
 
-template<int N>
-Vector<N> operator*(float factor, const Vector<N>& v) {
+template<int N, typename T>
+Vector<N, T> operator*(T factor, const Vector<N, T>& v) {
     return v * factor;
 }
 
-template<>
-Vector<3>& Vector<3>::setAngles(float lengthAngle, float widthAngle);
+typedef Vector<4, float> Vector4;
+typedef Vector<3, float> Vector3;
+typedef Vector<2, float> Vector2;
+
+typedef Vector<4, int> IntVector4;
+typedef Vector<3, int> IntVector3;
+typedef Vector<2, int> IntVector2;
 
 template<>
-Vector<3> Vector<3>::cross(const Vector<3>& other) const;
+Vector3& Vector3::setAngles(float lengthAngle, float widthAngle);
 
-typedef Vector<4> Vector4;
-typedef Vector<3> Vector3;
-typedef Vector<2> Vector2;
+template<>
+Vector3 Vector3::cross(const Vector3& other) const;
 
 #endif

+ 0 - 1
meson.build

@@ -26,7 +26,6 @@ src = [
     'utils/Error.cpp',
     'utils/Logger.cpp',
     'utils/Random.cpp',
-    'utils/Size.cpp',
     'wrapper/GL.cpp',
     'libs/lodepng/lodepng.cpp',
 ]

+ 5 - 5
rendering/Window.cpp

@@ -10,7 +10,7 @@
 static GLFWwindow* window = nullptr;
 static Clock fps;
 static Clock tps;
-static Size size{0, 0};
+static IntVector2 size{0, 0};
 static bool sizeChanged = false;
 
 static List<uint32> input;
@@ -133,8 +133,8 @@ static void onChar(GLFWwindow*, uint32 codepoint) {
 
 static void onResize(GLFWwindow*, int width, int height) {
     sizeChanged = true;
-    size.width = width;
-    size.height = height;
+    size[0] = width;
+    size[1] = height;
 }
 
 static void onMouse(GLFWwindow*, int button, int action, int mods) {
@@ -168,7 +168,7 @@ Error Window::open(const WindowOptions& o) {
     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, o.minorVersion);
 
     GLFWmonitor* m = o.fullscreen ? glfwGetPrimaryMonitor() : nullptr;
-    window = glfwCreateWindow(o.size.width, o.size.height, o.name, m, nullptr);
+    window = glfwCreateWindow(o.size[0], o.size[1], o.name, m, nullptr);
     if(window == nullptr) {
         Error e{"could not create window"};
         addError(e);
@@ -211,7 +211,7 @@ float Window::getFramesPerSecond() {
     return fps.getUpdatesPerSecond();
 }
 
-const Size& Window::getSize() {
+const IntVector2& Window::getSize() {
     return size;
 }
 

+ 1 - 1
rendering/Window.h

@@ -22,7 +22,7 @@ namespace Window {
     void trapCursor();
     void freeCursor();
 
-    const Size& getSize();
+    const IntVector2& getSize();
     bool hasSizeChanged();
 
     void show();

+ 1 - 1
rendering/WindowOptions.cpp

@@ -1,7 +1,7 @@
 #include "rendering/WindowOptions.h"
 
 WindowOptions::WindowOptions(int majorVersion, int minorVersion,
-                             const Size& size, bool es, const char* name)
+                             const IntVector2& size, bool es, const char* name)
     : majorVersion(majorVersion), minorVersion(minorVersion), size(size),
       fullscreen(false), es(es), vsync(true), name(name) {
 }

+ 4 - 3
rendering/WindowOptions.h

@@ -1,18 +1,19 @@
 #ifndef WINDOWOPTIONS_H
 #define WINDOWOPTIONS_H
 
-#include "utils/Size.h"
+#include "math/Vector.h"
 
 struct WindowOptions final {
     int majorVersion;
     int minorVersion;
-    const Size& size;
+    const IntVector2& size;
     bool fullscreen;
     bool es;
     bool vsync;
     const char* name;
 
-    WindowOptions(int majorVersion, int minorVersion, const Size& size, bool es, const char* name);
+    WindowOptions(int majorVersion, int minorVersion, const IntVector2& size,
+                  bool es, const char* name);
 };
 
 #endif

+ 2 - 2
tests/FrustumTests.cpp

@@ -15,7 +15,7 @@ static void testToString(Test& test) {
 }
 
 static void testPointIsInside(Test& test) {
-    Size size(200, 100);
+    IntVector2 size(200, 100);
     Frustum f(60.0f, 0.1f, 1000.0f);
     f.updatePlanes(Vector3(0.0f, 0.0f, 0.0f), Vector3(1.0f, 0.0f, 0.0f),
                    Vector3(0.0f, 1.0f, 0.0f), Vector3(0.0f, 0.0f, 1.0f), size);
@@ -37,7 +37,7 @@ static void testPointIsInside(Test& test) {
 }
 
 static void testSphereIsInside(Test& test) {
-    Size size(200, 100);
+    IntVector2 size(200, 100);
     Frustum f(60.0f, 0.1f, 1000.0f);
     f.updatePlanes(Vector3(0.0f, 0.0f, 0.0f), Vector3(1.0f, 0.0f, 0.0f),
                    Vector3(0.0f, 1.0f, 0.0f), Vector3(0.0f, 0.0f, 1.0f), size);

+ 3 - 3
tests/MatrixTests.cpp

@@ -5,9 +5,9 @@
 
 const float eps = 0.0001f;
 
-template<int N>
-static void compareVectors(Test& test, const Vector<N>& wanted,
-                           const Vector<N>& actual, const char* text) {
+template<int N, typename T>
+static void compareVectors(Test& test, const Vector<N, T>& wanted,
+                           const Vector<N, T>& actual, const char* text) {
     for(int i = 0; i < N; i++) {
         test.checkFloat(
             wanted[i], actual[i], eps,

+ 35 - 18
tests/QuaternionTests.cpp

@@ -1,34 +1,40 @@
 #include "tests/QuaternionTests.h"
-#include "tests/Test.h"
 #include "math/Quaternion.h"
+#include "tests/Test.h"
 #include "utils/StringBuffer.h"
 
 typedef StringBuffer<100> String;
 
 const float eps = 0.0001f;
 
-template<int N>
-static void compareVectors(Test& test, const Vector<N>& wanted, const Vector<N>& actual, const char* text) {
+template<int N, typename T>
+static void compareVectors(Test& test, const Vector<N, T>& wanted,
+                           const Vector<N, T>& actual, const char* text) {
     for(int i = 0; i < N; i++) {
-        test.checkFloat(wanted[i], actual[i], eps, StringBuffer<50>(text).append(" (").append(i).append(")"));
+        test.checkFloat(
+            wanted[i], actual[i], eps,
+            StringBuffer<50>(text).append(" (").append(i).append(")"));
     }
 }
 
 static void testInit(Test& test) {
     Quaternion q;
-    test.checkEqual(String("(0.00 i + 0.00 j + 0.00 k + 1.00)"), String(q), "init");
+    test.checkEqual(String("(0.00 i + 0.00 j + 0.00 k + 1.00)"), String(q),
+                    "init");
 }
 
 static void testAxisAndDegreesInit(Test& test) {
     Quaternion q(Vector3(1.0f, 2.0f, 3.0f), 142.0f);
-    test.checkEqual(String("(0.25 i + 0.51 j + 0.76 k + 0.33)"), String(q), "init with axis and degrees");
+    test.checkEqual(String("(0.25 i + 0.51 j + 0.76 k + 0.33)"), String(q),
+                    "init with axis and degrees");
 }
 
 static void testLerp(Test& test) {
     Quaternion q1(Vector3(2.0f, 5.0f, 7.0f), 130.0f);
     Quaternion q2(Vector3(1.0f, 2.0f, 4.0f), 260.0f);
     Quaternion q3 = q1.lerp(0.3f, q2);
-    test.checkEqual(String("(0.22 i + 0.53 j + 0.81 k + 0.12)"), String(q3), "lerp");
+    test.checkEqual(String("(0.22 i + 0.53 j + 0.81 k + 0.12)"), String(q3),
+                    "lerp");
 }
 
 static void testMulSet(Test& test) {
@@ -38,14 +44,16 @@ static void testMulSet(Test& test) {
     q3 *= q1;
     test.checkEqual(String(q1), String(q3), "mul set 1");
     q3 *= q2;
-    test.checkEqual(String(Quaternion(Vector3(2.0f, 5.0f, 7.0f), 110.0f)), String(q3), "mul set 2");
+    test.checkEqual(String(Quaternion(Vector3(2.0f, 5.0f, 7.0f), 110.0f)),
+                    String(q3), "mul set 2");
 }
 
 static void testMul(Test& test) {
     Quaternion q1(Vector3(2.0f, 5.0f, 7.0f), 50.0f);
     Quaternion q2(Vector3(2.0f, 5.0f, 7.0f), 60.0f);
     Quaternion q3 = q1 * q2;
-    test.checkEqual(String(Quaternion(Vector3(2.0f, 5.0f, 7.0f), 110.0f)), String(q3), "mul");
+    test.checkEqual(String(Quaternion(Vector3(2.0f, 5.0f, 7.0f), 110.0f)),
+                    String(q3), "mul");
 }
 
 static void testMulVector(Test& test) {
@@ -57,17 +65,26 @@ static void testMulVector(Test& test) {
     Vector3 v2(0.0f, 1.0f, 0.0f);
     Vector3 v3(0.0f, 0.0f, 1.0f);
 
-    compareVectors(test, Vector3(1.0f, 0.0f, 0.0f), q1 * v1, "mul with vector 1");
-    compareVectors(test, Vector3(0.0f, 0.0f, 1.0f), q1 * v2, "mul with vector 2");
-    compareVectors(test, Vector3(0.0f, -1.0f, 0.0f), q1 * v3, "mul with vector 3");
+    compareVectors(test, Vector3(1.0f, 0.0f, 0.0f), q1 * v1,
+                   "mul with vector 1");
+    compareVectors(test, Vector3(0.0f, 0.0f, 1.0f), q1 * v2,
+                   "mul with vector 2");
+    compareVectors(test, Vector3(0.0f, -1.0f, 0.0f), q1 * v3,
+                   "mul with vector 3");
 
-    compareVectors(test, Vector3(0.0f, 0.0f, -1.0f), q2 * v1, "mul with vector 4");
-    compareVectors(test, Vector3(0.0f, 1.0f, 0.0f), q2 * v2, "mul with vector 5");
-    compareVectors(test, Vector3(1.0f, 0.0f, 0.0f), q2 * v3, "mul with vector 6");
+    compareVectors(test, Vector3(0.0f, 0.0f, -1.0f), q2 * v1,
+                   "mul with vector 4");
+    compareVectors(test, Vector3(0.0f, 1.0f, 0.0f), q2 * v2,
+                   "mul with vector 5");
+    compareVectors(test, Vector3(1.0f, 0.0f, 0.0f), q2 * v3,
+                   "mul with vector 6");
 
-    compareVectors(test, Vector3(0.0f, 1.0f, 0.0f), q3 * v1, "mul with vector 7");
-    compareVectors(test, Vector3(-1.0f, 0.0f, 0.0f), q3 * v2, "mul with vector 8");
-    compareVectors(test, Vector3(0.0f, 0.0f, 1.0f), q3 * v3, "mul with vector 9");
+    compareVectors(test, Vector3(0.0f, 1.0f, 0.0f), q3 * v1,
+                   "mul with vector 7");
+    compareVectors(test, Vector3(-1.0f, 0.0f, 0.0f), q3 * v2,
+                   "mul with vector 8");
+    compareVectors(test, Vector3(0.0f, 0.0f, 1.0f), q3 * v3,
+                   "mul with vector 9");
 }
 
 void QuaternionTests::test() {

+ 6 - 6
tests/VectorTests.cpp

@@ -5,9 +5,9 @@
 
 const float eps = 0.0001f;
 
-template<int N>
-static void compareVectors(Test& test, const Vector<N>& wanted,
-                           const Vector<N>& actual, const char* text) {
+template<int N, typename T>
+static void compareVectors(Test& test, const Vector<N, T>& wanted,
+                           const Vector<N, T>& actual, const char* text) {
     for(int i = 0; i < N; i++) {
         test.checkFloat(
             wanted[i], actual[i], eps,
@@ -232,9 +232,9 @@ static void testNormalize(Test& test) {
 
 static void testToString(Test& test) {
     StringBuffer<50> s;
-    s.append(Vector<1>())
-        .append(Vector<2>(2.0f, 3.0f))
-        .append(Vector<3>(4.0f, 5.0f, 6.0f));
+    s.append(Vector<1, float>())
+        .append(Vector2(2.0f, 3.0f))
+        .append(Vector3(4.0f, 5.0f, 6.0f));
     test.checkEqual(StringBuffer<50>("[0.00][2.00, 3.00][4.00, 5.00, 6.00]"), s,
                     "to string");
 }

+ 0 - 4
utils/Size.cpp

@@ -1,4 +0,0 @@
-#include "utils/Size.h"
-
-Size::Size(int width, int height) : width(width), height(height) {
-}

+ 0 - 10
utils/Size.h

@@ -1,10 +0,0 @@
-#ifndef SIZE_H
-#define SIZE_H
-
-struct Size final {
-    int width;
-    int height;
-    Size(int width, int height);
-};
-
-#endif