Browse Source

planes and tests

Kajetan Johannes Hammerle 3 years ago
parent
commit
ecbdaff9ba
7 changed files with 89 additions and 1 deletions
  1. 2 0
      Main.cpp
  2. 13 0
      math/Plane.cpp
  3. 26 0
      math/Plane.h
  4. 2 0
      math/Vector.h
  5. 1 1
      meson.build
  6. 37 0
      tests/PlaneTests.cpp
  7. 8 0
      tests/PlaneTests.h

+ 2 - 0
Main.cpp

@@ -10,6 +10,7 @@
 #include "tests/MatrixTests.h"
 #include "tests/StackTests.h"
 #include "tests/MatrixStackTests.h"
+#include "tests/PlaneTests.h"
 
 int main() {
     ArrayTests::test();
@@ -24,5 +25,6 @@ int main() {
     MatrixTests::test();
     StackTests::test();
     MatrixStackTests::test();
+    PlaneTests::test();
     return 0;
 }

+ 13 - 0
math/Plane.cpp

@@ -0,0 +1,13 @@
+#include "math/Plane.h"
+
+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(a);
+}
+
+float Plane::getSignedDistance(const Vector3& v) const {
+    return abc.dot(v) + d;
+}

+ 26 - 0
math/Plane.h

@@ -0,0 +1,26 @@
+#ifndef PLANE_H
+#define PLANE_H
+
+#include "math/Vector.h"
+#include "utils/StringBuffer.h"
+
+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;
+
+    template<int L>
+    void toString(StringBuffer<L>& 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).append(')');
+    }
+};
+
+#endif

+ 2 - 0
math/Vector.h

@@ -1,6 +1,8 @@
 #ifndef VECTOR_H
 #define VECTOR_H
 
+#include <cmath>
+
 #include "utils/StringBuffer.h"
 
 template<int N>

+ 1 - 1
meson.build

@@ -1,6 +1,6 @@
 project('gaming core tests', 'cpp')
 
-sources = ['Main.cpp', 'tests/Test.cpp', 'tests/ArrayTests.cpp', 'tests/HashMapTests.cpp', 'tests/ListTests.cpp', 'tests/BitArrayTests.cpp', 'tests/StringBufferTests.cpp', 'tests/RandomTests.cpp', 'utils/Random.cpp', 'tests/RingBufferTests.cpp', 'tests/SplitStringTests.cpp', 'tests/VectorTests.cpp', 'math/Vector.cpp', 'math/Matrix.cpp', 'tests/MatrixTests.cpp', 'tests/StackTests.cpp', 'tests/MatrixStackTests.cpp']
+sources = ['Main.cpp', 'tests/Test.cpp', 'tests/ArrayTests.cpp', 'tests/HashMapTests.cpp', 'tests/ListTests.cpp', 'tests/BitArrayTests.cpp', 'tests/StringBufferTests.cpp', 'tests/RandomTests.cpp', 'utils/Random.cpp', 'tests/RingBufferTests.cpp', 'tests/SplitStringTests.cpp', 'tests/VectorTests.cpp', 'math/Vector.cpp', 'math/Matrix.cpp', 'tests/MatrixTests.cpp', 'tests/StackTests.cpp', 'tests/MatrixStackTests.cpp', 'tests/PlaneTests.cpp', 'math/Plane.cpp']
 
 executable('tests', 
     sources: sources,

+ 37 - 0
tests/PlaneTests.cpp

@@ -0,0 +1,37 @@
+#include "tests/PlaneTests.h"
+#include "tests/Test.h"
+#include "math/Plane.h"
+#include "utils/StringBuffer.h"
+
+typedef StringBuffer<50> String;
+const float eps = 0.0001f;
+
+static void testToString1(Test& test) {
+    Plane p;
+    test.checkEqual(String("(0.00 x + 0.00 y + 0.00 z + 0.00)"), String(p), "to string 1");
+}
+
+static void testToString2(Test& test) {
+    Plane p(Vector3(3.0f, 6.0f, 8.0f), Vector3(7.0f, 6.0f, 2.0f), Vector3(4.0f, 4.0f, 4.0f));
+    test.checkEqual(String("(-0.68 x + 0.57 y + -0.46 z + 2.28)"), String(p), "to string 2");
+}
+
+static void testSignedDistance(Test& test) {
+    Vector3 a(3.0f, 6.0f, 8.0f);
+    Vector3 b(7.0f, 6.0f, 2.0f);
+    Vector3 c(4.0f, 4.0f, 4.0f);
+    Plane p(a, b, c);
+    test.checkFloat(0.0f, p.getSignedDistance(a), eps, "no distance to init points 1");
+    test.checkFloat(0.0f, p.getSignedDistance(b), eps, "no distance to init points 2");
+    test.checkFloat(0.0f, p.getSignedDistance(c), eps, "no distance to init points 3");
+    test.checkFloat(-1.13960576f, p.getSignedDistance(Vector3(5.0f, 8.0f, 10.0f)), eps, "positive distance");
+    test.checkFloat(0.911684612f, p.getSignedDistance(Vector3(3.0f, 2.0f, 1.0f)), eps, "negative distance");
+}
+
+void PlaneTests::test() {
+    Test test("Plane");
+    testToString1(test);
+    testToString2(test);
+    testSignedDistance(test);
+    test.finalize();
+}

+ 8 - 0
tests/PlaneTests.h

@@ -0,0 +1,8 @@
+#ifndef PLANETESTS_H
+#define PLANETESTS_H
+
+namespace PlaneTests {
+    void test();
+}
+
+#endif