Пример #1
0
 def test_normalize_2(self):
     v = data.Vector(3.0, 4.0, 0.0)
     v_normalized = vector_math.normalize_vector(v)
     self.assertEqual(v_normalized, data.Vector(0.6, 0.8, 0.0))
     self.assertAlmostEqual(v_normalized.x, 0.6)
     self.assertAlmostEqual(v_normalized.y, 0.8)
     self.assertAlmostEqual(v_normalized.z, 0.0)
Пример #2
0
 def test_difference_vector_2(self):
     v1 = data.Vector(1.0, 2.0, 3.0)
     v2 = data.Vector(1.0, 2.0, 3.0)
     v3 = vector_math.difference_vector(v1, v2)
     self.assertAlmostEqual(v3.x, 0.0)
     self.assertAlmostEqual(v3.y, 0.0)
     self.assertAlmostEqual(v3.z, 0.0)
Пример #3
0
 def test_scale_2(self):
     v = data.Vector(1.0, 2.0, 3.0)
     scale_factor = 1.5
     v_scaled = vector_math.scale_vector(v, scale_factor)
     self.assertEqual(v_scaled, data.Vector(1.5, 3.0, 4.5))
     self.assertAlmostEqual(v_scaled.x, 1.5)
     self.assertAlmostEqual(v_scaled.y, 3.0)
     self.assertAlmostEqual(v_scaled.z, 4.5)
Пример #4
0
 def test_scale_1(self):
     v = data.Vector(0.0, 0.1, 0.2)
     scale_factor = 1.5
     v_scaled = vector_math.scale_vector(v, scale_factor)
     self.assertEqual(v_scaled, data.Vector(0.0, 0.15, 0.3))
     self.assertAlmostEqual(v_scaled.x, 0.0)
     self.assertAlmostEqual(v_scaled.y, 0.15)
     self.assertAlmostEqual(v_scaled.z, 0.3)
Пример #5
0
 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)
Пример #6
0
 def test_compute_specular_intensity1(self):
     eye_pt = data.Point(0, 0, -14)
     Pe = data.Point(0.47705401519604174, 1.4311620455881253,
                     -3.5048116656870825)
     normal = data.Vector(-0.04499212706658469, -0.13497638119975397,
                          -0.98982654648647)
     L_dir = data.Vector(-0.5887765593901371, 0.5775947735794256,
                         -0.5654435785566279)
     normal_Ldir = 0.5082198628016603
     result = auxiliary.compute_specular_intensity(Pe, normal, L_dir,
                                                   normal_Ldir, eye_pt)
     check = 0.5082196122911
     self.assertAlmostEqual(result, check)
Пример #7
0
 def test_compute_specular_intensity2(self):
     eye_pt = data.Point(0, 0, -14)
     Pe = data.Point(0.5507345333286873, 1.6622035998606193,
                     -1.843840267688793)
     normal = data.Vector(-0.22351515756871967, 0.3294545272938401,
                          -0.9173334665118366)
     L_dir = data.Vector(-0.5862702247202266, 0.5733674871854466,
                         -0.5723084380341965)
     normal_Ldir = 0.8449364794884124
     result = auxiliary.compute_specular_intensity(Pe, normal, L_dir,
                                                   normal_Ldir, eye_pt)
     check = 0.96077723243
     self.assertAlmostEqual(result, check)
Пример #8
0
 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)
Пример #9
0
 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)
Пример #10
0
 def test_ray_2(self):
     point2 = data.Point(0, -1, -2.0)
     vector2 = data.Vector(1, 2, 3.0)
     ray2 = data.Ray(point2, vector2)
     self.assertAlmostEqual(ray2.pt.x, 0)
     self.assertAlmostEqual(ray2.pt.y, -1)
     self.assertAlmostEqual(ray2.pt.z, -2)
     self.assertAlmostEqual(ray2.dir.x, 1)
     self.assertAlmostEqual(ray2.dir.y, 2)
     self.assertAlmostEqual(ray2.dir.z, 3)
     self.assertEqual(ray2.pt, point2)
     self.assertEqual(ray2.dir, vector2)
     self.assertEqual(ray2.pt, data.Point(0, -1, -2))
     self.assertEqual(ray2.dir, data.Vector(1, 2, 3))
     self.assertEqual(ray2,
                      data.Ray(data.Point(0, -1, -2), data.Vector(1, 2, 3)))
Пример #11
0
 def test_ray(self):
     point = data.Point(1, 2, 3)
     vector = data.Vector(4, 5, 6)
     ray = data.Ray(point, vector)
     self.assertAlmostEqual(ray.pt.x, 1)
     self.assertAlmostEqual(ray.pt.y, 2)
     self.assertAlmostEqual(ray.pt.z, 3)
     self.assertAlmostEqual(ray.dir.x, 4)
     self.assertAlmostEqual(ray.dir.y, 5)
     self.assertAlmostEqual(ray.dir.z, 6)
     self.assertEqual(ray.pt, point)
     self.assertEqual(ray.dir, vector)
     self.assertEqual(ray.pt, data.Point(1, 2, 3))
     self.assertEqual(ray.dir, data.Vector(4, 5, 6))
     self.assertEqual(ray,
                      data.Ray(data.Point(1, 2, 3), data.Vector(4, 5, 6)))
Пример #12
0
 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))
Пример #13
0
 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.assertAlmostEqual(p_translated.x, 1.0)
     self.assertAlmostEqual(p_translated.y, 2.0)
     self.assertAlmostEqual(p_translated.z, 3.0)
Пример #14
0
 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)
Пример #15
0
def normalize_vector(vector):
   invlength = 1 / math.sqrt(vector.x ** 2 + vector.y ** 2 + vector.z ** 2)
   x = vector.x * invlength
   y = vector.y * invlength
   z = vector.z * invlength
   normalized = data.Vector(x, y, z)
   return normalized
Пример #16
0
 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)
Пример #17
0
 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))
Пример #18
0
def normalize_vector(vector):
   length = math.sqrt(vector.x ** 2+ vector.y ** 2 + vector.z **2)
   n_vector_x = vector.x / length
   n_vector_y = vector.y / length
   n_vector_z = vector.z / length
   n_vector = data.Vector(n_vector_x, n_vector_y, n_vector_z)
   return n_vector
Пример #19
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)
Пример #20
0
 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))
Пример #21
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)
Пример #22
0
 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)
Пример #23
0
 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))
Пример #24
0
 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)
Пример #25
0
 def test_ray_1(self):
     ray1 = data.Ray(data.Point(0, 0, 0), data.Vector(1, 0, 0))
     self.assertAlmostEqual(ray1.pt.x, 0)
     self.assertAlmostEqual(ray1.pt.y, 0)
     self.assertAlmostEqual(ray1.pt.z, 0)
     self.assertAlmostEqual(ray1.dir.x, 1)
     self.assertAlmostEqual(ray1.dir.y, 0)
     self.assertAlmostEqual(ray1.dir.z, 0)
Пример #26
0
 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)
Пример #27
0
 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, [])
Пример #28
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)
Пример #29
0
 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)
Пример #30
0
 def test_translate_point_1(self):
     p = data.Point(0.0, 0.0, 0.0)
     v = data.Vector(0.0, 0.0, 0.0)
     p_translated = vector_math.translate_point(p, v)
     self.assertEqual(p_translated, data.Point(0.0, 0.0, 0.0))
     self.assertAlmostEqual(p_translated.x, 0.0)
     self.assertAlmostEqual(p_translated.y, 0.0)
     self.assertAlmostEqual(p_translated.z, 0.0)