Sfoglia il codice sorgente

parameter packs for vector constructors

Kajetan Johannes Hammerle 3 anni fa
parent
commit
bdf34d2e66
4 ha cambiato i file con 24 aggiunte e 66 eliminazioni
  1. 1 40
      math/Vector.cpp
  2. 11 16
      math/Vector.h
  3. 4 4
      tests/MatrixTests.cpp
  4. 8 6
      tests/VectorTests.cpp

+ 1 - 40
math/Vector.cpp

@@ -2,45 +2,6 @@
 
 #include "math/Vector.h"
 
-template<>
-Vector<4>& Vector<4>::set(float x, float y, float z, float w) {
-    values[0] = x;
-    values[1] = y;
-    values[2] = z;
-    values[3] = w;
-    return *this;
-}
-
-template<>
-Vector<4>::Vector(float x, float y, float z, float w) {
-    set(x, y, z, w);
-}
-
-template<>
-Vector<3>& Vector<3>::set(float x, float y, float z) {
-    values[0] = x;
-    values[1] = y;
-    values[2] = z;
-    return *this;
-}
-
-template<>
-Vector<3>::Vector(float x, float y, float z) {
-    set(x, y, z);
-}
-
-template<>
-Vector<2>& Vector<2>::set(float x, float y) {
-    values[0] = x;
-    values[1] = y;
-    return *this;
-}
-
-template<>
-Vector<2>::Vector(float x, float y) {
-    set(x, y);
-}
-
 template<>
 Vector<3>& Vector<3>::setAngles(float lengthAngle, float widthAngle) {
     lengthAngle *= (M_PI / 180.0f);
@@ -54,7 +15,7 @@ Vector<3>& Vector<3>::setAngles(float lengthAngle, float widthAngle) {
     float cosLength;
     sincosf(lengthAngle, &sinLength, &cosLength);
 
-    return set(cosWidth * cosLength, sinWidth, -sinLength * cosWidth);
+    return *this = Vector<3>(cosWidth * cosLength, sinWidth, -sinLength * cosWidth);
 }
 
 template<>

+ 11 - 16
math/Vector.h

@@ -3,6 +3,8 @@
 
 #include "utils/StringBuffer.h"
 
+#include <initializer_list>
+
 template<int N>
 class Vector final {
     float values[N];
@@ -15,14 +17,15 @@ public:
         }
     }
 
-    /*template<typename... Args>
-    Vector(Args&&... args) {
-        args.
-    }*/
-
-    Vector(float, float) = delete;
-    Vector(float, float, float) = delete;
-    Vector(float, float, float, float) = delete;
+    template<typename... Args>
+    Vector(float a, Args&&... args) {
+        const int size = sizeof...(args) + 1;
+        float init[size] = {a, args...};
+        static_assert(N == size, "vector size and amount of float arguments does not match");
+        for(int i = 0; i < N; i++) {
+            values[i] = init[i];
+        }
+    }
 
     Vector& set(float, float) = delete;
     Vector& set(float, float, float) = delete;
@@ -126,14 +129,6 @@ Vector<N> operator*(float factor, const Vector<N>& v) {
     return v * factor;
 }
 
-template<> Vector<4>::Vector(float x, float y, float z, float w);
-template<> Vector<3>::Vector(float x, float y, float z);
-template<> Vector<2>::Vector(float x, float y);
-
-template<> Vector<4>& Vector<4>::set(float x, float y, float z, float w);
-template<> Vector<3>& Vector<3>::set(float x, float y, float z);
-template<> Vector<2>& Vector<2>::set(float x, float y);
-
 template<> Vector<3>& Vector<3>::setAngles(float lengthAngle, float widthAngle);
 template<> Vector<3> Vector<3>::cross(const Vector<3>& other) const;
 

+ 4 - 4
tests/MatrixTests.cpp

@@ -90,8 +90,8 @@ static void testCombination(Test& test) {
     m.translateX(1.0f);
     m.translateY(2.0f);
     m.translateZ(3.0f);
-    m.translate(Vector3(-4.0f, 2.0, 3.0f));
-    m.scale(Vector3(2.0f, 3.0, 4.0f));
+    m.translate(Vector3(-4.0f, 2.0f, 3.0f));
+    m.scale(Vector3(2.0f, 3.0f, 4.0f));
     m.scale(0.5f);
     compareVectors(test, Vector3(-1.0f, 9.0f, 16.0f), m * Vector3(1.0f, 1.0f, 1.0f), "combination");
 }
@@ -99,11 +99,11 @@ static void testCombination(Test& test) {
 static void testMatrixCombination(Test& test) {
     Matrix a;
     a.scale(2.0f);
-    a.translate(Vector3(1.0f, 2.0, 3.0f));
+    a.translate(Vector3(1.0f, 2.0f, 3.0f));
 
     Matrix b;
     b.scale(3.0f);
-    b.translate(Vector3(1.0f, 1.0, 1.0f));
+    b.translate(Vector3(1.0f, 1.0f, 1.0f));
 
     Matrix c;
     c.translate(Vector3(-1.0f, -2.0f, -3.0f));

+ 8 - 6
tests/VectorTests.cpp

@@ -31,6 +31,8 @@ static void testInitAndRead(Test& test) {
     test.checkEqual(7.0f, v4[1], "init 10");
     test.checkEqual(8.0f, v4[2], "init 11");
     test.checkEqual(9.0f, v4[3], "init 12");
+    
+    //v1.func(1.0f, 2.0f, 3.0f);
 }
 
 static void testSetAngles(Test& test) {
@@ -69,17 +71,17 @@ static void testSetAngles(Test& test) {
 
 static void testCross(Test& test) {
     compareVectors(test, Vector3(0.0f, 0.0f, 1.0f),
-            Vector3(1.0f, 0.0f, 0.0f).cross(Vector3(0.0f, 1.0f, 0.0)), "cross 1");
+            Vector3(1.0f, 0.0f, 0.0f).cross(Vector3(0.0f, 1.0f, 0.0f)), "cross 1");
     compareVectors(test, Vector3(0.0f, -1.0f, 0.0f),
-            Vector3(1.0f, 0.0f, 0.0f).cross(Vector3(0.0f, 0.0f, 1.0)), "cross 2");
+            Vector3(1.0f, 0.0f, 0.0f).cross(Vector3(0.0f, 0.0f, 1.0f)), "cross 2");
     compareVectors(test, Vector3(0.0f, 0.0f, -1.0f),
-            Vector3(0.0f, 1.0f, 0.0f).cross(Vector3(1.0f, 0.0f, 0.0)), "cross 3");
+            Vector3(0.0f, 1.0f, 0.0f).cross(Vector3(1.0f, 0.0f, 0.0f)), "cross 3");
     compareVectors(test, Vector3(1.0f, 0.0f, 0.0f),
-            Vector3(0.0f, 1.0f, 0.0f).cross(Vector3(0.0f, 0.0f, 1.0)), "cross 4");
+            Vector3(0.0f, 1.0f, 0.0f).cross(Vector3(0.0f, 0.0f, 1.0f)), "cross 4");
     compareVectors(test, Vector3(0.0f, 1.0f, 0.0f),
-            Vector3(0.0f, 0.0f, 1.0f).cross(Vector3(1.0f, 0.0f, 0.0)), "cross 5");
+            Vector3(0.0f, 0.0f, 1.0f).cross(Vector3(1.0f, 0.0f, 0.0f)), "cross 5");
     compareVectors(test, Vector3(-1.0f, 0.0f, 0.0f),
-            Vector3(0.0f, 0.0f, 1.0f).cross(Vector3(0.0f, 1.0f, 0.0)), "cross 6");
+            Vector3(0.0f, 0.0f, 1.0f).cross(Vector3(0.0f, 1.0f, 0.0f)), "cross 6");
 }
 
 static void testSetAdd(Test& test) {