no 'dangling' functions

@staticmethod is what we need here
This commit is contained in:
Martin Diehl 2020-02-21 10:45:14 +01:00
parent 1e1cb3f151
commit c84a6e90c9
1 changed files with 427 additions and 427 deletions

View File

@ -4,6 +4,13 @@ from . import Lambert
P = -1 P = -1
def isone(a):
return np.isclose(a,1.0,atol=1.0e-7,rtol=0.0)
def iszero(a):
return np.isclose(a,0.0,atol=1.0e-12,rtol=0.0)
#################################################################################################### ####################################################################################################
class Rotation: class Rotation:
u""" u"""
@ -183,7 +190,7 @@ class Rotation:
return angles in degrees. return angles in degrees.
""" """
eu = qu2eu(self.quaternion) eu = Rotation.qu2eu(self.quaternion)
if degrees: eu = np.degrees(eu) if degrees: eu = np.degrees(eu)
return eu return eu
@ -201,13 +208,13 @@ class Rotation:
return tuple of axis and angle. return tuple of axis and angle.
""" """
ax = qu2ax(self.quaternion) ax = Rotation.qu2ax(self.quaternion)
if degrees: ax[3] = np.degrees(ax[3]) if degrees: ax[3] = np.degrees(ax[3])
return (ax[:3],np.degrees(ax[3])) if pair else ax return (ax[:3],np.degrees(ax[3])) if pair else ax
def asMatrix(self): def asMatrix(self):
"""Rotation matrix.""" """Rotation matrix."""
return qu2om(self.quaternion) return Rotation.qu2om(self.quaternion)
def asRodrigues(self, def asRodrigues(self,
vector = False): vector = False):
@ -221,16 +228,16 @@ class Rotation:
return as actual Rodrigues--Frank vector, i.e. rotation axis scaled by tan(ω/2). return as actual Rodrigues--Frank vector, i.e. rotation axis scaled by tan(ω/2).
""" """
ro = qu2ro(self.quaternion) ro = Rotation.qu2ro(self.quaternion)
return ro[:3]*ro[3] if vector else ro return ro[:3]*ro[3] if vector else ro
def asHomochoric(self): def asHomochoric(self):
"""Homochoric vector: (h_1, h_2, h_3).""" """Homochoric vector: (h_1, h_2, h_3)."""
return qu2ho(self.quaternion) return Rotation.qu2ho(self.quaternion)
def asCubochoric(self): def asCubochoric(self):
"""Cubochoric vector: (c_1, c_2, c_3).""" """Cubochoric vector: (c_1, c_2, c_3)."""
return qu2cu(self.quaternion) return Rotation.qu2cu(self.quaternion)
def asM(self): def asM(self):
""" """
@ -276,7 +283,7 @@ class Rotation:
if np.any(eu < 0.0) or np.any(eu > 2.0*np.pi) or eu[1] > np.pi: if np.any(eu < 0.0) or np.any(eu > 2.0*np.pi) or eu[1] > np.pi:
raise ValueError('Euler angles outside of [0..2π],[0..π],[0..2π].\n{} {} {}.'.format(*eu)) raise ValueError('Euler angles outside of [0..2π],[0..π],[0..2π].\n{} {} {}.'.format(*eu))
return Rotation(eu2qu(eu)) return Rotation(Rotation.eu2qu(eu))
@staticmethod @staticmethod
def fromAxisAngle(angleAxis, def fromAxisAngle(angleAxis,
@ -294,7 +301,7 @@ class Rotation:
if not np.isclose(np.linalg.norm(ax[0:3]), 1.0): if not np.isclose(np.linalg.norm(ax[0:3]), 1.0):
raise ValueError('Axis angle rotation axis is not of unit length.\n{} {} {}'.format(*ax[0:3])) raise ValueError('Axis angle rotation axis is not of unit length.\n{} {} {}'.format(*ax[0:3]))
return Rotation(ax2qu(ax)) return Rotation(Rotation.ax2qu(ax))
@staticmethod @staticmethod
def fromBasis(basis, def fromBasis(basis,
@ -316,7 +323,7 @@ class Rotation:
or not np.isclose(np.dot(om[2],om[0]), 0.0): or not np.isclose(np.dot(om[2],om[0]), 0.0):
raise ValueError('matrix is not orthogonal.\n{}'.format(om)) raise ValueError('matrix is not orthogonal.\n{}'.format(om))
return Rotation(om2qu(om)) return Rotation(Rotation.om2qu(om))
@staticmethod @staticmethod
def fromMatrix(om, def fromMatrix(om,
@ -338,7 +345,7 @@ class Rotation:
if ro[3] < 0.0: if ro[3] < 0.0:
raise ValueError('Rodriques rotation angle not positive.\n'.format(ro[3])) raise ValueError('Rodriques rotation angle not positive.\n'.format(ro[3]))
return Rotation(ro2qu(ro)) return Rotation(Rotation.ro2qu(ro))
@staticmethod @staticmethod
def fromHomochoric(homochoric, def fromHomochoric(homochoric,
@ -348,7 +355,7 @@ class Rotation:
else np.array(homochoric,dtype=float) else np.array(homochoric,dtype=float)
if P > 0: ho *= -1 # convert from P=1 to P=-1 if P > 0: ho *= -1 # convert from P=1 to P=-1
return Rotation(ho2qu(ho)) return Rotation(Rotation.ho2qu(ho))
@staticmethod @staticmethod
def fromCubochoric(cubochoric, def fromCubochoric(cubochoric,
@ -356,10 +363,10 @@ class Rotation:
cu = cubochoric if isinstance(cubochoric, np.ndarray) and cubochoric.dtype == np.dtype(float) \ cu = cubochoric if isinstance(cubochoric, np.ndarray) and cubochoric.dtype == np.dtype(float) \
else np.array(cubochoric,dtype=float) else np.array(cubochoric,dtype=float)
ho = cu2ho(cu) ho = Rotation.cu2ho(cu)
if P > 0: ho *= -1 # convert from P=1 to P=-1 if P > 0: ho *= -1 # convert from P=1 to P=-1
return Rotation(ho2qu(ho)) return Rotation(Rotation.ho2qu(ho))
@staticmethod @staticmethod
@ -437,417 +444,410 @@ class Rotation:
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#################################################################################################### ####################################################################################################
#---------- Quaternion ----------
def isone(a): @staticmethod
return np.isclose(a,1.0,atol=1.0e-7,rtol=0.0) def qu2om(qu):
"""Quaternion to rotation matrix."""
def iszero(a): qq = qu[0]**2-(qu[1]**2 + qu[2]**2 + qu[3]**2)
return np.isclose(a,0.0,atol=1.0e-12,rtol=0.0) om = np.diag(qq + 2.0*np.array([qu[1],qu[2],qu[3]])**2)
#---------- Quaternion ---------- om[1,0] = 2.0*(qu[2]*qu[1]+qu[0]*qu[3])
om[0,1] = 2.0*(qu[1]*qu[2]-qu[0]*qu[3])
def qu2om(qu): om[2,1] = 2.0*(qu[3]*qu[2]+qu[0]*qu[1])
"""Quaternion to rotation matrix.""" om[1,2] = 2.0*(qu[2]*qu[3]-qu[0]*qu[1])
qq = qu[0]**2-(qu[1]**2 + qu[2]**2 + qu[3]**2) om[0,2] = 2.0*(qu[1]*qu[3]+qu[0]*qu[2])
om = np.diag(qq + 2.0*np.array([qu[1],qu[2],qu[3]])**2) om[2,0] = 2.0*(qu[3]*qu[1]-qu[0]*qu[2])
return om if P > 0.0 else om.T
om[1,0] = 2.0*(qu[2]*qu[1]+qu[0]*qu[3])
om[0,1] = 2.0*(qu[1]*qu[2]-qu[0]*qu[3]) @staticmethod
om[2,1] = 2.0*(qu[3]*qu[2]+qu[0]*qu[1]) def qu2eu(qu):
om[1,2] = 2.0*(qu[2]*qu[3]-qu[0]*qu[1]) """Quaternion to Bunge-Euler angles."""
om[0,2] = 2.0*(qu[1]*qu[3]+qu[0]*qu[2]) q03 = qu[0]**2+qu[3]**2
om[2,0] = 2.0*(qu[3]*qu[1]-qu[0]*qu[2]) q12 = qu[1]**2+qu[2]**2
return om if P > 0.0 else om.T chi = np.sqrt(q03*q12)
if iszero(chi):
def qu2eu(qu): eu = np.array([np.arctan2(-P*2.0*qu[0]*qu[3],qu[0]**2-qu[3]**2), 0.0, 0.0]) if iszero(q12) else \
"""Quaternion to Bunge-Euler angles.""" np.array([np.arctan2(2.0*qu[1]*qu[2],qu[1]**2-qu[2]**2), np.pi, 0.0])
q03 = qu[0]**2+qu[3]**2 else:
q12 = qu[1]**2+qu[2]**2 eu = np.array([np.arctan2((-P*qu[0]*qu[2]+qu[1]*qu[3])*chi, (-P*qu[0]*qu[1]-qu[2]*qu[3])*chi ),
chi = np.sqrt(q03*q12) np.arctan2( 2.0*chi, q03-q12 ),
np.arctan2(( P*qu[0]*qu[2]+qu[1]*qu[3])*chi, (-P*qu[0]*qu[1]+qu[2]*qu[3])*chi )])
if iszero(chi):
eu = np.array([np.arctan2(-P*2.0*qu[0]*qu[3],qu[0]**2-qu[3]**2), 0.0, 0.0]) if iszero(q12) else \ # reduce Euler angles to definition range, i.e a lower limit of 0.0
np.array([np.arctan2(2.0*qu[1]*qu[2],qu[1]**2-qu[2]**2), np.pi, 0.0]) eu = np.where(eu<0, (eu+2.0*np.pi)%np.array([2.0*np.pi,np.pi,2.0*np.pi]),eu)
else: return eu
eu = np.array([np.arctan2((-P*qu[0]*qu[2]+qu[1]*qu[3])*chi, (-P*qu[0]*qu[1]-qu[2]*qu[3])*chi ),
np.arctan2( 2.0*chi, q03-q12 ), @staticmethod
np.arctan2(( P*qu[0]*qu[2]+qu[1]*qu[3])*chi, (-P*qu[0]*qu[1]+qu[2]*qu[3])*chi )]) def qu2ax(qu):
"""
# reduce Euler angles to definition range, i.e a lower limit of 0.0 Quaternion to axis angle pair.
eu = np.where(eu<0, (eu+2.0*np.pi)%np.array([2.0*np.pi,np.pi,2.0*np.pi]),eu)
return eu Modified version of the original formulation, should be numerically more stable
"""
if iszero(qu[1]**2+qu[2]**2+qu[3]**2): # set axis to [001] if the angle is 0/360
def qu2ax(qu): ax = [ 0.0, 0.0, 1.0, 0.0 ]
""" elif not iszero(qu[0]):
Quaternion to axis angle pair. s = np.sign(qu[0])/np.sqrt(qu[1]**2+qu[2]**2+qu[3]**2)
omega = 2.0 * np.arccos(np.clip(qu[0],-1.0,1.0))
Modified version of the original formulation, should be numerically more stable ax = [ qu[1]*s, qu[2]*s, qu[3]*s, omega ]
""" else:
if iszero(qu[1]**2+qu[2]**2+qu[3]**2): # set axis to [001] if the angle is 0/360 ax = [ qu[1], qu[2], qu[3], np.pi]
ax = [ 0.0, 0.0, 1.0, 0.0 ]
elif not iszero(qu[0]): return np.array(ax)
s = np.sign(qu[0])/np.sqrt(qu[1]**2+qu[2]**2+qu[3]**2)
omega = 2.0 * np.arccos(np.clip(qu[0],-1.0,1.0)) @staticmethod
ax = [ qu[1]*s, qu[2]*s, qu[3]*s, omega ] def qu2ro(qu):
else: """Quaternion to Rodriques-Frank vector."""
ax = [ qu[1], qu[2], qu[3], np.pi] if iszero(qu[0]):
ro = [qu[1], qu[2], qu[3], np.inf]
return np.array(ax) else:
s = np.linalg.norm([qu[1],qu[2],qu[3]])
ro = [0.0,0.0,P,0.0] if iszero(s) else \
def qu2ro(qu): [ qu[1]/s, qu[2]/s, qu[3]/s, np.tan(np.arccos(np.clip(qu[0],-1.0,1.0)))] # avoid numerical difficulties
"""Quaternion to Rodriques-Frank vector."""
if iszero(qu[0]): return np.array(ro)
ro = [qu[1], qu[2], qu[3], np.inf]
else: @staticmethod
s = np.linalg.norm([qu[1],qu[2],qu[3]]) def qu2ho(qu):
ro = [0.0,0.0,P,0.0] if iszero(s) else \ """Quaternion to homochoric vector."""
[ qu[1]/s, qu[2]/s, qu[3]/s, np.tan(np.arccos(np.clip(qu[0],-1.0,1.0)))] # avoid numerical difficulties omega = 2.0 * np.arccos(np.clip(qu[0],-1.0,1.0)) # avoid numerical difficulties
return np.array(ro) if iszero(omega):
ho = np.array([ 0.0, 0.0, 0.0 ])
else:
def qu2ho(qu): ho = np.array([qu[1], qu[2], qu[3]])
"""Quaternion to homochoric vector.""" f = 0.75 * ( omega - np.sin(omega) )
omega = 2.0 * np.arccos(np.clip(qu[0],-1.0,1.0)) # avoid numerical difficulties ho = ho/np.linalg.norm(ho) * f**(1./3.)
if iszero(omega): return ho
ho = np.array([ 0.0, 0.0, 0.0 ])
else: @staticmethod
ho = np.array([qu[1], qu[2], qu[3]]) def qu2cu(qu):
f = 0.75 * ( omega - np.sin(omega) ) """Quaternion to cubochoric vector."""
ho = ho/np.linalg.norm(ho) * f**(1./3.) return Rotation.ho2cu(Rotation.qu2ho(qu))
return ho
#---------- Rotation matrix ----------
@staticmethod
def qu2cu(qu): def om2qu(om):
"""Quaternion to cubochoric vector.""" """
return ho2cu(qu2ho(qu)) Rotation matrix to quaternion.
The original formulation (direct conversion) had (numerical?) issues
#---------- Rotation matrix ---------- """
return Rotation.eu2qu(Rotation.om2eu(om))
def om2qu(om):
""" @staticmethod
Rotation matrix to quaternion. def om2eu(om):
"""Rotation matrix to Bunge-Euler angles."""
The original formulation (direct conversion) had (numerical?) issues if abs(om[2,2]) < 1.0:
""" zeta = 1.0/np.sqrt(1.0-om[2,2]**2)
return eu2qu(om2eu(om)) eu = np.array([np.arctan2(om[2,0]*zeta,-om[2,1]*zeta),
np.arccos(om[2,2]),
np.arctan2(om[0,2]*zeta, om[1,2]*zeta)])
def om2eu(om): else:
"""Rotation matrix to Bunge-Euler angles.""" eu = np.array([np.arctan2( om[0,1],om[0,0]), np.pi*0.5*(1-om[2,2]),0.0]) # following the paper, not the reference implementation
if abs(om[2,2]) < 1.0:
zeta = 1.0/np.sqrt(1.0-om[2,2]**2) # reduce Euler angles to definition range, i.e a lower limit of 0.0
eu = np.array([np.arctan2(om[2,0]*zeta,-om[2,1]*zeta), eu = np.where(eu<0, (eu+2.0*np.pi)%np.array([2.0*np.pi,np.pi,2.0*np.pi]),eu)
np.arccos(om[2,2]), return eu
np.arctan2(om[0,2]*zeta, om[1,2]*zeta)])
else: @staticmethod
eu = np.array([np.arctan2( om[0,1],om[0,0]), np.pi*0.5*(1-om[2,2]),0.0]) # following the paper, not the reference implementation def om2ax(om):
"""Rotation matrix to axis angle pair."""
# reduce Euler angles to definition range, i.e a lower limit of 0.0 ax=np.empty(4)
eu = np.where(eu<0, (eu+2.0*np.pi)%np.array([2.0*np.pi,np.pi,2.0*np.pi]),eu)
return eu # first get the rotation angle
t = 0.5*(om.trace() -1.0)
ax[3] = np.arccos(np.clip(t,-1.0,1.0))
def om2ax(om):
"""Rotation matrix to axis angle pair.""" if iszero(ax[3]):
ax=np.empty(4) ax = [ 0.0, 0.0, 1.0, 0.0]
else:
# first get the rotation angle w,vr = np.linalg.eig(om)
t = 0.5*(om.trace() -1.0) # next, find the eigenvalue (1,0j)
ax[3] = np.arccos(np.clip(t,-1.0,1.0)) i = np.where(np.isclose(w,1.0+0.0j))[0][0]
ax[0:3] = np.real(vr[0:3,i])
if iszero(ax[3]): diagDelta = np.array([om[1,2]-om[2,1],om[2,0]-om[0,2],om[0,1]-om[1,0]])
ax = [ 0.0, 0.0, 1.0, 0.0] ax[0:3] = np.where(iszero(diagDelta), ax[0:3],np.abs(ax[0:3])*np.sign(-P*diagDelta))
else:
w,vr = np.linalg.eig(om) return np.array(ax)
# next, find the eigenvalue (1,0j)
i = np.where(np.isclose(w,1.0+0.0j))[0][0] @staticmethod
ax[0:3] = np.real(vr[0:3,i]) def om2ro(om):
diagDelta = np.array([om[1,2]-om[2,1],om[2,0]-om[0,2],om[0,1]-om[1,0]]) """Rotation matrix to Rodriques-Frank vector."""
ax[0:3] = np.where(iszero(diagDelta), ax[0:3],np.abs(ax[0:3])*np.sign(-P*diagDelta)) return Rotation.eu2ro(Rotation.om2eu(om))
return np.array(ax) @staticmethod
def om2ho(om):
"""Rotation matrix to homochoric vector."""
def om2ro(om): return Rotation.ax2ho(Rotation.om2ax(om))
"""Rotation matrix to Rodriques-Frank vector."""
return eu2ro(om2eu(om)) @staticmethod
def om2cu(om):
"""Rotation matrix to cubochoric vector."""
def om2ho(om): return Rotation.ho2cu(Rotation.om2ho(om))
"""Rotation matrix to homochoric vector."""
return ax2ho(om2ax(om))
#---------- Bunge-Euler angles ----------
@staticmethod
def om2cu(om): def eu2qu(eu):
"""Rotation matrix to cubochoric vector.""" """Bunge-Euler angles to quaternion."""
return ho2cu(om2ho(om)) ee = 0.5*eu
cPhi = np.cos(ee[1])
sPhi = np.sin(ee[1])
#---------- Bunge-Euler angles ---------- qu = np.array([ cPhi*np.cos(ee[0]+ee[2]),
-P*sPhi*np.cos(ee[0]-ee[2]),
def eu2qu(eu): -P*sPhi*np.sin(ee[0]-ee[2]),
"""Bunge-Euler angles to quaternion.""" -P*cPhi*np.sin(ee[0]+ee[2]) ])
ee = 0.5*eu if qu[0] < 0.0: qu*=-1
cPhi = np.cos(ee[1]) return qu
sPhi = np.sin(ee[1])
qu = np.array([ cPhi*np.cos(ee[0]+ee[2]), @staticmethod
-P*sPhi*np.cos(ee[0]-ee[2]), def eu2om(eu):
-P*sPhi*np.sin(ee[0]-ee[2]), """Bunge-Euler angles to rotation matrix."""
-P*cPhi*np.sin(ee[0]+ee[2]) ]) c = np.cos(eu)
if qu[0] < 0.0: qu*=-1 s = np.sin(eu)
return qu
om = np.array([[+c[0]*c[2]-s[0]*s[2]*c[1], +s[0]*c[2]+c[0]*s[2]*c[1], +s[2]*s[1]],
[-c[0]*s[2]-s[0]*c[2]*c[1], -s[0]*s[2]+c[0]*c[2]*c[1], +c[2]*s[1]],
def eu2om(eu): [+s[0]*s[1], -c[0]*s[1], +c[1] ]])
"""Bunge-Euler angles to rotation matrix."""
c = np.cos(eu) om[np.where(iszero(om))] = 0.0
s = np.sin(eu) return om
om = np.array([[+c[0]*c[2]-s[0]*s[2]*c[1], +s[0]*c[2]+c[0]*s[2]*c[1], +s[2]*s[1]], @staticmethod
[-c[0]*s[2]-s[0]*c[2]*c[1], -s[0]*s[2]+c[0]*c[2]*c[1], +c[2]*s[1]], def eu2ax(eu):
[+s[0]*s[1], -c[0]*s[1], +c[1] ]]) """Bunge-Euler angles to axis angle pair."""
t = np.tan(eu[1]*0.5)
om[np.where(iszero(om))] = 0.0 sigma = 0.5*(eu[0]+eu[2])
return om delta = 0.5*(eu[0]-eu[2])
tau = np.linalg.norm([t,np.sin(sigma)])
alpha = np.pi if iszero(np.cos(sigma)) else \
def eu2ax(eu): 2.0*np.arctan(tau/np.cos(sigma))
"""Bunge-Euler angles to axis angle pair."""
t = np.tan(eu[1]*0.5) if iszero(alpha):
sigma = 0.5*(eu[0]+eu[2]) ax = np.array([ 0.0, 0.0, 1.0, 0.0 ])
delta = 0.5*(eu[0]-eu[2]) else:
tau = np.linalg.norm([t,np.sin(sigma)]) ax = -P/tau * np.array([ t*np.cos(delta), t*np.sin(delta), np.sin(sigma) ]) # passive axis angle pair so a minus sign in front
alpha = np.pi if iszero(np.cos(sigma)) else \ ax = np.append(ax,alpha)
2.0*np.arctan(tau/np.cos(sigma)) if alpha < 0.0: ax *= -1.0 # ensure alpha is positive
if iszero(alpha): return ax
ax = np.array([ 0.0, 0.0, 1.0, 0.0 ])
else: @staticmethod
ax = -P/tau * np.array([ t*np.cos(delta), t*np.sin(delta), np.sin(sigma) ]) # passive axis angle pair so a minus sign in front def eu2ro(eu):
ax = np.append(ax,alpha) """Bunge-Euler angles to Rodriques-Frank vector."""
if alpha < 0.0: ax *= -1.0 # ensure alpha is positive ro = eu2ax(eu) # convert to axis angle pair representation
if ro[3] >= np.pi: # Differs from original implementation. check convention 5
return ax ro[3] = np.inf
elif iszero(ro[3]):
ro = np.array([ 0.0, 0.0, P, 0.0 ])
def eu2ro(eu): else:
"""Bunge-Euler angles to Rodriques-Frank vector.""" ro[3] = np.tan(ro[3]*0.5)
ro = eu2ax(eu) # convert to axis angle pair representation
if ro[3] >= np.pi: # Differs from original implementation. check convention 5 return ro
ro[3] = np.inf
elif iszero(ro[3]): @staticmethod
ro = np.array([ 0.0, 0.0, P, 0.0 ]) def eu2ho(eu):
else: """Bunge-Euler angles to homochoric vector."""
ro[3] = np.tan(ro[3]*0.5) return Rotation.ax2ho(Rotation.eu2ax(eu))
return ro @staticmethod
def eu2cu(eu):
"""Bunge-Euler angles to cubochoric vector."""
def eu2ho(eu): return Rotation.ho2cu(Rotation.eu2ho(eu))
"""Bunge-Euler angles to homochoric vector."""
return ax2ho(eu2ax(eu))
#---------- Axis angle pair ----------
@staticmethod
def eu2cu(eu): def ax2qu(ax):
"""Bunge-Euler angles to cubochoric vector.""" """Axis angle pair to quaternion."""
return ho2cu(eu2ho(eu)) if iszero(ax[3]):
qu = np.array([ 1.0, 0.0, 0.0, 0.0 ])
else:
#---------- Axis angle pair ---------- c = np.cos(ax[3]*0.5)
s = np.sin(ax[3]*0.5)
def ax2qu(ax): qu = np.array([ c, ax[0]*s, ax[1]*s, ax[2]*s ])
"""Axis angle pair to quaternion."""
if iszero(ax[3]): return qu
qu = np.array([ 1.0, 0.0, 0.0, 0.0 ])
else: @staticmethod
c = np.cos(ax[3]*0.5) def ax2om(ax):
s = np.sin(ax[3]*0.5) """Axis angle pair to rotation matrix."""
qu = np.array([ c, ax[0]*s, ax[1]*s, ax[2]*s ]) c = np.cos(ax[3])
s = np.sin(ax[3])
return qu omc = 1.0-c
om=np.diag(ax[0:3]**2*omc + c)
def ax2om(ax): for idx in [[0,1,2],[1,2,0],[2,0,1]]:
"""Axis angle pair to rotation matrix.""" q = omc*ax[idx[0]] * ax[idx[1]]
c = np.cos(ax[3]) om[idx[0],idx[1]] = q + s*ax[idx[2]]
s = np.sin(ax[3]) om[idx[1],idx[0]] = q - s*ax[idx[2]]
omc = 1.0-c
om=np.diag(ax[0:3]**2*omc + c) return om if P < 0.0 else om.T
for idx in [[0,1,2],[1,2,0],[2,0,1]]: @staticmethod
q = omc*ax[idx[0]] * ax[idx[1]] def ax2eu(ax):
om[idx[0],idx[1]] = q + s*ax[idx[2]] """Rotation matrix to Bunge Euler angles."""
om[idx[1],idx[0]] = q - s*ax[idx[2]] return Rotation.om2eu(Rotation.ax2om(ax))
return om if P < 0.0 else om.T @staticmethod
def ax2ro(ax):
"""Axis angle pair to Rodriques-Frank vector."""
def ax2eu(ax): if iszero(ax[3]):
"""Rotation matrix to Bunge Euler angles.""" ro = [ 0.0, 0.0, P, 0.0 ]
return om2eu(ax2om(ax)) else:
ro = [ax[0], ax[1], ax[2]]
# 180 degree case
def ax2ro(ax): ro += [np.inf] if np.isclose(ax[3],np.pi,atol=1.0e-15,rtol=0.0) else \
"""Axis angle pair to Rodriques-Frank vector.""" [np.tan(ax[3]*0.5)]
if iszero(ax[3]):
ro = [ 0.0, 0.0, P, 0.0 ] return np.array(ro)
else:
ro = [ax[0], ax[1], ax[2]] @staticmethod
# 180 degree case def ax2ho(ax):
ro += [np.inf] if np.isclose(ax[3],np.pi,atol=1.0e-15,rtol=0.0) else \ """Axis angle pair to homochoric vector."""
[np.tan(ax[3]*0.5)] f = (0.75 * ( ax[3] - np.sin(ax[3]) ))**(1.0/3.0)
ho = ax[0:3] * f
return np.array(ro) return ho
@staticmethod
def ax2ho(ax): def ax2cu(ax):
"""Axis angle pair to homochoric vector.""" """Axis angle pair to cubochoric vector."""
f = (0.75 * ( ax[3] - np.sin(ax[3]) ))**(1.0/3.0) return Rotation.ho2cu(Rotation.ax2ho(ax))
ho = ax[0:3] * f
return ho
#---------- Rodrigues-Frank vector ----------
@staticmethod
def ax2cu(ax): def ro2qu(ro):
"""Axis angle pair to cubochoric vector.""" """Rodriques-Frank vector to quaternion."""
return ho2cu(ax2ho(ax)) return Rotation.ax2qu(Rotation.ro2ax(ro))
@staticmethod
#---------- Rodrigues-Frank vector ---------- def ro2om(ro):
"""Rodgrigues-Frank vector to rotation matrix."""
def ro2qu(ro): return Rotation.ax2om(Rotation.ro2ax(ro))
"""Rodriques-Frank vector to quaternion."""
return ax2qu(ro2ax(ro)) @staticmethod
def ro2eu(ro):
"""Rodriques-Frank vector to Bunge-Euler angles."""
def ro2om(ro): return Rotation.om2eu(Rotation.ro2om(ro))
"""Rodgrigues-Frank vector to rotation matrix."""
return ax2om(ro2ax(ro)) @staticmethod
def ro2ax(ro):
"""Rodriques-Frank vector to axis angle pair."""
def ro2eu(ro): ta = ro[3]
"""Rodriques-Frank vector to Bunge-Euler angles."""
return om2eu(ro2om(ro)) if iszero(ta):
ax = [ 0.0, 0.0, 1.0, 0.0 ]
elif not np.isfinite(ta):
def ro2ax(ro): ax = [ ro[0], ro[1], ro[2], np.pi ]
"""Rodriques-Frank vector to axis angle pair.""" else:
ta = ro[3] angle = 2.0*np.arctan(ta)
ta = 1.0/np.linalg.norm(ro[0:3])
if iszero(ta): ax = [ ro[0]/ta, ro[1]/ta, ro[2]/ta, angle ]
ax = [ 0.0, 0.0, 1.0, 0.0 ]
elif not np.isfinite(ta): return np.array(ax)
ax = [ ro[0], ro[1], ro[2], np.pi ]
else: @staticmethod
angle = 2.0*np.arctan(ta) def ro2ho(ro):
ta = 1.0/np.linalg.norm(ro[0:3]) """Rodriques-Frank vector to homochoric vector."""
ax = [ ro[0]/ta, ro[1]/ta, ro[2]/ta, angle ] if iszero(np.sum(ro[0:3]**2.0)):
ho = [ 0.0, 0.0, 0.0 ]
return np.array(ax) else:
f = 2.0*np.arctan(ro[3]) -np.sin(2.0*np.arctan(ro[3])) if np.isfinite(ro[3]) else np.pi
ho = ro[0:3] * (0.75*f)**(1.0/3.0)
def ro2ho(ro):
"""Rodriques-Frank vector to homochoric vector.""" return np.array(ho)
if iszero(np.sum(ro[0:3]**2.0)):
ho = [ 0.0, 0.0, 0.0 ] @staticmethod
else: def ro2cu(ro):
f = 2.0*np.arctan(ro[3]) -np.sin(2.0*np.arctan(ro[3])) if np.isfinite(ro[3]) else np.pi """Rodriques-Frank vector to cubochoric vector."""
ho = ro[0:3] * (0.75*f)**(1.0/3.0) return ho2cu(ro2ho(ro))
return np.array(ho)
#---------- Homochoric vector----------
@staticmethod
def ro2cu(ro): def ho2qu(ho):
"""Rodriques-Frank vector to cubochoric vector.""" """Homochoric vector to quaternion."""
return ho2cu(ro2ho(ro)) return Rotation.ax2qu(Rotation.ho2ax(ho))
@staticmethod
#---------- Homochoric vector---------- def ho2om(ho):
"""Homochoric vector to rotation matrix."""
def ho2qu(ho): return Rotation.ax2om(Rotation.ho2ax(ho))
"""Homochoric vector to quaternion."""
return ax2qu(ho2ax(ho)) @staticmethod
def ho2eu(ho):
"""Homochoric vector to Bunge-Euler angles."""
def ho2om(ho): return Rotation.ax2eu(Rotation.ho2ax(ho))
"""Homochoric vector to rotation matrix."""
return ax2om(ho2ax(ho)) @staticmethod
def ho2ax(ho):
"""Homochoric vector to axis angle pair."""
def ho2eu(ho): tfit = np.array([+1.0000000000018852, -0.5000000002194847,
"""Homochoric vector to Bunge-Euler angles.""" -0.024999992127593126, -0.003928701544781374,
return ax2eu(ho2ax(ho)) -0.0008152701535450438, -0.0002009500426119712,
-0.00002397986776071756, -0.00008202868926605841,
+0.00012448715042090092, -0.0001749114214822577,
def ho2ax(ho): +0.0001703481934140054, -0.00012062065004116828,
"""Homochoric vector to axis angle pair.""" +0.000059719705868660826, -0.00001980756723965647,
tfit = np.array([+1.0000000000018852, -0.5000000002194847, +0.000003953714684212874, -0.00000036555001439719544])
-0.024999992127593126, -0.003928701544781374, # normalize h and store the magnitude
-0.0008152701535450438, -0.0002009500426119712, hmag_squared = np.sum(ho**2.)
-0.00002397986776071756, -0.00008202868926605841, if iszero(hmag_squared):
+0.00012448715042090092, -0.0001749114214822577, ax = np.array([ 0.0, 0.0, 1.0, 0.0 ])
+0.0001703481934140054, -0.00012062065004116828, else:
+0.000059719705868660826, -0.00001980756723965647, hm = hmag_squared
+0.000003953714684212874, -0.00000036555001439719544])
# normalize h and store the magnitude # convert the magnitude to the rotation angle
hmag_squared = np.sum(ho**2.) s = tfit[0] + tfit[1] * hmag_squared
if iszero(hmag_squared): for i in range(2,16):
ax = np.array([ 0.0, 0.0, 1.0, 0.0 ]) hm *= hmag_squared
else: s += tfit[i] * hm
hm = hmag_squared ax = np.append(ho/np.sqrt(hmag_squared),2.0*np.arccos(np.clip(s,-1.0,1.0)))
return ax
# convert the magnitude to the rotation angle
s = tfit[0] + tfit[1] * hmag_squared @staticmethod
for i in range(2,16): def ho2ro(ho):
hm *= hmag_squared """Axis angle pair to Rodriques-Frank vector."""
s += tfit[i] * hm return Rotation.ax2ro(Rotation.ho2ax(ho))
ax = np.append(ho/np.sqrt(hmag_squared),2.0*np.arccos(np.clip(s,-1.0,1.0)))
return ax @staticmethod
def ho2cu(ho):
"""Homochoric vector to cubochoric vector."""
def ho2ro(ho): return Lambert.BallToCube(ho)
"""Axis angle pair to Rodriques-Frank vector."""
return ax2ro(ho2ax(ho))
#---------- Cubochoric ----------
@staticmethod
def ho2cu(ho): def cu2qu(cu):
"""Homochoric vector to cubochoric vector.""" """Cubochoric vector to quaternion."""
return Lambert.BallToCube(ho) return Rotation.ho2qu(Rotation.cu2ho(cu))
@staticmethod
#---------- Cubochoric ---------- def cu2om(cu):
"""Cubochoric vector to rotation matrix."""
def cu2qu(cu): return Rotation.ho2om(Rotation.cu2ho(cu))
"""Cubochoric vector to quaternion."""
return ho2qu(cu2ho(cu)) @staticmethod
def cu2eu(cu):
"""Cubochoric vector to Bunge-Euler angles."""
def cu2om(cu): return Rotation.ho2eu(Rotation.cu2ho(cu))
"""Cubochoric vector to rotation matrix."""
return ho2om(cu2ho(cu)) @staticmethod
def cu2ax(cu):
"""Cubochoric vector to axis angle pair."""
def cu2eu(cu): return Rotation.ho2ax(Rotation.cu2ho(cu))
"""Cubochoric vector to Bunge-Euler angles."""
return ho2eu(cu2ho(cu)) @staticmethod
def cu2ro(cu):
"""Cubochoric vector to Rodriques-Frank vector."""
def cu2ax(cu): return Rotation.ho2ro(Rotation.cu2ho(cu))
"""Cubochoric vector to axis angle pair."""
return ho2ax(cu2ho(cu)) @staticmethod
def cu2ho(cu):
"""Cubochoric vector to homochoric vector."""
def cu2ro(cu): return Lambert.CubeToBall(cu)
"""Cubochoric vector to Rodriques-Frank vector."""
return ho2ro(cu2ho(cu))
def cu2ho(cu):
"""Cubochoric vector to homochoric vector."""
return Lambert.CubeToBall(cu)