コード例 #1
0
    def test_inversion(self):

        matrix_list = [[-5, 2, 6, -8], [1, -5, 1, 8], [7, 7, -6, -7],
                       [1, -3, 7, 4]]

        matrix_list_inverse = [[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]]

        Mt = matrix(matrix_list)
        Mt_inverse = inverse(Mt)
        Mt_inverse_output = matrix(matrix_list_inverse)

        self.assertEqual(det(Mt), 532, 'The determinant of 4x4 matrix failed')

        self.assertEqual(cofactor(Mt, 2, 3), -160,
                         'The cofactor of 4x4 matrix failed')
        self.assertEqual(Mt_inverse[3, 2], -160 / 532,
                         'The inverse element failed')

        self.assertEqual(cofactor(Mt, 3, 2), 105,
                         'The cofactor of 4x4 matrix failed')
        self.assertEqual(Mt_inverse[2, 3], 105 / 532,
                         'The inverse element failed')

        self.assertEqual(Mt_inverse, Mt_inverse_output,
                         'The 4x4 inverse failed')
コード例 #2
0
    def test_inverse_scaling(self):

        transform = scaling(2, 3, 4)

        inv = inverse(transform)

        v = vector(-4, 6, 8)

        v_out = vector(-2, 2, 2)

        self.assertEqual(inv * v, v_out, 'The inverse scaling failed')
コード例 #3
0
    def test_inv_translation(self):

        transform = translation(5, -3, 2)

        inv = inverse(transform)

        p = point(-3, 4, 5)

        p_out = point(-8, 7, 3)

        self.assertEqual(inv * p, p_out, 'The inverse translation failed')
コード例 #4
0
def intersect(obj, r):
    '''
    Intercection time between obj and r.

    intersect(obj, r)

    Intersection beween an object obj and a ray r that returns the intersection objects the ray
    intersects the obj. If there is no intersection, return an empty array.

    Parameters
    ----------

    -obj (scene_obj): object in the scene to intersect
    -r (ray): ray to intersect

    Return
    ------
    -intersections: list with all intersections . Tangent intersections appear twice.
    '''

    if not isinstance(obj, scene_obj):
        raise TypeError('obj should be an scene_obj')

    rnew = transform(r, inverse(obj.transform))

    if isinstance(obj, sphere):

        d = rnew.direction
        r0 = rnew.origin
        rc = obj.center

        r2 = obj.r * obj.r
        D = r0 - rc

        D2 = dot(D, D)
        Dd = dot(D, d)
        d2 = dot(d, d)

        discriminant = Dd * Dd - d2 * (D2 - r2)

        if discriminant < 0:
            return intersections()

        t1 = (-Dd - sqrt(discriminant)) / d2
        t2 = (-Dd + sqrt(discriminant)) / d2

        i1 = intersection(t1, obj)
        i2 = intersection(t2, obj)

        return intersections(i1, i2)
    else:
        raise NotImplementedError('Not implemented')
コード例 #5
0
    def test_inversion3(self):

        matrix_list = [[9, 3, 0, 9], [-5, -2, -6, -3], [-4, 9, 6, 4],
                       [-7, 6, 6, 2]]

        matrix_list_inv = [[-0.04074, -0.07778, 0.14444, -0.22222],
                           [-0.07778, 0.03333, 0.36667, -0.33333],
                           [-0.02901, -0.14630, -0.10926, 0.12963],
                           [0.17778, 0.06667, -0.26667, 0.33333]]

        Mt = matrix(matrix_list)
        Mt_inverse = inverse(Mt)
        Mt_inverse_output = matrix(matrix_list_inv)

        self.assertEqual(Mt_inverse, Mt_inverse_output,
                         'The inverse of 4x4 matrix failed')
コード例 #6
0
    def test_inversion2(self):

        matrix_list1 = [[8, -5, 9, 2], [7, 5, 6, 1], [-6, 0, 9, 6],
                        [-3, 0, -9, -4]]

        matrix_list1_inv = [[-0.15385, -0.15385, -0.28205, -0.53846],
                            [-0.07692, 0.12308, 0.02564, 0.03077],
                            [0.35897, 0.35897, 0.43590, 0.92308],
                            [-0.69231, -0.69231, -0.76923, -1.92308]]

        Mt1 = matrix(matrix_list1)
        Mt_inverse1 = inverse(Mt1)
        Mt_inverse1_output = matrix(matrix_list1_inv)

        self.assertEqual(Mt_inverse1, Mt_inverse1_output,
                         'The inverse of 4x4 matrix failed')
コード例 #7
0
    def test_rotation_x(self):

        p = point(0, 1, 0)

        half_quarter = rotation_x(pi / 4)
        full_quarter = rotation_x(pi / 2)
        half_quarter_inv = inverse(half_quarter)

        p_out_half = point(0, sqrt(2) / 2, sqrt(2) / 2)
        p_out_full = point(0, 0, 1)
        p_out_inv = point(0, sqrt(2) / 2, -sqrt(2) / 2)

        self.assertEqual(half_quarter * p, p_out_half, 'x rotation failed')
        self.assertEqual(full_quarter * p, p_out_full, 'x rotation failed')
        self.assertEqual(half_quarter_inv * p, p_out_inv,
                         'x inverse rotation failed')
コード例 #8
0
    def test_inverse_multiplication(self):

        matrix_list_A = [[3, -9, 7, 3], [3, -8, 2, -9], [-4, 4, 4, 1],
                         [-6, 5, -1, 1]]

        matrix_list_B = [[8, 2, 2, 2], [3, -1, 7, 0], [7, 0, 5, 4],
                         [6, -2, 0, 5]]

        MtA = matrix(matrix_list_A)
        MtB = matrix(matrix_list_B)

        MtC = MtA * MtB

        self.assertEqual(MtC * inverse(MtB), MtA,
                         'Inverse multiplication failed')

        self.assertEqual