105. opengl.matrix
— opengl/matrix.py¶
Python OpenGL framework for pyFormex
- class opengl.matrix.Matrix4(data=None)[source]¶
A 4x4 transformation matrix for homogeneous coordinates.
The matrix is to be used with post-multiplication on row vectors (i.e. OpenGL convention).
- Parameters:
data (array_like (4,4), optional) – If specified, should be a (4,4) float array or compatible. Else a 4x4 identity matrix is created.
Examples
>>> I = Matrix4() >>> print(I) [[1. 0. 0. 0.] [0. 1. 0. 0.] [0. 0. 1. 0.] [0. 0. 0. 1.]]
We can first scale and then rotate, or first rotate and then scale:
>>> a = Matrix4().scale([4.,4.,4.]).rotate(45.,[0.,0.,1.]) >>> a Matrix4([[ 2.8284, 2.8284, 0. , 0. ], [-2.8284, 2.8284, 0. , 0. ], [ 0. , 0. , 4. , 0. ], [ 0. , 0. , 0. , 1. ]]) >>> b = Matrix4().rotate(45.,[0.,0.,1.]).scale([4.,4.,4.]) >>> np.allclose(a,b) True
- gl()[source]¶
Get the transformation matrix as a ‘ready-to-use’-gl version.
Returns the (4,4) Matrix as a rowwise flattened array of type float32.
Example:
>>> Matrix4().gl() Matrix4([1., 0., 0., 0., 0., 1., 0., 0., 0., 0., 1., 0., 0., 0., 0., 1.], dtype=float32)
- property rot¶
Return the (3,3) rotation matrix
- property trl¶
Return the (3,) translation vector
- translate(vector)[source]¶
Translate a 4x4 matrix by a (3,) vector.
vector: (3,) float array: the translation vector
Changes the Matrix in place and also returns the result
Example:
>>> Matrix4().translate([1.,2.,3.]) Matrix4([[1., 0., 0., 0.], [0., 1., 0., 0.], [0., 0., 1., 0.], [1., 2., 3., 1.]])
- rotate(angle, axis=None)[source]¶
Rotate a Matrix4.
The rotation can be specified by
an angle and axis,
a 3x3 rotation matrix,
a 4x4 trtransformation matrix (Matrix4).
Parameters:
- angle: float: the rotation angle. A 3x3 or 4x4 matrix may be
give instead, to directly specify the roation matrix.
axis: int or (3,) float: the axis to rotate around
Changes the Matrix in place and also returns the result.
Example:
>>> Matrix4().rotate(90.,[0.,1.,0.]) Matrix4([[ 0., 0., -1., 0.], [ 0., 1., 0., 0.], [ 1., 0., 0., 0.], [ 0., 0., 0., 1.]])
- scale(vector)[source]¶
Scale a 4x4 matrix by a (3,) vector.
vector: (3,) float array: the scaling vector
Changes the Matrix in place and also returns the result
Example:
>>> Matrix4().scale([1.,2.,3.]) Matrix4([[1., 0., 0., 0.], [0., 2., 0., 0.], [0., 0., 3., 0.], [0., 0., 0., 1.]])
- transform(x)[source]¶
Transform a vertex using this matrix.
x: a (3,) or (4,) vector.
If the vector has length 4, it holds homogeneous coordinates, and the result is the dot product of the vector with the Matrix: x * M. If the vector has length 3, the 4th homogeneous coordinate is assumed to be 1, and the product is computed in an optimized way.