Browse Source

Boundingboxes should be ready.

Next step (later) would be to properly use them other than group to lower the number of intersection calculation per ray.
Godzil 4 years ago
parent
commit
2ea4abdce7

+ 87 - 2
source/include/boundingbox.h

@@ -11,11 +11,96 @@
 
 struct BoundingBox
 {
+private:
+    bool isReset;
+
+public:
     Tuple min;
     Tuple max;
 
-    BoundingBox() : min(-0, -0, -0), max(0, 0, 0) { };
-    BoundingBox(Tuple min, Tuple max) : min(min), max(max) { };
+    BoundingBox() : min(INFINITY, INFINITY, INFINITY, 1.0), max(-INFINITY, -INFINITY, -INFINITY, 1.0), isReset(true) { };
+    BoundingBox(Tuple min, Tuple max) : min(min), max(max), isReset(false) { };
+
+    void operator|(const BoundingBox &b) {
+        isReset = false;
+
+        if (this->min.x > b.min.x) { this->min.x = b.min.x; }
+        if (this->min.y > b.min.y) { this->min.y = b.min.y; }
+        if (this->min.z > b.min.z) { this->min.z = b.min.z; }
+
+        if (this->max.x < b.max.x) { this->max.x = b.max.x; }
+        if (this->max.y < b.max.y) { this->max.y = b.max.y; }
+        if (this->max.z < b.max.z) { this->max.z = b.max.z; }
+    }
+
+    bool haveFiniteBounds() { return this->min.isRepresentable() && this->max.isRepresentable(); };
+
+    bool fitsIn(const BoundingBox &other) {
+        bool fits = true;
+
+        if (this->min.x > other.min.x) { fits = false; }
+        if (this->min.y > other.min.y) { fits = false; }
+        if (this->min.z > other.min.z) { fits = false; }
+
+        if (this->max.x < other.max.x) { fits = false; }
+        if (this->max.y < other.max.y) { fits = false; }
+        if (this->max.z < other.max.z) { fits = false; }
+
+        return fits;
+    }
+
+    void checkAxis(double axeOrigin, double axeDirection, double xMin, double xMax, double *axeMin, double *axeMax)
+    {
+        double tMinNumerator = (xMin - axeOrigin);
+        double tMaxNumerator = (xMax - axeOrigin);
+
+        if (fabs(axeDirection) >= getEpsilon())
+        {
+            *axeMin = tMinNumerator / axeDirection;
+            *axeMax = tMaxNumerator / axeDirection;
+        }
+        else
+        {
+            *axeMin = tMinNumerator * INFINITY;
+            *axeMax = tMaxNumerator * INFINITY;
+        }
+
+        if (*axeMin > *axeMax)
+        {
+            double swap = *axeMax;
+            *axeMax = *axeMin;
+            *axeMin = swap;
+        }
+    }
+
+    void reset()
+    {
+        this->isReset = true;
+        min.x = min.y = min.z = INFINITY;
+        max.x = max.y = max.z = -INFINITY;
+    }
+
+    bool isEmpty() { return this->isReset; };
+
+    bool intesectMe(Ray r) {
+
+        double xtMin, xtMax, ytMin, ytMax, ztMin, ztMax;
+        double tMin, tMax;
+
+        this->checkAxis(r.origin.x, r.direction.x, this->min.x, this->max.x, &xtMin, &xtMax);
+        this->checkAxis(r.origin.y, r.direction.y, this->min.y, this->max.y, &ytMin, &ytMax);
+        this->checkAxis(r.origin.z, r.direction.z, this->min.z, this->max.z, &ztMin, &ztMax);
+
+        tMin = max3(xtMin, ytMin, ztMin);
+        tMax = min3(xtMax, ytMax, ztMax);
+
+        if (tMin <= tMax)
+        {
+            return true;
+        }
+        return false;
+    }
+
 };
 
 #endif //DORAYME_BOUNDINGBOX_H

+ 1 - 0
source/include/cone.h

@@ -29,6 +29,7 @@ public:
 
     Cone() : minCap(-INFINITY), maxCap(INFINITY), isClosed(false), Shape(SHAPE_CONE) {};
     BoundingBox getBounds();
+    bool haveFiniteBounds() { return !(isinf(this->minCap) || isinf(this->maxCap)); };
 };
 
 #endif /* DORAYME_CONE_H */

+ 1 - 0
source/include/cylinder.h

@@ -30,6 +30,7 @@ public:
     Cylinder() : minCap(-INFINITY), maxCap(INFINITY), isClosed(false), Shape(SHAPE_CYLINDER) {};
 
     BoundingBox getBounds();
+    bool haveFiniteBounds() { return !(isinf(this->minCap) || isinf(this->maxCap)); };
 };
 
 #endif //DORAYME_CYLINDER_H

+ 9 - 1
source/include/group.h

@@ -11,6 +11,8 @@
 
 #include <shape.h>
 
+/* TODO: Add a way to force(?) material from group to be applied on childs */
+
 class Group : public Shape
 {
 private:
@@ -18,11 +20,15 @@ private:
     Shape* *objectList;
     uint32_t objectCount;
 
+    uint32_t allocatedUnboxableObjectCount;
+    Shape* *unboxableObjectList;
+    uint32_t unboxableObjectCount;
+
 protected:
     Intersect localIntersect(Ray r);
     Tuple localNormalAt(Tuple point);
 
-
+    BoundingBox bounds;
 
 public:
     bool isEmpty();
@@ -34,6 +40,8 @@ public:
 
     BoundingBox getBounds();
 
+    void updateBoundingBox();
+
     Group();
 };
 

+ 1 - 0
source/include/plane.h

@@ -18,6 +18,7 @@ private:
 public:
     Plane() : Shape(SHAPE_PLANE) { };
     BoundingBox getBounds();
+    bool haveFiniteBounds() { return false; };
 };
 
 #endif //DORAYME_PLANE_H

+ 3 - 1
source/include/shape.h

@@ -53,10 +53,12 @@ public:
     Shape(ShapeType = SHAPE_NONE);
 
     virtual Intersect intersect(Ray r);
+    virtual Intersect intersectOOB(Ray r) { return this->intersect(r); };
     Tuple normalAt(Tuple point);
 
-    /* Bouding box points are always world value */
+    /* Bounding box points are always world value */
     virtual BoundingBox getBounds();
+    virtual bool haveFiniteBounds() { return true; };
 
     void updateTransform();
     Tuple worldToObject(Tuple point) { return this->inverseTransform * point; };

+ 4 - 0
source/include/tuple.h

@@ -40,6 +40,10 @@ public:
     Tuple operator/(const double &b) const { return Tuple(this->x / b, this->y / b,
                                                           this->z / b, this->w / b); };
 
+    void fixPoint();
+    void fixVector();
+    bool isRepresentable();
+
     void set(double nX, double nY, double nZ) { this->x = nX; this->y = nY; this->z = nZ; };
     double magnitude();
     Tuple normalise();

+ 3 - 0
source/math_helper.cpp

@@ -25,6 +25,9 @@ double getEpsilon()
 
 bool double_equal(double a, double b)
 {
+    if (isinf(a) && isinf(b))
+        return true;
+
     return fabs(a - b) < current_precision;
 }
 

+ 4 - 1
source/shapes/cone.cpp

@@ -129,10 +129,13 @@ BoundingBox Cone::getBounds()
 
     double a = fabs(this->minCap);
     double b = fabs(this->maxCap);
-    double limit = (a < b)?a:b;
+    double limit = (a > b)?a:b;
 
     ret.min = this->objectToWorld(Point(-limit, this->minCap, -limit));
     ret.max = this->objectToWorld(Point(limit, this->maxCap, limit));
 
+    ret.min.fixPoint();
+    ret.max.fixPoint();
+
     return ret;
 }

+ 3 - 0
source/shapes/cylinder.cpp

@@ -115,5 +115,8 @@ BoundingBox Cylinder::getBounds()
     ret.min = this->objectToWorld(Point(-1, this->minCap, -1));
     ret.max = this->objectToWorld(Point(1, this->maxCap, 1));
 
+    ret.min.fixPoint();
+    ret.max.fixPoint();
+
     return ret;
 }

+ 64 - 25
source/shapes/group.cpp

@@ -19,6 +19,11 @@ Group::Group() : Shape(SHAPE_GROUP)
     this->allocatedObjectCount = MIN_ALLOC;
     this->objectList = (Shape **)calloc(sizeof(Shape *), MIN_ALLOC);
     this->objectCount = 0;
+
+    this->allocatedUnboxableObjectCount = MIN_ALLOC;
+    this->unboxableObjectList = (Shape **)calloc(sizeof(Shape *), MIN_ALLOC);
+    this->unboxableObjectCount = 0;
+
 }
 
 Intersect Group::intersect(Ray r)
@@ -27,9 +32,28 @@ Intersect Group::intersect(Ray r)
     int i, j;
     if (this->objectCount > 0)
     {
-        for(i = 0; i < this->objectCount; i++)
+        if (this->bounds.intesectMe(r))
+        {
+            for (i = 0 ; i < this->objectCount ; i++)
+            {
+                Intersect xs = this->objectList[i]->intersect(r);
+                if (xs.count() > 0)
+                {
+                    for (j = 0 ; j < xs.count() ; j++)
+                    {
+                        ret.add(xs[j]);
+                    }
+                }
+            }
+        }
+    }
+
+    /* We are force to do them all the time */
+    if (this->unboxableObjectCount > 0)
+    {
+        for(i = 0; i < this->unboxableObjectCount; i++)
         {
-            Intersect xs = this->objectList[i]->intersect(r);
+            Intersect xs = this->unboxableObjectList[i]->intersect(r);
             if (xs.count() > 0)
             {
                 for(j = 0; j < xs.count(); j++)
@@ -39,7 +63,6 @@ Intersect Group::intersect(Ray r)
             }
         }
     }
-
     return ret;
 }
 
@@ -53,48 +76,64 @@ Tuple Group::localNormalAt(Tuple point)
     return Vector(1, 0, 0);
 }
 
+/* ONLY INSERT SHAPES THAT ARE NOT GOING TO CHANGE ELSE..! */
 void Group::addObject(Shape *s)
 {
-    if ((this->objectCount + 1) > this->allocatedObjectCount)
+    if (s->haveFiniteBounds())
     {
-        this->allocatedObjectCount *= 2;
-        this->objectList = (Shape **)realloc(this->objectList, sizeof(Shape **) * this->allocatedObjectCount);
+        if ((this->objectCount + 1) > this->allocatedObjectCount)
+        {
+            this->allocatedObjectCount *= 2;
+            this->objectList = (Shape **)realloc(this->objectList, sizeof(Shape **) * this->allocatedObjectCount);
+        }
+
+        s->parent = this;
+        s->updateTransform();
+
+        this->objectList[this->objectCount++] = s;
+
+        this->bounds | s->getBounds();
+
     }
+    else
+    {
+        if ((this->unboxableObjectCount + 1) > this->allocatedUnboxableObjectCount)
+        {
+            this->allocatedUnboxableObjectCount *= 2;
+            this->unboxableObjectList = (Shape **)realloc(this->unboxableObjectList, sizeof(Shape **) * this->allocatedUnboxableObjectCount);
+        }
 
-    s->parent = this;
-    s->updateTransform();
+        s->parent = this;
+        s->updateTransform();
 
-    this->objectList[this->objectCount++] = s;
+        this->unboxableObjectList[this->unboxableObjectCount++] = s;
+    }
 }
 
 bool Group::isEmpty()
 {
-    return (this->objectCount == 0);
+    return (this->objectCount == 0) && (this->unboxableObjectCount == 0);
 }
 
 BoundingBox Group::getBounds()
 {
-    BoundingBox ret;
+    if (this->bounds.isEmpty()) { this->updateBoundingBox(); }
+    return this->bounds;
+}
 
+void Group::updateBoundingBox()
+{
+    this->bounds.reset();
     if (this->objectCount > 0)
     {
-        ret.min = Point(INFINITY, INFINITY, INFINITY);
-        ret.max = Point(-INFINITY, -INFINITY, -INFINITY);
-
         int i;
         for(i = 0; i < this->objectCount; i++)
         {
-            BoundingBox obj = this->objectList[i]->getBounds();
-
-            if (ret.min.x > obj.min.x) {  ret.min.x = obj.min.x; }
-            if (ret.min.y > obj.min.y) {  ret.min.y = obj.min.y; }
-            if (ret.min.z > obj.min.z) {  ret.min.z = obj.min.z; }
-
-            if (ret.max.x < obj.max.x) {  ret.max.x = obj.max.x; }
-            if (ret.max.y < obj.max.y) {  ret.max.y = obj.max.y; }
-            if (ret.max.z < obj.max.z) {  ret.max.z = obj.max.z; }
+            if (!this->objectList[i]->haveFiniteBounds())
+            {
+                BoundingBox objB = this->objectList[i]->getBounds();
+                this->bounds | objB;
+            }
         }
     }
-
-    return ret;
 }

+ 3 - 0
source/shapes/plane.cpp

@@ -42,5 +42,8 @@ BoundingBox Plane::getBounds()
     ret.min = this->objectToWorld(Point(-INFINITY, 0-getEpsilon(), -INFINITY));
     ret.max = this->objectToWorld(Point(INFINITY, 0+getEpsilon(), INFINITY));
 
+    ret.min.fixPoint();
+    ret.max.fixPoint();
+
     return ret;
 }

+ 24 - 0
source/tuple.cpp

@@ -43,4 +43,28 @@ Tuple Tuple::cross(const Tuple &b) const
 Tuple Tuple::reflect(const Tuple &normal)
 {
     return *this - normal * 2 * this->dot(normal);
+}
+
+void Tuple::fixPoint()
+{
+    if (isnan(this->x) || isnan(this->y) || isnan(this->z))
+    {
+        /* w is probably broken, so fix it */
+        this->w = 1;
+    }
+}
+
+void Tuple::fixVector()
+{
+
+    if (isnan(this->x) || isnan(this->y) || isnan(this->z))
+    {
+        /* w is probably broken, so fix it */
+        this->w = 0;
+    }
+}
+
+bool Tuple::isRepresentable()
+{
+    return !(isnan(this->x) || isnan(this->y) || isnan(this->z));
 }

+ 2 - 1
tests/CMakeLists.txt

@@ -5,7 +5,8 @@ find_package(Threads REQUIRED)
 
 set(TESTS_SRC math_test.cpp tuple_test.cpp colour_test.cpp canvas_test.cpp matrix_test.cpp transformation_test.cpp
         ray_test.cpp intersect_test.cpp sphere_test.cpp light_test.cpp material_test.cpp world_test.cpp camera_test.cpp
-        shape_test.cpp plane_test.cpp pattern_test.cpp cube_test.cpp cylinder_test.cpp cone_test.cpp group_test.cpp)
+        shape_test.cpp plane_test.cpp pattern_test.cpp cube_test.cpp cylinder_test.cpp cone_test.cpp group_test.cpp
+        boundingbox_test.cpp)
 
 add_executable(testMyRays)
 target_include_directories(testMyRays PUBLIC ${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})

+ 81 - 0
tests/boundingbox_test.cpp

@@ -0,0 +1,81 @@
+/*
+ *  DoRayMe - a quick and dirty Raytracer
+ *  Boundingbox unit tests
+ *
+ *  Created by Manoël Trapier
+ *  Copyright (c) 2020 986-Studio.
+ *
+ */
+/*
+ *  DoRayMe - a quick and dirty Raytracer
+ *  Camera unit tests
+ *
+ *  Created by Manoël Trapier
+ *  Copyright (c) 2020 986-Studio.
+ *
+ */
+#include <math.h>
+#include <math_helper.h>
+#include <ray.h>
+#include <transformation.h>
+#include <stdint.h>
+#include <boundingbox.h>
+#include <gtest/gtest.h>
+
+TEST(BoundingBox, Default_boundingbox_is_not_set)
+{
+    BoundingBox bb;
+
+    ASSERT_TRUE(bb.isEmpty());
+    ASSERT_EQ(bb.min, Point(INFINITY, INFINITY, INFINITY));
+    ASSERT_EQ(bb.max, Point(-INFINITY, -INFINITY, -INFINITY));
+}
+
+TEST(BoundingBox, Bounding_box_can_be_created_with_values)
+{
+    BoundingBox bb = BoundingBox(Point(-1, -1, -1), Point(1, 1, 1));
+
+    ASSERT_FALSE(bb.isEmpty());
+    ASSERT_EQ(bb.min, Point(-1, -1, -1));
+    ASSERT_EQ(bb.max, Point(1, 1, 1));
+}
+
+TEST(BoundingBox, Cating_a_bb_to_an_empty_bb_reset_the_original_one)
+{
+    BoundingBox bb;
+
+    bb | BoundingBox(Point(-1, -1, -1), Point(1, 1, 1));
+
+    ASSERT_FALSE(bb.isEmpty());
+    ASSERT_EQ(bb.min, Point(-1, -1, -1));
+    ASSERT_EQ(bb.max, Point(1, 1, 1));
+}
+
+TEST(BoundingBox, Cating_a_bb_to_another_bb_expand_the_original_one_if_needed)
+{
+    BoundingBox bb(Point(-1, -1, -1), Point(1, 1, 1));
+
+    bb | BoundingBox(Point(-2, 0, -5), Point(4, 5, 0.5));
+
+    ASSERT_FALSE(bb.isEmpty());
+    ASSERT_EQ(bb.min, Point(-2, -1, -5));
+    ASSERT_EQ(bb.max, Point(4, 5, 1));
+}
+
+TEST(BoundingBox, A_smaller_bb_should_fit_in_a_bigger)
+{
+    BoundingBox bigBb = BoundingBox(Point(-10, -10, -10), Point(10, 10, 10));
+
+    BoundingBox smallBb = BoundingBox(Point(-2, -2, -2), Point(2, 2, 2));
+
+    ASSERT_TRUE(bigBb.fitsIn(smallBb));
+}
+
+TEST(BoundingBox, A_big_bb_should_not_fit_in_a_smaller)
+{
+    BoundingBox bigBb = BoundingBox(Point(-10, -10, -10), Point(10, 10, 10));
+
+    BoundingBox smallBb = BoundingBox(Point(-2, -2, -2), Point(2, 2, 2));
+
+    ASSERT_FALSE(smallBb.fitsIn(bigBb));
+}

+ 38 - 0
tests/cone_test.cpp

@@ -130,3 +130,41 @@ TEST(ConeTest, Computing_the_normal_vector_on_a_cone)
         ASSERT_EQ(cone.doLocalNormalAt(HitPointss[i]), Normals[i]);
     }
 }
+
+TEST(ConeTest, The_bounding_box_of_a_cut_cone)
+{
+    Cone t = Cone();
+    BoundingBox b = BoundingBox(Point(-8, -5, -8), Point(8, 8, 8));
+    t.minCap = -5;
+    t.maxCap = 8;
+    BoundingBox res = t.getBounds();
+
+    ASSERT_EQ(res.min, b.min);
+    ASSERT_EQ(res.max, b.max);
+}
+
+TEST(ConeTest, The_bounding_box_of_a_uncut_cone)
+{
+    /* This one is tricky. Infinite size don't cope well with transformations */
+    Cone t = Cone();
+    BoundingBox res = t.getBounds();
+
+    ASSERT_FALSE(res.min.isRepresentable());
+    ASSERT_FALSE(res.max.isRepresentable());
+}
+
+TEST(ConeTest, An_uncut_cone_have_infinite_bounds)
+{
+    Cone t = Cone();
+
+    ASSERT_FALSE(t.haveFiniteBounds());
+}
+
+TEST(ConeTest, A_cut_cone_have_finite_bounds)
+{
+    Cone t = Cone();
+    t.minCap = -1;
+    t.maxCap = 1;
+
+    ASSERT_TRUE(t.haveFiniteBounds());
+}

+ 8 - 1
tests/cube_test.cpp

@@ -116,7 +116,7 @@ TEST(CubeTest, The_normal_on_the_surface_of_a_cube)
     }
 }
 
-TEST(Cube, The_bounding_box_of_a_cube)
+TEST(CubeTest, The_bounding_box_of_a_cube)
 {
     Cube t = Cube();
     BoundingBox b = BoundingBox(Point(-1, -1, -1), Point(1, 1, 1));
@@ -125,4 +125,11 @@ TEST(Cube, The_bounding_box_of_a_cube)
 
     ASSERT_EQ(res.min, b.min);
     ASSERT_EQ(res.max, b.max);
+}
+
+TEST(CubeTest, A_cube_have_finite_bounds)
+{
+    Cube t = Cube();
+
+    ASSERT_TRUE(t.haveFiniteBounds());
 }

+ 27 - 1
tests/cylinder_test.cpp

@@ -222,7 +222,7 @@ TEST(CylinderTest, The_normal_on_a_cylinder_end_cap)
     }
 }
 
-TEST(CylinderTest, The_bounding_box_of_an_uncut_cylinder)
+TEST(CylinderTest, The_bounding_box_of_a_cut_cylinder)
 {
     Cylinder t = Cylinder();
     BoundingBox b = BoundingBox(Point(-1, -10000, -1), Point(1, 10000, 1));
@@ -232,4 +232,30 @@ TEST(CylinderTest, The_bounding_box_of_an_uncut_cylinder)
 
     ASSERT_EQ(res.min, b.min);
     ASSERT_EQ(res.max, b.max);
+}
+
+TEST(CylinderTest, The_bounding_box_of_a_uncut_cylinder)
+{
+    /* This one is tricky. Infinite size don't cope well with transformations */
+    Cylinder t = Cylinder();
+    BoundingBox res = t.getBounds();
+
+    ASSERT_FALSE(res.min.isRepresentable());
+    ASSERT_FALSE(res.max.isRepresentable());
+}
+
+TEST(CylinderTest, An_uncut_cylinder_have_infinite_bounds)
+{
+    Cylinder t = Cylinder();
+
+    ASSERT_FALSE(t.haveFiniteBounds());
+}
+
+TEST(CylinderTest, A_cut_cylinder_have_finite_bounds)
+{
+    Cylinder t = Cylinder();
+    t.minCap = -1;
+    t.maxCap = 1;
+
+    ASSERT_TRUE(t.haveFiniteBounds());
 }

+ 19 - 1
tests/group_test.cpp

@@ -66,7 +66,7 @@ TEST(GroupTest, Intersecting_a_ray_with_an_nonempty_group)
     EXPECT_EQ(xs[3].object, &s1);
 }
 
-TEST(GroupTest, Intersecting_a_transformer_group)
+TEST(GroupTest, Intersecting_a_transformed_group)
 {
     Group g = Group();
     Sphere s = Sphere();
@@ -79,4 +79,22 @@ TEST(GroupTest, Intersecting_a_transformer_group)
     Ray r = Ray(Point(10, 0, -50), Vector(0, 0, 1));
     Intersect xs = g.intersect(r);
     ASSERT_EQ(xs.count(), 2);
+}
+
+TEST(GroupTest, Group_bounding_box)
+{
+    Group g = Group();
+    Sphere s = Sphere();
+
+    g.setTransform(scaling(2, 2, 2));
+    s.setTransform(translation(5, 0, 0));
+
+    g.addObject(&s);
+
+    BoundingBox b = BoundingBox(Point(8, -2, -2), Point(12, 2, 2));
+
+    BoundingBox res = g.getBounds();
+
+    ASSERT_EQ(res.min, b.min);
+    ASSERT_EQ(res.max, b.max);
 }

+ 17 - 0
tests/plane_test.cpp

@@ -68,4 +68,21 @@ TEST(PlaneTest, A_ray_intersecting_a_plane_from_below)
     ASSERT_EQ(xs.count(), 1);
     ASSERT_EQ(xs[0].t, 1);
     ASSERT_EQ(xs[0].object, &p);
+}
+
+TEST(PlaneTest, The_bounding_box_of_a_plane)
+{
+    Plane t = Plane();
+    BoundingBox b = BoundingBox(Point(-8, -5, -8), Point(8, 8, 8));
+    BoundingBox res = t.getBounds();
+
+    ASSERT_FALSE(res.min.isRepresentable());
+    ASSERT_FALSE(res.max.isRepresentable());
+}
+
+TEST(PlaneTest, A_plane_have_infinite__bounds)
+{
+    Plane t = Plane();
+
+    ASSERT_FALSE(t.haveFiniteBounds());
 }

+ 7 - 0
tests/sphere_test.cpp

@@ -218,4 +218,11 @@ TEST(SphereTest, The_bounding_box_of_a_sphere)
 
     ASSERT_EQ(res.min, b.min);
     ASSERT_EQ(res.max, b.max);
+}
+
+TEST(SphereTest, A_sphere_have_finite_bounds)
+{
+    Sphere t = Sphere();
+
+    ASSERT_TRUE(t.haveFiniteBounds());
 }