from raytracer.base import Color from raytracer.patterns import ( StripePattern, _TestPattern, GradientPattern, RingPattern, CheckersPattern, ) from raytracer.base import Point, Scaling, Translation, Identity from raytracer.shapes import Sphere black = Color(0, 0, 0) white = Color(1, 1, 1) def test_stripe_pattern(): pattern = StripePattern(white, black) assert pattern.a == white assert pattern.b == black def test_stripe_x(): pattern = StripePattern(white, black) assert pattern.pattern_at(Point(0, 0, 0)) == white assert pattern.pattern_at(Point(0.9, 0, 0)) == white assert pattern.pattern_at(Point(1, 0, 0)) == black assert pattern.pattern_at(Point(-0.1, 0, 0)) == black assert pattern.pattern_at(Point(-1, 0, 0)) == black assert pattern.pattern_at(Point(-1.1, 0, 0)) == white
def test_object_transform(): s = Sphere() s.set_transform(Scaling(2, 2, 2)) pattern = _TestPattern() c = pattern.pattern_at_shape(s, Point(2, 3, 4)) assert c == Color(1, 1.5, 2)
def pattern_at(self, point: Point): return Color(point.x, point.y, point.z)
def test_light_attributes(): position = Point(0, 0, 0) intensity = Color(1, 1, 1) light = PointLight(position, intensity) assert light.position == position assert light.intensity == intensity
def test_point_light(self): i = Color(1, 1, 1) p = Point(0, 0, 0) light = PointLight(p, i) self.assertEqual(light.position, p) self.assertEqual(light.intensity, i)
# height: 100 # field-of-view: 0.785 # from: [ -6, 6, -10 ] # to: [ 6, 0, 6 ] # up: [ -0.45, 1, 0 ] c = Camera(512, 512, 0.785) c.transform = ViewTransform(Point(-6, 6, -10), Point(6, 0, 6), Vector(-0.45, 1, 0)) # # ====================================================== # # light sources # # ====================================================== # - add: light # at: [ 50, 100, -50 ] # intensity: [ 1, 1, 1 ] # # an optional second light for additional illumination light = PointLight(Point(50, 100, -50), Color(1, 1, 1)) # - add: light # at: [ -400, 50, -10 ] # intensity: [ 0.2, 0.2, 0.2 ] # # ====================================================== # # define some constants to avoid duplication # # ====================================================== # - define: white-material # value: # color: [ 1, 1, 1 ] # diffuse: 0.7 # ambient: 0.1 # specular: 0.0 # reflective: 0.1 white = Material()
def test_write_canvas(): c = Canvas(10, 20) red = Color(1, 0, 0) c.write_pixel(2, 3, red) assert c.read_pixel(2, 3) == red
def test_long_ppm(): c = Canvas(10, 2, Color(1.0, 0.8, 0.6)) assert ( "255 204 153 255 204 153 255 204 153 255 204 153 255 204 153 255 204\n153 255 204 153 255 204 153 255 204 153 255 204 153\n255 204 153 255 204 153 255 204 153 255 204 153 255 204 153 255 204\n153 255 204 153 255 204 153 255 204 153 255 204 153" in c.to_ppm() )
def test_multiply_colors(self): c1 = Color(1, 0.2, 0.4) c2 = Color(0.9, 1, 0.1) combined = c1 * c2 self.assertEqual(combined, Color(0.9, 0.2, 0.04)) self.assertIsInstance(combined, Color)
def test_color(self): c = Color(-0.5, 0.4, 1.7) self.assertEqual(c.red, -0.5) self.assertEqual(c.green, 0.4) self.assertEqual(c.blue, 1.7)
def test_scalar_multiplication(self): c = Color(0.2, 0.3, 0.4) mult = c * 2 self.assertEqual(mult, Color(0.4, 0.6, 0.8)) self.assertIsInstance(mult, Color)
def test_subtraction(self): c1 = Color(0.9, 0.6, 0.75) c2 = Color(0.7, 0.1, 0.25) sub = c1 - c2 self.assertEqual(sub, Color(0.2, 0.5, 0.5)) self.assertIsInstance(sub, Color)
def test_addition(self): c1 = Color(0.9, 0.6, 0.75) c2 = Color(0.7, 0.1, 0.25) add = c1 + c2 self.assertEqual(add, Color(1.6, 0.7, 1.0)) self.assertIsInstance(add, Color)
RotationZ, Translation, Scaling, Color, Point, ViewTransform, Vector, ) from raytracer.patterns import CheckersPattern, StripePattern from raytracer.lights import PointLight from raytracer.camera import Camera from raytracer.world import World import math floor = Plane() floor.material.color = Color(1.0, 0.9, 0.9) floor.material.pattern = CheckersPattern(Color(1, 1, 1), Color(0, 0, 0)) floor.material.reflective = 0.5 floor.material.specular = 0 middle = Sphere() middle.set_transform(Translation(-0.5, 1, 0.5)) middle.material.transparency = 0.9 middle.material.diffuse = 0.1 middle.material.reflective = 0.9 middle.material.refractive_index = 1.5 middle.material.specular = 1 middle.material.shininess = 300 right = Sphere() right.set_transform(Translation(1.5, 0.5, -0.5) * Scaling(0.5, 1, 0.5))
def test_writepixel(self): canvas = Canvas(10, 20) red = Color(1, 0, 0) canvas.write_pixel(2, 3, red) self.assertEqual(canvas.pixel_at(2, 3), red) self.assertNotEqual(canvas.pixel_at(2, 4), red)