-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
67 lines (51 loc) · 1.8 KB
/
main.cpp
File metadata and controls
67 lines (51 loc) · 1.8 KB
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
#include "BoidRenderer.hpp"
#include "boid.hpp"
#include "config.hpp"
#include "helper.hpp"
#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
std::string getInfoString(std::vector<Boid> const& boids)
{
std::ostringstream oss;
auto [avgDistance, stdDevDistance] = averageDistanceAndStdDev(boids);
oss << "Avg. Distance: " << avgDistance
<< " std. dev.:" << stdDevDistance << "\n";
auto [avgVelocity, stdDevVelocity] = averageVelocityAndStdDev(boids);
oss << "Avg. Velocity: (" << avgVelocity.first << ", " << avgVelocity.second << ")\n"
<< "Std. Dev. of Velocity: (" << stdDevVelocity.first << ", " << stdDevVelocity.second << ")";
return oss.str();
}
// Let's define an alias for shorter usage, cannot be modified
const Config& cfg = Config::getInstance();
int main()
{
// Load the configuration once, the cast allows us to make it modifiable.
Config& config = const_cast<Config&>(Config::getInstance());
config.loadConfig("config.txt");
BoidRenderer renderer(1500, 1000, "Boids Simulation");
std::vector<Boid> boids =
generateBoids(cfg.getNBoids(), cfg.getXSpace()[0], cfg.getXSpace()[1],
cfg.getYSpace()[0], cfg.getYSpace()[1]);
lazyUpdateNeighbors(boids);
std::string info=getInfoString(boids);
for (double time = 0; time < cfg.getTTot(); time += cfg.getDt()) {
renderer.processEvents();
std::for_each(boids.begin(), boids.end(), [](Boid& b) {
b.updateVelocity();
b.updatePosition();
});
lazyUpdateNeighbors(boids);
renderer.render(boids, info);
// print info every 10s
if (static_cast<int>(time / cfg.getDt())
% static_cast<int>(1 / cfg.getDt())
== 0) {
info = getInfoString(boids);
}
if (!renderer.isOpen()) {
break;
}
}
}