gwnavruntime/querysystem/iquery.h Source File

iquery.h
Go to the documentation of this file.
1 /*
2 * Copyright 2015 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 // primary contact: JAPA - secondary contact: NOBODY
8 #ifndef Navigation_IQuery_H
9 #define Navigation_IQuery_H
10 
16 
17 
18 namespace Kaim
19 {
20 
21 class World;
22 class Database;
23 class WorkingMemory;
24 class QueryQueue;
25 
28 enum QueryType
29 {
30  TypeRayCanGo = 0,
31  TypeRayCast,
32  TypeDiskCanGo,
33  TypeDiskCast,
34  TypeDiskCollision,
35  TypeAStar,
36  TypeTriangleFromPos,
37  TypeMoveOnNavMesh,
38  TypeSpatializedPointCollectorInAABB,
39  TypeTriangleFromPosAndTriangleSeed,
40  TypeNearestBorderHalfEdgeFromPos,
41  TypeSegmentCast,
42  TypeSegmentCanGo,
43  TypeDiskExpansion,
44  TypeInsidePosFromOutsidePos,
45  TypeMultipleFloorTrianglesFromPos,
46  TypeRayCanGoOnSegment,
47  TypeRayCastOnSegment,
48  TypeDynamicNavMesh,
49  TypeFindFirstVisiblePositionOnPath,
50  TypeBestGraphVertexPathFinderQuery,
51  TypeMakeNavFloorStitchQuery,
52  TypeCollisionRayCast,
53  TypeRayCastFull3D,
54  TypePathFromPolyline,
55  TypeTagVolumesFromPos,
56  TypeMultiDestinationPathFinderQuery,
57  TypeCircleArcSplineComputation,
58 
59  QueryType_FirstCustom
60 };
61 
62 // Gives the context in which an IAtomicQuery's PerformQuery function is called
63 // in order to monitor when such queries are performed,
64 // used by ScopedPerformedQueryCounter
66 {
67  QueryStat_Generation,
68  QueryStat_Spatialization,
69  QueryStat_NavGraph,
70  QueryStat_PathFinder,
71  QueryStat_ProgressOnPath,
72  QueryStat_PathValidityInterval,
73  QueryStat_TargetOnPathIsStillVisible,
74  QueryStat_TargetOnPathShortcutForward,
75  QueryStat_TargetOnPathSearchBackward,
76  QueryStat_Avoidance,
77  QueryStat_Channel,
78  QueryStat_Spline,
79 
80  QueryStat_GameKit,
81  QueryStat_LabEngine,
82 
84 
85  PerformQueryStat_Count,
86  PerformQueryStat_Unspecified = PerformQueryStat_Count,
87 };
88 
91 {
92  QueryNotStarted = 0,
93  QueryInProcess,
94  QueryDone,
95  QueryCanceled,
96  QueryResultMax,
97 };
98 
101 {
102  QueryNotInQueue,
103  QueryPushedAsCommand,
104  QueryInQueryQueue,
105 };
106 
107 enum QueryProcessMode
108 {
109  QueryProcessMode_Sync,
110  QueryProcessMode_ASync
111 };
112 
117 class IOnDone : public RefCountBase<IOnDone, MemStat_Query>
118 {
119 public:
120  // ---------------------------------- Public Member Functions ----------------------------------
121  virtual ~IOnDone() {}
122  virtual void OnDone() = 0;
123 };
124 
126 class IQuery : public RefCountBase<IQuery, MemStat_Query>
127 {
128  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_Query)
129 public:
130  IQuery();
131  virtual ~IQuery();
132 
133  // ---------------------------------- Main API Functions ----------------------------------
134 
137  bool CanBeInitialized() const;
138 
142  bool IsInAQueryQueue() const;
143 
147  bool IsSafeToReadResult() const;
148 
152  void PerformQueryBlocking(WorkingMemory* workingMemory = KY_NULL);
153 
159  void OnDone();
160 
161 
162  // Function that can be used to send a query to the NavigationLab.
163  // Currently the implementation transform the query using a ScopedDisplayList,
164  // and the display list is shown only at the frame SendVisualDebug is called,
165  KyResult SendVisualDebug();
166 
168  void SetTraverseLogicUserData(void* traverseLogicUserData);
169  void* GetTraverseLogicUserData() const;
170 
171  // ---------------------------------- Pure virtual functions - Query framework contract ----------------------------------
172 
173  virtual QueryType GetType() const = 0;
174 
183  virtual void Advance(WorkingMemory* workingMemory) = 0;
184 
185 public: // internal
186  // ---------------------------------- Internal Virtual functions for NavigationLab Remote Query processing ----------------------------------
194 
195  virtual Ptr<BaseBlobHandler> CreateQueryBlobHandler() { return KY_NULL; }
196  virtual void BuildQueryBlob(Kaim::BaseBlobHandler* /*blobHandler*/) {}
197  virtual void InitFromQueryBlob(World* /*world*/, void* /*blob*/) {}
198 
199 public: // internal
200  static const char* GetQueryTypeName(QueryType queryType);
201 
202  // ---------------------------------- Mandatory initialization calls to be done by derived classes ----------------------------------
203 
207  void BindToDatabase(Database* database);
208 
215  void Initialize();
216 
217 public:
218  // ---------------------------------- Public Data Members ----------------------------------
219 
223  Ptr<IOnDone> m_onDone;
224 
225 public: // Internal
226  Database* m_database;
228 
229  QueryQueue* m_queue;
231  KyUInt32 m_queryInfoId;
232 };
233 
234 
237 class IAtomicQuery : public IQuery
238 {
239 public:
240  IAtomicQuery();
241  virtual ~IAtomicQuery() {}
242 
243  void SetPerformQueryStat(PerformQueryStat contextOfPerformQueryCall);
244  PerformQueryStat GetPerformQueryStat();
245 
246 public:
247  PerformQueryStat m_performQueryStat; // used by ScopedPerformedQueryCounter to know in which context PerformQuery was called
248 };
249 
250 
252 class ITimeSlicedQuery : public IQuery
253 {
254 public:
255  ITimeSlicedQuery();
256  virtual ~ITimeSlicedQuery();
257 
258  // ---------------------------------- Public Member Functions ----------------------------------
259 
263  void Initialize();
264 
265 
266  // ---------------------------------- Pure virtual functions ----------------------------------
267 
271  virtual void ReleaseWorkingMemoryOnCancelDuringProcess(WorkingMemory* workingMemory) = 0;
273 public:
274  KyUInt32 m_advanceCount;
275  KyUInt32 m_lastAdvanceFrameIdx;
276 };
280 {
283 };
284 
285 
286 //------------------------------------- IQuery -----------------------------------------------
287 
288 KY_INLINE IQuery::IQuery() :
289  m_processStatus(QueryNotStarted),
290  m_database(KY_NULL),
291  m_traverseLogicUserData(KY_NULL),
292  m_queue(KY_NULL),
293  m_inQueueStatus(QueryNotInQueue),
294  m_queryInfoId(0)
295 {}
296 
297 KY_INLINE IQuery::~IQuery() {}
298 
299 KY_INLINE void IQuery::BindToDatabase(Database* database)
300 {
301  KY_LOG_ERROR_IF(database == KY_NULL, ("The database you are bounding the query to is not valid."));
302  KY_LOG_ERROR_IF(CanBeInitialized() == false, ("You cannot bind a query to a Database while it is registered in a QueryQueue."));
303  m_database = database;
305 }
307 KY_INLINE void IQuery::Initialize()
308 {
309  KY_LOG_ERROR_IF(m_database == KY_NULL, ("This query must have been bound to a valid Database before calling Initialize"));
310  KY_LOG_ERROR_IF(CanBeInitialized() == false, ("You cannot initialized a query while it is registered in a QueryQueue."));
311  m_processStatus = QueryNotStarted;
312 }
313 
314 KY_INLINE bool IQuery::CanBeInitialized() const { return m_inQueueStatus != QueryInQueryQueue; }
315 KY_INLINE bool IQuery::IsInAQueryQueue() const { return m_inQueueStatus != QueryNotInQueue; }
316 KY_INLINE bool IQuery::IsSafeToReadResult() const { return (IsInAQueryQueue() == false) && (m_processStatus == QueryDone); }
317 
318 KY_INLINE void* IQuery::GetTraverseLogicUserData() const { return m_traverseLogicUserData; }
319 KY_INLINE void IQuery::SetTraverseLogicUserData(void* traverseLogicUserData) { m_traverseLogicUserData = traverseLogicUserData; }
320 
321 KY_INLINE void IQuery::OnDone()
322 {
323  if (m_onDone != KY_NULL)
324  m_onDone->OnDone();
325 
326  m_onDone = KY_NULL;
327 }
328 
329 //------------------------------------- IAtomicQuery -----------------------------------------------
330 
331 KY_INLINE IAtomicQuery::IAtomicQuery() : m_performQueryStat(PerformQueryStat_Unspecified) {}
332 KY_INLINE void IAtomicQuery::SetPerformQueryStat(PerformQueryStat performQueryStat) { m_performQueryStat = performQueryStat; }
333 KY_INLINE PerformQueryStat IAtomicQuery::GetPerformQueryStat() { return m_performQueryStat; }
334 
335 //------------------------------------- ITimeSlicedQuery -----------------------------------------------
336 
337 KY_INLINE ITimeSlicedQuery::ITimeSlicedQuery() : m_advanceCount(0), m_lastAdvanceFrameIdx(0) {}
338 KY_INLINE ITimeSlicedQuery::~ITimeSlicedQuery() {}
339 KY_INLINE void ITimeSlicedQuery::Initialize()
340 {
342  m_advanceCount = 0;
343  m_lastAdvanceFrameIdx = 0;
344 }
345 
347 }
348 
349 #endif
KyInt32 KyResult
Defines a type that can be returned by methods or functions in the Gameware Navigation SDK to indicat...
Definition: types.h:254
QueryProcessStatus m_processStatus
Modified by the query within Advance().
Definition: iquery.h:272
CommonQueryResult
Enumerates the result codes that are common to all queries.
Definition: iquery.h:344
virtual void Advance(WorkingMemory *workingMemory)=0
This function is called by the QueryQueue to process one step on the query.
virtual void ReleaseWorkingMemoryOnCancelDuringProcess(WorkingMemory *workingMemory)=0
Called when a query is canceled in FlushCommands while its status is QueryInProcess to make sure that...
#define KY_NULL
Null value.
Definition: types.h:247
bool IsSafeToReadResult() const
Returns true if the query has been processed, is no longer in a queue, and OnDone (if there is one) h...
Definition: iquery.h:381
bool CanBeInitialized() const
In the case of a query processed in a QueryQueue, this must be tested before initializing the query...
Definition: iquery.h:379
Ptr< IOnDone > m_onDone
The optional IOnDone instance to be called. Must be set manually by the user. See IOnDone...
Definition: iquery.h:273
QueryType
Enumerates all the type of query.
Definition: iquery.h:29
Small interface class that you have to derive from and set to an IQuery to get the IOnDone::OnDone() ...
Definition: iquery.h:119
Definition: gamekitcrowddispersion.h:20
Indicates that the query has not yet been initialized.
Definition: iquery.h:346
QueryProcessStatus
Enumerates all the processing status a query can have.
Definition: iquery.h:91
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:137
void BindToDatabase(Database *database)
Should be called by the derived class before Initializing the query It sets m_database to the provide...
Definition: iquery.h:364
bool IsInAQueryQueue() const
Returns true if the query is currently registered in a QueryQueue.
Definition: iquery.h:380
QueryStatusInQueue m_inQueueStatus
Used by the QueryQueue. Do not modify.
Definition: iquery.h:281
PerformQueryStat
Definition: iquery.h:66
void PerformQueryBlocking(WorkingMemory *workingMemory=0)
Process the query at once whether it is time-sliced or not.
unsigned int KyUInt32
Type used internally to represent an unsigned 32-bit integer.
Definition: types.h:36
QueryQueue * m_queue
Updated by the QueryQueue. Do not modify.
Definition: iquery.h:280
Indicates that the query has not yet been launched.
Definition: iquery.h:347
void * m_traverseLogicUserData
This userData is typically passed by the Bot.
Definition: iquery.h:278
can be used sporadically to differentiate from PerformQueryStat_Unspecified
Definition: iquery.h:84
void Initialize()
Should be called by the derived class before trying to perform the query or to push it in a QueryQueu...
Definition: iquery.h:404
void Initialize()
Should be called by the derived class before trying to perform the query or to push it in a QueryQueu...
Definition: iquery.h:372
Database * m_database
The database on which the query will be performed. Set in Initialize(), do not directly modify...
Definition: iquery.h:277
void OnDone()
Calls the OnDone function of the IOnDone object if provided.
Definition: iquery.h:386
QueryStatusInQueue
Enumerates the different states a query may have in a QueryQueue.
Definition: iquery.h:101
void SetTraverseLogicUserData(void *traverseLogicUserData)
Should be set for queries templated by a traverse logic, useless otherwise.
Definition: iquery.h:384