-
Notifications
You must be signed in to change notification settings - Fork 7
/
viewer.cpp
260 lines (210 loc) · 6.91 KB
/
viewer.cpp
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
#include <madrona/viz/viewer.hpp>
#include <madrona/render/render_mgr.hpp>
#include <madrona/window.hpp>
#include "sim.hpp"
#include "mgr.hpp"
#include "types.hpp"
#include <filesystem>
#include <fstream>
using namespace madrona;
using namespace madrona::viz;
static HeapArray<int32_t> readReplayLog(const char *path)
{
std::ifstream replay_log(path, std::ios::binary);
replay_log.seekg(0, std::ios::end);
int64_t size = replay_log.tellg();
replay_log.seekg(0, std::ios::beg);
HeapArray<int32_t> log(size / sizeof(int32_t));
replay_log.read((char *)log.data(), (size / sizeof(int32_t)) * sizeof(int32_t));
return log;
}
int main(int argc, char *argv[])
{
using namespace madEscape;
constexpr int64_t num_views = 2;
// Read command line arguments
uint32_t num_worlds = 1;
if (argc >= 2) {
num_worlds = (uint32_t)atoi(argv[1]);
}
ExecMode exec_mode = ExecMode::CPU;
if (argc >= 3) {
if (!strcmp("--cpu", argv[2])) {
exec_mode = ExecMode::CPU;
} else if (!strcmp("--cuda", argv[2])) {
exec_mode = ExecMode::CUDA;
}
}
// Setup replay log
const char *replay_log_path = nullptr;
if (argc >= 4) {
replay_log_path = argv[3];
}
auto replay_log = Optional<HeapArray<int32_t>>::none();
uint32_t cur_replay_step = 0;
uint32_t num_replay_steps = 0;
if (replay_log_path != nullptr) {
replay_log = readReplayLog(replay_log_path);
num_replay_steps = replay_log->size() / (num_worlds * num_views * 4);
}
bool enable_batch_renderer =
#ifdef MADRONA_MACOS
false;
#else
true;
#endif
WindowManager wm {};
WindowHandle window = wm.makeWindow("Escape Room", 2730, 1536);
render::GPUHandle render_gpu = wm.initGPU(0, { window.get() });
// Create the simulation manager
Manager mgr({
.execMode = exec_mode,
.gpuID = 0,
.numWorlds = num_worlds,
.randSeed = 5,
.autoReset = replay_log.has_value(),
.enableBatchRenderer = enable_batch_renderer,
.extRenderAPI = wm.gpuAPIManager().backend(),
.extRenderDev = render_gpu.device(),
});
float camera_move_speed = 10.f;
math::Vector3 initial_camera_position = { 0, consts::worldLength / 2.f, 30 };
math::Quat initial_camera_rotation =
(math::Quat::angleAxis(-math::pi / 2.f, math::up) *
math::Quat::angleAxis(-math::pi / 2.f, math::right)).normalize();
// Create the viewer viewer
viz::Viewer viewer(mgr.getRenderManager(), window.get(), {
.numWorlds = num_worlds,
.simTickRate = 20,
.cameraMoveSpeed = camera_move_speed,
.cameraPosition = initial_camera_position,
.cameraRotation = initial_camera_rotation,
});
// Replay step
auto replayStep = [&]() {
if (cur_replay_step == num_replay_steps - 1) {
return true;
}
printf("Step: %u\n", cur_replay_step);
for (uint32_t i = 0; i < num_worlds; i++) {
for (uint32_t j = 0; j < num_views; j++) {
uint32_t base_idx = 0;
base_idx = 4 * (cur_replay_step * num_views * num_worlds +
i * num_views + j);
int32_t move_amount = (*replay_log)[base_idx];
int32_t move_angle = (*replay_log)[base_idx + 1];
int32_t turn = (*replay_log)[base_idx + 2];
int32_t g = (*replay_log)[base_idx + 3];
printf("%d, %d: %d %d %d %d\n",
i, j, move_amount, move_angle, turn, g);
mgr.setAction(i, j, move_amount, move_angle, turn, g);
}
}
cur_replay_step++;
return false;
};
// Printers
auto self_printer = mgr.selfObservationTensor().makePrinter();
auto partner_printer = mgr.partnerObservationsTensor().makePrinter();
auto room_ent_printer = mgr.roomEntityObservationsTensor().makePrinter();
auto door_printer = mgr.doorObservationTensor().makePrinter();
auto lidar_printer = mgr.lidarTensor().makePrinter();
auto steps_remaining_printer = mgr.stepsRemainingTensor().makePrinter();
auto reward_printer = mgr.rewardTensor().makePrinter();
auto printObs = [&]() {
printf("Self\n");
self_printer.print();
printf("Partner\n");
partner_printer.print();
printf("Room Entities\n");
room_ent_printer.print();
printf("Door\n");
door_printer.print();
printf("Lidar\n");
lidar_printer.print();
printf("Steps Remaining\n");
steps_remaining_printer.print();
printf("Reward\n");
reward_printer.print();
printf("\n");
};
(void)printObs;
// Main loop for the viewer viewer
viewer.loop(
[&mgr](CountT world_idx, const Viewer::UserInput &input)
{
using Key = Viewer::KeyboardKey;
if (input.keyHit(Key::R)) {
mgr.triggerReset(world_idx);
}
},
[&mgr](CountT world_idx, CountT agent_idx,
const Viewer::UserInput &input)
{
using Key = Viewer::KeyboardKey;
int32_t x = 0;
int32_t y = 0;
int32_t r = 2;
int32_t g = 0;
bool shift_pressed = input.keyPressed(Key::Shift);
if (input.keyPressed(Key::W)) {
y += 1;
}
if (input.keyPressed(Key::S)) {
y -= 1;
}
if (input.keyPressed(Key::D)) {
x += 1;
}
if (input.keyPressed(Key::A)) {
x -= 1;
}
if (input.keyPressed(Key::Q)) {
r += shift_pressed ? 2 : 1;
}
if (input.keyPressed(Key::E)) {
r -= shift_pressed ? 2 : 1;
}
if (input.keyHit(Key::G)) {
g = 1;
}
int32_t move_amount;
if (x == 0 && y == 0) {
move_amount = 0;
} else if (shift_pressed) {
move_amount = consts::numMoveAmountBuckets - 1;
} else {
move_amount = 1;
}
int32_t move_angle;
if (x == 0 && y == 1) {
move_angle = 0;
} else if (x == 1 && y == 1) {
move_angle = 1;
} else if (x == 1 && y == 0) {
move_angle = 2;
} else if (x == 1 && y == -1) {
move_angle = 3;
} else if (x == 0 && y == -1) {
move_angle = 4;
} else if (x == -1 && y == -1) {
move_angle = 5;
} else if (x == -1 && y == 0) {
move_angle = 6;
} else if (x == -1 && y == 1) {
move_angle = 7;
} else {
move_angle = 0;
}
mgr.setAction(world_idx, agent_idx, move_amount, move_angle, r, g);
}, [&]() {
if (replay_log.has_value()) {
bool replay_finished = replayStep();
if (replay_finished) {
viewer.stopLoop();
}
}
mgr.step();
//printObs();
}, []() {});
}