Skip to content

Commit b9383e4

Browse files
author
Sami Hatna
committed
Merge branch 'phase1' into 'main'
Phase1 Code Review See merge request sami.hatna/crowd-simulation!2
2 parents 7588e34 + 160f605 commit b9383e4

23 files changed

+499
-212
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
/.vscode
2-
/build
2+
/build

CMakeLists.txt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ include_directories(${SDL2_INCLUDE_DIRS})
1414
add_executable(crowdsim src/main.cpp)
1515
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsycl")
1616

17-
add_subdirectory(include)
18-
19-
target_link_libraries(crowdsim PUBLIC ${SDL2_LIBRARIES} include)
20-
target_include_directories(crowdsim PUBLIC "${PROJECT_BINARY_DIR}" "${PROJECT_SOURCE_DIR}/include")
21-
17+
add_subdirectory(external)
2218

19+
target_link_libraries(crowdsim PUBLIC ${SDL2_LIBRARIES} external)
20+
target_include_directories(crowdsim PUBLIC "${PROJECT_BINARY_DIR}" "${PROJECT_SOURCE_DIR}/external")

external/Actor.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include "Actor.hpp"
2+
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+
}
13+
14+
SYCL_EXTERNAL vecType Actor::getPos() const {
15+
return pos;
16+
}
17+
18+
SYCL_EXTERNAL vecType Actor::getVelocity() const {
19+
return velocity;
20+
}
21+
22+
SYCL_EXTERNAL vecType Actor::getDesiredVelocity() const {
23+
return desiredVelocity;
24+
}
25+
26+
SYCL_EXTERNAL vecType Actor::getDestination() const {
27+
return destination;
28+
}
29+
30+
SYCL_EXTERNAL float Actor::getMass() const {
31+
return mass;
32+
}
33+
34+
SYCL_EXTERNAL float Actor::getRadius() const {
35+
return radius;
36+
}
37+
38+
SYCL_EXTERNAL bool Actor::getAtDestination() const {
39+
return atDestination;
40+
}
41+
42+
SYCL_EXTERNAL std::array<int, 3> Actor::getColor() const {
43+
return color;
44+
}
45+
46+
SYCL_EXTERNAL void Actor::setPos(vecType newPos) {
47+
pos = newPos;
48+
}
49+
50+
SYCL_EXTERNAL void Actor::setVelocity(vecType newVelocity) {
51+
velocity = newVelocity;
52+
}
53+
54+
SYCL_EXTERNAL void Actor::setDesiredVelocity(vecType newDesiredVelocity) {
55+
desiredVelocity = newDesiredVelocity;
56+
}
57+
58+
SYCL_EXTERNAL void Actor::setDestination(vecType newDestination) {
59+
destination = newDestination;
60+
}
61+
62+
SYCL_EXTERNAL void Actor::setAtDestination(bool param) {
63+
atDestination = param;
64+
}

external/Actor.hpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef Actor_hpp
2+
#define Actor_hpp
3+
4+
#include <iostream>
5+
#include <array>
6+
#include <sycl/sycl.hpp>
7+
#include "VectorMaths.hpp"
8+
9+
using vecType = std::array<float, 2>;
10+
11+
class Actor {
12+
private:
13+
vecType pos;
14+
vecType velocity;
15+
vecType desiredVelocity;
16+
vecType destination;
17+
float mass;
18+
float radius;
19+
bool atDestination;
20+
std::array<int, 3> color;
21+
22+
public:
23+
Actor(vecType pPos, vecType pVelocity, vecType pDesiredVelocity, vecType pDestination, float pMass, float pRadius, bool pAtDestination, std::array<int, 3> pColor);
24+
25+
SYCL_EXTERNAL vecType getPos() const;
26+
SYCL_EXTERNAL vecType getVelocity() const;
27+
SYCL_EXTERNAL vecType getDesiredVelocity() const;
28+
SYCL_EXTERNAL vecType getDestination() const;
29+
SYCL_EXTERNAL float getMass() const;
30+
SYCL_EXTERNAL float getRadius() const;
31+
SYCL_EXTERNAL bool getAtDestination() const;
32+
SYCL_EXTERNAL std::array<int, 3> getColor() const;
33+
34+
SYCL_EXTERNAL void setPos(vecType newPos);
35+
SYCL_EXTERNAL void setVelocity(vecType newVelocity);
36+
SYCL_EXTERNAL void setDesiredVelocity(vecType newDesiredVelocity);
37+
SYCL_EXTERNAL void setDestination(vecType newDestination);
38+
SYCL_EXTERNAL void setAtDestination(bool param);
39+
};
40+
41+
#endif

external/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_library(external Actor.cpp Room.cpp MathHelper.cpp DifferentialEq.cpp VectorMaths.cpp)

external/DifferentialEq.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include "DifferentialEq.hpp"
2+
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];
5+
6+
vecType pos = i->getPos();
7+
8+
float mi = i->getMass();
9+
float v0i = 2.0f;
10+
vecType e0i = normalize(getDirectionVector(i->getPos(), i->getDestination()));
11+
vecType vi = i->getVelocity();
12+
13+
vecType personalImpulse = mi * (((v0i * e0i) - vi) / Ti);
14+
15+
vecType peopleForces = {0, 0};
16+
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]};
23+
float g = dij > rij ? 0 : rij - dij;
24+
float deltavtij = dotProduct((j.getVelocity() - i->getVelocity()), tij);
25+
26+
peopleForces += (Ai * exp((rij - dij) / Bi) + K1 * g) * nij + (K2 * g * deltavtij * tij);
27+
}
28+
}
29+
30+
vecType wallForces = {0, 0};
31+
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);
35+
float diw = dAndn.first;
36+
float g = diw > ri ? 0 : ri - diw;
37+
vecType niw = dAndn.second;
38+
vecType tiw = {-niw[1], niw[0]};
39+
40+
wallForces += (Ai * exp((ri - diw) / Bi) + K1 * g) * niw - (K2 * g * dotProduct(vi, tiw) * tiw);
41+
}
42+
43+
vecType forceSum = personalImpulse + peopleForces + wallForces;
44+
vecType acceleration = forceSum / mi;
45+
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+
51+
i->setVelocity(vi + forceSum * 0.001);
52+
i->setPos(pos + i->getVelocity() * 0.001);
53+
54+
vecType newPos = i->getPos();
55+
vecType destination = i->getDestination();
56+
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+
}
62+
}

external/DifferentialEq.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef DifferentialEqu_hpp
2+
#define DifferentialEqu_hpp
3+
4+
#include <iostream>
5+
#include <array>
6+
#include <sycl/sycl.hpp>
7+
#include "Actor.hpp"
8+
#include "MathHelper.hpp"
9+
#include "VectorMaths.hpp"
10+
11+
constexpr float Ai = 200;
12+
constexpr float Bi = 0.08;
13+
constexpr float K1 = 125000;
14+
constexpr float K2 = 240000;
15+
constexpr float Ti = 0.5;
16+
17+
using vecType = std::array<float, 2>;
18+
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);
20+
21+
#endif

external/MathHelper.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include "MathHelper.hpp"
2+
3+
SYCL_EXTERNAL vecType getDirectionVector(vecType from, vecType to) {
4+
return to - from;
5+
}
6+
7+
SYCL_EXTERNAL vecType velFromSpeedAndDir(float speed, vecType direction) {
8+
return direction * (speed * inverseMagnitude(direction));
9+
}
10+
11+
SYCL_EXTERNAL vecType velToPoint(float speed, vecType pos, vecType destination) {
12+
return velFromSpeedAndDir(speed, getDirectionVector(pos, destination));
13+
}
14+
15+
SYCL_EXTERNAL float magnitude(vecType inp) {
16+
return std::sqrt((inp[0] * inp[0]) + (inp[1] * inp[1]));
17+
}
18+
19+
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);
27+
}
28+
29+
SYCL_EXTERNAL float dotProduct(vecType a, vecType b) {
30+
return (a[0] * b[0]) + (a[1] * b[1]);
31+
}
32+
33+
SYCL_EXTERNAL float distance(vecType from, vecType to) {
34+
return magnitude(from - to);
35+
}
36+
37+
SYCL_EXTERNAL vecType normalize(vecType inp) {
38+
return inp * inverseMagnitude(inp);
39+
}
40+
41+
SYCL_EXTERNAL std::pair<float, vecType> getDistanceAndNiw(vecType point, std::array<vecType, 2> wall) {
42+
vecType AB = getDirectionVector(wall[0], wall[1]);
43+
vecType BP = getDirectionVector(wall[1], point);
44+
vecType AP = getDirectionVector(wall[0], point);
45+
46+
float ABdotBP = dotProduct(AB, BP);
47+
float ABdotAP = dotProduct(AB, AP);
48+
49+
if (ABdotBP >= 0) {
50+
return {distance(point, wall[1]), normalize(BP)};
51+
}
52+
else if (ABdotAP < 0) {
53+
return {distance(point, wall[0]), normalize(AP)};
54+
}
55+
else {
56+
// float lSquared = pow(magnitude(AB), 2);
57+
float lSquared = dotProduct(AB, AB);
58+
if (lSquared == 0.0) {
59+
return {0, {0, 0}};
60+
}
61+
float t = std::max(0.0f, std::min(1.0f, dotProduct(AP, AB) / lSquared));
62+
auto projection = t * AB;
63+
64+
return {distance(AP, projection), normalize(AP - projection)};
65+
}
66+
}

external/MathHelper.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef MathHelper_hpp
2+
#define MathHelper_hpp
3+
4+
#include <iostream>
5+
#include <array>
6+
#include <sycl/sycl.hpp>
7+
#include "VectorMaths.hpp"
8+
9+
using vecType = std::array<float, 2>;
10+
11+
SYCL_EXTERNAL vecType getDirectionVector(vecType a, vecType b);
12+
13+
SYCL_EXTERNAL vecType velFromSpeedAndDir(float speed, vecType direction);
14+
15+
SYCL_EXTERNAL vecType velToPoint(float speed, vecType pos, vecType destination);
16+
17+
SYCL_EXTERNAL float magnitude(vecType inp);
18+
19+
SYCL_EXTERNAL float inverseMagnitude(vecType inp);
20+
21+
SYCL_EXTERNAL float dotProduct(vecType a, vecType b);
22+
23+
SYCL_EXTERNAL float distance(vecType a, vecType b);
24+
25+
SYCL_EXTERNAL vecType normalize(vecType inp);
26+
27+
SYCL_EXTERNAL std::pair<float, vecType> getDistanceAndNiw(vecType point, std::array<vecType, 2> wall);
28+
29+
#endif

external/Room.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include "Room.hpp"
2+
3+
Room::Room(std::vector<std::array<vecType, 2>> pWalls) {
4+
walls = pWalls;
5+
}
6+
7+
std::vector<std::array<vecType, 2>> Room::getWalls() const {
8+
return walls;
9+
}

0 commit comments

Comments
 (0)