-
Notifications
You must be signed in to change notification settings - Fork 2
/
goals.py
85 lines (70 loc) · 2.35 KB
/
goals.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
import numpy as np
class Goal:
def __init__(self, pos, **kwds):
super().__init__(**kwds)
self.pos = np.array(pos)
def __repr__(self):
return "Goal Type: " + self.goalType() + "\n" + \
"Position: " + str(self.pos) + "\n"
def __str__(self):
return self.__repr__()
@staticmethod
def goalType():
return "NoForce"
class ConstGoal(Goal):
def __init__(self, force, **kwds):
super().__init__(**kwds)
self.force = force
@staticmethod
def goalType():
return "ConstForce"
def calcForceToPed(self, ped):
disp = ped.pos - self.pos
forceDir = -disp / np.sqrt(np.dot(disp, disp))
force = forceDir * self.force
return force
class DistanceGoal(Goal):
def __init__(self, distCoeff = 1.0, **kwds):
super().__init__(**kwds)
self.distCoeff = distCoeff
@staticmethod
def goalType():
return "DistForce"
def calcForceToPed(self, ped):
disp = self.pos - ped.pos
force = disp * self.distCoeff
return force
class TimeToGoal(Goal):
def __init__(self, distCoeff = 1.0, timeCoeff = 0.1, **kwds):
self.distCoeff = distCoeff
self.timeCoeff = timeCoeff
super().__init__(**kwds)
@staticmethod
def goalType():
return "TimeForce"
def calcTimeToMinDist(self, ped):
dv = ped.vel
dp = self.pos - ped.pos
dvMag = np.sqrt(np.dot(dv, dv))
if dvMag == 0:
minDist = np.sqrt(np.dot(dp, dp))
# The pedestrian is already at the minimum distance,
# so it doesn't make sense to have a force associated
# with the time to reach it
return (minDist, 0)
dvDir = dv / dvMag
ptMinDist = np.dot(dvDir, dp)
minDistTime = ptMinDist / dvMag
minDistVec = dp - ptMinDist * dvDir
minDist = np.sqrt(np.dot(minDistVec, minDistVec))
return (minDist, minDistTime)
def calcForceToPed(self, ped):
minDist, minDistTime = self.calcTimeToMinDist(ped)
dp = self.pos - ped.pos
dpDir = dp / np.sqrt(np.dot(dp, dp))
if minDistTime < 0:
minDistTime *= -2
f = self.distCoeff * minDist + \
self.timeCoeff * minDistTime
force = dpDir * f
return force