class TestImageTracer(unittest.TestCase): def setUp(self) -> None: self.image = HdrImage(width=4, height=2) self.camera = PerspectiveCamera(aspect_ratio=2) self.tracer = ImageTracer(image=self.image, camera=self.camera) def test_orientation(self): top_left_ray = self.tracer.fire_ray(0, 0, u_pixel=0.0, v_pixel=0.0) assert Point(0.0, 2.0, 1.0).is_close(top_left_ray.at(1.0)) bottom_right_ray = self.tracer.fire_ray(3, 1, u_pixel=1.0, v_pixel=1.0) assert Point(0.0, -2.0, -1.0).is_close(bottom_right_ray.at(1.0)) def test_uv_sub_mapping(self): # Here we're cheating: we are asking `ImageTracer.fire_ray` to fire one ray *outside* # the pixel we're specifying ray1 = self.tracer.fire_ray(0, 0, u_pixel=2.5, v_pixel=1.5) ray2 = self.tracer.fire_ray(2, 1, u_pixel=0.5, v_pixel=0.5) assert ray1.is_close(ray2) def test_image_coverage(self): self.tracer.fire_all_rays(lambda ray: Color(1.0, 2.0, 3.0)) for row in range(self.image.height): for col in range(self.image.width): assert self.image.get_pixel(col, row) == Color(1.0, 2.0, 3.0)
class TestImageTracer(unittest.TestCase): def setUp(self) -> None: self.image = HdrImage(width=4, height=2) self.camera = PerspectiveCamera(aspect_ratio=2) self.tracer = ImageTracer(image=self.image, camera=self.camera) def test_orientation(self): top_left_ray = self.tracer.fire_ray(0, 0, u_pixel=0.0, v_pixel=0.0) assert Point(0.0, 2.0, 1.0).is_close(top_left_ray.at(1.0)) bottom_right_ray = self.tracer.fire_ray(3, 1, u_pixel=1.0, v_pixel=1.0) assert Point(0.0, -2.0, -1.0).is_close(bottom_right_ray.at(1.0)) def test_uv_sub_mapping(self): # Here we're cheating: we are asking `ImageTracer.fire_ray` to fire one ray *outside* # the pixel we're specifying ray1 = self.tracer.fire_ray(0, 0, u_pixel=2.5, v_pixel=1.5) ray2 = self.tracer.fire_ray(2, 1, u_pixel=0.5, v_pixel=0.5) assert ray1.is_close(ray2) def test_image_coverage(self): self.tracer.fire_all_rays(lambda ray: Color(1.0, 2.0, 3.0)) for row in range(self.image.height): for col in range(self.image.width): assert self.image.get_pixel(col, row) == Color(1.0, 2.0, 3.0) def test_antialiasing(self): num_of_rays = 0 small_image = HdrImage(width=1, height=1) camera = OrthogonalCamera(aspect_ratio=1) tracer = ImageTracer(small_image, camera, samples_per_side=10, pcg=PCG()) 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) tracer.fire_all_rays(trace_ray) # Check that the number of rays that were fired is what we expect (10²) assert num_of_rays == 100