Browse Source

Continue working on bounding boxes.

Godzil 4 years ago
parent
commit
831a096281
6 changed files with 71 additions and 7 deletions
  1. 6 2
      source/shapes/cone.cpp
  2. 11 0
      tests/cube_test.cpp
  3. 12 0
      tests/cylinder_test.cpp
  4. 30 4
      tests/shape_test.cpp
  5. 11 0
      tests/sphere_test.cpp
  6. 1 1
      tests/test_render.cpp

+ 6 - 2
source/shapes/cone.cpp

@@ -127,8 +127,12 @@ BoundingBox Cone::getBounds()
 {
     BoundingBox ret;
 
-    ret.min = this->objectToWorld(Point(-1, this->minCap, -1));
-    ret.max = this->objectToWorld(Point(1, this->maxCap, 1));
+    double a = fabs(this->minCap);
+    double b = fabs(this->maxCap);
+    double limit = (a < b)?a:b;
+
+    ret.min = this->objectToWorld(Point(-limit, this->minCap, -limit));
+    ret.max = this->objectToWorld(Point(limit, this->maxCap, limit));
 
     return ret;
 }

+ 11 - 0
tests/cube_test.cpp

@@ -114,4 +114,15 @@ TEST(CubeTest, The_normal_on_the_surface_of_a_cube)
     {
         ASSERT_EQ(c.normalAt(HitPoints[i]), ExpectedNormals[i]);
     }
+}
+
+TEST(Cube, The_bounding_box_of_a_cube)
+{
+    Cube t = Cube();
+    BoundingBox b = BoundingBox(Point(-1, -1, -1), Point(1, 1, 1));
+
+    BoundingBox res = t.getBounds();
+
+    ASSERT_EQ(res.min, b.min);
+    ASSERT_EQ(res.max, b.max);
 }

+ 12 - 0
tests/cylinder_test.cpp

@@ -220,4 +220,16 @@ TEST(CylinderTest, The_normal_on_a_cylinder_end_cap)
     {
         ASSERT_EQ(cyl.normalAt(HitPointss[idx]), Normals[idx]);
     }
+}
+
+TEST(CylinderTest, The_bounding_box_of_an_uncut_cylinder)
+{
+    Cylinder t = Cylinder();
+    BoundingBox b = BoundingBox(Point(-1, -10000, -1), Point(1, 10000, 1));
+    t.minCap = -10000;
+    t.maxCap = 10000;
+    BoundingBox res = t.getBounds();
+
+    ASSERT_EQ(res.min, b.min);
+    ASSERT_EQ(res.max, b.max);
 }

+ 30 - 4
tests/shape_test.cpp

@@ -106,7 +106,7 @@ TEST(ShapeTest, A_shape_has_a_parent_attribute)
     ASSERT_EQ(s.parent, nullptr);
 }
 
-TEST(TestShape, Converting_a_point_from_world_to_object_space)
+TEST(ShapeTest, Converting_a_point_from_world_to_object_space)
 {
     Group g1 = Group();
     g1.setTransform(rotationY(M_PI / 2));
@@ -122,7 +122,7 @@ TEST(TestShape, Converting_a_point_from_world_to_object_space)
     ASSERT_EQ(p, Point(0, 0, -1));
 }
 
-TEST(TestShape, Converting_a_normal_form_object_to_world_space)
+TEST(ShapeTest, Converting_a_normal_form_object_to_world_space)
 {
     Group g1 = Group();
     g1.setTransform(rotationY(M_PI / 2));
@@ -144,7 +144,7 @@ TEST(TestShape, Converting_a_normal_form_object_to_world_space)
 }
 
 
-TEST(TestShape, Finding_the_normal_on_a_child_object)
+TEST(ShapeTest, Finding_the_normal_on_a_child_object)
 {
     Group g1 = Group();
     g1.setTransform(rotationY(M_PI / 2));
@@ -165,7 +165,7 @@ TEST(TestShape, Finding_the_normal_on_a_child_object)
     set_equal_precision(FLT_EPSILON);
 }
 
-TEST(TestShape, Test_the_bouding_box_of_the_test_shape)
+TEST(ShapeTest, Test_the_bouding_box_of_the_test_shape)
 {
     TestShape t = TestShape();
     BoundingBox b = BoundingBox(Point(-1, -1, -1), Point(1, 1, 1));
@@ -175,3 +175,29 @@ TEST(TestShape, Test_the_bouding_box_of_the_test_shape)
     ASSERT_EQ(res.min, b.min);
     ASSERT_EQ(res.max, b.max);
 }
+
+TEST(ShapeTest, Test_the_bouding_box_of_the_scaled_shape)
+{
+    TestShape t = TestShape();
+    t.setTransform(scaling(3, 3, 3));
+
+    BoundingBox b = BoundingBox(Point(-3, -3, -3), Point(3, 3, 3));
+
+    BoundingBox res = t.getBounds();
+
+    ASSERT_EQ(res.min, b.min);
+    ASSERT_EQ(res.max, b.max);
+}
+
+TEST(ShapeTest, Test_the_bouding_box_of_the_translated_shape)
+{
+    TestShape t = TestShape();
+    t.setTransform(translation(10, 0, 0));
+
+    BoundingBox b = BoundingBox(Point(9, -1, -1), Point(11, 1, 1));
+
+    BoundingBox res = t.getBounds();
+
+    ASSERT_EQ(res.min, b.min);
+    ASSERT_EQ(res.max, b.max);
+}

+ 11 - 0
tests/sphere_test.cpp

@@ -207,4 +207,15 @@ TEST(SphereTest, A_helper_for_producing_a_sphere_with_a_glassy_material)
     ASSERT_EQ(s.transformMatrix, Matrix4().identity());
     ASSERT_EQ(s.material.transparency, 1.0);
     ASSERT_EQ(s.material.refractiveIndex, 1.5);
+}
+
+TEST(SphereTest, The_bounding_box_of_a_sphere)
+{
+    Sphere t = Sphere();
+    BoundingBox b = BoundingBox(Point(-1, -1, -1), Point(1, 1, 1));
+
+    BoundingBox res = t.getBounds();
+
+    ASSERT_EQ(res.min, b.min);
+    ASSERT_EQ(res.max, b.max);
 }

+ 1 - 1
tests/test_render.cpp

@@ -119,7 +119,7 @@ int main()
     /* ----------------------------- */
 
     /* Set the camera */
-    Camera camera = Camera(400, 200, deg_to_rad(90));
+    Camera camera = Camera(1280, 900, deg_to_rad(90));
     camera.setTransform(viewTransform(Point(-2, 2.5, -3.5),
                                       Point(2, 0, 3),
                                       Vector(0, 1, 0)));