From 831a09628116091bed7ef59f9102e0d1c441d9dd Mon Sep 17 00:00:00 2001 From: Godzil Date: Tue, 25 Feb 2020 09:20:38 +0000 Subject: [PATCH] Continue working on bounding boxes. --- source/shapes/cone.cpp | 8 ++++++-- tests/cube_test.cpp | 11 +++++++++++ tests/cylinder_test.cpp | 12 ++++++++++++ tests/shape_test.cpp | 34 ++++++++++++++++++++++++++++++---- tests/sphere_test.cpp | 11 +++++++++++ tests/test_render.cpp | 2 +- 6 files changed, 71 insertions(+), 7 deletions(-) diff --git a/source/shapes/cone.cpp b/source/shapes/cone.cpp index 1e694f2..5d22394 100644 --- a/source/shapes/cone.cpp +++ b/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; } \ No newline at end of file diff --git a/tests/cube_test.cpp b/tests/cube_test.cpp index 19250e3..24ccf6b 100644 --- a/tests/cube_test.cpp +++ b/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); } \ No newline at end of file diff --git a/tests/cylinder_test.cpp b/tests/cylinder_test.cpp index af781c1..d38c2a8 100644 --- a/tests/cylinder_test.cpp +++ b/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); } \ No newline at end of file diff --git a/tests/shape_test.cpp b/tests/shape_test.cpp index c306073..fd7ca6d 100644 --- a/tests/shape_test.cpp +++ b/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); +} \ No newline at end of file diff --git a/tests/sphere_test.cpp b/tests/sphere_test.cpp index b610209..11260ff 100644 --- a/tests/sphere_test.cpp +++ b/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); } \ No newline at end of file diff --git a/tests/test_render.cpp b/tests/test_render.cpp index 8f99f85..8e8fbd2 100644 --- a/tests/test_render.cpp +++ b/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)));