1~2问题我查api docs没找到,3问题是我使用之后的疑问。
- 是ti.Matrix能否可以获取某行,某列然后变成ti.Vector呢?
- ti.Matrix能否可以想numpy那样 m[0:2, 0:2],这样使用?我需要m[0, 0] … m[2, 2]去赋值
- ti.Matrix.Identity 为什么不能在python-score里面用呢?我必须自己写一个
@ti.pyfunc
def identity():
return ti.Matrix([
[1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0],
])
- 我自己写的taichi代码,这里还有优化空间吗?
# refer to GLM
# Right-Handed Coordinate System
import taichi as ti
@ti.pyfunc
def radians(degrees):
return degrees * 0.01745329251994329576923690768489
@ti.pyfunc
def degrees(radians):
return radians * 57.295779513082320876798154814105
@ti.pyfunc
def identity():
return ti.Matrix([
[1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0],
])
@ti.pyfunc
def translate(m, v):
return ti.Matrix([
[
m[0, 0], m[0, 1], m[0, 2],
m[0, 0] * v[0] + m[0, 1] * v[1] + m[0, 2] * v[2] + m[0, 3],
],
[
m[1, 0], m[1, 1], m[1, 2],
m[1, 0] * v[0] + m[1, 1] * v[1] + m[1, 2] * v[2] + m[1, 3],
],
[
m[2, 0], m[2, 1], m[2, 2],
m[2, 0] * v[0] + m[2, 1] * v[1] + m[2, 2] * v[2] + m[2, 3],
],
[
m[3, 0], m[3, 1], m[3, 2],
m[3, 0] * v[0] + m[3, 1] * v[1] + m[3, 2] * v[2] + m[3, 3],
],
])
@ti.pyfunc
def rotate(m, angle, v):
c = ti.cos(angle)
s = ti.sin(angle)
axis = v.normalized()
temp = (1.0 - c) * axis
Rotate = ti.Matrix([
[
temp[0] * axis[0] + c,
temp[0] * axis[1] - axis[2] * s,
temp[0] * axis[2] + axis[1] * s,
0.0,
],
[
temp[1] * axis[0] + axis[2] * s,
temp[1] * axis[1] + c,
temp[1] * axis[2] - axis[0] * s,
0.0,
],
[
temp[2] * axis[0] - axis[1] * s,
temp[2] * axis[1] + axis[0] * s,
temp[2] * axis[2] + c,
0.0,
],
[0.0, 0.0, 0.0, 1.0],
])
return Rotate @ m
@ti.pyfunc
def scale(m, v):
return ti.Matrix([
[m[0, 0] * v[0], m[0, 1] * v[1], m[0, 2] * v[2], m[0, 3]],
[m[1, 0] * v[0], m[1, 1] * v[1], m[1, 2] * v[2], m[1, 3]],
[m[2, 0] * v[0], m[2, 1] * v[1], m[2, 2] * v[2], m[2, 3]],
[m[3, 0] * v[0], m[3, 1] * v[1], m[3, 2] * v[2], m[3, 3]],
])
@ti.pyfunc
def lookAt(eye, center, up):
f = (center - eye).normalized()
s = (f.cross(up)).normalized()
u = s.cross(f)
return ti.Matrix([
[s.x, s.y, s.z, -s.dot(eye)],
[u.x, u.y, u.z, -u.dot(eye)],
[-f.x, -f.y, -f.z, f.dot(eye)],
[0.0, 0.0, 0.0, 1.0],
])
@ti.pyfunc
def ortho(left, right, bottom, top, zNear, zFar):
return ti.Matrix([
[2.0 / (right - left), 0.0, 0.0, -(right + left) / (right - left)],
[0.0, 2.0 / (top - bottom), 0.0, -(top + bottom) / (top - bottom)],
[0.0, 0.0, -2.0 / (zFar - zNear), -(zFar + zNear) / (zFar - zNear)],
[0.0, 0.0, 0.0, 1.0],
])
@ti.pyfunc
def perspective(fovy, aspect, zNear, zFar):
tanHalfFovy = ti.tan(fovy / 2.0)
return ti.Matrix([
[1.0 / (aspect * tanHalfFovy), 0.0, 0.0, 0.0],
[0.0, 1.0 / tanHalfFovy, 0.0, 0.0],
[
0.0, 0.0,
-(zFar + zNear) / (zFar - zNear),
-(2.0 * zFar * zNear) / (zFar - zNear),
],
[0.0, 0.0, -1.0, 0.0],
])