def test_intersection_points_3(self): s1 = data.Sphere(data.Point(2, 2, 2), math.sqrt(3), data.Color(0, 1, 0), data.Finish(.2, 1, 0, 0)) s2 = data.Sphere(data.Point(-2, -2, 3), 1, data.Color(0, 1, 0), data.Finish(0.2, 1, 0, 0)) s3 = data.Sphere(data.Point(0, 3, 0), 3 * math.sqrt(2), data.Color(0, 1, 0), data.Finish(.2, 1, 0, 0)) ray = data.Ray(data.Point(0, 0, 0), data.Vector(1, 1, 1)) s_list = [s1, s2, s3] result_list = [(s1, data.Point(1, 1, 1)), (s3, data.Point(3, 3, 3))] self.assertEqual(collisions.find_intersection_points(s_list, ray), result_list)
def test_intersection_points_2(self): s1 = data.Sphere(data.Point(2, 0, 2), 2, data.Color(0, 1, 0), data.Finish(.2, 1, 0, 0)) s2 = data.Sphere(data.Point(0, 2, 4), 2, data.Color(0, 1, 0), data.Finish(.2, 1, 0, 0)) s3 = data.Sphere(data.Point(2, 2, 0), 2, data.Color(0, 1, 0), data.Finish(.2, 1, 0, 0)) ray = data.Ray(data.Point(0, 0, -6), data.Vector(0, 0, 1)) s_list = [s1, s2, s3] result_list = [(s1, data.Point(0, 0, 2)), (s2, data.Point(0, 0, 4))] self.assertEqual(collisions.find_intersection_points(s_list, ray), result_list)
def test1(self): result = commandline.process_cmdArguments( ['', 'test_file', '-ambient', '0.9', '0.9', '0.9']) sphere1 = data.Sphere(data.Point(1, 1, 0), 2, data.Color(1, 0, 1), data.Finish(0.2, 0.4, 0.5, 0.05)) sphere2 = data.Sphere(data.Point(8, -10, 110), 100, data.Color(0.2, 0.2, 0.6), data.Finish(0.4, 0.8, 0, 0.05)) check = (data.Light(data.Point(-100, 100, -100), data.Color(1.5, 1.5, 1.5)), data.Color(0.9, 0.9, 0.9), [sphere1, sphere2]) self.assertEqual(result, check)
def test_collides_with_spheres_1(self): s = data.Sphere(data.Point(0.0, 0.0, 0.0), 2.0, data.Color(1.0, 0.0, 0.0), data.Finish(.2, .4, .5, .05)) s1 = data.Sphere(data.Point(5.0, 5.0, 5.0), 2.0, data.Color(1.0, 0.0, 0.0), data.Finish(.2, .4, .5, .05)) ray = data.Ray(data.Point(-5, 0, 0), data.Vector(1, 0, 0)) light = data.Light(data.Point(0, 0, -14), data.Color(1, 1, 1)) expected = False self.assertEqual( cast.collides_with_spheres(ray, [s, s1], data.Point(-3, -3, -3), light), expected)
def test_get_difuse_color_2(self): light = data.Light(data.Point(-5.0, 10.0, -10.0), data.Color(1.5, 1.5, 1.5)) a = data.Sphere(data.Point(0, 0, 0), 4.0, data.Color(.5, 0.3, .8), data.Finish(.2, .4, .5, .05)) b = data.Sphere(data.Point(.5, 1.5, -3.0), .5, data.Color(1.0, 0.0, 0.0), data.Finish(.4, .4, .5, .05)) list = [a, b] t = (a, data.Point(2.0, 0, 0)) result = cast.get_diffuse_color(t, light, list) expected = data.Color(0, 0, 0) self.assertEqual(result, expected)
def test_get_color_2(self): ambient = data.Color(.2, .2, .2) light = data.Light(data.Point(-100.0, 100.0, -100.0), data.Color(1.5, 1.5, 1.5)) a = data.Sphere(data.Point(0, 0, 0), 2.0, data.Color(.5, 0.3, .8), data.Finish(.2, .4, .5, .05)) b = data.Sphere(data.Point(.5, 1.5, -3.0), .5, data.Color(1.0, 0.0, 0.0), data.Finish(.4, .4, .5, .05)) list = [a, b] t = (a, data.Point(2.0, 0, 0)) result = cast.get_color(a.color, ambient, t, light, list) expected = data.Color(0.02, 0.012, 0.032) self.assertEqual(result, expected)
def test_sphere_2(self): point2 = data.Point(0, -1, -2.0) color2 = data.Color(0, 1, 0) finish2 = data.Finish(0.2, 1, 0, 0) sphere2 = data.Sphere(point2, 2.5, color2, finish2) self.assertAlmostEqual(sphere2.center.x, 0) self.assertAlmostEqual(sphere2.center.y, -1) self.assertAlmostEqual(sphere2.center.z, -2.0) self.assertEqual(sphere2.center, point2) self.assertAlmostEqual(sphere2.radius, 2.5) self.assertEqual(point2, data.Point(0, -1, -2)) self.assertEqual( sphere2, data.Sphere(data.Point(0, -1, -2), 2.5, data.Color(0, 1, 0), data.Finish(0.2, 1, 0, 0)))
def test_sphere(self): point = data.Point(1, 2, 3) color = data.Color(0, 0, 0) finish = data.Finish(0.2, 1, 0, 0) sphere = data.Sphere(point, 1.0, color, finish) self.assertAlmostEqual(sphere.center.x, 1) self.assertAlmostEqual(sphere.center.y, 2) self.assertAlmostEqual(sphere.center.z, 3) self.assertEqual(sphere.center, point) self.assertAlmostEqual(sphere.radius, 1.0) self.assertEqual(sphere.center, data.Point(1, 2, 3)) self.assertEqual( sphere, data.Sphere(data.Point(1, 2, 3), 1, data.Color(0, 0, 0), data.Finish(.2, 1, 0, 0)))
def test_computePe2(self): result = auxiliary.compute_Pe( (data.Sphere(data.Point(0.0, 2.0, 0.0), 1.0, data.Color(1, 1, 1), data.Finish(0.5, 0.4, 0.5, 0.05)), data.Point(0, 1, 0))) check = data.Point(0, 0.99, 0) self.assertEqual(result, check)
def test_normal_point_3(self): sphere = data.Sphere(data.Point(1, -2, 2), 3, data.Color(0, 1, 0), data.Finish(.2, 1, 0, 0)) point = data.Point(0, 0, 0) l = 3.0 vec = data.Vector(-1 / l, 2 / l, -2 / l) self.assertEqual(collisions.sphere_normal_at_point(sphere, point), vec) self.assertAlmostEqual(vector_math.length_vector(vec), 1)
def test_cast_ray2(self): spheres = [ data.Sphere(data.Point(6.0, 0.0, 0.0), 1.0, data.Color(1, 0, 0), data.Finish(0.5, 0.4, 0.5, 0.05)), data.Sphere(data.Point(0.0, 5.0, 0.0), 1.0, data.Color(0, 0, 1), data.Finish(0.5, 0.4, 0.5, 0.05)), data.Sphere(data.Point(0.0, -4.0, 0.0), 1.0, data.Color(0, 1, 0), data.Finish(0.5, 0.4, 0.5, 0.05)), data.Sphere(data.Point(0.0, 8.0, 0.0), 1.0, data.Color(0, 0, 0), data.Finish(0.5, 0.4, 0.5, 0.05)) ] ray = data.Ray(data.Point(0.0, 0.0, 0.0), data.Vector(0.0, 1.0, 0.0)) result = cast.cast_ray( ray, spheres, data.Color(1, 1, 1), data.Light(data.Point(-100.0, 100.0, -100.0), data.Color(1.5, 1.5, 1.5)), data.Point(0, 0, -14)) self.assertEqual(result, data.Color(0, 0, 0.5))
def test_get_pE_2(self): sphere = data.Sphere(data.Point(0.0, 0.0, 0.0), 2.0, data.Color(0.0, 0.0, 1.0), data.Finish(.2, .4, .5, .05)) tuple = (sphere, data.Point(0, 2, 0)) pe = cast.get_pe(tuple) expected = data.Point(0, 2.01, 0) self.assertEqual(pe, expected)
def test_calculateColor2(self): result = auxiliary.calculate_color( data.Sphere(data.Point(0.0, 2.0, 0.0), 1.0, data.Color(1, 1, 1), data.Finish(0.5, 0.4, 0.5, 0.05)), data.Color(1, 1, 1), 1, 1, data.Light(data.Point(-100.0, 100.0, -100.0), data.Color(1.5, 1.5, 1.5))) check = data.Color(1.85, 1.85, 1.85) self.assertEqual(result, check)
def test_cast_ray(self): sph1 = data.Sphere(data.Point(0, 3, 3), 5, data.Color(1.0, 1.0, 0.0), \ data.Finish(0.5, 0.4, 0.5, 0.05)) sph2 = data.Sphere(data.Point(5, 1, 2), 2, data.Color(0.0, 1.0, 1.0), \ data.Finish(0.9, 0.4, 0.5, 0.05)) sph3 = data.Sphere(data.Point(2, 2, 2), 4, data.Color(1.0, 0.0, 1.0), \ data.Finish(0.8, 0.4, 0.5, 0.05)) N = [sph1, sph2, sph3] ray1 = data.Ray(data.Point(4, 4, 4), data.Vector(-2, -2, -2)) self.assertTrue(cast.cast_ray(ray1, N, 0.8, data.Light(data.Point(-100.0, \ 100.0, -100.0), data.Color(1.5, 1.5, 1.5)), data.Point(0.0, 0.0, -14.0)), \ data.Color(0.8, 0.8, 0.8)) ray2 = data.Ray(data.Point(3, 5, 1), data.Vector(-3, 1, 4)) self.assertTrue(cast.cast_ray(ray2, N, 0.5, data.Light(data.Point(-100.0, \ 100.0, -100.0), data.Color(1.5, 1.5, 1.5)), data.Point(0.0, 0.0, -14.0)), \ data.Color(1.0, 1.0, 1.0))
def create_sphere(list, i): try: cen = data.Point(float(list[0]), float(list[1]), float(list[2])) rad = float(list[3]) color = data.Color(float(list[4]), float(list[5]), float(list[6])) finish = data.Finish(float(list[7]), float(list[8]), float(list[9]), \ float(list[10])) return data.Sphere(cen, rad, color, finish) except: print 'malformed sphere in line', i, '... skipping'
def test_get_diffuse_2(self): lDir = 2 light = data.Light(data.Point(-100.0, 100.0, -100.0), data.Color(1.5, 1.5, 1.5)) s = data.Sphere(data.Point(1.0, 1.0, 0.0), 2.0, data.Color(0.0, 0.0, 1.0), data.Finish(.2, .4, .5, .05)) diffuse = .4 expected = data.Color(0, 0, 1.2) self.assertEqual(cast.get_diffuse(lDir, light, s, diffuse), expected)
def test_closestSphere1(self): spheres = [ data.Sphere(data.Point(0.0, 2.0, 0.0), 1.0, data.Color(1, 0, 0), data.Finish(0.5, 0.4, 0.5, 0.05)), data.Sphere(data.Point(0.0, 5.0, 0.0), 1.0, data.Color(0, 0, 1), data.Finish(0.5, 0.4, 0.5, 0.05)), data.Sphere(data.Point(0.0, 4.0, 0.0), 1.0, data.Color(0, 1, 0), data.Finish(0.5, 0.4, 0.5, 0.05)), data.Sphere(data.Point(0.0, 8.0, 0.0), 1.0, data.Color(0, 0, 0), data.Finish(0.5, 0.4, 0.5, 0.05)) ] ray = data.Ray(data.Point(0.0, 0.0, 0.0), data.Vector(0.0, 1.0, 0.0)) intersected_spheres = collisions.find_intersection_points(spheres, ray) result = auxiliary.closest_sphere(intersected_spheres, ray) check = (data.Sphere(data.Point(0.0, 2.0, 0.0), 1.0, data.Color(1, 0, 0), data.Finish(0.5, 0.4, 0.5, 0.05)), data.Point(0, 1, 0)) self.assertEqual(result, check)
def create_picture(): eyept = data.Point(0.0, 0.0, -14.0) sph1 = data.Sphere(data.Point(1.0, 1.0, 0.0), 2.0, data.Color(0.0, 0.0, \ 1.0), data.Finish(0.2, 0.4, 0.5, 0.05)) sph2 = data.Sphere(data.Point(0.5, 1.5, -3.0), 0.5, data.Color(1.0, 0.0, \ 0.0), data.Finish(0.4, 0.4, 0.5, 0.05)) S = [sph1, sph2] min_x = -10 max_x = 10 min_y = -7.5 max_y = 7.5 width = 1024 height = 768 ambient = 1.0 light = data.Light(data.Point(-100.0, 100.0, -100.0), data.Color(1.5, 1.5, \ 1.5)) cast.cast_all_rays(min_x, max_x, min_y, max_y, width, height, eyept, S, \ ambient, light)
def test_calculate_light_component2(self): spheres = [ data.Sphere(data.Point(1, 1, 0), 2, data.Color(0, 0, 1), data.Finish(0.2, 0.4, 0.5, 0.05)), data.Sphere(data.Point(0.5, 1.5, -3), 0.5, data.Color(1, 0, 0), data.Finish(0.2, 0.4, 0.5, 0.05)) ] intersection_pt = collisions.sphere_intersection_point( data.Ray(data.Point(0, 0, -14), data.Vector(0.5, 1.5, 11)), data.Sphere(data.Point(0.5, 1.5, -3), 0.5, data.Color(1, 0, 0), data.Finish(0.2, 0.4, 0.5, 0.05))) eye_pt = data.Point(0, 0, -14) light = data.Light(data.Point(-100.0, 100.0, -100.0), data.Color(1.5, 1.5, 1.5)) result = auxiliary.calculate_light_components( (data.Sphere(data.Point(0.5, 1.5, -3), 0.5, data.Color(1, 0, 0), data.Finish(0.2, 0.4, 0.5, 0.05)), intersection_pt), spheres, eye_pt, light) check = (0.5082198628016603, 0.5082198628016605) self.assertEqual(result, check)
def process_line(line): val_list = line.strip().split() float_list = [] if len(val_list) != 11: raise RuntimeError("The input file is not properly formatted") for i in range(len(val_list)): float_list.append(float(val_list[i])) return data.Sphere( data.Point(float_list[0], float_list[1], float_list[2]), float_list[3], data.Color(float_list[4], float_list[5], float_list[6]), data.Finish(float_list[7], float_list[8], float_list[9], float_list[10]))
def test_cast_ray_1(self): sphere_list = [ data.Sphere(data.Point(0, 0, 0), 1, data.Color(0, 0, 0), data.Finish(.2, 1, 0, 0)) ] ray = data.Ray(data.Point(0, 0, -15), data.Vector(0, 0, 1)) light = data.Light(data.Point(0, 0, 3), data.Color(1, 1, 1)) ambient_color = data.Color(1, 1, 1) eye_point = data.Point(0, 0, -14) calc_color = cast.cast_ray(ray, sphere_list, ambient_color, light, eye_point) exp_color = data.Color(0, 0, 0) self.assertEqual(calc_color, exp_color)
def get_sphere(line,line_num): l = line.split() try: return data.Sphere(data.Point(float(l[0]),float(l[1]), float(l[2])), float(l[3]), data.Color(float(l[4]),float(l[5]), float(l[6])), data.Finish(float(l[7]),float(l[8]), float(l[9]),float(l[10]))) except: print ('malformed sphere on line ' + str(line_num) + '... skipping')
def test_get_specular_intensity_2(self): lDir = data.Vector(.5, 10, 3) lDirection = 3 N = data.Vector(2, 2, 3) pE = data.Point(2, 3, -3) light = data.Light(data.Point(0, 0, -14), data.Color(1.5, 1.5, 1.5)) sphere = data.Sphere(data.Point(0.0, 0.0, 0.0), 1.0, data.Color(1.0, .5, 1.0), data.Finish(.2, .4, .5, .05)) result = cast.get_specular_intensity(lDir, lDirection, N, pE, light, sphere) expected = data.Color(0, 0, 0) self.assertEqual(result, expected)
def get_sphere(l): if not len(l) == 11: print "ERROR-- Number of arugements is incorrect: ", len(l) else: try: point = data.Point(float(l[0]), float(l[1]), float(l[2])) radius = float(l[3]) color = data.Color(float(l[4]), float(l[5]), float(l[6])) finish = data.Finish(float(l[7]), float(l[8]), float(l[9]), float(l[10])) except: print "ERROR-- Something went wrong while parsing spheres" return data.Sphere(point, radius, color, finish)
def test_normal_point_1(self): sphere = data.Sphere(data.Point(0, 0, 0), 5, data.Color(0, 1, 0), data.Finish(.2, 1, 0, 0)) point_x = data.Point(5, 0, 0) point_y = data.Point(0, 5, 0) point_z = data.Point(0, 0, 5) vec_x = data.Vector(1, 0, 0) vec_y = data.Vector(0, 1, 0) vec_z = data.Vector(0, 0, 1) self.assertEqual(collisions.sphere_normal_at_point(sphere, point_x), vec_x) self.assertEqual(collisions.sphere_normal_at_point(sphere, point_y), vec_y) self.assertEqual(collisions.sphere_normal_at_point(sphere, point_z), vec_z)
def test_cast_ray_1(self): pt = data.Point(0,0,0) dir = data.Vector(0,0,1) ray = data.Ray(pt,dir) center_1 = data.Point(12,0,0) center_2 = data.Point(0,5,0) center_3 = data.Point(5,0,0) center_4 = data.Point(50,0,0) radius = 2 color = data.Color(1,0,.1) finish = data.Finish(1,.2,1,1) light = data.Light(data.Color(1.5,1.5,1.5),data.Point(0,0,0)) eye_point = data.Point(1,1,1) sphere_list = [data.Sphere(center_1,radius,color,finish),data.Sphere(center_2,radius,color,finish),data.Sphere(center_3,radius,color,finish),data.Sphere(center_4,radius,color,finish)] test_cast_ray_1 = cast.cast_ray(ray,sphere_list,finish,light,eye_point) self.assertTrue(test_cast_ray_1 == data.Color(1,1,1))
def test_cast_ray_2(self): pt = data.Point(0,0,0) dir = data.Vector(0,1,0) ray = data.Ray(pt,dir) center_1 = data.Point(0,0,5) center_2 = data.Point(0,5,0) center_3 = data.Point(5,0,0) radius = 2 color = data.Color(0,1,0) color_1 = data.Color(0,0,0) finish = data.Finish(1,1,1,1) light = data.Light(data.Point(1,1,1),data.Color(1,1,1)) eye_point = data.Point(1,1,1) ambient = data.Color(1,1,1) sphere_list = [data.Sphere(center_1,radius,color_1,finish),data.Sphere(center_2,radius,color,finish),data.Sphere(center_3,radius,color_1,finish)] test_cast_ray_2 = cast.cast_ray(ray,sphere_list,ambient,light,eye_point) self.assertTrue(test_cast_ray_2 == data.Color(0.328870320968,2.14399924358,0.328870320968))
def test_cast_ray_1(self): finish = data.Finish(.4, .4, .5, .05) one = data.Sphere(data.Point(2, 2, 0), 1.0, data.Color(.4, .2, .1), finish) two = data.Sphere(data.Point(6, 6, 0), 2.0, data.Color(.2, .3, .5), finish) three = data.Sphere(data.Point(-2, -2, 0), 1.0, data.Color(1, 1, 1), finish) four = data.Sphere(data.Point(-6, -6, 0), 15.0, data.Color(.5, .5, .5), finish) ray = data.Ray(data.Point(0, 0, -14), data.Vector(0, 0, 1)) l = [one, two, three, four] light = data.Light(data.Point(0, 0, -14), data.Color(1.5, 1.5, 1.5)) ambient_color = data.Color(.2, .2, .2) result = cast.cast_ray(ray, l, ambient_color, light, data.Point(0, 0, -14)) expected = data.Color(0.286793135633, 0.286793135633, 0.286793135633) self.assertEqual(result, expected)
def read_file(file_name): count = 0 list_of_spheres = [] for line in file_name: count += 1 a = line.split() if len(a) != 11: print "malformed sphere on line ", count, " ...skipping" else: try: sphere_center = data.Point(float(a[0]), float(a[1]), float(a[2])) sphere_radius = float(a[3]) sphere_color = data.Color(float(a[4]), float(a[5]), float(a[6])) sphere_finish = data.Finish(float(a[7]), float(a[8]), float(a[9]), float(a[10])) sphere_complete = data.Sphere(sphere_center, sphere_radius, sphere_color, sphere_finish) list_of_spheres.append(sphere_complete) except: print "malformed sphere on line ", count, "...skipping" return list_of_spheres
import unittest import data import cast import collisions import vector_math a = data.Sphere(data.Point(0.0, 0.0, 0.0), 2.0, data.Color(0.0, 0.0, 1.0), data.Finish(.2, .4, .5, .05)) t = (a, data.Point(0, 2, 0)) pe = cast.get_pe(t) sphere = data.Sphere(data.Point(0, 0, 0), 2.0, data.Color(0, 0, 0), data.Finish(.2, .4, .5, .05)) N = data.Vector(0, 0, 0) V = data.Vector(5, 5, 5) s = vector_math.dot_vector(N, V) print s print "%f %f %f" % (pe.x, pe.y, pe.z)