129 lines
4.9 KiB
C++
129 lines
4.9 KiB
C++
#include "forces.hpp"
|
|
#include <math.h>
|
|
|
|
// # 8 flops = 5 MULTs, 2 ADD, 1 SQRT
|
|
double w(Vector2d e, Vector2d f) {
|
|
if (e.dot(f) >= f.norm() * cos_phi)
|
|
return 1.0;
|
|
else
|
|
return c;
|
|
}
|
|
|
|
// # 2 DIVs
|
|
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;
|
|
}
|
|
|
|
// # 8 flops
|
|
void compute_acceleration_term(Pedestrian *p) {
|
|
// # 2 flops
|
|
Vector2d desired_velocity = p->e * p->desired_v0;
|
|
Vector2d actual_velocity = p->curr_vel;
|
|
// # 6 flops = 4 ADDs, 2 DIVs
|
|
p->f_i += (desired_velocity - actual_velocity) / tau_alpha;
|
|
}
|
|
|
|
// # 55(N - 1) = 55N - 55 flops
|
|
//
|
|
// $\sum_{\beta} \vec{F}_{\alpha\beta}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{\beta})$
|
|
//
|
|
void compute_repulsive_effects_from_pedestrians(Pedestrian *pedestrians, Pedestrian *p, int N) {
|
|
|
|
for (int i = 0; i < N; i++) {
|
|
|
|
if (pedestrians + i == p)
|
|
continue;
|
|
|
|
// # Optional: Do not consider far away pedestrians from current pedestrian
|
|
// # Might get better speed up, remove if not
|
|
// if ((pedestrians[i].curr_pos - p->curr_pos).norm() > DISTANCE) {
|
|
// continue;
|
|
// }
|
|
|
|
// # 2 ADDs
|
|
// $\vec{r}_{\alpha\beta}$
|
|
Vector2d r_ab = p->curr_pos - pedestrians[i].curr_pos;
|
|
|
|
// # 4 flops = 1 SQRT, 2 MULTs, 1 ADD
|
|
// $\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert$
|
|
double r_ab_norm = r_ab.norm();
|
|
|
|
// $\vec{e}_{\beta}$
|
|
Vector2d e_b = pedestrians[i].e;
|
|
|
|
// # 2 MULTs
|
|
// $v_\beta \Delta t$
|
|
double s_b = pedestrians[i].curr_vel.norm() * delta_t;
|
|
|
|
// # 4 flops = 2 ADDs, 2 MULTs
|
|
// $\vec{r}_{\alpha\beta} - \vec{v}_{\beta}\varDelta_{t}\vec{e}_{\beta}$
|
|
Vector2d r_ab2 = r_ab - (e_b * s_b);
|
|
|
|
// # 4 flops = 1 SQRT, 2 MULTs, 1 ADD
|
|
// $\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert$
|
|
double r_ab2_norm = r_ab2.norm();
|
|
|
|
// # 7 flops = 1 SQRT, 3 ADDs, 2 MULTs, 1 DIV
|
|
//
|
|
// $(4):2b = \sqrt{(\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert)^2 - (v_\beta \Delta t)^2}$
|
|
//
|
|
double b = sqrt((r_ab_norm + r_ab2_norm) * (r_ab_norm + r_ab2_norm) - s_b * s_b) / 2;
|
|
|
|
// # 12 flops = 6 DIVs, 3 ADDs, 3 MULTs
|
|
//
|
|
// $(7):\grad_{\vec{r}_{\alpha \beta}}b = \frac{(\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert) (\frac{\vec{r}_{\alpha \beta}}{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert} + \frac{\vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta}{\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert})}{4b}$
|
|
//
|
|
Vector2d b_grad = (r_ab / r_ab_norm + r_ab2 / r_ab2_norm) * (r_ab_norm + r_ab2_norm) / (4 * b);
|
|
|
|
// # 6 flops = 3 MULTs, 2 DIVs, 1 EXP
|
|
//
|
|
// $(5):\vec{f}_{\alpha \beta}(\vec{r}_{\alpha \beta}) = \frac{V^0_{\alpha \beta}}{\sigma}e^{-b / \sigma} \grad_{\vec{r}_{\alpha \beta}}b$
|
|
//
|
|
Vector2d f_ped = b_grad * ((V0_AB / SIGMA) * (exp(-b / SIGMA)));
|
|
|
|
// # 14 flops: 9 MULTs, 4 ADDs, 1 SQRT
|
|
//
|
|
// $(11):\vec{F}_{\alpha\beta}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{\beta}) = w(\vec{e}_{\alpha}, -\vec{f}_{\alpha\beta})\vec{f}_{\alpha\beta}(\vec{r}_{\alpha} - \vec{r}_{\beta})$
|
|
//
|
|
p->f_i += f_ped * w(p->e, f_ped * -1);
|
|
}
|
|
}
|
|
|
|
// # Assume that the four borders are also 4 obstacles
|
|
// # 48 n_obstacles flops = 192 flops
|
|
void compute_repulsive_effects_from_borders(Board &board, Pedestrian *p) {
|
|
|
|
Obstacle *obstacles = board.obstacles;
|
|
int n_borders = board.n_obstacles;
|
|
|
|
for (int i = 0; i < n_borders; i++) {
|
|
|
|
Obstacle obstacle = obstacles[i];
|
|
|
|
// # 17 flops
|
|
// $\vec{r}_{\alpha B}$
|
|
Vector2d r_aB = p->curr_pos - obstacle.find_nearest_obstacle_point(p->curr_pos);
|
|
|
|
// # 17 flops = 2 DIVs, 1 SQRT, 2 MULTs, 1 ADD, 2 MULTs, 1 DIV, 2 MULTs, 1 EXP, 1 SQRT, 2 MULTs, 1 ADD, 1 DIV
|
|
//
|
|
// $\vec{F}_{\alpha B}(\vec{r}_{\alpha B}) = \frac{U^0_{\alpha B}}{R} e^{- \Arrowvert \vec{r}_{\alpha B} \Arrowvert / R} \frac{\vec{r}_{\alpha B}}{\Arrowvert \vec{r}_{\alpha B} \Arrowvert}$
|
|
//
|
|
Vector2d f_obst = (r_aB / r_aB.norm()) * (U0_AB / R) * std::exp(-r_aB.norm() / R);
|
|
|
|
// # 2 ADDs
|
|
// p->f_i += f_obst; //commented since this version dont takes the field of view / angle to the border into account
|
|
|
|
// I have read the paper again and on page 6 they say
|
|
// However, the formulas above for attractive and repulsive effects only hold for situations that are perceived in the desired direction e_alpha(t) of motion.
|
|
// I assumed that with formulas above its also meant for equation 8 (even though not explicitly mentioned their)
|
|
// # 14 flops: 9 MULTs, 4 ADDS, 1 SQRT
|
|
//
|
|
// $(12):\vec{F}_{\alpha B}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{ B}) = w(\vec{e}_{\alpha}, -\vec{F}_{\alpha B})\vec{F}_{\alpha B}(\vec{r}_{\alpha} - \vec{r}_{ B})$
|
|
//
|
|
p->f_i += f_obst * w(p->e, f_obst * -1);
|
|
}
|
|
}
|