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 testFlatRenderer(self): sphere_color = Color(1.0, 2.0, 3.0) sphere = Sphere(transformation=translation(Vec(2, 0, 0)) * scaling(Vec(0.2, 0.2, 0.2)), material=Material(brdf=DiffuseBRDF( pigment=UniformPigment(sphere_color)))) image = HdrImage(width=3, height=3) camera = OrthogonalCamera() tracer = ImageTracer(image=image, camera=camera) world = World() world.add_shape(sphere) renderer = FlatRenderer(world=world) tracer.fire_all_rays(renderer) assert image.get_pixel(0, 0).is_close(BLACK) assert image.get_pixel(1, 0).is_close(BLACK) assert image.get_pixel(2, 0).is_close(BLACK) assert image.get_pixel(0, 1).is_close(BLACK) assert image.get_pixel(1, 1).is_close(sphere_color) assert image.get_pixel(2, 1).is_close(BLACK) assert image.get_pixel(0, 2).is_close(BLACK) assert image.get_pixel(1, 2).is_close(BLACK) assert image.get_pixel(2, 2).is_close(BLACK)
def test_normalize_image(self): img = HdrImage(2, 1) img.set_pixel(0, 0, Color(0.5e1, 1.0e1, 1.5e1)) img.set_pixel(1, 0, Color(0.5e3, 1.0e3, 1.5e3)) img.normalize_image(factor=1000.0, luminosity=100.0) assert img.get_pixel(0, 0).is_close(Color(0.5e2, 1.0e2, 1.5e2)) assert img.get_pixel(1, 0).is_close(Color(0.5e4, 1.0e4, 1.5e4))
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
def test_get_set_pixel(self): img = HdrImage(7, 4) reference_color = Color(1.0, 2.0, 3.0) img.set_pixel(3, 2, reference_color) assert reference_color.is_close(img.get_pixel(3, 2))