Example #1
0
 def test_find_intersection_points(self):
     sphere_list = [
         data.Sphere(data.Point(0, 0, 0), 2, data.Color(1, 0.5, 0.3), data.Finish(0.5, 0.4, 0.5, 0.05)),
         data.Sphere(data.Point(0, 20, 0), 1, data.Color(1.0, 0.0, 0.4), data.Finish(0.5, 0.4, 0.5, 0.05)),
     ]
     ray = data.Ray(data.Point(0, -20, 0), data.Vector(0, 1.3, 0))
     fip = collisions.find_intersection_points(sphere_list, ray)
     result_list = [
         (sphere_list[0], collisions.sphere_intersection_point(ray, sphere_list[0])),
         (sphere_list[1], collisions.sphere_intersection_point(ray, sphere_list[1])),
     ]
     self.assertEqual(fip, result_list)
Example #2
0
 def test_cast_ray_1(self):
     ray = data.Ray(data.Point(0, 10, 6), data.Vector(0, 0, -2))
     sphere_list = [
         data.Sphere(data.Point(0, 10, -2), 3, data.Color(0, 0.5, 1.0), data.Finish(0.5, 0.4, 0.5, 0.05)),
         data.Sphere(data.Point(0, 10, -20), 0.5, data.Color(0, 0.3, 0.2), data.Finish(0.5, 0.4, 0.5, 0.05)),
     ]
     ambient_color = data.Color(0.25, 0.5, 0.75)
     light = data.Light(data.Point(-100.0, 100.0, -100.0), data.Color(1.5, 1.5, 1.5))
     eye_position = data.Point(0, 0, -14)
     cr = cast.cast_ray(ray, sphere_list, ambient_color, light, eye_position)
     inter_point = collisions.sphere_intersection_point(ray, sphere_list[0])
     normal = collisions.sphere_normal_at_point(sphere_list[0], inter_point)
     off_pt = cast.find_pt_off_sphere(inter_point, normal)
     l_dir = vector_math.normalize_vector(vector_math.vector_from_to(off_pt, light.pt))
     l_dot_n = vector_math.dot_vector(normal, l_dir)
     diffuse_list = cast.determine_diffuse_contribution(
         sphere_list[0], off_pt, light, normal, sphere_list, l_dir, l_dot_n
     )
     spec_list = cast.determine_specular_contribution(
         l_dir, l_dot_n, normal, eye_position, off_pt, light.color, sphere_list[0].finish
     )
     self.assertEqual(
         cr,
         data.Color(
             0 + diffuse_list[0] + spec_list[0],
             0.125 + diffuse_list[1] + spec_list[1],
             0.375 + diffuse_list[2] + spec_list[2],
         ),
     )
Example #3
0
def calculate_light_components(
        sphere_point: typing.Tuple[data.Sphere, data.Point],
        spheres_list: typing.List[data.Sphere], eye_point: data.Point,
        light: data.Light) -> typing.Tuple[float, float]:
    Pe = compute_Pe(sphere_point)
    normal = collisions.sphere_normal_at_point(sphere_point[0],
                                               sphere_point[1])
    L_dir = v_math.normalize_vector(v_math.vector_from_to(Pe, light.pt))
    L_ray = data.Ray(Pe, L_dir)

    for other_sphere in spheres_list:
        if other_sphere is not sphere_point[0]:
            L_ray_intersection = collisions.sphere_intersection_point(
                L_ray, other_sphere)
            if L_ray_intersection:
                distance_sphere = distance_between_points(
                    Pe, L_ray_intersection)
                distance_light = distance_between_points(Pe, light.pt)
                if distance_sphere < distance_light:
                    return (0, 0)

    normal_Ldir = v_math.dot_vector(normal, L_dir)

    if normal_Ldir > 0:
        specular_intensity = compute_specular_intensity(
            Pe, normal, L_dir, normal_Ldir, eye_point)
        return (normal_Ldir, specular_intensity)
    else:
        return (0, 0)
Example #4
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)
Example #5
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)
Example #6
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)
 def test_sphere_intersection_1(self):
     sphere = data.Sphere(data.Point(5, 1, 0), 1)
     ray = data.Ray(data.Point(0, 0, 0), data.Vector(1, 0, 0))
     result = data.Point(5, 0, 0)
     compute = collisions.sphere_intersection_point(ray, sphere)
     self.assertEqual(compute == result, True)
     pass
Example #8
0
def find_intersection_points(sphere_list, ray):
    list_of_pairs = []
    for sphere in sphere_list:
        if sphere_intersection_point(ray, sphere) != None:
            list_of_pairs.append(
                (sphere, collisions.sphere_intersection_point(ray, sphere)))
    return list_of_pairs
Example #9
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)
Example #10
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)
Example #11
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))
Example #12
0
 def test_find_intersection_points_2(self):
     sphere_list = [
         data.Sphere(data.Point(0, 0, 0), 2, data.Color(0.23, 0.23, 0.23), data.Finish(0.5, 0.4, 0.5, 0.05)),
         data.Sphere(data.Point(20.3, 20, -100), 1, data.Color(0.45, 0.45, 0.67), data.Finish(0.5, 0.4, 0.5, 0.05)),
     ]
     ray = data.Ray(data.Point(0, -7, 0), data.Vector(0, 5, 0))
     fip = collisions.find_intersection_points(sphere_list, ray)
     result_list = [(sphere_list[0], collisions.sphere_intersection_point(ray, sphere_list[0]))]
     self.assertEqual(fip, result_list)
Example #13
0
 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)
Example #14
0
 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)
Example #15
0
 def test_sphere_intersection_point_3(self):
     sphere = data.Sphere(data.Point(0, 0, 0), 5)
     ray = data.Ray(data.Point(6, 0, 0), data.Vector(-2, 0, 0))
     outcome = data.Point(5, 0, 0)
     self.assertEqual(collisions.sphere_intersection_point(ray, sphere),
                      outcome)
Example #16
0
 def test_sphere_intersection1(self):
    sphere = data.Sphere(data.Point(0,0,.3), 2.1, data.Color(1.0, 1.0, 1.0))
    ray = data.Ray(data.Point(3, 6.1, 9.2), data.Vector(5,5,5))
    expected = None
    self.assertEqual(collisions.sphere_intersection_point(ray, sphere), expected)
Example #17
0
 def test_sphere_intersection4(self):
    sphere = data.Sphere(data.Point (4,5,6), 7, data.Color(1.0, 1.0, 1.0))
    ray = data.Ray(data.Point(1,2,3), data.Vector(1,2,3))
    expected = data.Point(4.08140072714, 8.16280145428, 12.2442021814)
    self.assertEqual(collisions.sphere_intersection_point(ray, sphere), expected)
Example #18
0
 def test_sphere_intersection5(self):
    sphere = data.Sphere(data.Point (-14, -16, -18), -20, data.Color(1.0, 1.0, 1.0))
    ray = data.Ray(data.Point(-21, -5, -19), data.Vector(-8, -15, -23))
    expected = data.Point(-26.1566613757, -14.6687400795, -33.8254014552)
    self.assertEqual(collisions.sphere_intersection_point(ray, sphere), expected)
Example #19
0
 def test_sphere_intersection6(self):
    sphere = data.Sphere(data.Point (0,0,0), 2, data.Color(1.0, 1.0, 1.0))
    ray = data.Ray(data.Point(2,0,0), data.Vector(0,5,0))
    expected = data.Point(2,0,0)
    self.assertEqual(collisions.sphere_intersection_point(ray, sphere), expected)
Example #20
0
 def test_sphere_intersection_point_5(self):
     ray = data.Ray(data.Point(1, -11, -12), data.Vector(0, 3, 3))
     sphere = data.Sphere(data.Point(0, 0, 0), 10, data.Color(1.0, 1.0, 1.0), data.Finish(0.5, 0.4, 0.5, 0.05))
     sip = collisions.sphere_intersection_point(ray, sphere)
     self.assertEqual(sip, data.Point(1, -6.51783442, -7.51783442))
 def test_sphere_intersection_3(self):
     sphere = data.Sphere(data.Point(1, 1, 1), 1)
     ray = data.Ray(data.Point(0, 0, 0), data.Vector(-1, -1, -1))
     result = None
     self.assertEqual(collisions.sphere_intersection_point(ray, sphere) == result, True)
     pass
Example #22
0
 def test_sphere_intersection_point2(self):
     #Test When Ray Starts in Sphere
     ray2 = data.Ray(data.Point(0, 5, 0), data.Vector(0, 10, 0))
     sphere2 = data.Sphere(data.Point(0, 5, 0), 1)
     self.assertEqual(collisions.sphere_intersection_point(ray2, sphere2),
                      data.Point(0, 6, 0))
Example #23
0
 def test_sphere_intersection_point1(self):
     #Test When Ray Passes Through Sphere Twice
     ray1 = data.Ray(data.Point(0, 0, 0), data.Vector(20, 0, 0))
     sphere1 = data.Sphere(data.Point(10, 0, 0), 1)
     self.assertEqual(collisions.sphere_intersection_point(ray1, sphere1),
                      data.Point(9, 0, 0))
Example #24
0
 def test_intersection_point_4(self):
     sphere = data.Sphere(data.Point(0, 0, 0), 1, data.Color(0, 1, 0),
                          data.Finish(.2, 1, 0, 0))
     ray = data.Ray(data.Point(0, 0, 0), data.Vector(1, 0, 0))
     int_point = collisions.sphere_intersection_point(ray, sphere)
     self.assertEqual(int_point, data.Point(1, 0, 0))
Example #25
0
 def test_sphere_intersection_point_6(self):
     ray = data.Ray(data.Point(0, -7, 0), data.Vector(0, 5, 0))
     sphere = data.Sphere(data.Point(20.3, 20, -100), 1, data.Color(0, 0, 0), data.Finish(0.5, 0.4, 0.5, 0.05))
     sip = collisions.sphere_intersection_point(ray, sphere)
     self.assertEqual(sip, None)
 def test_sphere_intersection_2(self):
     sphere = data.Sphere(data.Point(1, 1, 0), 1)
     ray = data.Ray(data.Point(0, 0, 0), data.Vector(1, 1, 0))
     result = data.Point(magicValue, magicValue, 0)
     self.assertEqual(collisions.sphere_intersection_point(ray, sphere) == result, True)
     pass
Example #27
0
 def test_sphere_intersection_point3(self):
     #Test when Ray misses Sphere
     ray3 = data.Ray(data.Point(0, 0, 0), data.Vector(-10, 0, 0))
     sphere3 = data.Sphere(data.Point(15, 0, 0), 1)
     self.assertEqual(collisions.sphere_intersection_point(ray3, sphere3),
                      None)
Example #28
0
 def test_sphere_intersection_point4(self):
     #Test when Ray glances/skims sphere
     ray4 = data.Ray(data.Point(1, 1, 0), data.Vector(-3, 0, 0))
     sphere4 = data.Sphere(data.Point(0, 0, 0), 1)
     self.assertEqual(collisions.sphere_intersection_point(ray4, sphere4),
                      data.Point(0, 1, 0))
Example #29
0
 def test_sphere_intersection_point_4(self):
     ray = data.Ray(data.Point(-10, 0, 3), data.Vector(1, 0, 0))
     sphere = data.Sphere(data.Point(0, 0, 0), 3, data.Color(1.0, 1.0, 1.0), data.Finish(0.5, 0.4, 0.5, 0.05))
     sip = collisions.sphere_intersection_point(ray, sphere)
     self.assertEqual(sip, data.Point(0, 0, 3))