This commit is contained in:
Nguyen Quy Anh
2026-03-27 17:20:55 +01:00
parent 980bb5635e
commit d347534ff2
9 changed files with 450 additions and 79 deletions
+65 -79
View File
@@ -1,103 +1,89 @@
#include <cfloat>
#include <iostream>
#include <math.h>
#include "board.hpp"
#include "forces.hpp"
#include "math.h"
#include "pedestrian.hpp"
#include "vector2d.hpp"
double g(double v_max_alpha, double w_alpha_norm) {
if (w_alpha_norm <= v_max_alpha) {
return 1;
}
return v_max_alpha / w_alpha_norm;
}
using namespace std;
struct Vector2d {
double x, y;
Vector2d(double a, double b) : x(a), y(b) {}
Vector2d() : x(.0), y(.0) {}
Vector2d operator+(Vector2d v) const { return Vector2d(v.x + x, v.y + y); }
Vector2d operator-(Vector2d v) const { return Vector2d(x - v.x, y - v.y); }
Vector2d operator-(double a) const { return Vector2d(x - a, y - a); }
Vector2d operator+(double a) const { return Vector2d(x + a, y + a); }
Vector2d operator/(double a) const { return Vector2d(x / a, y / a); }
Vector2d operator*(double a) const { return Vector2d(x * a, y * a); }
Vector2d operator+=(const Vector2d &v) {
*this = *this + v;
return *this;
}
double norm() { return sqrt(x * x + y * y); }
double dot(Vector2d v) { return x * v.x + y * v.y; }
void reset() {
x = 0;
y = 0;
}
};
// Numer of pedestrians
// $n_p$
const int n_p = 100;
// const int n_p = 100;
// Number of iterations
// $n_{iter}$
const int n_iter = 100;
// const int n_iter = 100;
// TODO: Change this
// $\tau$
const double tau = 0.12345;
void compute_acceleration_term(Vector2d *F_i) {}
void compute_repulsive_effects_from_pedestrians(Vector2d *F_i) {}
void compute_repulsive_effects_from_borders(Vector2d *F_i) {}
// Return an array of doubles
double *w(Vector2d e, Vector2d f) {
// TODO:
// $cos(60\degree)$
// if (e * f > f.norm() * (0.5)) {
// }
// return 0;
Pedestrian *init_pedestrians(int n_p) {
Pedestrian *P = new Pedestrian[n_p];
for (int i = 0; i < n_p; i++) {
P[i] = Pedestrian();
}
return P;
}
int main() {
Board init_board(int width, int length) {
Vector2d obs[] = {Vector2d(2.0, 1.0), Vector2d(4.0, 1.0), Vector2d(6.0, 2.0), Vector2d(6.0, 4.0)};
return Board(width, length, 4, obs);
}
// ! DONT TOUCH
int main(int argc, const char* argv[]) {
// $\vec{v}^0_i$
// Velocity at iteration $t$ for pedestrian $i$
double v[n_iter][n_p];
Board B = init_board(10, 15);
// $\vec{r}^0_i$
// Intended destination at iteration $t$ for pedestrian $i$
Vector2d r[n_iter][n_p];
int n_p = atoi(argv[1]);
int n_iter = atoi(argv[2]);
// $\vec{e}_i$
// Desired direction to the next way point at iteration $t$ for pedestrian $i$
Vector2d e[n_iter][n_p];
// $v_i^{max}$
// Maximum allowed speed for pedestrian $i$
double v_max[n_p];
// $v_i^{0}$
// Desired speed for pedestrian $i$
double v_0[n_p];
// $\vec{F}_i$
// Total social force for pedestrian $i$
Vector2d F[n_p][n_p];
// $\vec{r}_0$
// Desired destination of pedestrian $n_p$
Vector2d r_0[n_p];
Pedestrian *pedestrians = init_pedestrians(n_p);
for (int t = 0; t < n_iter; t++) {
for (int i = 0; i < n_p; i++) {
for (int itr = 0; itr < n_p; itr++) {
F[i][itr].reset();
}
compute_acceleration_term(F[i]);
compute_repulsive_effects_from_pedestrians(F[i]);
compute_repulsive_effects_from_borders(F[i]);
// TODO: Update $\vec{w}_i$
Pedestrian &p = pedestrians[i];
// TODO: Update $\vec{v}_i^{t+1}$
p.F_i.reset();
// TODO: pdate $\vec{r}_i^{t+1}$
//
// $\vec{F}_\alpha(\vec{v}_\alpha, v^0_\alpha \vec{e}_\alpha) \vec{e}_\alpha(t) = \frac{1}{\tau_\alpha}(v^0_\alpha \vec{e}_\alpha - \vec{v}_\alpha)$
//
compute_acceleration_term(&p);
//
// $\sum_{\beta} \vec{F}_{\alpha\beta}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{\beta})$
//
compute_repulsive_effects_from_pedestrians(pedestrians, &p, n_p);
//
// $\sum_{B} \vec{F}_{\alpha B}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{B})$
//
compute_repulsive_effects_from_borders(B, &p);
//
// $\vec{w}_i = \vec{v}_i + \vec{F}_idt$
//
p.w_alpha = p.curr_vel + p.F_i * delta_t;
//
// $\vec{v}_i^{t+1} = \vec{w}_ig(\frac{v^{max}_i}{\Arrowvert \vec{w}_i \Arrowvert}) $
//
p.next_vel = p.w_alpha * g(p.v_max, p.w_alpha.norm());
//
// $\vec{r}_i^{t+1} += \vec{r}^t_i + v^t_idt$
//
p.curr_pos = p.curr_pos + p.curr_vel * delta_t;
}
for (int i = 0; i < n_p; i++) {
pedestrians[i].update();
}
}