Skip to content

Commit 370355e

Browse files
committed
Removed paths as members of Actors and replaced with Path class
1 parent ced3c60 commit 370355e

15 files changed

+2641
-14713
lines changed

external/Actor.cpp

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

3-
Actor::Actor(vecType pPos, vecType pVelocity, float pDesiredSpeed, std::array<vecType, PATHALLOCATIONSIZE> pPath, int pPathSize, float pMass, float pRadius, bool pAtDestination, std::array<int, 3> pColor, bool pHeatmapEnabled):
3+
Actor::Actor(vecType pPos, vecType pVelocity, float pDesiredSpeed, int pPathId, float pMass, float pRadius, bool pAtDestination, std::array<int, 3> pColor, bool pHeatmapEnabled):
44
pos(pPos), velocity(pVelocity), desiredSpeed(pDesiredSpeed),
5-
path(pPath), pathSize(pPathSize), mass(pMass), radius(pRadius),
5+
pathId(pPathId), mass(pMass), radius(pRadius),
66
atDestination(pAtDestination), color(pColor), heatmapEnabled(pHeatmapEnabled) {
77
variation = {0, 0};
88
destinationIndex = 0;
@@ -20,12 +20,12 @@ SYCL_EXTERNAL float Actor::getDesiredSpeed() const {
2020
return desiredSpeed;
2121
}
2222

23-
SYCL_EXTERNAL std::array<vecType, PATHALLOCATIONSIZE> Actor::getPath() const {
24-
return path;
23+
SYCL_EXTERNAL int Actor::getPathId() const {
24+
return pathId;
2525
}
2626

27-
SYCL_EXTERNAL vecType Actor::getDestination() const {
28-
return path[destinationIndex];
27+
SYCL_EXTERNAL int Actor::getDestinationIndex() const {
28+
return destinationIndex;
2929
}
3030

3131
SYCL_EXTERNAL vecType Actor::getVariation() const {
@@ -64,10 +64,6 @@ SYCL_EXTERNAL void Actor::setDesiredSpeed(float newDesiredSpeed) {
6464
desiredSpeed = newDesiredSpeed;
6565
}
6666

67-
SYCL_EXTERNAL void Actor::setPath(std::array<vecType, PATHALLOCATIONSIZE> newPath) {
68-
path = newPath;
69-
}
70-
7167
SYCL_EXTERNAL void Actor::setVariation(vecType newVariation) {
7268
variation = newVariation;
7369
}
@@ -80,11 +76,11 @@ SYCL_EXTERNAL void Actor::setColor(std::array<int, 3> newColor) {
8076
color = newColor;
8177
}
8278

83-
SYCL_EXTERNAL void Actor::checkAtDestination() {
84-
std::array<float, 4> destinationBoundingBox = {path[destinationIndex][0] + 0.2f,
85-
path[destinationIndex][0] - 0.2f,
86-
path[destinationIndex][1] + 0.2f,
87-
path[destinationIndex][1] - 0.2f};
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};
8884
if (pos[0] <= destinationBoundingBox[0] && pos[0] >= destinationBoundingBox[1]
8985
&& pos[1] <= destinationBoundingBox[2] && pos[1] >= destinationBoundingBox[3]) {
9086
if (destinationIndex >= PATHALLOCATIONSIZE - 1 || destinationIndex >= pathSize - 1) {

external/Actor.hpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@
77
#include <vector>
88
#include <sycl/sycl.hpp>
99
#include "VectorMaths.hpp"
10-
11-
const int PATHALLOCATIONSIZE = 10;
10+
#include "Path.hpp"
1211

1312
class Actor {
1413
private:
1514
vecType pos;
1615
vecType velocity;
1716
float desiredSpeed;
18-
std::array<vecType, PATHALLOCATIONSIZE> path;
19-
int pathSize;
17+
int pathId;
2018
int destinationIndex;
2119
vecType variation;
2220
float mass;
@@ -25,14 +23,14 @@ class Actor {
2523
std::array<int, 3> color;
2624
bool heatmapEnabled;
2725

28-
public:
29-
Actor(vecType pPos, vecType pVelocity, float pdesiredSpeed, std::array<vecType, PATHALLOCATIONSIZE> pPath, int pathSize, float pMass, float pRadius, bool pAtDestination, std::array<int, 3> pColor, bool pHeatmapEnabled);
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);
3028

3129
SYCL_EXTERNAL vecType getPos() const;
3230
SYCL_EXTERNAL vecType getVelocity() const;
3331
SYCL_EXTERNAL float getDesiredSpeed() const;
34-
SYCL_EXTERNAL std::array<vecType, PATHALLOCATIONSIZE> getPath() const;
35-
SYCL_EXTERNAL vecType getDestination() const;
32+
SYCL_EXTERNAL int getPathId() const;
33+
SYCL_EXTERNAL int getDestinationIndex() const;
3634
SYCL_EXTERNAL vecType getVariation() const;
3735
SYCL_EXTERNAL float getMass() const;
3836
SYCL_EXTERNAL float getRadius() const;
@@ -43,12 +41,11 @@ class Actor {
4341
SYCL_EXTERNAL void setPos(vecType newPos);
4442
SYCL_EXTERNAL void setVelocity(vecType newVelocity);
4543
SYCL_EXTERNAL void setDesiredSpeed(float newDesiredSpeed);
46-
SYCL_EXTERNAL void setPath(std::array<vecType, PATHALLOCATIONSIZE> newPath);
4744
SYCL_EXTERNAL void setVariation(vecType newVariation);
4845
SYCL_EXTERNAL void setAtDestination(bool param);
4946
SYCL_EXTERNAL void setColor(std::array<int, 3> newColor);
5047

51-
SYCL_EXTERNAL void checkAtDestination();
48+
SYCL_EXTERNAL void checkAtDestination(vecType destination, int pathSize);
5249
void refreshVariation();
5350
};
5451

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 Heatmap.cpp ParseInputFile.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: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
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::stream out) {
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) {
44
Actor* currentActor = &actors[currentIndex];
55

66
vecType pos = currentActor->getPos();
77

88
float mi = currentActor->getMass();
99
float v0i = currentActor->getDesiredSpeed();
10-
vecType e0i = normalize(getDirectionVector(currentActor->getPos(), currentActor->getDestination()));
10+
vecType destination = paths[currentActor->getPathId()].getCheckpoints()[currentActor->getDestinationIndex()];
11+
vecType e0i = normalize(getDirectionVector(currentActor->getPos(), destination));
1112
vecType vi = currentActor->getVelocity();
1213

1314
vecType personalImpulse = mi * (((v0i * e0i) - vi) / Ti);
1415

16+
out << pos[0] << ", " << pos[1] << " -> " << destination[0] << ", " << destination[1] << sycl::endl;
17+
1518
vecType peopleForces = {0, 0};
1619
for (int x = 0; x < actors.size(); x++) {
1720
Actor neighbour = actors[x];
@@ -61,5 +64,5 @@ SYCL_EXTERNAL void differentialEq(int currentIndex, sycl::accessor<Actor, 1, syc
6164
currentActor->setVelocity(vi + acceleration * TIMESTEP);
6265
currentActor->setPos(pos + currentActor->getVelocity() * TIMESTEP);
6366

64-
currentActor->checkAtDestination();
67+
currentActor->checkAtDestination(destination, paths[currentActor->getPathId()].getPathSize());
6568
}

external/DifferentialEq.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "MathHelper.hpp"
99
#include "VectorMaths.hpp"
1010
#include "Heatmap.hpp"
11+
#include "Path.hpp"
1112

1213
constexpr float Ai = 200;
1314
constexpr float Bi = 0.08;
@@ -17,6 +18,6 @@ constexpr float Ti = 0.5;
1718

1819
constexpr float TIMESTEP = 0.001;
1920

20-
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::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);
2122

2223
#endif

external/MathHelper.hpp

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

9+
910
SYCL_EXTERNAL vecType getDirectionVector(vecType a, vecType b);
1011

1112
SYCL_EXTERNAL vecType velFromSpeedAndDir(float speed, vecType direction);

external/ParseInputFile.cpp

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ void validateParameters(rapidjson::Document& jsonDoc) {
66
if (!jsonDoc.HasMember("config")) missingParameters += "config ";
77
if (!jsonDoc.HasMember("room")) missingParameters += "room ";
88
if (!jsonDoc.HasMember("actors")) missingParameters += "actors ";
9+
if (!jsonDoc.HasMember("paths")) missingParameters += "paths ";
910

1011
if (missingParameters != "") {
1112
throw JSONException("Missing these parameters: " + missingParameters);
@@ -21,25 +22,28 @@ void validateParameters(rapidjson::Document& jsonDoc) {
2122

2223
if (!jsonDoc["room"].HasMember("walls")) missingParameters += "walls ";
2324

24-
if (jsonDoc["actors"].GetArray().Size() > 0) {
25-
auto actorParams = {
26-
"pos", "velocity", "desiredSpeed", "path",
27-
"mass", "radius", "heatmapEnabled"
28-
};
29-
for (auto& a : jsonDoc["actors"].GetArray()) {
30-
for (auto p : actorParams) {
31-
if (!a.HasMember(p)) missingParameters += std::string(p) + " ";
32-
}
25+
auto actorParams = {
26+
"pos", "velocity", "desiredSpeed", "pathId", "mass",
27+
"radius", "atDestination", "color", "heatmapEnabled"
28+
};
29+
for (auto& a : jsonDoc["actors"].GetArray()) {
30+
for (auto p : actorParams) {
31+
if (!a.HasMember(p)) missingParameters += std::string(p) + " ";
3332
}
3433
}
34+
35+
for (auto& p : jsonDoc["paths"].GetArray()) {
36+
if (!p.HasMember("id")) missingParameters += "id ";
37+
if (!p.HasMember("checkpoints")) missingParameters += "checkpoints ";
38+
}
3539

3640
if (missingParameters != "") {
3741
throw JSONException("Missing these parameters: " + missingParameters);
3842
}
3943
}
4044
}
4145

42-
void parseInputFile(std::string filename, std::vector<Actor> &actors, Room &room, int &WIDTH, int &HEIGHT, int &SCALE, int &DELAY) {
46+
void parseInputFile(std::string filename, std::vector<Actor> &actors, Room &room, std::vector<Path> &paths, int &WIDTH, int &HEIGHT, int &SCALE, int &DELAY) {
4347
std::ifstream jsonFile(filename);
4448
if (!jsonFile.is_open()) {
4549
throw JSONException("Error opening file " + filename);
@@ -68,20 +72,27 @@ void parseInputFile(std::string filename, std::vector<Actor> &actors, Room &room
6872
}
6973
room.setWalls(walls);
7074

75+
// Paths
76+
auto jsonPaths = jsonDoc["paths"].GetArray();
77+
for (auto& p : jsonPaths) {
78+
int id = p["id"].GetInt();
79+
auto jsonCheckpoints = p["checkpoints"].GetArray();
80+
std::array<vecType, PATHALLOCATIONSIZE> checkpoints;
81+
for (int i = 0; i < jsonCheckpoints.Size(); i++) {
82+
checkpoints[i] = vecType({jsonCheckpoints[i][0].GetFloat(), jsonCheckpoints[i][1].GetFloat()});
83+
}
84+
int pathSize = jsonCheckpoints.Size();
85+
paths.push_back(Path(id, checkpoints, pathSize));
86+
}
87+
7188
// Actor
7289
auto jsonActors = jsonDoc["actors"].GetArray();
7390
for (auto& a : jsonActors) {
7491
vecType pos = {a["pos"][0].GetFloat(), a["pos"][1].GetFloat()};
7592
vecType velocity = {a["velocity"][0].GetFloat(), a["velocity"][1].GetFloat()};
7693
float desiredSpeed = a["desiredSpeed"].GetFloat();
77-
78-
auto jsonPath = a["path"].GetArray();
79-
std::array<vecType, PATHALLOCATIONSIZE> path;
80-
for (int x = 0; x < jsonPath.Size(); x++) {
81-
path[x] = {jsonPath[x][0].GetFloat(), jsonPath[x][1].GetFloat()};
82-
}
83-
84-
int pathSize = jsonPath.Size();
94+
int pathId = a["pathId"].GetInt();
95+
int pathSize = paths[pathId].getPathSize();
8596
int mass = a["mass"].GetInt();
8697
float radius = a["radius"].GetFloat();
8798
bool atDestination = a["atDestination"].GetBool();
@@ -91,6 +102,6 @@ void parseInputFile(std::string filename, std::vector<Actor> &actors, Room &room
91102

92103
bool heatmapEnabled = a["heatmapEnabled"].GetBool();
93104

94-
actors.push_back(Actor(pos, velocity, desiredSpeed, path, pathSize, mass, radius, atDestination, color, heatmapEnabled));
105+
actors.push_back(Actor(pos, velocity, desiredSpeed, pathId, mass, radius, atDestination, color, heatmapEnabled));
95106
}
96107
}

external/ParseInputFile.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "VectorMaths.hpp"
1313
#include "Actor.hpp"
1414
#include "Room.hpp"
15+
#include "Path.hpp"
1516

1617
class JSONException : public std::runtime_error {
1718
public:
@@ -20,6 +21,6 @@ class JSONException : public std::runtime_error {
2021

2122
void validateParameters(rapidjson::Document& jsonDoc);
2223

23-
void parseInputFile(std::string filename, std::vector<Actor> &actors, Room &room, int &WIDTH, int &HEIGHT, int &SCALE, int &DELAY);
24+
void parseInputFile(std::string filename, std::vector<Actor> &actors, Room &room, std::vector<Path> &paths, int &WIDTH, int &HEIGHT, int &SCALE, int &DELAY);
2425

2526
#endif

external/Path.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "Path.hpp"
2+
3+
Path::Path(int pId, std::array<vecType, PATHALLOCATIONSIZE> pCheckpoints, int pPathSize) : id(pId), checkpoints(pCheckpoints), pathSize(pPathSize) {}
4+
5+
SYCL_EXTERNAL int Path::getId() const {
6+
return id;
7+
}
8+
9+
SYCL_EXTERNAL std::array<vecType, PATHALLOCATIONSIZE> Path::getCheckpoints() const {
10+
return checkpoints;
11+
}
12+
13+
SYCL_EXTERNAL int Path::getPathSize() const {
14+
return pathSize;
15+
}
16+
17+
void Path::setId(int newId) {
18+
id = newId;
19+
}
20+
21+
void Path::setCheckpoints(std::array<vecType, PATHALLOCATIONSIZE> newCheckpoints) {
22+
checkpoints = newCheckpoints;
23+
}

external/Path.hpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef Path_hpp
2+
#define Path_hpp
3+
4+
#include <iostream>
5+
#include <array>
6+
#include <sycl/sycl.hpp>
7+
#include "VectorMaths.hpp"
8+
9+
const int PATHALLOCATIONSIZE = 10;
10+
11+
class Path {
12+
private:
13+
int id;
14+
std::array<vecType, PATHALLOCATIONSIZE> checkpoints;
15+
int pathSize;
16+
public:
17+
Path(int pId, std::array<vecType, PATHALLOCATIONSIZE> pCheckpoints, int pPathSize);
18+
19+
SYCL_EXTERNAL int getId() const;
20+
SYCL_EXTERNAL std::array<vecType, PATHALLOCATIONSIZE> getCheckpoints() const;
21+
SYCL_EXTERNAL int getPathSize() const;
22+
23+
void setId(int newId);
24+
void setCheckpoints(std::array<vecType, PATHALLOCATIONSIZE> newCheckpoints);
25+
};
26+
27+
#endif

0 commit comments

Comments
 (0)