103. opengl.matrix
— opengl/matrix.py¶
Python OpenGL framework for pyFormex
103.1. Classes defined in module opengl.matrix¶
-
class
opengl.matrix.
Vector4
(data=None)[source]¶ A set of homogeneous coordinates
The input can be 3D or 4D coordinates. The results is always a 2-dimensional array: a single point has nrows=1.
Examples
>>> Vector4([1, 2, 3]) Vector4([[1., 2., 3., 1.]]) >>> Vector4([[1, 2, 3],[4, 5, 6]]) Vector4([[1., 2., 3., 1.], [4., 5., 6., 1.]]) >>> Vector4([[1, 2, 3, 0], [4., 5., 6., 1.]]) Vector4([[1., 2., 3., 0.], [4., 5., 6., 1.]]) >>> Vector4([[1,2,3]]).dtype dtype('float32')
-
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 (with another scaling factor):
>>> a = I.scale([4.,4.,4.]).rotate(45.,[0.,0.,1.]) >>> b = I.rotate(45.,[0.,0.,1.]).scale([2.,2.,2.]) >>> a Matrix4([[ 0., 4., 0., 0.], [-4., 0., 0., 0.], [ 0., 0., 8., 0.], [ 0., 0., 0., 1.]]) >>> (a==b).all() 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.