Working on groups
This commit is contained in:
@@ -5,7 +5,7 @@ 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)
|
||||
shape_test.cpp plane_test.cpp pattern_test.cpp cube_test.cpp cylinder_test.cpp cone_test.cpp group_test.cpp)
|
||||
|
||||
add_executable(testMyRays)
|
||||
target_include_directories(testMyRays PUBLIC ${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
|
||||
|
||||
82
tests/group_test.cpp
Normal file
82
tests/group_test.cpp
Normal file
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* DoRayMe - a quick and dirty Raytracer
|
||||
* Group unit tests
|
||||
*
|
||||
* Created by Manoël Trapier
|
||||
* Copyright (c) 2020 986-Studio.
|
||||
*
|
||||
*/
|
||||
#include <intersect.h>
|
||||
#include <intersection.h>
|
||||
#include <group.h>
|
||||
#include <testshape.h>
|
||||
#include <sphere.h>
|
||||
|
||||
#include <transformation.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(GroupTest, Creating_a_new_group)
|
||||
{
|
||||
Group g = Group();
|
||||
|
||||
ASSERT_EQ(g.transformMatrix, Matrix4().identity());
|
||||
ASSERT_TRUE(g.isEmpty());
|
||||
}
|
||||
|
||||
TEST(GroupTest, Adding_a_child_to_a_group)
|
||||
{
|
||||
Group g = Group();
|
||||
TestShape s = TestShape();
|
||||
|
||||
g.addObject(&s);
|
||||
|
||||
ASSERT_FALSE(g.isEmpty());
|
||||
ASSERT_EQ(s.parent, &g);
|
||||
ASSERT_EQ(g[0], &s);
|
||||
}
|
||||
|
||||
TEST(GroupTest, Intersecting_a_ray_with_an_empty_group)
|
||||
{
|
||||
Group g = Group();
|
||||
Ray r = Ray(Point(0, 0, 0), Vector(0, 0, 1));
|
||||
Intersect xs = g.intersect(r);
|
||||
ASSERT_EQ(xs.count(), 0);
|
||||
}
|
||||
|
||||
TEST(GroupTest, Intersecting_a_ray_with_an_nonempty_group)
|
||||
{
|
||||
Group g = Group();
|
||||
Sphere s1 = Sphere();
|
||||
Sphere s2 = Sphere();
|
||||
Sphere s3 = Sphere();
|
||||
|
||||
s2.setTransform(translation(0, 0, -3));
|
||||
s3.setTransform(translation(5, 0, 0));
|
||||
|
||||
g.addObject(&s1);
|
||||
g.addObject(&s2);
|
||||
g.addObject(&s3);
|
||||
|
||||
Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
|
||||
Intersect xs = g.intersect(r);
|
||||
ASSERT_EQ(xs.count(), 4);
|
||||
EXPECT_EQ(xs[0].object, &s2);
|
||||
EXPECT_EQ(xs[1].object, &s2);
|
||||
EXPECT_EQ(xs[2].object, &s1);
|
||||
EXPECT_EQ(xs[3].object, &s1);
|
||||
}
|
||||
|
||||
TEST(GroupTest, Intersecting_a_transformer_group)
|
||||
{
|
||||
Group g = Group();
|
||||
Sphere s = Sphere();
|
||||
|
||||
g.setTransform(scaling(2, 2, 2));
|
||||
s.setTransform(translation(5, 0, 0));
|
||||
|
||||
g.addObject(&s);
|
||||
|
||||
Ray r = Ray(Point(10, 0, -50), Vector(0, 0, 1));
|
||||
Intersect xs = g.intersect(r);
|
||||
ASSERT_EQ(xs.count(), 2);
|
||||
}
|
||||
@@ -9,6 +9,8 @@
|
||||
#include <shape.h>
|
||||
#include <testshape.h>
|
||||
#include <matrix.h>
|
||||
#include <group.h>
|
||||
#include <sphere.h>
|
||||
#include <transformation.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
@@ -94,5 +96,49 @@ TEST(ShapeTest, Computing_the_normal_on_a_tranformed_shape)
|
||||
|
||||
ASSERT_EQ(n, Vector(0, 0.97014, -0.24254));
|
||||
|
||||
set_equal_precision(FLT_EPSILON);
|
||||
}
|
||||
|
||||
TEST(ShapeTest, A_shape_has_a_parent_attribute)
|
||||
{
|
||||
TestShape s = TestShape();
|
||||
|
||||
ASSERT_EQ(s.parent, nullptr);
|
||||
}
|
||||
|
||||
TEST(TestShape, Converting_a_point_from_world_to_object_space)
|
||||
{
|
||||
Group g1 = Group();
|
||||
g1.setTransform(rotationY(M_PI / 2));
|
||||
Group g2 = Group();
|
||||
g2.setTransform(scaling(2, 2, 2));
|
||||
g1.addObject(&g2);
|
||||
Sphere s = Sphere();
|
||||
s.setTransform(translation(5, 0, 0));
|
||||
g2.addObject(&s);
|
||||
|
||||
Tuple p = s.worldToObject(Point(-2, 0, -10));
|
||||
|
||||
ASSERT_EQ(p, Point(0, 0, -1));
|
||||
}
|
||||
|
||||
TEST(TestShape, Converting_a_normal_form_object_to_world_space)
|
||||
{
|
||||
Group g1 = Group();
|
||||
g1.setTransform(rotationY(M_PI / 2));
|
||||
Group g2 = Group();
|
||||
g2.setTransform(scaling(1, 2, 3));
|
||||
g1.addObject(&g2);
|
||||
Sphere s = Sphere();
|
||||
s.setTransform(translation(5, 0, 0));
|
||||
g2.addObject(&s);
|
||||
|
||||
Tuple p = s.normalToWorld(Point(sqrt(3)/3, sqrt(3)/3, sqrt(3)/3));
|
||||
|
||||
/* Temporary lower the precision */
|
||||
set_equal_precision(0.0001);
|
||||
|
||||
ASSERT_EQ(p, Point(0.2857, 0.4286, -0.8571));
|
||||
|
||||
set_equal_precision(FLT_EPSILON);
|
||||
}
|
||||
Reference in New Issue
Block a user