Atoms Crowd  7.0.0
NavMesh.h
1 #pragma once
2 // ===========================================================================
3 // Copyright (c) 2015 Toolchefs Ltd. All rights reserved.
4 //
5 // Use of this software is subject to the terms of the Toolchefs license
6 // agreement provided at the time of installation or download, or which
7 // otherwise accompanies this software in either electronic or hard copy form.
8 // ===========================================================================
9 
10 #include <AtomsUtils/Globals.h>
11 #include <AtomsUtils/Mesh.h>
12 #ifndef ATOMS_UNREAL
13 #include <AtomsUtils/NavigationMesh/Recast/Recast.h>
14 #include <AtomsUtils/NavigationMesh/Detour/DetourNavMesh.h>
15 #include <AtomsUtils/NavigationMesh/Detour/DetourNavMeshQuery.h>
16 #include <AtomsUtils/NavigationMesh/RecastContext.h>
17 #endif
18 
19 namespace AtomsUtils
20 {
21 #ifdef ATOMS_UNREAL
22  class ATOMSUTILS_EXPORT dtNavMeshQuery
23  {
24  public:
25  dtNavMeshQuery() {}
26  ~dtNavMeshQuery() {}
27  };
28 #endif
29 
30  typedef dtNavMeshQuery NavMeshQuery;
31 
32  class ATOMSUTILS_EXPORT NavMeshPath
33  {
34  public:
35 
36  std::vector<AtomsMath::Vector3f> points;
37 #ifndef ATOMS_UNREAL
38  std::vector<dtPolyRef> polygons;
39 #endif
40  std::vector<float> costs;
41  std::vector<unsigned int> customLinks;
42  };
43 
44  class ATOMSUTILS_EXPORT NavMesh
45  {
46  public:
47 
48  class ATOMSUTILS_EXPORT Config
49  {
50  public:
51 
52  enum PartitionType
53  {
54  SAMPLE_PARTITION_WATERSHED,
55  SAMPLE_PARTITION_MONOTONE,
56  SAMPLE_PARTITION_LAYERS,
57  };
58 
59  Config();
60 
61  public:
62  float m_cellSize;
63  float m_cellHeight;
64  float m_agentHeight;
65  float m_agentRadius;
66  float m_agentMaxClimb;
67  float m_agentMaxSlope;
68  float m_regionMinSize;
69  float m_regionMergeSize;
70  float m_edgeMaxLen;
71  float m_edgeMaxError;
72  float m_vertsPerPoly;
73  float m_detailSampleDist;
74  float m_detailSampleMaxError;
75  int m_partitionType;
76  float m_tileSize;
77  bool m_filterLowHangingObstacles;
78  bool m_filterLedgeSpans;
79  bool m_filterWalkableLowHeightSpans;
80  };
81 
82  NavMesh();
83  virtual ~NavMesh();
84 
85  bool build(const AtomsUtils::Mesh& mesh, const Config& config, bool asTiles = false);
86 
87  unsigned char* buildTileMesh(const AtomsUtils::Mesh& mesh, const Config& config, const int tx, const int ty, const float* bmin, const float* bmax, int& dataSize);
88  void getTilePos(const AtomsUtils::Mesh& mesh, const Config& config, const float* pos, int& tx, int& ty, const float* bmin, const float* bmax);
89  void buildTile(const AtomsUtils::Mesh& mesh, const Config& config, const float* pos, const float* bmin, const float* bmax);
90  void removeTile(const AtomsUtils::Mesh& mesh, const Config& config, const float* pos, const float* bmin, const float* bmax);
91  void buildAllTiles(const AtomsUtils::Mesh& mesh, const Config& config, const float* bmin, const float* bmax);
92  void removeAllTiles(const AtomsUtils::Mesh& mesh, const Config& config, const float* bmin, const float* bmax);
93 
94  void buildPolyMesh();
95 
96  const AtomsUtils::Mesh& polyMesh();
97 
98  void clear();
99 
100  void setBoundingBox(const AtomsMath::Box3f& box);
101 
102  virtual void findPath(const AtomsMath::Vector3f& start, const AtomsMath::Vector3f& end, NavMeshPath& path, const dtNavMeshQuery* m_navQuery = nullptr);
103 
104  virtual unsigned int findCorridorPath(const AtomsMath::Vector3f& start, const AtomsMath::Vector3f& end, NavMeshPath& path, const dtNavMeshQuery* m_navQuery = nullptr);
105 
106  virtual bool findStraightPath(const AtomsMath::Vector3f& start, const AtomsMath::Vector3f& end, NavMeshPath& path, const dtNavMeshQuery* m_navQuery = nullptr);
107 
108  virtual bool projectPointToNavMesh(const AtomsMath::Vector3f& point, AtomsMath::Vector3f& result, const AtomsMath::Vector3f& extent, const dtNavMeshQuery* m_navQuery = nullptr);
109 
110  virtual bool pathCost(const AtomsMath::Vector3f& pathStart, const AtomsMath::Vector3f& pathEnd, float& cost, const dtNavMeshQuery* m_navQuery = nullptr);
111 
112  virtual bool pathLength(const AtomsMath::Vector3f& pathStart, const AtomsMath::Vector3f& pathEnd, float& length, const dtNavMeshQuery* m_navQuery = nullptr);
113 
114  virtual bool pathConstAndLength(const AtomsMath::Vector3f& pathStart, const AtomsMath::Vector3f& pathEnd, float& cost, float& length, const dtNavMeshQuery* m_navQuery = nullptr);
115 
116  virtual bool raycast(const AtomsMath::Vector3f& rayStart, const AtomsMath::Vector3f& rayEnd, AtomsMath::Vector3f& hitPoint, bool& hit, const dtNavMeshQuery* m_navQuery = nullptr);
117 
118  virtual bool randomPoint(AtomsMath::Vector3f& result, int seed, const dtNavMeshQuery* m_navQuery = nullptr);
119 
120  virtual bool randomReachablePointInRadius(const AtomsMath::Vector3f& origin, int seed, float radius, AtomsMath::Vector3f& result, const dtNavMeshQuery* m_navQuery = nullptr);
121 
122  virtual bool findDistanceToWall(const AtomsMath::Vector3f& point, const float radius, float& hitDist, AtomsMath::Vector3f& hitPos, AtomsMath::Vector3f& hitNormal, const AtomsMath::Vector3f& extent = AtomsMath::Vector3f(1.0, 1.0, 1.0), const dtNavMeshQuery* m_navQuery = nullptr);
123 
124  virtual void getPolyBoundaries(std::vector<AtomsMath::Vector3f>& segmentVerts, int& segmentCount, bool inner, const NavMeshQuery* m_navQuery = nullptr);
125 
126  virtual void initQuery(dtNavMeshQuery* navQuery);
127 
128  virtual bool getEdgeBorderInRadius(const AtomsMath::Vector3f& position, double radius, std::vector<AtomsMath::Vector3f>& lines);
129  virtual bool getEdgeBorderInRadius(const AtomsMath::Vector3& position, double radius, std::vector<AtomsMath::Vector3>& lines);
130 
131  inline const AtomsMath::Box3f& boundingBox() const { return m_bbox; }
132 #ifndef ATOMS_UNREAL
133  inline const RecastContext* recastContext() const { return m_ctx; };
134  inline const dtNavMesh* recastNavMesh() const { return m_navMesh; };
135  inline const dtNavMeshQuery& recastNavMeshQuery() const { return m_navQuery; };
136  inline const dtQueryFilter& recastNavMeshQueryFilter() const { return m_navQueryFilter; };
137 #endif
138 
139  protected:
140 #ifndef ATOMS_UNREAL
141  void PostProcessPath(dtStatus FindPathStatus, NavMeshPath& Path,
142  const dtNavMeshQuery& NavQuery, const dtQueryFilter* Filter,
143  int StartPolyID, int EndPolyID,
144  AtomsMath::Vector3f StartLoc, AtomsMath::Vector3f EndLoc,
145  AtomsMath::Vector3f RecastStartPos, AtomsMath::Vector3f RecastEndPos,
146  dtQueryResult& PathResult);
147 #endif
148 
149  protected:
150 
151  Mesh m_navPolyMesh;
152 
153  AtomsMath::Box3f m_bbox;
154 #ifndef ATOMS_UNREAL
155  RecastContext* m_ctx;
156  dtNavMesh* m_navMesh;
157  dtNavMeshQuery m_navQuery;
158  dtQueryFilter m_navQueryFilter;
159 #endif
160 
161  };
162 
163 }
Mesh class.
Definition: Mesh.h:30
Definition: NavMesh.h:49
Definition: NavMesh.h:45
Definition: NavMesh.h:33
Base64 encode/decode.
Definition: RecastContext.h:20
Definition: DetourNavMeshQuery.h:323
AtomsCore namespace.
Definition: Base64.h:13