taichi icon indicating copy to clipboard operation
taichi copied to clipboard

[gui] Add some built-in APIs for building transformation matrix

Open Morcki opened this issue 1 year ago • 1 comments

Related issue = #5513

Usage

# Get translation matrix
translate_vec = ti.Vector([1., 2., 3.])
translate_mat = ti.Matrix.translate(translate_vec[0], translate_vec[1], translate_vec[2])
# Get scale matrix
scale_vec = ti.Vector([1., 2., 3.])
scale_mat = ti.Matrix.scale(scale_vec[0], scale_vec[1], scale_vec[2])
# Get rotation matrix
angleZ =  1.046
angleX = 0.52
angleY = -0.785
rotation_mat = ti.Matrix.rotation4d(angleX, angleY, angleZ)
# or call as blew, which is the same restult with rotation4d
# rotation_mat = ti.Matrix.eulerAngleYXZ(angleZ, angleX, angleY)

# Then build a transformation matrix
trans_mat = translate_mat @ rotation_mat @ scale_mat

# Additional
# usage of rotate_by_vector
identity = ti.Matrix.identity(ti.f32, 4)
Angle = math.pi * 0.5
X = ti.Vector([1.0, 0.0, 0.0])
Y = ti.Vector([0.0, 1.0, 0.0, 1.0])
Y1 = ti.Matrix.rotate_by_vector(identity, Angle, X) @ Y
# Here Y1 is the same with Y2
Y2 = ti.Matrix.eulerAngleX(Angle) @ Y

Mainly new built-in APIs Details please refer to the notes of each API

  • Translation
    • ti.Matrix.translate(dx, dy, dz)
  • Scale
    • ti.Matrix.scale(sx, sy, sz)
  • Rotation
    • ti.Matrix.eulerAngleX(angleX)
    • ti.Matrix.eulerAngleY(angleY)
    • ti.Matrix.eulerAngleZ(angleZ)
    • ti.Matrix.eulerAngleXY(angleX, angleY)
    • ti.Matrix.eulerAngleYX(angleY, angleX)
    • ti.Matrix.eulerAngleXZ(angleX, angleZ)
    • ti.Matrix.eulerAngleZX(angleZ, angleX)
    • ti.Matrix.eulerAngleYZ(angleY, angleZ)
    • ti.Matrix.eulerAngleZY(angleZ, angleY)
    • ti.Matrix.eulerAngleXYZ(t1, t2, t3)
    • ti.Matrix.eulerAngleYXZ(yaw, pitch, roll)
    • ti.Matrix.eulerAngleXZX(t1, t2, t3)
    • ti.Matrix.eulerAngleXYX(t1, t2, t3)
    • ti.Matrix.eulerAngleYXY(t1, t2, t3)
    • ti.Matrix.eulerAngleYZY(t1, t2, t3)
    • ti.Matrix.eulerAngleZYZ(t1, t2, t3)
    • ti.Matrix.eulerAngleZXZ(t1, t2, t3)
    • ti.Matrix.eulerAngleXZY(t1, t2, t3)
    • ti.Matrix.eulerAngleYZX(t1, t2, t3)
    • ti.Matrix.eulerAngleZYX(t1, t2, t3)
    • ti.Matrix.eulerAngleZXY(t1, t2, t3)
    • ti.Matrix.yawPitchRoll(yaw, pitch, roll)
    • ti.Matrix.rotate_by_vector(m, angle, v)
    • ti.Matrix.rotation3d(angle)
    • ti.Matrix.rotation4d(angleX, angleY, angleZ)

Morcki avatar Aug 11 '22 10:08 Morcki

Deploy Preview for docsite-preview ready!

Name Link
Latest commit 1d5294b22ddf680a7b88b7b619a64193a9b6dad8
Latest deploy log https://app.netlify.com/sites/docsite-preview/deploys/62f4f9bfdad57b00087b3386
Deploy Preview https://deploy-preview-5735--docsite-preview.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

netlify[bot] avatar Aug 11 '22 10:08 netlify[bot]

I'm afraid we have several rotation implemented in different places now:

  1. https://github.com/taichi-dev/taichi/blob/master/python/taichi/lang/matrix.py#L1081
  2. https://github.com/taichi-dev/taichi/blob/master/python/taichi/math/mathimpl.py#L497
  3. https://github.com/taichi-dev/taichi/blob/master/python/taichi/math/mathimpl.py#L1081

Shall we collect them in one place?

neozhaoliang avatar Aug 16 '22 07:08 neozhaoliang

@neozhaoliang Sorry for not noticing what we have done, I think we need to merge all those into one place. (Maybe need a meeting to talk about it)

Morcki avatar Aug 16 '22 08:08 Morcki

This is great! Just wondering if we'd make these matrix operations library functions (i.e ti.math.xx) or member functions to Matrix? Any preferences @strongoier?

jim19930609 avatar Aug 16 '22 09:08 jim19930609

This is great! Just wondering if we'd make these matrix operations library functions (i.e ti.math.xx) or member functions to Matrix? Any preferences @strongoier?

I prefer making them library functions.

strongoier avatar Aug 16 '22 09:08 strongoier

I'm afraid we have several rotation implemented in different places now:

  1. https://github.com/taichi-dev/taichi/blob/master/python/taichi/lang/matrix.py#L1081
  2. https://github.com/taichi-dev/taichi/blob/master/python/taichi/math/mathimpl.py#L497
  3. https://github.com/taichi-dev/taichi/blob/master/python/taichi/math/mathimpl.py#L1081

Shall we collect them in one place?

Agreed, then we'd suggest migration these implementations to https://github.com/taichi-dev/taichi/blob/master/python/taichi/math/mathimpl.py#L497. Thanks in advance! @Morcki

jim19930609 avatar Aug 16 '22 10:08 jim19930609

I'm afraid we have several rotation implemented in different places now:

  1. https://github.com/taichi-dev/taichi/blob/master/python/taichi/lang/matrix.py#L1081
  2. https://github.com/taichi-dev/taichi/blob/master/python/taichi/math/mathimpl.py#L497
  3. https://github.com/taichi-dev/taichi/blob/master/python/taichi/math/mathimpl.py#L1081

Shall we collect them in one place?

Agreed, then we'd suggest migration these implementations to https://github.com/taichi-dev/taichi/blob/master/python/taichi/math/mathimpl.py#L497. Thanks in advance! @Morcki

Ok, I will finish that. But there are still some questions suggested by rendong that if we should keep only some of the APIs listed as blew (drop others), and if the name of API should change to rotate3d_by_**, and if the name of the args should change from angle to radius. We should keep those all one unified format and give users a convenient usage.

  • ti.Matrix.eulerAngleX(angleX)
  • ti.Matrix.eulerAngleY(angleY)
  • ti.Matrix.eulerAngleZ(angleZ)
  • ti.Matrix.eulerAngleYXZ(yaw, pitch, roll)
  • ti.Matrix.yawPitchRoll(yaw, pitch, roll)
  • ti.Matrix.rotate_by_vector(m, angle, v)
  • ti.Matrix.rotation3d(angle)
  • ti.Matrix.rotation4d(angleX, angleY, angleZ)

Morcki avatar Aug 16 '22 13:08 Morcki

As talked under offerline meeting, we decide to keep the APIs as below:

  • Translation
    • ti.math.translate(dx, dy, dz)
  • Scale
    • ti.math.scale(sx, sy, sz)
  • Rotation
    • ti.math.rot3d_by_axis(ang, axis)
    • ti.math.rot3d_yaw_pitch_roll(yaw, pitch, roll)
    • ti.math.rotation3d(ang_x, ang_y, ang_z)

refer to #5827

Morcki avatar Aug 18 '22 10:08 Morcki