-
Notifications
You must be signed in to change notification settings - Fork 0
/
warpforce.py
197 lines (157 loc) · 7.54 KB
/
warpforce.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
'''A class to manage the warp-based version of crowd simulation
'''
import numpy as np
import warp as wp
import forces as crowd_force
import pam as pam_force
class WarpCrowd():
def __init__(self, up_axis='y', device='cuda'):
self.device = device
self.up_axis = up_axis
# generate n number of agents
self.nagents = None
# set radius
self.radius = 0.5
self.radius_min = 0.4
self.radius_max = .65
self.hash_radius = 0.5 # Radius to use for hashgrid
# set mass
self.mass = 80
# set pereption radius
self.perception_radius = 6
self.dt = 1.0/30.0
self.threshold = 0.5 # Distance from goal to switch to new one
self.goal = [0.0,0.0,0.0]
self.up_vec = wp.vec3(0.0,0.0,0.0)
self.inv_up_vector = wp.vec3(1.0,1.0,1.0)
self.forward_vec = wp.vec3(1.0,0.0,0.0)
self.up_vec[1] = 1.0 # y-up
self.inv_up_vector[1] = 0.0 # y-up
def demo_agents(self, s=1.1, m=50, n=50):
# Initialize agents in a grid for testing
self.agents_pos = np.asarray([np.array([(s/2) + (x * s), self.radius_max/2, (s/2) + (y * s)], dtype=np.double)
for x in range(m)
for y in range(n)])
self.nagents = len(self.agents_pos)
self.configure_params()
def configure_params(self):
'''Convert all parameters to warp
Should be called only after number of agents have been defined
'''
self.agents_hdir = np.asarray([np.array([0,0,0,1], dtype=float) for x in range(self.nagents)])
self.agents_vel = np.asarray([np.array([0,0,0]) for x in range(self.nagents)])
self.agents_radi = np.random.uniform(self.radius_min, self.radius_max, self.nagents)
self.agents_mass = [self.mass for x in range(self.nagents)]
self.agents_percept = np.asarray([self.perception_radius for x in range(self.nagents)])
self.agents_goal = np.asarray([np.array(self.goal, dtype=float) for x in range(self.nagents)])
self.goal_idx = np.zeros(self.nagents, dtype=int)
self.xnew = np.zeros_like(self.agents_pos)
self.vnew = np.zeros_like(self.agents_vel)
self.agents_hdir_wp = wp.array(self.agents_hdir, device=self.device, dtype=wp.vec4)
self.agent_force_wp = wp.zeros(shape=self.nagents,device=self.device, dtype=wp.vec3)
self.agents_pos_wp = wp.array(self.agents_pos, device=self.device, dtype=wp.vec3)
self.agents_vel_wp = wp.array(self.agents_vel, device=self.device, dtype=wp.vec3)
self.agents_goal_wp = wp.array(self.agents_goal, device=self.device, dtype=wp.vec3)
self.agents_radi_wp = wp.array(self.agents_radi, device=self.device, dtype=float)
self.agents_mass_wp = wp.array(self.agents_mass, device=self.device, dtype=float)
self.agents_percept_wp = wp.array(self.agents_percept, device=self.device, dtype=float)
self.xnew_wp = wp.zeros_like(wp.array(self.xnew, device=self.device, dtype=wp.vec3))
self.vnew_wp = wp.zeros_like(wp.array(self.vnew, device=self.device, dtype=wp.vec3))
self.hdir_wp = wp.zeros_like(wp.array(self.agents_hdir, device=self.device, dtype=wp.vec4))
def config_hashgrid(self, nagents=None):
'''Create a hash grid based on the number of agents
Currently assumes z up
Parameters
----------
nagents : int, optional
_description_, by default None
'''
if nagents is None: nagents = self.nagents
grid = int(np.sqrt(nagents))
self.grid = wp.HashGrid(dim_x=grid, dim_y=1, dim_z=grid, device=self.device)
def config_mesh(self, points, faces):
'''Create a warp mesh object from points and faces
Parameters
----------
points : List[[x,y,z]]
A list of floating point xyz vertices of a mesh
faces : List[int]
A list of integers corresponding to vertices. Must be triangle-based
'''
# Init mesh for environment collision
self.mesh = wp.Mesh(points=wp.array(points, dtype=wp.vec3, device=self.device),
indices=wp.array(faces, dtype=int ,device=self.device)
)
points = self.mesh.points.numpy()
faces = self.mesh.indices.numpy()
return points, faces
def update_goals(self, goal):
if len(goal) == 1:
self.agents_goal = np.asarray([np.array(goal[0], dtype=float) for x in range(self.nagents)])
else:
self.agents_goal = goal
self.agents_goal_wp = wp.array(self.agents_goal, device=self.device, dtype=wp.vec3)
def compute_step(self):
# Rebuild hashgrid given new positions
self.grid.build(points=self.agents_pos_wp, radius=self.hash_radius)
# self.model_sf()
self.model_pam()
def model_sf(self):
# launch kernel
wp.launch(kernel=crowd_force.get_forces,
dim=self.nagents,
inputs=[self.agents_pos_wp, self.agents_vel_wp, self.agents_goal_wp, self.agents_radi_wp,
self.agents_mass_wp, self.dt, self.agents_percept_wp, self.grid.id, self.mesh.id,
self.inv_up_vector],
outputs=[self.agent_force_wp],
device=self.device
)
# Given the forces, integrate for pos and vel
wp.launch(kernel=crowd_force.integrate,
dim=self.nagents,
inputs=[self.agents_pos_wp, self.agents_vel_wp, self.agent_force_wp, self.dt],
outputs=[self.xnew_wp, self.vnew_wp],
device=self.device
)
self.agents_pos_wp = self.xnew_wp
self.agents_vel_wp = self.vnew_wp
wp.launch(kernel=crowd_force.heading,
dim=self.nagents,
inputs=[self.agents_vel_wp, self.up_vec, self.forward_vec],
outputs=[self.hdir_wp],
device=self.device
)
self.agents_hdir_wp = self.hdir_wp
# See if agent is ready for the next goal
wp.launch(kernel=crowd_force.goal_distance,
dim=self.nagents,
inputs=[self.agents_goal, self.agents_pos_wp, self.threshold],
outputs=[self.goal_idx],
device=self.device
)
def model_pam(self):
# launch kernel
wp.launch(kernel=pam_force.get_forces,
dim=self.nagents,
inputs=[self.agents_pos_wp, self.agents_vel_wp, self.agents_goal_wp, self.agents_radi_wp,
self.agents_mass_wp, self.dt, self.agents_percept_wp, self.grid.id, self.mesh.id,
self.inv_up_vector],
outputs=[self.agent_force_wp],
device=self.device
)
# Given the forces, integrate for pos and vel
wp.launch(kernel=crowd_force.integrate,
dim=self.nagents,
inputs=[self.agents_pos_wp, self.agents_vel_wp, self.agent_force_wp, self.dt],
outputs=[self.xnew_wp, self.vnew_wp],
device=self.device
)
self.agents_pos_wp = self.xnew_wp
self.agents_vel_wp = self.vnew_wp
wp.launch(kernel=crowd_force.heading,
dim=self.nagents,
inputs=[self.agents_vel_wp, self.up_vec, self.forward_vec],
outputs=[self.hdir_wp],
device=self.device
)
self.agents_hdir_wp = self.hdir_wp