gwnavruntime/querysystem/iquery.h Source File

iquery.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 #pragma once
8 
14 
15 namespace Kaim
16 {
17 
18 class World;
19 class Database;
20 class WorkingMemory;
21 class QueryQueue;
22 
26 {
27  TypeRayCanGo = 0,
28  TypeRayCast,
29  TypeDiskCanGo,
30  TypeDiskCast,
31  TypeDiskCollision,
32  TypeAStar,
33  TypeTriangleFromPos,
34  TypeMoveOnNavMesh,
35  TypeSpatializedPointCollectorInAABB,
36  TypeTriangleFromPosAndTriangleSeed,
37  TypeNearestBorderHalfEdgeFromPos,
38  TypeSegmentCast,
39  TypeSegmentCanGo,
40  TypeDiskExpansion,
41  TypeInsidePosFromOutsidePos,
42  TypeMultipleFloorTrianglesFromPos,
43  TypeRayCanGoOnSegment,
44  TypeRayCastOnSegment,
45  TypeDynamicNavMesh,
46  TypeFindFirstVisiblePositionOnPath,
47  TypeBestGraphVertexPathFinderQuery,
48  TypeMakeNavFloorStitchQuery,
49  TypeCollisionRayCast,
50  TypeRayCastFull3D,
51  TypePathFromPolyline,
52  TypeTagVolumesFromPos,
53  TypeMultiDestinationPathFinderQuery,
54  TypeCircleArcSplineComputation,
55 
56  QueryType_FirstCustom
57 };
58 
59 // Gives the context in which an IAtomicQuery's PerformQuery function is called
60 // in order to monitor when such queries are performed,
61 // used by ScopedPerformedQueryCounter
63 {
64  QueryStat_Generation,
65  QueryStat_Spatialization,
66  QueryStat_NavGraph,
67  QueryStat_PathFinder,
68  QueryStat_ProgressOnPath,
69  QueryStat_PathValidityInterval,
70  QueryStat_TargetOnPathIsStillVisible,
71  QueryStat_TargetOnPathShortcutForward,
72  QueryStat_TargetOnPathSearchBackward,
73  QueryStat_Avoidance,
74  QueryStat_Channel,
75  QueryStat_Spline,
76 
77  QueryStat_GameKit,
78  QueryStat_LabEngine,
79 
81 
82  PerformQueryStat_Count,
83  PerformQueryStat_Unspecified = PerformQueryStat_Count,
84 };
85 
88 {
89  QueryNotStarted = 0,
90  QueryInProcess,
91  QueryDone,
92  QueryCanceled,
93  QueryResultMax,
94 };
95 
98 {
99  QueryNotInQueue,
100  QueryPushedAsCommand,
101  QueryInQueryQueue
102 };
103 
104 enum QueryProcessMode
105 {
106  QueryProcessMode_Sync,
107  QueryProcessMode_ASync
108 };
109 
114 class IOnDone : public RefCountBase<IOnDone, MemStat_Query>
115 {
116 public:
117  // ------------------------------ Functions -----------------------------
118  virtual ~IOnDone() {}
119  virtual void OnDone() = 0;
120 };
121 
123 class IQuery : public RefCountBase<IQuery, MemStat_Query>
124 {
125  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_Query)
126 public:
127  // ------------------------------ IQuery Functions -----------------------------
128  IQuery() : m_processStatus(QueryNotStarted), m_database(nullptr), m_traverseLogicUserData(nullptr), m_queue(nullptr), m_inQueueStatus(QueryNotInQueue), m_queryInfoId(0) {}
129 
130  virtual ~IQuery() {}
131 
134  bool CanBeInitialized() const { return m_inQueueStatus != QueryInQueryQueue; }
135 
139  bool IsInAQueryQueue() const { return m_inQueueStatus != QueryNotInQueue; }
140 
144  bool IsSafeToReadResult() const { return IsInAQueryQueue() == false && m_processStatus == QueryDone; }
145 
149  KyUInt32 PerformQueryBlocking(WorkingMemory* workingMemory = nullptr);
150 
155  void OnDone()
156  {
157  if (m_onDone != nullptr)
158  m_onDone->OnDone();
159  m_onDone = nullptr;
160  }
161 
165 
167  void SetTraverseLogicUserData(void* traverseLogicUserData) { m_traverseLogicUserData = traverseLogicUserData; }
168 
171 
172  // ------------------------------ Pure virtual functions - Query framework contract ------------------------------
173 
175  virtual QueryType GetType() const = 0;
176 
184  virtual void Advance(WorkingMemory* workingMemory) = 0;
185 
186 public: // internal
187  // ---------------------------------- Internal Virtual functions for NavigationLab Remote Query processing ----------------------------------
188 
189  // These functions are used by IQuery::SendVisualDebug()
190  // and also used by the NavigationLab to request remote execution:
191  // using VisualDebugClient::Send(IQuery*) to ask for a query to be executed
192  // using VisualDebugServer::Send(IQuery*) to send back the query result.
193  // Queries that can be visual debug override these 3 functions.
194  // By convenience, such queries provide a static function with the following signature: /code
195  // static Ptr<BaseBlobHandler> CreateStaticQueryBlobHandler(); /endcode
196  virtual Ptr<BaseBlobHandler> CreateQueryBlobHandler() { return nullptr; }
197  virtual void BuildQueryBlob(Kaim::BaseBlobHandler* /*blobHandler*/) {}
198  virtual void InitFromQueryBlob(World* /*world*/, void* /*blob*/) {}
199 
200 public: // internal
201  static const char* GetQueryTypeName(QueryType queryType);
202 
203  // ---------------------------------- Mandatory initialization calls to be done by derived classes ----------------------------------
204 
207  void BindToDatabase(Database* database)
208  {
209  KY_LOG_ERROR_IF(database == nullptr, ("The database you are bounding the query to is null."));
210  KY_LOG_ERROR_IF(CanBeInitialized() == false, ("You cannot bind a query to a database while it is in a QueryQueue."));
211  m_database = database;
212  m_traverseLogicUserData = nullptr;
213  }
214 
220  void Initialize()
221  {
222  KY_LOG_ERROR_IF(m_database == nullptr, ("This query must have been bound to a valid database before calling Initialize"));
223  KY_LOG_ERROR_IF(CanBeInitialized() == false, ("You cannot initialize a query while it is in a QueryQueue."));
224  m_processStatus = QueryNotStarted;
225  }
226 
227 public:
229 
232  Ptr<IOnDone> m_onDone;
233 
234 public: // internal
237 
240  KyUInt32 m_queryInfoId;
241 };
242 
245 class IAtomicQuery : public IQuery
246 {
247 public:
248  //------------------------------------- IAtomicQuery -----------------------------------------------
249  IAtomicQuery() : m_performQueryStat(PerformQueryStat_Unspecified) {}
250 
251  virtual ~IAtomicQuery() {}
252 
253  void SetPerformQueryStat(PerformQueryStat performQueryStat) { m_performQueryStat = performQueryStat; }
254 
255  PerformQueryStat GetPerformQueryStat() { return m_performQueryStat; }
256 
257 public:
258  PerformQueryStat m_performQueryStat; // used by ScopedPerformedQueryCounter to know in which context PerformQuery was called
259 };
260 
262 class ITimeSlicedQuery : public IQuery
263 {
264 public:
265  // ------------------------------ Functions -----------------------------
266  ITimeSlicedQuery() : m_advanceCount(0), m_lastAdvanceFrameIdx(0) {}
267 
268  virtual ~ITimeSlicedQuery() {}
269 
273  void Initialize()
274  {
276  m_advanceCount = 0;
277  m_lastAdvanceFrameIdx = 0;
278  }
279 
280  // ---------------------------------- Pure virtual functions ----------------------------------
281 
285  virtual void ReleaseWorkingMemoryOnCancelDuringProcess(WorkingMemory* workingMemory) = 0;
286 
287 public:
288  KyUInt32 m_advanceCount;
289  KyUInt32 m_lastAdvanceFrameIdx;
290 };
291 
294 {
297 };
298 
299 }
KyUInt32 PerformQueryBlocking(WorkingMemory *workingMemory=nullptr)
Calls Advance() untill the query is done.
Definition: iquery.cpp:58
std::uint32_t KyUInt32
uint32_t
Definition: types.h:29
QueryProcessStatus m_processStatus
Modified by the query within Advance(). Do not modify.
Definition: iquery.h:231
Base class for all the queries that do not need to be time-sliced.
Definition: iquery.h:245
QueryQueue processes queries in an asynchronous, time-sliced way, within a QueryQueueArray.
Definition: queryqueue.h:73
CommonQueryResult
Enumerates the result codes that are common to all queries.
Definition: iquery.h:293
virtual void Advance(WorkingMemory *workingMemory)=0
This function is called by the QueryQueue to process one step on the query.
void * GetTraverseLogicUserData() const
Gets m_traverseLogicUserData.
Definition: iquery.h:170
virtual void ReleaseWorkingMemoryOnCancelDuringProcess(WorkingMemory *workingMemory)=0
Called when a query is canceled in FlushCommands while its status is QueryInProcess to make sure that...
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:144
bool CanBeInitialized() const
In the case of a query processed in a QueryQueue, this must be tested before initializing the query...
Definition: iquery.h:134
Ptr< IOnDone > m_onDone
The optional IOnDone instance to be called. Must be set manually by the user. See IOnDone...
Definition: iquery.h:232
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:132
Base class for all the queries that need to be time-sliced.
Definition: iquery.h:262
QueryType
Enumerates all the type of query.
Definition: iquery.h:25
This class is a runtime container for all NavData that represents the world from the point of view of...
Definition: database.h:57
Small interface class that you have to derive from and set to an IQuery to get the IOnDone::OnDone() ...
Definition: iquery.h:114
Navigation return code class.
Definition: types.h:108
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
Indicates that the query has not yet been initialized.
Definition: iquery.h:295
QueryProcessStatus
Enumerates all the processing status a query can have.
Definition: iquery.h:87
void BindToDatabase(Database *database)
Should be called by the derived class before Initializing the query Sets m_database and sets m_traver...
Definition: iquery.h:207
bool IsInAQueryQueue() const
Returns true if the query is currently registered in a QueryQueue.
Definition: iquery.h:139
KyResult SendVisualDebug()
Sends the query to the NavigationLab.
Definition: iquery.cpp:74
QueryStatusInQueue m_inQueueStatus
Used by the QueryQueue. Do not modify.
Definition: iquery.h:239
PerformQueryStat
Definition: iquery.h:62
QueryQueue * m_queue
Updated by the QueryQueue. Do not modify.
Definition: iquery.h:238
Indicates that the query has not yet been launched.
Definition: iquery.h:296
void * m_traverseLogicUserData
This userData is typically passed by the Bot.
Definition: iquery.h:236
can be used sporadically to differentiate from PerformQueryStat_Unspecified
Definition: iquery.h:80
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:273
void Initialize()
Should be called by the derived class prior to perform the query or to push it in a QueryQueue...
Definition: iquery.h:220
Database * m_database
The database on which the query will be performed. Set in Initialize(), do not directly modify...
Definition: iquery.h:235
Abstract class for all queries.
Definition: iquery.h:123
virtual QueryType GetType() const =0
Get query type.
void OnDone()
Calls m_onDone IOnDone::OnDone() if m_onDone is not null.
Definition: iquery.h:155
QueryStatusInQueue
Enumerates the different states a query may have in a QueryQueue.
Definition: iquery.h:97
void SetTraverseLogicUserData(void *traverseLogicUserData)
Sets m_traverseLogicUserData for queries that have a custom TraverseLogic, useless otherwise...
Definition: iquery.h:167