-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath_utils.py
123 lines (90 loc) · 2.49 KB
/
math_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
import math
import numpy as np
'''
一些用到的数学函数
'''
class math_utils:
def __init__(self):
pass
'''
填充向量
'''
def Fillvect(self, x, y, z):
VectToFill = np.zeros(3)
VectToFill[0] = x
VectToFill[1] = y
VectToFill[2] = z
return VectToFill
'''
平方和开根号
'''
def VectAbs(self, InputVector):
return math.sqrt(InputVector[0]**2+InputVector[1]**2+InputVector[2]**2)
'''
两个向量的差
'''
def VectDifference(self, VectA, VectB):
ABDiff = np.zeros(3)
ABDiff = self.Fillvect(VectA[0]-VectB[0], VectA[1]-VectB[1], VectA[2]-VectB[2])
return ABDiff
'''
两个向量的和
'''
def VectSum(self, VectA, VectB):
ABSum = np.zeros(3)
ABSum = self.Fillvect(VectA[0]+VectB[0], VectA[1]+VectB[1], VectA[2]+VectB[2])
return ABSum
'''
单位化某个向量
'''
def UnitVect(self, InputVector):
OutputVector = np.zeros(3)
Abs = self.VectAbs(InputVector)
if Abs < 0.000000000001:
OutputVector = self.NullVect(3)
else:
for i in range(3):
OutputVector[i] = InputVector[i] / float(Abs)
return OutputVector
'''
将某个向量所有值置零,具体实现方法可以再商榷
'''
def NullVect(self, Dim):
VectorToNull = np.zeros(Dim)
for i in range(Dim):
VectorToNull[i] = 0
return VectorToNull
'''
向量乘一个数
'''
def MultiplicateWithScalar(self, VectorToMultiplicate, Scalar, Dim):
OutputVector = np.zeros(3)
for i in range(Dim):
OutputVector[i] = Scalar * VectorToMultiplicate[i]
return OutputVector
'''
对应文章中的斥力部分的计算公式
v_rep = p_rep*(r_0_rep-r_ij)
'''
def SigmoidLin(self, x, p, v_max, r0):
vel = (r0-x) * p
if (p <= 0 or vel <= 0):
return 0
if (vel >= v_max):
return v_max
return vel
'''
引力刹车曲线
'''
def VelDecayLinSqrt(self,x, p, acc, v_max, r0):
vel = (x - r0) * p
if (acc <= 0) or (p <= 0) or (vel <= 0):
return 0
if vel < acc/p:
if vel >= v_max:
return v_max
return vel
vel = math.sqrt(2 * acc * (x-r0) - acc * acc / p / p)
if vel >= v_max:
return v_max
return vel