Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cOctree.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cOctree_H
7 #define _INC_cOctree_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cOctreeObject.h"
13 #include "cSpatialTest.h"
14 
15 namespace Gray3D
16 {
18  class cOctreeNode;
20 
21 #ifdef _DEBUG
22 #define USE_OCTREE_DBG_ALLOC
23 #endif
24 
25  typedef cVecT3<INT32> cVec3i; // must be 32 bit int.
26 
28  {
31 
32  public:
33  typedef int CHILDIDX_t;
34  static const CHILDIDX_t k_NodeMax = 8;
35 
36  protected:
37  cOctreeNode* m_aChildNodes[k_NodeMax];
39 
40  protected:
41  mutable int m_iSpatialTestVal;
42 
43  protected:
44 
45  bool TraverseIntersectHex(ISpatialTestVisitor* pResults, const cHexahedron& frustum, cHexahedron::PLANES_MASK_t planes);
46 
47  // Implement ISpatialTest for searching the Octree.
48  virtual bool TraverseIntersectSphere(ISpatialTestVisitor* pResults, const cSpheref& sphere) const override;
49  virtual bool TraverseIntersectBounds(ISpatialTestVisitor* pResults, const cBounds3f& bbox) const override;
50  virtual bool TraverseIntersectHex(ISpatialTestVisitor* pResults, const cHexahedron& frustum) override;
51  virtual bool TraverseIntersectSegment(ISpatialTestVisitor* pResults, const cSegment3f& segment) override;
52  virtual bool TraverseAll(ISpatialTestVisitor* pResults) const override;
53 
54  public:
55  cOctreeNodeCont() noexcept
56  : m_nChildNodes(0)
57  , m_iSpatialTestVal(0)
58  {
59  cMem::Zero(m_aChildNodes, sizeof(m_aChildNodes));
60  }
61  virtual ~cOctreeNodeCont()
62  {
63  }
64 
65  void FreeChildNode(CHILDIDX_t ci);
66  void FreeChildNodes();
67 
69  };
70 
72  {
81 
82  friend class cOctreeNode;
83 
84  private:
85  static INT32 _FloatUnpack(float fVal, int iBase) noexcept;
86 
87  cOctreeNode* AllocNode();
88  void FreeNode(cOctreeNode* pNode);
89  static cVec3i MakeNodePoint(const cVector3f& vPos, int iLevel) noexcept;
90 
91  public:
92  cOctree();
93  virtual ~cOctree();
94 
95  static inline CHILDIDX_t MakeRootIdx(const cVec3i& vPi) noexcept
96  {
100  return ((vPi.m_x >> 31) & 1) + ((vPi.m_y >> (31 - 1)) & 2) + ((vPi.m_z >> (31 - 2)) & 4);
101  }
102 
103  void AddToOctree(cOctreeObject* pObj, const cVector3f& vPos, float fRadius);
104 
105  int TestTree() const;
106 
107 
108 #if 0
109  STDMETHOD_(ULONG, AddRef)(void) override
110  {
112  return 1;
113  }
114  STDMETHOD_(ULONG, Release)(void) override
115  {
117  return 1;
118  }
119  STDMETHOD(QueryInterface)( /* [in] */ const IID& riid, /* [iid_is][out] */ void __RPC_FAR* __RPC_FAR* ppvObject) override
120  {
121  return E_FAIL;
122  }
123 #endif
124 
125  };
126 }
127 #endif // _INC_cOctree_H
#define GRAY3D_LINK
Definition: Gray3D.h:15
Definition: cOctree.h:28
UNITTEST_FRIEND(cOctree)
run an heavy usage simulation to test
int m_iSpatialTestVal
validation value. incremented periodically (per frame?)
Definition: cOctree.h:41
int CHILDIDX_t
0 to 7 k_NodeMax
Definition: cOctree.h:33
virtual ~cOctreeNodeCont()
Definition: cOctree.h:61
cOctreeNodeCont() noexcept
Definition: cOctree.h:55
CHILDIDX_t m_nChildNodes
Number of actually populated m_aChildNodes. <= 8.
Definition: cOctree.h:38
Definition: cOctreeNode.h:25
Definition: cOctreeObject.h:21
Definition: cOctree.h:72
static CHILDIDX_t MakeRootIdx(const cVec3i &vPi) noexcept
Definition: cOctree.h:95
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
Definition: cVector.h:94
Definition: cSpatialTest.h:19
Definition: cSpatialTest.h:106
Definition: Gray3D.cpp:12
cVecT3< INT32 > cVec3i
Definition: cOctree.h:25
DECLARE_INTERFACE(ISpatialTestVisitor)
UNITTEST2_PREDEF(cOctree)
static void Zero(void *pData, size_t nSizeBlock) noexcept
Definition: cMem.h:100
Definition: IUnknown.h:34