def normal_at(self, world_point: Point) -> Vector: transformer = inverse(self.transform) object_point = transformer * world_point object_normal = object_point - point(0, 0, 0) world_normal = transpose(transformer) * object_normal world_normal = vector(world_normal.x, world_normal.y, world_normal.z) # set w to 0 return normalize(world_normal)
def run() -> None: room = create_room() objects = create_objects() world = World() world.light = PointLight(point(-10, 10, -10), Color(1, 1, 1)) world.objects.extend(room) world.objects.extend(objects) camera = Camera(400, 200, math.pi / 3) camera.transform = view_transform(point(0, 1.5, -5), point(0, 1, 0), vector(0, 1, 0)) canvas = camera.render(world) PPM(canvas).save_to_file("scene.ppm")
def check_attribute_point(context, var, att, tuple_type, x, y, z): if tuple_type == "point": expected = point(x, y, z) elif tuple_type == "vector": expected = vector(x, y, z) elif tuple_type == "color": expected = Color(x, y, z) # type: ignore else: raise ValueError(f"tuple type '{tuple_type}' not recognized") my_variable = context.variables[var] assert (getattr( my_variable, att) == expected), f"{getattr(my_variable, att)} == {expected}"
def create_structure( structure_type: str, x: float, y: float, z: float ) -> Union[Tuple, Color, Matrix]: if structure_type == "vector": return vector(x, y, z) elif structure_type == "point": return point(x, y, z) elif structure_type == "color": return Color(x, y, z) elif structure_type == "scaling": return scaling(x, y, z) elif structure_type == "translation": return translation(x, y, z) else: raise NotImplementedError(f"structure type '{structure_type}' not recognized")
def assign_ray_inline(context, var, px, py, pz, vx, vy, vz): origin = point(px, py, pz) direction = vector(vx, vy, vz) context.variables[var] = Ray(origin, direction)
def assign_vector(context, var, x, y, z): context.variables[var] = vector(x, y, z)
def check_cross(context, var_1, var_2, x, y, z): v1 = context.variables[var_1] v2 = context.variables[var_2] assert cross(v1, v2) == vector(x, y, z)
def check_approximate_normalize(context, var, x, y, z): my_variable = context.variables[var] assert normalize(my_variable).approximately_equals(vector(x, y, z))
def check_normalize(context, var, x, y, z): my_variable = context.variables[var] assert normalize(my_variable) == vector(x, y, z)