Shapeworks Studio  2.1
Shape analysis software suite
itkPSMRegionNeighborhood.hxx
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 __itkPSMRegionNeighborhood_hxx
19 #define __itkPSMRegionNeighborhood_hxx
20 #include "itkPSMRegionNeighborhood.h"
21 
22 namespace itk
23 {
24 template<unsigned int VDimension>
26 {
27  Superclass::SetDomain(d);
28  m_Tree->ConstructTree(d->GetLowerBound(), d->GetUpperBound(), m_TreeLevels);
29 }
30 
31 template <unsigned int VDimension>
32 typename PSMRegionNeighborhood<VDimension>::PointVectorType
34 ::FindNeighborhoodPoints(const PointType &center, double radius) const
35 {
36  // Compute bounding box of the given hypersphere.
37  PointType l, u;
38  for (unsigned int i = 0; i < VDimension; i++)
39  {
40  l[i] = center[i] - radius;
41  u[i] = center[i] + radius;
42  }
43 
44  // Grab the list of points in this bounding box.
45  typename PointTreeType::PointIteratorListType pointlist = m_Tree->FindPointsInRegion(l, u);
46 
47  // Allocate return vector. Reserve ensures no extra copies occur.
48  PointVectorType ret;
49  ret.reserve(pointlist.size());
50 
51  // Add any point whose distance from center is less than radius to the return
52  // list.
53  for (typename PointTreeType::PointIteratorListType::const_iterator it = pointlist.begin();
54  it != pointlist.end(); it++)
55  {
56  // double dist = this->GetDomain()->Distance(center, (*it)->Point);
57  double sum = 0.0;
58  for (unsigned int i = 0; i < VDimension; i++)
59  {
60  double q = center[i] - (*it)->Point[i];
61  sum += q*q;
62  }
63  sum = sqrt(sum);
64 
65  if ( sum < radius && sum >0 )
66  {
67  ret.push_back( **it );
68  }
69  }
70 
71  return ret;
72 }
73 
74 template <unsigned int VDimension>
76 ::AddPosition(const PointType &p, unsigned int idx, int)
77 {
78  // Cache this point and index into the tree. AddPoint returns a pointer
79  // to the cached values and the node in which the point resides. This info
80  // is saved for efficient moves and deletes.
81  typename IteratorNodePair::IteratorType it;
82  typename IteratorNodePair::NodePointerType node;
83  it = m_Tree->AddPoint(p, idx, node);
84 
85  m_IteratorMap->operator[](idx) = IteratorNodePair(it, node);
86 }
87 
88 template <unsigned int VDimension>
90 ::SetPosition(const PointType &p, unsigned int idx, int threadId)
91 {
92  // Check whether the given index has moved outside its current bin. If it
93  // has moved outside its current bin, delete and reinsert into the tree.
94  IteratorNodePair pr = m_IteratorMap->operator[](idx);
95 
96  for (unsigned int i = 0; i < VDimension; i++)
97  {
98  if (p[i] < pr.NodePointer->GetLowerBound()[i] || p[i] > pr.NodePointer->GetUpperBound()[i])
99  {
100  this->RemovePosition(idx, threadId);
101  this->AddPosition(p, idx, threadId);
102  return;
103  }
104  }
105 
106  // Otherwise, simply modify the given point value.
107  pr.Iterator->Point = p;
108 }
109 
110 template <unsigned int VDimension>
112 ::RemovePosition(unsigned int idx, int)
113 {
114  IteratorNodePair pr = m_IteratorMap->operator[](idx);
115  m_IteratorMap->Erase(idx);
116  pr.NodePointer->GetList().erase(pr.Iterator);
117 }
118 
119 
120 }
121 
122 #endif
void AddPosition(const PointType &p, unsigned int idx, int threadId=0)
Base class for defining the domain in which a particle system exists.
Definition: itkPSMDomain.h:48
virtual void SetDomain(DomainType *p)
std::vector< PSMPointIndexPair< VDimension > > PointVectorType
virtual const PointType & GetLowerBound() const
Definition: itkPSMDomain.h:95
virtual PointVectorType FindNeighborhoodPoints(const PointType &, double) const