1
1
#include " DifferentialEq.hpp"
2
2
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];
5
9
6
10
vecType pos = currentActor->getPos ();
7
11
8
12
// Calculate personal impulse
9
13
float mi = currentActor->getMass ();
10
14
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
+
13
33
vecType vi = currentActor->getVelocity ();
14
34
15
35
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)
18
52
vecType peopleForces = {0 , 0 };
19
53
for (int x = 0 ; x < actors.size (); x++) {
20
54
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) {
23
64
vecType currentToNeighbour = pos - neighbour.getPos ();
24
65
float dij = magnitude (currentToNeighbour);
66
+ float rij = neighbour.getRadius () + currentActor->getRadius ();
25
67
vecType nij = (currentToNeighbour) / dij;
26
68
vecType tij = getTangentialVector (nij);
27
69
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);
29
72
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);
31
76
}
32
77
}
33
78
@@ -39,10 +84,11 @@ SYCL_EXTERNAL void differentialEq(int currentIndex, sycl::accessor<Actor, 1, syc
39
84
std::pair<float , vecType> dAndn = getDistanceAndNiw (pos, currentWall);
40
85
float diw = dAndn.first ;
41
86
float g = diw > ri ? 0 : ri - diw;
42
- vecType niw = dAndn.second ;
87
+ vecType niw = normalize ( dAndn.second ) ;
43
88
vecType tiw = getTangentialVector (niw);
44
89
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);
46
92
}
47
93
48
94
vecType forceSum = personalImpulse + peopleForces + wallForces;
@@ -52,20 +98,23 @@ SYCL_EXTERNAL void differentialEq(int currentIndex, sycl::accessor<Actor, 1, syc
52
98
53
99
// Color actor according to heatmap
54
100
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
+ }
57
110
auto color = findColor (colorVal);
58
111
currentActor->setColor ({int (color[0 ]), int (color[1 ]), int (color[2 ])});
59
112
}
60
113
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
-
66
114
vecType acceleration = forceSum / mi;
67
115
currentActor->setVelocity (vi + acceleration * TIMESTEP);
68
116
currentActor->setPos (pos + currentActor->getVelocity () * TIMESTEP);
69
117
70
- currentActor->checkAtDestination (destination, paths[currentActor->getPathId ()].getPathSize ());
118
+ currentActor->checkAtDestination (
119
+ destination, paths[currentActor->getPathId ()].getPathSize ());
71
120
}
0 commit comments