Example #1
0
    def assertInverse(self, a, b, symmetric=True):
        self.assertEqual(a.inverse(), b)
        if symmetric:
            self.assertEqual(b.inverse(), a)

        self.assertAlmostEqual(a.to_matrix() * b.to_matrix(),
                               Matrix.identity(4))
        self.assertAlmostEqual(b.to_matrix() * a.to_matrix(),
                               Matrix.identity(4))
Example #2
0
def rotation_matrix(axis_index, angle):
    """Create a 4x4 matrix representing a rotation around a single axis.

    The created matrix describes a rotation around the axis specified by its
    index. axis_index can be 0, 1, or 2. The angle is specified in radians.
    """

    i1 = (axis_index + 1) % 3
    i2 = (axis_index + 2) % 3

    s = math.sin(angle)
    c = math.cos(angle)

    rows = [
        [1, 0, 0, 0],
        [0, 1, 0, 0],
        [0, 0, 1, 0],
        [0, 0, 0, 1],
    ]

    rows[i1][i1] = c
    rows[i1][i2] = -s
    rows[i2][i1] = s
    rows[i2][i2] = c

    return Matrix(rows=rows)
Example #3
0
 def to_matrix(self):
     x, y, z = self._xyz
     return Matrix(rows=[
         [x, 0, 0, 0],
         [0, y, 0, 0],
         [0, 0, z, 0],
         [0, 0, 0, 1],
     ])
Example #4
0
 def to_matrix(self):
     x, y, z = self._vector
     return Matrix(rows=[
         [1, 0, 0, x],
         [0, 1, 0, y],
         [0, 0, 1, z],
         [0, 0, 0, 1],
     ])
Example #5
0
 def to_matrix(self):
     f = self._factor
     return Matrix(rows=[
         [f, 0, 0, 0],
         [0, f, 0, 0],
         [0, 0, f, 0],
         [0, 0, 0, 1],
     ])
Example #6
0
    def to_matrix(self):
        axis, angle = self._to_axis_angle()

        if axis is None:
            # No rotation
            return Matrix.identity(4)
        else:
            # Yes rotation
            return RotateAxisAngle(axis, angle).to_matrix()
Example #7
0
    def test_assert_almost_equal_matrix(self):
        epsilon = 1e-8
        delta   = 0.1

        m1 = Matrix(rows = [[1, 2], [3, 4]])
        m2 = Matrix(rows = [[1 + epsilon, 2 + epsilon], [3 + epsilon, 4 + epsilon]])
        m3 = Matrix(rows = [[1 + delta  , 2 + delta  ], [3 + delta  , 4 + delta  ]])
        m4 = Matrix(rows = [[1, 2], [3, 4], [5, 6]])

        # Almost equal
        self.assertAlmostEqual(m1, m1)
        self.assertAlmostEqual(m1, m2)

        # Not almost equal
        with self.assertRaises(AssertionError):
            self.assertAlmostEqual(m1, m3)

        # Not if the dimensions are different
        with self.assertRaises(AssertionError):
            self.assertAlmostEqual(m1, m4)
Example #8
0
    def to_matrix(self):
        s = math.sin(self._angle * degree)
        c = math.cos(self._angle * degree)
        C = 1 - c
        x, y, z = self._axis.normalized()

        return Matrix(rows=[
            [x * x * C + c, x * y * C - z * s, x * z * C + y * s, 0],
            [y * x * C + z * s, y * y * C + c, y * z * C - x * s, 0],
            [z * x * C - y * s, z * y * C + x * s, z * z * C + c, 0],
            [0, 0, 0, 1],
        ])
Example #9
0
def affine_matrix(x, y, z, t=None):
    """Create a 4x4 matrix representing an arbitrary affine transform.

    The created matrix describes an affine transform in 3D homogeneous
    coordinates:
        v -> (v1 * x + t1, v2 * y + t2, v3 * z + t3)

    The base vectors x, y, and z are not required to be orthogonal or
    normalized. The translation vector t can be omitted if it is the zero
    vector.
    """
    t = t or [0, 0, 0]
    return Matrix(rows=[
        [x[0], y[0], z[0], t[0]],
        [x[1], y[1], z[1], t[1]],
        [x[2], y[2], z[2], t[2]],
        [0, 0, 0, 1],
    ])
Example #10
0
 def to_matrix(self):
     result = Matrix.identity(4)
     for transform in self._transforms:
         result = result * transform.to_matrix()
     return result
Example #11
0
 def assertIdentity(self, matrix):
     self.assertSquare(matrix)
     self.assertAlmostEqual(matrix, Matrix.identity(matrix.row_count))
Example #12
0
from cadlib.util import Matrix

print("Empty matrix:")
print(Matrix())
print()

print("Full matrix:")
print(
    Matrix.from_rows([1, 0, 0, 0], [0, 22, 0, 0], [0, 0, 333, 0],
                     [0, 0, 0, 1]))