Skip to content

Commit 047e83b

Browse files
partial changes
1 parent b3bf15f commit 047e83b

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

src/init.hpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,39 @@ namespace gpudrive
1414

1515
// Cannot use Madrona::math::Vector2 because it is not a POD type.
1616
// Getting all zeros if using any madrona types.
17-
struct MapVector2
17+
struct MapVector3
1818
{
1919
float x;
2020
float y;
21+
float z;
2122
};
2223

2324
struct MapObject
2425
{
25-
MapVector2 position[MAX_POSITIONS];
26+
MapVector3 position[MAX_POSITIONS];
2627
float width;
2728
float length;
2829
float heading[MAX_POSITIONS];
29-
MapVector2 velocity[MAX_POSITIONS];
30+
MapVector3 velocity[MAX_POSITIONS];
3031
bool valid[MAX_POSITIONS];
31-
MapVector2 goalPosition;
32+
MapVector3 goalPosition;
3233
EntityType type;
3334

3435
uint32_t numPositions;
3536
uint32_t numHeadings;
3637
uint32_t numVelocities;
3738
uint32_t numValid;
38-
MapVector2 mean;
39+
MapVector3 mean;
3940
bool markAsStatic{false};
4041
};
4142

4243
struct MapRoad
4344
{
4445
// std::array<MapPosition, MAX_POSITIONS> geometry;
45-
MapVector2 geometry[MAX_GEOMETRY];
46+
MapVector3 geometry[MAX_GEOMETRY];
4647
EntityType type;
4748
uint32_t numPoints;
48-
MapVector2 mean;
49+
MapVector3 mean;
4950
};
5051

5152
struct Map
@@ -56,7 +57,7 @@ namespace gpudrive
5657
uint32_t numObjects;
5758
uint32_t numRoads;
5859
uint32_t numRoadSegments;
59-
MapVector2 mean;
60+
MapVector3 mean;
6061

6162
// Constructor
6263
Map() = default;

src/json_serialization.hpp

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@
77

88
namespace gpudrive
99
{
10-
void from_json(const nlohmann::json &j, MapVector2 &p)
10+
void from_json(const nlohmann::json &j, MapVector3 &p)
1111
{
1212
p.x = j.at("x").get<float>();
1313
p.y = j.at("y").get<float>();
14+
p.z = j.at("z").get<float>();
1415
}
1516

1617
void from_json(const nlohmann::json &j, MapObject &obj)
1718
{
1819
const auto &valid = j.at("valid");
1920

20-
obj.mean = {0,0};
21+
obj.mean = {0,0,0};
2122
uint32_t i = 0;
2223
for (const auto &pos : j.at("position"))
2324
{
@@ -26,6 +27,7 @@ namespace gpudrive
2627
from_json(pos, obj.position[i]);
2728
obj.mean.x += (obj.position[i].x - obj.mean.x)/(i+1);
2829
obj.mean.y += (obj.position[i].y - obj.mean.y)/(i+1);
30+
obj.mean.z += (obj.position[i].z - obj.mean.z)/(i+1);
2931
++i;
3032
}
3133
else
@@ -94,15 +96,15 @@ namespace gpudrive
9496
else
9597
obj.type = EntityType::None;
9698

97-
std::string markAsStaticKey = "mark_as_static";
98-
if (j.contains(markAsStaticKey)) {
99-
from_json(j.at("mark_as_static"), obj.markAsStatic);
100-
}
99+
std::string markAsStaticKey = "mark_as_static";
100+
if (j.contains(markAsStaticKey)) {
101+
from_json(j.at("mark_as_static"), obj.markAsStatic);
102+
}
101103
}
102104

103105
void from_json(const nlohmann::json &j, MapRoad &road, float polylineReductionThreshold = 0.0)
104106
{
105-
road.mean = {0,0};
107+
road.mean = {0,0,0};
106108
std::string type = j.at("type");
107109
if(type == "road_edge")
108110
road.type = EntityType::RoadEdge;
@@ -119,10 +121,10 @@ namespace gpudrive
119121
else
120122
road.type = EntityType::None;
121123

122-
std::vector<MapVector2> geometry_points_;
124+
std::vector<MapVector3> geometry_points_;
123125
for(const auto &point: j.at("geometry"))
124126
{
125-
MapVector2 p;
127+
MapVector3 p;
126128
from_json(point, p);
127129
geometry_points_.push_back(p);
128130
}
@@ -155,10 +157,24 @@ namespace gpudrive
155157
}
156158
if (k_2 >= num_sampled_points)
157159
break;
160+
//Using 3D space area calculation
158161
auto point1 = geometry_points_[k * sample_every_n_];
159162
auto point2 = geometry_points_[k_1 * sample_every_n_];
160163
auto point3 = geometry_points_[k_2 * sample_every_n_];
161-
float_t area = 0.5 * std::abs((point1.x - point3.x) * (point2.y - point1.y) - (point1.x - point2.x) * (point3.y - point1.y));
164+
// Vector 1: point1 -> point2
165+
float vx1 = point2.x - point1.x;
166+
float vy1 = point2.y - point1.y;
167+
float vz1 = point2.z - point1.z;
168+
// Vector 2: point1 -> point3
169+
float vx2 = point3.x - point1.x;
170+
float vy2 = point3.y - point1.y;
171+
float vz2 = point3.z - point1.z;
172+
// Cross product to get the normal vector
173+
float nx = vy1 * vz2 - vz1 * vy2;
174+
float ny = vz1 * vx2 - vx1 * vz2;
175+
float nz = vx1 * vy2 - vy1 * vx2;
176+
// Magnitude of the cross product
177+
float_t area = 0.5 * std::sqrt(nx * nx + ny * ny + nz * nz);
162178
if (area < polylineReductionThreshold)
163179
{ // If the area is less than the threshold, then we skip the middle point
164180
skip[k_1] = true; // Mark the middle point as skipped
@@ -176,7 +192,7 @@ namespace gpudrive
176192
k = 0;
177193
skip[0] = false;
178194
skip[num_sampled_points - 1] = false;
179-
std::vector<MapVector2> new_geometry_points; // This list stores the points that are not skipped
195+
std::vector<MapVector3> new_geometry_points; // This list stores the points that are not skipped
180196
while (k < num_sampled_points)
181197
{
182198
if (!skip[k])
@@ -208,13 +224,14 @@ namespace gpudrive
208224
{
209225
road.mean.x += (road.geometry[i].x - road.mean.x)/(i+1);
210226
road.mean.y += (road.geometry[i].y - road.mean.y)/(i+1);
227+
road.mean.z += (road.geometry[i].z - road.mean.z)/(i+1);
211228
}
212229

213230
}
214231

215232
std::pair<float, float> calc_mean(const nlohmann::json &j)
216233
{
217-
std::pair<float, float> mean = {0, 0};
234+
std::pair<float, float> mean = {0, 0, 0};
218235
int64_t numEntities = 0;
219236
for (const auto &obj : j["objects"])
220237
{
@@ -226,9 +243,11 @@ namespace gpudrive
226243
numEntities++;
227244
float newX = pos["x"];
228245
float newY = pos["y"];
246+
float newZ = pos["z"];
229247
// Update mean incrementally
230248
mean.first += (newX - mean.first) / numEntities;
231249
mean.second += (newY - mean.second) / numEntities;
250+
mean.third += (newZ - mean.third) / numEntities;
232251
}
233252
}
234253
for (const auto &obj : j["roads"])
@@ -238,10 +257,12 @@ namespace gpudrive
238257
numEntities++;
239258
float newX = point["x"];
240259
float newY = point["y"];
260+
float newZ = point["z"];
241261

242262
// Update mean incrementally
243263
mean.first += (newX - mean.first) / numEntities;
244264
mean.second += (newY - mean.second) / numEntities;
265+
mean.third += (newZ - mean.third) / numEntities;
245266
}
246267
}
247268
return mean;

0 commit comments

Comments
 (0)