Kajetan Johannes Hammerle 2 months ago
parent
commit
4b8e261a11
14 changed files with 137 additions and 301 deletions
  1. 4 5
      CMakeLists.txt
  2. 13 9
      include/core/Plane.hpp
  3. 5 4
      include/core/Test.hpp
  4. 13 27
      include/core/View.hpp
  5. 0 21
      old/Plane.cpp
  6. 0 20
      old/Plane.hpp
  7. 0 37
      old/PlaneTests.cpp
  8. 0 49
      old/View.cpp
  9. 0 41
      old/ViewTests.cpp
  10. 16 12
      src/Plane.cpp
  11. 42 28
      src/View.cpp
  12. 2 2
      test/Main.cpp
  13. 16 17
      test/modules/PlaneTests.cpp
  14. 26 29
      test/modules/ViewTests.cpp

+ 4 - 5
CMakeLists.txt

@@ -13,7 +13,7 @@ set(SRC
     #"src/HashMap.cpp"
     "src/Logger.cpp"
     "src/Matrix.cpp"
-    #"src/Plane.cpp"
+    "src/Plane.cpp"
     "src/Quaternion.cpp"
     "src/Random.cpp"
     #"src/ReadLine.cpp"
@@ -25,7 +25,7 @@ set(SRC
     "src/Unicode.cpp"
     "src/Utility.cpp"
     "src/Vector.cpp"
-    #"src/View.cpp"
+    "src/View.cpp"
 )
 
 set(SRC_TESTS
@@ -39,7 +39,7 @@ set(SRC_TESTS
     #"test/modules/HashMapTests.cpp"
     #"test/modules/ListTests.cpp"
     "test/modules/MatrixTests.cpp"
-    #"test/modules/PlaneTests.cpp"
+    "test/modules/PlaneTests.cpp"
     "test/modules/QuaternionTests.cpp"
     #"test/modules/QueueTests.cpp"
     "test/modules/RandomTests.cpp"
@@ -49,8 +49,7 @@ set(SRC_TESTS
     "test/modules/TestTests.cpp"
     "test/modules/UnicodeTests.cpp"
     "test/modules/UtilityTests.cpp"
-    #"test/modules/VectorTests.cpp"
-    #"test/modules/ViewTests.cpp"
+    "test/modules/ViewTests.cpp"
     "test/modules/ListTests.cpp"
     "test/modules/UniquePointerTests.cpp"
     "test/modules/VectorTests.cpp"

+ 13 - 9
include/core/Plane.hpp

@@ -1,15 +1,19 @@
-#ifndef CORE_PLANE_H
-#define CORE_PLANE_H
+#ifndef CORE_PLANE_HPP
+#define CORE_PLANE_HPP
 
 #include "core/Vector.hpp"
 
-typedef struct {
-    Vector3 abc;
-    float d;
-} Plane;
+namespace Core {
+    class Plane final {
+        Vector3 abc;
+        float d;
 
-void initPlane(Plane* p, const Vector3* a, const Vector3* b, const Vector3* c);
-float signedDistance(const Plane* p, const Vector3* v);
-size_t toStringPlane(const Plane* p, char* buffer, size_t n);
+    public:
+        Plane();
+        Plane(const Vector3& a, const Vector3& b, const Vector3& c);
+        float signedDistance(const Vector3& v) const;
+        size_t toString(char* s, size_t n) const;
+    };
+}
 
 #endif

+ 5 - 4
include/core/Test.hpp

@@ -1,5 +1,5 @@
-#ifndef CORE_TEST_H
-#define CORE_TEST_H
+#ifndef CORE_TEST_HPP
+#define CORE_TEST_HPP
 
 #include "core/Logger.hpp"
 
@@ -30,9 +30,10 @@ namespace Core {
     bool testString(
         const char* file, int line, const A& wanted, const B& actual) {
         char wantedString[512];
-        toString(wanted, wantedString, sizeof(wantedString));
+        size_t lw = toString(wanted, wantedString, sizeof(wantedString));
         char actualString[512];
-        toString(actual, actualString, sizeof(actualString));
+        size_t la = toString(actual, actualString, sizeof(actualString));
+        testEqual(file, line, lw, la);
         return testString(
             file, line, static_cast<const char*>(wantedString),
             static_cast<const char*>(actualString));

+ 13 - 27
include/core/View.hpp

@@ -1,42 +1,28 @@
-#ifndef CORE_VIEW_H
-#define CORE_VIEW_H
+#ifndef CORE_VIEW_HPP
+#define CORE_VIEW_HPP
 
 #include "core/Matrix.hpp"
 
-typedef struct {
-    Matrix view;
-    Vector3 back;
-    Vector3 down;
-    Vector3 front;
-    Vector3 left;
-    Vector3 right;
-    Vector3 up;
-} View;
-
-void initView(View* v);
-void updateDirections(View* v, float lengthAngle, float widthAngle);
-void updateDirectionsQ(View* v, const Quaternion* q);
-Matrix* updateMatrix(View* v, const Vector3* pos);
-
-#include "core/math/Matrix.hpp"
-
 namespace Core {
-    class View final {
+    class View {
         Matrix view{};
+        Vector3 back{};
+        Vector3 down{};
+        Vector3 front{};
+        Vector3 left{};
         Vector3 right{};
         Vector3 up{};
-        Vector3 back{};
 
     public:
         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;
-        Vector3 getRight() const;
-        Vector3 getFront() const;
-        Vector3 getBack() const;
+        const Vector3& getBack() const;
+        const Vector3& getDown() const;
+        const Vector3& getFront() const;
+        const Vector3& getLeft() const;
+        const Vector3& getRight() const;
+        const Vector3& getUp() const;
     };
 }
 

+ 0 - 21
old/Plane.cpp

@@ -1,21 +0,0 @@
-#include "core/math/Plane.hpp"
-
-Core::Plane::Plane() : abc(), d(0) {
-}
-
-Core::Plane::Plane(const Vector3& a, const Vector3& b, const Vector3& c)
-    : abc((b - a).cross(c - a).normalize()), d(-abc.dot(b)) {
-}
-
-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(')');
-}

+ 0 - 20
old/Plane.hpp

@@ -1,20 +0,0 @@
-#ifndef CORE_PLANE_HPP
-#define CORE_PLANE_HPP
-
-#include "core/math/Vector.hpp"
-#include "core/utils/ArrayString.hpp"
-
-namespace Core {
-    class Plane final {
-        Vector3 abc;
-        float d;
-
-    public:
-        Plane();
-        Plane(const Vector3& a, const Vector3& b, const Vector3& c);
-        float getSignedDistance(const Vector3& v) const;
-        void toString(BufferString& s) const;
-    };
-}
-
-#endif

+ 0 - 37
old/PlaneTests.cpp

@@ -1,37 +0,0 @@
-#include "../Tests.hpp"
-#include "core/math/Plane.hpp"
-
-const float eps = 0.0001f;
-
-using V3 = Core::Vector3;
-
-static void testToString1() {
-    Core::Plane p;
-    CORE_TEST_STRING("(0.00 x + 0.00 y + 0.00 z + 0.00)", p);
-}
-
-static void testToString2() {
-    Core::Plane p(V3(3.0f, 6.0f, 8.0f), V3(7.0f, 6.0f, 2.0f),
-                  V3(4.0f, 4.0f, 4.0f));
-    CORE_TEST_STRING("(-0.68 x + 0.57 y + -0.46 z + 2.28)", p);
-}
-
-static void testSignedDistance() {
-    V3 a(3.0f, 6.0f, 8.0f);
-    V3 b(7.0f, 6.0f, 2.0f);
-    V3 c(4.0f, 4.0f, 4.0f);
-    Core::Plane p(a, b, c);
-    CORE_TEST_FLOAT(0.0f, p.getSignedDistance(a), eps);
-    CORE_TEST_FLOAT(0.0f, p.getSignedDistance(b), eps);
-    CORE_TEST_FLOAT(0.0f, p.getSignedDistance(c), eps);
-    CORE_TEST_FLOAT(-1.13960576f, p.getSignedDistance(V3(5.0f, 8.0f, 10.0f)),
-                    eps);
-    CORE_TEST_FLOAT(0.911684612f, p.getSignedDistance(V3(3.0f, 2.0f, 1.0f)),
-                    eps);
-}
-
-void Core::testPlane() {
-    testToString1();
-    testToString2();
-    testSignedDistance();
-}

+ 0 - 49
old/View.cpp

@@ -1,49 +0,0 @@
-#include "core/math/View.hpp"
-
-void Core::View::updateDirections(float lengthAngle, float widthAngle) {
-    back.setAngles(lengthAngle, widthAngle);
-    right = back.cross(Vector3(0.0f, 1.0f, 0.0f));
-    right.normalize();
-    up = right.cross(back);
-    up.normalize();
-    back = -back;
-}
-
-void Core::View::updateDirections(const Quaternion& q) {
-    up = q * Vector3(0.0f, 1.0f, 0.0f);
-    back = q * Vector3(-1.0f, 0.0f, 0.0f);
-    right = up.cross(back);
-    right.normalize();
-}
-
-const Core::Matrix& Core::View::updateMatrix(const Vector3& pos) {
-    view.set(0, Vector4(right[0], right[1], right[2], right.dot(-pos)));
-    view.set(1, Vector4(up[0], up[1], up[2], up.dot(-pos)));
-    view.set(2, Vector4(back[0], back[1], back[2], back.dot(-pos)));
-    view.set(3, Vector4(0.0f, 0.0f, 0.0f, 1.0f));
-    return view;
-}
-
-Core::Vector3 Core::View::getUp() const {
-    return up;
-}
-
-Core::Vector3 Core::View::getDown() const {
-    return -up;
-}
-
-Core::Vector3 Core::View::getLeft() const {
-    return -right;
-}
-
-Core::Vector3 Core::View::getRight() const {
-    return right;
-}
-
-Core::Vector3 Core::View::getFront() const {
-    return -back;
-}
-
-Core::Vector3 Core::View::getBack() const {
-    return back;
-}

+ 0 - 41
old/ViewTests.cpp

@@ -1,41 +0,0 @@
-#include "../Tests.hpp"
-#include "core/math/View.hpp"
-
-using V3 = Core::Vector3;
-
-static void testFromAngles() {
-    Core::View v;
-    v.updateDirections(0.0f, 0.0f);
-    CORE_TEST_VECTOR(V3(0.0f, 1.0f, 0.0f), v.getUp());
-    CORE_TEST_VECTOR(V3(0.0f, -1.0f, 0.0f), v.getDown());
-    CORE_TEST_VECTOR(V3(0.0f, 0.0f, -1.0f), v.getLeft());
-    CORE_TEST_VECTOR(V3(0.0f, 0.0f, 1.0f), v.getRight());
-    CORE_TEST_VECTOR(V3(1.0f, 0.0f, 0.0f), v.getFront());
-    CORE_TEST_VECTOR(V3(-1.0f, 0.0f, 0.0f), v.getBack());
-}
-
-static void testFromQuaternion() {
-    Core::View v;
-    v.updateDirections(Core::Quaternion());
-    CORE_TEST_VECTOR(V3(0.0f, 1.0f, 0.0f), v.getUp());
-    CORE_TEST_VECTOR(V3(0.0f, -1.0f, 0.0f), v.getDown());
-    CORE_TEST_VECTOR(V3(0.0f, 0.0f, -1.0f), v.getLeft());
-    CORE_TEST_VECTOR(V3(0.0f, 0.0f, 1.0f), v.getRight());
-    CORE_TEST_VECTOR(V3(1.0f, 0.0f, 0.0f), v.getFront());
-    CORE_TEST_VECTOR(V3(-1.0f, 0.0f, 0.0f), v.getBack());
-}
-
-static void testUpdateMatrix() {
-    Core::View v;
-    CORE_TEST_STRING("[[0.00, 0.00, 0.00, 0.00], "
-                     "[0.00, 0.00, 0.00, 0.00], "
-                     "[0.00, 0.00, 0.00, 0.00], "
-                     "[0.00, 0.00, 0.00, 1.00]]",
-                     v.updateMatrix(Core::Vector3(1.0f, 2.0f, 3.0f)));
-}
-
-void Core::testView() {
-    testFromAngles();
-    testFromQuaternion();
-    testUpdateMatrix();
-}

+ 16 - 12
src/Plane.cpp

@@ -1,20 +1,24 @@
 #include "core/Plane.hpp"
 
-#include "core/Generic.hpp"
-#include "core/ToString.hpp"
+#include <cstdio>
 
-void initPlane(Plane* p, const Vector3* a, const Vector3* b, const Vector3* c) {
-    cross(&p->abc, sub(b, a), sub(c, a));
-    normalize(&p->abc);
-    p->d = -dot(&p->abc, b);
+using Core::Plane;
+
+Plane::Plane() : abc(), d(0) {
+}
+
+Plane::Plane(const Vector3& a, const Vector3& b, const Vector3& c) :
+    abc(cross(b - a, c - a).normalize()), d(-abc.dot(b)) {
 }
 
-float signedDistance(const Plane* p, const Vector3* v) {
-    return dot(&p->abc, v) + p->d;
+float Plane::signedDistance(const Vector3& v) const {
+    return abc.dot(v) + d;
 }
 
-size_t toStringPlane(const Plane* p, char* buffer, size_t n) {
-    return toString(
-        buffer, n, "(%.3f x + %.3f y + %.3f z + %.3f)", (double)p->abc.x,
-        (double)p->abc.y, (double)p->abc.z, (double)p->d);
+size_t Plane::toString(char* s, size_t n) const {
+    int w = snprintf(
+        s, n, "(%.3f x + %.3f y + %.3f z + %.3f)", static_cast<double>(abc[0]),
+        static_cast<double>(abc[1]), static_cast<double>(abc[2]),
+        static_cast<double>(d));
+    return w >= 0 ? static_cast<size_t>(w) : 0;
 }

+ 42 - 28
src/View.cpp

@@ -1,42 +1,56 @@
 #include "core/View.hpp"
 
-#include "core/Generic.hpp"
-
-void initView(View* v) {
-    *v = (View){0};
+using Core::View;
+
+void View::updateDirections(float lengthAngle, float widthAngle) {
+    setAngles(front, lengthAngle, widthAngle);
+    right = cross(front, Vector3(0.0f, 1.0f, 0.0f));
+    right.normalize();
+    up = cross(right, front);
+    up.normalize();
+    left = -right;
+    back = -front;
+    down = -up;
 }
 
-void updateDirections(View* v, float lengthAngle, float widthAngle) {
-    angles(&v->front, lengthAngle, widthAngle);
+void View::updateDirections(const Quaternion& q) {
+    up = q * Vector3(0.0f, 1.0f, 0.0f);
+    back = q * Vector3(-1.0f, 0.0f, 0.0f);
+    right = cross(up, back);
+    right.normalize();
+    left = -right;
+    front = -back;
+    down = -up;
+}
 
-    cross(&v->right, &v->front, &V(0.0f, 1.0f, 0.0f));
-    normalize(&v->right);
+const Core::Matrix& View::updateMatrix(const Vector3& pos) {
+    view.set(0, Vector4(right[0], right[1], right[2], -right.dot(pos)));
+    view.set(1, Vector4(up[0], up[1], up[2], -up.dot(pos)));
+    view.set(2, Vector4(back[0], back[1], back[2], -back.dot(pos)));
+    view.set(3, Vector4(0.0f, 0.0f, 0.0f, 1.0f));
+    return view;
+}
 
-    cross(&v->up, &v->right, &v->front);
-    normalize(&v->up);
+const Core::Vector3& Core::View::getUp() const {
+    return up;
+}
 
-    invert(&v->left, &v->right);
-    invert(&v->back, &v->front);
-    invert(&v->down, &v->up);
+const Core::Vector3& Core::View::getDown() const {
+    return down;
 }
 
-void updateDirectionsQ(View* v, const Quaternion* q) {
-    mul(&v->up, q, &V(0.0f, 1.0f, 0.0f));
-    mul(&v->back, q, &V(-1.0f, 0.0f, 0.0f));
+const Core::Vector3& Core::View::getLeft() const {
+    return left;
+}
 
-    cross(&v->right, &v->up, &v->back);
-    normalize(&v->right);
+const Core::Vector3& Core::View::getRight() const {
+    return right;
+}
 
-    invert(&v->left, &v->right);
-    invert(&v->front, &v->back);
-    invert(&v->down, &v->up);
+const Core::Vector3& Core::View::getFront() const {
+    return front;
 }
 
-Matrix* updateMatrix(View* v, const Vector3* pos) {
-    Vector4* d = v->view.data;
-    d[0] = V(v->right.x, v->right.y, v->right.z, -dot(&v->right, pos));
-    d[1] = V(v->up.x, v->up.y, v->up.z, -dot(&v->up, pos));
-    d[2] = V(v->back.x, v->back.y, v->back.z, -dot(&v->back, pos));
-    d[3] = V(0.0f, 0.0f, 0.0f, 1.0f);
-    return &v->view;
+const Core::Vector3& Core::View::getBack() const {
+    return back;
 }

+ 2 - 2
test/Main.cpp

@@ -74,7 +74,7 @@ int main(int argAmount, const char** args) {
     // testFrustum();
     // testHashMap(light);
     testMatrix();
-    // testPlane();
+    testPlane();
     testQuaternion();
     // testQueue();
     testRandom(light);
@@ -85,7 +85,7 @@ int main(int argAmount, const char** args) {
     testTerminal(!light);
     testUnicode();
     testUtility(light);
-    // testView();
+    testView();
 
     logLevel = LogLevel::WARNING;
     LOG_DEBUG("You won't see this!");

+ 16 - 17
test/modules/PlaneTests.cpp

@@ -1,27 +1,26 @@
-#include "../Tests.h"
-#include "core/Plane.h"
+#include "../Tests.hpp"
+#include "core/Plane.hpp"
+#include "core/Test.hpp"
 
 static const float eps = 0.0001f;
+using V3 = Core::Vector3;
 
 static void testToString() {
-    Plane p;
-    initPlane(&p, &V(3, 6, 8), &V(7, 6, 2), &V(4, 4, 4));
-    char buffer[128];
-    toStringPlane(&p, buffer, sizeof(buffer));
-    TEST_STRING("(-0.684 x + 0.570 y + -0.456 z + 2.279)", buffer);
+    Core::Plane p(V3(3, 6, 8), V3(7, 6, 2), V3(4, 4, 4));
+    TEST_STRING("(-0.684 x + 0.570 y + -0.456 z + 2.279)", p);
+    TEST_STRING("(0.000 x + 0.000 y + 0.000 z + 0.000)", Core::Plane());
 }
 
 static void testSignedDistance() {
-    Vector3 a = V(3, 6, 8);
-    Vector3 b = V(7, 6, 2);
-    Vector3 c = V(4, 4, 4);
-    Plane p;
-    initPlane(&p, &a, &b, &c);
-    TEST_FLOAT(0.0f, signedDistance(&p, &a), eps);
-    TEST_FLOAT(0.0f, signedDistance(&p, &b), eps);
-    TEST_FLOAT(0.0f, signedDistance(&p, &c), eps);
-    TEST_FLOAT(-1.13960576f, signedDistance(&p, &V(5, 8, 10)), eps);
-    TEST_FLOAT(0.911684612f, signedDistance(&p, &V(3, 2, 1)), eps);
+    V3 a(3, 6, 8);
+    V3 b(7, 6, 2);
+    V3 c(4, 4, 4);
+    Core::Plane p(a, b, c);
+    TEST_FLOAT(0.0f, p.signedDistance(a), eps);
+    TEST_FLOAT(0.0f, p.signedDistance(b), eps);
+    TEST_FLOAT(0.0f, p.signedDistance(c), eps);
+    TEST_FLOAT(-1.13960576f, p.signedDistance(V3(5, 8, 10)), eps);
+    TEST_FLOAT(0.911684612f, p.signedDistance(V3(3, 2, 1)), eps);
 }
 
 void testPlane() {

+ 26 - 29
test/modules/ViewTests.cpp

@@ -1,44 +1,41 @@
-#include "../Tests.h"
-#include "core/View.h"
+#include "../Tests.hpp"
+#include "core/Test.hpp"
+#include "core/View.hpp"
+
+using V3 = Core::Vector3;
+using Core::View;
 
 static void testFromAngles() {
     View v;
-    initView(&v);
-    updateDirections(&v, 0.0f, 0.0f);
-    TEST_V3(&V(0.0f, 1.0f, 0.0f), &v.up);
-    TEST_V3(&V(0.0f, -1.0f, 0.0f), &v.down);
-    TEST_V3(&V(0.0f, 0.0f, -1.0f), &v.left);
-    TEST_V3(&V(0.0f, 0.0f, 1.0f), &v.right);
-    TEST_V3(&V(1.0f, 0.0f, 0.0f), &v.front);
-    TEST_V3(&V(-1.0f, 0.0f, 0.0f), &v.back);
+    v.updateDirections(0.0f, 0.0f);
+    TEST(V3(0.0f, 1.0f, 0.0f), v.getUp());
+    TEST(V3(0.0f, -1.0f, 0.0f), v.getDown());
+    TEST(V3(0.0f, 0.0f, -1.0f), v.getLeft());
+    TEST(V3(0.0f, 0.0f, 1.0f), v.getRight());
+    TEST(V3(1.0f, 0.0f, 0.0f), v.getFront());
+    TEST(V3(-1.0f, 0.0f, 0.0f), v.getBack());
 }
 
 static void testFromQuaternion() {
     View v;
-    initView(&v);
-    updateDirectionsQ(&v, &UNIT_QUATERNION);
-    TEST_V3(&V(0.0f, 1.0f, 0.0f), &v.up);
-    TEST_V3(&V(0.0f, -1.0f, 0.0f), &v.down);
-    TEST_V3(&V(0.0f, 0.0f, -1.0f), &v.left);
-    TEST_V3(&V(0.0f, 0.0f, 1.0f), &v.right);
-    TEST_V3(&V(1.0f, 0.0f, 0.0f), &v.front);
-    TEST_V3(&V(-1.0f, 0.0f, 0.0f), &v.back);
+    v.updateDirections(Core::Quaternion());
+    TEST(V3(0.0f, 1.0f, 0.0f), v.getUp());
+    TEST(V3(0.0f, -1.0f, 0.0f), v.getDown());
+    TEST(V3(0.0f, 0.0f, -1.0f), v.getLeft());
+    TEST(V3(0.0f, 0.0f, 1.0f), v.getRight());
+    TEST(V3(1.0f, 0.0f, 0.0f), v.getFront());
+    TEST(V3(-1.0f, 0.0f, 0.0f), v.getBack());
 }
 
 static void testUpdateMatrix() {
     View v;
-    initView(&v);
-    Matrix* m = updateMatrix(&v, &V(1.0f, 2.0f, 3.0f));
-
-    char buffer[128];
-    toStringMatrix(m, buffer, sizeof(buffer));
-
+    const Core::Matrix& m = v.updateMatrix(V3(1.0f, 2.0f, 3.0f));
     TEST_STRING(
-        "[[0.000, 0.000, 0.000, -0.000], "
-        "[0.000, 0.000, 0.000, -0.000], "
-        "[0.000, 0.000, 0.000, -0.000], "
-        "[0.000, 0.000, 0.000, 1.000]]",
-        buffer);
+        "[[0.00, 0.00, 0.00, -0.00], "
+        "[0.00, 0.00, 0.00, -0.00], "
+        "[0.00, 0.00, 0.00, -0.00], "
+        "[0.00, 0.00, 0.00, 1.00]]",
+        m);
 }
 
 void testView() {