This repository has been archived on 2020-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
Files
Indoor/navMesh/walk/NavMeshSub.h
k-a-z-u fee6cd3496 worked on navMesh stuff
- creation
- walking
- helper
2018-01-10 16:57:19 +01:00

83 lines
1.7 KiB
C++

#ifndef NAVMESHSUB_H
#define NAVMESHSUB_H
#include "../NavMesh.h"
#include "../NavMeshLocation.h"
#include "../NavMeshRandom.h"
#include <vector>
#include <unordered_set>
namespace NM {
template <typename Tria> class NavMeshSub {
std::vector<const Tria*> toVisit;
public:
NavMeshSub(const NavMeshLocation<Tria>& loc, float radius_m) {
build(loc,radius_m);
}
/** does this submesh contain the given point? */
bool contains(const Point2 p2) const {
for (const Tria* t : toVisit) {
if (t->contains(p2)) {return true;}
}
return false;
}
/** get the triangle that contains the given point (if any) */
const Tria* getContainingTriangle(const Point2 p2) const {
for (const Tria* t : toVisit) {
if (t->contains(p2)) {return t;}
}
return nullptr;
}
/** perform random operations on the submesh */
NavMeshRandom<Tria> getRandom() {
return NavMeshRandom<Tria>(toVisit);
}
private:
void build(const NavMeshLocation<Tria>& loc, float radius_m) {
std::unordered_set<const Tria*> visited;
// starting-triangle + all its (max 3) neighbors
toVisit.push_back(loc.tria);
visited.insert(loc.tria);
for (const auto* n : *loc.tria) {
toVisit.push_back( (const Tria*)n );
}
size_t next = 1; // start with the first neighbor (skip starting triangle itself)
while (next < toVisit.size()) {
// next triangle
const NavMeshTriangle* cur = toVisit[next]; ++next;
// neighbors
for (const auto* n : *cur) {
const Tria* t = (const Tria*) n;
const float dist = loc.pos.getDistance(n->getCenter());
if (dist > radius_m) {continue;}
if (visited.find(t) != visited.end()) {continue;}
toVisit.push_back(t);
visited.insert(t);
}
}
}
};
}
#endif // NAVMESHSUB_H