Kajetan Johannes Hammerle 2 сар өмнө
parent
commit
4e373fac7e
8 өөрчлөгдсөн 114 нэмэгдсэн , 260 устгасан
  1. 2 2
      CMakeLists.txt
  2. 19 14
      include/core/Box.hpp
  3. 0 56
      old/Box.cpp
  4. 0 33
      old/Box.hpp
  5. 0 60
      old/BoxTests.cpp
  6. 49 36
      src/Box.cpp
  7. 1 1
      test/Main.cpp
  8. 43 58
      test/modules/BoxTests.cpp

+ 2 - 2
CMakeLists.txt

@@ -5,7 +5,7 @@ set(CMAKE_CXX_STANDARD 23)
 
 set(SRC
     #"src/BitArray.cpp"
-    #"src/Box.cpp"
+    "src/Box.cpp"
     #"src/Buffer.cpp"
     #"src/Components.cpp"
     "src/File.cpp"
@@ -31,7 +31,7 @@ set(SRC
 set(SRC_TESTS
     "test/Main.cpp"
     #"test/modules/BitArrayTests.cpp"
-    #"test/modules/BoxTests.cpp"
+    "test/modules/BoxTests.cpp"
     #"test/modules/BufferTests.cpp"
     #"test/modules/ComponentsTests.cpp"
     "test/modules/FileTests.cpp"

+ 19 - 14
include/core/Box.hpp

@@ -1,23 +1,28 @@
-#ifndef CORE_BOX_H
-#define CORE_BOX_H
+#ifndef CORE_BOX_HPP
+#define CORE_BOX_HPP
 
 #include "core/Vector.hpp"
 
-typedef union {
-    struct {
+namespace Core {
+    class Box final {
         Vector3 min;
         Vector3 max;
-    };
 
-    Vector3 v[2];
-} Box;
+        Box(const Vector3& min, const Vector3& max);
+
+    public:
+        Box(const Vector3& size);
+
+        Box offset(const Vector3& offset) const;
+        bool collidesWith(const Box& other) const;
+        Box expand(const Vector3& offset) const;
+        Box grow(const Vector3& growth) const;
 
-#define BOX ((Box){0})
-void setBox(Box* box, const Vector3* size);
-void offsetBox(Box* box, const Vector3* offset);
-bool collidesWithBox(const Box* box, const Box* other);
-void expandBox(Box* box, const Vector3* offset);
-void growBox(Box* box, const Vector3* growth);
-size_t toStringBox(const Box* box, char* buffer, size_t n);
+        const Vector3& getMin() const;
+        const Vector3& getMax() const;
+
+        size_t toString(char* s, size_t n) const;
+    };
+}
 
 #endif

+ 0 - 56
old/Box.cpp

@@ -1,56 +0,0 @@
-#include "core/math/Box.hpp"
-
-Core::Box::Box(const Vector3& min_, const Vector3& max_)
-    : min(min_), max(max_) {
-}
-
-Core::Box::Box(const Vector3& size) : min(), max() {
-    for(size_t i = 0; i < 3; i++) {
-        if(size[i] < 0.0f) {
-            min[i] = size[i];
-        } else {
-            max[i] = size[i];
-        }
-    }
-}
-
-Core::Box Core::Box::offset(const Vector3& offset) const {
-    return Box(min + offset, max + offset);
-}
-
-bool Core::Box::collidesWith(const Box& other) const {
-    return max[0] > other.min[0] && min[0] < other.max[0] &&
-           max[1] > other.min[1] && min[1] < other.max[1] &&
-           max[2] > other.min[2] && min[2] < other.max[2];
-}
-
-Core::Box Core::Box::expand(const Vector3& offset) const {
-    Vector3 add(offset[0] > 0.0f ? offset[0] : 0.0f,
-                offset[1] > 0.0f ? offset[1] : 0.0f,
-                offset[2] > 0.0f ? offset[2] : 0.0f);
-    Vector3 sub(offset[0] < 0.0f ? offset[0] : 0.0f,
-                offset[1] < 0.0f ? offset[1] : 0.0f,
-                offset[2] < 0.0f ? offset[2] : 0.0f);
-    return Box(min + sub, max + add);
-}
-
-Core::Box Core::Box::grow(const Vector3& growth) const {
-    Vector3 half = growth * 0.5f;
-    Vector3 nMin = min - half;
-    Vector3 nMax = max + half;
-    for(size_t i = 0; i < 3; i++) {
-        if(nMin[i] > nMax[i]) {
-            nMin[i] = (min[i] + max[i]) * 0.5f;
-            nMax[i] = nMin[i];
-        }
-    }
-    return Box(nMin, nMax);
-}
-
-const Core::Vector3& Core::Box::getMin() const {
-    return min;
-}
-
-const Core::Vector3& Core::Box::getMax() const {
-    return max;
-}

+ 0 - 33
old/Box.hpp

@@ -1,33 +0,0 @@
-#ifndef CORE_BOX_HPP
-#define CORE_BOX_HPP
-
-#include "core/math/Vector.hpp"
-#include "core/utils/ArrayString.hpp"
-
-namespace Core {
-    class Box final {
-        Vector3 min;
-        Vector3 max;
-
-        Box(const Vector3& min, const Vector3& max);
-
-    public:
-        Box(const Vector3& size);
-
-        Box offset(const Vector3& offset) const;
-        bool collidesWith(const Box& other) const;
-        Box expand(const Vector3& offset) const;
-        Box grow(const Vector3& growth) const;
-
-        const Vector3& getMin() const;
-        const Vector3& getMax() const;
-
-        void toString(BufferString& s) const {
-            s.append("Box(");
-            s.append(min).append(", ");
-            s.append(max).append(")");
-        }
-    };
-}
-
-#endif

+ 0 - 60
old/BoxTests.cpp

@@ -1,60 +0,0 @@
-#include "../Tests.hpp"
-#include "core/math/Box.hpp"
-
-static void testInit() {
-    Core::Box box(Core::Vector3(1.0f, 2.0f, 3.0f));
-    CORE_TEST_STRING("Box([0.00, 0.00, 0.00], [1.00, 2.00, 3.00])", box);
-    CORE_TEST_STRING("[0.00, 0.00, 0.00]", box.getMin());
-    CORE_TEST_STRING("[1.00, 2.00, 3.00]", box.getMax());
-
-    box = Core::Box(Core::Vector3(-1.0f, -2.0f, -3.0f));
-    CORE_TEST_STRING("Box([-1.00, -2.00, -3.00], [0.00, 0.00, 0.00])", box);
-    CORE_TEST_STRING("[-1.00, -2.00, -3.00]", box.getMin());
-    CORE_TEST_STRING("[0.00, 0.00, 0.00]", box.getMax());
-}
-
-static void testOffset() {
-    Core::Box box(Core::Vector3(1.0f, 2.0f, 3.0f));
-    CORE_TEST_STRING("Box([7.00, -4.00, 6.00], [8.00, -2.00, 9.00])",
-                     box.offset(Core::Vector3(7.0f, -4.0f, 6.0f)));
-}
-
-static void testCollidesWith() {
-    Core::Box boxA(Core::Vector3(1.0f, 2.0f, 3.0f));
-    Core::Box boxB(Core::Vector3(-1.0f, -2.0f, -3.0f));
-    Core::Box boxC(Core::Vector3(2.0f, 2.0f, 2.0f));
-    boxC = boxC.offset(Core::Vector3(-1.0f, -1.0f, -1.0f));
-
-    CORE_TEST_TRUE(boxC.collidesWith(boxA));
-    CORE_TEST_TRUE(boxC.collidesWith(boxB));
-    CORE_TEST_TRUE(boxA.collidesWith(boxC));
-    CORE_TEST_TRUE(boxB.collidesWith(boxC));
-    CORE_TEST_FALSE(boxA.collidesWith(boxB));
-    CORE_TEST_FALSE(boxB.collidesWith(boxA));
-}
-
-static void testExpand() {
-    Core::Box box(Core::Vector3(1.0f, 2.0f, 3.0f));
-    CORE_TEST_STRING("Box([0.00, -4.00, 0.00], [8.00, 2.00, 9.00])",
-                     box.expand(Core::Vector3(7.0f, -4.0f, 6.0f)));
-    CORE_TEST_STRING("Box([-7.00, 0.00, -6.00], [1.00, 6.00, 3.00])",
-                     box.expand(Core::Vector3(-7.0f, 4.0f, -6.0f)));
-}
-
-static void testGrow() {
-    Core::Box box(Core::Vector3(1.0f, 2.0f, 3.0f));
-    CORE_TEST_STRING("Box([-2.00, -1.00, -3.00], [3.00, 3.00, 6.00])",
-                     box.grow(Core::Vector3(4.0f, 2.0f, 6.0f)));
-    CORE_TEST_STRING("Box([0.50, 1.00, 1.50], [0.50, 1.00, 1.50])",
-                     box.grow(Core::Vector3(-4.0f, -2.0f, -6.0f)));
-    CORE_TEST_STRING("Box([0.05, 1.00, 0.50], [0.95, 1.00, 2.50])",
-                     box.grow(Core::Vector3(-0.1f, -4.0f, -1.0f)));
-}
-
-void Core::testBox() {
-    testInit();
-    testOffset();
-    testCollidesWith();
-    testExpand();
-    testGrow();
-}

+ 49 - 36
src/Box.cpp

@@ -1,57 +1,70 @@
 #include "core/Box.hpp"
 
-#include "core/Generic.hpp"
-#include "core/ToString.hpp"
+#include <cstdio>
 
-void setBox(Box* box, const Vector3* size) {
+using Core::Box;
+
+Box::Box(const Vector3& min_, const Vector3& max_) : min(min_), max(max_) {
+}
+
+Box::Box(const Vector3& size) : min(), max() {
     for(size_t i = 0; i < 3; i++) {
-        if(size->data[i] < 0.0f) {
-            box->min.data[i] = size->data[i];
-            box->max.data[i] = 0.0f;
+        if(size[i] < 0.0f) {
+            min[i] = size[i];
         } else {
-            box->min.data[i] = 0.0f;
-            box->max.data[i] = size->data[i];
+            max[i] = size[i];
         }
     }
 }
 
-void offsetBox(Box* box, const Vector3* offset) {
-    addSet(&box->min, offset);
-    addSet(&box->max, offset);
+Box Box::offset(const Vector3& offset) const {
+    return Box(min + offset, max + offset);
 }
 
-bool collidesWithBox(const Box* box, const Box* other) {
-    return box->max.x > other->min.x && box->min.x < other->max.x &&
-           box->max.y > other->min.y && box->min.y < other->max.y &&
-           box->max.z > other->min.z && box->min.z < other->max.z;
+bool Box::collidesWith(const Box& other) const {
+    return max[0] > other.min[0] && min[0] < other.max[0] &&
+           max[1] > other.min[1] && min[1] < other.max[1] &&
+           max[2] > other.min[2] && min[2] < other.max[2];
 }
 
-void expandBox(Box* box, const Vector3* offset) {
-    for(size_t i = 0; i < 3; i++) {
-        box->v[offset->data[i] > 0.0f].data[i] += offset->data[i];
-    }
+Box Box::expand(const Vector3& offset) const {
+    Vector3 add(
+        offset[0] > 0.0f ? offset[0] : 0.0f,
+        offset[1] > 0.0f ? offset[1] : 0.0f,
+        offset[2] > 0.0f ? offset[2] : 0.0f);
+    Vector3 sub(
+        offset[0] < 0.0f ? offset[0] : 0.0f,
+        offset[1] < 0.0f ? offset[1] : 0.0f,
+        offset[2] < 0.0f ? offset[2] : 0.0f);
+    return Box(min + sub, max + add);
 }
 
-void growBox(Box* box, const Vector3* growth) {
-    Vector3 half;
-    mul(&half, growth, 0.5f);
-    Vector3 nMin;
-    sub(&nMin, &box->min, &half);
-    Vector3 nMax;
-    add(&nMax, &box->max, &half);
+Box Box::grow(const Vector3& growth) const {
+    Vector3 half = growth * 0.5f;
+    Vector3 nMin = min - half;
+    Vector3 nMax = max + half;
     for(size_t i = 0; i < 3; i++) {
-        if(nMin.data[i] > nMax.data[i]) {
-            nMin.data[i] = (box->min.data[i] + box->max.data[i]) * 0.5f;
-            nMax.data[i] = nMin.data[i];
+        if(nMin[i] > nMax[i]) {
+            nMin[i] = (min[i] + max[i]) * 0.5f;
+            nMax[i] = nMin[i];
         }
     }
-    box->min = nMin;
-    box->max = nMax;
+    return Box(nMin, nMax);
+}
+
+const Core::Vector3& Box::getMin() const {
+    return min;
+}
+
+const Core::Vector3& Box::getMax() const {
+    return max;
 }
 
-size_t toStringBox(const Box* box, char* buffer, size_t n) {
-    return toString(
-        buffer, n, "Box([%.3f, %.3f, %.3f], [%.3f, %.3f, %.3f])",
-        (double)box->min.x, (double)box->min.y, (double)box->min.z,
-        (double)box->max.x, (double)box->max.y, (double)box->max.z);
+size_t Box::toString(char* s, size_t n) const {
+    int w = snprintf(
+        s, n, "Box([%.2f, %.2f, %.2f], [%.2f, %.2f, %.2f])",
+        static_cast<double>(min[0]), static_cast<double>(min[1]),
+        static_cast<double>(min[2]), static_cast<double>(max[0]),
+        static_cast<double>(max[1]), static_cast<double>(max[2]));
+    return w >= 0 ? static_cast<size_t>(w) : 0;
 }

+ 1 - 1
test/Main.cpp

@@ -67,7 +67,7 @@ int main(int argAmount, const char** args) {
     testMath();
     testArray();
     // testBitArray();
-    // testBox();
+    testBox();
     // testBuffer(light);
     // testComponents();
     testFile();

+ 43 - 58
test/modules/BoxTests.cpp

@@ -1,75 +1,60 @@
-#include "../Tests.h"
-#include "core/Box.h"
+#include "../Tests.hpp"
+#include "core/Box.hpp"
+#include "core/Test.hpp"
+
+using Core::Box;
+using V3 = Core::Vector3;
 
 static void testInit() {
-    Box box = BOX;
-    setBox(&box, &V(1.0f, 2.0f, 3.0f));
-    char buffer[128];
-    toStringBox(&box, buffer, sizeof(buffer));
-    TEST_STRING("Box([0.000, 0.000, 0.000], [1.000, 2.000, 3.000])", buffer);
-    setBox(&box, &V(-1.0f, -2.0f, -3.0f));
-    toStringBox(&box, buffer, sizeof(buffer));
-    TEST_STRING("Box([-1.000, -2.000, -3.000], [0.000, 0.000, 0.000])", buffer);
+    Box box(V3(1.0f, 2.0f, 3.0f));
+    TEST_STRING("Box([0.00, 0.00, 0.00], [1.00, 2.00, 3.00])", box);
+    TEST_STRING("[0.00, 0.00, 0.00]", box.getMin());
+    TEST_STRING("[1.00, 2.00, 3.00]", box.getMax());
+    box = Box(V3(-1.0f, -2.0f, -3.0f));
+    TEST_STRING("Box([-1.00, -2.00, -3.00], [0.00, 0.00, 0.00])", box);
+    TEST_STRING("[-1.00, -2.00, -3.00]", box.getMin());
+    TEST_STRING("[0.00, 0.00, 0.00]", box.getMax());
 }
 
 static void testOffset() {
-    Box box = BOX;
-    setBox(&box, &V(1.0f, 2.0f, 3.0f));
-    offsetBox(&box, &V(7.0f, -4.0f, 6.0f));
-    char buffer[128];
-    toStringBox(&box, buffer, sizeof(buffer));
-    TEST_STRING("Box([7.000, -4.000, 6.000], [8.000, -2.000, 9.000])", buffer);
+    Box box(V3(1.0f, 2.0f, 3.0f));
+    box = box.offset(V3(7.0f, -4.0f, 6.0f));
+    TEST_STRING("Box([7.00, -4.00, 6.00], [8.00, -2.00, 9.00])", box);
 }
 
 static void testCollidesWith() {
-    Box boxA = BOX;
-    setBox(&boxA, &V(1.0f, 2.0f, 3.0f));
-    Box boxB = BOX;
-    setBox(&boxB, &V(-1.0f, -2.0f, -3.0f));
-    Box boxC = BOX;
-    setBox(&boxC, &V(2.0f, 2.0f, 2.0f));
-    offsetBox(&boxC, &V(-1.0f, -1.0f, -1.0f));
-
-    TEST_TRUE(collidesWithBox(&boxC, &boxA));
-    TEST_TRUE(collidesWithBox(&boxC, &boxB));
-    TEST_TRUE(collidesWithBox(&boxA, &boxC));
-    TEST_TRUE(collidesWithBox(&boxB, &boxC));
-    TEST_FALSE(collidesWithBox(&boxA, &boxB));
-    TEST_FALSE(collidesWithBox(&boxB, &boxA));
+    Box boxA(V3(1.0f, 2.0f, 3.0f));
+    Box boxB(V3(-1.0f, -2.0f, -3.0f));
+    Box boxC(V3(2.0f, 2.0f, 2.0f));
+    boxC = boxC.offset(V3(-1.0f, -1.0f, -1.0f));
+    TEST_TRUE(boxC.collidesWith(boxA));
+    TEST_TRUE(boxC.collidesWith(boxB));
+    TEST_TRUE(boxA.collidesWith(boxC));
+    TEST_TRUE(boxB.collidesWith(boxC));
+    TEST_FALSE(boxA.collidesWith(boxB));
+    TEST_FALSE(boxB.collidesWith(boxA));
 }
 
 static void testExpand() {
-    Box box = BOX;
-    setBox(&box, &V(1.0f, 2.0f, 3.0f));
-    expandBox(&box, &V(7.0f, -4.0f, 6.0f));
-
-    char buffer[128];
-    toStringBox(&box, buffer, sizeof(buffer));
-    TEST_STRING("Box([0.000, -4.000, 0.000], [8.000, 2.000, 9.000])", buffer);
-
-    setBox(&box, &V(1.0f, 2.0f, 3.0f));
-    expandBox(&box, &V(-7.0f, 4.0f, -6.0f));
-    toStringBox(&box, buffer, sizeof(buffer));
-    TEST_STRING("Box([-7.000, 0.000, -6.000], [1.000, 6.000, 3.000])", buffer);
+    Box box(V3(1.0f, 2.0f, 3.0f));
+    box = box.expand(V3(7.0f, -4.0f, 6.0f));
+    TEST_STRING("Box([0.00, -4.00, 0.00], [8.00, 2.00, 9.00])", box);
+    box = Box(V3(1.0f, 2.0f, 3.0f));
+    box = box.expand(V3(-7.0f, 4.0f, -6.0f));
+    TEST_STRING("Box([-7.00, 0.00, -6.00], [1.00, 6.00, 3.00])", box);
 }
 
 static void testGrow() {
-    Box box = BOX;
-    setBox(&box, &V(1.0f, 2.0f, 3.0f));
-    growBox(&box, &V(4.0f, 2.0f, 6.0f));
-    char buffer[128];
-    toStringBox(&box, buffer, sizeof(buffer));
-    TEST_STRING("Box([-2.000, -1.000, -3.000], [3.000, 3.000, 6.000])", buffer);
-
-    setBox(&box, &V(1.0f, 2.0f, 3.0f));
-    growBox(&box, &V(-4.0f, -2.0f, -6.0f));
-    toStringBox(&box, buffer, sizeof(buffer));
-    TEST_STRING("Box([0.500, 1.000, 1.500], [0.500, 1.000, 1.500])", buffer);
-
-    setBox(&box, &V(1.0f, 2.0f, 3.0f));
-    growBox(&box, &V(-0.1f, -4.0f, -1.0f));
-    toStringBox(&box, buffer, sizeof(buffer));
-    TEST_STRING("Box([0.050, 1.000, 0.500], [0.950, 1.000, 2.500])", buffer);
+    Box box(V3(1.0f, 2.0f, 3.0f));
+    TEST_STRING(
+        "Box([-2.00, -1.00, -3.00], [3.00, 3.00, 6.00])",
+        box.grow(V3(4.0f, 2.0f, 6.0f)));
+    TEST_STRING(
+        "Box([0.50, 1.00, 1.50], [0.50, 1.00, 1.50])",
+        box.grow(V3(-4.0f, -2.0f, -6.0f)));
+    TEST_STRING(
+        "Box([0.05, 1.00, 0.50], [0.95, 1.00, 2.50])",
+        box.grow(V3(-0.1f, -4.0f, -1.0f)));
 }
 
 void testBox() {