def test_cross(self): test = points.Point(3,1) test2 = points.Point(-3,2) test3 = points.Point(2,1) self.assertEqual(test.cross(test2), 9) self.assertEqual(test.cross(test3), 1) self.assertEqual(test2.cross(test3), -7)
def test_gradient(self): """Test that the gradient pattern works""" p = patterns.GradientPattern(WHITE, BLACK) self.assertEqual(p.pattern_at(points.Point( 0, 0, 0, )), WHITE) self.assertEqual(p.pattern_at(points.Point( 0.25, 0, 0, )), colors.Color(0.75, 0.75, 0.75)) self.assertEqual(p.pattern_at(points.Point( 0.5, 0, 0, )), colors.Color(0.5, 0.5, 0.5)) self.assertEqual(p.pattern_at(points.Point( 0.75, 0, 0, )), colors.Color(0.25, 0.25, 0.25))
def test_render_scene(self): """Test we can render a pixel in a simple scene""" # Inner sphere size 0.5, centered on the origin s1 = shapes.Sphere() s1.set_transform(transforms.Scale(0.5,0.5,0.5)) # Outer sphere centered on the origin, size 1.0 s2 = shapes.Sphere() s2.material = materials.Material( color=colors.Color(0.8, 1.0, 0.6), diffuse=0.7, specular=0.2) l1 = lights.Light( position=points.Point(-10, 10, -10), intensity=colors.Color(1, 1, 1) ) scene = scenes.Scene( objects = [s1, s2], lights = [l1] ) cam = cameras.Camera(11, 11, math.pi/2) from_point = points.Point(0, 0, -5) to_point = points.Point(0, 0, 0) up = vectors.Vector(0, 1, 0) cam.transform = transforms.ViewTransform(from_point, to_point, up) image = cam.render(scene) self.assertEqual(image.get(5, 5), colors.Color(0.3807, 0.4758, 0.2855))
def test_mul(self): test = points.Point(3,1) test2 = points.Point(-3,2) test3 = points.Point(2,1) self.assertEqual(test * test2, -7) self.assertEqual(test * test3, 7) self.assertEqual(test2 * test3, -4)
def test_length(self): self.assertEqual(points.Point(2, 0).length(), 2) self.assertEqual(points.Point(0, 0).length(), 0) self.assertEqual(points.Point(3, 4).length(), 5) def tearDown(self): pass
def test_shadows__full_scene(self): """Test that we identify a shadow in a full scene""" # First sphere is at z=10 s1 = shapes.Sphere() s1.set_transform(transforms.Translate(0, 0, 10)) # Second sphere is at the origin s2 = shapes.Sphere() s2.material = materials.Material() # Light is at z=-10 l1 = lights.Light(position=points.Point(0, 0, -10), intensity=colors.Color(1, 1, 1)) scene = scenes.Scene(objects=[s1, s2], lights=[l1]) # The ray is at z=5 (i.e. between the spheres), pointing at the further # out sphere ray = rays.Ray(points.Point(0, 0, 5), vectors.Vector(0, 0, 1)) isection = intersections.Intersection(s2, 4) comps = isection.precompute(ray) result, _ = scene.shade_hit(comps) self.assertEqual(result, colors.Color(0.1, 0.1, 0.1))
def test_pattern(self): """Test that the pattern of a material can change its color""" m = materials.Material(pattern=patterns.StripePattern( colors.Color( 0, 0, 0, ), colors.Color(1, 1, 1)), ambient=1, diffuse=0, specular=0) eyev = vectors.Vector(0, 0, -1) normalv = vectors.Vector(0, 0, -1) light = lights.PointLight(points.Point(0, 0, -10), colors.Color(1, 1, 1)) color_1 = m.lighting(light, points.Point(0.9, 0, 0), eyev, normalv, in_shadow=False) color_2 = m.lighting(light, points.Point(1.1, 0, 0), eyev, normalv, in_shadow=False) self.assertEqual(color_1, colors.Color(0, 0, 0)) self.assertEqual(color_2, colors.Color(1, 1, 1))
def test_is_shadowed(self): """Test we find shadows appropriately""" # Directly illuminated self.assertFalse( self.default_scene.is_shadowed(points.Point(0, 10, 0), self.default_scene.lights[0])) # Behind the sphere self.assertTrue( self.default_scene.is_shadowed(points.Point(10, -10, 10), self.default_scene.lights[0])) # Light between point and object self.assertFalse( self.default_scene.is_shadowed(points.Point(-20, 20, -20), self.default_scene.lights[0])) # Point between light and object self.assertFalse( self.default_scene.is_shadowed(points.Point(-2, 2, -2), self.default_scene.lights[0])) # Point inside object self.assertTrue( self.default_scene.is_shadowed(points.Point(-0.5, 0.5, -0.5), self.default_scene.lights[0]))
def test_rings(self): """Test the ring pattern works""" p = patterns.RingPattern(WHITE, BLACK) self.assertEqual(p.pattern_at(points.Point( 0, 0, 0, )), WHITE) self.assertEqual(p.pattern_at(points.Point( 1, 0, 0, )), BLACK) self.assertEqual(p.pattern_at(points.Point( 0, 0, 1, )), BLACK) self.assertEqual(p.pattern_at(points.Point( 0.708, 0, 0.708, )), BLACK)
def test_stripes_in_z(self): """Test that the default pattern is constant in z""" stripes = patterns.StripePattern(WHITE, BLACK) self.assertEqual(stripes.pattern_at(points.Point(0, 0, 0)), WHITE) self.assertEqual(stripes.pattern_at(points.Point(0, 0, 1)), WHITE) self.assertEqual(stripes.pattern_at(points.Point(0, 0, 2)), WHITE)
def test_default_view(self): """Test that the view transform for the default view is identity""" from_point = points.Point(0, 0, 0) to_point = points.Point(0, 0, -1) up = vectors.Vector(0, 1, 0) result = transforms.ViewTransform(from_point, to_point, up) self.assertEqual(result, transforms.Identity(4))
def test_rotate_camera(self): """A view transformation matrix looking in the +ve z direction""" from_point = points.Point(0, 0, 0) to_point = points.Point(0, 0, 1) up = vectors.Vector(0, 1, 0) result = transforms.ViewTransform(from_point, to_point, up) self.assertEqual(result, transforms.Scale(-1, 1, -1))
def test_scaling_reflection(self): """Test we can reflect a point about an axis using the scaling matrix""" S = transforms.Scale(-1, 1, 1) p = points.Point(-4, 6, 8) p2 = S * p self.assertEqual(p2, points.Point(4, 6, 8))
def test_projete_sur_droite(self): #Origine -> Droite x=t, y=0, z=1 p = points.origine d = droites.Droite(points.Point(0, 0, 1), points.Point(1, 0, 1)) projete = p.projete_orthogonal(d) self.assertEqual(projete.x, 0) self.assertEqual(projete.y, 0) self.assertEqual(projete.z, 1)
def test_subtraction__vector_point(self): """test that we can subtract a vector from a point""" a1 = points.Point(3, 2, 1) a2 = vectors.Vector(5, 6, 7) a3 = a1 - a2 self.assertEqual(a3, points.Point(-2, -4, -6))
def test_chained_transforms(self): """Test we can chain together transforms with the apply function""" point = points.Point(1, 0, 1) p2 = point.apply(transforms.RotateX(math.pi/2)) \ .apply(transforms.Scale(5, 5, 5)) \ .apply(transforms.Translate(10, 5, 7)) self.assertEqual(p2, points.Point(15, 0, 7))
def test_move_camera(self): """Test we can move the camera (well... move the world)""" from_point = points.Point(0, 0, 9) to_point = points.Point(0, 0, 0) up = vectors.Vector(0, 1, 0) result = transforms.ViewTransform(from_point, to_point, up) self.assertEqual(result, transforms.Translate(0, 0, -9))
def test_addition(self): """test that we can add a vector to a point""" a1 = points.Point(3, -2, 5) a2 = vectors.Vector(-2, 3, 1) a3 = a1 + a2 self.assertEqual(a3, tuples.Tuple(["x", "y", "z", "w"], 1, 1, 6, 1)) self.assertEqual(a3, points.Point(1, 1, 6))
def test_position(self): origin = points.Point(2, 3, 4) direction = vectors.Vector(1, 0, 0) r = rays.Ray(origin, direction) self.assertEqual(r.position(0), origin) self.assertEqual(r.position(1), points.Point(3, 3, 4)) self.assertEqual(r.position(-1), points.Point(1, 3, 4)) self.assertEqual(r.position(2.5), points.Point(4.5, 3, 4))
def test_rotate_x_inverse(self): """Test that roating by the inverse of a rotation rotates the other way """ p = points.Point(0, 1, 0) full_quarter = transforms.RotateX(math.pi/2) p2 = full_quarter.inverse()*p self.assertEqual(p2, points.Point(0, 0, -1))
def test_lighting__shadow(self): """Test that we get the ambient color if we're in shadow""" m = materials.Material() p = points.Point(0, 0, 0) eyev = vectors.Vector(0, 0, -1) normalv = vectors.Vector(0, 0, -1) light = lights.PointLight(points.Point(0, 0, -10), colors.Color(1, 1, 1)) result = m.lighting(light, p, eyev, normalv, in_shadow=True) self.assertEqual(result, colors.Color(0.1, 0.1, 0.1))
def test_distance(self): #Distance point-origine a = points.origine b = points.Point(1, 1, 1) self.assertEqual(points.distance(a, b), 3**0.5) #Distance point positif - point positif a = points.Point(2, 3, 4) b = points.Point(1, 1, 1) self.assertEqual(points.distance(a, b), ((2 - 1)**2 + (3 - 1)**2 + (4 - 1)**2)**0.5)
def test_local_normal_at(self): """Test that the local normal is always in the y direction""" p = shapes.Plane() self.assertEqual(p.local_normal_at(points.Point(0, 0, 0)), vectors.Vector(0, 1, 0)) self.assertEqual(p.local_normal_at(points.Point(10, 0, -10)), vectors.Vector(0, 1, 0)) self.assertEqual(p.local_normal_at(points.Point(-5, 0, 150)), vectors.Vector(0, 1, 0))
def test_normal_at__transformed(self): """Test we can calculate normal vectors on a transformed sphere""" s = shapes.Sphere() s.set_transform(transforms.Translate(0, 1, 0)) n = s.normal_at(points.Point(0, 1.70711, -0.70711)) self.assertEqual(n, vectors.Vector(0, 0.70711, -0.70711)) s.set_transform( transforms.Scale(1, 0.5, 1) * transforms.RotateZ(math.pi / 5)) n = s.normal_at(points.Point(0, math.sqrt(2) / 2, -math.sqrt(2) / 2)) self.assertEqual(n, vectors.Vector(0, 0.97014, -0.24254))
def test_translate_point(self): """Test that if we multiply a translate matrix by a point it moves it""" p = points.Point(-3, 4, 5) T = transforms.Translate(5, -3, 2) p2 = T * p self.assertEqual(p2, points.Point(2, 1, 7)) # Translating by the inverse of the translate matrix moves the # opposite direction p3 = T.inverse() * p self.assertEqual(p3, points.Point(-8, 7, 3))
def test_ray_transforms(self): """Test that we can transform a ray""" origin = points.Point(1, 2, 3) direction = vectors.Vector(0, 1, 0) r = rays.Ray(origin, direction) r2 = r.transform(transforms.Translate(3, 4, 5)) self.assertEqual(r2.origin, points.Point(4, 6, 8)) self.assertEqual(r2.direction, vectors.Vector(0, 1, 0)) r3 = r.transform(transforms.Scale(2, 3, 4)) self.assertEqual(r3.origin, points.Point(2, 6, 12)) self.assertEqual(r3.direction, vectors.Vector(0, 3, 0))
def test_precompute__inside(self): """Test that we can precompute vectors for an intersection and ray when inside of a shape""" r = rays.Ray(points.Point(0, 0, 0), vectors.Vector(0, 0, 1)) s = shapes.Sphere() i = intersections.Intersection(s, 1) computations = i.precompute(r) self.assertEqual(computations.t, 1) self.assertEqual(computations.point, points.Point(0, 0, 1)) self.assertEqual(computations.eyev, vectors.Vector(0, 0, -1)) self.assertEqual(computations.normalv, vectors.Vector(0, 0, -1)) self.assertTrue(computations.inside)
def test_simple_scaling(self): """Test that we can scale a point""" S = transforms.Scale(2, 3, 4) p = points.Point(-4, 6, 8) v = vectors.Vector(-4, 6, 8) p2 = S * p self.assertEqual(p2, points.Point(-8, 18, 32)) p3 = S * v self.assertEqual(p3, vectors.Vector(-8, 18, 32)) p4 = S.inverse() * p self.assertEqual(p4, points.Point(-2, 2, 2))
def test_arbitrary_view_transform(self): from_point = points.Point(1, 3, 2) to_point = points.Point(4, -2, 8) up = vectors.Vector(1, 1, 0) result = transforms.ViewTransform(from_point, to_point, up) # pp. 99 of the Ray Tracer Challenge expected = matrices.Matrix(4, 4) expected.set_row(0, [-0.50709, 0.50709, 0.67612, -2.36643]) expected.set_row(1, [0.76772, 0.60609, 0.12122, -2.82843]) expected.set_row(2, [-0.35857, 0.59761, -0.71714, 0.0]) expected.set_row(3, [0, 0, 0, 1]) self.assertEqual(result, expected)
def test_ray_for_pixel(self): """Test we can fire a ray through any pixel""" cam = cameras.Camera(201, 101, math.pi/2) # Middle of the screen ray = cam.ray_for_pixel(100, 50) self.assertEqual(ray.origin, points.Point(0, 0, 0)) self.assertEqual(ray.direction, vectors.Vector(0, 0, -1)) # A corner ray = cam.ray_for_pixel(0, 0) self.assertEqual(ray.origin, points.Point(0, 0, 0)) self.assertEqual(ray.direction, vectors.Vector(0.66519, 0.33259, -0.66851))