Skip to content

Commit 664e537

Browse files
Added files
1 parent b38c8cd commit 664e537

File tree

13 files changed

+350
-0
lines changed

13 files changed

+350
-0
lines changed

ax2om.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import numpy as np
2+
import pytest
3+
4+
#This function takes a axis angle pair (radians) as a four element vector and converts it into corresponding rotation matrix
5+
def ax2om(n):
6+
c= np.cos(n[3])
7+
s= np.sin(n[3])
8+
p=-1
9+
A=np.zeros((3,3))
10+
A[0][0]= c + (1-c)*(n[0]**2)
11+
A[0][1]= (1-c)*n[0]*n[1] + s*n[2]
12+
A[0][2]= (1-c)*n[0]*n[2] - s*n[1]
13+
A[1][0]= (1-c)*n[0]*n[1] - s*n[2]
14+
A[1][1]= c + (1-c)*(n[1]**2)
15+
A[1][2]= (1-c)*n[2]*n[1] + s*n[0]
16+
A[2][0]= (1-c)*n[0]*n[2] + s*n[1]
17+
A[2][1]= (1-c)*n[2]*n[1] - s*n[0]
18+
A[2][2]= c + (1-c)*(n[2]**2)
19+
return A
20+
21+
def test_case1():
22+
n=[0,0,1,0]
23+
x= np.eye(3)
24+
y=np.array_equal(ax2om(n),x)
25+
print(ax2om(n))
26+
assert y== True
27+
28+
29+
def test_case2():
30+
n=[-1,0,0,np.pi]
31+
x= np.eye(3)
32+
x[1][1]=-1
33+
x[2][2]=-1
34+
y=np.array_equal(np.around(ax2om(n),1),x)
35+
print(np.around(ax2om(n),2))
36+
assert y== True
37+

ax2qu.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import pytest
2+
import numpy as np
3+
# This function converts a axis angle pair in radians into unit quarternions
4+
5+
def ax2qu(n):
6+
q=[np.cos(n[3]/2),np.sin(n[3]/2)*n[0],np.sin(n[3]/2)*n[1],np.sin(n[3]/2)*n[2]]
7+
return q
8+
9+
10+
def test_case1():
11+
n=[0,0,1,0]
12+
assert ax2qu(n) == [1,0,0,0]
13+
def test_case2():
14+
n =[0,0,-1,0.5*np.pi]
15+
x=np.around(ax2qu(n),7)
16+
y = np.array([0.7071068,0,0,-0.7071068])
17+
z = np.array_equal(x,y)
18+
assert z==True
19+

ax2ro.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import numpy as np
2+
import pytest
3+
4+
#This function takes in axis angle pair in radians and converts it into corresponding rodrigues vector
5+
6+
def ax2ro(n):
7+
if n[3]==np.pi:
8+
print("rodrigues vector is not possible as tan(90)=infinity")
9+
10+
n[3]=np.tan(n[3]/2)
11+
return n
12+
13+
def test_case1():
14+
n=[0,0,1,0.5*np.pi]
15+
x=np.around(ax2ro(n),1)
16+
17+
assert x[3]==1

eu2ax.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import numpy as np
2+
#This function takes in the three euler angles in the bunge convention in degrees and gives the corresponding axis angle pair the output is of
3+
# form (n,alpha) in degrees
4+
def eu2ax(a,b,c):
5+
a= np.pi*a/180
6+
b= np.pi*b/180
7+
c= np.pi*c/180
8+
t=np.tan(b/2)
9+
sigma=(a+c)/2
10+
delta=(a-c)/2
11+
p=-1
12+
tau=np.sqrt((t*t)+(np.sin(sigma)*np.sin(sigma)))
13+
alpha=2*np.arctan(tau/np.cos(sigma))
14+
output = np.zeros((4))
15+
if alpha <= np.pi:
16+
output[3]= alpha*180/np.pi
17+
output[0]= -p*t*np.cos(delta)/tau
18+
output[1]=-p*t*np.sin(delta)/tau
19+
output[2]=-p*t*np.sin(sigma)/tau
20+
else:
21+
output[3]=360- alpha*180/np.pi
22+
output[0]= p*t*np.cos(delta)/tau
23+
output[1]=p*t*np.sin(delta)/tau
24+
output[2]=p*t*np.sin(sigma)/tau
25+
return output
26+
27+
28+
print(eu2ax(360,90,180))

eu2om.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import numpy as np
2+
import scipy
3+
#This function converts given euler angles (Bunge Convention, degrees)into corresponding equivalent rotation matrix
4+
def eu2om(a,b,d):
5+
6+
a= (np.pi)*a/180
7+
b= (np.pi)*b/180
8+
d= (np.pi)*d/180
9+
10+
11+
A=np.zeros((3,3))
12+
c1=np.cos(a)
13+
s1=np.sin(a)
14+
c2=np.cos(d)
15+
s2=np.sin(d)
16+
c=np.cos(b)
17+
s=np.sin(b)
18+
19+
A[0][0]=(c1*c2) - (s1*c*s2)
20+
A[0][1]=(s1*c2) + (c1*c*s2)
21+
A[0][2]=s*s2
22+
A[1][0]=(-c1*s2)-(s1*c*c2)
23+
A[1][1]=(-s1*s2)+ (c1*c*c2)
24+
A[1][2]=s*c2
25+
A[2][0]=s1*s
26+
A[2][1]=-c1*s
27+
A[2][2]=c
28+
return A
29+
30+
print(np.around(eu2om(270,180,0),1))

eu2qu.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import numpy as np
2+
#This function takes 3 euler angles in bunge convention in radians and gives a quartenion
3+
def eu2qu(a1,a2,a3):
4+
c = np.cos(a2/2)
5+
s=np.sin(a2/2)
6+
sigma =(a1+a3)/2
7+
delta = (a1-a3)/2
8+
p=-1
9+
q= [0,0,0,0]
10+
q = [c*np.cos(sigma),-1*p*s*np.cos(delta),-1*p*s*np.sin(delta),-1*p*c*np.sin(sigma)]
11+
if q[0]<0:
12+
q= -1*q
13+
return q
14+
15+
print(eu2qu(0,0.25*np.pi,0))
16+
17+

om2ax.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import math
2+
import pytest
3+
import numpy as np
4+
from numpy import linalg as LA
5+
#This function takes in the rotation matrix and gives axis angle pair in radians
6+
def om2ax(A):
7+
p=-1
8+
w= math.acos(0.5*((A[0][0]+A[1][1]+A[2][2])-1))
9+
if w==0:
10+
f = [0,0,1,0]
11+
else:
12+
values,vectors= LA.eig(A)
13+
if values[0]==1:
14+
vector=vectors[:,0]
15+
elif values[1]==1:
16+
vector=vectors[:,1]
17+
else:
18+
vector= vectors[:,2]
19+
if A[2][1]!=A[1][2]:
20+
vector[0]=sign(p*(A[2][1]-A[1][2]))*abs(vector[0])
21+
if A[0][2]!=A[2][0]:
22+
vector[1]=sign(p*(A[0][2]-A[2][0]))*abs(vector[1])
23+
if A[1][0]!=A[0][1]:
24+
vector[2]=sign(p*(A[1][0]-A[0][1]))*abs(vector[2])
25+
f = [vector[0],vector[1],vector[2],w]
26+
return f
27+
28+
29+
30+
31+
def test_case1():
32+
A=np.eye(3)
33+
assert om2ax(A)==[0,0,1,0]
34+
35+
36+
def test_case2():
37+
A=np.eye(3)
38+
A[1][1]=-1
39+
A[2][2]=-1
40+
assert om2ax(A)==[1,0,0,np.pi]
41+
42+

om2eu.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import pytest
2+
3+
import numpy as np
4+
#This function takes in the rotation matrix and gives euler angles in bunge convention in radians
5+
6+
def om2eu(A):
7+
if abs(A[2][2]) != 1:
8+
xhi= 1/np.sqrt(1-(A[2][2]*A[2][2]))
9+
theta = [np.arctan2(A[2][0]*xhi,A[2][1]*xhi*-1),np.arccos(A[2][2]),np.arctan2(xhi*A[0][2],xhi*A[1][2])]
10+
else:
11+
theta = [np.arctan2(A[0][1],A[0][0]),0.5*np.pi*(1-A[2][2]),0]
12+
return theta
13+
14+
def test_case1():
15+
A= np.eye(3)
16+
assert om2eu(A)== [0,0,0]
17+
18+
def test_case2():
19+
A=np.eye(3)
20+
A[0][0]=-1
21+
A[1][1]=-1
22+
print(A)
23+
assert om2eu(A)==[2*np.pi,0,np.pi]
24+
def test_case3():
25+
A= np.zeros((3,3))
26+
A[0][2]=1
27+
A[1][1]=-1
28+
A[2][0]=1
29+
assert om2eu(A)==[0.5*np.pi,0.5*np.pi,0.5*np.pi]

om2qu.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import pytest
2+
import numpy as np
3+
#This function takes a rotation matrix and gives corresponding quaternion
4+
def om2qu(A):
5+
q0=0.5*np.sqrt(1+A[0][0]+A[1][1]+A[2][2])
6+
q1=0.5*np.sqrt(1+A[0][0]-A[1][1]-A[2][2])
7+
q2=0.5*np.sqrt(1-A[0][0]+A[1][1]-A[2][2])
8+
q3=0.5*np.sqrt(1-A[0][0]-A[1][1]+A[2][2])
9+
if A[2][1]<A[1][2]:
10+
q1=-1*q1
11+
if A[0][2]<A[2][0]:
12+
q2=-1*q2
13+
if A[1][0]<A[0][1]:
14+
q3=-1*q3
15+
16+
v = np.sqrt(q0**2+q1**2+q2**2+q3**2)
17+
q=[q0/v,q1/v,q2/v,q3/v]
18+
return q
19+
20+
def test_case1():
21+
A=np.eye(3)
22+
assert om2qu(A)==[1,0,0,0]
23+
def test_case2():
24+
A=np.eye(3)
25+
A[0][0]=-1
26+
A[1][1]=-1
27+
assert om2qu(A)==[0,0,0,1]
28+

qu2ax.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import math
2+
import numpy as np
3+
import pytest
4+
#This function converts quarternion into axis angle pair in radians
5+
def qu2ax(q):
6+
w=2*math.acos(q[0])
7+
if w==0:
8+
n=[0,0,1,0]
9+
elif q[0]==0:
10+
n=[q[1],q[2],q[3],np.pi]
11+
elif q[0]>0:
12+
s = 1/np.sqrt(q[1]**2+q[2]**2+q[3]**2)
13+
n = [s*q[1],s*q[2],s*q[3],w]
14+
else:
15+
s = -1/np.sqrt(q[1]**2+q[2]**2+q[3]**2)
16+
n = [s*q[1],s*q[2],s*q[3],w]
17+
return n
18+
def test_case1():
19+
x = qu2ax([1,0,0,0])
20+
y = [0,0,1,0]
21+
i = np.array_equal(x,y)
22+
assert i == True
23+
def test_case2():
24+
x = qu2ax([0,0,0,1])
25+
y = [0,0,1,np.pi]
26+
i = np.array_equal(x,y)
27+
assert i == True

0 commit comments

Comments
 (0)