Skip to content

Commit 4e07622

Browse files
author
Sami Hatna
committed
Merge branch 'phase3' into 'main'
Phase3 Code Review See merge request sami.hatna/crowd-simulation!4
2 parents d85ee34 + a144a27 commit 4e07622

24 files changed

+916
-33058
lines changed

external/Actor.cpp

Lines changed: 48 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,43 @@
11
#include "Actor.hpp"
22

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-
}
3+
Actor::Actor(vecType pPos, vecType pVelocity, float pDesiredSpeed, int pPathId,
4+
float pMass, float pRadius, bool pAtDestination,
5+
std::array<int, 3> pColor, bool pHeatmapEnabled)
6+
: pos(pPos), velocity(pVelocity), desiredSpeed(pDesiredSpeed),
7+
pathId(pPathId), mass(pMass), radius(pRadius),
8+
atDestination(pAtDestination), color(pColor),
9+
heatmapEnabled(pHeatmapEnabled), variation({0, 0}), destinationIndex(0),
10+
bBox({0, 0}) {}
1011

11-
SYCL_EXTERNAL vecType Actor::getPos() const {
12-
return pos;
13-
}
12+
SYCL_EXTERNAL vecType Actor::getPos() const { return pos; }
1413

15-
SYCL_EXTERNAL vecType Actor::getVelocity() const {
16-
return velocity;
17-
}
14+
SYCL_EXTERNAL vecType Actor::getVelocity() const { return velocity; }
1815

19-
SYCL_EXTERNAL float Actor::getDesiredSpeed() const {
20-
return desiredSpeed;
21-
}
16+
SYCL_EXTERNAL float Actor::getDesiredSpeed() const { return desiredSpeed; }
2217

23-
SYCL_EXTERNAL int Actor::getPathId() const {
24-
return pathId;
25-
}
18+
SYCL_EXTERNAL int Actor::getPathId() const { return pathId; }
2619

2720
SYCL_EXTERNAL int Actor::getDestinationIndex() const {
2821
return destinationIndex;
2922
}
3023

31-
SYCL_EXTERNAL vecType Actor::getVariation() const {
32-
return variation;
33-
}
24+
SYCL_EXTERNAL vecType Actor::getVariation() const { return variation; }
3425

35-
SYCL_EXTERNAL float Actor::getMass() const {
36-
return mass;
37-
}
26+
SYCL_EXTERNAL float Actor::getMass() const { return mass; }
3827

39-
SYCL_EXTERNAL float Actor::getRadius() const {
40-
return radius;
41-
}
28+
SYCL_EXTERNAL float Actor::getRadius() const { return radius; }
4229

43-
SYCL_EXTERNAL bool Actor::getAtDestination() const {
44-
return atDestination;
45-
}
30+
SYCL_EXTERNAL bool Actor::getAtDestination() const { return atDestination; }
4631

47-
SYCL_EXTERNAL std::array<int, 3> Actor::getColor() const {
48-
return color;
49-
}
32+
SYCL_EXTERNAL std::array<int, 3> Actor::getColor() const { return color; }
5033

51-
SYCL_EXTERNAL bool Actor::getHeatmapEnabled() const {
52-
return heatmapEnabled;
53-
}
34+
SYCL_EXTERNAL bool Actor::getHeatmapEnabled() const { return heatmapEnabled; }
5435

55-
SYCL_EXTERNAL void Actor::setPos(vecType newPos) {
56-
pos = newPos;
57-
}
36+
SYCL_EXTERNAL std::array<int, 2> Actor::getBBox() const { return bBox; }
37+
38+
SYCL_EXTERNAL uint Actor::getSeed() const { return seed; }
39+
40+
SYCL_EXTERNAL void Actor::setPos(vecType newPos) { pos = newPos; }
5841

5942
SYCL_EXTERNAL void Actor::setVelocity(vecType newVelocity) {
6043
velocity = newVelocity;
@@ -76,25 +59,31 @@ SYCL_EXTERNAL void Actor::setColor(std::array<int, 3> newColor) {
7659
color = newColor;
7760
}
7861

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-
}
62+
SYCL_EXTERNAL void Actor::setBBox(std::array<int, 2> newBBox) {
63+
bBox = newBBox;
9364
}
9465

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)});
66+
SYCL_EXTERNAL void Actor::setSeed(uint newSeed) { seed = newSeed; }
67+
68+
SYCL_EXTERNAL void Actor::refreshVariations() {
69+
// Previous RNG output is used as next seed
70+
seed = randXorShift(seed);
71+
float randX = (float(seed) * (2.0f / 4294967296.0f)) - 1.0f;
72+
seed = randXorShift(seed);
73+
float randY = (float(seed) * (2.0f / 4294967296.0f)) - 1.0f;
74+
this->setVariation({randX, randY});
75+
}
76+
77+
SYCL_EXTERNAL void Actor::checkAtDestination(std::array<vecType, 4> destination,
78+
int pathSize) {
79+
// Destinations are defined as rectangular regions
80+
if (pos[0] >= destination[0][0] && pos[0] <= destination[2][0] &&
81+
pos[1] >= destination[0][1] && pos[1] <= destination[2][1]) {
82+
if (destinationIndex >= PATHALLOCATIONSIZE - 1 ||
83+
destinationIndex >= pathSize - 1) {
84+
this->setAtDestination(true);
85+
} else {
86+
destinationIndex++;
87+
}
88+
}
10089
}

external/Actor.hpp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
#ifndef Actor_hpp
22
#define Actor_hpp
33

4-
#include <iostream>
4+
#include "Path.hpp"
5+
#include "VectorMaths.hpp"
6+
#include "RandomNumber.hpp"
57
#include <array>
8+
#include <iostream>
69
#include <random>
7-
#include <vector>
810
#include <sycl/sycl.hpp>
9-
#include "VectorMaths.hpp"
10-
#include "Path.hpp"
11+
#include <vector>
1112

1213
class Actor {
13-
private:
14+
private:
1415
vecType pos;
1516
vecType velocity;
1617
float desiredSpeed;
@@ -22,10 +23,14 @@ class Actor {
2223
bool atDestination;
2324
std::array<int, 3> color;
2425
bool heatmapEnabled;
26+
std::array<int, 2> bBox;
27+
uint seed;
28+
29+
public:
30+
Actor(vecType pPos, vecType pVelocity, float pdesiredSpeed, int pPathId,
31+
float pMass, float pRadius, bool pAtDestination,
32+
std::array<int, 3> pColor, bool pHeatmapEnabled);
2533

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);
28-
2934
SYCL_EXTERNAL vecType getPos() const;
3035
SYCL_EXTERNAL vecType getVelocity() const;
3136
SYCL_EXTERNAL float getDesiredSpeed() const;
@@ -37,16 +42,22 @@ class Actor {
3742
SYCL_EXTERNAL bool getAtDestination() const;
3843
SYCL_EXTERNAL std::array<int, 3> getColor() const;
3944
SYCL_EXTERNAL bool getHeatmapEnabled() const;
45+
SYCL_EXTERNAL std::array<int, 2> getBBox() const;
46+
SYCL_EXTERNAL uint getSeed() const;
4047

4148
SYCL_EXTERNAL void setPos(vecType newPos);
4249
SYCL_EXTERNAL void setVelocity(vecType newVelocity);
4350
SYCL_EXTERNAL void setDesiredSpeed(float newDesiredSpeed);
4451
SYCL_EXTERNAL void setVariation(vecType newVariation);
4552
SYCL_EXTERNAL void setAtDestination(bool param);
4653
SYCL_EXTERNAL void setColor(std::array<int, 3> newColor);
54+
SYCL_EXTERNAL void setBBox(std::array<int, 2> newBBox);
55+
SYCL_EXTERNAL void setSeed(uint newSeed);
56+
57+
SYCL_EXTERNAL void refreshVariations();
4758

48-
SYCL_EXTERNAL void checkAtDestination(vecType destination, int pathSize);
49-
void refreshVariation();
59+
SYCL_EXTERNAL void checkAtDestination(std::array<vecType, 4> destination,
60+
int pathSize);
5061
};
5162

5263
#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 Path.cpp MathHelper.cpp DifferentialEq.cpp VectorMaths.cpp Heatmap.cpp ParseInputFile.cpp)
1+
add_library(external Actor.cpp Room.cpp Path.cpp MathHelper.cpp DifferentialEq.cpp VectorMaths.cpp Heatmap.cpp ParseInputFile.cpp RandomNumber.cpp)

external/DifferentialEq.cpp

Lines changed: 69 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,78 @@
11
#include "DifferentialEq.hpp"
22

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];
3+
SYCL_EXTERNAL void differentialEq(
4+
int actorIndex,
5+
sycl::accessor<Actor, 1, sycl::access::mode::read_write> actors,
6+
sycl::accessor<std::array<vecType, 2>, 1, sycl::access::mode::read> walls,
7+
sycl::accessor<Path, 1, sycl::access::mode::read> paths, sycl::stream out) {
8+
Actor *currentActor = &actors[actorIndex];
59

610
vecType pos = currentActor->getPos();
711

812
// Calculate personal impulse
913
float mi = currentActor->getMass();
1014
float v0i = currentActor->getDesiredSpeed();
11-
vecType destination = paths[currentActor->getPathId()].getCheckpoints()[currentActor->getDestinationIndex()];
12-
vecType e0i = normalize(getDirectionVector(currentActor->getPos(), destination));
15+
std::array<vecType, 4> destination =
16+
paths[currentActor->getPathId()]
17+
.getCheckpoints()[currentActor->getDestinationIndex()];
18+
19+
// Find direction vector to nearest point in destination region
20+
std::pair<float, vecType> minRegionDistance;
21+
for (int x = 0; x < 4; x++) {
22+
int endIndex = x == 3 ? 0 : x + 1;
23+
auto dniw = getDistanceAndNiw(currentActor->getPos(),
24+
{destination[x], destination[endIndex]});
25+
if (dniw.first < minRegionDistance.first ||
26+
minRegionDistance.first == 0) {
27+
minRegionDistance = dniw;
28+
}
29+
}
30+
minRegionDistance.second = normalize(minRegionDistance.second);
31+
vecType e0i = {-minRegionDistance.second[0], -minRegionDistance.second[1]};
32+
1333
vecType vi = currentActor->getVelocity();
1434

1535
vecType personalImpulse = mi * (((v0i * e0i) - vi) / Ti);
16-
17-
// Calculate forces applied by neighbouring actors
36+
37+
// Collect neighbouring bounding boxes
38+
std::array<int, 2> currentBBox = currentActor->getBBox();
39+
std::array<std::array<int, 2>, 9> neighbourBoxes = {{
40+
{currentBBox},
41+
{currentBBox[0] - 1, currentBBox[1] - 1},
42+
{currentBBox[0] - 1, currentBBox[1]},
43+
{currentBBox[0] - 1, currentBBox[1] + 1},
44+
{currentBBox[0], currentBBox[1] + 1},
45+
{currentBBox[0], currentBBox[1] - 1},
46+
{currentBBox[0] + 1, currentBBox[1] - 1},
47+
{currentBBox[0] + 1, currentBBox[1]},
48+
{currentBBox[0] + 1, currentBBox[1] + 1},
49+
}};
50+
51+
// Calculate forces applied by neighbouring actors (in valid bounding boxes)
1852
vecType peopleForces = {0, 0};
1953
for (int x = 0; x < actors.size(); x++) {
2054
Actor neighbour = actors[x];
21-
if (currentIndex != x && !neighbour.getAtDestination()) {
22-
float rij = neighbour.getRadius() + currentActor->getRadius();
55+
56+
bool bBoxFlag =
57+
std::any_of(neighbourBoxes.begin(), neighbourBoxes.end(),
58+
[neighbour](std::array<int, 2> i) {
59+
return i[0] == neighbour.getBBox()[0] &&
60+
i[1] == neighbour.getBBox()[1];
61+
});
62+
63+
if (actorIndex != x && !neighbour.getAtDestination() && bBoxFlag) {
2364
vecType currentToNeighbour = pos - neighbour.getPos();
2465
float dij = magnitude(currentToNeighbour);
66+
float rij = neighbour.getRadius() + currentActor->getRadius();
2567
vecType nij = (currentToNeighbour) / dij;
2668
vecType tij = getTangentialVector(nij);
2769
float g = dij > rij ? 0 : rij - dij;
28-
float deltavtij = dotProduct((neighbour.getVelocity() - currentActor->getVelocity()), tij);
70+
float deltavtij = dotProduct(
71+
(neighbour.getVelocity() - currentActor->getVelocity()), tij);
2972

30-
peopleForces += (Ai * sycl::exp((rij - dij) / Bi) + K1 * g) * nij + (K2 * g * deltavtij * tij);
73+
peopleForces +=
74+
(PEOPLEAi * sycl::exp((rij - dij) / Bi) + K1 * g) * nij +
75+
(K2 * g * deltavtij * tij);
3176
}
3277
}
3378

@@ -39,10 +84,11 @@ SYCL_EXTERNAL void differentialEq(int currentIndex, sycl::accessor<Actor, 1, syc
3984
std::pair<float, vecType> dAndn = getDistanceAndNiw(pos, currentWall);
4085
float diw = dAndn.first;
4186
float g = diw > ri ? 0 : ri - diw;
42-
vecType niw = dAndn.second;
87+
vecType niw = normalize(dAndn.second);
4388
vecType tiw = getTangentialVector(niw);
4489

45-
wallForces += (Ai * sycl::exp((ri - diw) / Bi) + K1 * g) * niw - (K2 * g * dotProduct(vi, tiw) * tiw);
90+
wallForces += (WALLAi * sycl::exp((ri - diw) / Bi) + K1 * g) * niw -
91+
(K2 * g * dotProduct(vi, tiw) * tiw);
4692
}
4793

4894
vecType forceSum = personalImpulse + peopleForces + wallForces;
@@ -52,20 +98,23 @@ SYCL_EXTERNAL void differentialEq(int currentIndex, sycl::accessor<Actor, 1, syc
5298

5399
// Color actor according to heatmap
54100
if (currentActor->getHeatmapEnabled()) {
55-
auto colorVal = sycl::fabs((forceSum[0] + forceSum[1]) / 700.0f);
56-
if (colorVal > 1) { colorVal = 1.0f; }
101+
// theoreticalMax was decided based on observed max forces for a set of
102+
// configurations It may need to be altered based on the max forces of
103+
// your config to create a satisfying heatmap
104+
float theoreticalMax = 700.0f;
105+
auto colorVal =
106+
sycl::fabs((forceSum[0] + forceSum[1]) / theoreticalMax);
107+
if (colorVal > 1) {
108+
colorVal = 1.0f;
109+
}
57110
auto color = findColor(colorVal);
58111
currentActor->setColor({int(color[0]), int(color[1]), int(color[2])});
59112
}
60113

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;
65-
66114
vecType acceleration = forceSum / mi;
67115
currentActor->setVelocity(vi + acceleration * TIMESTEP);
68116
currentActor->setPos(pos + currentActor->getVelocity() * TIMESTEP);
69117

70-
currentActor->checkAtDestination(destination, paths[currentActor->getPathId()].getPathSize());
118+
currentActor->checkAtDestination(
119+
destination, paths[currentActor->getPathId()].getPathSize());
71120
}

external/DifferentialEq.hpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,36 @@
11
#ifndef DifferentialEqu_hpp
22
#define DifferentialEqu_hpp
33

4-
#include <iostream>
5-
#include <array>
6-
#include <sycl/sycl.hpp>
74
#include "Actor.hpp"
8-
#include "MathHelper.hpp"
9-
#include "VectorMaths.hpp"
105
#include "Heatmap.hpp"
6+
#include "MathHelper.hpp"
117
#include "Path.hpp"
8+
#include "VectorMaths.hpp"
9+
#include <algorithm>
10+
#include <array>
11+
#include <iostream>
12+
#include <sycl/sycl.hpp>
1213

13-
constexpr float Ai = 200;
14+
// Constant for scaling inter-actor repulsive force
15+
constexpr float PEOPLEAi = 500;
16+
// Constant for scaling actor-wall repulsive force
17+
constexpr float WALLAi = 1300;
18+
// Reproduces distance kept at normal desired velocities
1419
constexpr float Bi = 0.08;
20+
// Obstruction effect
1521
constexpr float K1 = 125000;
22+
// Obstruction effect
1623
constexpr float K2 = 240000;
24+
// Characteristic time
1725
constexpr float Ti = 0.5;
1826

27+
// Integration timestep
1928
constexpr float TIMESTEP = 0.001;
2029

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);
30+
SYCL_EXTERNAL void differentialEq(
31+
int actorIndex,
32+
sycl::accessor<Actor, 1, sycl::access::mode::read_write> actors,
33+
sycl::accessor<std::array<vecType, 2>, 1, sycl::access::mode::read> walls,
34+
sycl::accessor<Path, 1, sycl::access::mode::read> paths, sycl::stream out);
2235

2336
#endif

0 commit comments

Comments
 (0)