Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkPointLocator.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "mitkPointLocator.h"
14 #include <ANN/ANN.h>
15 #include <vtkPointSet.h>
16 
18  : m_SearchTreeInitialized(false),
19  m_VtkPoints(nullptr),
20  m_MitkPoints(nullptr),
21  m_ItkPoints(nullptr),
22  m_ANNK(1),
23  m_ANNDimension(3),
24  m_ANNEpsilon(0),
25  m_ANNDataPoints(nullptr),
26  m_ANNQueryPoint(nullptr),
27  m_ANNPointIndexes(nullptr),
28  m_ANNDistances(nullptr),
29  m_ANNTree(nullptr)
30 {
31 }
32 
34 {
36  DestroyANN();
37 }
38 
39 void mitk::PointLocator::SetPoints(vtkPointSet *pointSet)
40 {
41  if (pointSet == nullptr)
42  {
43  itkWarningMacro("Points are nullptr!");
44  return;
45  }
46  vtkPoints *points = pointSet->GetPoints();
47 
48  if (m_VtkPoints)
49  {
50  if ((m_VtkPoints == points) && (m_VtkPoints->GetMTime() == points->GetMTime()))
51  {
52  return; // no need to recalculate search tree
53  }
54  }
55  m_VtkPoints = points;
56 
57  size_t size = points->GetNumberOfPoints();
58  if (m_ANNDataPoints != nullptr)
59  delete[] m_ANNDataPoints;
60  m_ANNDataPoints = annAllocPts(size, m_ANNDimension);
62  m_IndexToPointIdContainer.resize(size);
63  for (vtkIdType i = 0; (unsigned)i < size; ++i)
64  {
65  double *currentPoint = points->GetPoint(i);
66  (m_ANNDataPoints[i])[0] = currentPoint[0];
67  (m_ANNDataPoints[i])[1] = currentPoint[1];
68  (m_ANNDataPoints[i])[2] = currentPoint[2];
70  }
71  InitANN();
72 }
73 
75 {
76  if (points == nullptr)
77  {
78  itkWarningMacro("Points are nullptr!");
79  return;
80  }
81 
82  if (m_MitkPoints)
83  {
84  if ((m_MitkPoints == points) && (m_MitkPoints->GetMTime() == points->GetMTime()))
85  {
86  return; // no need to recalculate search tree
87  }
88  }
89  m_MitkPoints = points;
90 
91  size_t size = points->GetSize();
92  if (m_ANNDataPoints != nullptr)
93  delete[] m_ANNDataPoints;
94  m_ANNDataPoints = annAllocPts(size, m_ANNDimension);
96  m_IndexToPointIdContainer.resize(size);
97  size_t counter = 0;
98  mitk::PointSet::PointsContainer *pointsContainer = points->GetPointSet()->GetPoints();
99  mitk::PointSet::PointsContainer::Iterator it;
100  mitk::PointSet::PointType currentPoint;
101  mitk::PointSet::PointsContainer::ElementIdentifier currentId;
102  for (it = pointsContainer->Begin(); it != pointsContainer->End(); ++it, ++counter)
103  {
104  currentPoint = it->Value();
105  currentId = it->Index();
106  (m_ANNDataPoints[counter])[0] = currentPoint[0];
107  (m_ANNDataPoints[counter])[1] = currentPoint[1];
108  (m_ANNDataPoints[counter])[2] = currentPoint[2];
109  m_IndexToPointIdContainer[counter] = currentId;
110  }
111  InitANN();
112 }
113 
115 {
116  if (pointSet == nullptr)
117  {
118  itkWarningMacro("Points are nullptr!");
119  return;
120  }
121 
122  if (m_ItkPoints)
123  {
124  if ((m_ItkPoints == pointSet) && (m_ItkPoints->GetMTime() == pointSet->GetMTime()))
125  {
126  return; // no need to recalculate search tree
127  }
128  }
129  m_ItkPoints = pointSet;
130 
131  size_t size = pointSet->GetNumberOfPoints();
132  if (m_ANNDataPoints != nullptr)
133  delete[] m_ANNDataPoints;
134  m_ANNDataPoints = annAllocPts(size, m_ANNDimension);
136  m_IndexToPointIdContainer.resize(size);
137  size_t counter = 0;
138  ITKPointSet::PointsContainerConstPointer pointsContainer = pointSet->GetPoints();
139  ITKPointSet::PointsContainer::ConstIterator it;
140  ITKPointSet::PointType currentPoint;
141  ITKPointSet::PointsContainer::ElementIdentifier currentId;
142  for (it = pointsContainer->Begin(); it != pointsContainer->End(); ++it, ++counter)
143  {
144  currentPoint = it->Value();
145  currentId = it->Index();
146  (m_ANNDataPoints[counter])[0] = currentPoint[0];
147  (m_ANNDataPoints[counter])[1] = currentPoint[1];
148  (m_ANNDataPoints[counter])[2] = currentPoint[2];
149  m_IndexToPointIdContainer[counter] = currentId;
150  }
151  InitANN();
152 }
153 
155 {
156  m_ANNQueryPoint[0] = point[0];
157  m_ANNQueryPoint[1] = point[1];
158  m_ANNQueryPoint[2] = point[2];
160 }
161 
163 {
164  m_ANNQueryPoint[0] = x;
165  m_ANNQueryPoint[1] = y;
166  m_ANNQueryPoint[2] = z;
168 }
169 
171 {
172  m_ANNQueryPoint[0] = point[0];
173  m_ANNQueryPoint[1] = point[1];
174  m_ANNQueryPoint[2] = point[2];
176 }
177 
179 {
181  return -1;
182  m_ANNTree->annkSearch(point, m_ANNK, m_ANNPointIndexes, m_ANNDistances);
184 }
185 
187 {
188  m_ANNQueryPoint[0] = point[0];
189  m_ANNQueryPoint[1] = point[1];
190  m_ANNQueryPoint[2] = point[2];
192 }
193 
195 {
197  return -1;
198  m_ANNTree->annkSearch(point, m_ANNK, m_ANNPointIndexes, m_ANNDistances);
199  return m_ANNDistances[0];
200 }
201 
203 {
205  DestroyANN();
206 
207  m_ANNQueryPoint = annAllocPt(m_ANNDimension);
208  m_ANNPointIndexes = new ANNidx[m_ANNK];
209  m_ANNDistances = new ANNdist[m_ANNK];
211 
213 }
214 
216 {
217  m_SearchTreeInitialized = false;
218  if (m_ANNQueryPoint != nullptr)
219  annDeallocPt(m_ANNQueryPoint);
220  if (m_ANNDataPoints != nullptr)
221  annDeallocPts(m_ANNDataPoints);
222  if (m_ANNPointIndexes != nullptr)
223  delete[] m_ANNPointIndexes;
224  if (m_ANNDistances != nullptr)
225  delete[] m_ANNDistances;
226  if (m_ANNTree != nullptr)
227  delete m_ANNTree;
228 }
229 
231 {
232  m_ANNQueryPoint[0] = point[0];
233  m_ANNQueryPoint[1] = point[1];
234  m_ANNQueryPoint[2] = point[2];
235 
237 
239  *dist = m_ANNDistances[0];
240  return true;
241 }
unsigned int m_ANNDimension
MyANNidxArray m_ANNPointIndexes
MyANNpoint m_ANNQueryPoint
ANNkd_tree * m_ANNTree
mitk::PointSet * m_MitkPoints
DistanceType GetMinimalDistance(mitk::PointSet::PointType point)
ITKPointSet * m_ItkPoints
void SetPoints(vtkPointSet *points)
IdVectorType m_IndexToPointIdContainer
bool FindClosestPointAndDistance(mitk::PointSet::PointType point, IdType *id, DistanceType *dist)
virtual int GetSize(unsigned int t=0) const
returns the current size of the point-list
Data structure which stores a set of points. Superclass of mitk::Mesh.
Definition: mitkPointSet.h:75
MyANNpointArray m_ANNDataPoints
virtual DataType::Pointer GetPointSet(int t=0) const
returns the pointset
unsigned long GetMTime() const override
Get the modified time of the last change of the contents this data object or its geometry.
MyANNdistArray m_ANNDistances
DataType::PointsContainer PointsContainer
Definition: mitkPointSet.h:134
itk::PointSet< PixelType, 3, MeshTraits > ITKPointSet
IdType FindClosestANNPoint(const MyANNpoint &point)
IdType FindClosestPoint(const double point[3])