Update main.cpp: add command line arguments checks

This commit is contained in:
AKallala
2026-04-27 14:42:50 +02:00
parent b9cd6b15e8
commit 4273b83236
+23 -5
View File
@@ -60,12 +60,30 @@ Pedestrian *init_pedestrians(int n_p) {
int main(int argc, const char *argv[]) { int main(int argc, const char *argv[]) {
Obstacle obs[] = {}; // Check command line arguments
Board board = Board(BOARD_LENGTH, BOARD_WIDTH, 0, obs); if (argc < 3) {
printf("Usage: ./sim <number_of_pedestrians> <number_of_iterations>\n");
printf(" number_of_iterations: each iteration = %.2f seconds of simulation time\n", dt);
return 1;
}
// Parse command line arguments
int n_p = atoi(argv[1]); int n_p = atoi(argv[1]);
int n_iter = atoi(argv[2]); int n_iter = atoi(argv[2]);
if (n_p <= 0) {
printf("Error: <number_of_pedestrians> must be a positive integer, got '%s'\n", argv[1]);
return 1;
}
if (n_iter <= 0) {
printf("Error: <number_of_iterations> must be a positive integer, got '%s'\n", argv[2]);
return 1;
}
Obstacle obs[] = {};
Board board = Board(BOARD_LENGTH, BOARD_WIDTH, 0, obs);
// Logic to get/dump the simulation data // Logic to get/dump the simulation data
const int dump_interval = 50; // dump every 5 iterations -> 20 fps (since dt = 0.01) const int dump_interval = 50; // dump every 5 iterations -> 20 fps (since dt = 0.01)
const char *dump_path = (argc >= 4) ? argv[3] : nullptr; const char *dump_path = (argc >= 4) ? argv[3] : nullptr;
@@ -123,6 +141,9 @@ int main(int argc, const char *argv[]) {
// //
p.curr_pos = p.curr_pos + p.curr_vel * dt; p.curr_pos = p.curr_pos + p.curr_vel * dt;
// # 10 flops = 5 ADDs, 2 DIVs, 1 SQRT, 2 MULTs
p.e = (p.destination - p.curr_pos) / (p.destination - p.curr_pos).norm();
// how to handle if a pedestrian reached the end of our simulation/end of the hallway // how to handle if a pedestrian reached the end of our simulation/end of the hallway
// The option we do here is to just "teleport" the person back to the beginning // The option we do here is to just "teleport" the person back to the beginning
// the good thing is we will have a constant flow of pedestrians and in the simulation we should see how lines build up // the good thing is we will have a constant flow of pedestrians and in the simulation we should see how lines build up
@@ -136,9 +157,6 @@ int main(int argc, const char *argv[]) {
// p.destination.y = p.curr_pos.y; // p.destination.y = p.curr_pos.y;
} }
// # 10 flops = 5 ADDs, 2 DIVs, 1 SQRT, 2 MULTs
p.e = (p.destination - p.curr_pos) / (p.destination - p.curr_pos).norm();
// if we have a dump path/file we wanna save the data for visualisation // if we have a dump path/file we wanna save the data for visualisation
if (dump_path && (t % dump_interval == 0)) { if (dump_path && (t % dump_interval == 0)) {
samples.emplace_back(t, i, p.curr_pos.x, p.curr_pos.y, p.color); samples.emplace_back(t, i, p.curr_pos.x, p.curr_pos.y, p.color);