Skip to content

Commit 41b7703

Browse files
committed
Cleaning up code
1 parent 230c8a3 commit 41b7703

File tree

9 files changed

+58
-97
lines changed

9 files changed

+58
-97
lines changed

external/Actor.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
#include "Actor.hpp"
2-
#include <iostream>
3-
#include <array>
4-
#include <sycl/sycl.hpp>
52

63
Actor::Actor(vecType pPos, vecType pVelocity, vecType pDesiredVelocity, vecType pDestination, float pMass, float pRadius, bool pAtDestination, std::array<int, 3> pColor) {
74
pos = pPos;

external/DifferentialEq.cpp

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,61 @@
1-
#include <iostream>
2-
#include <vector>
3-
#include <array>
4-
#include <sycl.hpp>
51
#include "DifferentialEq.hpp"
6-
#include "Actor.hpp"
7-
#include "Room.hpp"
8-
#include "MathHelper.hpp"
92

103
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) {
114
Actor* i = &actors[z];
125

13-
auto pos = i->getPos();
14-
15-
auto mi = i->getMass();
16-
auto v0i = 2;
17-
auto e0i = normalize(getDirectionVector(i->getPos(), i->getDestination()));
18-
auto vi = i->getVelocity();
6+
vecType pos = i->getPos();
197

20-
auto personalImpulse = mi * (((v0i * e0i) - vi) / Ti);
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);
2114

2215
vecType peopleForces = {0, 0};
2316
for (int x = 0; x < actors.size(); x++) {
24-
auto j = actors[x];
17+
Actor j = actors[x];
2518
if (z != x && !j.getAtDestination()) {
26-
auto rij = j.getRadius() + i->getRadius();
27-
auto dij = magnitude(i->getPos() - j.getPos());
28-
auto nij = (i->getPos() - j.getPos()) / dij;
19+
float rij = j.getRadius() + i->getRadius();
20+
float dij = magnitude(pos - j.getPos());
21+
vecType nij = (pos - j.getPos()) / dij;
2922
vecType tij = {-nij[1], nij[0]};
30-
auto g = dij > rij ? 0 : rij - dij;
31-
auto deltavtij = dotProduct((j.getVelocity() - i->getVelocity()), tij);
23+
float g = dij > rij ? 0 : rij - dij;
24+
float deltavtij = dotProduct((j.getVelocity() - i->getVelocity()), tij);
3225

3326
peopleForces += (Ai * exp((rij - dij) / Bi) + K1 * g) * nij + (K2 * g * deltavtij * tij);
3427
}
3528
}
3629

3730
vecType wallForces = {0, 0};
3831
for (int x = 0; x < walls.size(); x++) {
39-
auto w = walls[x];
40-
auto ri = i->getRadius();
41-
auto diw = distanceToWall(pos, w);
42-
auto g = diw > ri ? 0 : ri - diw;
43-
auto niw = getniw(pos, w);
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;
4438
vecType tiw = {-niw[1], niw[0]};
4539

4640
wallForces += (Ai * exp((ri - diw) / Bi) + K1 * g) * niw - (K2 * g * dotProduct(vi, tiw) * tiw);
4741
}
4842

4943
vecType forceSum = personalImpulse + peopleForces + wallForces;
50-
auto acceleration = forceSum / mi;
44+
vecType acceleration = forceSum / mi;
5145

5246
//out << "People Forces: (" << peopleForces[0] << ", " << peopleForces[1] << ") " << z << sycl::endl;
5347
//out << "Wall Forces: (" << wallForces[0] << ", " << wallForces[1] << ") " << z << sycl::endl;
5448
//out << "Acceleration: (" << acceleration[0] << ", " << acceleration[1] << ") " << z << sycl::endl;
5549
//out << "-----------------------" << sycl::endl;
5650

57-
i->setVelocity(i->getVelocity() + forceSum * 0.001);
58-
i->setPos(i->getPos() + i->getVelocity() * 0.001);
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();
5956

60-
if (i->getPos()[0] <= i->getDestination()[0] + 0.01 && i->getPos()[0] >= i->getDestination()[0] - 0.01
61-
&& i->getPos()[1] <= i->getDestination()[1] + 0.01 && i->getPos()[1] >= i->getDestination()[1] - 0.01) {
57+
if (newPos[0] <= destination[0] + 0.01 && newPos[0] >= destination[0] - 0.01
58+
&& pos[1] <= destination[1] + 0.01 && pos[1] >= destination[1] - 0.01) {
6259
i->setAtDestination(true);
6360
}
6461
}

external/DifferentialEq.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
#define DifferentialEqu_hpp
33

44
#include <iostream>
5-
#include <vector>
65
#include <array>
76
#include <sycl/sycl.hpp>
87
#include "Actor.hpp"
9-
#include "Room.hpp"
108
#include "MathHelper.hpp"
119
#include "VectorMaths.hpp"
1210

external/MathHelper.cpp

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
#include "MathHelper.hpp"
2-
#include "VectorMaths.hpp"
3-
#include <iostream>
4-
#include <vector>
5-
#include <array>
6-
#include <cmath>
7-
#include <algorithm>
8-
#include <sycl/sycl.hpp>
92

103
SYCL_EXTERNAL vecType getDirectionVector(vecType from, vecType to) {
114
return to - from;
@@ -31,42 +24,33 @@ SYCL_EXTERNAL float distance(vecType from, vecType to) {
3124
return magnitude(from - to);
3225
}
3326

34-
SYCL_EXTERNAL float distanceToWall(vecType point, std::array<vecType, 2> wall) {
35-
float lSquared = pow(magnitude(wall[1] - wall[0]), 2) +0.01f;
36-
if (lSquared == 0.0) {
37-
return distance(point, wall[0]);
38-
}
39-
float t = std::max(float(0.0), std::min(float(1.0), dotProduct(point - wall[0], wall[1] - wall[0]) / lSquared));
40-
auto projection = wall[0] + t * (wall[1] - wall[0]);
41-
return distance(point, projection);
42-
}
43-
4427
SYCL_EXTERNAL vecType normalize(vecType inp) {
4528
return inp / magnitude(inp);
4629
}
4730

48-
SYCL_EXTERNAL vecType getniw(vecType point, std::array<vecType, 2> wall) {
49-
vecType AB = wall[1] - wall[0];
50-
vecType BP = point - wall[1];
51-
vecType AP = point - wall[0];
31+
SYCL_EXTERNAL std::pair<float, vecType> getDistanceAndNiw(vecType point, std::array<vecType, 2> wall) {
32+
vecType AB = getDirectionVector(wall[0], wall[1]);
33+
vecType BP = getDirectionVector(wall[1], point);
34+
vecType AP = getDirectionVector(wall[0], point);
5235

5336
float ABdotBP = dotProduct(AB, BP);
5437
float ABdotAP = dotProduct(AB, AP);
5538

5639
if (ABdotBP >= 0) {
57-
return normalize(point - wall[1]);
40+
return {distance(point, wall[1]), normalize(BP)};
5841
}
5942
else if (ABdotAP < 0) {
60-
return normalize(point - wall[0]);
43+
return {distance(point, wall[0]), normalize(AP)};
6144
}
6245
else {
63-
float lSquared = pow(magnitude(wall[1] - wall[0]), 2);
46+
// float lSquared = pow(magnitude(AB), 2);
47+
float lSquared = dotProduct(AB, AB);
6448
if (lSquared == 0.0) {
65-
return {0, 0};
49+
return {0, {0, 0}};
6650
}
67-
float t = std::max(float(0.0), std::min(float(1.0), dotProduct(point - wall[0], wall[1] - wall[0]) / lSquared));
51+
float t = std::max(float(0.0), std::min(float(1.0), dotProduct(AP, AB) / lSquared));
6852
auto projection = t * AB;
6953

70-
return normalize(AP - projection);
54+
return {distance(AP, projection), normalize(AP - projection)};
7155
}
7256
}

external/MathHelper.hpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define MathHelper_hpp
33

44
#include <iostream>
5-
#include <vector>
65
#include <array>
76
#include <sycl/sycl.hpp>
87
#include "VectorMaths.hpp"
@@ -21,10 +20,8 @@ SYCL_EXTERNAL float dotProduct(vecType a, vecType b);
2120

2221
SYCL_EXTERNAL float distance(vecType a, vecType b);
2322

24-
SYCL_EXTERNAL float distanceToWall(vecType a, std::array<vecType, 2> w);
25-
2623
SYCL_EXTERNAL vecType normalize(vecType inp);
2724

28-
SYCL_EXTERNAL vecType getniw(vecType a, std::array<vecType, 2> w);
25+
SYCL_EXTERNAL std::pair<float, vecType> getDistanceAndNiw(vecType point, std::array<vecType, 2> wall);
2926

3027
#endif

external/Room.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
#include "Room.hpp"
2-
#include "VectorMaths.hpp"
3-
#include <iostream>
4-
#include <vector>
5-
#include <array>
6-
#include <sycl/sycl.hpp>
72

83
Room::Room(std::vector<std::array<vecType, 2>> pWalls) {
94
walls = pWalls;

external/Room.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define Room_hpp
33

44
#include <iostream>
5-
#include <vector>
65
#include <array>
76
#include <sycl/sycl.hpp>
87
#include "VectorMaths.hpp"

external/VectorMaths.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
#include <iostream>
2-
#include <cmath>
3-
#include <sycl/sycl.hpp>
4-
#include <array>
51
#include "VectorMaths.hpp"
62

73
SYCL_EXTERNAL vecType operator*=(vecType& a, vecType b) {

src/main.cpp

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,6 @@ void draw(SDL_Renderer* &render, std::vector<Actor> actors, Room room) {
111111
SDL_RenderDrawLine(render, wall[0][0] * SCALE, wall[0][1] * SCALE, wall[1][0] * SCALE, wall[1][1] * SCALE);
112112
}
113113

114-
SDL_SetRenderDrawColor(render, 0, 255, 0, 255);
115-
SDL_Rect r;
116-
r.x = 35; r.y = 195; r.w = 10; r.h = 10;
117-
SDL_RenderDrawRect(render, &r);
118-
119114
SDL_RenderPresent(render);
120115
}
121116

@@ -130,22 +125,25 @@ int main() {
130125
SDL_Renderer* render = NULL;
131126

132127
std::vector<Actor> actors;
133-
Room room = Room({{vecType{3, 3}, vecType{4.25, 3}},
134-
{vecType{4.25, 3}, vecType{4.25, 4.25}},
135-
{vecType{4.25, 4.25}, vecType{3, 4.25}},
136-
{vecType{3, 4.25}, vecType{3, 3}},
137-
{vecType{4.75, 3}, vecType{6, 3}},
138-
{vecType{6, 3}, vecType{6, 4.25}},
139-
{vecType{6, 4.25}, vecType{4.75, 4.25}},
140-
{vecType{4.75, 4.25}, vecType{4.75, 3}},
141-
{vecType{3, 4.75}, vecType{4.25, 4.75}},
142-
{vecType{4.25, 4.75}, vecType{4.25, 6}},
143-
{vecType{4.25, 6}, vecType{3, 6}},
144-
{vecType{3, 6}, vecType{3, 4.75}},
145-
{vecType{4.75, 4.75}, vecType{6, 4.75}},
146-
{vecType{6, 4.75}, vecType{6, 6}},
147-
{vecType{6, 6}, vecType{4.75, 6}},
148-
{vecType{4.75, 6}, vecType{4.75, 4.75}},
128+
Room room = Room({{vecType{3.15, 3.15}, vecType{4.25, 3.15}},
129+
{vecType{4.25, 3.15}, vecType{4.25, 4.25}},
130+
{vecType{4.25, 4.25}, vecType{3.15, 4.25}},
131+
{vecType{3.15, 4.25}, vecType{3.15, 3.15}},
132+
133+
{vecType{4.75, 3.15}, vecType{5.85, 3.15}},
134+
{vecType{5.85, 3.15}, vecType{5.85, 4.25}},
135+
{vecType{5.85, 4.25}, vecType{4.75, 4.25}},
136+
{vecType{4.75, 4.25}, vecType{4.75, 3.15}},
137+
138+
{vecType{3.15, 4.75}, vecType{4.25, 4.75}},
139+
{vecType{4.25, 4.75}, vecType{4.25, 5.85}},
140+
{vecType{4.25, 5.85}, vecType{3.15, 5.85}},
141+
{vecType{3.15, 5.85}, vecType{3.15, 4.75}},
142+
143+
{vecType{4.75, 4.75}, vecType{5.85, 4.75}},
144+
{vecType{5.85, 4.75}, vecType{5.85, 5.85}},
145+
{vecType{5.85, 5.85}, vecType{4.75, 5.85}},
146+
{vecType{4.75, 5.85}, vecType{4.75, 4.75}},
149147
});
150148
// Room room = Room({{vecType{2, 3.5}, vecType{6, 3.5}}});
151149
// Room room = Room({{vecType{3.5, 0.5}, vecType{4.5, 5.5}}});

0 commit comments

Comments
 (0)