def trace_ray(ray: Ray) -> Color: nonlocal num_of_rays point = ray.at(1) # Check that all the rays intersect the screen within the region [−1, 1] × [−1, 1] assert pytest.approx(0.0) == point.x assert -1.0 <= point.y <= 1.0 assert -1.0 <= point.z <= 1.0 num_of_rays += 1 return Color(0.0, 0.0, 0.0)
class RayTestCase(unittest.TestCase): def setUp(self): pr = np.array([0, 0, 1]) nr = np.array([0, 0, 1]) self.ray = Ray(pr, nr) def test_intersect_sphere(self): # case there is intersection sphere_position = np.array([0, 0, 4]) material = None radius = 1 sphere = Sphere(sphere_position, material, SHADER_TYPE, radius) t = self.ray.intersect(sphere) self.assertEqual(t, 2) # cases object is behind sphere_position = np.array([0, 0, -4]) material = None radius = 2 sphere = Sphere(sphere_position, material, SHADER_TYPE, radius) t = self.ray.intersect(sphere) self.assertEqual(t, -1) sphere_position = np.array([3, 24, -1]) material = None radius = 0.4 sphere = Sphere(sphere_position, material, SHADER_TYPE, radius) t = self.ray.intersect(sphere) self.assertEqual(t, -1) # case is not in the ray line sphere_position = np.array([-3.3, 12.6, 5.2]) material = None radius = 0.4 sphere = Sphere(sphere_position, material, SHADER_TYPE, radius) t = self.ray.intersect(sphere) self.assertEqual(t, -1) def test_intersect_hollow_sphere(self): # case there is intersection position = np.array([0, 0, 0]) material = None radius = 3 sphere = HollowSphere(position, material, SHADER_TYPE, radius) t = self.ray.intersect(sphere) self.assertEqual(t, 2) # cases object is behind position = np.array([0, 0, -4]) radius = 4 sphere = HollowSphere(position, material, SHADER_TYPE, radius) t = self.ray.intersect(sphere) self.assertEqual(t, -1) position = np.array([3, 24, -1]) radius = 0.4 sphere = HollowSphere(position, material, SHADER_TYPE, radius) t = self.ray.intersect(sphere) self.assertEqual(t, -1) # case is not in the ray line position = np.array([-3.3, 12.6, 5.2]) radius = 0.4 sphere = HollowSphere(position, material, SHADER_TYPE, radius) t = self.ray.intersect(sphere) self.assertEqual(t, -1) def test_intersect_plane(self): # Case there is intersection p0 = np.array([0, 0, 3]) n = np.array([0, 0, -1]) n0 = np.array([1, 0, 0]) material = None plane = Plane(p0, material, SHADER_TYPE, n, n0) t = self.ray.intersect(plane) self.assertEqual(t, 2) # Case there is not because it's parallel to the ray p0 = np.array([0, -1, 0]) n = np.array([0, 1, 0]) plane = Plane(p0, material, SHADER_TYPE, n, n0) t = self.ray.intersect(plane) self.assertEqual(t, -1) # Case there is not because it is behind p0 = np.array([0, 0, -1]) n = np.array([0, 0, 1]) plane = Plane(p0, material, SHADER_TYPE, n, n0) t = self.ray.intersect(plane) self.assertEqual(t, -1) def test_at(self): self.assertTrue(np.array_equal(self.ray.at(2), np.array([0, 0, 3])))
def test_at(self): ray = Ray(origin=Point(1.0, 2.0, 4.0), dir=Vec(4.0, 2.0, 1.0)) assert ray.at(0.0).is_close(ray.origin) assert ray.at(1.0).is_close(Point(5.0, 4.0, 5.0)) assert ray.at(2.0).is_close(Point(9.0, 6.0, 6.0))