Skip to content

Commit d85ee34

Browse files
author
Sami Hatna
committed
Merge branch 'phase2' into 'main'
Phase2 Code Review See merge request sami.hatna/crowd-simulation!3
2 parents b9383e4 + 3db5919 commit d85ee34

21 files changed

+33207
-161
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ find_package(SDL2 REQUIRED)
1212
include_directories(${SDL2_INCLUDE_DIRS})
1313

1414
add_executable(crowdsim src/main.cpp)
15-
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsycl")
15+
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsycl -fsycl-targets=spir64-gen")
1616

1717
add_subdirectory(external)
1818

external/Actor.cpp

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
#include "Actor.hpp"
22

3-
Actor::Actor(vecType pPos, vecType pVelocity, vecType pDesiredVelocity, vecType pDestination, float pMass, float pRadius, bool pAtDestination, std::array<int, 3> pColor) {
4-
pos = pPos;
5-
velocity = pVelocity;
6-
desiredVelocity = pDesiredVelocity;
7-
destination = pDestination;
8-
mass = pMass;
9-
radius = pRadius;
10-
atDestination = pAtDestination;
11-
color = pColor;
12-
}
3+
Actor::Actor(vecType pPos, vecType pVelocity, float pDesiredSpeed, int pPathId, float pMass, float pRadius, bool pAtDestination, std::array<int, 3> pColor, bool pHeatmapEnabled):
4+
pos(pPos), velocity(pVelocity), desiredSpeed(pDesiredSpeed),
5+
pathId(pPathId), mass(pMass), radius(pRadius),
6+
atDestination(pAtDestination), color(pColor), heatmapEnabled(pHeatmapEnabled) {
7+
variation = {0, 0};
8+
destinationIndex = 0;
9+
}
1310

1411
SYCL_EXTERNAL vecType Actor::getPos() const {
1512
return pos;
@@ -19,12 +16,20 @@ SYCL_EXTERNAL vecType Actor::getVelocity() const {
1916
return velocity;
2017
}
2118

22-
SYCL_EXTERNAL vecType Actor::getDesiredVelocity() const {
23-
return desiredVelocity;
19+
SYCL_EXTERNAL float Actor::getDesiredSpeed() const {
20+
return desiredSpeed;
21+
}
22+
23+
SYCL_EXTERNAL int Actor::getPathId() const {
24+
return pathId;
2425
}
2526

26-
SYCL_EXTERNAL vecType Actor::getDestination() const {
27-
return destination;
27+
SYCL_EXTERNAL int Actor::getDestinationIndex() const {
28+
return destinationIndex;
29+
}
30+
31+
SYCL_EXTERNAL vecType Actor::getVariation() const {
32+
return variation;
2833
}
2934

3035
SYCL_EXTERNAL float Actor::getMass() const {
@@ -43,6 +48,10 @@ SYCL_EXTERNAL std::array<int, 3> Actor::getColor() const {
4348
return color;
4449
}
4550

51+
SYCL_EXTERNAL bool Actor::getHeatmapEnabled() const {
52+
return heatmapEnabled;
53+
}
54+
4655
SYCL_EXTERNAL void Actor::setPos(vecType newPos) {
4756
pos = newPos;
4857
}
@@ -51,14 +60,41 @@ SYCL_EXTERNAL void Actor::setVelocity(vecType newVelocity) {
5160
velocity = newVelocity;
5261
}
5362

54-
SYCL_EXTERNAL void Actor::setDesiredVelocity(vecType newDesiredVelocity) {
55-
desiredVelocity = newDesiredVelocity;
63+
SYCL_EXTERNAL void Actor::setDesiredSpeed(float newDesiredSpeed) {
64+
desiredSpeed = newDesiredSpeed;
5665
}
5766

58-
SYCL_EXTERNAL void Actor::setDestination(vecType newDestination) {
59-
destination = newDestination;
67+
SYCL_EXTERNAL void Actor::setVariation(vecType newVariation) {
68+
variation = newVariation;
6069
}
6170

6271
SYCL_EXTERNAL void Actor::setAtDestination(bool param) {
6372
atDestination = param;
6473
}
74+
75+
SYCL_EXTERNAL void Actor::setColor(std::array<int, 3> newColor) {
76+
color = newColor;
77+
}
78+
79+
SYCL_EXTERNAL void Actor::checkAtDestination(vecType destination, int pathSize) {
80+
std::array<float, 4> destinationBoundingBox = {destination[0] + 0.2f,
81+
destination[0] - 0.2f,
82+
destination[1] + 0.2f,
83+
destination[1] - 0.2f};
84+
if (pos[0] <= destinationBoundingBox[0] && pos[0] >= destinationBoundingBox[1]
85+
&& pos[1] <= destinationBoundingBox[2] && pos[1] >= destinationBoundingBox[3]) {
86+
if (destinationIndex >= PATHALLOCATIONSIZE - 1 || destinationIndex >= pathSize - 1) {
87+
this->setAtDestination(true);
88+
}
89+
else {
90+
destinationIndex++;
91+
}
92+
}
93+
}
94+
95+
void Actor::refreshVariation() {
96+
std::random_device rd;
97+
std::mt19937 gen(rd());
98+
std::normal_distribution<float> distr{0.0f, 2.0f};
99+
this->setVariation({distr(gen), distr(gen)});
100+
}

external/Actor.hpp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,50 @@
33

44
#include <iostream>
55
#include <array>
6+
#include <random>
7+
#include <vector>
68
#include <sycl/sycl.hpp>
79
#include "VectorMaths.hpp"
8-
9-
using vecType = std::array<float, 2>;
10+
#include "Path.hpp"
1011

1112
class Actor {
1213
private:
1314
vecType pos;
1415
vecType velocity;
15-
vecType desiredVelocity;
16-
vecType destination;
16+
float desiredSpeed;
17+
int pathId;
18+
int destinationIndex;
19+
vecType variation;
1720
float mass;
1821
float radius;
1922
bool atDestination;
2023
std::array<int, 3> color;
24+
bool heatmapEnabled;
2125

22-
public:
23-
Actor(vecType pPos, vecType pVelocity, vecType pDesiredVelocity, vecType pDestination, float pMass, float pRadius, bool pAtDestination, std::array<int, 3> pColor);
26+
public:
27+
Actor(vecType pPos, vecType pVelocity, float pdesiredSpeed, int pPathId, float pMass, float pRadius, bool pAtDestination, std::array<int, 3> pColor, bool pHeatmapEnabled);
2428

2529
SYCL_EXTERNAL vecType getPos() const;
2630
SYCL_EXTERNAL vecType getVelocity() const;
27-
SYCL_EXTERNAL vecType getDesiredVelocity() const;
28-
SYCL_EXTERNAL vecType getDestination() const;
31+
SYCL_EXTERNAL float getDesiredSpeed() const;
32+
SYCL_EXTERNAL int getPathId() const;
33+
SYCL_EXTERNAL int getDestinationIndex() const;
34+
SYCL_EXTERNAL vecType getVariation() const;
2935
SYCL_EXTERNAL float getMass() const;
3036
SYCL_EXTERNAL float getRadius() const;
3137
SYCL_EXTERNAL bool getAtDestination() const;
3238
SYCL_EXTERNAL std::array<int, 3> getColor() const;
39+
SYCL_EXTERNAL bool getHeatmapEnabled() const;
3340

3441
SYCL_EXTERNAL void setPos(vecType newPos);
3542
SYCL_EXTERNAL void setVelocity(vecType newVelocity);
36-
SYCL_EXTERNAL void setDesiredVelocity(vecType newDesiredVelocity);
37-
SYCL_EXTERNAL void setDestination(vecType newDestination);
43+
SYCL_EXTERNAL void setDesiredSpeed(float newDesiredSpeed);
44+
SYCL_EXTERNAL void setVariation(vecType newVariation);
3845
SYCL_EXTERNAL void setAtDestination(bool param);
46+
SYCL_EXTERNAL void setColor(std::array<int, 3> newColor);
47+
48+
SYCL_EXTERNAL void checkAtDestination(vecType destination, int pathSize);
49+
void refreshVariation();
3950
};
4051

4152
#endif

external/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
add_library(external Actor.cpp Room.cpp MathHelper.cpp DifferentialEq.cpp VectorMaths.cpp)
1+
add_library(external Actor.cpp Room.cpp Path.cpp MathHelper.cpp DifferentialEq.cpp VectorMaths.cpp Heatmap.cpp ParseInputFile.cpp)

external/DifferentialEq.cpp

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,71 @@
11
#include "DifferentialEq.hpp"
22

3-
SYCL_EXTERNAL void differentialEq(int z, sycl::accessor<Actor, 1, sycl::access::mode::read_write> actors, sycl::accessor<std::array<vecType, 2>, 1, sycl::access::mode::read> walls, sycl::stream out) {
4-
Actor* i = &actors[z];
3+
SYCL_EXTERNAL void differentialEq(int currentIndex, sycl::accessor<Actor, 1, sycl::access::mode::read_write> actors, sycl::accessor<std::array<vecType, 2>, 1, sycl::access::mode::read> walls, sycl::accessor<Path, 1, sycl::access::mode::read> paths, sycl::stream out) {
4+
Actor* currentActor = &actors[currentIndex];
55

6-
vecType pos = i->getPos();
6+
vecType pos = currentActor->getPos();
77

8-
float mi = i->getMass();
9-
float v0i = 2.0f;
10-
vecType e0i = normalize(getDirectionVector(i->getPos(), i->getDestination()));
11-
vecType vi = i->getVelocity();
8+
// Calculate personal impulse
9+
float mi = currentActor->getMass();
10+
float v0i = currentActor->getDesiredSpeed();
11+
vecType destination = paths[currentActor->getPathId()].getCheckpoints()[currentActor->getDestinationIndex()];
12+
vecType e0i = normalize(getDirectionVector(currentActor->getPos(), destination));
13+
vecType vi = currentActor->getVelocity();
1214

1315
vecType personalImpulse = mi * (((v0i * e0i) - vi) / Ti);
14-
16+
17+
// Calculate forces applied by neighbouring actors
1518
vecType peopleForces = {0, 0};
1619
for (int x = 0; x < actors.size(); x++) {
17-
Actor j = actors[x];
18-
if (z != x && !j.getAtDestination()) {
19-
float rij = j.getRadius() + i->getRadius();
20-
float dij = magnitude(pos - j.getPos());
21-
vecType nij = (pos - j.getPos()) / dij;
22-
vecType tij = {-nij[1], nij[0]};
20+
Actor neighbour = actors[x];
21+
if (currentIndex != x && !neighbour.getAtDestination()) {
22+
float rij = neighbour.getRadius() + currentActor->getRadius();
23+
vecType currentToNeighbour = pos - neighbour.getPos();
24+
float dij = magnitude(currentToNeighbour);
25+
vecType nij = (currentToNeighbour) / dij;
26+
vecType tij = getTangentialVector(nij);
2327
float g = dij > rij ? 0 : rij - dij;
24-
float deltavtij = dotProduct((j.getVelocity() - i->getVelocity()), tij);
28+
float deltavtij = dotProduct((neighbour.getVelocity() - currentActor->getVelocity()), tij);
2529

26-
peopleForces += (Ai * exp((rij - dij) / Bi) + K1 * g) * nij + (K2 * g * deltavtij * tij);
30+
peopleForces += (Ai * sycl::exp((rij - dij) / Bi) + K1 * g) * nij + (K2 * g * deltavtij * tij);
2731
}
2832
}
2933

34+
// Calculate forces applied by walls
3035
vecType wallForces = {0, 0};
3136
for (int x = 0; x < walls.size(); x++) {
32-
std::array<vecType, 2> w = walls[x];
33-
float ri = i->getRadius();
34-
std::pair<float, vecType> dAndn = getDistanceAndNiw(pos, w);
37+
std::array<vecType, 2> currentWall = walls[x];
38+
float ri = currentActor->getRadius();
39+
std::pair<float, vecType> dAndn = getDistanceAndNiw(pos, currentWall);
3540
float diw = dAndn.first;
3641
float g = diw > ri ? 0 : ri - diw;
3742
vecType niw = dAndn.second;
38-
vecType tiw = {-niw[1], niw[0]};
43+
vecType tiw = getTangentialVector(niw);
3944

40-
wallForces += (Ai * exp((ri - diw) / Bi) + K1 * g) * niw - (K2 * g * dotProduct(vi, tiw) * tiw);
45+
wallForces += (Ai * sycl::exp((ri - diw) / Bi) + K1 * g) * niw - (K2 * g * dotProduct(vi, tiw) * tiw);
4146
}
4247

4348
vecType forceSum = personalImpulse + peopleForces + wallForces;
44-
vecType acceleration = forceSum / mi;
4549

46-
//out << "People Forces: (" << peopleForces[0] << ", " << peopleForces[1] << ") " << z << sycl::endl;
47-
//out << "Wall Forces: (" << wallForces[0] << ", " << wallForces[1] << ") " << z << sycl::endl;
48-
//out << "Acceleration: (" << acceleration[0] << ", " << acceleration[1] << ") " << z << sycl::endl;
49-
//out << "-----------------------" << sycl::endl;
50+
// Apply random force variations
51+
forceSum += currentActor->getVariation();
52+
53+
// Color actor according to heatmap
54+
if (currentActor->getHeatmapEnabled()) {
55+
auto colorVal = sycl::fabs((forceSum[0] + forceSum[1]) / 700.0f);
56+
if (colorVal > 1) { colorVal = 1.0f; }
57+
auto color = findColor(colorVal);
58+
currentActor->setColor({int(color[0]), int(color[1]), int(color[2])});
59+
}
5060

51-
i->setVelocity(vi + forceSum * 0.001);
52-
i->setPos(pos + i->getVelocity() * 0.001);
61+
// out << "People Forces: (" << peopleForces[0] << ", " << peopleForces[1] << ") " << z << sycl::endl;
62+
// out << "Wall Forces: (" << wallForces[0] << ", " << wallForces[1] << ") " << z << sycl::endl;
63+
// out << "Acceleration: (" << acceleration[0] << ", " << acceleration[1] << ") " << z << sycl::endl;
64+
// out << "-----------------------" << sycl::endl;
5365

54-
vecType newPos = i->getPos();
55-
vecType destination = i->getDestination();
66+
vecType acceleration = forceSum / mi;
67+
currentActor->setVelocity(vi + acceleration * TIMESTEP);
68+
currentActor->setPos(pos + currentActor->getVelocity() * TIMESTEP);
5669

57-
std::array<float, 4> destinationBoundingBox = {destination[0] + 0.01f, destination[0] - 0.01f, destination[1] + 0.01f, destination[1] - 0.01f};
58-
if (newPos[0] <= destinationBoundingBox[0] && newPos[0] >= destinationBoundingBox[1]
59-
&& pos[1] <= destinationBoundingBox[2] && pos[1] >= destinationBoundingBox[3]) {
60-
i->setAtDestination(true);
61-
}
70+
currentActor->checkAtDestination(destination, paths[currentActor->getPathId()].getPathSize());
6271
}

external/DifferentialEq.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@
77
#include "Actor.hpp"
88
#include "MathHelper.hpp"
99
#include "VectorMaths.hpp"
10+
#include "Heatmap.hpp"
11+
#include "Path.hpp"
1012

1113
constexpr float Ai = 200;
1214
constexpr float Bi = 0.08;
1315
constexpr float K1 = 125000;
1416
constexpr float K2 = 240000;
1517
constexpr float Ti = 0.5;
1618

17-
using vecType = std::array<float, 2>;
19+
constexpr float TIMESTEP = 0.001;
1820

19-
SYCL_EXTERNAL void differentialEq(int x, sycl::accessor<Actor, 1, sycl::access::mode::read_write> actors, sycl::accessor<std::array<vecType, 2>, 1, sycl::access::mode::read> walls, sycl::stream out);
21+
SYCL_EXTERNAL void differentialEq(int currentIndex, sycl::accessor<Actor, 1, sycl::access::mode::read_write> actors, sycl::accessor<std::array<vecType, 2>, 1, sycl::access::mode::read> walls, sycl::accessor<Path, 1, sycl::access::mode::read> paths, sycl::stream out);
2022

2123
#endif

external/Heatmap.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "Heatmap.hpp"
2+
3+
SYCL_EXTERNAL std::array<float, 3> HSVtoRGB(float h, float s, float v) {
4+
float c = v * s;
5+
double scaledH = sycl::fmod(h / 60.0, 6.0);
6+
float x = c * (1 - sycl::fabs(sycl::fmod(scaledH, 2.0) - 1));
7+
float m = v - c;
8+
9+
std::array<float, 3> rgb = {};
10+
11+
if (0 <= scaledH && scaledH < 1) { rgb[0] = c; rgb[1] = x; rgb[2] = 0; }
12+
else if (1 <= scaledH && scaledH < 2) { rgb[0] = x; rgb[1] = c; rgb[2] = 0; }
13+
else if (2 <= scaledH && scaledH < 3) { rgb[0] = 0; rgb[1] = c; rgb[2] = x; }
14+
else if (3 <= scaledH && scaledH < 4) { rgb[0] = 0; rgb[1] = x; rgb[2] = c; }
15+
else if (4 <= scaledH && scaledH < 5) { rgb[0] = x; rgb[1] = 0; rgb[2] = c; }
16+
else if (5 <= scaledH && scaledH < 6) { rgb[0] = c; rgb[1] = 0; rgb[2] = x; }
17+
else { rgb[0] = 0; rgb[1] = 0; rgb[2] = 0; }
18+
19+
return rgb;
20+
}
21+
22+
SYCL_EXTERNAL std::array<float, 3> findColor(float val) {
23+
float h = (1.0f - val) * 120.0f;
24+
return HSVtoRGB(h, 100, 50);
25+
}

external/Heatmap.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef Heatmap_hpp
2+
#define Heatmap_hpp
3+
4+
#include <iostream>
5+
#include <array>
6+
#include <vector>
7+
#include <sycl/sycl.hpp>
8+
9+
SYCL_EXTERNAL std::array<float, 3> HSVtoRGB(float h, float s, float v);
10+
11+
SYCL_EXTERNAL std::array<float, 3> findColor(float val);
12+
13+
#endif

external/MathHelper.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,11 @@ SYCL_EXTERNAL vecType velToPoint(float speed, vecType pos, vecType destination)
1313
}
1414

1515
SYCL_EXTERNAL float magnitude(vecType inp) {
16-
return std::sqrt((inp[0] * inp[0]) + (inp[1] * inp[1]));
16+
return sycl::sqrt((inp[0] * inp[0]) + (inp[1] * inp[1]));
1717
}
1818

1919
SYCL_EXTERNAL float inverseMagnitude(vecType inp) {
20-
float x = (inp[0] * inp[0]) + (inp[1] * inp[1]);
21-
float y = x;
22-
uint32_t *i = reinterpret_cast<uint32_t *>(&y);
23-
const uint32_t expMask = 0x7F800000;
24-
const uint32_t magicNumber = 0x5F000000;
25-
*i = magicNumber - ((*i >> 1) & expMask);
26-
return (x * y * y + 1) / (2 * x * y);
20+
return sycl::rsqrt((inp[0] * inp[0]) + (inp[1] * inp[1]));
2721
}
2822

2923
SYCL_EXTERNAL float dotProduct(vecType a, vecType b) {
@@ -57,10 +51,14 @@ SYCL_EXTERNAL std::pair<float, vecType> getDistanceAndNiw(vecType point, std::ar
5751
float lSquared = dotProduct(AB, AB);
5852
if (lSquared == 0.0) {
5953
return {0, {0, 0}};
60-
}
61-
float t = std::max(0.0f, std::min(1.0f, dotProduct(AP, AB) / lSquared));
54+
}
55+
float t = sycl::max(0.0f, sycl::min(1.0f, dotProduct(AP, AB) / lSquared));
6256
auto projection = t * AB;
6357

6458
return {distance(AP, projection), normalize(AP - projection)};
6559
}
6660
}
61+
62+
SYCL_EXTERNAL vecType getTangentialVector(vecType normal) {
63+
return {-normal[1], normal[0]};
64+
}

external/MathHelper.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include <sycl/sycl.hpp>
77
#include "VectorMaths.hpp"
88

9-
using vecType = std::array<float, 2>;
109

1110
SYCL_EXTERNAL vecType getDirectionVector(vecType a, vecType b);
1211

@@ -26,4 +25,6 @@ SYCL_EXTERNAL vecType normalize(vecType inp);
2625

2726
SYCL_EXTERNAL std::pair<float, vecType> getDistanceAndNiw(vecType point, std::array<vecType, 2> wall);
2827

28+
SYCL_EXTERNAL vecType getTangentialVector(vecType normal);
29+
2930
#endif

0 commit comments

Comments
 (0)