simvx.core.math.matrices¶
Pure NumPy matrix utilities — replaces GLM dependency.
All matrices use NumPy’s native row-major layout. When sending to GLSL:
Matrices must be transposed (GLSL expects column-major)
This is handled at GPU boundary points only
This eliminates the entire class of row-major vs column-major bugs.
Module Contents¶
Functions¶
Convert a quaternion to a 4x4 rotation matrix. |
|
Create 4x4 identity matrix. |
|
Create perspective projection matrix. |
|
Create view matrix using look-at vectors. |
|
Create translation matrix. |
|
Create rotation matrix using axis-angle representation. |
|
Create scale matrix. |
|
Create orthographic projection matrix. |
|
Build model matrix from position, rotation quaternion, and scale. |
|
Build N model matrices from arrays of positions, quaternions, and scales. |
|
Convert mat4 to bytes for GPU upload (64 bytes, row-major float32). |
Data¶
API¶
- simvx.core.math.matrices.log¶
‘getLogger(…)’
- simvx.core.math.matrices.__all__¶
[‘identity’, ‘perspective’, ‘look_at’, ‘translate’, ‘rotate’, ‘scale’, ‘orthographic’, ‘quat_to_mat4…
- simvx.core.math.matrices.quat_to_mat4(q, dtype: numpy.dtype = np.float32) numpy.ndarray[source]¶
Convert a quaternion to a 4x4 rotation matrix.
Args: q: Quaternion — any object with .w, .x, .y, .z attributes (e.g. glm.quat) or a 4-element array [w, x, y, z]. dtype: NumPy data type (default float32)
Returns: 4x4 rotation matrix as numpy array
- simvx.core.math.matrices.identity(dtype: numpy.dtype = np.float32) numpy.ndarray[source]¶
Create 4x4 identity matrix.
Args: dtype: NumPy data type (default float32)
Returns: 4x4 identity matrix as numpy array
- simvx.core.math.matrices.perspective(fov: float, aspect: float, near: float, far: float, dtype: numpy.dtype = np.float32) numpy.ndarray[source]¶
Create perspective projection matrix.
Args: fov: Field of view in radians aspect: Aspect ratio (width / height) near: Near clipping plane far: Far clipping plane dtype: NumPy data type (default float32)
Returns: 4x4 perspective projection matrix
Note: Does NOT include Y-flip for Vulkan. Caller must do: proj[1, 1] *= -1 # Flip Y-axis for Vulkan
- simvx.core.math.matrices.look_at(eye: numpy.ndarray | tuple[float, float, float], center: numpy.ndarray | tuple[float, float, float], up: numpy.ndarray | tuple[float, float, float], dtype: numpy.dtype = np.float32) numpy.ndarray[source]¶
Create view matrix using look-at vectors.
Args: eye: Camera position center: Point to look at up: Up vector (should be normalized) dtype: NumPy data type (default float32)
Returns: 4x4 view matrix
- simvx.core.math.matrices.translate(t: numpy.ndarray | tuple[float, float, float], dtype: numpy.dtype = np.float32) numpy.ndarray[source]¶
Create translation matrix.
Args: t: Translation vector (x, y, z) dtype: NumPy data type (default float32)
Returns: 4x4 translation matrix
- simvx.core.math.matrices.rotate(axis: numpy.ndarray | tuple[float, float, float], angle: float, dtype: numpy.dtype = np.float32) numpy.ndarray[source]¶
Create rotation matrix using axis-angle representation.
Args: axis: Rotation axis (should be normalized) angle: Rotation angle in radians dtype: NumPy data type (default float32)
Returns: 4x4 rotation matrix
Uses Rodrigues’ rotation formula.
- simvx.core.math.matrices.scale(s: numpy.ndarray | tuple[float, float, float] | float, dtype: numpy.dtype = np.float32) numpy.ndarray[source]¶
Create scale matrix.
Args: s: Scale factors (x, y, z) or uniform scale factor dtype: NumPy data type (default float32)
Returns: 4x4 scale matrix
- simvx.core.math.matrices.orthographic(left: float, right: float, bottom: float, top: float, near: float, far: float, dtype: numpy.dtype = np.float32) numpy.ndarray[source]¶
Create orthographic projection matrix.
Args: left: Left plane right: Right plane bottom: Bottom plane top: Top plane near: Near plane far: Far plane dtype: NumPy data type (default float32)
Returns: 4x4 orthographic projection matrix
Note: For Vulkan, you may need to flip Y: proj[1, 1] *= -1
- simvx.core.math.matrices.mat4_from_trs(pos: tuple[float, float, float] | numpy.ndarray, rot, scl: tuple[float, float, float] | numpy.ndarray) numpy.ndarray[source]¶
Build model matrix from position, rotation quaternion, and scale.
Args: pos: Position (x, y, z) rot: Rotation quaternion — Quat or any object with .w/.x/.y/.z scl: Scale (x, y, z)
Returns: 4x4 model matrix as numpy array (Translate * Rotate * Scale)
- simvx.core.math.matrices.batch_mat4_from_trs(positions: numpy.ndarray, rotations: numpy.ndarray, scales: numpy.ndarray) numpy.ndarray[source]¶
Build N model matrices from arrays of positions, quaternions, and scales.
Args: positions: (N, 3) float32 positions rotations: (N, 4) float32 quaternions [w, x, y, z] scales: (N, 3) float32 scale factors
Returns: (N, 4, 4) float32 model matrices (Translate * Rotate * Scale)