Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Octree.h
1 /*
2 Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7 
8 Redistributions of source code must retain the above copyright notice, this list of
9 conditions and the following disclaimer. Redistributions in binary form must reproduce
10 the above copyright notice, this list of conditions and the following disclaimer
11 in the documentation and/or other materials provided with the distribution.
12 
13 Neither the name of the Johns Hopkins University nor the names of its contributors
14 may be used to endorse or promote products derived from this software without specific
15 prior written permission.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
20 SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26 DAMAGE.
27 */
28 
29 #ifndef OCT_NODE_INCLUDED
30 #define OCT_NODE_INCLUDED
31 
32 #include "Allocator.h"
33 #include "BinaryNode.h"
34 #include "MarchingCubes.h"
35 
36 #define DIMENSION 3
37 
38 template< class NodeData , class Real=float >
39 class OctNode
40 {
41 private:
42  static int UseAlloc;
43 
45  {
46  public:
47  int count;
48  void Function( const OctNode<NodeData,Real>* node1 , const OctNode<NodeData,Real>* node2 );
49  };
50  template<class NodeAdjacencyFunction>
51  void __processNodeFaces(OctNode* node,NodeAdjacencyFunction* F,int cIndex1,int cIndex2,int cIndex3,int cIndex4);
52  template<class NodeAdjacencyFunction>
53  void __processNodeEdges(OctNode* node,NodeAdjacencyFunction* F,int cIndex1,int cIndex2);
54  template<class NodeAdjacencyFunction>
55  void __processNodeNodes(OctNode* node,NodeAdjacencyFunction* F);
56  template<class NodeAdjacencyFunction>
57  static void __ProcessNodeAdjacentNodes(int dx,int dy,int dz,OctNode* node1,int radius1,OctNode* node2,int radius2,int cWidth2,NodeAdjacencyFunction* F);
58  template<class TerminatingNodeAdjacencyFunction>
59  static void __ProcessTerminatingNodeAdjacentNodes(int dx,int dy,int dz,OctNode* node1,int radius1,OctNode* node2,int radius2,int cWidth2,TerminatingNodeAdjacencyFunction* F);
60  template<class PointAdjacencyFunction>
61  static void __ProcessPointAdjacentNodes(int dx,int dy,int dz,OctNode* node2,int radius2,int cWidth2,PointAdjacencyFunction* F);
62  template<class NodeAdjacencyFunction>
63  static void __ProcessFixedDepthNodeAdjacentNodes(int dx,int dy,int dz,OctNode* node1,int radius1,OctNode* node2,int radius2,int cWidth2,int depth,NodeAdjacencyFunction* F);
64  template<class NodeAdjacencyFunction>
65  static void __ProcessMaxDepthNodeAdjacentNodes(int dx,int dy,int dz,OctNode* node1,int radius1,OctNode* node2,int radius2,int cWidth2,int depth,NodeAdjacencyFunction* F);
66 
67  // This is made private because the division by two has been pulled out.
68  static inline int Overlap(int c1,int c2,int c3,int dWidth);
69  inline static int ChildOverlap(int dx,int dy,int dz,int d,int cRadius2);
70 
71  const OctNode* __faceNeighbor(int dir,int off) const;
72  const OctNode* __edgeNeighbor(int o,const int i[2],const int idx[2]) const;
73  OctNode* __faceNeighbor(int dir,int off,int forceChildren);
74  OctNode* __edgeNeighbor(int o,const int i[2],const int idx[2],int forceChildren);
75 public:
76  static const int DepthShift,OffsetShift,OffsetShift1,OffsetShift2,OffsetShift3;
77  static const int DepthMask,OffsetMask;
78 
79  static AllocatorT<OctNode> Allocator;
80  static int UseAllocator(void);
81  static void SetAllocator(int blockSize);
82 
83  OctNode* parent;
84  OctNode* children;
85  short d , off[DIMENSION];
86  NodeData nodeData;
87 
88  OctNode(void);
89  ~OctNode(void);
90  int initChildren( void );
91 
92  void depthAndOffset(int& depth,int offset[DIMENSION]) const;
93  int depth(void) const;
94  static inline void DepthAndOffset(const long long& index,int& depth,int offset[DIMENSION]);
95  static inline void CenterAndWidth(const long long& index,Point3D<Real>& center,Real& width);
96  static inline int Depth(const long long& index);
97  static inline void Index(int depth,const int offset[3],short& d,short off[DIMENSION]);
98  void centerAndWidth( Point3D<Real>& center , Real& width ) const;
99  bool isInside( Point3D< Real > p ) const;
100 
101  int leaves(void) const;
102  int maxDepthLeaves(int maxDepth) const;
103  int nodes(void) const;
104  int maxDepth(void) const;
105 
106  const OctNode* root(void) const;
107 
108  const OctNode* nextLeaf(const OctNode* currentLeaf=NULL) const;
109  OctNode* nextLeaf(OctNode* currentLeaf=NULL);
110  const OctNode* nextNode(const OctNode* currentNode=NULL) const;
111  OctNode* nextNode(OctNode* currentNode=NULL);
112  const OctNode* nextBranch(const OctNode* current) const;
113  OctNode* nextBranch(OctNode* current);
114  const OctNode* prevBranch(const OctNode* current) const;
115  OctNode* prevBranch(OctNode* current);
116 
117  void setFullDepth(int maxDepth);
118 
119  void printLeaves(void) const;
120  void printRange(void) const;
121 
122  template<class NodeAdjacencyFunction>
123  void processNodeFaces(OctNode* node,NodeAdjacencyFunction* F,int fIndex,int processCurrent=1);
124  template<class NodeAdjacencyFunction>
125  void processNodeEdges(OctNode* node,NodeAdjacencyFunction* F,int eIndex,int processCurrent=1);
126  template<class NodeAdjacencyFunction>
127  void processNodeCorners(OctNode* node,NodeAdjacencyFunction* F,int cIndex,int processCurrent=1);
128  template<class NodeAdjacencyFunction>
129  void processNodeNodes(OctNode* node,NodeAdjacencyFunction* F,int processCurrent=1);
130 
131  template<class NodeAdjacencyFunction>
132  static void ProcessNodeAdjacentNodes(int maxDepth,OctNode* node1,int width1,OctNode* node2,int width2,NodeAdjacencyFunction* F,int processCurrent=1);
133  template<class NodeAdjacencyFunction>
134  static void ProcessNodeAdjacentNodes(int dx,int dy,int dz,OctNode* node1,int radius1,OctNode* node2,int radius2,int width2,NodeAdjacencyFunction* F,int processCurrent=1);
135  template<class TerminatingNodeAdjacencyFunction>
136  static void ProcessTerminatingNodeAdjacentNodes(int maxDepth,OctNode* node1,int width1,OctNode* node2,int width2,TerminatingNodeAdjacencyFunction* F,int processCurrent=1);
137  template<class TerminatingNodeAdjacencyFunction>
138  static void ProcessTerminatingNodeAdjacentNodes(int dx,int dy,int dz,OctNode* node1,int radius1,OctNode* node2,int radius2,int width2,TerminatingNodeAdjacencyFunction* F,int processCurrent=1);
139  template<class PointAdjacencyFunction>
140  static void ProcessPointAdjacentNodes(int maxDepth,const int center1[3],OctNode* node2,int width2,PointAdjacencyFunction* F,int processCurrent=1);
141  template<class PointAdjacencyFunction>
142  static void ProcessPointAdjacentNodes(int dx,int dy,int dz,OctNode* node2,int radius2,int width2,PointAdjacencyFunction* F,int processCurrent=1);
143  template<class NodeAdjacencyFunction>
144  static void ProcessFixedDepthNodeAdjacentNodes(int maxDepth,OctNode* node1,int width1,OctNode* node2,int width2,int depth,NodeAdjacencyFunction* F,int processCurrent=1);
145  template<class NodeAdjacencyFunction>
146  static void ProcessFixedDepthNodeAdjacentNodes(int dx,int dy,int dz,OctNode* node1,int radius1,OctNode* node2,int radius2,int width2,int depth,NodeAdjacencyFunction* F,int processCurrent=1);
147  template<class NodeAdjacencyFunction>
148  static void ProcessMaxDepthNodeAdjacentNodes(int maxDepth,OctNode* node1,int width1,OctNode* node2,int width2,int depth,NodeAdjacencyFunction* F,int processCurrent=1);
149  template<class NodeAdjacencyFunction>
150  static void ProcessMaxDepthNodeAdjacentNodes(int dx,int dy,int dz,OctNode* node1,int radius1,OctNode* node2,int radius2,int width2,int depth,NodeAdjacencyFunction* F,int processCurrent=1);
151 
152  static int CornerIndex(const Point3D<Real>& center,const Point3D<Real> &p);
153 
154  OctNode* faceNeighbor(int faceIndex,int forceChildren=0);
155  const OctNode* faceNeighbor(int faceIndex) const;
156  OctNode* edgeNeighbor(int edgeIndex,int forceChildren=0);
157  const OctNode* edgeNeighbor(int edgeIndex) const;
158  OctNode* cornerNeighbor(int cornerIndex,int forceChildren=0);
159  const OctNode* cornerNeighbor(int cornerIndex) const;
160 
161  OctNode* getNearestLeaf(const Point3D<Real>& p);
162  const OctNode* getNearestLeaf(const Point3D<Real>& p) const;
163 
164  static int CommonEdge(const OctNode* node1,int eIndex1,const OctNode* node2,int eIndex2);
165  static int CompareForwardDepths(const void* v1,const void* v2);
166  static int CompareByDepthAndXYZ( const void* v1 , const void* v2 );
167  static int CompareByDepthAndZIndex( const void* v1 , const void* v2 );
168  static int CompareForwardPointerDepths(const void* v1,const void* v2);
169  static int CompareBackwardDepths(const void* v1,const void* v2);
170  static int CompareBackwardPointerDepths(const void* v1,const void* v2);
171 
172 
173  template<class NodeData2>
174  OctNode& operator = (const OctNode<NodeData2,Real>& node);
175 
176  static inline int Overlap2(const int &depth1,const int offSet1[DIMENSION],const Real& multiplier1,const int &depth2,const int offSet2[DIMENSION],const Real& multiplier2);
177 
178 
179  int write(const char* fileName) const;
180  int write(FILE* fp) const;
181  int read(const char* fileName);
182  int read(FILE* fp);
183 
185  {
186  public:
187  OctNode* neighbors[3][3][3];
188  Neighbors3( void );
189  void clear( void );
190  };
192  {
193  public:
194  Neighbors3* neighbors;
195 
196  NeighborKey3( void );
197  ~NeighborKey3( void );
198 
199  void set( int depth );
200  Neighbors3& setNeighbors( OctNode* root , Point3D< Real > p , int d );
201  Neighbors3& getNeighbors( OctNode* root , Point3D< Real > p , int d );
202  Neighbors3& setNeighbors( OctNode* node , bool flags[3][3][3] );
203  Neighbors3& setNeighbors( OctNode* node );
204  Neighbors3& getNeighbors( OctNode* node );
205  };
207  {
208  public:
209  const OctNode* neighbors[3][3][3];
210  ConstNeighbors3( void );
211  void clear( void );
212  };
214  {
215  public:
216  ConstNeighbors3* neighbors;
217 
218  ConstNeighborKey3(void);
219  ~ConstNeighborKey3(void);
220 
221  void set(int depth);
222  ConstNeighbors3& getNeighbors( const OctNode* node );
223  ConstNeighbors3& getNeighbors( const OctNode* node , int minDepth );
224  };
226  {
227  public:
228  OctNode* neighbors[5][5][5];
229  Neighbors5( void );
230  void clear( void );
231  };
233  {
234  public:
235  const OctNode* neighbors[5][5][5];
236  ConstNeighbors5( void );
237  void clear( void );
238  };
239 
241  {
242  int _depth;
243  public:
244  Neighbors5* neighbors;
245 
246  NeighborKey5( void );
247  ~NeighborKey5( void );
248 
249  void set( int depth );
250  Neighbors5& getNeighbors( OctNode* node );
251  Neighbors5& setNeighbors( OctNode* node , int xStart=0 , int xEnd=5 , int yStart=0 , int yEnd=5 , int zStart=0 , int zEnd=5 );
252  };
254  {
255  int _depth;
256  public:
257  ConstNeighbors5* neighbors;
258 
259  ConstNeighborKey5( void );
260  ~ConstNeighborKey5( void );
261 
262  void set( int depth );
263  ConstNeighbors5& getNeighbors( const OctNode* node );
264  };
265 
266  void centerIndex(int maxDepth,int index[DIMENSION]) const;
267  int width(int maxDepth) const;
268 };
269 
270 #ifndef DOXY_IGNORE_THIS
271 #include "Octree.inl"
272 #endif
273 
274 #endif // OCT_NODE
Definition: Octree.h:39