1 /*=========================================================================
2  *
3  *  Copyright Insight Software Consortium
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *         http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkOctreeNode_h
19 #define itkOctreeNode_h
20 #include "itkMacro.h"
21 namespace itk
22 {
23 enum LeafIdentifier { ZERO = 0, ONE = 1, TWO = 2, THREE = 3, FOUR = 4, FIVE = 5, SIX = 6, SEVEN = 7 };
24 
25 // Forward reference because of circular dependencies
26 class ITK_FORWARD_EXPORT OctreeNodeBranch;
27 class ITK_FORWARD_EXPORT OctreeBase;
28 
29 /**
30  * \class OctreeNode
31  * \brief A data structure representing a node in an Octree.
32  *
33  * OctreeNode is the basic building block of an octree. It is rarely used by
34  * itself, and commonly used by the Octree class.
35  *
36  * OctreeNodes have two states: 1) They are a Colored node and the m_Branch is
37  * a sentinal value indicating the color, or 2) they are a branch node, and
38  * m_Branch is a dynamically allocated array of 8 pointers to OctreeNodes. In
39  * the second state, the 8 child OctreeNodes are instantiated by the parent node.
40  *
41  * \author Hans J. Johnson
42  * \todo FIXME copy & paste documentation in all methods.
43  * \ingroup ITKCommon
44  */
45 class ITKCommon_EXPORT OctreeNode
46 {
47 public:
48   /**
49    * Default constructor
50    * \post After construction, it is assumed all children of this node are colored
51    * with values of 0.
52    */
53   OctreeNode();
54 
55   /**
56    * Default destructor
57    */
58   virtual ~OctreeNode();
59 
60   /**
61    * Returns the value of the specified Child for this OctreeNode
62    * \param ChildID The numerical identifier of the desired child.
63    * \return A pointer to the Desired child. NOTE: This is always an
64    * instance of an OctreeNode.
65    * @{
66    */
67   OctreeNode & GetChild(const enum LeafIdentifier ChildID) const;
68 
69   OctreeNode & GetChild(const enum LeafIdentifier ChildID);
70   /** @}
71   */
72 
73   /**
74    * Determines the color value of the specified Child for this OctreeNode
75    * \pre Must determine that the specified node is colored (Use IsNodeColored()
76    * member function.  Behavior is undefined when the child is another Octree.
77    * \return A value between 0 and 255 to indicate the color of the Desired child.
78    */
79   long int GetColor() const;
80 
81   /**
82    * Sets the color value of the specified Child for this OctreeNode
83    * \param NodeColor The desired color of this node.
84    * \post All children of the specified child are removed, and the child is set to
85    * the desired value.
86    */
87   void SetColor(int NodeColor);
88 
89   /**
90    * Sets the color value of the specified Child for this OctreeNode
91    * \post All children of the specified child are removed, and the child is set to
92    * the desired value.
93    */
94   void SetBranch(OctreeNodeBranch *NewBranch);
95 
96   /**
97    * Determines if the child is a leaf node (colored), or a branch node (uncolored)
98    * \return true if it is colored, false if it is not
99    */
100   bool IsNodeColored() const;
101 
SetParentOctree(OctreeBase * parent)102   inline void SetParentOctree(OctreeBase *parent)
103   {
104     m_Parent = parent;
105   }
106 
107 protected:
108 
109 private:
110   /**
111    * Removes all children from this node down, and sets the value
112    * value of the children to background.
113    */
114   void RemoveChildren();
115 
116   /**
117    * Each element holds COLOR or pointer to another octree node
118    */
119   OctreeNodeBranch *m_Branch;
120   OctreeBase *      m_Parent;
121 };
122 
123 class ITKCommon_EXPORT OctreeNodeBranch
124 {
125 public:
126   OctreeNodeBranch() = default;
OctreeNodeBranch(OctreeBase * parent)127   OctreeNodeBranch(OctreeBase *parent)
128   {
129     for (auto & leaf : m_Leaves)
130     {
131       leaf.SetParentOctree(parent);
132     }
133   }
134 
GetLeaf(enum LeafIdentifier LeafID)135   inline OctreeNode * GetLeaf(enum LeafIdentifier LeafID)
136   {
137     return &m_Leaves[LeafID];
138   }
139 
140 private:
141   OctreeNode m_Leaves[8];
142 };
143 } //End of itk Namespace
144 #endif                          /* itkOctreeNode_h */
145