gwnavruntime/spatialization/spatializedpoint.h Source File

spatializedpoint.h
Go to the documentation of this file.
1 /*
2 * Copyright 2016 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 
8 #pragma once
9 
10 
15 
16 namespace Kaim
17 {
18 
19 class DatabaseBinding;
20 class Database;
21 class PositionSpatializationRange;
22 class WorldIntegerPos;
23 class NavTrianglePtr;
24 
25 class Bot;
26 class CylinderObstacle;
27 class BoxObstacle;
28 class PointOfInterest;
29 
30 
33 {
34  SpatializedPointObjectType_Undefined = 0,
35  SpatializedPointObjectType_Bot,
36  SpatializedPointObjectType_CylinderObstacle,
37  SpatializedPointObjectType_BoxObstacle,
38  SpatializedPointObjectType_PointOfInterest,
39 
40  SpatializedPointObjectType_Count
41 };
42 
43 enum SpatializationUpdateCoherency
44 {
45  SpatializationUpdateCoherency_Maximum, // The update is conservative, the current floor is flavored
46  SpatializationUpdateCoherency_Minimum, // The update won't favor current floor, so it will ease vertical transition
47 };
48 
49 // { NavTriangle, navMeshChangeIdx }
50 class PointDbSpatialization
51 {
52  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_Spatialization)
53 public:
54  PointDbSpatialization() : m_navMeshChangeIdx(KyUInt32MAXVAL) {}
55 
56  // calls UpdateFromScratch() if the RayCanGo fails
57  void UpdateFromPrevious(
58  Database* database, const PositionSpatializationRange& range,
59  const Vec3f& prevPosition, const PointDbSpatialization& prevDbSpatialization, const Vec3f& newPosition);
60 
61  void UpdateFromScratch(
62  Database* database, const PositionSpatializationRange& range,
63  const Vec3f& newPosition, const WorldIntegerPos* newPositionInteger = nullptr);
64 
65 public:
66  static PositionSpatializationRange ComputeRange(
67  const Database* database, SpatializationUpdateCoherency updateMode, KyFloat32 height = 0.f);
68 
69 public:
70  NavTriangleRawPtr m_navTriangle;
71  KyUInt32 m_navMeshChangeIdx;
72 };
73 
74 
75 // Maintain { NavTriangle, navMeshChangeIdx } for each database (in the database binding).
76 // The 3D position is not part of PointSpatialization, it's part of SpatializedPoint.
77 // PointSpatialization is used in SpatializedPoint and in Bot. Merging both classes into one could make things simpler.
78 class PointSpatialization
79 {
80  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_Spatialization)
81  KY_CLASS_WITHOUT_COPY(PointSpatialization)
82 
83 public:
84  PointSpatialization() : m_pointDbSpatialization(nullptr) {}
85 
86  void Init(DatabaseBinding* databaseBinding);
87  void Clear(); // reset the state as if freshly constructed.
88 
89  void InvalidateAllDbSpatializations(); // called from Bot, not from SpatializedPoint
90  bool OneBoundDatabaseNavMeshHasChanged(); // called from Bot, not from SpatializedPoint
91 
92  NavTrianglePtr GetNavTrianglePtr(Database* database) const;
93 
94 public: // internal
95  // does not update navFloors, called in Bot::GetAndUpdatePositionSpatialiaation
96  void UpdateSpatializationButNotFloors(const Vec3f& prevPosition, const PointSpatialization& prevPointSpatialization, const Vec3f& newPosition, SpatializationUpdateCoherency updateMode, KyFloat32 height);
97 
98 private:
99  void CreateDbSpatializationPoints();
100  void DestroyDbSpatializationPoints();
101  bool BoundDatabaseNavMeshHasChanged(KyUInt32 boundDatabaseIndex);
102 
103 public: // internal
104  Ptr<DatabaseBinding> m_databaseBinding;
105  PointDbSpatialization* m_pointDbSpatialization; // array indexed like m_databaseBinding
106  // Note: most of the time, there is only 1 database, and we alloc 8 bytes.
107  // we should have a small array optimization with a union { PointDbSpatialization, PointDbSpatialization* }
108 };
109 
112 {
113 public:
114  // ---------------------------------- Main API Functions ----------------------------------
115 
117 
119  void SetDefaults();
120 
121  // ---------------------------------- Initialization ----------------------------------
122 
123  // All these 4 function do quite the same thing and have bad taste dependency (even if forward declared) to Bot, CylinderObstacle, BoxObstacle, PointOfInterest
124  // TODo: kill them
125  void InitFromBot( Ptr<DatabaseBinding> databaseBinding, Bot* bot , KyFloat32 height);
126  void InitFromCylinderObstacle(Ptr<DatabaseBinding> databaseBinding, CylinderObstacle* cylinderObstacle, KyFloat32 height);
127  void InitFromBoxObstacle( Ptr<DatabaseBinding> databaseBinding, BoxObstacle* boxObstacle , KyUInt32 indexInObject, KyFloat32 height);
128  void InitFromPointOfInterest( Ptr<DatabaseBinding> databaseBinding, PointOfInterest* pointOfInterest , KyFloat32 height);
129 
130  // ---------------------------------- Public Data Members ----------------------------------
131 
134  Ptr<DatabaseBinding> m_databaseBinding;
135  void* m_object; // Bot, CylinderObstacle, BoxObstacle or PointOfInterest
136  SpatializedPointObjectType m_objectType; // Bot, CylinderObstacle, BoxObstacle or PointOfInterest
137  KyUInt32 m_indexInObject; // Relevant in the case of BoxObstacle, always zero otherwise
138  KyFloat32 m_height;
139 };
140 
141 
146 class SpatializedPoint : public RefCountBase<SpatializedPoint, MemStat_Spatialization>
147 {
148  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_Spatialization)
150 
151 public:
152  // ---------------------------------- Main API Functions ----------------------------------
153 
155  ~SpatializedPoint() { Clear(); }
156 
157  void Init(const SpatializedPointInitConfig& initConfig);
158  void Clear(); // reset SpatializedPoint as if it was freshly constructed
159 
160  void InvalidateAllDbSpatializations();
161 
162  // ---------------------------------- Getters ----------------------------------
163 
164  const DatabaseBinding* GetDatabaseBinding() const { return m_pointSpatialization.m_databaseBinding; }
165  KyUInt32 GetDbSpatializationCount() const { return m_pointSpatialization.m_databaseBinding->GetBoundDatabaseCount(); }
166 
167  const PointDbSpatialization& GetDbSpatialization(KyUInt32 boundDatabaseIdx) const { return m_pointSpatialization.m_pointDbSpatialization[boundDatabaseIdx]; }
168 
169  const Vec3f& GetPosition() const { return m_position; }
170  KyFloat32 GetHeight() const { return m_height; }
171  void* GetObject() const { return m_object; }
172  SpatializedPointObjectType GetObjectType() const { return m_objectType; }
173  KyUInt32 GetIndexInObject() const { return m_indexInObject; }
174 
175  NavTrianglePtr GetNavTrianglePtr(Database* database) const;
176 
177  bool GetHasChanged() const { return m_hasChanged; }
178 
179  const PointSpatialization& GetPointSpatialization() const { return m_pointSpatialization; }
180 
181 public: // internal
182  // Just sets the position. Does not update any spatialization information.
183  void SetPosition(const Vec3f& position) { m_position = position; }
184 
185  // Just sets the height. Does not update any spatialization information.
186  void SetHeight(KyFloat32 height) { m_height = height; }
187 
188  // Computes m_pointSpatialization from newPosition and update NavFloors->SpatializedPoints accordingly
189  // Returns Spatialization has changed, i.e. at least one PointDbSpatialization is spatialized differently
190  void UpdateSpatialization(const Vec3f& newPosition);
191 
192  // Copy m_pointSpatialization from newPointSpatialization and update NavFloors->SpatializedPoints accordingly
193  // Returns Spatialization has changed, i.e. at least one PointDbSpatialization is spatialized differently
194  void UpdateSpatialization(const Vec3f& newPosition, const PointSpatialization& newPointSpatialization);
195 
196  // When a navFloor is deactivated, this function will invalidate all
197  // references to it in this SpatializedPoint.
198  void OnNavFloorDeActivation(NavFloor* navFloor);
199 
200  void SetHasChanged(bool hasChanged) { m_hasChanged = hasChanged; }
201 
202  DatabaseBinding* GetDatabaseBinding() { return m_pointSpatialization.m_databaseBinding; }
203 
204  static const char* GetObjectTypeDescrition(SpatializedPointObjectType objectType);
205 
206 private:
207  PointDbSpatialization& GetDbSpatialization(KyUInt32 boundDatabaseIdx) { return m_pointSpatialization.m_pointDbSpatialization[boundDatabaseIdx]; }
208  void Invalidate(KyUInt32 boundDatabaseIdx);
209 
210 private:
211  Vec3f m_position; // position last time the spatialization was updated, shared by all databases
212  PointSpatialization m_pointSpatialization;
213  void* m_object; // Bot, CylinderObstacle, BoxObstacle or PointOfInterest
214  SpatializedPointObjectType m_objectType; // Bot, CylinderObstacle, BoxObstacle or PointOfInterest
215  KyUInt32 m_indexInObject; // Relevant in the case of BoxObstacle, always zero otherwise
216  KyFloat32 m_height;
217  bool m_hasChanged; // When a NavFloor Allow to force to send visual debug of spatialization when it goes outside navmesh
218 };
219 
220 
221 } // namespace Kaim
222 
CylinderObstacles represent dynamic objects of your game engine that prevent your Bots from moving fr...
Definition: cylinderobstacle.h:76
This class is the world element that represent an active character in Autodesk Navigation.
Definition: bot.h:128
This class is a runtime wrapper of a NavFloorBlob, it gathers all the runtime information associated ...
Definition: navfloor.h:32
std::uint32_t KyUInt32
uint32_t
Definition: types.h:29
void SetDefaults()
Sets all members to their default value.
Definition: spatializedpoint.cpp:415
#define KY_CLASS_WITHOUT_COPY(ClassName)
Define to forbid copy constructor and copy assignment.
Definition: types.h:196
Ptr< DatabaseBinding > m_databaseBinding
Defines the Databases in which the SpatializedPoint will be spatialized.
Definition: spatializedpoint.h:134
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:132
Class used to initialize a SpatializedPoint.
Definition: spatializedpoint.h:111
This class is a runtime container for all NavData that represents the world from the point of view of...
Definition: database.h:57
DatabaseBinding is a collection of the databases to be used for world elements spatialization.
Definition: databasebinding.h:21
SpatializedPointObjectType
Enumerates the possible object types the SpatializedPoint refers to.
Definition: spatializedpoint.h:32
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
Each instance of this class uniquely identifies a single NavTriangle in a NavFloor.
Definition: navtriangleptr.h:17
PointOfInterest is a tagged position spatialized into the NavMesh.
Definition: pointofinterest.h:71
SpatializedPoint is used to the NavTriangle of "object": Bot, CylinderObstacle, BoxObstacle and Point...
Definition: spatializedpoint.h:146
Each instance of the BoxObstacle class represents a dynamic, physical object in your game engine that...
Definition: boxobstacle.h:127
#define KyUInt32MAXVAL
KyUInt32 max value
Definition: types.h:68
float KyFloat32
float
Definition: types.h:32
3d vector using 32bits floating points.
Definition: vec3f.h:16