added arrows
This commit is contained in:
Binary file not shown.
+9
-6
@@ -33,7 +33,7 @@ void compute_acceleration_term(Pedestrian *p) {
|
|||||||
|
|
||||||
// # 55(N - 1) = 55N - 55 flops
|
// # 55(N - 1) = 55N - 55 flops
|
||||||
//
|
//
|
||||||
// $\sum_{\beta} \vec{F}_{\alpha\beta}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{\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) {
|
void compute_repulsive_effects_from_pedestrians(Pedestrian *pedestrians, Pedestrian *p, int N) {
|
||||||
|
|
||||||
@@ -73,25 +73,25 @@ void compute_repulsive_effects_from_pedestrians(Pedestrian *pedestrians, Pedestr
|
|||||||
|
|
||||||
// # 7 flops = 1 SQRT, 3 ADDs, 2 MULTs, 1 DIV
|
// # 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}$
|
// $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;
|
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
|
// # 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}$
|
// $\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);
|
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
|
// # 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$
|
// $\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)));
|
Vector2d f_ped = b_grad * ((V0_AB / SIGMA) * (exp(-b / SIGMA)));
|
||||||
|
|
||||||
// # 14 flops: 9 MULTs, 4 ADDs, 1 SQRT
|
// # 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})$
|
// $\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);
|
p->f_i += f_ped * w(p->e, f_ped * -1);
|
||||||
}
|
}
|
||||||
@@ -99,6 +99,9 @@ void compute_repulsive_effects_from_pedestrians(Pedestrian *pedestrians, Pedestr
|
|||||||
|
|
||||||
// # Assume that the four borders are also 4 obstacles
|
// # Assume that the four borders are also 4 obstacles
|
||||||
// # 48 n_obstacles flops = 192 flops
|
// # 48 n_obstacles flops = 192 flops
|
||||||
|
//
|
||||||
|
// $\vec{F}_{\alpha B}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{B})$
|
||||||
|
//
|
||||||
void compute_repulsive_effects_from_borders(Board &board, Pedestrian *p) {
|
void compute_repulsive_effects_from_borders(Board &board, Pedestrian *p) {
|
||||||
|
|
||||||
Obstacle *obstacles = board.obstacles;
|
Obstacle *obstacles = board.obstacles;
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ Pedestrian *init_pedestrians(int n_p) {
|
|||||||
for (int i = 0; i < n_p / 2; i++) {
|
for (int i = 0; i < n_p / 2; i++) {
|
||||||
double y = y_gen(gen);
|
double y = y_gen(gen);
|
||||||
Vector2d start_pos = Vector2d(x_gen(gen), y);
|
Vector2d start_pos = Vector2d(x_gen(gen), y);
|
||||||
Vector2d destination = Vector2d(BOARD_LENGTH + 300, BOARD_WIDTH / 2);
|
Vector2d destination = Vector2d(BOARD_LENGTH + 30, BOARD_WIDTH / 2);
|
||||||
// double desired_v0 = velocity_gen(gen);
|
// double desired_v0 = velocity_gen(gen);
|
||||||
double desired_v0;
|
double desired_v0;
|
||||||
do {
|
do {
|
||||||
@@ -46,7 +46,7 @@ Pedestrian *init_pedestrians(int n_p) {
|
|||||||
for (int i = n_p / 2; i < n_p; i++) {
|
for (int i = n_p / 2; i < n_p; i++) {
|
||||||
double y = y_gen(gen);
|
double y = y_gen(gen);
|
||||||
Vector2d start_pos = Vector2d(x_gen(gen), y);
|
Vector2d start_pos = Vector2d(x_gen(gen), y);
|
||||||
Vector2d destination = Vector2d(-300, BOARD_WIDTH / 2);
|
Vector2d destination = Vector2d(-30, BOARD_WIDTH / 2);
|
||||||
// double desired_v0 = velocity_gen(gen);
|
// double desired_v0 = velocity_gen(gen);
|
||||||
double desired_v0;
|
double desired_v0;
|
||||||
do {
|
do {
|
||||||
@@ -70,7 +70,7 @@ int main(int argc, const char *argv[]) {
|
|||||||
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;
|
||||||
|
|
||||||
std::vector<std::tuple<int, int, double, double, int>> samples; // t, id, x, y, color
|
std::vector<std::tuple<int, int, double, double, double, double, int>> samples; // t, id, x, y, e.x, e.y, color
|
||||||
if (dump_path)
|
if (dump_path)
|
||||||
samples.reserve((size_t)n_iter / dump_interval * n_p);
|
samples.reserve((size_t)n_iter / dump_interval * n_p);
|
||||||
|
|
||||||
@@ -119,7 +119,7 @@ int main(int argc, const char *argv[]) {
|
|||||||
|
|
||||||
// # 4 flops = 2 ADDs, 2 MULTs
|
// # 4 flops = 2 ADDs, 2 MULTs
|
||||||
//
|
//
|
||||||
// $\vec{r}_i^{t+1} += \vec{r}^t_i + v^t_idt$
|
// $\vec{r}_i^{t+1} = \vec{r}^t_i + v^t_idt$
|
||||||
//
|
//
|
||||||
p.curr_pos = p.curr_pos + p.curr_vel * dt;
|
p.curr_pos = p.curr_pos + p.curr_vel * dt;
|
||||||
|
|
||||||
@@ -137,11 +137,13 @@ int main(int argc, const char *argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// # 10 flops = 5 ADDs, 2 DIVs, 1 SQRT, 2 MULTs
|
// # 10 flops = 5 ADDs, 2 DIVs, 1 SQRT, 2 MULTs
|
||||||
p.e = (p.destination - p.curr_pos) / (p.destination - p.curr_pos).norm();
|
//p.e = (p.destination - p.curr_pos) / (p.destination - p.curr_pos).norm();
|
||||||
|
//p.e = (p.curr_vel - p.destination) / (p.curr_vel - p.destination).norm();
|
||||||
|
p.e = (p.destination - (p.curr_vel / p.curr_vel.norm())) / (p.destination - (p.curr_vel / p.curr_vel.norm())).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.e.x, p.e.y, p.color);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -164,9 +166,9 @@ int main(int argc, const char *argv[]) {
|
|||||||
// now we wanna save the data to a csv
|
// now we wanna save the data to a csv
|
||||||
if (dump_path) {
|
if (dump_path) {
|
||||||
std::ofstream out(dump_path);
|
std::ofstream out(dump_path);
|
||||||
out << "time,id,x,y,color\n";
|
out << "time,id,x,y,p.e.x,p.e.y,color\n";
|
||||||
for (auto &[t, id, x, y, c] : samples) {
|
for (auto &[t, id, x, y, e_x, e_y, c] : samples) {
|
||||||
out << t << ',' << id << ',' << x << ',' << y << ',' << c << '\n';
|
out << t << ',' << id << ',' << x << ',' << y << ',' << e_x << ',' << e_y << ',' << c << '\n';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
+1
-1
@@ -14,7 +14,7 @@ struct Pedestrian {
|
|||||||
// $\vec{r}_\alpha(t)$
|
// $\vec{r}_\alpha(t)$
|
||||||
Vector2d curr_pos;
|
Vector2d curr_pos;
|
||||||
// $\vec{e}_{\alpha}(t)$
|
// $\vec{e}_{\alpha}(t)$
|
||||||
Vector2d e;
|
Vector2d e; // desired direction vector
|
||||||
// $v_{\alpha}^{max} = 1.3 v^{0}_{\alpha}$
|
// $v_{\alpha}^{max} = 1.3 v^{0}_{\alpha}$
|
||||||
double v_max;
|
double v_max;
|
||||||
// $v_\alpha^0$
|
// $v_\alpha^0$
|
||||||
|
|||||||
+98001
-1000001
File diff suppressed because it is too large
Load Diff
Binary file not shown.
+33
-22
@@ -1,52 +1,63 @@
|
|||||||
import sys
|
import sys
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
from matplotlib.animation import FuncAnimation
|
from matplotlib.animation import FuncAnimation
|
||||||
|
|
||||||
|
if len(sys.argv) < 2:
|
||||||
|
sys.exit("Use it like the following: python visualization.py positions.csv (out.mp4)")
|
||||||
|
|
||||||
if len(sys.argv)<2:
|
|
||||||
sys.exit("Use it like the following: python visualization.py positions.csv (out.gif)")
|
|
||||||
|
|
||||||
|
|
||||||
df = pd.read_csv(sys.argv[1])
|
df = pd.read_csv(sys.argv[1])
|
||||||
frames = sorted(df["time"].unique())
|
frames = sorted(df["time"].unique())
|
||||||
|
|
||||||
fig, ax = plt.subplots(figsize = (10,2.2))
|
fig, ax = plt.subplots(figsize=(10, 2.2))
|
||||||
ax.set_xlim(0, 50); ax.set_ylim(0, 10)
|
ax.set_xlim(0, 50); ax.set_ylim(0, 10)
|
||||||
ax.set_aspect("equal"); ax.set_facecolor("white")
|
ax.set_aspect("equal"); ax.set_facecolor("white")
|
||||||
ax.set_xticks([]); ax.set_yticks([])
|
ax.set_xticks([]); ax.set_yticks([])
|
||||||
for s in ("top", "right", "left", "bottom"):
|
for s in ("top", "right", "left", "bottom"):
|
||||||
ax.spines[s].set_color("gray")
|
ax.spines[s].set_color("gray")
|
||||||
|
|
||||||
|
s_white = ax.scatter([], [], s=30, facecolors="black", edgecolor="black", zorder=2)
|
||||||
# Lets define the 2 groups
|
s_black = ax.scatter([], [], s=30, facecolors="white", edgecolor="black", zorder=2)
|
||||||
# WHITE = 1 (left -> right, drawn as filled black circles, like paper)
|
|
||||||
# BLACK = 0 (right -> left, drawn as empty circles)
|
|
||||||
s_white = ax.scatter([],[], s=30, facecolors = "black", edgecolor = "black", zorder = 2)
|
|
||||||
s_black = ax.scatter([],[], s=30, facecolors = "white", edgecolor = "black", zorder = 2)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# update our graphic from t to t+1
|
|
||||||
title = ax.set_title("")
|
title = ax.set_title("")
|
||||||
|
|
||||||
|
# Keep a reference to quiver objects so we can remove them each frame
|
||||||
|
quivers = []
|
||||||
|
|
||||||
def update(t):
|
def update(t):
|
||||||
sub = df[df["time"] == t]
|
sub = df[df["time"] == t]
|
||||||
w = sub[sub["color"] == 1]
|
w = sub[sub["color"] == 1]
|
||||||
b = sub[sub["color"] == 0]
|
b = sub[sub["color"] == 0]
|
||||||
|
|
||||||
s_white.set_offsets(w[["x", "y"]].values)
|
s_white.set_offsets(w[["x", "y"]].values)
|
||||||
s_black.set_offsets(b[["x", "y"]].values)
|
s_black.set_offsets(b[["x", "y"]].values)
|
||||||
|
|
||||||
|
# Remove old quiver arrows
|
||||||
|
for q in quivers:
|
||||||
|
q.remove()
|
||||||
|
quivers.clear()
|
||||||
|
|
||||||
|
# Draw fresh arrows each frame
|
||||||
|
arrow_scale = 1.5 # Tune this to change arrow length visually
|
||||||
|
if len(w):
|
||||||
|
q_white = ax.quiver(w["x"], w["y"], w["p.e.x"], w["p.e.y"],
|
||||||
|
color="black", angles="xy", scale_units="xy",
|
||||||
|
scale=1/arrow_scale, width=0.003,
|
||||||
|
headwidth=4, headlength=5, zorder=3)
|
||||||
|
quivers.append(q_white)
|
||||||
|
if len(b):
|
||||||
|
q_black = ax.quiver(b["x"], b["y"], b["p.e.x"], b["p.e.y"],
|
||||||
|
color="gray", angles="xy", scale_units="xy",
|
||||||
|
scale=1/arrow_scale, width=0.003,
|
||||||
|
headwidth=4, headlength=5, zorder=3)
|
||||||
|
quivers.append(q_black)
|
||||||
|
|
||||||
title.set_text(f"t = {t}")
|
title.set_text(f"t = {t}")
|
||||||
return s_white, s_black, title
|
return s_white, s_black, title
|
||||||
|
|
||||||
anim = FuncAnimation(fig,update, frames = frames, interval=50, blit=False)
|
anim = FuncAnimation(fig, update, frames=frames, interval=50, blit=False)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if len(sys.argv) >= 3:
|
if len(sys.argv) >= 3:
|
||||||
anim.save(sys.argv[2], writer="ffmpeg", fps=20)
|
anim.save(sys.argv[2], writer="ffmpeg", fps=20)
|
||||||
print(f"saved {sys.argv[2]}")
|
print(f"Saved {sys.argv[2]}")
|
||||||
else:
|
else:
|
||||||
plt.show()
|
plt.show()
|
||||||
Reference in New Issue
Block a user