def test_add_and_sub_equalize(self): u = Vector3(x=1, y=2, z=3) v = Vector3(x=4, y=5, z=6) actual = u.add(v).sub(v) assert actual.x == u.x assert actual.y == u.y assert actual.z == u.z
def test_cross_of_vector_is_perpendicular(self): u = Vector3(x=0.5, y=0.5, z=0) v = Vector3(x=-0.5, y=0.5, z=0) actual = u.cross(v) assert actual.x == 0 assert actual.y == 0 assert actual.z != 0
def test_not_hit_example(self): color = Color(r=0, g=0, b=0) ray = Ray(origin=Vector3(x=0, y=0, z=0), direction=Vector3(x=1, y=1, z=0)) sphere = Sphere(center=Vector3(x=2, y=0, z=0), radius=1, color=color) result = sphere.hit(ray) assert result is None
def test_add_example(self): u = Vector3(x=1, y=2, z=3) v = Vector3(x=4, y=5, z=6) actual = u.add(v) assert actual.x == 5 assert actual.y == 7 assert actual.z == 9
def test_cross_uv_and_vu_point_in_opposite_direction(self): u = Vector3(x=1, y=2, z=3) v = Vector3(x=2, y=3, z=1) c0 = u.cross(v) c1 = v.cross(u) assert c0.x == -c1.x assert c0.y == -c1.y assert c0.z == -c1.z
def test_hit_example(self): color = Color(r=0, g=0, b=0) ray = Ray(origin=Vector3(x=0, y=0, z=0), direction=Vector3(x=1, y=0, z=0)) sphere = Sphere(center=Vector3(x=2, y=0, z=0), radius=1, color=color) result = sphere.hit(ray) assert result is not None assert result.shade_record.hit_point == Vector3(x=1, y=0, z=0) assert result.tmin == 1
def test_add_and_sub_operators(self): u = Vector3(x=1, y=2, z=3) v = Vector3(x=4, y=5, z=6) actual = u + v assert actual.x == 5 assert actual.y == 7 assert actual.z == 9 actual = actual - v assert actual.x == 1 assert actual.y == 2 assert actual.z == 3
def render(self) -> List[Color]: hres = self._view_plane.horizontal_resolution vres = self._view_plane.vertical_resolution pixel_size = self._view_plane.pixel_size colors = [None] * (hres * vres) # type: List[Color] num_samples = self._view_plane.sampler.num_samples for r in range(vres): for c in range(hres): color = Color(r=0, g=0, b=0) samples = self._view_plane.sampler.sample_unit_square(0) for i in range(num_samples): x = pixel_size * (c - 0.5 * hres + samples[i].x) y = pixel_size * (r - 0.5 * vres + samples[i].y) ray = Ray(origin=Vector3(x=x, y=y, z=100), direction=Vector3(x=0, y=0, z=-1)) color += self._tracer.trace_ray(ray, self) colors[r * hres + c] = color.div(num_samples) return colors
def main() -> None: print("Rendering...") width = 160 height = 120 world = World( view_plane=ViewPlane( hres=width, vres=height, pixel_size=0.5, gamma=1.0, # sampler=StandardSampler()), sampler=JitteredSampler(samples_per_axis=3, num_sets=1, random=random)), background_color=Color(r=0, g=0, b=0), tracer=Tracer(), scene_objects=[ Sphere(center=Vector3(x=-100, y=0, z=0), radius=70.0, color=Color(r=1, g=0, b=0)), Sphere(center=Vector3(x=00, y=0, z=0), radius=25.0, color=Color(r=0, g=1, b=0)), Sphere(center=Vector3(x=100, y=0, z=0), radius=70.0, color=Color(r=0, g=0, b=1)) ]) colors = world.render() bitmap = Bitmap(width=width, height=height, colors=colors) with open("out.bmp", "wb") as file: bitmap.write_to(file) print("Done")
def test_from_ray_example(self): r0 = Ray(origin=Vector3(x=1, y=2, z=3), direction=Vector3(x=1, y=0, z=0)) other = Ray.from_ray(r0) assert other.origin == Vector3(x=1, y=2, z=3) assert other.direction == Vector3(x=1, y=0, z=0)
def test_hash_is_based_on_values(self): u = Vector3(x=1, y=2, z=3) v = Vector3(x=1, y=2, z=3) assert hash(u) == hash(v)
def test_equality_compares_values(self): assert Vector3(x=1, y=2, z=3) == Vector3(x=1, y=2, z=3) assert Vector3(x=1, y=2, z=3) != Vector3(x=0, y=0, z=0)
def test_normalized_vector_has_length_one(self): u = Vector3(x=1, y=1, z=0) n = u.normalized() assert n.length() == pytest.approx(1.0)
def test_dot_of_unit_vector_is_one(self): u = Vector3(x=0, y=1, z=0) v = Vector3(x=0, y=1, z=0) assert u.dot(v) == 1
def test_dot_perpendicular_vector_is_zero(self): u = Vector3(x=1, y=0, z=0) v = Vector3(x=0, y=1, z=0) assert u.dot(v) == 0
def test_length_example(self): u = Vector3(x=1, y=1, z=1) assert u.length() == math.sqrt(3)
def test_length_squared_example(self): u = Vector3(x=1, y=2, z=3) assert u.length_squared() == 14