-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtool_func.py
208 lines (173 loc) · 5.17 KB
/
tool_func.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import glv
from math import *
import numpy as np
#返回nums中第一个>=target的值位置,如果nums中都比target小,则返回len(nums)
def lower_bound(nums, target):
low, high = 0, len(nums)-1
pos = len(nums)-1
while low<high:
mid = int((low+high)/2)
if nums[mid] < target:
low = mid+1
else:#>=
high = mid
#pos = high
if nums[low]>=target:
pos = low
return pos
def blh2xyz(B,L,H):
sB = sin(B)
cB = cos(B)
sL = sin(L)
cL = cos(L)
N = glv.a/sqrt(1-glv.e2*sB**2)
X = (N+H)*cB*cL
Y = (N+H)*cB*sL
Z = (N*(1-glv.e2)+H)*sB
return np.array([X,Y,Z])
def xyz2blh(X,Y,Z):
bell = glv.a*(1.0-1.0/glv.f)
ss = sqrt(X*X+Y*Y)
zps = Z/ss
theta = atan( (Z*glv.a) / (ss*glv.b) )
sin3 = sin(theta) * sin(theta) * sin(theta)
cos3 = cos(theta) * cos(theta) * cos(theta)
#Closed formula
B = atan((Z + glv.ep2 * glv.b * sin3) / (ss - glv.e2 * glv.a * cos3))
L = atan2(Y,X)
nn = glv.a/sqrt(1.0-glv.e2*sin(B)*sin(L))
H = ss/cos(B) - nn
i=0
while i<=100:
nn = glv.a/sqrt(1.0-glv.e2*sin(B)*sin(B))
hOld = H
phiOld = B
H = ss/cos(B)-nn
B = atan(zps/(1.0-glv.e2*nn/(nn+H)))
if abs(phiOld-B) <= 1.0e-11 and abs(hOld-H) <= 1.0e-5:
# always convert longitude to 0-360
if L < 0.0 :
L += 2 * pi
break
i+=1
return np.array([B,L,H])
def xyz2blh_batch(XX,YY,ZZ):
BB,LL,HH=[],[],[]
for i in range(len(XX)):
X,Y,Z=XX[i],YY[i],ZZ[i]
bell = glv.a*(1.0-1.0/glv.f)
ss = sqrt(X*X+Y*Y)
zps = Z/ss
theta = atan( (Z*glv.a) / (ss*glv.b) )
sin3 = sin(theta) * sin(theta) * sin(theta)
cos3 = cos(theta) * cos(theta) * cos(theta)
#Closed formula
B = atan((Z + glv.ep2 * glv.b * sin3) / (ss - glv.e2 * glv.a * cos3))
L = atan2(Y,X)
nn = glv.a/sqrt(1.0-glv.e2*sin(B)*sin(L))
H = ss/cos(B) - nn
i=0
while i<=100:
nn = glv.a/sqrt(1.0-glv.e2*sin(B)*sin(B))
hOld = H
phiOld = B
H = ss/cos(B)-nn
B = atan(zps/(1.0-glv.e2*nn/(nn+H)))
if abs(phiOld-B) <= 1.0e-11 and abs(hOld-H) <= 1.0e-5:
# always convert longitude to 0-360
if L < 0.0 :
L += 2 * pi
break
i+=1
BB.append(B)
LL.append(L)
HH.append(H)
return np.array([BB,LL,HH]).T
def xyz2enu(XYZ=[],XYZ_Ref=[]):
[b,l,h]=xyz2blh(XYZ_Ref[0],XYZ_Ref[1],XYZ_Ref[2])
r=[XYZ[0]-XYZ_Ref[0], XYZ[1]-XYZ_Ref[1], XYZ[2]-XYZ_Ref[2]]
sinPhi = sin(b)
cosPhi = cos(b)
sinLam = sin(l)
cosLam = cos(l)
N = -sinPhi * cosLam * r[0] - sinPhi * sinLam * r[1] + cosPhi * r[2]
E = -sinLam * r[0] + cosLam * r[1]
U = +cosPhi * cosLam * r[0] + cosPhi * sinLam * r[1] + sinPhi * r[2]
return np.array([E,N,U])
def vxyz2enu(VXYZ=[],VXYZ_Ref=[],XYZ_Ref=[]):
[b,l,h]=xyz2blh(XYZ_Ref[0],XYZ_Ref[1],XYZ_Ref[2])
v=[VXYZ[0]-VXYZ_Ref[0], VXYZ[1]-VXYZ_Ref[1], VXYZ[2]-VXYZ_Ref[2]]
sinPhi = sin(b)
cosPhi = cos(b)
sinLam = sin(l)
cosLam = cos(l)
N = -sinPhi * cosLam * v[0] - sinPhi * sinLam * v[1] + cosPhi * v[2]
E = -sinLam * v[0] + cosLam * v[1]
U = +cosPhi * cosLam * v[0] + cosPhi * sinLam * v[1] + sinPhi * v[2]
return np.array([E,N,U])
def Cne(B,L):
res=np.mat(np.zeros((3,3)))
slat = np.sin(B)
clat = np.cos(B)
slon = np.sin(L)
clon = np.cos(L)
res = np.matrix([[ -slon,clon,0 ],
[ -slat*clon, -slat*slon, clat],
[ clat*clon, clat*slon, slat]])
return res
def Cnb(pitch,roll,yaw):
sp = np.sin(pitch)
cp = np.cos(pitch)
sr = np.sin(roll)
cr = np.cos(roll)
sy = np.sin(yaw)
cy = np.cos(yaw)
res=np.matrix([[cy*cr - sy*sp*sr, -sy*cp, cy*sr + sy*sp*cr],
[sy*cr + cy*sp*sr, cy*cp, sy*sr - cy*sp*cr],
[-cp*sr, sp, cp*cr]])
return res
def diff_enu(t1=[],xyz=[],t2=[],xyz_ref=[]):
t,E,N,U=[],[],[],[]
for i in range(len(t1)):
index=lower_bound(t2,t1[i])
if abs(t1[i]-t2[index])>1e-2:
continue
t.append(t1[i])
[e,n,u]=xyz2enu(xyz[i],xyz_ref[index])
E.append(e)
N.append(n)
U.append(u)
# time=np.array(t).T
diff=np.array([t,E,N,U]).T
return diff
def diff_vel(t1=[],vel=[],t2=[],vel_ref=[],xyz_ref=[]):
t,E,N,U=[],[],[],[]
for i in range(len(t1)):
index=lower_bound(t2,t1[i])
if abs(t1[i]-t2[index])>1e-6:
continue
t.append(t1[i])
[e,n,u]=vxyz2enu(vel[i],vel_ref[index],xyz_ref[index])
# [e,n,u]=vel[i]-vel_ref[index]
E.append(e)
N.append(n)
U.append(u)
# time=np.array(t).T
diff=np.array([t,E,N,U]).T
return diff
def diff_att(t1=[],att=[],t2=[],att_ref=[]):
t,p,r,y=[],[],[],[]
for i in range(len(t1)):
index=lower_bound(t2,t1[i])
if abs(t1[i]-t2[index])>1e-6:
continue
t.append(t1[i])
[pp,rr,yy]=att[i]-att_ref[index]
if abs(yy) > 100:
yy=0
p.append(pp)
r.append(rr)
y.append(yy)
# time=np.array(t).T
diff=np.array([t,p,r,y]).T
return diff