Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cOctreeNode.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cOctreeNode_H
7 #define _INC_cOctreeNode_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cOctree.h"
13 #include "cOctreeObject.h"
16 #include "GrayLib/include/Misc/cPool.h" // IPoolNode
17 #include "GrayCore/include/CList.h"
18 
19 namespace Gray3D
20 {
22  : public IPoolNode
23  , public cListT<cOctreeObject>
24  , public cOctreeNodeCont
25  {
29 
30  friend class cOctree;
31  friend class cOctreeObject;
32  friend class cOctreeNodeCont;
33  friend class cOctreeNodePool;
35 
36  protected:
37  int m_iLevel;
41 
42  protected:
43  static constexpr bool IsInsideNode(int v) noexcept
44  {
45  return v >= 0 && v < 6;
46  }
47  constexpr bool IsInsideNode(const cVec3i& vPi) const noexcept
48  {
49  return IsInsideNode(vPi.m_x - (m_vTreePos.m_x << 2) + 1)
50  && IsInsideNode(vPi.m_y - (m_vTreePos.m_y << 2) + 1)
51  && IsInsideNode(vPi.m_z - (m_vTreePos.m_z << 2) + 1);
52  }
53 
54  void onDetachSpatialObject(cOctreeObject* pObj);
55 
56  HRESULT DisposeThis();
57  void FreeThisNode()
58  {
59  if (m_pParentNode != nullptr)
60  {
61  // i am a regular node.
62  CHILDIDX_t nChildIdx = get_ChildIdx();
63  ASSERT(m_pParentNode->m_aChildNodes[nChildIdx] == this);
64  m_pParentNode->FreeChildNode(nChildIdx);
65  }
66  else
67  {
68  // i am a root node.
69  CHILDIDX_t nRootIdx = get_RootIdx();
70  if (m_pTree->m_aChildNodes[nRootIdx] == nullptr) // a transient root node.
71  {
72  DisposeThis();
73  }
74  else
75  {
76  ASSERT(m_pTree->m_aChildNodes[nRootIdx] == this);
77  m_pTree->FreeChildNode(nRootIdx);
78  }
79  }
80  }
81 
83  {
85  if (m_nChildNodes || !LIST_t::isEmpty())
86  return false;
87  FreeThisNode();
88  return true; // was deleted
89  }
90 
91  bool CollapseLeafAndTrimRootCheck();
92 
93  public:
94  cOctreeNode();
95  virtual ~cOctreeNode();
96 
97  float get_StepF() const noexcept
98  {
99  return cFloatEst::pow2(m_iLevel);
100  }
101 
102  static inline CHILDIDX_t MakeChildIdx(const cVec3i& vPi)
103  {
106  return (vPi.m_x & 1) + ((vPi.m_y & 1) << 1) + ((vPi.m_z & 1) << 2);
107  }
108 
110  {
111  ASSERT(m_pParentNode == nullptr);
112  return cOctree::MakeRootIdx(m_vTreePos);
113  }
114 
116  {
118  return MakeChildIdx(m_vTreePos);
119  }
120 
121  cBounds3f get_BoundBox() const
122  {
124  cBounds3f bounds;
125  bounds.m_vMin.x = bounds.m_vMax.x = (float)m_vTreePos.m_x;
126  bounds.m_vMin.y = bounds.m_vMax.y = (float)m_vTreePos.m_y;
127  bounds.m_vMin.z = bounds.m_vMax.z = (float)m_vTreePos.m_z;
128 
129  static const cVector3f k_vMio(-0.5f, -0.5f, -0.5f);
130  bounds.m_vMin += k_vMio;
131  static const cVector3f k_vMao(1.5f, 1.5f, 1.5f);
132  bounds.m_vMax += k_vMao;
133 
134  float fStepSize = get_StepF();
135  bounds.m_vMin *= fStepSize;
136  bounds.m_vMax *= fStepSize;
137  return bounds;
138  }
139 
141  {
143  float fStepSize = get_StepF();
144 
145  // 1.73205080756887729 = square root of 3
146  cSpheref bsphere(0.5f + (float)m_vTreePos.m_x, 0.5f + (float)m_vTreePos.m_y, 0.5f + (float)m_vTreePos.m_z,
147  1.73205080756887729f * fStepSize); // node bsphere fRadius
148 
149  bsphere.m_vCenter *= fStepSize;
150  return bsphere;
151  }
152 
154  {
157  cSpheref bsphereThis = get_BoundSphere();
158  return bsphereThis.GetIntersectSphere(rSphere);
159  }
160 
161  // cOctreeNodeCont::ISpatialTest tests for objects in the node.
162  virtual bool TraverseIntersectSphere(ISpatialTestVisitor* tif, const cSpheref& frustum) const override;
163  virtual bool TraverseIntersectBounds(ISpatialTestVisitor* tif, const cBounds3f& bbox) const override;
164  virtual bool TraverseIntersectHex(ISpatialTestVisitor* tif, const cHexahedron& frustum, cHexahedron::PLANES_MASK_t planes); // override??
165  virtual bool TraverseIntersectSegment(ISpatialTestVisitor* tif, const cSegment3f& segment) override;
166  virtual bool TraverseAll(ISpatialTestVisitor* tif) const override;
167 
168  int TestTree(const cOctree* pTree, CHILDIDX_t rootIdx) const; // validates tree structure
169  bool isPoolNodeValid(UINT_PTR uCheckParent = 0) const;
170  };
171 };
172 
173 #endif // _INC_cOctreeNode_H
#define GRAY3D_LINK
Definition: Gray3D.h:15
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define ASSERT(exp)
Definition: cDebugAssert.h:87
Definition: cOctree.h:28
int CHILDIDX_t
0 to 7 k_NodeMax
Definition: cOctree.h:33
cOctreeNode * m_aChildNodes[k_NodeMax]
container for sparse 2x2x2 possible subdivided children. for each sign octant
Definition: cOctree.h:37
void FreeChildNode(CHILDIDX_t ci)
Definition: cOctree.cpp:52
Definition: cOctreeNode.h:25
cOctreeNode * m_pParentNode
my parent node, nullptr = in m_pTree->m_aChildNodes. root node.
Definition: cOctreeNode.h:40
CHILDIDX_t get_RootIdx() const
Definition: cOctreeNode.h:109
void FreeThisNode()
Definition: cOctreeNode.h:57
int m_iLevel
node step = powf( 2, level ). this is stepping between nodes, size is 2x times bigger,...
Definition: cOctreeNode.h:37
cSpheref get_BoundSphere() const
Definition: cOctreeNode.h:140
static CHILDIDX_t MakeChildIdx(const cVec3i &vPi)
Definition: cOctreeNode.h:102
static constexpr bool IsInsideNode(int v) noexcept
Definition: cOctreeNode.h:43
cVec3i m_vTreePos
integer node position in m_iLevel steps. (not world coords)
Definition: cOctreeNode.h:38
constexpr bool IsInsideNode(const cVec3i &vPi) const noexcept
Definition: cOctreeNode.h:47
CHILDIDX_t get_ChildIdx() const
Definition: cOctreeNode.h:115
cBounds3f get_BoundBox() const
Definition: cOctreeNode.h:121
GINTERSECT_TYPE GetIntersectTestNode(const cSpheref &rSphere) const
Definition: cOctreeNode.h:153
bool CollapseLeafCheck()
Definition: cOctreeNode.h:82
float get_StepF() const noexcept
Definition: cOctreeNode.h:97
cOctree * m_pTree
my parent system. nullptr = free node in pool.
Definition: cOctreeNode.h:39
Definition: cOctreeObject.h:21
Definition: cOctree.h:72
static CHILDIDX_t MakeRootIdx(const cVec3i &vPi) noexcept
Definition: cOctree.h:95
static float pow2(int iPow)
Definition: cFloatEst.h:49
Definition: cHexahedron.h:18
UINT PLANES_MASK_t
Bit Mask of the 6 planes.
Definition: cHexahedron.h:24
Definition: cSegment3.h:20
Definition: cSphere.h:20
TYPE m_z
Definition: cVecT.h:544
TYPE m_x
Definition: cVecT.h:544
TYPE m_y
Definition: cVecT.h:544
Definition: cVector.h:94
Definition: cList.h:224
Definition: cSpatialTest.h:19
Definition: cPool.h:150
Definition: Gray3D.cpp:12
GINTERSECT_TYPE
Definition: cVecT.h:20