18 import general_python
as genpy
19 from math_tools
import general_math
as gen
23 f = int((n - d) * 1000000);
24 s =
'%d.%d' % (d, f)
if f > 0
else '%d' % d
25 ss =
'+' if n >= 0
else '-'
56 q1.w*q2.w - q1.x*q2.x - q1.y*q2.y - q1.z*q2.z,
57 q1.w*q2.x + q1.x*q2.w + q1.y*q2.z - q1.z*q2.y,
58 q1.w*q2.y - q1.x*q2.z + q1.y*q2.w + q1.z*q2.x,
59 q1.w*q2.z + q1.x*q2.y - q1.y*q2.x + q1.z*q2.w,
64 s = float(q2.w*q2.w + q2.x*q2.x + q2.y*q2.y + q2.z*q2.z)
66 ( q1.w*q2.w + q1.x*q2.x + q1.y*q2.y + q1.z*q2.z) / s,
67 (- q1.w*q2.x + q1.x*q2.w + q1.y*q2.z - q1.z*q2.y) / s,
68 (- q1.w*q2.y - q1.x*q2.z + q1.y*q2.w + q1.z*q2.x) / s,
69 (- q1.w*q2.z + q1.x*q2.y - q1.y*q2.x + q1.z*q2.w) / s
75 return math.sqrt(q.w*q.w + q.x*q.x + q.y*q.y + q.z*q.z);
84 """Conjugate of Quaternion.
86 >>> q = Quaternion((2, 2, 2, 2))
98 return (self.w, self.x, self.y, self.z)
102 return numpy.array([self.w, self.x, self.y, self.z])
113 return '(%s%s %s %si %s %sj %s %sk)' % tuple(args)
116 """Convert Quaternion to Unit Quaternion.
118 Unit Quaternion is Quaternion who's length is equal to 1.
120 >>> q = Quaternion((1, 3, 3, 3))
122 >>> print(q) # doctest: +ELLIPSIS
123 (0.1889822... + 0.5669467...i + 0.5669467...j + 0.5669467...k)
154 Returns a vector (4 X 1) containing elements of the unit quaternion corresponding to transformation or rotation matrix TRM.
155 (TRM can be the 4*4 transformation matrix or 3*3 rotation matrix)
156 the first element of the output vector contains the scalar part of the unit quaternion and the last three elements represent the vectorial part
159 assert gen.equal(numpy.linalg.det(TRM[0:3,0:3]), 1.0), genpy.err_str(__name__, self.__class__.__name__,sys._getframe().f_code.co_name,
"Given TRM is not a rotation matrix.")
161 uqn = numpy.zeros((4))
165 p = 1.0 + TRM[u,u] - TRM[v,v] - TRM[w,w]
167 if (p > gen.epsilon):
170 uqn[v+1] = (TRM[v,u] + TRM[u,v])/(2*p)
171 uqn[w+1] = (TRM[w,u] + TRM[u,w])/(2*p)
172 uqn[0] = (TRM[w,v] - TRM[v,w])/(2*p)
177 assert gen.equal(numpy.linalg.norm(uqn), 1.0),
"Impossible !"
182 Returns a vector (4 X 1) containing elements of the time derivation of the unit quaternion corresponding to transformation or rotation matrix TRM.
183 Matrix TRMD is the time derivative of the given transfer or rotation matrix TRM
185 (TRM and TRMD can be 4*4 transformation or 3*3 rotation matrices)
187 The last three elements of the output vector, represent the vectorial part
188 The first element, contains the real part of the unit quaternion speed.
190 uqns = numpy.zeros((4))
193 p = math.sqrt(1+TRM[u,u]-TRM[v,v]-TRM[w,w])
194 pp = (TRMD[u,u] - TRMD[v,v] - TRMD[w,w])/(2*p)
197 uqns[v+1] = (TRMD[v,u] + TRMD[u,v])/(2*p) - pp*(TRM[v,u] + TRM[u,v])/(2*p*p)
198 uqns[w+1] = (TRMD[w,u] + TRMD[u,w])/(2*p) - pp*(TRM[w,u] + TRM[u,w])/(2*p*p)
199 uqns[0] = (TRMD[w,v] - TRMD[v,w])/(2*p) - pp*(TRM[w,v] - TRM[v,w])/(2*p*p)
203 w = (TRM[w,v] - TRM[v,w])/(2*p)
214 Returns a vector (3 X 1) containing elements of the normalized quaternion corresponding to transformation matrix TRM.
215 (TRM is the 4*4 transformation matrix)
216 A normalized quaternion here means a quaternion that its real part is 1.0
218 nqn = numpy.zeros((3))
221 nqn[u] = (1+TRM[u,u]-TRM[v,v]-TRM[w,w])/(TRM[w,v]-TRM[v,w])
222 nqn[v] = (TRM[u,v]+TRM[v,u])/(TRM[w,v]-TRM[v,w])
223 nqn[w] = (TRM[u,w]+TRM[w,u])/(TRM[w,v]-TRM[v,w])