6 from __future__
import print_function
8 from numpy
import sin, cos, arccos, arcsin, arctan2
9 from numpy
import r_, c_
26 return r_[ q[0], -q[1], -q[2], -q[3] ]
30 return quatConj(q)/np.square(np.linalg.norm(q))
42 result[0] = q1[0]*q2[0] - q1[1]*q2[1] - q1[2]*q2[2] - q1[3]*q2[3];
43 result[1] = q1[0]*q2[1] + q1[1]*q2[0] - q1[2]*q2[3] + q1[3]*q2[2];
44 result[2] = q1[0]*q2[2] + q1[1]*q2[3] + q1[2]*q2[0] - q1[3]*q2[1];
45 result[3] = q1[0]*q2[3] - q1[1]*q2[2] + q1[2]*q2[1] + q1[3]*q2[0];
54 d = 1.0 / (q1[0]*q1[0] + q1[1]*q1[1] + q1[2]*q1[2] + q1[3]*q1[3])
56 result[0] = (q1[0]*q2[0] + q1[1]*q2[1] + q1[2]*q2[2] + q1[3]*q2[3]) * d
57 result[1] = (q1[0]*q2[1] - q1[1]*q2[0] - q1[2]*q2[3] + q1[3]*q2[2]) * d
58 result[2] = (q1[0]*q2[2] + q1[1]*q2[3] - q1[2]*q2[0] - q1[3]*q2[1]) * d
59 result[3] = (q1[0]*q2[3] - q1[1]*q2[2] + q1[2]*q2[1] - q1[3]*q2[0]) * d
69 w1 = v1 / np.linalg.norm(v1)
70 w2 = v2 / np.linalg.norm(v2)
74 qResult[1:4] = np.cross(w1, w2)
77 qResult[0] = np.sqrt( np.square( np.dot(w1,w1) ) ) + np.dot(w1, w2)
80 qResult = qResult / np.linalg.norm(qResult)
85 return v / np.linalg.norm(v)
91 t = 2.0 * np.cross(q[1:4], v)
92 result = v + q[0] * t + np.cross(q[1:4], t)
96 t = 2.0 * np.cross(q[:,1:4], v)
97 result = v + (q[:,0] * t.T).T + np.cross(q[:,1:4], t)
106 dot = q1[0]*q2[0] + q1[1]*q2[1] + q1[2]*q2[2] + q1[3]*q2[3]
112 result = blendI*q1 + blend*tmpF
114 result = blendI*q1 + blend*q2
117 result = result / np.linalg.norm(result)
129 theta[0] = np.arctan2( 2 * (q[0]*q[1] + q[2]*q[3]), 1 - 2 * (q[1]*q[1] + q[2]*q[2]) )
130 theta[1] = np.arcsin( 2 * (q[0]*q[2] - q[3]*q[1]) )
131 theta[2] = np.arctan2( 2 * (q[0]*q[3] + q[1]*q[2]), 1 - 2 * (q[2]*q[2] + q[3]*q[3]) )
135 theta = np.empty((np.shape(q)[0],3))
137 theta[:,0] = np.arctan2( 2 * (q[:,0]*q[:,1] + q[:,2]*q[:,3]), 1 - 2 * (q[:,1]*q[:,1] + q[:,2]*q[:,2]) )
138 theta[:,1] = np.arcsin( 2 * (q[:,0]*q[:,2] - q[:,3]*q[:,1]) )
139 theta[:,2] = np.arctan2( 2 * (q[:,0]*q[:,3] + q[:,1]*q[:,2]), 1 - 2 * (q[:,2]*q[:,2] + q[:,3]*q[:,3]) )
149 hphi = euler[0] * 0.5
150 hthe = euler[1] * 0.5
151 hpsi = euler[2] * 0.5
162 q[0] = chphi * chthe * chpsi + shphi * shthe * shpsi
163 q[1] = shphi * chthe * chpsi - chphi * shthe * shpsi
164 q[2] = chphi * shthe * chpsi + shphi * chthe * shpsi
165 q[3] = chphi * chthe * shpsi - shphi * shthe * chpsi
169 q = np.zeros((np.shape(euler)[0],4))
170 hphi = euler[:,0] * 0.5
171 hthe = euler[:,1] * 0.5
172 hpsi = euler[:,2] * 0.5
183 q[:,0] = chphi * chthe * chpsi + shphi * shthe * shpsi
184 q[:,1] = shphi * chthe * chpsi - chphi * shthe * shpsi
185 q[:,2] = chphi * shthe * chpsi + shphi * chthe * shpsi
186 q[:,3] = chphi * chthe * shpsi - shphi * shthe * chpsi
203 psi = arctan2( A[0,1], A[0,0] )
219 c_[ cthe*cpsi, cthe*spsi, -sthe ],
220 c_[ -cphi*spsi + sphi*sthe*cpsi, cphi*cpsi + sphi*sthe*spsi, sphi*cthe ],
221 c_[ sphi*spsi + cphi*sthe*cpsi, -sphi*cpsi + cphi*sthe*spsi, cphi*cthe ],
228 phi = arctan2( A[1,2], A[2,2] )
229 the = arcsin( -A[0,2] )
230 psi = arctan2( A[0,1], A[0,0] )
232 return r_[ phi, the, psi ]
236 psi = arctan2( A[0,1]/cos(the), A[0,0]/cos(the) )
238 return r_[ phi, the, psi ]
253 c_[ 1.0 - 2 * (q[2]*q[2] + q[3]*q[3]), 2 * (q[1]*q[2] + q[0]*q[3]), 2 * (q[1]*q[3] - q[0]*q[2]) ],
254 c_[ 2 * (q[1]*q[2] - q[0]*q[3]), 1.0 - 2 * (q[1]*q[1] + q[3]*q[3]), 2 * (q[2]*q[3] + q[0]*q[1]) ],
255 c_[ 2 * (q[1]*q[3] + q[0]*q[2]), 2 * (q[2]*q[3] - q[0]*q[1]), 1.0 - 2 * (q[1]*q[1] + q[2]*q[2]) ],
271 q[0] = 0.5 * np.sqrt(1.0 + mat[0,0] + mat[1,1] + mat[2,2])
273 d = 1.0 / (4.0 * q[0])
275 q[1] = d * (mat[1,2] - mat[2,1])
276 q[2] = d * (mat[2,0] - mat[0,2])
277 q[3] = d * (mat[0,1] - mat[1,0])
286 mat = np.zeros((3,3))
313 mat = np.zeros((4,4))
346 q = q / np.linalg.norm(q)
348 theta = np.arccos( q[0] ) * 2.0
349 sin_a = np.sqrt( 1.0 - q[0] * q[0] )
351 if np.fabs( sin_a ) < 0.0005:
361 print(theta * 180/pi)
376 t1 = 1 - 2 * (q[2]*q[2] + q[3]*q[2])
377 t2 = 2 * (q[1]*q[2] + q[0]*q[3])
378 err = 2 / ( t1*t1 + t2*t2 )
380 dq[0] = err * (q[3]*t1)
381 dq[1] = err * (q[2]*t1)
382 dq[2] = err * (q[1]*t1 + 2 * q[2]*t2)
383 dq[3] = err * (q[0]*t1 + 2 * q[3]*t2)
415 Rn = R / np.sqrt(1 - e*e*Smu*Smu)
417 Pe = np.zeros(np.shape(LLA))
418 Pe[:,0] = (Rn + LLA[:,2]) * Cmu * Cl
419 Pe[:,1] = (Rn + LLA[:,2]) * Cmu * Sl
420 Pe[:,2] = (Rn*b*b/R/R + LLA[:,2]) * Smu
432 deltaLLA = np.zeros(np.shape(lla))
433 deltaLLA[:,0] = lla[:,0] - llaRef[0]
434 deltaLLA[:,1] = lla[:,1] - llaRef[1]
435 deltaLLA[:,2] = lla[:,2] - llaRef[2]
437 ned = np.zeros(np.shape(lla))
438 ned[:,0] = deltaLLA[:,0] * a2d
439 ned[:,1] = deltaLLA[:,1] * a2d * cos(llaRef[0] * pi/180)
440 ned[:,2] = -deltaLLA[:,2]
453 deltaLLA = np.zeros(np.shape(lla))
454 deltaLLA[0] = lla[0] - llaRef[0]
455 deltaLLA[1] = lla[1] - llaRef[1]
456 deltaLLA[2] = lla[2] - llaRef[2]
458 ned = np.zeros(np.shape(lla))
459 ned[0] = deltaLLA[0] * a2d
460 ned[1] = deltaLLA[1] * a2d * cos(llaRef[0] * pi/180)
461 ned[2] = -deltaLLA[2]
472 invA2d = 1.0 / 111120.0
474 deltaLLA = np.zeros(np.shape(ned))
475 deltaLLA[:,0] = ned[:,0] * invA2d
476 deltaLLA[:,1] = ned[:,1] * invA2d / cos(llaRef[0] * pi/180)
477 deltaLLA[:,2] = -ned[:,2]
489 lla = np.zeros(np.shape(ned))
490 lla[:,0] = llaRef[0] + deltaLLA[:,0]
491 lla[:,1] = llaRef[1] + deltaLLA[:,1]
492 lla[:,2] = llaRef[2] + deltaLLA[:,2]
541 euler = np.zeros(np.shape(ned))
542 euler[:,2] = arctan2( ned[:,1], ned[:,0] )
543 euler[:,1] = arctan2( -ned[:,2], np.sqrt( ned[:,0]**2 + ned[:,1]**2 ) )
558 eResult = np.zeros(np.shape(e))
562 for i
in range(0,np.shape(eResult)[0]):
574 eResult = np.zeros(np.shape(e))
578 for i
in range(0,np.shape(eResult)[0]):
590 vResult = np.zeros(np.shape(vIn))
592 for i
in range(0,np.shape(vResult)[0]):
596 vResult[i,:] = np.dot( DCM, vIn[i,:] )
602 vResult = np.zeros(np.shape(vIn))
604 for i
in range(0,np.shape(vResult)[0]):
608 vResult[i,:] = np.dot( DCM.T, vIn[i,:] )
614 vResult = np.zeros(np.shape(vIn))
619 for i
in range(0,np.shape(vResult)[0]):
621 vResult[i,:] = np.dot( DCM, vIn[i,:] )
627 vResult = np.zeros(np.shape(vIn))
632 for i
in range(0,np.shape(vResult)[0]):
634 vResult[i,:] = np.dot( DCM.T, vIn[i,:] )
641 psi = np.arctan2( v[1], v[0] )
643 v2 = np.dot( dcm, v[0:2] )
644 theta = np.arctan2( -v[2], v2[0] )
645 result = r_[ 0., theta, psi ]
658 for i
in range(0,np.shape(angle)[0]):
659 while angle[i] < -pi:
668 euler = np.zeros(np.shape(acc))
670 euler[:,0] = np.arctan2( -acc[:,1], -acc[:,2] )
671 euler[:,1] = np.arctan2( acc[:,0], np.sqrt( acc[:,1]*acc[:,1] + acc[:,2]*acc[:,2]) );
675 att = np.zeros(np.shape(acc))
676 bias = np.zeros(np.shape(acc))
683 gIF = np.r_[ 0, 0, -9.80665 ]
684 for i
in range(0,np.shape(bias)[0]):
688 g = np.dot( DCM, gIF )
689 bias[i,:] = acc[i,:] - g
696 return np.sqrt(np.sum(v*v, axis=axis))
698 qmat_matrix = np.array([[[1.0, 0, 0, 0],
716 if q1.shape[0] == 1
and q2.shape[0] == 1:
717 dots = np.empty_like(q2)
718 for i
in range(q2.shape[0]):
719 dots[i, :] = qmat_matrix.dot(q2.T).squeeze().dot(q1.T).T
720 elif q1.shape[0] == 1
and q2.shape[0] != 1:
721 dots = np.empty_like(q2)
722 for i
in range(q2.shape[0]):
723 dots[i, :] = qmat_matrix.dot(q2[i, :].T).squeeze().dot(q1.T).T
724 elif q1.shape[0] != 1
and q2.shape[0] == 1:
725 dots = np.empty_like(q2)
726 for i
in range(q2.shape[0]):
727 dots[i, :] = qmat_matrix.dot(q2.T).squeeze().dot(q1[i,:].T).T
728 elif q1.shape[0] == q2.shape[0]:
729 dots = np.empty_like(q2)
730 for i
in range(q2.shape[0]):
731 dots[i, :] = qmat_matrix.dot(q2[i,:].T).squeeze().dot(q1[i, :].T).T
733 raise Exception(
"Incompatible quaternion arrays -- cannot multiply")
752 q *= np.sign(q[:,0])[:,
None]
753 norm_v =
norm(q[:,1:], axis=1)
754 out = np.empty((len(q), 3))
756 out[~idx] = 2.0 * np.sign(q[~idx, 0,
None]) * q[~idx,1:]
757 out[idx] = (2.0 * np.arctan2(norm_v[idx,
None], q[idx,0,
None])) * q[idx,1:]/norm_v[idx,
None]
761 out = np.empty((len(v), 4))
762 norm_v =
norm(v, axis=1)
765 out[~idx, 1:] = v[~idx,:]
766 out[idx] = np.cos(norm_v[idx,
None]/2.0)
767 out[idx, 1:] = np.sin(norm_v[idx,
None]/2.0) * v[idx,:]/norm_v[idx,
None]
785 n = float(q.shape[0])
789 while prev_mu
is None or norm(
qboxminus(mu, prev_mu)) > 1e-3:
793 assert np.abs(1.0 -
norm(mu)) <= 1e03
797 assert q.shape[2] == 4
798 mu = np.empty((q.shape[0], 4))
799 for i
in tqdm(
range(q.shape[0])):
810 if __name__ ==
'__main__':
811 q = np.random.random((5000, 4))
812 q = q /
norm(q, axis=1)[:,
None]
822 assert np.sqrt(np.sum(np.square(
qmult(
qinv(q1), q3) - q2))) < 1e-8
826 qarr = np.random.random((5000, 25, 4))
827 qarr = qarr * 1.0/
norm(qarr, axis=2)[:,:,
None]
def vectorRotateInertialToBody2(vIn, eRot)
def eulerRotateBodyToInertial2(e, rot)
def quatRotVectArray(q, v)
def mul_Quat_Quat(q1, q2)
def quatNLerp(q1, q2, blend)
def lla2ecef(lla, metric=True)
GeneratorWrapper< T > range(T const &start, T const &end, T const &step)
def quat_Vec3_Vec3(v1, v2)
def rotate_ned2ecef(latlon)
def eulerRotateInertialToBody2(e, rot)
def lla2ned_single(llaRef, lla)
def ned2DeltaLla(ned, llaRef)
def div_Quat_Quat(q1, q2)
def rotate_ecef2ned(latlon)
def vectorRotateBodyToInertial2(vIn, eRot)
def vectorRotateBodyToInertial(vIn, eRot)
def vectorRotateInertialToBody(vIn, eRot)
def DCMeulerToPsi(A, phi, the)
def euler2quatArray(euler)