#ifndef NAVMESHRANDOM_H #define NAVMESHRANDOM_H #include #include #include "../math/DrawList.h" #include "../geo/Point3.h" #include "NavMeshLocation.h" template class NavMeshRandom { std::minstd_rand gen; std::uniform_real_distribution dOnTriangle = std::uniform_real_distribution(0.0f, 1.0f); const std::vector& triangles; DrawList lst; public: struct Result { Point3 pos; size_t triaIdx; Result(const Point3 pos, const size_t triaIdx) : pos(pos), triaIdx(triaIdx) {;} }; /** ctor */ NavMeshRandom(const std::vector& triangles) : triangles(triangles) { for (size_t idx = 0; idx < triangles.size(); ++idx) { lst.add(idx, triangles[idx]->getArea()); } } /** draw a random point within the map */ NavMeshLocation draw() { const size_t idx = lst.get(); const Tria* tria = triangles[idx]; while (true) { const float u = dOnTriangle(gen); const float v = dOnTriangle(gen); if (u+v > 1) {continue;} const Point3 pos = tria.getA() + (tria.getAB() * u) + (tria.getAC() * v); return NavMeshLocation(pos, tria); } } }; #endif // NAVMESHRANDOM_H