gwnavruntime/querysystem/workingmemcontainers/halfedgedataptringrid.h Source File

halfedgedataptringrid.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_HalfEdgeDataPtrInGrid_H
9 #define Navigation_HalfEdgeDataPtrInGrid_H
10 
13 
14 namespace Kaim
15 {
16 
17 class HalfEdgeDataPtrInGrid
18 {
19 public:
20 
21  struct NavFloorToEdgeDataPtrBuffer
22  {
23  KY_INLINE void* GetEdgeDataPtr(NavHalfEdgeIdx navHalfEdgeIdx) const { return GetEdgeDataPtrBuffer()[navHalfEdgeIdx]; }
24  KY_INLINE void SetEdgeDataPtr(NavHalfEdgeIdx navHalfEdgeIdx, void* tdata) { GetEdgeDataPtrBuffer()[navHalfEdgeIdx] = tdata; }
25 
26  KY_INLINE void** GetEdgeDataPtrBuffer() const { return (void**)((char*)this + m_offsetToEdgeDataPtrBuffer); }
27 
28  KY_INLINE bool IsValid() const { return m_offsetToEdgeDataPtrBuffer != (UPInt)-1; }
29  UPInt m_offsetToEdgeDataPtrBuffer;
30  };
31 
32  struct CellPosToNavFloors
33  {
34  KY_INLINE bool IsValid() const { return m_offSetToNavFloorToEdgeDataPtrBuffer != KyUInt32MAXVAL; }
35  KY_INLINE NavFloorToEdgeDataPtrBuffer* GetNavFloorToEdgeDataPtrBuffer() const { return (NavFloorToEdgeDataPtrBuffer*)((char*)this + m_offSetToNavFloorToEdgeDataPtrBuffer); }
36 
37  KyUInt32 m_offSetToNavFloorToEdgeDataPtrBuffer;
38  KyUInt32 m_navDataChangeIdx;
39  };
40 
41 public:
42  HalfEdgeDataPtrInGrid() : m_currentOffsetFromBuffer(0) {}
43  HalfEdgeDataPtrInGrid(WorkingMemory* workingMemory, ActiveData* activeData) : m_cellBox(activeData->GetCellBox())
44  {
45  m_workingMemContainerBase.Init(workingMemory);
46  m_currentOffsetFromBuffer = 0;
47  MakeEmpty();
48  }
49 
50  void Init(WorkingMemory* workingMemory, ActiveData* activeData)
51  {
52  m_workingMemContainerBase.Init(workingMemory);
53  m_cellBox = activeData->GetCellBox();
54  m_currentOffsetFromBuffer = 0;
55  MakeEmpty();
56  }
57 
58  void ReleaseWorkingMemoryBuffer();
59 
60  KyUInt32 GetAvailableSizeInBytes() const;
61 
62  bool IsEnoughPlaceForAllocation(KyUInt32 sizeInBytes);
63 
64  bool TryToResize();
65 
66  void MakeEmpty();
67 
68  void* AllocateInBufferAndMemsetTo1(KyUInt32 totalSizeToNewOffSet);
69  void* AllocateInBufferAndMemsetTo0(KyUInt32 totalSizeToNewOffSet);
70 
71  void** AllocateEdgeDataPtrBuffer(KyUInt32 numberOfEdges);
72 
73  CellPosToNavFloors* AllocateCellPosToNavFloors(KyUInt32 numberOfCell);
74  NavFloorToEdgeDataPtrBuffer* AllocateNavFloorToEdgeData(KyUInt32 numberofFloors);
75 
76  CellPosToNavFloors* GetCellPosToNavFloors(const CellPos& cellPos);
77 
78  KyResult GetNavFloorToEdgeDataPtrBuffer(ActiveData* activeData, const NavFloorRawPtr& navFloorRawPtr, NavFloorToEdgeDataPtrBuffer*& nodeIndices);
79  NavFloorToEdgeDataPtrBuffer* GetNavFloorToEdgeDataPtrBuffer_Unsafe(const NavFloorRawPtr& navFloorRawPtr);
80 
81  bool IsInitialized() const { return m_workingMemContainerBase.IsInitialized(); }
82  bool HasNavDataChanged(Database* database);
83 public:
84  WorkingMemContainerBase m_workingMemContainerBase;
85  KyUInt32 m_currentOffsetFromBuffer;
86  CellBox m_cellBox;
87 };
88 
89 
90 KY_INLINE void HalfEdgeDataPtrInGrid::ReleaseWorkingMemoryBuffer() { m_workingMemContainerBase.ReleaseBuffer(); }
91 KY_INLINE KyUInt32 HalfEdgeDataPtrInGrid::GetAvailableSizeInBytes() const { return m_workingMemContainerBase.GetBufferSize() - m_currentOffsetFromBuffer; }
92 KY_INLINE bool HalfEdgeDataPtrInGrid::IsEnoughPlaceForAllocation(KyUInt32 sizeInBytes)
93 {
94  while (GetAvailableSizeInBytes() < sizeInBytes)
95  {
96  if (TryToResize() == false)
97  return false;
98  }
99 
100  return true;
101 }
102 
103 KY_INLINE void** HalfEdgeDataPtrInGrid::AllocateEdgeDataPtrBuffer(KyUInt32 numberOfEdges)
104 {
105  return (void**)AllocateInBufferAndMemsetTo0(numberOfEdges * sizeof(void*));
106 }
107 
108 KY_INLINE HalfEdgeDataPtrInGrid::NavFloorToEdgeDataPtrBuffer* HalfEdgeDataPtrInGrid::AllocateNavFloorToEdgeData(KyUInt32 numberofFloors)
109 {
110  return (NavFloorToEdgeDataPtrBuffer*)AllocateInBufferAndMemsetTo1(sizeof(NavFloorToEdgeDataPtrBuffer) * numberofFloors);
111 }
112 
113 KY_INLINE HalfEdgeDataPtrInGrid::CellPosToNavFloors* HalfEdgeDataPtrInGrid::AllocateCellPosToNavFloors(KyUInt32 numberOfCell)
114 {
115  return (CellPosToNavFloors*)AllocateInBufferAndMemsetTo1(sizeof(CellPosToNavFloors) * numberOfCell);
116 }
117 
118 
119 KY_INLINE HalfEdgeDataPtrInGrid::CellPosToNavFloors* HalfEdgeDataPtrInGrid::GetCellPosToNavFloors(const CellPos& cellPos)
120 {
121  CellPosToNavFloors* memoryStartForGrid = (CellPosToNavFloors*)m_workingMemContainerBase.GetBuffer();
122  return memoryStartForGrid + m_cellBox.GetRowMajorIndex(cellPos);
123 }
124 
125 
126 KY_INLINE HalfEdgeDataPtrInGrid::NavFloorToEdgeDataPtrBuffer* HalfEdgeDataPtrInGrid::GetNavFloorToEdgeDataPtrBuffer_Unsafe(const NavFloorRawPtr& navFloorRawPtr)
127 {
128  KY_DEBUG_ASSERTN(m_cellBox.IsInside(navFloorRawPtr.GetCellPos()), ("Invalid CellBox"));
129 
130  NavFloor* navFloor = navFloorRawPtr.GetNavFloor();
131  const CellPos& cellPos = navFloor->GetCellPos();
132 
133  CellPosToNavFloors* cellPosToNavFloors = GetCellPosToNavFloors(cellPos);
134  KY_DEBUG_ASSERTN(cellPosToNavFloors->IsValid(), ("Bad usage of UnSafe function"));
135 
136  NavFloorToEdgeDataPtrBuffer& navFloorToNodeIndices = cellPosToNavFloors->GetNavFloorToEdgeDataPtrBuffer()[navFloor->GetIndexInCollection()];
137  KY_DEBUG_ASSERTN(navFloorToNodeIndices.IsValid(), ("Bad usage of UnSafe function"));
138 
139  return &navFloorToNodeIndices;
140 }
141 
142 
143 }
144 
145 
146 
147 #endif //Navigation_HalfEdgeDataPtrInGrid_H
148 
KyInt32 KyResult
Defines a type that can be returned by methods or functions in the Gameware Navigation SDK to indicat...
Definition: types.h:254
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
KyUInt32 NavHalfEdgeIdx
An index that uniquely identifies a single edge of a triangle within the set of edges owned by a NavF...
Definition: navmeshtypes.h:87
Definition: gamekitcrowddispersion.h:20
unsigned int KyUInt32
Type used internally to represent an unsigned 32-bit integer.
Definition: types.h:36
#define KyUInt32MAXVAL
The maximum value that can be stored in the KyUInt32 variable type.
Definition: types.h:226