Skip to content

Instantly share code, notes, and snippets.

@AnweshGangula
Forked from nkint/vec3-mat4.py
Created November 18, 2022 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnweshGangula/3f3d0d7f28464ea658c28d3ce35cbf6e to your computer and use it in GitHub Desktop.
Save AnweshGangula/3f3d0d7f28464ea658c28d3ce35cbf6e to your computer and use it in GitHub Desktop.
numpy - vec3 transformation mat4
import numpy as np
def cube_geometry(x, y, z):
xSize = x / 2
ySize = y / 2
zSize = z / 2
positions = np.array([
[-xSize, ySize, zSize], [xSize, ySize, zSize],
[-xSize, -ySize, zSize], [xSize, -ySize, zSize],
[xSize, ySize, -zSize], [-xSize, ySize, -zSize],
[xSize, -ySize, -zSize], [-xSize, -ySize, -zSize],
[-xSize, ySize, -zSize], [-xSize, ySize, zSize],
[-xSize, -ySize, -zSize], [-xSize, -ySize, zSize],
[xSize, ySize, zSize], [xSize, ySize, -zSize],
[xSize, -ySize, zSize], [xSize, -ySize, -zSize],
[-xSize, ySize, -zSize], [xSize, ySize, -zSize],
[-xSize, ySize, zSize], [xSize, ySize, zSize],
[-xSize, -ySize, zSize], [xSize, -ySize, zSize],
[-xSize, -ySize, -zSize], [xSize, -ySize, -zSize]
])
normals = np.array([
[0, 0, 1], [0, 0, 1],
[0, 0, 1], [0, 0, 1],
[0, 0, -1], [0, 0, -1],
[0, 0, -1], [0, 0, -1],
[-1, 0, 0], [-1, 0, 0],
[-1, 0, 0], [-1, 0, 0],
[1, 0, 0], [1, 0, 0],
[1, 0, 0], [1, 0, 0],
[0, 1, 0], [0, 1, 0],
[0, 1, 0], [0, 1, 0],
[0, -1, 0], [0, -1, 0],
[0, -1, 0], [0, -1, 0]
])
uvs = np.array([
[0, 1], [1, 1], [0, 0],
[1, 0], [0, 1], [1, 1],
[0, 0], [1, 0], [0, 1],
[1, 1], [0, 0], [1, 0],
[0, 1], [1, 1], [0, 0],
[1, 0], [0, 1], [1, 1],
[0, 0], [1, 0], [0, 1],
[1, 1], [0, 0], [1, 0]
])
cells = np.array([
[0, 2, 3], [0, 3, 1],
[4, 6, 7], [4, 7, 5],
[8, 10, 11], [8, 11, 9],
[12, 14, 15], [12, 15, 13],
[16, 18, 19], [16, 19, 17],
[20, 22, 23], [20, 23, 21]
])
return positions, cells, normals, uvs
def transform_vec3_mat4(a: np.ndarray, _m: np.ndarray):
"""
Transforms the vec3 with a mat4.
4th vector component is implicitly '1'
Reference:
https://en.wikipedia.org/wiki/Transformation_matrix#Examples_in_3D_computer_graphics
http://math.hws.edu/graphicsbook/c3/transform-matrices-3d.png
Code from:
https://github.com/stackgl/gl-vec3/blob/master/transformMat4.js
:param a: (3) -- float64
3 dimensions vector
:param m: (4, 4) -- float64
Mat 4x4 representing a transformation in 3D cartesian space
:return: (3) -- float64
transfromed vec3
"""
m = _m.flatten()
x = a[0]
y = a[1]
z = a[2]
w = m[3] * x + m[7] * y + m[11] * z + m[15]
w = w or 1.0
out = np.array([
(m[0] * x + m[4] * y + m[8] * z + m[12]) / w,
(m[1] * x + m[5] * y + m[9] * z + m[13]) / w,
(m[2] * x + m[6] * y + m[10] * z + m[14]) / w
])
return out
def transform_points(array: np.ndarray, mat: np.ndarray):
return np.array([transform_vec3_mat4(point, mat) for point in array])
positions, cells, normals, uvs = cube_geometry(1, 1, 1)
vx = 0.5
vy = 0
vz = 0
translate = np.array([
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
vx, vy, vz, 1,
])
positionsTranslated = transform_points(positions, translate)
print(positions)
print(positionsTranslated)
"""
result:
[[-0.5 0.5 0.5]
[ 0.5 0.5 0.5]
[-0.5 -0.5 0.5]
[ 0.5 -0.5 0.5]
[ 0.5 0.5 -0.5]
[-0.5 0.5 -0.5]
[ 0.5 -0.5 -0.5]
[-0.5 -0.5 -0.5]
[-0.5 0.5 -0.5]
[-0.5 0.5 0.5]
[-0.5 -0.5 -0.5]
[-0.5 -0.5 0.5]
[ 0.5 0.5 0.5]
[ 0.5 0.5 -0.5]
[ 0.5 -0.5 0.5]
[ 0.5 -0.5 -0.5]
[-0.5 0.5 -0.5]
[ 0.5 0.5 -0.5]
[-0.5 0.5 0.5]
[ 0.5 0.5 0.5]
[-0.5 -0.5 0.5]
[ 0.5 -0.5 0.5]
[-0.5 -0.5 -0.5]
[ 0.5 -0.5 -0.5]]
[[ 0. 0.5 0.5]
[ 1. 0.5 0.5]
[ 0. -0.5 0.5]
[ 1. -0.5 0.5]
[ 1. 0.5 -0.5]
[ 0. 0.5 -0.5]
[ 1. -0.5 -0.5]
[ 0. -0.5 -0.5]
[ 0. 0.5 -0.5]
[ 0. 0.5 0.5]
[ 0. -0.5 -0.5]
[ 0. -0.5 0.5]
[ 1. 0.5 0.5]
[ 1. 0.5 -0.5]
[ 1. -0.5 0.5]
[ 1. -0.5 -0.5]
[ 0. 0.5 -0.5]
[ 1. 0.5 -0.5]
[ 0. 0.5 0.5]
[ 1. 0.5 0.5]
[ 0. -0.5 0.5]
[ 1. -0.5 0.5]
[ 0. -0.5 -0.5]
[ 1. -0.5 -0.5]]
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment