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_cast_ray_5(self): ray = data.Ray(data.Point(0.0, 0.0, 0.0), data.Vector(5.0, 0.0, 0.0)) sphere1 = data.Sphere(data.Point(5.0, 0.0, -5.0), 5.0, data.Color(1.0, 0.0, 0.0)) sphere2 = data.Sphere(data.Point(17.0, 0.0, 5.0), 5.0, data.Color(0.0, 0.0, 1.0)) sphere_list = [sphere1, sphere2] casted = cast.cast_ray(ray, sphere_list) self.assertEqual(casted, data.Color(1.0, 0.0, 0.0))
def main(): args = sys.argv sphere_list = get_sphere_list(args[1]) eye_point = data.Point(0.0, 0.0, -14.0) view = [-10.0, 10.0, -7.5, 7.5, 1024, 768] ambient_light = data.Color(1.0, 1.0, 1.0) light = data.Light(data.Point(-100.0, 100.0, -100.0), data.Color(1.5, 1.5, 1.5)) if len(args) > 2: try: for i in range(2, len(args)): if args[i] == "-eye": l = args[i:i + 4] eye_point = commandline.get_eye_point(l, [0.0, 0.0, -14.0]) elif args[i] == "-view": l = args[i:i + 7] view = commandline.get_view(l, view) elif args[i] == "-light": l = args[i:i + 7] light = commandline.get_light( l, [-100.0, 100.0, -100.0, 1.5, 1.5, 1.5]) elif args[i] == "-ambient": l = args[i:i + 4] ambient_light = commandline.get_ambient_light( l, [1.0, 1.0, 1.0]) except: print "Something went horribly wrong" cast.cast_all_rays(view[0], view[1], view[2], view[3], view[4], view[5], eye_point, sphere_list, ambient_light, light)
def test_sphere_intersection_point(self): #no intersection with sphere sphere = data.Sphere(data.Point(0, 0, 0), 1) ray = data.Ray(data.Point(5, 0, 0), data.Vector(10, 0, 0)) outcome = None self.assertEqual(collisions.sphere_intersection_point(ray, sphere), outcome)
def test_sphere_intersection_point_2(self): #ray intersects at two points sphere = data.Sphere(data.Point(0, 0, 0), 5) ray = data.Ray(data.Point(0, -10, 0), data.Vector(0, 5, 0)) outcome = data.Point(0, -5, 0) self.assertEqual(collisions.sphere_intersection_point(ray, sphere), outcome)
def test_cast_ray_4(self): ray = data.Ray(data.Point(0.0, -3.0, -3.0), data.Vector(2.0, 1.0, 1.0)) sphere1 = data.Sphere(data.Point(0.0, 2.0, 2.0), 6.0, data.Color(1.0, 0.0, 0.0)) sphere2 = data.Sphere(data.Point(-10.0, -15.0, -20.0), 2.0, data.Color(0.0, 0.0, 1.0)) sphere_list = [sphere1, sphere2] casted = cast.cast_ray(ray, sphere_list) self.assertEqual(casted, data.Color(1.0, 0.0, 0.0))
def test_vector_from_to_2(self): p1 = data.Point(3.0, 2.0, 1.0) p2 = data.Point(1.0, 2.0, 3.0) v = vector_math.vector_from_to(p1, p2) self.assertAlmostEqual(v.x, -2.0) self.assertAlmostEqual(v.y, 0.0) self.assertAlmostEqual(v.z, 2.0)
def test_sphere_intersection_point1(self): theRay = data.Ray(data.Point(0, 0, 0), data.Vector(1, 0, 0)) theSphere = data.Sphere(data.Point(0, 0, 0), 1) pointt = collisions.sphere_intersection_point(theRay, theSphere) self.assertAlmostEqual(pointt.x, 1.0) self.assertAlmostEqual(pointt.y, 0.0) self.assertAlmostEqual(pointt.z, 0.0)
def test_difference_point_1(self): p1 = data.Point(0.0, 0.0, 0.0) p2 = data.Point(1.0, 2.0, 3.0) p3 = vector_math.difference_point(p1, p2) self.assertAlmostEqual(p3.x, -1.0) self.assertAlmostEqual(p3.y, -2.0) self.assertAlmostEqual(p3.z, -3.0)
def test_sphere_intersection_0(self): point_ray = data.Point(-10.0, 0.0, 0.0) direction = data.Vector(1.0, 0.0, 0.0) ray = data.Ray(point_ray, direction) point0_sphere = data.Point(0.0, 2.0, 0.0) sphere0 = data.Sphere(point0_sphere, 1.0) self.assertEqual(collisions.sphere_intersection_point(ray, sphere0), None)
def test_cast_ray1(self): ray = data.Ray(data.Point(0, 0, 0), data.Vector(1, 0, 0)) sphere_list = [ data.Sphere(data.Point(1, 1, 1), 3), data.Sphere(data.Point(3, 3, 3), 1) ] boolean = cast.cast_ray(ray, sphere_list) self.assertEqual(boolean, True)
def test_ray_equality_2(self): p1 = data.Point(0.1, 1.0, 2.0) v1 = data.Vector(3.0, 4.0, 5.0) r1 = data.Ray(p1, v1) p2 = data.Point(0.0, 1.0, 2.0) v2 = data.Vector(3.0, 4.0, 5.0) r2 = data.Ray(p2, v2) self.assertFalse(r1 == r2)
def test_translate_point_2(self): p = data.Point(0.0, 0.0, 0.0) v = data.Vector(1.0, 2.0, 3.0) p_translated = vector_math.translate_point(p, v) self.assertEqual(p_translated, data.Point(1.0, 2.0, 3.0)) self.assertAlmostEqual(p_translated.x, 1.0) self.assertAlmostEqual(p_translated.y, 2.0) self.assertAlmostEqual(p_translated.z, 3.0)
def test_sphere_equality_2(self): p1 = data.Point(1.0, 0.0, 0.0) r1 = 1.0 s1 = data.Sphere(p1, r1) p2 = data.Point(0.0, 0.0, 0.0) r2 = 1.0 s2 = data.Sphere(p2, r2) self.assertFalse(s1 == s2)
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_find_sphere_intersections_0(self): point_ray = data.Point(-10.0, 0.0, 0.0) direction = data.Vector(1.0, 0.0, 0.0) ray = data.Ray(point_ray, direction) point0_sphere = data.Point(0.0, 2.0, 0.0) sphere0 = data.Sphere(point0_sphere, 1.0) collision_points = collisions.find_intersection_points([sphere0], ray) self.assertListAlmostEqual(collision_points, [])
def test_sphere_intersection_3(self): point_ray = data.Point(-2581.5873, 7307.182, -4513.9069) direction = data.Vector(2669.2004, -7638.7545, -83.06) ray = data.Ray(point_ray, direction) point_sphere = data.Point(-188.86, -50.360, -300.360) sphere = data.Sphere(point_sphere, 400) collision_point = collisions.sphere_intersection_point(ray, sphere) self.assertEqual(collision_point, None)
def test_sphere_intersection_1(self): point_ray = data.Point(-10.0, 0.0, 0.0) direction = data.Vector(1.0, 0.0, 0.0) ray = data.Ray(point_ray, direction) point1_sphere = data.Point(0.0, 1.0, 0.0) sphere1 = data.Sphere(point1_sphere, 1.0) collision_point = collisions.sphere_intersection_point(ray, sphere1) self.assertEqual(collision_point, data.Point(0.0, 0.0, 0.0))
def test_find_intersection_2(self): sphere_1 = data.Sphere(data.Point(20, 20, 20), 2) sphere_2 = data.Sphere(data.Point(60, 60, 60), 1) s_list = [sphere_1, sphere_2] new_list = [] r = data.Ray(data.Point(0, 0, 0), data.Vector(0, 0, 1)) self.assertEqual(collisions.find_intersection_points(s_list, r), new_list)
def test_cast_ray(self): r = data.Ray(data.Point(0, 0, 0), data.Vector(0, 50, 0)) sphere_list = [ data.Sphere(data.Point(0, 50, 0), 10, data.Color(0.0, 1.0, 1.0)), data.Sphere(data.Point(0, 100, 0), 10, data.Color(1.0, 1.0, 0.0)) ] self.assertEqual(cast.cast_ray(r, sphere_list), data.Color(0.0, 1.0, 1.0))
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_find_intersection(self): sphere_1 = data.Sphere(data.Point(1, 1, 0), 1) sphere_2 = data.Sphere(data.Point(200, 200, 200), 1) s_list = [sphere_1, sphere_2] new_list = [(data.Sphere(data.Point(1, 1, 0), 1), data.Point(0, 1, 0))] r = data.Ray(data.Point(-1, 1, 0), data.Vector(20, 0, 0)) self.assertEqual(collisions.find_intersection_points(s_list, r), new_list)
def cmd_line(command_arguments): eye = data.Point(0, 0, -14) min_x = -10.0 max_x = 10.0 min_y = -7.5 max_y = 7.5 width = 1024.0 height = 768.0 light = data.Light(data.Point(-100.0, 100.0, -100.0), data.Color(1.5, 1.5, 1.5)) ambient = data.Color(1.0, 1.0, 1.0) for idx, arg in enumerate(command_arguments[2:]): if arg == "-eye": try: eye = data.Point(float(command_arguments[idx + 3]), float(command_arguments[idx + 4]), float(command_arguments[idx + 5])) except: print "argument error in eye" sys.exit() elif arg == "-view": try: min_x = float(command_arguments[idx + 3]) max_x = float(command_arguments[idx + 4]) min_y = float(command_arguments[idx + 5]) max_y = float(command_arguments[idx + 6]) width = float(command_arguments[idx + 7]) height = float(command_arguments[idx + 8]) except: print "argument error in view" sys.exit() elif arg == "-light": try: light_point = data.Point(float(command_arguments[idx + 3]), float(command_arguments[idx + 4]), float(command_arguments[idx + 5])) light_color = data.Color(float(command_arguments[idx + 6]), float(command_arguments[idx + 7]), float(command_arguments[idx + 8])) light = data.Light(light_point, light_color) except: print "argument error in light" sys.exit() elif arg == "-ambient": try: ambient = data.Color(float(command_arguments[idx + 3]), float(command_arguments[idx + 4]), float(command_arguments[idx + 5])) except: print "argument error in ambient" else: try: test_float = float(arg) except: print "usage: python ray_caster.py <filename> [-eye x y z] [-view min_x max_x min_y max_y width height] [-light x y z r g b] [-ambient r g b]" arg_list = [eye, min_x, max_x, min_y, max_y, width, height, light, ambient] return arg_list
def test_cast_ray(self): ray = data.Ray(data.Point(0, 0, 0), data.Vector(1, 0, 0)) sphere_list = [ data.Sphere(data.Point(1, 1, 1), 1), data.Sphere(data.Point(3, 3, 3), 1) ] color = data.Color boolean = cast.cast_ray(ray, sphere_list, color) self.assertEqual(boolean, False)
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_sphere_intersection_2(self): point_ray = data.Point(-10.0, 0.0, 0.0) direction = data.Vector(1.0, 0.0, 0.0) ray = data.Ray(point_ray, direction) point2_sphere = data.Point(0.0, 0.0, 0.0) sphere2 = data.Sphere(point2_sphere, 1.0) collision_points = collisions.sphere_intersection_point(ray, sphere2) self.assertAlmostEqual(collision_points.x, -1.0) self.assertAlmostEqual(collision_points.y, 0.0) self.assertAlmostEqual(collision_points.z, 0.0)
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 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_find_intersection_point1(self): #test ray 1 ray1 = data.Ray(data.Point(0, 0, 0), data.Vector(1, 0, 0)) spheres_list1 = [ data.Sphere(data.Point(3, 0, 0), 1), data.Sphere(data.Point(6, 0, 0), 1) ] intersection_points1 = [(spheres_list1[0], data.Point(2, 0, 0)), (spheres_list1[1], data.Point(5, 0, 0))] self.assertEqual( collisions.find_intersection_point(spheres_list1, ray1), intersection_points1)
def test_find_intersection_point2(self): #test ray 2 ray2 = data.Ray(data.Point(1, 5, 0), data.Vector(0, -1, 0)) spheres_list2 = [ data.Sphere(data.Point(1, 0, 0), 1), data.Sphere(data.Point(1, -5, 0), 1) ] intersection_points2 = [(spheres_list2[0], data.Point(1, 1, 0)), (spheres_list2[1], data.Point(1, -4, 0))] self.assertEqual( collisions.find_intersection_point(spheres_list2, ray2), intersection_points2)