# 72. `opengl.matrix` — opengl/matrix.py

Python OpenGL framework for pyFormex

This OpenGL framework is intended to replace (in due time) the current OpenGL framework in pyFormex.

3. 2013 Benedict Verhegghe and the pyFormex project.

## 72.1. Classes defined in module opengl.matrix

class `opengl.matrix.``Vector4`

  One or more homogeneous coordinates

class `opengl.matrix.``Matrix4`

  A 4x4 transformation matrix for homogeneous coordinates.

  The matrix is to be used with post-multiplication on row vectors (i.e. OpenGL convention).

  - data: if specified, should be a (4,4) float array or compatible. Else a 4x4 identity matrix is created.

  Example:

      >>> 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.])
      >>> print(a)
      [[ 0.  4.  0.  0.]
       [-4.  0.  0.  0.]
       [ 0.  0.  8.  0.]
       [ 0.  0.  0.  1.]]
      >>> (a==b).all()
      True

  `gl`()

    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)

  `identity`()

    Reset the matrix to a 4x4 identity matrix.

  `translate`(vector)

    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)

    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)

    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.]])

  `swapRows`(row1, row2)

    Swap two rows.

    - row1, row2: index of the rows to swap
  `swapCols`(col1, col2)

    Swap two columns.

    - col1, col2: index of the columns to swap
  `inverse`()

    Return the inverse matrix

  `transform`(x)

    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.

  `invtransform`(x)

    Transform a vertex with the inverse of this matrix.

    - x: a (3,) or (4,) vector.
  `transinv`()

    Return the transpose of the inverse.

## 72.2. Functions defined in module opengl.matrix