gwnavruntime/querysystem/workingmemcontainers/statusingridbase.h Source File

statusingridbase.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 #ifndef Navigation_StatusInGridBase_H
8 #define Navigation_StatusInGridBase_H
9 
13 
14 namespace Kaim
15 {
16 
17 class StatusInGridBase
18 {
19 public:
20  typedef KyUInt32 StatusWord;
21 
22  struct IndexedStatus
23  {
24  KY_INLINE StatusWord* GetBufferOfStatus() const { return (StatusWord*)((char*)this + m_offSetToStatus); }
25 
26  KY_INLINE bool IsValid() const { return m_offSetToStatus != KyUInt32MAXVAL; }
27 
28  KY_INLINE bool IsNodeOpen(KyUInt32 triangleIdx) const { return BitFieldUtils::IsBitSet(GetBufferOfStatus(), triangleIdx); }
29  KY_INLINE void OpenNode(KyUInt32 triangleIdx) const { BitFieldUtils::SetBit(GetBufferOfStatus(), triangleIdx); }
30 
31  KY_INLINE void SetBufferOfStatusWord(StatusWord* buffer) { m_offSetToStatus = (KyUInt32)((char*)buffer - (char*)(this)); }
32 
33  private:
34  KyUInt32 m_offSetToStatus;
35  };
36 
37  template <class UnderlyingClass>
38  struct IntermediateStatusAccessor
39  {
40  KY_INLINE bool IsValid() const { return m_offSetToUnderlyingClass != KyUInt32MAXVAL; }
41  KY_INLINE UnderlyingClass* GetUnderlyingObjectBuffer() const { return (UnderlyingClass*)((char*)this + m_offSetToUnderlyingClass); }
42  KY_INLINE UnderlyingClass* GetUnderlyingObject(KyUInt32 objectVertexIdx) const { return &GetUnderlyingObjectBuffer()[objectVertexIdx]; }
43 
44  KY_INLINE void SetBufferOfUnderlyingObjects(UnderlyingClass* buffer) { m_offSetToUnderlyingClass = (KyUInt32)((char*)buffer - (char*)(this)); }
45  private:
46  KyUInt32 m_offSetToUnderlyingClass;
47  };
48 
49 public:
50  StatusInGridBase() {}
51 
52  StatusInGridBase(WorkingMemory* workingMemory, const CellBox& cellBox) : m_cellBox(cellBox)
53  {
54  m_workingMemContainerBase.Init(workingMemory);
55  m_currentOffsetFromBuffer = 0;
56  }
57 
58  void Init(WorkingMemory* workingMemory, const CellBox& cellBox)
59  {
60  m_workingMemContainerBase.Init(workingMemory);
61  m_cellBox = cellBox;
62  m_currentOffsetFromBuffer = 0;
63  }
64 
65  void ReleaseWorkingMemoryBuffer() { m_workingMemContainerBase.ReleaseBuffer(); }
66 
67  KY_INLINE KyUInt32 GetAvailableSizeInBytes() const { return m_workingMemContainerBase.GetBufferSize() - m_currentOffsetFromBuffer; }
68 
69  KY_INLINE bool IsEnoughPlaceForAllocation(KyUInt32 sizeInBytes)
70  {
71  while (GetAvailableSizeInBytes() < sizeInBytes)
72  {
73  if (TryToResize() == false)
74  return false;
75  }
76 
77  return true;
78  }
79 
80  bool TryToResize()
81  {
82  void* oldBuffer = GetBuffer();
83  KyUInt32 oldSize = m_workingMemContainerBase.GetBufferSize();
84 
85  void* newBuffer = m_workingMemContainerBase.Resize();
86  if (newBuffer == KY_NULL)
87  return false;
88 
89  memcpy(newBuffer, oldBuffer, oldSize);
90 #if defined(KY_BUILD_DEBUG)
91  memset(oldBuffer, 0xAB, oldSize);
92 #endif
93  m_workingMemContainerBase.ReleaseMemory((void*)oldBuffer);
94  return true;
95  }
96 
97  void* AllocateInBufferAndMemset(KyUInt32 sizeOfOneElementInBytes, KyUInt32 numberOfElements, KyUInt8 memsetValue);
98  StatusWord* AllocateStatusWords(KyUInt32 numberOfNodes);
99 
100  void* GetBuffer() const;
101  bool IsInitialized() const;
102 public:
103  WorkingMemContainerBase m_workingMemContainerBase;
104  KyUInt32 m_currentOffsetFromBuffer;
105  CellBox m_cellBox;
106 };
107 
108 KY_INLINE void* StatusInGridBase::GetBuffer() const { return m_workingMemContainerBase.GetBuffer(); }
109 KY_INLINE bool StatusInGridBase::IsInitialized() const { return m_workingMemContainerBase.IsInitialized(); }
110 
111 KY_INLINE void* StatusInGridBase::AllocateInBufferAndMemset(KyUInt32 sizeOfOneElementInBytes, KyUInt32 numberOfElements, KyUInt8 memSetValue)
112 {
113  KY_DEBUG_ASSERTN(sizeOfOneElementInBytes %4 == 0, ("this function cannot be called with other values multiple of 4"));
114 
115  const KyUInt32 totalSizeToNewOffSet = sizeOfOneElementInBytes * numberOfElements;
116  if (IsEnoughPlaceForAllocation(totalSizeToNewOffSet) == false)
117  return KY_NULL;
118 
119  void* returnValue = (void*)((char*)GetBuffer() + m_currentOffsetFromBuffer);
120 
121  memset((char*)GetBuffer() + m_currentOffsetFromBuffer, memSetValue, totalSizeToNewOffSet);
122 
123  m_currentOffsetFromBuffer += totalSizeToNewOffSet;
124  return returnValue;
125 }
126 
127 KY_INLINE StatusInGridBase::StatusWord* StatusInGridBase::AllocateStatusWords(KyUInt32 numberOfNodes)
128 {
129  return (StatusInGridBase::StatusWord*)AllocateInBufferAndMemset(sizeof(StatusInGridBase::StatusWord), BitFieldUtils::GetWordsCount(numberOfNodes), 0);
130 }
131 
132 }
133 
134 
135 #endif
136 
Box2i CellBox
A type that represents a bounding box around cells in a 2D grid.
Definition: navmeshtypes.h:34
#define KY_NULL
Null value.
Definition: types.h:247
unsigned char KyUInt8
Type used internally to represent an unsigned 8-bit integer.
Definition: types.h:41
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