gwnavruntime/queries/utils/pathrefinercontext.h Source File

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