Skip to content

Commit 7588e34

Browse files
author
Sami Hatna
committed
Merge branch 'sandbox' into 'main'
Merge sandbox into main See merge request sami.hatna/crowd-simulation!1
2 parents 790fde9 + d80856a commit 7588e34

File tree

10 files changed

+204
-14
lines changed

10 files changed

+204
-14
lines changed

include/Actor.cpp

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,53 @@
11
#include "Actor.hpp"
22
#include <iostream>
33
#include <array>
4+
#include <sycl/sycl.hpp>
45

5-
Actor::Actor(std::array<float, 2> pPos, std::array<float, 2> pVelocity, std::array<float, 2> pDesiredVelocity, float pMass, float pRadius) {
6+
Actor::Actor(std::array<float, 2> pPos, std::array<float, 2> pVelocity, std::array<float, 2> pDesiredVelocity, std::array<float, 2> pDestination, float pMass, float pRadius) {
67
pos = pPos;
78
velocity = pVelocity;
89
desiredVelocity = pDesiredVelocity;
10+
destination = pDestination;
911
mass = pMass;
1012
radius = pRadius;
13+
}
14+
15+
std::array<float, 2> Actor::getPos() {
16+
return pos;
17+
}
18+
19+
SYCL_EXTERNAL std::array<float, 2> Actor::getVelocity() {
20+
return velocity;
21+
}
22+
23+
std::array<float, 2> Actor::getDesiredVelocity() {
24+
return desiredVelocity;
25+
}
26+
27+
std::array<float, 2> Actor::getDestination() {
28+
return destination;
29+
}
30+
31+
float Actor::getMass() {
32+
return mass;
33+
}
34+
35+
float Actor::getRadius() {
36+
return radius;
37+
}
38+
39+
void Actor::setPos(std::array<float, 2> newPos) {
40+
pos = newPos;
41+
}
42+
43+
void Actor::setVelocity(std::array<float, 2> newVelocity) {
44+
velocity = newVelocity;
45+
}
46+
47+
void Actor::setDesiredVelocity(std::array<float, 2> newDesiredVelocity) {
48+
desiredVelocity = newDesiredVelocity;
49+
}
50+
51+
void Actor::setDestination(std::array<float, 2> newDestination) {
52+
destination = newDestination;
1153
}

include/Actor.hpp

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

4-
#include<iostream>
4+
#include <iostream>
55
#include <array>
6+
#include <sycl/sycl.hpp>
67

78
class Actor {
89
private:
910
std::array<float, 2> pos;
1011
std::array<float, 2> velocity;
1112
std::array<float, 2> desiredVelocity;
13+
std::array<float, 2> destination;
1214
float mass;
1315
float radius;
1416
public:
15-
Actor(std::array<float, 2> pPos, std::array<float, 2> pVelocity, std::array<float, 2> pDesiredVelocity, float pMass, float pRadius);
17+
Actor(std::array<float, 2> pPos, std::array<float, 2> pVelocity, std::array<float, 2> pDesiredVelocity, std::array<float, 2> pDestination, float pMass, float pRadius);
18+
19+
SYCL_EXTERNAL std::array<float, 2> getPos();
20+
SYCL_EXTERNAL std::array<float, 2> getVelocity();
21+
std::array<float, 2> getDesiredVelocity();
22+
std::array<float, 2> getDestination();
23+
float getMass();
24+
float getRadius();
25+
26+
SYCL_EXTERNAL void setPos(std::array<float, 2> newPos);
27+
void setVelocity(std::array<float, 2> newVelocity);
28+
void setDesiredVelocity(std::array<float, 2> newDesiredVelocity);
29+
void setDestination(std::array<float, 2> newDestination);
1630
};
1731

1832
#endif

include/CMakeLists.txt

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

include/DifferentialEq.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <array>
4+
#include "DifferentialEq.hpp"
5+
#include "Actor.hpp"
6+
#include "Room.hpp"
7+
#include "MathHelper.hpp"
8+
9+
void DifferentialEqu(Actor actor);

include/DifferentialEq.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef DifferentialEqu_hpp
2+
#define DifferentialEqu_hpp
3+
4+
#include <iostream>
5+
#include <vector>
6+
#include <array>
7+
#include "Actor.hpp"
8+
#include "Room.hpp"
9+
#include "MathHelper.hpp"
10+
11+
const float Ai = 2000;
12+
const float Bi = 0.08;
13+
const float k1 = 125000;
14+
const float k2 = 240000;
15+
const float ti = 0.5;
16+
17+
void DifferentialEqu(Actor actor);
18+
19+
#endif

include/MathHelper.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "MathHelper.hpp"
2+
#include <iostream>
3+
#include <vector>
4+
#include <array>
5+
#include <cmath>
6+
7+
std::array<float, 2> getDirectionVector(std::array<float, 2> a, std::array<float, 2> b) {
8+
return {float(b[0] - a[0]), float(b[1] - a[1])};
9+
}
10+
11+
std::array<float, 2> velFromSpeedAndDir(float s, std::array<float, 2> d) {
12+
return {(s / magnitude(d)) * d[0], (s / magnitude(d)) * d[1]};
13+
}
14+
15+
std::array<float, 2> velToPoint(float s, std::array<float, 2> pos, std::array<float, 2> destination) {
16+
return velFromSpeedAndDir(s, getDirectionVector(pos, destination));
17+
}
18+
19+
float magnitude(std::array<float, 2> inp) {
20+
return std::sqrt((inp[0] * inp[0]) + (inp[1] * inp[1]));
21+
}

include/MathHelper.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef MathHelper_hpp
2+
#define MathHelper_hpp
3+
4+
#include <iostream>
5+
#include <vector>
6+
#include <array>
7+
8+
std::array<float, 2> getDirectionVector(std::array<float, 2> a, std::array<float, 2> b);
9+
10+
std::array<float, 2> velFromSpeedAndDir(float s, std::array<float, 2> d);
11+
12+
std::array<float, 2> velToPoint(float s, std::array<float, 2> pos, std::array<float, 2> destination);
13+
14+
float magnitude(std::array<float, 2> inp);
15+
16+
#endif

include/Room.cpp

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

include/Room.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef Room_hpp
2+
#define Room_hpp
3+
4+
#include <iostream>
5+
#include <vector>
6+
#include <array>
7+
8+
class Room {
9+
private:
10+
std::vector<std::array<float, 4>> walls;
11+
public:
12+
Room(std::vector<std::array<float, 4>> pWalls);
13+
14+
std::vector<std::array<float, 4>> getWalls();
15+
};
16+
17+
#endif

src/main.cpp

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
1-
#include <CL/sycl.hpp>
1+
#include <sycl/sycl.hpp>
22
#include <SDL2/SDL.h>
3-
#include "Actor.hpp"
43
#include <iostream>
54

5+
#include "Actor.hpp"
6+
#include "Room.hpp"
7+
#include "MathHelper.hpp"
8+
#include "DifferentialEq.hpp"
9+
610
const int WIDTH = 8; // metres
711
const int HEIGHT = 6; // metres
812
const int SCALE = 100;
13+
const int DELAY = 10000;
914

10-
void init(SDL_Window* win, SDL_Renderer* render, std::vector<Actor> &actors) {
15+
void init(SDL_Window* &win, SDL_Renderer* &render, std::vector<Actor> &actors) {
1116
SDL_Init(SDL_INIT_VIDEO);
1217
win = SDL_CreateWindow("SYCL Crowd Simulation", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH * SCALE, HEIGHT * SCALE, SDL_WINDOW_SHOWN);
1318
render = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
1419

15-
actors.push_back(Actor{{1,2}, {3,4}, {5,6}, 3, 4});
20+
actors.push_back(Actor{{4, 0}, {0.01, 0.01}, {0.02, 0.02}, {1, 2}, 50, 0.05});
21+
actors.push_back(Actor{{8, 6}, {-0.02, -0.02}, {-0.03, -0.03}, {1, 2}, 60, 0.08});
1622
}
1723

18-
void drawCircle(SDL_Renderer* render, SDL_Point center, int radius, SDL_Color color) {
24+
void drawCircle(SDL_Renderer* &render, SDL_Point center, int radius, SDL_Color color) {
1925
SDL_SetRenderDrawColor(render, color.r, color.g, color.b, color.a);
2026
for (int w = 0; w < radius * 2; w++) {
2127
for (int h = 0; h < radius * 2; h++) {
@@ -28,11 +34,36 @@ void drawCircle(SDL_Renderer* render, SDL_Point center, int radius, SDL_Color co
2834
}
2935
}
3036

31-
void draw(SDL_Renderer* render, std::vector<Actor> &actors) {
37+
void update(std::vector<Actor> &actors) {
38+
sycl::queue myQueue{sycl::gpu_selector()};
39+
40+
auto actorBuf = sycl::buffer<Actor>(actors.data(), actors.size());
41+
42+
myQueue.submit([&](sycl::handler& cgh) {
43+
auto actorAcc = actorBuf.get_access<sycl::access::mode::read_write>(cgh);
44+
45+
cgh.parallel_for(sycl::range<1>{actors.size()}, [=](sycl::id<1> index) {
46+
Actor current = actorAcc[index];
47+
actorAcc[index].setPos({current.getPos()[0] + current.getVelocity()[0],
48+
current.getPos()[1] + current.getVelocity()[1]});
49+
});
50+
}).wait();
51+
}
52+
53+
void draw(SDL_Renderer* &render, std::vector<Actor> actors, Room room) {
3254
SDL_SetRenderDrawColor(render, 255, 255, 255, 255);
3355
SDL_RenderClear(render);
3456

57+
SDL_Color red = {255, 0, 0, 255};
58+
for (Actor actor : actors) {
59+
SDL_Point pos = {int(actor.getPos()[0] * SCALE), int(actor.getPos()[1] * SCALE)};
60+
drawCircle(render, pos, actor.getRadius() * SCALE, red);
61+
}
3562

63+
SDL_SetRenderDrawColor(render, 0, 0, 0, 255);
64+
for (std::array<float, 4> wall : room.getWalls()) {
65+
SDL_RenderDrawLine(render, wall[0] * SCALE, wall[1] * SCALE, wall[2] * SCALE, wall[3] * SCALE);
66+
}
3667

3768
SDL_RenderPresent(render);
3869
}
@@ -48,13 +79,16 @@ int main() {
4879
SDL_Renderer* render = NULL;
4980

5081
std::vector<Actor> actors;
82+
Room room = Room({{0.5, 0.5, 0.5, 1.5}, {0.5, 2.5, 0.5, 5.5}, {0.5, 5.5, 7.5, 5.5}, {7.5, 5.5, 7.5, 0.5}, {7.5, 0.5, 0.5, 0.5}});
5183

5284
init(win, render, actors);
5385

54-
draw(render, actors);
86+
// Make actor move towards destination
87+
actors[0].setVelocity(velToPoint(0.008, actors[0].getPos(), actors[0].getDestination()));
5588

56-
Actor test = Actor({0, 2}, {3, 4}, {5,6}, 12, 3);
89+
draw(render, actors, room);
5790

91+
int delayCounter = 0;
5892
bool isQuit = false;
5993
SDL_Event event;
6094

@@ -64,8 +98,14 @@ int main() {
6498
isQuit = true;
6599
}
66100
}
67-
68-
draw(render, actors);
101+
if (delayCounter >= DELAY) {
102+
delayCounter = 0;
103+
update(actors);
104+
draw(render, actors, room);
105+
}
106+
else {
107+
delayCounter++;
108+
}
69109
}
70110

71111
close(win);

0 commit comments

Comments
 (0)