gwnavruntime/querysystem/workingmemcontainers/navdatachangeindexingrid.h Source File

navdatachangeindexingrid.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 #ifndef Navigation_NavDataChangeIndexInGrid_H
9 #define Navigation_NavDataChangeIndexInGrid_H
10 
14 
15 namespace Kaim
16 {
17 class AstarNodeIndexInGrid;
18 class Database;
19 
20 class NavDataChangeIndexInGrid
21 {
22  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_QueryWorkingMem)
23 public:
24 
25  struct CellPosNavDataIdx
26  {
27  KY_INLINE bool IsValid() const { return m_navDataChangeIdx != 0; }
28  KyUInt32 m_navDataChangeIdx;
29  };
30 
31 public:
32  NavDataChangeIndexInGrid() : m_currentOffsetFromBuffer(0), m_navGraphChangeIdx(0) {}
33 
34  NavDataChangeIndexInGrid(WorkingMemory* workingMemory, const CellBox& cellBox) :
35  m_navGraphChangeIdx(0), m_abstractGraphChangeIdx(0), m_cellBox(cellBox)
36  {
37  m_workingMemContainerBase.Init(workingMemory);
38  m_currentOffsetFromBuffer = 0;
39  MakeEmpty();
40  }
41 
42  ~NavDataChangeIndexInGrid() { ReleaseWorkingMemoryBuffer(); }
43 
44  void Init(WorkingMemory* workingMemory, const CellBox& cellBox)
45  {
46  m_workingMemContainerBase.Init(workingMemory);
47  m_cellBox = cellBox;
48  m_currentOffsetFromBuffer = 0;
49  m_navGraphChangeIdx = 0;
50  m_abstractGraphChangeIdx = 0;
51  MakeEmpty();
52  }
53 
54  void RetrieveAllIndices(AstarNodeIndexInGrid& astarNodeIndexInGrid);
55  void RetrieveAllIndicesFromDatabase(Database* database);
56 
57  void ReleaseWorkingMemoryBuffer();
58 
59  KyUInt32 GetAvailableSizeInBytes() const;
60 
61  bool IsEnoughPlaceForAllocation(KyUInt32 sizeInBytes);
62 
63  bool TryToResize();
64 
65  void MakeEmpty();
66 
67  void* AllocateInBufferAndMemsetTo0(KyUInt32 sizeOfOneElementInBytes, KyUInt32 numberOfElements);
68 
69  CellPosNavDataIdx* AllocateCellPosNavDataIdx(KyUInt32 numberOfCell);
70  CellPosNavDataIdx* GetCellPosNavDataIdx(const CellPos& cellPos);
71 
72  bool IsInitialized() const { return m_workingMemContainerBase.IsInitialized(); }
73  bool HasNavDataChanged(Database* database, PathFinderAbstractGraphTraversalMode abstractGraphTraversalMode);
74 public:
75  WorkingMemContainerBase m_workingMemContainerBase;
76  KyUInt32 m_currentOffsetFromBuffer;
77  KyUInt32 m_navGraphChangeIdx;
78  KyUInt32 m_abstractGraphChangeIdx;
79  CellBox m_cellBox;
80 };
81 
82 KY_INLINE void NavDataChangeIndexInGrid::ReleaseWorkingMemoryBuffer() { m_workingMemContainerBase.ReleaseBuffer(); }
83 
84 KY_INLINE KyUInt32 NavDataChangeIndexInGrid::GetAvailableSizeInBytes() const { return m_workingMemContainerBase.GetBufferSize() - m_currentOffsetFromBuffer; }
85 
86 KY_INLINE bool NavDataChangeIndexInGrid::IsEnoughPlaceForAllocation(KyUInt32 sizeInBytes)
87 {
88  while (GetAvailableSizeInBytes() < sizeInBytes)
89  {
90  if (TryToResize() == false)
91  return false;
92  }
93 
94  return true;
95 }
96 
97 KY_INLINE NavDataChangeIndexInGrid::CellPosNavDataIdx* NavDataChangeIndexInGrid::AllocateCellPosNavDataIdx(KyUInt32 numberOfCell)
98 {
99  return (CellPosNavDataIdx*)AllocateInBufferAndMemsetTo0(sizeof(CellPosNavDataIdx), numberOfCell);
100 }
101 
102 KY_INLINE NavDataChangeIndexInGrid::CellPosNavDataIdx* NavDataChangeIndexInGrid::GetCellPosNavDataIdx(const CellPos& cellPos)
103 {
104  CellPosNavDataIdx* memoryStartForGrid = (CellPosNavDataIdx*)(m_workingMemContainerBase.GetBuffer());
105  return memoryStartForGrid + m_cellBox.GetRowMajorIndex(cellPos);
106 }
107 
108 }
109 
110 
111 #endif //Navigation_NavDataChangeIndexInGrid_H
112 
Box2i CellBox
A type that represents a bounding box around cells in a 2D grid.
Definition: navmeshtypes.h:34
Vec2i CellPos
A type that represents the position of a cell within a 2D grid.
Definition: navmeshtypes.h:33
PathFinderAbstractGraphTraversalMode
Enumerates the possible way of dealing with AbstractGraphs.
Definition: pathfinderabstractgraphtraversalmode.h:16
Definition: gamekitcrowddispersion.h:20
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:137
unsigned int KyUInt32
Type used internally to represent an unsigned 32-bit integer.
Definition: types.h:36