def test__ray_equal__given_two_not_equal_rays__return_false(): assert (Ray(direction=Vector((1, 2, 3)), origin=Point( (3, 5, 6))) == Ray(direction=Vector((1, 2, 3)), origin=Point((4, 5, 7)))) is False assert (Ray(direction=Vector((1, 2, 3)), origin=Point( (3, 5, 6))) == Ray(direction=Vector((4, 5, 7)), origin=Point((3, 5, 6)))) is False
def test__intersect_ray_with_sphere__two_point_intersection(): R = Ray(Point((0, 0, 2)), Vector( (1, 0, 0))) #ray passing through the sphere S = Sphere(Point((0, 0, 0)), 3) val = np.sqrt(5) assert (_intersect_ray_with_sphere(R, S) == (Point( (val, 0, 2)), Point((-val, 0, 2)))) is True
def test__sphere_equal__given_two_not_equal_spheres__return_false(): assert (Sphere(center=Point( (1, 2, 3)), radius=5) == Sphere(center=Point( (1, 2, 3)), radius=6)) is False assert (Sphere(center=Point( (1, 2, 3)), radius=5) == Sphere(center=Point( (3, 5, 6)), radius=5)) is False
def _intersect_ray_with_sphere(ray, sphere): """Function which provides the co-ordinates of intersection.""" o = ray._origin u = ray._direction r = sphere._radius c = sphere._center norm_u = np.linalg.norm(u._vector) U = (1 / norm_u) * u._vector #finds the unit direction b = 2 * np.dot(u._vector, (o - c)._vector) a = 1 c = np.dot((o - c)._vector, (o - c)._vector) - r**2 discriminant = b**2 - 4 * a * c if discriminant > 0: """two point intersection case.""" square_root_discriminant = np.sqrt(discriminant) d1 = (-1 * b + square_root_discriminant) / (2 * a) d2 = (-1 * b - square_root_discriminant) / (2 * a) p1 = Point(o._point + (d1 * U)) p2 = Point(o._point + (d2 * U)) return p1, p2 elif discriminant == 0: """one point interscetion case.""" d = -1 * b / 2 * a p = Point(o._point + (d * U)) return p else: return "Does not intersect"
def test_intersect_ray_with_sphere_false(): r1 = Ray((0, 0, 0), (1, 1, 1)) s1 = Sphere((10, 10, 10), 5) d, intercepts = intersect(r1, s1) intercepts_analytical = [Point([5.0, 0.0, 0.0]), Point([-5.0, 0.0, 0.0])] d_analytical = 2 assert ((intercepts == intercepts_analytical) and (d_analytical == d)) is False
def test__intersect__ray_with_sphere__return_false(): r1 = Ray((0, 0, 0), (1, 1, 1)) s1 = Sphere((10, 10, 10), 3) d, intersections = intersect(r1, s1) calculated_intersections = [ Point([3.0, 0.0, 0.0]), Point([-3.0, 0.0, 0.0]) ] d_analytical = 2 assert ((intersections == calculated_intersections) and (d_analytical == d)) is False
def test_intersect_ray_with_triangle_true(): t = Triangle((1, 0, 0), (0, 1, 0), (0, 0, 1)) r1 = Ray((0, 0, 0), (1, 1, 1)) d, intercepts = intersect(r1, t) intercepts_analytical = Point( [0.3333333333333333, 0.3333333333333333, 0.3333333333333333]) d_analytical = 1 assert ((intercepts == intercepts_analytical) and (d_analytical == d)) is True
def test__point_equal__given_two_equal_points__return_true(): assert (Point((1, 2, 3)) == Point((1, 2, 3))) is True
def test__point_subtraction__given_two_points__return_correct_vector(): """The result of a point being subtracted from another one is a vector.""" assert Point((0, 1, 2)) - Point((3, 4, 5)) == Vector((-3, -3, -3))
def test__point_right_addition__given_vector_and_point__return_correct_point(): """The result of a vector being added to a point is a point.""" assert Vector((0, 1, 2)) + Point((3, 4, 5)) == Point((3, 5, 7))
def test__ray_equal__given_two_rays_with_different_direction_and_origin_return_false( ): assert (Ray(Point((1, 1, 1)), Vector( (3, -4, 5))) == Ray(Point((0, 0, 0)), Vector((3, 4, 5)))) is False
def test_intersection__sphere_center_elsewhere__given_ray_that_originates_inside_sphere__return_true( ): assert (intersection(Ray((1, 1, 1), (0, 0, 1)), Sphere( (1, 1, 1), 1.0)) == Point([1, 1, 2]))
def test_intersection__sphere_center_elsewhere__given_tangent_ray__return_true( ): assert (intersection(Ray((-3, 1, 0), (1, 0, 0)), Sphere( (1, 1, 1), 1.0)) == Point([1, 1, 0]))
def test_intersection__sphere_center_at_origin__given_ray_that_intersects_at_two_points__return_true( ): assert array_equal( intersection(Ray((-2, 0, 0), (1, 0, 0)), Sphere((0, 0, 0), 1.0)), [Point([-1, 0, 0]), Point([1, 0, 0])]) is True
def test_intersection__ray_with_sphere_intersects_at_one_point_given_tangent_point__return_true( ): assert (intersect(Ray((0, 0, 0), (1, 1, 0)), Sphere( (0, 0, 0), 1.0)) == Point([1 / np.sqrt(2), 1 / np.sqrt(2), 0]))
# _intersect_ray_with_sphere # inputs for non intersecting ray and sphere x = Ray((0, 10, 0), (0, -1, 0)) y = Sphere((10, 0, 0), 5) def test_non_intersecting_ray_sphere_return_false(): assert _intersect_ray_with_sphere(x, y) is False # inputs for ray and sphere intersecting at 2 points x1 = Ray((0, 10, 0), (0, -1, 0)) y1 = Sphere((0, 0, 0), 5) # The two expected intersecting points p1 = Point((0, -5, 0)) p2 = Point((0, 5, 0)) def test_intersecting_ray_sphere_at_two_points_return_true(): x = _intersect_ray_with_sphere(x1, y1) diff1 = x[0] - p1 diff2 = x[1] - p2 d1 = abs(np.linalg.norm(diff1._vector)) d2 = abs(np.linalg.norm(diff2._vector)) assert (d1 < 10**(-5) and d2 < 10**(-5)) == True is True # inputs for ray and sphere intersecting at 1 point x2 = Ray((0, 10, 0), (0, -1, 0)) y2 = Sphere((5, 0, 0), 5)
def test__sphere_equal__given_two_not_equal_sphere__return_false(): assert (Sphere(Point((1,2,3)),5) == Sphere(Point((3,2,1)),4)) is False
def test__intersect_ray_with_sphere__one_point_intersection(): R = Ray(Point((0, 0, 3)), Vector((1, 0, 0))) #ray touching sphere surface S = Sphere(Point((0, 0, 0)), 3) assert (_intersect_ray_with_sphere(R, S) == Point((0, 0, 3))) is True
def test__intersect_ray_with_sphere__does_not_intersect(): R = Ray(Point((0, 0, 4)), Vector((1, 0, 0))) #ray outside sphere S = Sphere(Point((0, 0, 0)), 3) #sphere with center at origin and radius 3 assert (_intersect_ray_with_sphere(R, S) == "Does not intersect") is True
def test__sphere_equal__given_two_spheres_with_different_centers_and_radi_return_false( ): assert (Sphere(Point((1, 1, 1)), 5) == Sphere(Point( (0, 0, 0)), 10)) is False
def test_sphere_equal_given_two_equal_spheres_retun_true(): assert (Sphere(Point((0, 1, 2)), 5) == Sphere(Point((0, 1, 2)), 5)) is True
def test__point_equal__given_two_not_equal_points__return_false(): assert (Point((1, 2, 3)) == Point((4, 5, 6))) is False
def test_intersection__sphere_center_at_origin__given_tangent_ray__return_true( ): assert (intersection(Ray((-3, 1, 0), (1, 0, 0)), Sphere( (0, 0, 0), 1.0)) == Point([0, 1, 0]))
def test_triangle_init_function_point_list(): t1 = Triangle(Point((0, 10, 0)), Point((10, 0, 0)), Point((0, 0, 10))) t2 = Triangle((0, 10, 0), (10, 0, 0), (0, 0, 10)) assert (t1 == t2) is True
def test_intersection__sphere_center_at_origin__given_ray_that_originates_inside_sphere__return_true( ): assert (intersection(Ray((-0.5, 0, 0), (1, 0, 0)), Sphere( (0, 0, 0), 1.0)) == Point([1, 0, 0]))
def test_ray_init_function_point_list(): r1 = Ray(Point((0, 0, 0)), Point((1, 0, 0))) expected = Ray((0, 0, 0), (1, 0, 0)) assert (r1 == expected) is True
def test_intersection__sphere_center_elsewhere__given_ray_that_intersects_at_two_points__return_true( ): assert array_equal( intersection(Ray((-2, 1, 1), (1, 0, 0)), Sphere((1, 1, 1), 1.0)), [Point([0, 1, 1]), Point([2, 1, 1])]) is True
def test_sphere_init_function_point(): s1 = Sphere(Point((0, 0, 0)), 5) expected = Sphere((0, 0, 0), 5) assert (s1 == expected) is True
def test_intersection__ray_with_sphere_intersects_at_two_points_given_intersected_points__return_true( ): assert np.array_equal( intersect(Ray((-1, 0, 0), (1, 0, 0)), Sphere((0, 0, 0), 1.0)), [Point([-1, 0, 0]), Point([1, 0, 0])]) is True
def test_ray_equal_given_two_equal_rays_retun_true(): assert (Ray(Point((0, 1, 2)), Vector( (3, 4, 5))) == Ray(Point((0, 1, 2)), Vector((3, 4, 5)))) is True