Shapeworks Studio  2.1
Shape analysis software suite
itkPSMPointTree.h
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 __itkPSMPointTree_h
19 #define __itkPSMPointTree_h
20 
21 #include "itkPSMPointIndexPair.h"
22 #include "itkLightObject.h"
23 #include "itkDataObject.h"
24 #include "itkWeakPointer.h"
25 #include "itkTreeContainer.h"
26 #include "itkPoint.h"
27 #include <list>
28 
29 namespace itk
30 {
31 
33 template <int a, int b>
34 struct powstruct
35 {
36  static const int c = a * powstruct<a,b-1>::c;
37 };
38 template<int a>
39 struct powstruct<a,0>
40 {
41  static const int c = 1;
42 };
43 
54 template <unsigned int VDimension>
55 class PSMPointTreeNode : public LightObject
56 {
57 public:
60  typedef SmartPointer<Self> Pointer;
61  typedef SmartPointer<const Self> ConstPointer;
62  typedef LightObject Superclass;
63  itkTypeMacro( PSMPointTreeNode, LightObject);
64 
66  itkNewMacro(Self);
67 
70  itkStaticConstMacro(BranchesPerNode, int, (powstruct<2, VDimension>::c));
71 
74 
76  typedef std::list<PSMPointIndexPair<VDimension> > PointListType;
77 
79  bool operator==(const PSMPointTreeNode &o) const
80  {
81  if ((m_LowerBound == o.m_LowerBound) && (m_UpperBound == o.m_UpperBound) ) return true;
82  else return false;
83  }
84 
89  bool IsLeaf() const
90  {
91  if (m_Branches[0].GetPointer() != 0) return false;
92  else return true;
93  }
94 
96  typename PointListType::iterator InsertElement(const PSMPointIndexPair<VDimension> &elem)
97  {
98  return m_List.insert(m_List.end(), elem);
99  }
100 
103  bool Contains( const PointType &p ) const
104  {
105  for (unsigned int i = 0; i < VDimension; i++)
106  {
107  if (p[i] < m_LowerBound[i] || p[i] >= m_UpperBound[i]) return false;
108  }
109  return true;
110  }
111 
113  void SetLowerBound( const PointType &p)
114  { m_LowerBound = p; }
115  const PointType &GetLowerBound() const
116  { return m_LowerBound; }
117  void SetUpperBound( const PointType &p)
118  { m_UpperBound = p; }
119  const PointType &GetUpperBound() const
120  { return m_UpperBound; }
121 
123  typename Self::Pointer *GetBranches()
124  { return m_Branches; }
125  const typename Self::Pointer *GetBranches() const
126  { return m_Branches; }
127 
129  typename Self::Pointer &GetBranch(unsigned int i)
130  { return m_Branches[i]; }
131  const typename Self::Pointer &GetBranch(unsigned int i) const
132  { return m_Branches[i]; }
133 
135  void SetBranch(unsigned int b, typename Self::Pointer n)
136  { this->GetBranch(b) = n; }
137 
139  void PrintSelf(std::ostream& os, Indent indent) const;
140 
142  const PointListType &GetList() const
143  { return m_List; }
144  PointListType &GetList()
145  { return m_List; }
146 
147 protected:
148  PSMPointTreeNode() { }
149  ~PSMPointTreeNode() {}
150 
151  PSMPointTreeNode(const PSMPointTreeNode &o); // purposely not implemented
152  const PSMPointTreeNode & operator=(const PSMPointTreeNode &o);
153 
154 private:
155  PointType m_LowerBound;
156  PointType m_UpperBound;
157  PointListType m_List;
158 
159  typename Self::Pointer m_Branches[powstruct<2,VDimension>::c];
160 };
161 
173  template <unsigned int VDimension>
174  class ITK_EXPORT PSMPointTree : public DataObject
175 {
176 public:
179  typedef DataObject Superclass;
180  typedef SmartPointer<Self> Pointer;
181  typedef SmartPointer<const Self> ConstPointer;
182  typedef WeakPointer<const Self> ConstWeakPointer;
183 
186 
188  typedef typename NodeType::Pointer NodePointerType;
189 
192 
195  typedef std::vector<typename PointListType::const_iterator> PointIteratorListType;
196 
198  itkNewMacro(Self);
199 
201  itkTypeMacro(PSMPointTree, DataObject);
202 
204  itkStaticConstMacro(Dimension, unsigned int, VDimension);
205 
207  itkStaticConstMacro(BranchesPerNode, int, (powstruct<2, VDimension>::c));
208 
211  itkGetMacro(Depth, unsigned int);
212 
216  void ConstructTree(const PointType &, const PointType &, unsigned int);
217 
223  PointIteratorListType FindPointsInRegion(const PointType &, const PointType &) const;
224  unsigned int FindPointsInRegion(const PointType &, const PointType &, PointIteratorListType &) const;
225 
228  NodePointerType GetNode(const PointType &);
229  const NodePointerType GetNode(const PointType &) const;
230 
232  itkGetObjectMacro(Root, NodeType);
233  itkSetObjectMacro(Root, NodeType);
234 
245  typename PointListType::iterator AddPoint(const PointType &,
246  unsigned int, NodePointerType &);
247  typename PointListType::iterator AddPoint(const PointType &p,
248  unsigned int i)
249  {
250  NodePointerType node = NodeType::New();
251  return this->AddPoint(p, i, node);
252  }
253 
256  bool Overlap(const NodePointerType &, const PointType &, const PointType &) const;
257 
259  inline bool RegionContains(const PointType &p, const PointType &lowerbound,
260  const PointType &upperbound) const
261  {
262  for (unsigned int i = 0; i < VDimension; i++)
263  {
264  if (p[i] < lowerbound[i] || p[i] >= upperbound[i]) return false;
265  }
266  return true;
267  }
268 
269  void PrintSelf(std::ostream& os, Indent indent) const;
270 protected:
271  PSMPointTree()
272  {
273  m_Depth = 0;
274  }
275  virtual ~PSMPointTree() {}
276 
279  void BranchNode(NodePointerType &, unsigned int);
280 
284  void FindOneNodeInRegion(const NodePointerType&, const PointType &, const PointType &,
285  PointIteratorListType &) const;
286 
287 private:
288  PSMPointTree(const Self&); //purposely not implemented
289  void operator=(const Self&); //purposely not implemented
290 
291  NodePointerType m_Root;
292 
293  unsigned int m_Depth;
294 };
295 
296 } // end namespace itk
297 
298 #ifndef ITK_MANUAL_INSTANTIATION
299 #include "itkPSMPointTree.hxx"
300 #endif
301 
302 #endif
bool Contains(const PointType &p) const
PointListType::iterator InsertElement(const PSMPointIndexPair< VDimension > &elem)
bool operator==(const PSMPointTreeNode &o) const
PSMPointTreeNode< VDimension >::PointType PointType
Self::Pointer & GetBranch(unsigned int i)
PSMPointTreeNode< VDimension > NodeType
PSMPointTreeNode Self
Struct containing a Point and an index value associated with a point. This object is used mainly by P...
Point< double, VDimension > PointType
Self::Pointer * GetBranches()
const PointListType & GetList() const
std::list< PSMPointIndexPair< VDimension > > PointListType
NodeType::Pointer NodePointerType
NodeType::PointListType PointListType
void SetLowerBound(const PointType &p)
void SetBranch(unsigned int b, typename Self::Pointer n)