This repository has been archived by the owner on Oct 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
294 lines (244 loc) · 10.9 KB
/
utils.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import numpy as np
import math
import math as m
class Vector2 :
def __init__(self,list2) -> None:
self.vec = np.array(list2)
def prod(self,x : float):
self.vec[0] = self.vec[0] * x
self.vec[1] =self.vec[1] * x
return self
def add(self,v3):
v3 = v3.vec
self.vec[0] += v3[0]
self.vec[1] += v3[1]
return self
def __mul__(self, other):
if isinstance(other, Vector2):
return Vector2([self.vec[0] * other.vec[0], self.vec[1] * other.vec[1]])
else:
return Vector2([self.vec[0] * other, self.vec[1] * other])
def __add__(self, other):
if isinstance(other, Vector2):
return Vector2([self.vec[0] + other.vec[0], self.vec[1] + other.vec[1]])
else:
return Vector2([self.vec[0] + other, self.vec[1] + other])
def get_np(self):
return np.array(self.vec)
##########################################################################
def get_list(self):
return [self.vec[0],self.vec[1]]
def get_prod(self,v3 ):
v3 = v3.vec
product = self.vec[0] * v3[0]
product +=self.vec[1] * v3[1]
return product
def get_module(self,non_zero =False):
mo = self.vec[0]* self.vec[0]
mo += self.vec[1] * self.vec[1]
if (non_zero == True and mo ==0 ) : mo = 0.0001
return math.sqrt(mo)
def get_angle (self,v3):
ang = (self.get_prod(v3))*(1/(self.get_module(non_zero=True) * v3.get_module(non_zero = True)))
if ang >1 :ang =1
if ang <-1 : ang =-1
return math.acos(ang)
def get_Vector3(self,z=0):
lis = self.vec.tolist()
return Vector3([lis[0],lis[1],z])
class Vector3 :
def __init__(self,list3) -> None:
self.vec = np.array(list3)
def rotate_xyz_self(self,ax,ay,az):
rotate_matrix = np.array([[1,0,0],[0,math.cos(ax),-math.sin(ax)], [0,math.sin(ax),math.cos(ax)]])
rotate_matrix = rotate_matrix @ np.array([[math.cos(ay),0,math.sin(ay)],[0,1,0],[-math.sin(ay),0,math.cos(ay)]])
rotate_matrix = rotate_matrix @ np.array([[math.cos(az),-math.sin(az),0],[math.sin(az),math.cos(az),0],[0,0,1]])
self.vec = rotate_matrix @ self.vec
return self
def rotate_zyx_self(self,ax,ay,az) :
mz = np.array([[math.cos(az),-math.sin(az),0],[math.sin(az),math.cos(az),0],[0,0,1]])
my = np.array([[math.cos(ay),0,math.sin(ay)],[0,1,0],[-math.sin(ay),0,math.cos(ay)]])
mx = np.array([[1,0,0],[0,math.cos(ax),-math.sin(ax)], [0,math.sin(ax),math.cos(ax)]])
self.vec = np.dot(mz,np.dot(my,mx)) @ self.vec
return self
def rev_rotate_zyx_self(self,ax,ay,az):
mxt = np.array([[1, 0, 0],
[0,math.cos(ax),math.sin(ax)],
[0,-math.sin(ax),math.cos(ax)]])
myt = np.array([[math.cos(ay),0,-math.sin(ay)],
[0, 1, 0],
[math.sin(ay),0,math.cos(ay)]])
mzt = np.array([[math.cos(az),math.sin(az),0],
[-math.sin(az),math.cos(az),0],
[0, 0, 1]])
self.vec = np.dot(mxt,np.dot(myt,mzt)) @ self.vec
return self
def rotate_xyz_fix(self,ax,ay,az):
rotate_matrix = np.array([[math.cos(az),-math.sin(az),0],
[math.sin(az),math.cos(az),0],
[0,0,1]])
rotate_matrix = rotate_matrix @ np.array([[math.cos(ay),0,math.sin(ay)],
[0,1,0],
[-math.sin(ay),0,math.cos(ay)]])
rotate_matrix = rotate_matrix @ np.array([[1,0,0],
[0,math.cos(ax),-math.sin(ax)],
[0,math.sin(ax),math.cos(ax)]])
self.vec = rotate_matrix @ self.vec
return self
def rev_rotate_xyz_fix(self,ax,ay,az):
rotate_matrix = np.array([[1,0,0],
[0,math.cos(ax),math.sin(ax)],
[0,-math.sin(ax),math.cos(ax)]])
rotate_matrix = rotate_matrix @ np.array([[math.cos(ay),0,-math.sin(ay)],
[0 ,1, 0],
[math.sin(ay),0,math.cos(ay)]])
rotate_matrix = rotate_matrix @ np.array([[math.cos(az),math.sin(az),0],
[-math.sin(az),math.cos(az),0],
[0, 0, 1]])
self.vec = rotate_matrix @ self.vec
return self
def prod(self,x : float):
self.vec[0] = self.vec[0] * x
self.vec[1] =self.vec[1] * x
self.vec[2] = self.vec[2] *x
return self
def add(self,v3 ):
v3 = v3.vec
self.vec[0] += v3[0]
self.vec[1] += v3[1]
self.vec[2] += v3[2]
return self
##########################################################################
def get_list(self):
return [self.vec[0],self.vec[1],self.vec[2]]
def get_prod(self,v3 ):
v3 = v3.vec
product = self.vec[0] * v3[0]
product +=self.vec[1] * v3[1]
product += self.vec[2] * v3[2]
return product
def get_module(self,non_zero =False):
mo = self.vec[0]* self.vec[0]
mo += self.vec[1] * self.vec[1]
mo += self.vec[2] * self.vec[2]
if (non_zero == True and mo ==0 ) : mo = 0.0001
return math.sqrt(mo)
def get_angle (self,v3,pid_set_zero=-1):
if (pid_set_zero == -1 ):
ang = (self.get_prod(v3))*(1/(self.get_module(non_zero=True) * v3.get_module(non_zero = True)))
if ang >1 :ang =1
if ang <-1 : ang =-1
return math.acos(ang)
temp = Vector3(v3.get_list())
sig = 1
if (pid_set_zero == 0):
temp.vec[0] = 0
ang = math.acos((self.get_prod(temp))*(1/(self.get_module(non_zero=True) * temp.get_module(non_zero= True))))
if (temp.vec[1]<0) : sig = -1
# if (temp.vec[2]<0) : ang = math.pi - ang
return sig * ang
if ( pid_set_zero == 1):
temp.vec[1] = 0
ang = math.acos((self.get_prod(temp))*(1/(self.get_module(non_zero=True) * temp.get_module(non_zero=True))))
if (temp.vec[2]<0) : sig = -1
# if (temp.vec[0]<0) : ang = math.pi - ang
return sig * ang
# print("pid_set_zero ERROR, return None.")
return None
def get_Vector2(self):
lis = self.vec.tolist()
return Vector2([lis[0],lis[1]])
class Quaternion4 :
def __init__(self,xyz) :
# temp=[wxyz[1],wxyz[2],wxyz[3]]
temp = xyz
self.vec = np.array(temp)
self.q = np.array([0,0,0,0])
self.q = np.array(self.set_euler(xyz))
def set_euler (self,xyz):
a = xyz[0]
b = xyz[1]
c = xyz[2]
q0 = m.cos (b/2) * m.cos(c/2) * m.cos(a/2) + m.cos(a/2) * m.cos(b/2)* m.cos(c/2)
q1 = m.sin(a/2)*m.cos(b/2) *m.cos(c/2) +m.sin(b/2) * m.cos(a/2) * m.sin(c/2)
q2 = m.sin(b/2)*m.cos(a/2) *m.cos(c/2) - m.cos(b/2)*m.sin(a/2) * m.sin(c/2)
q3 = m.sin(c/2)*m.cos(b/2) * m.cos(a/2) - m.sin(b/2) * m.sin(a/2) * m.cos(c/2)
self.q[0] = q0
self.q[1] = q1
self.q[2] = q2
self.q[3] = q3
def get_euler(self):
pass
def rotate (self,a,b,c):
q0 = m.cos (b/2) * m.cos(c/2) * m.cos(a/2) + m.cos(a/2) * m.cos(b/2)* m.cos(c/2)
q1 = m.sin(a/2)*m.cos(b/2) *m.cos(c/2) +m.sin(b/2) * m.cos(a/2) * m.sin(c/2)
q2 = m.sin(b/2)*m.cos(a/2) *m.cos(c/2) - m.cos(b/2)*m.sin(a/2) * m.sin(c/2)
q3 = m.sin(c/2)*m.cos(b/2) * m.cos(a/2) - m.sin(b/2) * m.sin(a/2) * m.cos(c/2)
ro = np.array([[q0*q0 + q1*q1 -q2*q2 -q3*q3, 2*(q1*q2 - q0*q3) , 2*(q1*q3 + q0*q2)],
[2*(q1*q2+q0*q3),q0*q0 - q1*q1 + q2*q2 -q3*q3,2*(q2*q3-q0*q1)],
[2*(q1*q3 - q0*q2), 2*(q2*q3 + q0*q1) ,q0*q0-q1*q1 -q2*q2 -q3*q3 ]])
self.vec = ro @ self.vec
return self
def rev_rotate (self,x,y,z):
x = -x
y = -y
z = -z
a = y
b = x
c = z
q0 = m.cos (b/2) * m.cos(c/2) * m.cos(a/2) + m.cos(a/2) * m.cos(b/2)* m.cos(c/2)
q1 = m.sin(a/2)*m.cos(b/2) *m.cos(c/2) +m.sin(b/2) * m.cos(a/2) * m.sin(c/2)
q2 = m.sin(b/2)*m.cos(a/2) *m.cos(c/2) - m.cos(b/2)*m.sin(a/2) * m.sin(c/2)
q3 = m.sin(c/2)*m.cos(b/2) * m.cos(a/2) - m.sin(b/2) * m.sin(a/2) * m.cos(c/2)
ro = np.array([[q0*q0 + q1*q1 -q2*q2 -q3*q3, 2*(q1*q2 - q0*q3) , 2*(q1*q3 + q0*q2)],
[2*(q1*q2+q0*q3),q0*q0 - q1*q1 + q2*q2 -q3*q3,2*(q2*q3-q0*q1)],
[2*(q1*q3 - q0*q2), 2*(q2*q3 + q0*q1) ,q0*q0-q1*q1 -q2*q2 -q3*q3 ]])
self.vec = ro @ self.vec
return self
def prod(self,x : float):
self.vec[0] = self.vec[0] * x
self.vec[1] =self.vec[1] * x
self.vec[2] = self.vec[2] *x
return self
def add(self,v3 ):
v3 = v3.vec
self.vec[0] += v3[0]
self.vec[1] += v3[1]
self.vec[2] += v3[2]
return self
##########################################################################
def get_list(self):
return [self.vec[0],self.vec[1],self.vec[2]]
def get_prod(self,v3 ):
v3 = v3.vec
product = self.vec[0] * v3[0]
product +=self.vec[1] * v3[1]
product += self.vec[2] * v3[2]
return product
def get_module(self):
mo = self.vec[0]* self.vec[0]
mo += self.vec[1] * self.vec[1]
mo += self.vec[2] * self.vec[2]
return math.sqrt(mo)
def get_angle (self,v3,pid_set_zero=-1):
if (pid_set_zero == -1 ):
ang = (self.get_prod(v3))*(1/(self.get_module() * v3.get_module()))
if ang >1 :ang =1
if ang <-1 : ang =-1
return math.acos(ang)
temp = Quaternion4(v3.get_list())
sig = 1
if (pid_set_zero == 0):
temp.vec[0] = 0
ang = math.acos((self.get_prod(temp))*(1/(self.get_module() * temp.get_module())))
if (temp.vec[1]<0) : sig = -1
# if (temp.vec[2]<0) : ang = math.pi - ang
return sig * ang
if ( pid_set_zero == 1):
temp.vec[1] = 0
ang = math.acos((self.get_prod(temp))*(1/(self.get_module() * temp.get_module())))
if (temp.vec[2]<0) : sig = -1
# if (temp.vec[0]<0) : ang = math.pi - ang
return sig * ang
# print("pid_set_zero ERROR, return None.")
return None