Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkSurfaceBasedInterpolationController.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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
18 
19 #include "mitkColorProperty.h"
22 #include "mitkIOUtil.h"
23 #include "mitkImageAccessByItk.h"
24 #include "mitkImageCast.h"
26 #include "mitkInteractionConst.h"
27 #include "mitkMemoryUtilities.h"
28 #include "mitkProperties.h"
30 
32 #include "vtkAppendPolyData.h"
33 #include "vtkPolyData.h"
34 #include "vtkProperty.h"
35 #include "vtkSmartPointer.h"
36 
37 #include <itkCommand.h>
38 
40  : m_MinSpacing(1.0), m_MaxSpacing(1.0), m_WorkingImage(NULL), m_ActiveLabel(0)
41 {
42  this->Initialize();
43 }
44 
46 {
47  ContourListMap::iterator it = m_MapOfContourLists.begin();
48  for (; it != m_MapOfContourLists.end(); it++)
49  {
50  for (unsigned int j = 0; j < m_MapOfContourLists[(*it).first].size(); ++j)
51  {
52  delete (m_MapOfContourLists[(*it).first].at(j).second);
53  }
54  m_MapOfContourLists.erase(it);
55  }
56 }
57 
59 {
60  m_InterpolateSurfaceFilter = CreateDistanceImageFromSurfaceFilter::New();
61  m_InterpolateSurfaceFilter->SetUseProgressBar(false);
62 
63  m_Contours = Surface::New();
64 
65  m_InterpolationResult = NULL;
66 }
67 
69 {
71 
72  if (m_Instance == 0)
73  {
74  m_Instance = new SurfaceBasedInterpolationController();
75  }
76 
77  return m_Instance;
78 }
79 
82 {
83  if (m_ActiveLabel == 0)
84  return;
85 
87  transform = op->GetTransform();
88 
89  mitk::Vector3D direction = op->GetDirectionVector();
90  int pos(-1);
91 
92  for (unsigned int i = 0; i < m_MapOfContourLists[m_ActiveLabel].size(); i++)
93  {
94  itk::Matrix<ScalarType> diffM =
95  transform->GetMatrix() - m_MapOfContourLists[m_ActiveLabel].at(i).second->GetTransform()->GetMatrix();
96  bool isSameMatrix(true);
97  for (unsigned int j = 0; j < 3; j++)
98  {
99  if (fabs(diffM[j][0]) > 0.0001 && fabs(diffM[j][1]) > 0.0001 && fabs(diffM[j][2]) > 0.0001)
100  {
101  isSameMatrix = false;
102  break;
103  }
104  }
105  itk::Vector<float> diffV =
106  m_MapOfContourLists[m_ActiveLabel].at(i).second->GetTransform()->GetOffset() - transform->GetOffset();
107  if (isSameMatrix && m_MapOfContourLists[m_ActiveLabel].at(i).second->GetPos() == op->GetPos() &&
108  (fabs(diffV[0]) < 0.0001 && fabs(diffV[1]) < 0.0001 && fabs(diffV[2]) < 0.0001))
109  {
110  pos = i;
111  break;
112  }
113  }
114 
115  if (pos == -1 && newContour->GetNumberOfVertices() > 0) // add a new contour
116  {
118  OpRESTOREPLANEPOSITION, op->GetWidth(), op->GetHeight(), op->GetSpacing(), op->GetPos(), direction, transform);
119  ContourPositionPair newData = std::make_pair(newContour, newOp);
120  m_MapOfContourLists[m_ActiveLabel].push_back(newData);
121  }
122  else if (pos != -1) // replace existing contour
123  {
124  m_MapOfContourLists[m_ActiveLabel].at(pos).first = newContour;
125  }
126 
127  this->Modified();
128 }
129 
131 {
132  if (m_MapOfContourLists[m_ActiveLabel].size() < 2)
133  {
134  // If no interpolation is possible reset the interpolation result
135  m_InterpolationResult = NULL;
136  return;
137  }
138 
139  m_InterpolateSurfaceFilter->Reset();
140 
141  // MLI TODO
142  // m_InterpolateSurfaceFilter->SetReferenceImage( m_WorkingImage );
143 
144  // CreateDistanceImageFromSurfaceFilter::Pointer interpolateSurfaceFilter =
145  // CreateDistanceImageFromSurfaceFilter::New();
146  vtkSmartPointer<vtkAppendPolyData> polyDataAppender = vtkSmartPointer<vtkAppendPolyData>::New();
147 
148  for (unsigned int i = 0; i < m_MapOfContourLists[m_ActiveLabel].size(); i++)
149  {
151  converter->SetInput(m_MapOfContourLists[m_ActiveLabel].at(i).first);
152  converter->Update();
153 
154  mitk::Surface::Pointer surface = converter->GetOutput();
155  surface->DisconnectPipeline();
156 
158  reduceFilter->SetUseProgressBar(false);
159  reduceFilter->SetInput(surface);
160  reduceFilter->SetMinSpacing(m_MinSpacing);
161  reduceFilter->SetMaxSpacing(m_MaxSpacing);
162  reduceFilter->Update();
163 
165  normalsFilter->SetUseProgressBar(false);
166  normalsFilter->SetInput(reduceFilter->GetOutput());
167  normalsFilter->SetMaxSpacing(m_MaxSpacing);
168  // MLI TODO
169  // normalsFilter->SetSegmentationBinaryImage(m_WorkingImage, m_ActiveLabel);
170  // normalsFilter->Update();
171 
172  m_InterpolateSurfaceFilter->SetInput(i, normalsFilter->GetOutput());
173 
174  polyDataAppender->AddInputData(reduceFilter->GetOutput()->GetVtkPolyData());
175  }
176 
177  polyDataAppender->Update();
178  m_Contours->SetVtkPolyData(polyDataAppender->GetOutput());
179 
180  // update the filter and get the resulting distance-image
181  m_InterpolateSurfaceFilter->Update();
182  Image::Pointer distanceImage = m_InterpolateSurfaceFilter->GetOutput();
183  if (distanceImage.IsNull())
184  {
185  // If no interpolation is possible reset the interpolation result
186  m_InterpolationResult = NULL;
187  return;
188  }
189 
190  // create a surface from the distance-image
192  imageToSurfaceFilter->SetInput(distanceImage);
193  imageToSurfaceFilter->SetThreshold(0);
194  imageToSurfaceFilter->SetSmooth(true);
195  imageToSurfaceFilter->SetSmoothIteration(20);
196  imageToSurfaceFilter->Update();
197  m_InterpolationResult = imageToSurfaceFilter->GetOutput();
198 
199  m_InterpolationResult->DisconnectPipeline();
200 }
201 
203 {
204  return m_InterpolationResult;
205 }
206 
208 {
209  return m_Contours;
210 }
211 
213 {
214  m_MinSpacing = minSpacing;
215 }
216 
218 {
219  m_MaxSpacing = maxSpacing;
220 }
221 
223 {
224  m_DistanceImageVolume = value;
225 }
226 
228 {
229  m_WorkingImage = workingImage;
230 }
231 
233 {
234  return m_InterpolateSurfaceFilter->GetOutput();
235 }
236 
238 {
239  double numberOfPoints = 0.0;
240  for (unsigned int i = 0; i < m_MapOfContourLists[m_ActiveLabel].size(); i++)
241  {
242  numberOfPoints += m_MapOfContourLists[m_ActiveLabel].at(i).first->GetNumberOfVertices() * 3;
243  }
244 
245  double sizeOfPoints = pow(numberOfPoints, 2) * sizeof(double);
247  double percentage = sizeOfPoints / totalMem;
248  return percentage;
249 }
250 
252 {
253  if (m_ActiveLabel == activeLabel)
254  return;
255 
256  if (activeLabel == 0)
257  return;
258 
259  m_ActiveLabel = activeLabel;
260 
261  ContourListMap::iterator it = m_MapOfContourLists.find(m_ActiveLabel);
262 
263  if (it == m_MapOfContourLists.end())
264  {
265  ContourPositionPairList newList;
266  m_MapOfContourLists.insert(std::pair<unsigned int, ContourPositionPairList>(m_ActiveLabel, newList));
267  m_InterpolationResult = NULL;
268  }
269 
270  this->Modified();
271 }
272 
273 /*
274 void mitk::SurfaceBasedInterpolationController::RemoveSegmentationFromContourList(mitk::Image *segmentation)
275 {
276  if (segmentation != 0)
277  {
278  m_MapOfContourLists.erase(segmentation);
279  if (m_SelectedSegmentation == segmentation)
280  {
281  SetSegmentationImage(NULL);
282  m_SelectedSegmentation = 0;
283  }
284  }
285 }
286 */
287 
288 /*
289 void mitk::SurfaceBasedInterpolationController::OnSegmentationDeleted(const itk::Object *caller, const itk::EventObject
290 &event)
291 {
292  mitk::Image* tempImage = dynamic_cast<mitk::Image*>(const_cast<itk::Object*>(caller));
293  if (tempImage)
294  {
295  RemoveSegmentationFromContourList(tempImage);
296  if (tempImage == m_SelectedSegmentation)
297  {
298  SetSegmentationImage(NULL);
299  m_SelectedSegmentation = 0;
300  }
301  }
302 }
303 */
static vcl_size_t GetTotalSizeOfPhysicalRam()
Class for storing surfaces (vtkPolyData).
Definition: mitkSurface.h:32
itk::SmartPointer< Self > Pointer
static SurfaceBasedInterpolationController * GetInstance()
Constants for most interaction classes, due to the generic StateMachines.
void AddNewContour(ContourModel::Pointer newContour, RestorePlanePositionOperation *op)
Image class for storing images.
Definition: mitkImage.h:76
static Pointer New()
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.