Ejemplo n.º 1
0
    def __eq__(self, other):
        """Overrides the default implementation"""
        if not isinstance(other, Tuple):
            return False

        return all([
            equal(self.x, other.x),
            equal(self.y, other.y),
            equal(self.z, other.z),
            equal(self.w, other.w)
        ])
Ejemplo n.º 2
0
def test_calculating_the_inverse_of_a_matrix():
    # Given
    A = Matrix(-5, 2, 6, -8,
               1, -5, 1, 8,
               7, 7, -6, -7,
               1, -3, 7, 4)
    B = A.inverse()
    # Then
    assert A.determinant() == 532
    assert A.cofactor(2, 3) == -160
    assert equal(B.at(3, 2), -160/532)
    assert A.cofactor(3, 2) == 105
    assert equal(B.at(2, 3), 105/532)
    assert B == Matrix(0.21805, 0.45113, 0.24060, -0.04511,
                       -0.80827, -1.45677, -0.44361, 0.52068,
                       -0.07895, -0.22368, -0.05263, 0.19737,
                       -0.52256, -0.81391, -0.30075, 0.30639)
Ejemplo n.º 3
0
def test_intersecting_a_cone_with_a_ray():
    EXAMPLES = [
        # origin          direction            t0       t1
        (Point(0, 0, -5), Vector(0, 0, 1), 5, 5),
        (Point(0, 0, -5), Vector(1, 1, 1), 8.66025, 8.66025),
        (Point(1, 1, -5), Vector(-0.5, -1, 1), 4.55006, 49.44994),
    ]
    for origin, direction, t0, t1 in EXAMPLES:
        # Given
        shape = Cone()
        dir = direction.normalize()
        r = Ray(origin, dir)
        # When
        xs = shape.local_intersect(r)
        # Then
        assert len(xs) == 2
        assert equal(xs[0].t, t0)
        assert equal(xs[1].t, t1)
def test_a_ray_strikes_a_cylinder():
    EXAMPLES = [
        # origin            direction          t0       t1
        (Point(1, 0, -5), Vector(0, 0, 1), 5, 5),
        (Point(0, 0, -5), Vector(0, 0, 1), 4, 6),
        (Point(0.5, 0, -5), Vector(0.1, 1, 1), 6.80798, 7.08872),
    ]
    for origin, direction, t0, t1 in EXAMPLES:
        # Given
        cyl = Cylinder()
        dir = direction.normalize()
        r = Ray(origin, dir)
        # When
        xs = cyl.local_intersect(r)
        # Then
        assert len(xs) == 2
        assert equal(xs[0].t, t0)
        assert equal(xs[1].t, t1)
Ejemplo n.º 5
0
    def __eq__(self, other):
        """Overrides the default implementation"""
        if not isinstance(other, Matrix):
            return False

        return all([
            equal(self.at(r, c), other.at(r, c)) for r in range(self.size)
            for c in range(self.size)
        ])
Ejemplo n.º 6
0
def test_intersecting_a_cone_with_a_ray_parallel_to_one_of_its_halves():
    # Given
    shape = Cone()
    direction = Vector(0, 1, 1).normalize()
    r = Ray(Point(0, 0, -1), direction)
    # When
    xs = shape.local_intersect(r)
    # Then
    assert len(xs) == 1
    assert equal(xs[0].t, 0.35355)
Ejemplo n.º 7
0
def test_the_schlick_approximation_with_small_angle_and_n2_greater_than_n1():
    # Given
    shape = glass_sphere()
    r = Ray(Point(0, 0.99, -2), Vector(0, 0, 1))
    xs = Intersections(Intersection(1.8589, shape))
    # When
    comps = xs[0].prepare_computations(r, xs)
    reflectance = comps.schlick()
    # Then
    assert equal(reflectance, 0.48873)
Ejemplo n.º 8
0
def test_the_schlick_approximation_with_a_perpendicular_viewing_angle():
    # Given
    shape = glass_sphere()
    r = Ray(Point(0, 0, 0), Vector(0, 1, 0))
    xs = Intersections(Intersection(-1, shape), Intersection(1, shape))
    # When
    comps = xs[1].prepare_computations(r, xs)
    reflectance = comps.schlick()
    # Then
    assert equal(reflectance, 0.04)
def test_the_pixel_size_for_a_vertical_canvas():
    # Given
    c = Camera(125, 200, pi / 2)
    # Then
    assert equal(c.pixel_size, 0.01)
def test_the_pixel_size_for_a_horizontal_canvas():
    # Given
    c = Camera(200, 125, pi / 2)
    # Then
    assert equal(c.pixel_size, 0.01)