Browse Source

improved buffered value

Kajetan Johannes Hammerle 2 years ago
parent
commit
ca8c7fcbb9
3 changed files with 126 additions and 6 deletions
  1. 59 2
      math/BufferedValue.h
  2. 13 0
      math/Vector.h
  3. 54 4
      tests/BufferedValueTests.cpp

+ 59 - 2
math/BufferedValue.h

@@ -20,11 +20,68 @@ public:
         return Math::interpolate(last, current, lag);
     }
 
-    BufferedValue& operator=(const T& t) {
-        current = t;
+    template<typename O>
+    BufferedValue& operator=(const O& o) {
+        current = o;
         return *this;
     }
 
+    template<typename O>
+    BufferedValue& operator+=(const O& o) {
+        current += o;
+        return *this;
+    }
+
+    template<typename O>
+    BufferedValue& operator-=(const O& o) {
+        current -= o;
+        return *this;
+    }
+
+    template<typename O>
+    BufferedValue& operator*=(const O& o) {
+        current *= o;
+        return *this;
+    }
+
+    template<typename O>
+    BufferedValue& operator/=(const O& o) {
+        current /= o;
+        return *this;
+    }
+
+    template<typename O>
+    auto operator+(const O& o) {
+        return current + o;
+    }
+
+    template<typename O>
+    auto operator-(const O& o) {
+        return current - o;
+    }
+
+    template<typename O>
+    auto operator*(const O& o) {
+        return current * o;
+    }
+
+    template<typename O>
+    auto operator/(const O& o) {
+        return current / o;
+    }
+
+    auto operator-() {
+        return -current;
+    }
+
+    auto& operator[](int index) {
+        return current[index];
+    }
+
+    const auto& operator[](int index) const {
+        return current[index];
+    }
+
     operator T&() {
         return current;
     }

+ 13 - 0
math/Vector.h

@@ -103,12 +103,25 @@ public:
         return *this;
     }
 
+    Vector& operator/=(const Vector& other) {
+        for(int i = 0; i < N; i++) {
+            values[i] /= other.values[i];
+        }
+        return *this;
+    }
+
     Vector operator/(T factor) const {
         Vector v = *this;
         v /= factor;
         return v;
     }
 
+    Vector operator/(const Vector& other) const {
+        Vector v = *this;
+        v /= other;
+        return v;
+    }
+
     T dot(const Vector& v) const {
         T length = 0.0f;
         for(int i = 0; i < N; i++) {

+ 54 - 4
tests/BufferedValueTests.cpp

@@ -1,9 +1,20 @@
 #include "tests/BufferedValueTests.h"
 #include "math/BufferedValue.h"
+#include "math/Vector.h"
 #include "tests/Test.h"
 
 const float eps = 0.0001f;
 
+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<100>(text).append(" (").append(i).append(")"));
+    }
+}
+
 static void testInit(Test& test) {
     BufferedValue<float> b = 5.0f;
     test.checkFloat(5.0f, b.get(0.0f), eps, "init 1");
@@ -35,10 +46,48 @@ static void testCalculate(Test& test) {
     BufferedValue<float> b = 5.0f;
     b = 7.0f;
     b += 3.0f;
-    test.checkFloat(5.0f, b.get(0.0f), eps, "interpolate 1");
-    test.checkFloat(7.5f, b.get(0.5f), eps, "interpolate 2");
-    test.checkFloat(10.0f, b.get(1.0f), eps, "interpolate 3");
-    test.checkFloat(10.0f, b, eps, "interpolate 4");
+    test.checkFloat(5.0f, b.get(0.0f), eps, "calculate 1");
+    test.checkFloat(7.5f, b.get(0.5f), eps, "calculate 2");
+    test.checkFloat(10.0f, b.get(1.0f), eps, "calculate 3");
+    test.checkFloat(10.0f, b, eps, "calculate 4");
+    test.checkFloat(12.0f, b + 2.0f, eps, "calculate 5");
+}
+
+static void testVector(Test& test) {
+    Vector2 base(5.0f, 6.0f);
+    BufferedValue<Vector2> b = base;
+
+    compareVectors(test, base, b.get(1.0f), "vector 1");
+    b = Vector2(7.0f, 5.0);
+    compareVectors(test, Vector2(7.0f, 5.0f), b.get(1.0f), "vector 2");
+    b += Vector2(1.0f, 1.0f);
+    compareVectors(test, Vector2(8.0f, 6.0f), b.get(1.0f), "vector 3");
+    b -= Vector2(1.0f, 1.0f);
+    compareVectors(test, Vector2(7.0f, 5.0f), b.get(1.0f), "vector 4");
+    b *= Vector2(2.0f, 2.0f);
+    compareVectors(test, Vector2(14.0f, 10.0f), b.get(1.0f), "vector 5");
+    b /= Vector2(0.5f, 0.5f);
+    compareVectors(test, Vector2(28.0f, 20.0f), b.get(1.0f), "vector 6");
+    b = b + Vector2(1.0f, 1.0f);
+    compareVectors(test, Vector2(29.0f, 21.0f), b.get(1.0f), "vector 7");
+    b = b - Vector2(1.0f, 1.0f);
+    compareVectors(test, Vector2(28.0f, 20.0f), b.get(1.0f), "vector 8");
+    b = b * Vector2(2.0f, 2.0f);
+    compareVectors(test, Vector2(56.0f, 40.0f), b.get(1.0f), "vector 9");
+    b = b / Vector2(0.5f, 0.5f);
+    compareVectors(test, Vector2(112.0f, 80.0f), b.get(1.0f), "vector 10");
+    b = Vector2(1.0f, 1.0f) + b;
+    compareVectors(test, Vector2(113.0f, 81.0f), b.get(1.0f), "vector 11");
+    b = Vector2(1.0f, 1.0f) - b;
+    compareVectors(test, Vector2(-112.0f, -80.0f), b.get(1.0f), "vector 12");
+    b = Vector2(2.0f, 2.0f) * b;
+    compareVectors(test, Vector2(-224.0f, -160.0f), b.get(1.0f), "vector 13");
+    b = Vector2(224.0f, 160.0f) / b;
+    compareVectors(test, Vector2(-1.0f, -1.0f), b.get(1.0f), "vector 14");
+    b = -b;
+    compareVectors(test, Vector2(1.0f, 1.0f), b.get(1.0f), "vector 15");
+    b[0] += 3;
+    compareVectors(test, Vector2(4.0f, 1.0f), b.get(1.0f), "vector 16");
 }
 
 void BufferedValueTests::test() {
@@ -47,5 +96,6 @@ void BufferedValueTests::test() {
     testInterpolate(test);
     testUpdate(test);
     testCalculate(test);
+    testVector(test);
     test.finalize();
 }