gwnavruntime/queries/utils/astartraversalcontext.h Source File

astartraversalcontext.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 
12 
13 namespace Kaim
14 {
15 class QueryUtils;
16 
17 // Class for BinaryHeap to update index in binaryHeap assuming index is T::m_indexInBinaryHeap.
18 class AstarBinHeapIndexTracker
19 {
20 public:
21  AstarBinHeapIndexTracker() : m_astarNodes(nullptr) {}
22  void SetAstarNodeArray(WorkingMemArray<AStarNode>& astarNodeArray);
23 
24  void OnAdd(AStarNodeIndex& aStarNodeIndex, KyUInt32 indexInBinaryHeap);
25  void OnRemove(AStarNodeIndex& aStarNodeIndex);
26  void OnSwap(AStarNodeIndex& lhs, AStarNodeIndex& rhs);
27 
28  WorkingMemArray<AStarNode>* m_astarNodes;
29 };
30 
31 class AStarNodeComparator
32 {
33 public:
34  AStarNodeComparator() : m_astarNodes(nullptr) {}
35  void SetAstarNodeArray(WorkingMemArray<AStarNode>& astarNodeArray);
36 
37  bool operator () (const AStarNodeIndex lhsIdx, const AStarNodeIndex rhsIdx) const;
38 
39  WorkingMemArray<AStarNode>* m_astarNodes;
40 };
41 
42 
43 typedef WorkingMemBinaryHeap<AStarNodeIndex, AStarNodeComparator, AstarBinHeapIndexTracker> AstarTraversalBinHeap;
44 
45 
46 class AStarTraversalContext
47 {
48  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_QueryWorkingMem)
49 public:
50  AStarTraversalContext() {}
51 
52  KyResult Init(QueryUtils& queryUtils, const AStarContextConfig& astarContextconfig);
53  void ReleaseWorkingMemory();
54 
55  KyResult CheckTraversalBinaryHeapMemory();
56  KyResult CheckAstarNodeArrayMemory();
57  KyResult CheckNavHalfEdgeRawPtrArrayMemory();
58  KyResult CheckNavGraphVertexRawPtrArrayMemory();
59  KyResult CheckAbstractGraphNodeRawPtrArrayMemory();
60 
61  KyResult InsertOrUpdateInBinHeapTraversal(AStarNode& neighborNode, AStarNodeIndex neighborNodeIndex);
62 
63  AstarNodeIndexInGrid m_edgeIndexGrid;
64  AstarTraversalBinHeap m_traversalBinHeap;
65  WorkingMemArray<AStarNode> m_aStarNodes;
66  WorkingMemArray<NavHalfEdgeRawPtr> m_edgeRawPtrNodes;
67  WorkingMemArray<NavGraphVertexRawPtr> m_vertexRawPtrNodes;
68  WorkingMemArray<AbstractGraphNodeRawPtr> m_abstractRawPtrNodes;
69  AStarNodeIndex m_nodeIdx; // used when removing points in the middle of edges in the AstarQuery
70 };
71 
72 
73 
74 KY_INLINE void AstarBinHeapIndexTracker::SetAstarNodeArray(WorkingMemArray<AStarNode>& astarNodeArray) { m_astarNodes = &astarNodeArray; }
75 KY_INLINE void AstarBinHeapIndexTracker::OnAdd(AStarNodeIndex& aStarNodeIndex, KyUInt32 indexInBinaryHeap)
76 {
77  AStarNode& aStarNode = (*m_astarNodes)[aStarNodeIndex];
78  aStarNode.m_indexInBinaryHeap = (IndexInBinHeap)indexInBinaryHeap;
79 }
80 // Called before the element has been removed.
81 KY_INLINE void AstarBinHeapIndexTracker::OnRemove(AStarNodeIndex& aStarNodeIndex)
82 {
83  AStarNode& aStarNode = (*m_astarNodes)[aStarNodeIndex];
84  aStarNode.m_indexInBinaryHeap = IndexInBinHeap_Removed;
85 }
86 // Called after the elements have been swapped.
87 KY_INLINE void AstarBinHeapIndexTracker::OnSwap(AStarNodeIndex& lhs, AStarNodeIndex& rhs)
88 {
89  AStarNode& aStarNode1 = (*m_astarNodes)[lhs];
90  AStarNode& aStarNode2 = (*m_astarNodes)[rhs];
91  Alg::Swap(aStarNode1.m_indexInBinaryHeap, aStarNode2.m_indexInBinaryHeap);
92 }
93 
94 KY_INLINE void AStarNodeComparator::SetAstarNodeArray(WorkingMemArray<AStarNode>& astarNodeArray) { m_astarNodes = &astarNodeArray; }
95 KY_INLINE bool AStarNodeComparator::operator () (const AStarNodeIndex lhsIdx, const AStarNodeIndex rhsIdx) const
96 {
97  AStarNode& lhs = (*m_astarNodes)[lhsIdx];
98  AStarNode& rhs = (*m_astarNodes)[rhsIdx];
99  return lhs.m_costFromStart + lhs.m_estimatedCostToDest < rhs.m_costFromStart + rhs.m_estimatedCostToDest;
100 }
101 
102 
103 KY_INLINE KyResult AStarTraversalContext::CheckTraversalBinaryHeapMemory()
104 {
105  if (m_traversalBinHeap.IsFull() == false)
106  return KY_SUCCESS;
107 
108  KY_LOG_WARNING( ("This traversal has reached its maximum working memory size for its BinaryHeap"));
109  return KY_ERROR;
110 }
111 
112 KY_INLINE KyResult AStarTraversalContext::CheckAstarNodeArrayMemory()
113 {
114  if (m_aStarNodes.GrowIfNeeded())
115  return KY_SUCCESS;
116 
117  KY_LOG_WARNING(("This traversal has reached its maximum working memory size for storing astarNodes"));
118  return KY_ERROR;
119 }
120 
121 KY_INLINE KyResult AStarTraversalContext::CheckNavHalfEdgeRawPtrArrayMemory()
122 {
123  if (m_edgeRawPtrNodes.GrowIfNeeded())
124  return KY_SUCCESS;
125 
126  KY_LOG_WARNING(("This traversal has reached its maximum working memory size for storing astarNodes and corresponding NavHalfEdgeRawPtr"));
127  return KY_ERROR;
128 }
129 
130 KY_INLINE KyResult AStarTraversalContext::CheckNavGraphVertexRawPtrArrayMemory()
131 {
132  if (m_vertexRawPtrNodes.GrowIfNeeded())
133  return KY_SUCCESS;
134 
135  KY_LOG_WARNING(("This traversal has reached its maximum working memory size for storing astarNodes and corresponding NavGraphVertexRawPtr"));
136  return KY_ERROR;
137 }
138 
139 KY_INLINE KyResult AStarTraversalContext::CheckAbstractGraphNodeRawPtrArrayMemory()
140 {
141  if (m_abstractRawPtrNodes.GrowIfNeeded())
142  return KY_SUCCESS;
143 
144  KY_LOG_WARNING(("This traversal has reached its maximum working memory size for storing astarNodes and corresponding AbstractGraphNodeRawPtr"));
145  return KY_ERROR;
146 }
147 
148 
149 KY_INLINE KyResult AStarTraversalContext::InsertOrUpdateInBinHeapTraversal(AStarNode& neighborNode, AStarNodeIndex neighborNodeIndex)
150 {
151  if (neighborNode.m_indexInBinaryHeap < IndexInBinHeap_Removed)
152  {
153  // (the node is open, already present in the binary heap), we compute its new position in the BinaryHeap
154  m_traversalBinHeap.UpdateAt(neighborNode.m_indexInBinaryHeap);
155  }
156  else
157  {
158  // the node has been removed from the BinaryHeap, we add it again
159 
160  // check for memory to insert a newElement in the binary heap
161  KY_TRY(CheckTraversalBinaryHeapMemory());
162 
163  m_traversalBinHeap.Insert(neighborNodeIndex);
164  }
165 
166  return KY_SUCCESS;
167 }
168 }
169 
170 
171 
std::uint32_t KyUInt32
uint32_t
Definition: types.h:29
#define KY_TRY(expr)
Returns KY_ERROR if expression == KY_ERROR.
Definition: types.h:144
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:132
Navigation return code class.
Definition: types.h:108
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
#define KY_ERROR
use result == KY_ERROR to test for error
Definition: types.h:132