gwnavruntime/queries/utils/pathrefinercontext.h Source File

pathrefinercontext.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 
11 
12 namespace Kaim
13 {
14 
15 typedef KyUInt16 RefinerNodeIndex;
16 static const RefinerNodeIndex RefinerNodeIndex_Invalid = KyUInt16MAXVAL;
17 
18 class RefinerNode
19 {
20 public:
21  RefinerNode();
22  RefinerNode(const Vec3f& pos, NodeTypeAndRawPtrDataIdx nodeTypeAndRawPtrDataIdx, RefinerNodeIndex predecessorIdx, RefinerNodeIndex nextNodeIdx);
23 
24  PathNodeType GetNodeType() const;
25  KyUInt32 GetIdxOfRawPtrData() const;
26 
27  void SetNodeType(PathNodeType nodeType);
28  void SetIdxOfRawPtrData(KyUInt32 indexOfRawPtrData);
29 
30  Vec3f m_nodePosition;
31  WorldIntegerPos m_nodeIntegerPos;
32  KyFloat32 m_refinerCost;
33  KyFloat32 m_costFromPredecessor;
34  KyFloat32 m_maxCostMultAlongIncomingEdge;
35  NodeTypeAndRawPtrDataIdx m_nodeTypeAndRawPtrDataIdx; // 32 bits
36  RefinerNodeIndex m_predecessorNodeIdx; // 16 bits
37  RefinerNodeIndex m_nextNodeIdx; // 16bits
38  IndexInBinHeap m_indexInBinaryHeap; // 16bits
39 };
40 
41 class RefinerBinHeapIndexTracker
42 {
43 public:
44  RefinerBinHeapIndexTracker() : m_refinerNodes(nullptr) {}
45 
46  void SetRefinerNodeArray(WorkingMemArray<RefinerNode>& refinerNodes);
47  // Called after the element has been added.
48  void OnAdd(RefinerNodeIndex refinerNodeIndex, KyUInt32 indexInBinaryHeap);
49  // Called before the element has been removed.
50  void OnRemove(RefinerNodeIndex refinerNodeIndex);
51  // Called after the elements has been swapped.
52  void OnSwap(RefinerNodeIndex lhs, RefinerNodeIndex rhs);
53 
54  WorkingMemArray<RefinerNode>* m_refinerNodes;
55 };
56 
57 class RefinerNodeComparator
58 {
59 public:
60  RefinerNodeComparator() : m_refinerNodes(nullptr) {}
61  void SetRefinerNodeArray(WorkingMemArray<RefinerNode>& refinerNodes);
62  bool operator () (const RefinerNodeIndex lhsIdx, const RefinerNodeIndex rhsIdx) const;
63 
64  WorkingMemArray<RefinerNode>* m_refinerNodes;
65 };
66 
67 typedef WorkingMemBinaryHeap<RefinerNodeIndex, RefinerNodeComparator, RefinerBinHeapIndexTracker> RefinerBinHeap;
68 
69 class AStarTraversalContext;
70 class PathRefinerContext
71 {
72  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_QueryWorkingMem)
73 public:
74  PathRefinerContext() : m_currentNodeIdx(RefinerNodeIndex_Invalid) {}
75  ~PathRefinerContext() { ReleaseWorkingMemory(); }
76 
77  KyResult InitFromAstarTraversalContext(WorkingMemory* workingMemory, AStarTraversalContext* astarContext,
78  AStarNodeIndex destinationNodeIdx, bool doClearAStarTraversalContext = true);
79 
80  void ReleaseWorkingMemory();
81 
82 
83  RefinerBinHeap m_refinerBinHeap;
84  WorkingMemArray<RefinerNode> m_refinerNodes;
85  WorkingMemArray<NavTriangleRawPtr> m_triangleRawPtrNodes;
86  WorkingMemArray<NavGraphVertexRawPtr> m_vertexRawPtrNodes;
87 
88  RefinerNodeIndex m_currentNodeIdx;
89 };
90 
91 
92 KY_INLINE RefinerNode::RefinerNode() :
93  m_refinerCost(KyFloat32MAXVAL),
94  m_costFromPredecessor(KyFloat32MAXVAL),
95  m_maxCostMultAlongIncomingEdge(-KyFloat32MAXVAL),
96  m_predecessorNodeIdx(RefinerNodeIndex_Invalid),
97  m_nextNodeIdx(RefinerNodeIndex_Invalid),
98  m_indexInBinaryHeap(IndexInBinHeap_UnSet)
99 {}
100 
101 KY_INLINE RefinerNode::RefinerNode(const Vec3f& pos, NodeTypeAndRawPtrDataIdx nodeTypeAndRawPtrDataIdx, RefinerNodeIndex predecessorIdx, RefinerNodeIndex nextNodeIdx) :
102  m_nodePosition(pos),
103  m_refinerCost(KyFloat32MAXVAL),
104  m_costFromPredecessor(KyFloat32MAXVAL),
105  m_maxCostMultAlongIncomingEdge(-KyFloat32MAXVAL),
106  m_nodeTypeAndRawPtrDataIdx(nodeTypeAndRawPtrDataIdx),
107  m_predecessorNodeIdx(predecessorIdx),
108  m_nextNodeIdx(nextNodeIdx),
109  m_indexInBinaryHeap(IndexInBinHeap_UnSet)
110 {}
111 
112 KY_INLINE PathNodeType RefinerNode:: GetNodeType() const { return m_nodeTypeAndRawPtrDataIdx.GetNodeType(); }
113 KY_INLINE KyUInt32 RefinerNode::GetIdxOfRawPtrData() const { return m_nodeTypeAndRawPtrDataIdx.GetIdxOfRawPtrData(); }
114 KY_INLINE void RefinerNode::SetNodeType(PathNodeType nodeType) { m_nodeTypeAndRawPtrDataIdx.SetNodeType(nodeType); }
115 KY_INLINE void RefinerNode::SetIdxOfRawPtrData(KyUInt32 indexOfRawPtrData) { m_nodeTypeAndRawPtrDataIdx.SetIdxOfRawPtrData(indexOfRawPtrData); }
116 
117 
118 KY_INLINE void RefinerBinHeapIndexTracker::SetRefinerNodeArray(WorkingMemArray<RefinerNode>& refinerNodes) { m_refinerNodes = &refinerNodes; }
119 KY_INLINE void RefinerBinHeapIndexTracker::OnAdd(RefinerNodeIndex refinerNodeIndex, KyUInt32 indexInBinaryHeap)
120 {
121  RefinerNode& node = (*m_refinerNodes)[refinerNodeIndex];
122  node.m_indexInBinaryHeap = (IndexInBinHeap)indexInBinaryHeap;
123 }
124 KY_INLINE void RefinerBinHeapIndexTracker::OnRemove(RefinerNodeIndex refinerNodeIndex)
125 {
126  RefinerNode& node = (*m_refinerNodes)[refinerNodeIndex];
127  node.m_indexInBinaryHeap = IndexInBinHeap_UnSet;
128 }
129 KY_INLINE void RefinerBinHeapIndexTracker::OnSwap(RefinerNodeIndex lhs, RefinerNodeIndex rhs)
130 {
131  RefinerNode& node1 = (*m_refinerNodes)[lhs];
132  RefinerNode& node2 = (*m_refinerNodes)[rhs];
133  Alg::Swap(node1.m_indexInBinaryHeap, node2.m_indexInBinaryHeap);
134 }
135 
136 KY_INLINE void RefinerNodeComparator::SetRefinerNodeArray(WorkingMemArray<RefinerNode>& refinerNodes) { m_refinerNodes = &refinerNodes; }
137 KY_INLINE bool RefinerNodeComparator::operator() (const RefinerNodeIndex lhsIdx, const RefinerNodeIndex rhsIdx) const
138 {
139  RefinerNode& lhs = (*m_refinerNodes)[lhsIdx];
140  RefinerNode& rhs = (*m_refinerNodes)[rhsIdx];
141  return lhs.m_refinerCost < rhs.m_refinerCost;
142 }
143 
144 KY_INLINE void PathRefinerContext::ReleaseWorkingMemory()
145 {
146  m_triangleRawPtrNodes.ReleaseWorkingMemoryBuffer();
147  m_vertexRawPtrNodes.ReleaseWorkingMemoryBuffer();
148  m_refinerBinHeap.ReleaseWorkingMemoryBuffer();
149  m_refinerNodes.ReleaseWorkingMemoryBuffer();
150 }
151 
152 }
153 
154 
155 
#define KyFloat32MAXVAL
KyFloat32 max value
Definition: types.h:71
std::uint32_t KyUInt32
uint32_t
Definition: types.h:29
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:132
std::uint16_t KyUInt16
uint16_t
Definition: types.h:28
Navigation return code class.
Definition: types.h:108
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
#define KyUInt16MAXVAL
KyUInt16 max value
Definition: types.h:67
float KyFloat32
float
Definition: types.h:32