Skip to content

Commit 0151de9

Browse files
author
Sami Hatna
committed
Merge branch 'Tadej-Review' into 'main'
Code Review See merge request sami.hatna/crowd-simulation!11
2 parents dd78644 + 11fae57 commit 0151de9

22 files changed

+207
-209
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@ Below is an annotated example input file which creates a room containing two act
6262
"scale": 100,
6363
"delay": 0,
6464
"bgColor": [0, 0, 0],
65-
"wallColor": [255, 255, 255]
66-
},
67-
65+
"wallColor": [255, 255, 255],
66+
"heatmapEnabled": true <-- Flag denoting whether
67+
}, the heatmap should be
68+
applied to actors
6869
"room": {
6970
"walls": [
7071
[0.5, 0.5, 8.5, 0.5], <-- Walls are defined via their
@@ -84,9 +85,9 @@ Below is an annotated example input file which creates a room containing two act
8485
"radius": 0.05,
8586
"atDestination": false,
8687
"color": [255, 0, 0],
87-
"heatmapEnabled": true <-- Flag denoting whether the
88-
}, actor's colour should change
89-
{ with the heatmap
88+
"heatmapEnabled": true
89+
},
90+
{
9091
"pos": [0.7, 7.3],
9192
"velocity": [0.0789, 0.0444],
9293
"desiredSpeed": 0.6,

external/Actor.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,22 @@
2020
*
2121
* Description:
2222
* Class denoting an actor in social force model
23-
*
23+
*
2424
**************************************************************************/
2525

2626
#include "Actor.hpp"
2727

28-
Actor::Actor(vecType pPos, vecType pVelocity, float pDesiredSpeed, int pPathId,
29-
float pMass, float pRadius, bool pAtDestination,
30-
std::array<int, 3> pColor, bool pHeatmapEnabled)
28+
Actor::Actor(sycl::float2 pPos, sycl::float2 pVelocity, float pDesiredSpeed,
29+
int pPathId, float pMass, float pRadius, bool pAtDestination,
30+
std::array<int, 3> pColor)
3131
: pos(pPos), velocity(pVelocity), desiredSpeed(pDesiredSpeed),
3232
pathId(pPathId), mass(pMass), radius(pRadius),
33-
atDestination(pAtDestination), color(pColor),
34-
heatmapEnabled(pHeatmapEnabled), destinationIndex(0), bBox({0, 0}) {}
33+
atDestination(pAtDestination), color(pColor), destinationIndex(0),
34+
bBox({0, 0}) {}
3535

36-
SYCL_EXTERNAL vecType Actor::getPos() const { return pos; }
36+
SYCL_EXTERNAL sycl::float2 Actor::getPos() const { return pos; }
3737

38-
SYCL_EXTERNAL vecType Actor::getVelocity() const { return velocity; }
38+
SYCL_EXTERNAL sycl::float2 Actor::getVelocity() const { return velocity; }
3939

4040
SYCL_EXTERNAL float Actor::getDesiredSpeed() const { return desiredSpeed; }
4141

@@ -53,17 +53,15 @@ SYCL_EXTERNAL bool Actor::getAtDestination() const { return atDestination; }
5353

5454
SYCL_EXTERNAL std::array<int, 3> Actor::getColor() const { return color; }
5555

56-
SYCL_EXTERNAL bool Actor::getHeatmapEnabled() const { return heatmapEnabled; }
57-
5856
SYCL_EXTERNAL std::array<int, 2> Actor::getBBox() const { return bBox; }
5957

6058
SYCL_EXTERNAL uint Actor::getSeed() const { return seed; }
6159

6260
SYCL_EXTERNAL float Actor::getForce() const { return force; }
6361

64-
SYCL_EXTERNAL void Actor::setPos(vecType newPos) { pos = newPos; }
62+
SYCL_EXTERNAL void Actor::setPos(sycl::float2 newPos) { pos = newPos; }
6563

66-
SYCL_EXTERNAL void Actor::setVelocity(vecType newVelocity) {
64+
SYCL_EXTERNAL void Actor::setVelocity(sycl::float2 newVelocity) {
6765
velocity = newVelocity;
6866
}
6967

@@ -87,8 +85,9 @@ SYCL_EXTERNAL void Actor::setSeed(uint newSeed) { seed = newSeed; }
8785

8886
SYCL_EXTERNAL void Actor::setForce(float newForce) { force = newForce; }
8987

90-
SYCL_EXTERNAL void Actor::checkAtDestination(std::array<vecType, 2> destination,
91-
int pathSize) {
88+
SYCL_EXTERNAL void
89+
Actor::checkAtDestination(std::array<sycl::float2, 2> destination,
90+
int pathSize) {
9291
// Destinations are defined as rectangular regions
9392
if (pos[0] >= destination[0][0] && pos[0] <= destination[1][0] &&
9493
pos[1] >= destination[0][1] && pos[1] <= destination[1][1]) {

external/Actor.hpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@
2020
*
2121
* Description:
2222
* Class denoting an actor in social force model
23-
*
23+
*
2424
**************************************************************************/
2525

2626
#ifndef Actor_hpp
2727
#define Actor_hpp
2828

2929
#include "Path.hpp"
3030
#include "RandomNumber.hpp"
31-
#include "VectorMaths.hpp"
3231
#include <array>
3332
#include <iostream>
3433
#include <random>
@@ -37,50 +36,48 @@
3736

3837
class Actor {
3938
private:
40-
vecType pos;
41-
vecType velocity;
39+
std::array<int, 3> color;
40+
sycl::float2 pos;
41+
sycl::float2 velocity;
42+
std::array<int, 2> bBox;
4243
float desiredSpeed;
4344
int pathId;
4445
int destinationIndex;
4546
float mass;
4647
float radius;
47-
bool atDestination;
48-
std::array<int, 3> color;
49-
bool heatmapEnabled;
50-
std::array<int, 2> bBox;
5148
uint seed;
5249
float force;
50+
bool atDestination;
5351

5452
public:
55-
Actor(vecType pPos, vecType pVelocity, float pdesiredSpeed, int pPathId,
56-
float pMass, float pRadius, bool pAtDestination,
57-
std::array<int, 3> pColor, bool pHeatmapEnabled);
53+
Actor(sycl::float2 pPos, sycl::float2 pVelocity, float pdesiredSpeed,
54+
int pPathId, float pMass, float pRadius, bool pAtDestination,
55+
std::array<int, 3> pColor);
5856

59-
SYCL_EXTERNAL vecType getPos() const;
60-
SYCL_EXTERNAL vecType getVelocity() const;
57+
SYCL_EXTERNAL sycl::float2 getPos() const;
58+
SYCL_EXTERNAL sycl::float2 getVelocity() const;
6159
SYCL_EXTERNAL float getDesiredSpeed() const;
6260
SYCL_EXTERNAL int getPathId() const;
6361
SYCL_EXTERNAL int getDestinationIndex() const;
6462
SYCL_EXTERNAL float getMass() const;
6563
SYCL_EXTERNAL float getRadius() const;
6664
SYCL_EXTERNAL bool getAtDestination() const;
6765
SYCL_EXTERNAL std::array<int, 3> getColor() const;
68-
SYCL_EXTERNAL bool getHeatmapEnabled() const;
6966
SYCL_EXTERNAL std::array<int, 2> getBBox() const;
7067
SYCL_EXTERNAL uint getSeed() const;
7168
SYCL_EXTERNAL float getForce() const;
7269

73-
SYCL_EXTERNAL void setPos(vecType newPos);
74-
SYCL_EXTERNAL void setVelocity(vecType newVelocity);
70+
SYCL_EXTERNAL void setPos(sycl::float2 newPos);
71+
SYCL_EXTERNAL void setVelocity(sycl::float2 newVelocity);
7572
SYCL_EXTERNAL void setDesiredSpeed(float newDesiredSpeed);
7673
SYCL_EXTERNAL void setAtDestination(bool param);
7774
SYCL_EXTERNAL void setColor(std::array<int, 3> newColor);
7875
SYCL_EXTERNAL void setBBox(std::array<int, 2> newBBox);
7976
SYCL_EXTERNAL void setSeed(uint newSeed);
8077
SYCL_EXTERNAL void setForce(float newForce);
8178

82-
SYCL_EXTERNAL void checkAtDestination(std::array<vecType, 2> destination,
83-
int pathSize);
79+
SYCL_EXTERNAL void
80+
checkAtDestination(std::array<sycl::float2, 2> destination, int pathSize);
8481
};
8582

8683
#endif

external/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ add_library(external Actor.cpp
55
Path.cpp
66
MathHelper.cpp
77
DifferentialEq.cpp
8-
VectorMaths.cpp
98
Heatmap.cpp
109
ParseInputFile.cpp
1110
RandomNumber.cpp

external/DifferentialEq.cpp

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,32 @@
2020
*
2121
* Description:
2222
* Kernel for calculating social forces
23-
*
23+
*
2424
**************************************************************************/
2525

2626
#include "DifferentialEq.hpp"
2727

2828
SYCL_EXTERNAL void differentialEq(
2929
int actorIndex,
3030
sycl::accessor<Actor, 1, sycl::access::mode::read_write> actors,
31-
sycl::accessor<std::array<vecType, 2>, 1, sycl::access::mode::read> walls,
32-
sycl::accessor<Path, 1, sycl::access::mode::read> paths) {
31+
sycl::accessor<std::array<sycl::float2, 2>, 1, sycl::access::mode::read>
32+
walls,
33+
sycl::accessor<Path, 1, sycl::access::mode::read> paths,
34+
sycl::accessor<bool, 1, sycl::access::mode::read> heatmapEnabled) {
3335
Actor *currentActor = &actors[actorIndex];
3436

35-
vecType pos = currentActor->getPos();
37+
sycl::float2 pos = currentActor->getPos();
3638

3739
// Calculate personal impulse
3840
float mi = currentActor->getMass();
3941
float v0i = currentActor->getDesiredSpeed();
40-
std::array<vecType, 2> destination =
42+
std::array<sycl::float2, 2> destination =
4143
paths[currentActor->getPathId()]
4244
.getCheckpoints()[currentActor->getDestinationIndex()];
4345

4446
// Find direction vector to nearest point in destination region
45-
std::pair<float, vecType> minRegionDistance;
46-
std::array<vecType, 4> destinationRect = {
47+
std::pair<float, sycl::float2> minRegionDistance;
48+
std::array<sycl::float2, 4> destinationRect = {
4749
destination[0],
4850
{destination[1][0], destination[0][1]},
4951
destination[1],
@@ -59,11 +61,12 @@ SYCL_EXTERNAL void differentialEq(
5961
}
6062
}
6163
minRegionDistance.second = normalize(minRegionDistance.second);
62-
vecType e0i = {-minRegionDistance.second[0], -minRegionDistance.second[1]};
64+
sycl::float2 e0i = {-minRegionDistance.second[0],
65+
-minRegionDistance.second[1]};
6366

64-
vecType vi = currentActor->getVelocity();
67+
sycl::float2 vi = currentActor->getVelocity();
6568

66-
vecType personalImpulse = mi * (((v0i * e0i) - vi) / Ti);
69+
sycl::float2 personalImpulse = mi * (((v0i * e0i) - vi) / Ti);
6770

6871
// Collect neighbouring bounding boxes
6972
std::array<int, 2> currentBBox = currentActor->getBBox();
@@ -80,7 +83,7 @@ SYCL_EXTERNAL void differentialEq(
8083
}};
8184

8285
// Calculate forces applied by neighbouring actors (in valid bounding boxes)
83-
vecType peopleForces = {0, 0};
86+
sycl::float2 peopleForces = {0, 0};
8487
for (int x = 0; x < actors.size(); x++) {
8588
Actor neighbour = actors[x];
8689

@@ -92,11 +95,11 @@ SYCL_EXTERNAL void differentialEq(
9295
});
9396

9497
if (actorIndex != x && !neighbour.getAtDestination() && bBoxFlag) {
95-
vecType currentToNeighbour = pos - neighbour.getPos();
98+
sycl::float2 currentToNeighbour = pos - neighbour.getPos();
9699
float dij = magnitude(currentToNeighbour);
97100
float rij = neighbour.getRadius() + currentActor->getRadius();
98-
vecType nij = (currentToNeighbour) / dij;
99-
vecType tij = getTangentialVector(nij);
101+
sycl::float2 nij = currentToNeighbour / dij;
102+
sycl::float2 tij = getTangentialVector(nij);
100103
float g = dij > rij ? 0 : rij - dij;
101104
float deltavtij = dotProduct(
102105
(neighbour.getVelocity() - currentActor->getVelocity()), tij);
@@ -108,21 +111,22 @@ SYCL_EXTERNAL void differentialEq(
108111
}
109112

110113
// Calculate forces applied by walls
111-
vecType wallForces = {0, 0};
114+
sycl::float2 wallForces = {0, 0};
112115
for (int x = 0; x < walls.size(); x++) {
113-
std::array<vecType, 2> currentWall = walls[x];
116+
std::array<sycl::float2, 2> currentWall = walls[x];
114117
float ri = currentActor->getRadius();
115-
std::pair<float, vecType> dAndn = getDistanceAndNiw(pos, currentWall);
118+
std::pair<float, sycl::float2> dAndn =
119+
getDistanceAndNiw(pos, currentWall);
116120
float diw = dAndn.first;
117121
float g = diw > ri ? 0 : ri - diw;
118-
vecType niw = normalize(dAndn.second);
119-
vecType tiw = getTangentialVector(niw);
122+
sycl::float2 niw = normalize(dAndn.second);
123+
sycl::float2 tiw = getTangentialVector(niw);
120124

121125
wallForces += (WALLAi * sycl::exp((ri - diw) / Bi) + K1 * g) * niw -
122126
(K2 * g * dotProduct(vi, tiw) * tiw);
123127
}
124128

125-
vecType forceSum = personalImpulse + peopleForces + wallForces;
129+
sycl::float2 forceSum = personalImpulse + peopleForces + wallForces;
126130

127131
#ifdef STATS
128132
if (!currentActor->getAtDestination()) {
@@ -138,7 +142,7 @@ SYCL_EXTERNAL void differentialEq(
138142
currentActor->setSeed(randXorShift(seed));
139143

140144
// Color actor according to heatmap
141-
if (currentActor->getHeatmapEnabled()) {
145+
if (heatmapEnabled[0]) {
142146
// theoreticalMax was decided based on observed max forces for a set of
143147
// configurations It may need to be altered based on the max forces of
144148
// your config to create a satisfying heatmap
@@ -153,7 +157,7 @@ SYCL_EXTERNAL void differentialEq(
153157
}
154158

155159
// Perform integration
156-
vecType acceleration = forceSum / mi;
160+
sycl::float2 acceleration = forceSum / mi;
157161
currentActor->setVelocity(vi + acceleration * TIMESTEP);
158162
currentActor->setPos(pos + currentActor->getVelocity() * TIMESTEP);
159163

external/DifferentialEq.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*
2121
* Description:
2222
* Kernel for calculating social forces
23-
*
23+
*
2424
**************************************************************************/
2525

2626
#ifndef DifferentialEqu_hpp
@@ -30,7 +30,6 @@
3030
#include "Heatmap.hpp"
3131
#include "MathHelper.hpp"
3232
#include "Path.hpp"
33-
#include "VectorMaths.hpp"
3433
#include <algorithm>
3534
#include <array>
3635
#include <iostream>
@@ -55,7 +54,9 @@ constexpr float TIMESTEP = 0.001;
5554
SYCL_EXTERNAL void differentialEq(
5655
int actorIndex,
5756
sycl::accessor<Actor, 1, sycl::access::mode::read_write> actors,
58-
sycl::accessor<std::array<vecType, 2>, 1, sycl::access::mode::read> walls,
59-
sycl::accessor<Path, 1, sycl::access::mode::read> paths);
57+
sycl::accessor<std::array<sycl::float2, 2>, 1, sycl::access::mode::read>
58+
walls,
59+
sycl::accessor<Path, 1, sycl::access::mode::read> paths,
60+
sycl::accessor<bool, 1, sycl::access::mode::read> heatmapEnabled);
6061

6162
#endif

external/Heatmap.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* Description:
2222
* Applying heatmap across actors reflecting how much force they are
2323
* experiencing
24-
*
24+
*
2525
**************************************************************************/
2626

2727
#include "Heatmap.hpp"

external/Heatmap.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* Description:
2222
* Applying heatmap across actors reflecting how much force they are
2323
* experiencing
24-
*
24+
*
2525
**************************************************************************/
2626

2727
#ifndef Heatmap_hpp

0 commit comments

Comments
 (0)