gwnavruntime/querysystem/workingmemcontainers/navdatachangeindexingrid.h Source File

navdatachangeindexingrid.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 
13 
14 namespace Kaim
15 {
16 class AstarNodeIndexInGrid;
17 class Database;
18 
19 class NavDataChangeIndexInGrid
20 {
21  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_QueryWorkingMem)
22 public:
23 
24  struct CellPosNavMeshChangeIdx
25  {
26  KY_INLINE bool IsValid() const { return m_navMeshChangeIdx != 0; }
27  KyUInt32 m_navMeshChangeIdx;
28  };
29 
30 public:
31  NavDataChangeIndexInGrid() : m_currentOffsetFromBuffer(0), m_navGraphChangeIdx(0) {}
32 
33  NavDataChangeIndexInGrid(WorkingMemory* workingMemory, const CellBox& cellBox) :
34  m_navGraphChangeIdx(0), m_abstractGraphChangeIdx(0), m_cellBox(cellBox)
35  {
36  m_buffer.Init(workingMemory);
37  m_currentOffsetFromBuffer = 0;
38  MakeEmpty();
39  }
40 
41  ~NavDataChangeIndexInGrid() { ReleaseWorkingMemoryBuffer(); }
42 
43  void Init(WorkingMemory* workingMemory, const CellBox& cellBox)
44  {
45  m_buffer.Init(workingMemory);
46  m_cellBox = cellBox;
47  m_currentOffsetFromBuffer = 0;
48  m_navGraphChangeIdx = 0;
49  m_abstractGraphChangeIdx = 0;
50  MakeEmpty();
51  }
52 
53  // InitAllChangeIdx... means Init m_navGraphChangeIdx, m_abstractGraphChangeIdx, for each cell cellPosNavDataIdx->m_navMeshChangeIdx
54  void InitAllChangeIdxFromAstarNodeIndexInGrid(AstarNodeIndexInGrid& astarNodeIndexInGrid);
55  void InitAllChangeIdxFromDatabase(const 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  CellPosNavMeshChangeIdx* AllocateCellPosNavDataIdx(KyUInt32 numberOfCell);
70  CellPosNavMeshChangeIdx* GetCellPosNavDataIdx(const CellPos& cellPos);
71 
72  bool IsInitialized() const { return m_buffer.IsInitialized(); }
73  bool HasVisitedNavDataChanged(Database* database, PathFinderAbstractGraphTraversalMode abstractGraphTraversalMode);
74 
75 public:
76  WorkingMemContainerBase m_buffer;
77  KyUInt32 m_currentOffsetFromBuffer;
78  KyUInt32 m_navGraphChangeIdx;
79  KyUInt32 m_abstractGraphChangeIdx;
80  CellBox m_cellBox;
81 };
82 
83 KY_INLINE void NavDataChangeIndexInGrid::ReleaseWorkingMemoryBuffer() { m_buffer.ReleaseBuffer(); }
84 
85 KY_INLINE KyUInt32 NavDataChangeIndexInGrid::GetAvailableSizeInBytes() const { return m_buffer.GetBufferSize() - m_currentOffsetFromBuffer; }
86 
87 KY_INLINE bool NavDataChangeIndexInGrid::IsEnoughPlaceForAllocation(KyUInt32 sizeInBytes)
88 {
89  while (GetAvailableSizeInBytes() < sizeInBytes)
90  {
91  if (TryToResize() == false)
92  return false;
93  }
94 
95  return true;
96 }
97 
98 KY_INLINE NavDataChangeIndexInGrid::CellPosNavMeshChangeIdx* NavDataChangeIndexInGrid::AllocateCellPosNavDataIdx(KyUInt32 numberOfCell)
99 {
100  return (CellPosNavMeshChangeIdx*)AllocateInBufferAndMemsetTo0(sizeof(CellPosNavMeshChangeIdx), numberOfCell);
101 }
102 
103 KY_INLINE NavDataChangeIndexInGrid::CellPosNavMeshChangeIdx* NavDataChangeIndexInGrid::GetCellPosNavDataIdx(const CellPos& cellPos)
104 {
105  CellPosNavMeshChangeIdx* memoryStartForGrid = (CellPosNavMeshChangeIdx*)(m_buffer.GetBuffer());
106  return memoryStartForGrid + m_cellBox.GetRowMajorIndex(cellPos);
107 }
108 
109 }
110 
111 
112 
Box2i CellBox
A type that represents a bounding box around cells in a 2D grid.
Definition: navmeshtypes.h:31
Vec2i CellPos
A type that represents the position of a cell within a 2D grid.
Definition: navmeshtypes.h:30
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
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17