|
| 1 | +// The installed Recast & Detour wrapper: a navigation mesh built from a floor with a wall across it, the same |
| 2 | +// bytes twice; a path around the wall and a ray along the mesh stopped by it; a crowd of four walked through the |
| 3 | +// doorway to the far side. The mesh's bytes and the crowd's places are pinned, as the wrapper's CI pins them on |
| 4 | +// every platform; the adapted constants. |
| 5 | +import std; |
| 6 | +import mcpplibs.mwrap.recastnavigation; |
| 7 | + |
| 8 | +namespace nav = mcpplibs::mwrap::recastnavigation; |
| 9 | + |
| 10 | +namespace { |
| 11 | + |
| 12 | +int failures = 0; |
| 13 | +void check(bool held) { failures += held ? 0 : 1; } |
| 14 | + |
| 15 | +// A 20 m square floor at y 0, and a wall 0.4 m thick and 2 m high across x = 0 from z = -10 to z = 6 (a |
| 16 | +// doorway from z = 6 to 10), as triangles. |
| 17 | +struct Soup { |
| 18 | + std::vector<float> vertices; |
| 19 | + std::vector<int> triangles; |
| 20 | + void quad(std::array<float, 3> a, std::array<float, 3> b, std::array<float, 3> c, std::array<float, 3> d) { |
| 21 | + const int base = static_cast<int>(vertices.size() / 3); |
| 22 | + for (const auto& corner : {a, b, c, d}) vertices.insert(vertices.end(), corner.begin(), corner.end()); |
| 23 | + triangles.insert(triangles.end(), {base, base + 1, base + 2, base, base + 2, base + 3}); |
| 24 | + } |
| 25 | + // A box from `low` to `high`, its faces wound counter-clockwise seen from outside. |
| 26 | + void box(std::array<float, 3> low, std::array<float, 3> high) { |
| 27 | + const auto [x0, y0, z0] = low; |
| 28 | + const auto [x1, y1, z1] = high; |
| 29 | + quad({x0, y1, z0}, {x0, y1, z1}, {x1, y1, z1}, {x1, y1, z0}); // top |
| 30 | + quad({x0, y0, z0}, {x1, y0, z0}, {x1, y0, z1}, {x0, y0, z1}); // bottom |
| 31 | + quad({x0, y0, z0}, {x0, y1, z0}, {x1, y1, z0}, {x1, y0, z0}); // -z |
| 32 | + quad({x0, y0, z1}, {x1, y0, z1}, {x1, y1, z1}, {x0, y1, z1}); // +z |
| 33 | + quad({x0, y0, z0}, {x0, y0, z1}, {x0, y1, z1}, {x0, y1, z0}); // -x |
| 34 | + quad({x1, y0, z0}, {x1, y1, z0}, {x1, y1, z1}, {x1, y0, z1}); // +x |
| 35 | + } |
| 36 | +}; |
| 37 | +Soup scene() { |
| 38 | + Soup soup; |
| 39 | + soup.quad({-10, 0, -10}, {-10, 0, 10}, {10, 0, 10}, {10, 0, -10}); |
| 40 | + soup.box({-0.2f, 0, -10}, {0.2f, 2, 6}); |
| 41 | + return soup; |
| 42 | +} |
| 43 | + |
| 44 | +// The fingerprint of bytes: FNV-1a, 64 bits. |
| 45 | +std::uint64_t fnv(std::span<const unsigned char> bytes, std::uint64_t hash = 0xcbf29ce484222325ull) { |
| 46 | + for (const unsigned char byte : bytes) hash = (hash ^ byte) * 0x100000001b3ull; |
| 47 | + return hash; |
| 48 | +} |
| 49 | +std::uint64_t fnv(std::span<const float> values, std::uint64_t hash = 0xcbf29ce484222325ull) { |
| 50 | + return fnv(std::as_bytes(values).size() ? std::span(reinterpret_cast<const unsigned char*>(values.data()), values.size_bytes()) : std::span<const unsigned char>{}, hash); |
| 51 | +} |
| 52 | + |
| 53 | +// Recast's pipeline for one tile, as upstream's solo-mesh sample runs it: the triangles rasterised into a |
| 54 | +// heightfield, what an agent cannot stand on or pass under filtered out, regions, contours, polygons and the |
| 55 | +// detail mesh; then Detour's data of it. |
| 56 | +std::vector<unsigned char> build(const Soup& soup) { |
| 57 | + rcConfig config{}; |
| 58 | + config.cs = 0.2f; |
| 59 | + config.ch = 0.1f; |
| 60 | + config.walkableSlopeAngle = 45; |
| 61 | + config.walkableHeight = 20; // 2 m in cells of 0.1 |
| 62 | + config.walkableClimb = 4; // 0.4 m |
| 63 | + config.walkableRadius = 2; // 0.4 m in cells of 0.2 |
| 64 | + config.maxEdgeLen = 60; |
| 65 | + config.maxSimplificationError = 1.3f; |
| 66 | + config.minRegionArea = 8 * 8; |
| 67 | + config.mergeRegionArea = 20 * 20; |
| 68 | + config.maxVertsPerPoly = 6; |
| 69 | + config.detailSampleDist = 1.2f; |
| 70 | + config.detailSampleMaxError = 0.1f; |
| 71 | + const int vertex_count = static_cast<int>(soup.vertices.size() / 3), triangle_count = static_cast<int>(soup.triangles.size() / 3); |
| 72 | + rcCalcBounds(soup.vertices.data(), vertex_count, config.bmin, config.bmax); |
| 73 | + rcCalcGridSize(config.bmin, config.bmax, config.cs, &config.width, &config.height); |
| 74 | + |
| 75 | + rcContext context(false); |
| 76 | + rcHeightfield* solid = rcAllocHeightfield(); |
| 77 | + check(rcCreateHeightfield(&context, *solid, config.width, config.height, config.bmin, config.bmax, config.cs, config.ch)); |
| 78 | + std::vector<unsigned char> areas(static_cast<std::size_t>(triangle_count), 0); |
| 79 | + rcMarkWalkableTriangles(&context, config.walkableSlopeAngle, soup.vertices.data(), vertex_count, soup.triangles.data(), triangle_count, areas.data()); |
| 80 | + check(rcRasterizeTriangles(&context, soup.vertices.data(), vertex_count, soup.triangles.data(), areas.data(), triangle_count, *solid, config.walkableClimb)); |
| 81 | + rcFilterLowHangingWalkableObstacles(&context, config.walkableClimb, *solid); |
| 82 | + rcFilterLedgeSpans(&context, config.walkableHeight, config.walkableClimb, *solid); |
| 83 | + rcFilterWalkableLowHeightSpans(&context, config.walkableHeight, *solid); |
| 84 | + rcCompactHeightfield* compact = rcAllocCompactHeightfield(); |
| 85 | + check(rcBuildCompactHeightfield(&context, config.walkableHeight, config.walkableClimb, *solid, *compact)); |
| 86 | + rcFreeHeightField(solid); |
| 87 | + check(rcErodeWalkableArea(&context, config.walkableRadius, *compact)); |
| 88 | + check(rcBuildDistanceField(&context, *compact)); |
| 89 | + check(rcBuildRegions(&context, *compact, 0, config.minRegionArea, config.mergeRegionArea)); |
| 90 | + rcContourSet* contours = rcAllocContourSet(); |
| 91 | + check(rcBuildContours(&context, *compact, config.maxSimplificationError, config.maxEdgeLen, *contours)); |
| 92 | + rcPolyMesh* mesh = rcAllocPolyMesh(); |
| 93 | + check(rcBuildPolyMesh(&context, *contours, config.maxVertsPerPoly, *mesh)); |
| 94 | + rcPolyMeshDetail* detail = rcAllocPolyMeshDetail(); |
| 95 | + check(rcBuildPolyMeshDetail(&context, *mesh, *compact, config.detailSampleDist, config.detailSampleMaxError, *detail)); |
| 96 | + rcFreeCompactHeightfield(compact); |
| 97 | + rcFreeContourSet(contours); |
| 98 | + for (int poly = 0; poly < mesh->npolys; ++poly) |
| 99 | + if (mesh->areas[poly] == nav::RC_WALKABLE_AREA) mesh->flags[poly] = 1; |
| 100 | + |
| 101 | + dtNavMeshCreateParams params{}; |
| 102 | + params.verts = mesh->verts; |
| 103 | + params.vertCount = mesh->nverts; |
| 104 | + params.polys = mesh->polys; |
| 105 | + params.polyAreas = mesh->areas; |
| 106 | + params.polyFlags = mesh->flags; |
| 107 | + params.polyCount = mesh->npolys; |
| 108 | + params.nvp = mesh->nvp; |
| 109 | + params.detailMeshes = detail->meshes; |
| 110 | + params.detailVerts = detail->verts; |
| 111 | + params.detailVertsCount = detail->nverts; |
| 112 | + params.detailTris = detail->tris; |
| 113 | + params.detailTriCount = detail->ntris; |
| 114 | + params.walkableHeight = 2.0f; |
| 115 | + params.walkableRadius = 0.4f; |
| 116 | + params.walkableClimb = 0.4f; |
| 117 | + std::copy_n(mesh->bmin, 3, params.bmin); |
| 118 | + std::copy_n(mesh->bmax, 3, params.bmax); |
| 119 | + params.cs = config.cs; |
| 120 | + params.ch = config.ch; |
| 121 | + params.buildBvTree = true; |
| 122 | + unsigned char* data = nullptr; |
| 123 | + int size = 0; |
| 124 | + check(dtCreateNavMeshData(¶ms, &data, &size)); |
| 125 | + rcFreePolyMesh(mesh); |
| 126 | + rcFreePolyMeshDetail(detail); |
| 127 | + std::vector<unsigned char> out(data, data + size); |
| 128 | + dtFree(data); |
| 129 | + return out; |
| 130 | +} |
| 131 | + |
| 132 | +// The mesh loaded: Detour takes a copy it frees. |
| 133 | +dtNavMesh* load(const std::vector<unsigned char>& bytes) { |
| 134 | + dtNavMesh* mesh = dtAllocNavMesh(); |
| 135 | + auto* data = static_cast<unsigned char*>(dtAlloc(static_cast<int>(bytes.size()), DT_ALLOC_PERM)); |
| 136 | + std::ranges::copy(bytes, data); |
| 137 | + check(dtStatusSucceed(mesh->init(data, static_cast<int>(bytes.size()), DT_TILE_FREE_DATA))); |
| 138 | + return mesh; |
| 139 | +} |
| 140 | + |
| 141 | +} // namespace |
| 142 | + |
| 143 | +int main() { |
| 144 | + const std::vector<unsigned char> bytes = build(scene()); |
| 145 | + check(!bytes.empty() && bytes == build(scene())); |
| 146 | + dtNavMesh* mesh = load(bytes); |
| 147 | + dtNavMeshQuery* query = dtAllocNavMeshQuery(); |
| 148 | + check(dtStatusSucceed(query->init(mesh, 2048))); |
| 149 | + dtQueryFilter filter; |
| 150 | + const float extents[3] = {1, 2, 1}, start[3] = {-5, 0, -5}, end[3] = {5, 0, -5}; |
| 151 | + dtPolyRef start_ref = 0, end_ref = 0; |
| 152 | + float start_at[3], end_at[3]; |
| 153 | + query->findNearestPoly(start, extents, &filter, &start_ref, start_at); |
| 154 | + query->findNearestPoly(end, extents, &filter, &end_ref, end_at); |
| 155 | + dtPolyRef polys[256]; |
| 156 | + int poly_count = 0, corner_count = 0; |
| 157 | + float corners[3 * 32]; |
| 158 | + check(dtStatusSucceed(query->findPath(start_ref, end_ref, start_at, end_at, &filter, polys, &poly_count, 256))); |
| 159 | + check(dtStatusSucceed(query->findStraightPath(start_at, end_at, polys, poly_count, corners, nullptr, nullptr, &corner_count, 32))); |
| 160 | + float through = -100; |
| 161 | + for (int corner = 0; corner < corner_count; ++corner) through = std::max(through, corners[3 * corner + 2]); |
| 162 | + check(corner_count >= 3 && through > 6.0f); |
| 163 | + float hit = 0, normal[3]; |
| 164 | + dtPolyRef ray_polys[64]; |
| 165 | + int ray_count = 0; |
| 166 | + check(dtStatusSucceed(query->raycast(start_ref, start_at, end_at, &filter, &hit, normal, ray_polys, &ray_count, 64)) && hit > 0.4f && hit < 0.5f); |
| 167 | + |
| 168 | + dtCrowd* crowd = dtAllocCrowd(); |
| 169 | + check(crowd->init(8, 0.4f, mesh)); |
| 170 | + dtCrowdAgentParams agent{}; |
| 171 | + agent.radius = 0.4f, agent.height = 2, agent.maxAcceleration = 8, agent.maxSpeed = 3.5f; |
| 172 | + agent.collisionQueryRange = agent.radius * 12, agent.pathOptimizationRange = agent.radius * 30, agent.separationWeight = 2; |
| 173 | + agent.updateFlags = DT_CROWD_ANTICIPATE_TURNS | DT_CROWD_OPTIMIZE_VIS | DT_CROWD_OPTIMIZE_TOPO | DT_CROWD_OBSTACLE_AVOIDANCE | DT_CROWD_SEPARATION; |
| 174 | + agent.obstacleAvoidanceType = 3; |
| 175 | + for (int index = 0; index < 4; ++index) { |
| 176 | + const float at[3] = {-5, 0, -6.0f + static_cast<float>(index)}; |
| 177 | + check(crowd->addAgent(at, &agent) == index); |
| 178 | + check(crowd->requestMoveTarget(index, end_ref, end_at)); |
| 179 | + } |
| 180 | + for (int step = 0; step < 600; ++step) crowd->update(1.0f / 60, nullptr); |
| 181 | + std::vector<float> places; |
| 182 | + for (int index = 0; index < 4; ++index) { |
| 183 | + const dtCrowdAgent* walker = crowd->getAgent(index); |
| 184 | + places.insert(places.end(), walker->npos, walker->npos + 3); |
| 185 | + check(dtVdist2D(walker->npos, end_at) < 2.5f); |
| 186 | + } |
| 187 | + check(fnv(bytes) == 0xd50409b3475ce9afull && fnv(places) == 0x040f1c330fce843bull); |
| 188 | + check(nav::DT_VERTS_PER_POLYGON == 6 && nav::RC_WALKABLE_AREA == 63 && dtStatusFailed(nav::DT_FAILURE)); |
| 189 | + std::println("Recast & Detour 1.6.0 through import mcpplibs.mwrap.recastnavigation: {} polygon refs to the far side, {} corners; the mesh {:#018x}, the crowd {:#018x}{}", |
| 190 | + poly_count, corner_count, fnv(bytes), fnv(places), failures ? std::format(" — {} checks failed", failures) : ""); |
| 191 | + dtFreeCrowd(crowd), dtFreeNavMeshQuery(query), dtFreeNavMesh(mesh); |
| 192 | + return failures; |
| 193 | +} |
0 commit comments