Skip to content

Commit a212efb

Browse files
committed
Add setRotationFromMatrix to Object3D py-side
1 parent 8a01afd commit a212efb

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

pythreejs/core/Object3D.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from math import pi, sqrt
2+
13
from ipywidgets import register
24

35
from .._base.Three import ThreeWidget
@@ -30,6 +32,37 @@ def lookAt(self, vector):
3032
def rotateX(self, rad):
3133
self.exec_three_obj_method('rotateX', rad)
3234

35+
def rotateY(self, rad):
36+
self.exec_three_obj_method('rotateY', rad)
37+
38+
def rotateZ(self, rad):
39+
self.exec_three_obj_method('rotateZ', rad)
40+
41+
def setRotationFromMatrix(self, m):
42+
"""
43+
m is a 3 by 3 matrix, as a list of rows. The columns of this matrix are
44+
the vectors x, y, and z
45+
"""
46+
#x = self.normalize(m[0:3])
47+
#y = self.normalize(m[3:6])
48+
#z = self.normalize(m[6:9])
49+
x = m[0:3]
50+
y = m[3:6]
51+
z = m[6:9]
52+
trace = x[0] + y[1] + z[2]
53+
if (trace > 0):
54+
s = 0.5 / sqrt(trace + 1)
55+
self.quaternion = [(y[2] - z[1]) * s, (z[0] - x[2]) * s, (x[1] - y[0]) * s, 0.25 / s]
56+
elif (x[0] > y[1] and x[0] > z[2]):
57+
s = 2.0 * sqrt(1.0 + x[0] - y[1] - z[2])
58+
self.quaternion = [0.25 * s, (y[0] + x[1]) / s, (z[0] + x[2]) / s, (y[2] - z[1]) / s]
59+
elif (y[1] > z[2]):
60+
s = 2.0 * sqrt(1.0 + y[1] - x[0] - z[2])
61+
self.quaternion = [(y[0] + x[1]) / s, 0.25 * s, (z[1] + y[2]) / s, (z[0] - x[2]) / s]
62+
else:
63+
s = 2.0 * sqrt(1.0 + z[2] - x[0] - y[1])
64+
self.quaternion = [(z[0] + x[2]) / s, (z[1] + y[2]) / s, 0.25 * s, (x[1] - y[0]) / s]
65+
3366
def _repr_keys(self):
3467
# Don't include aggregate structures in repr
3568
super_keys = super(Object3D, self)._repr_keys()

0 commit comments

Comments
 (0)