MCPcopy Create free account
hub / github.com/PyMesh/PyMesh / Quaternion

Class Quaternion

python/pymesh/misc/quaternion.py:5–188  ·  view source on GitHub ↗

This class implements quaternion used for 3D rotations. Attributes: w (``float``): same as ``quaternion[0]``. x (``float``): same as ``quaternion[1]``. y (``float``): same as ``quaternion[2]``. z (``float``): same as ``quaternion[3]``.

Source from the content-addressed store, hash-verified

3from math import pi,sin,cos,atan2,sqrt
4
5class Quaternion:
6 """ This class implements quaternion used for 3D rotations.
7
8 Attributes:
9 w (``float``): same as ``quaternion[0]``.
10 x (``float``): same as ``quaternion[1]``.
11 y (``float``): same as ``quaternion[2]``.
12 z (``float``): same as ``quaternion[3]``.
13 """
14
15 def __init__(self, quat=[1, 0, 0, 0]):
16 self.__quat = np.array(quat, dtype=np.float)
17 self.normalize()
18
19 @classmethod
20 def fromAxisAngle(cls, axis, angle):
21 """ Crate quaternion from axis angle representation
22
23 Args:
24 angle (``float``): Angle in radian.
25 axis (``numpy.ndarray``): Rotational axis. Not necessarily
26 normalized.
27
28 Returns:
29 The following values are returned.
30
31 * ``quat`` (:class:`Quaternion`): The corresponding quaternion object.
32 """
33 axis = axis / norm(axis)
34 quat = np.array([
35 cos(angle/2),
36 sin(angle/2)*axis[0],
37 sin(angle/2)*axis[1],
38 sin(angle/2)*axis[2]
39 ])
40 return cls(quat)
41
42 @classmethod
43 def fromData(cls, v1, v2):
44 """ Create the rotation to rotate v1 to v2
45
46 Args:
47 v1 (``numpy.ndarray``): From vector. Normalization not necessary.
48 v2 (``numpy.ndarray``): To vector. Normalization not necessary.
49
50 Returns:
51 The following values are returned.
52
53 * ``quat`` (:class:`Quaternion`): Corresponding quaternion that
54 rotates ``v1`` to ``v2``.
55 """
56 eps = 1e-12
57 v1 /= norm(v1)
58 v2 /= norm(v2)
59 c = np.dot(v1, v2)
60 if c < -1.0 + eps:
61 # v1 is parallel and opposite of v2
62 u,s,v = svd(np.array([v1, v2]))

Callers 5

add_baseFunction · 0.90
__mul__Method · 0.85
__rmul__Method · 0.85
conjugateMethod · 0.85
rotateMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected