Browse Source

Plane and tests

Kajetan Johannes Hammerle 6 months ago
parent
commit
c24891804f
6 changed files with 89 additions and 4 deletions
  1. 3 3
      CMakeLists.txt
  2. 1 0
      cmake/clang_warnings.cmake
  3. 17 0
      include/core/Plane.h
  4. 24 0
      src/Plane.c
  5. 1 1
      test/Main.c
  6. 43 0
      test/modules/PlaneTests.c

+ 3 - 3
CMakeLists.txt

@@ -7,6 +7,7 @@ set(SRC
     "src/Buffer.c"
     "src/Logger.c"
     "src/Matrix.c"
+    "src/Plane.c"
     "src/Quaternion.c"
     "src/Random.c"
     "src/Utility.c"
@@ -14,7 +15,6 @@ set(SRC
     #"src/BitArray.cpp"
     #"src/Box.cpp"
     #"src/Frustum.cpp"
-    #"src/Plane.cpp"
     #"src/SpinLock.cpp"
     #"src/View.cpp"
 )
@@ -24,6 +24,7 @@ set(SRC_TESTS
     "test/Test.c"
     "test/modules/BufferTests.c"
     "test/modules/MatrixTests.c"
+    "test/modules/PlaneTests.c"
     "test/modules/QuaternionTests.c"
     "test/modules/RandomTests.c"
     "test/modules/UtilityTests.c"
@@ -36,7 +37,6 @@ set(SRC_TESTS
     #"test/modules/LinkedListTests.cpp"
     #"test/modules/ListTests.cpp"
     #"test/modules/MatrixStackTests.cpp"
-    #"test/modules/PlaneTests.cpp"
     #"test/modules/RingBufferTests.cpp"
     #"test/modules/StackTests.cpp"
     #"test/modules/ThreadTests.cpp"
@@ -100,6 +100,7 @@ target_sources(core PUBLIC
         ./include/core/Check.h
         ./include/core/Logger.h
         ./include/core/Matrix.h
+        ./include/core/Plane.h
         ./include/core/Quaternion.h
         ./include/core/Random.h
         ./include/core/Types.h
@@ -112,7 +113,6 @@ target_sources(core PUBLIC
 #        ./include/core/LinkedList.hpp
 #        ./include/core/List.hpp
 #        ./include/core/MatrixStack.hpp
-#        ./include/core/Plane.hpp
 #        ./include/core/ProbingHashMap.hpp
 #        ./include/core/RingBuffer.hpp
 #        ./include/core/Stack.hpp

+ 1 - 0
cmake/clang_warnings.cmake

@@ -26,6 +26,7 @@ set(WARNINGS
     -Wmissing-include-dirs
     -Wmissing-noreturn 
     -Wmissing-prototypes
+    -Wmissing-variable-declarations
     -Wmultichar
     -Wnarrowing
     -Wnested-externs

+ 17 - 0
include/core/Plane.h

@@ -0,0 +1,17 @@
+#ifndef CORE_PLANE_H
+#define CORE_PLANE_H
+
+#include "core/Vector.h"
+
+typedef struct {
+    CoreVector3 abc;
+    float d;
+} CorePlane;
+
+#define CORE_PLANE ((CorePlane){0})
+CorePlane* coreInitPlane(CorePlane* p, const CoreVector3* a,
+                         const CoreVector3* b, const CoreVector3* c);
+float coreGetSignedDistance(CorePlane* p, const CoreVector3* v);
+size_t coreToStringPlane(const CorePlane* p, char* buffer, size_t n);
+
+#endif

+ 24 - 0
src/Plane.c

@@ -0,0 +1,24 @@
+#include "core/Plane.h"
+
+#include <stdio.h>
+
+#define CV30 (&(CoreVector3){0})
+
+CorePlane* coreInitPlane(CorePlane* p, const CoreVector3* a,
+                         const CoreVector3* b, const CoreVector3* c) {
+    coreCross(&p->abc, coreSubV3(CV30, b, a), coreSubV3(CV30, c, a));
+    coreNormalizeV3(&p->abc);
+    p->d = -coreDotV3(&p->abc, b);
+    return p;
+}
+
+float coreGetSignedDistance(CorePlane* p, const CoreVector3* v) {
+    return coreDotV3(&p->abc, v) + p->d;
+}
+
+size_t coreToStringPlane(const CorePlane* p, char* buffer, size_t n) {
+    int w = snprintf(buffer, n, "(%.3f x + %.3f y + %.3f z + %.3f)",
+                     (double)p->abc.data[0], (double)p->abc.data[1],
+                     (double)p->abc.data[2], (double)p->d);
+    return w < 0 ? 0 : (size_t)w;
+}

+ 1 - 1
test/Main.c

@@ -41,7 +41,7 @@ int main(int argAmount, const char** args) {
     // coreTestList(light);
     // coreTestMatrixStack(light);
     coreTestMatrix();
-    // coreTestPlane();
+    coreTestPlane();
     coreTestQuaternion();
     coreTestRandom(light);
     // coreTestRingBuffer();

+ 43 - 0
test/modules/PlaneTests.c

@@ -0,0 +1,43 @@
+#include "../Tests.h"
+#include "core/Plane.h"
+
+static const float eps = 0.0001f;
+
+#define CV3(a, b, c) (&(CoreVector3){{a, b, c}})
+
+static void testToString1() {
+    CorePlane p = CORE_PLANE;
+    char buffer[128];
+    coreToStringPlane(&p, buffer, sizeof(buffer));
+    CORE_TEST_STRING("(0.000 x + 0.000 y + 0.000 z + 0.000)", buffer);
+}
+
+static void testToString2() {
+    CorePlane p = CORE_PLANE;
+    coreInitPlane(&p, CV3(3.0f, 6.0f, 8.0f), CV3(7.0f, 6.0f, 2.0f),
+                  CV3(4.0f, 4.0f, 4.0f));
+    char buffer[128];
+    coreToStringPlane(&p, buffer, sizeof(buffer));
+    CORE_TEST_STRING("(-0.684 x + 0.570 y + -0.456 z + 2.279)", buffer);
+}
+
+static void testSignedDistance() {
+    CoreVector3 a = {{3.0f, 6.0f, 8.0f}};
+    CoreVector3 b = {{7.0f, 6.0f, 2.0f}};
+    CoreVector3 c = {{4.0f, 4.0f, 4.0f}};
+    CorePlane p = CORE_PLANE;
+    coreInitPlane(&p, &a, &b, &c);
+    CORE_TEST_FLOAT(0.0f, coreGetSignedDistance(&p, &a), eps);
+    CORE_TEST_FLOAT(0.0f, coreGetSignedDistance(&p, &b), eps);
+    CORE_TEST_FLOAT(0.0f, coreGetSignedDistance(&p, &c), eps);
+    CORE_TEST_FLOAT(-1.13960576f,
+                    coreGetSignedDistance(&p, CV3(5.0f, 8.0f, 10.0f)), eps);
+    CORE_TEST_FLOAT(0.911684612f,
+                    coreGetSignedDistance(&p, CV3(3.0f, 2.0f, 1.0f)), eps);
+}
+
+void coreTestPlane() {
+    testToString1();
+    testToString2();
+    testSignedDistance();
+}