@@ -236,9 +236,20 @@ int pnpoly(int nvert, float* vertx, float* verty, float testx, float testy)
236
236
return c;
237
237
}
238
238
239
- void ReasoningGrid::build (NavPower::NavMesh* navMesh, BuildContext* ctx) {
240
- ctx->log (RC_LOG_PROGRESS, " Started building Airg." );
239
+ float calcZ (Vec3 p1, Vec3 p2, Vec3 p3, float x, float y) {
240
+ float dx1 = x - p1.X ;
241
+ float dy1 = y - p1.Y ;
242
+ float dx2 = p2.X - p1.X ;
243
+ float dy2 = p2.Y - p1.Y ;
244
+ float dx3 = p3.X - p1.X ;
245
+ float dy3 = p3.Y - p1.Y ;
246
+ return p1.Z + ((dy1 * dx3 - dx1 * dy3) * (p2.Z - p1.Z ) + (dx1 * dy2 - dy1 * dx2) * (p3.Z - p1.Z )) / (dx3 * dy2 - dx2 * dy3);
247
+ }
248
+
249
+ void ReasoningGrid::build (ReasoningGrid* airg, NavPower::NavMesh* navMesh, NavKit* navKit) {
250
+ navKit->log (RC_LOG_PROGRESS, " Started building Airg." );
241
251
double spacing = 2 ;
252
+ double zSpacing = 1 ;
242
253
// grid is set up in this order: Z[Y[X[]]]
243
254
std::vector<std::vector<std::vector<int >>> grid;
244
255
Vec3 min = navMesh->m_graphHdr ->m_bbox .m_min ;
@@ -247,39 +258,78 @@ void ReasoningGrid::build(NavPower::NavMesh* navMesh, BuildContext* ctx) {
247
258
Vec3 max = navMesh->m_graphHdr ->m_bbox .m_max ;
248
259
int gridXSize = std::ceil ((max.X - min.X ) / spacing);
249
260
int gridYSize = std::ceil ((max.Y - min.Y ) / spacing);
250
- int gridZSize = 1 ;
251
- m_Properties.fGridSpacing = spacing;
252
- m_Properties.nGridWidth = gridYSize;
253
- m_Properties.vMin .x = min.X ;
254
- m_Properties.vMin .y = min.Y ;
255
- m_Properties.vMin .z = min.Z ;
256
- m_Properties.vMin .w = 1 ;
257
- m_Properties.vMax .x = max.X ;
258
- m_Properties.vMax .y = max.Y ;
259
- m_Properties.vMax .z = max.Z ;
260
- m_Properties.vMax .w = 1 ;
261
+ int gridZSize = std::ceil ((max.Z - min.Z ) / zSpacing);
262
+ gridZSize = gridZSize > 0 ? gridZSize : 1 ;
263
+ airg->m_Properties .fGridSpacing = spacing;
264
+ airg->m_Properties .nGridWidth = gridYSize;
265
+ airg->m_Properties .vMin .x = min.X ;
266
+ airg->m_Properties .vMin .y = min.Y ;
267
+ airg->m_Properties .vMin .z = min.Z ;
268
+ airg->m_Properties .vMin .w = 1 ;
269
+ airg->m_Properties .vMax .x = max.X ;
270
+ airg->m_Properties .vMax .y = max.Y ;
271
+ airg->m_Properties .vMax .z = max.Z ;
272
+ airg->m_Properties .vMax .w = 1 ;
273
+
274
+ std::vector<double > areaZMins;
275
+ std::vector<double > areaZMaxes;
276
+
277
+ for (auto area : navMesh->m_areas ) {
278
+ const int areaPointCount = area.m_edges .size ();
279
+ double areaMinZ = 1000 ;
280
+ double areaMaxZ = -1000 ;
281
+ double pointZ = 0 ;
282
+ for (int i = 0 ; i < areaPointCount; i++) {
283
+ pointZ = area.m_edges [i]->m_pos .Z ;
284
+ areaMinZ = areaMinZ < pointZ ? areaMinZ : pointZ;
285
+ areaMaxZ = areaMaxZ > pointZ ? areaMaxZ : pointZ;
286
+ }
287
+ areaZMins.push_back (areaMinZ);
288
+ areaZMaxes.push_back (areaMaxZ);
289
+ }
290
+ std::vector<std::vector<int >> areasByZLevel;
291
+ for (int zi = 0 ; zi < gridZSize; zi++) {
292
+ std::vector<int > areasForZLevel;
293
+ for (int areaIndex = 0 ; areaIndex < navMesh->m_areas .size (); areaIndex++) {
294
+ double minZ = min.Z + zi * zSpacing;
295
+ double maxZ = min.Z + (zi + 1 ) * zSpacing;
296
+ if ((areaZMins[areaIndex] >= minZ && areaZMins[areaIndex] <= maxZ) ||
297
+ (areaZMaxes[areaIndex] >= minZ && areaZMaxes[areaIndex] <= maxZ) ||
298
+ (areaZMins[areaIndex] <= minZ && areaZMaxes[areaIndex] >= maxZ)) {
299
+ areasForZLevel.push_back (areaIndex);
300
+ }
301
+ }
302
+ areasByZLevel.push_back (areasForZLevel);
303
+ }
261
304
262
305
int wayPointIndex = 0 ;
263
306
for (int zi = 0 ; zi < gridZSize; zi++) {
264
307
std::vector<std::vector<int >> yRow;
308
+ navKit->log (rcLogCategory::RC_LOG_PROGRESS, (" Adding waypoints for Z level: " + std::to_string (zi) + " at Z: " + std::to_string (min.Z + zi * zSpacing)).c_str ());
309
+ double minZ = min.Z + zi * zSpacing;
310
+ double maxZ = min.Z + (zi + 1 ) * zSpacing;
265
311
for (int yi = 0 ; yi < gridYSize; yi++) {
266
312
std::vector<int > xRow;
267
313
for (int xi = 0 ; xi < gridXSize; xi++) {
268
- bool pointInArea = false ;
269
314
double x = min.X + xi * spacing;
270
315
double y = min.Y + yi * spacing;
271
- double z = min.Z + zi * spacing;
272
- for (auto area : navMesh->m_areas ) {
316
+ double z = min.Z + zi * zSpacing;
317
+ bool pointInArea = false ;
318
+ for (int areaIndex : areasByZLevel[zi]) {
319
+ auto area = navMesh->m_areas [areaIndex];
273
320
const int areaPointCount = area.m_edges .size ();
274
- float areaXCoords[10 ];
275
- float areaYCoords[10 ];
276
- for (int i = 0 ; i < areaPointCount; i++) {
277
- areaXCoords[i] = area.m_edges [i]->m_pos .X ;
278
- areaYCoords[i] = area.m_edges [i]->m_pos .Y ;
279
- }
280
- pointInArea = pnpoly (areaPointCount, areaXCoords, areaYCoords, x, y);
281
- if (pointInArea) {
282
- break ;
321
+ z = calcZ (area.m_edges [0 ]->m_pos , area.m_edges [1 ]->m_pos , area.m_edges [2 ]->m_pos , x, y) + 0.1 ;
322
+ if (z > (minZ - zSpacing * .8 ) && z < (maxZ + zSpacing * .8 )) {
323
+ float areaXCoords[10 ];
324
+ float areaYCoords[10 ];
325
+ for (int i = 0 ; i < areaPointCount; i++) {
326
+ areaXCoords[i] = area.m_edges [i]->m_pos .X ;
327
+ areaYCoords[i] = area.m_edges [i]->m_pos .Y ;
328
+ }
329
+ pointInArea = pnpoly (areaPointCount, areaXCoords, areaYCoords, x, y);
330
+ if (pointInArea) {
331
+ break ;
332
+ }
283
333
}
284
334
}
285
335
xRow.push_back (pointInArea ? wayPointIndex++ : 65535 );
@@ -291,16 +341,16 @@ void ReasoningGrid::build(NavPower::NavMesh* navMesh, BuildContext* ctx) {
291
341
waypoint.vPos .w = 1.0 ;
292
342
waypoint.nVisionDataOffset = 0 ;
293
343
waypoint.nLayerIndex = 0 ;
294
- m_WaypointList.push_back (waypoint);
344
+ airg-> m_WaypointList .push_back (waypoint);
295
345
}
296
346
}
297
347
yRow.push_back (xRow);
298
348
}
299
349
grid.push_back (yRow);
300
350
}
301
351
302
- m_nNodeCount = m_WaypointList.size ();
303
- m_deadEndData.m_nSize = m_nNodeCount;
352
+ airg-> m_nNodeCount = airg-> m_WaypointList .size ();
353
+ airg-> m_deadEndData .m_nSize = airg-> m_nNodeCount ;
304
354
// Neighbors: South is 0, increases CCW
305
355
std::pair<int , int > gridIndexDiff[8 ]{
306
356
std::pair (0 , -1 ),
@@ -326,12 +376,14 @@ void ReasoningGrid::build(NavPower::NavMesh* navMesh, BuildContext* ctx) {
326
376
nyi >= 0 && nyi < gridYSize) {
327
377
neighborWaypointIndex = grid[zi][nyi][nxi];
328
378
}
329
- m_WaypointList[waypointIndex].nNeighbors .push_back (neighborWaypointIndex);
379
+ airg-> m_WaypointList [waypointIndex].nNeighbors .push_back (neighborWaypointIndex);
330
380
}
331
381
}
332
382
}
333
383
}
334
384
}
335
- std::vector<uint32_t > visibilityData (m_nNodeCount * 1001 );
336
- m_pVisibilityData = visibilityData;
385
+ std::vector<uint32_t > visibilityData (airg->m_nNodeCount * 1001 );
386
+ airg->m_pVisibilityData = visibilityData;
387
+ navKit->log (RC_LOG_PROGRESS, " Done building Airg." );
388
+ navKit->airg ->airgLoaded = true ;
337
389
}
0 commit comments