Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkImageToUnstructuredGridFilter.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 <vtkPoints.h>
20 #include <vtkPolyVertex.h>
21 #include <vtkSmartPointer.h>
22 #include <vtkUnstructuredGrid.h>
23 
24 #include <itkImageRegionIterator.h>
25 
26 #include <mitkImageAccessByItk.h>
27 
28 mitk::ImageToUnstructuredGridFilter::ImageToUnstructuredGridFilter() : m_NumberOfExtractedPoints(0), m_Threshold(-0.1)
29 {
30  this->m_UnstructGrid = mitk::UnstructuredGrid::New();
31 }
32 
34 {
35 }
36 
38 {
39  mitk::UnstructuredGrid::Pointer unstructGrid = this->GetOutput();
40  const mitk::Image *image = this->GetInput();
41 
42  if (image == nullptr || !image->IsInitialized())
43  {
44  MITK_ERROR << "Wrong input image set" << std::endl;
45  return;
46  }
47 
48  m_Geometry = image->GetGeometry();
49 
50  m_NumberOfExtractedPoints = 0;
51 
52  AccessByItk(image, ExtractPoints)
53 }
54 
56 {
57  this->ProcessObject::SetNthInput(0, const_cast<mitk::Image *>(image));
58 }
59 
61 {
62  if (this->GetNumberOfInputs() < 1)
63  {
64  MITK_ERROR << "No input set" << std::endl;
65  return nullptr;
66  }
67 
68  return static_cast<const mitk::Image *>(this->ProcessObject::GetInput(0));
69 }
70 
72 {
73  if (this->GetNumberOfInputs() < 1)
74  {
75  MITK_ERROR << "No input set" << std::endl;
76  return nullptr;
77  }
78 
79  return static_cast<mitk::Image *>(this->ProcessObject::GetInput(0));
80 }
81 
82 template <typename TPixel, unsigned int VImageDimension>
83 void mitk::ImageToUnstructuredGridFilter::ExtractPoints(const itk::Image<TPixel, VImageDimension> *image)
84 {
85  typedef itk::Image<TPixel, VImageDimension> InputImageType;
86  typename itk::ImageRegionConstIterator<InputImageType> it(image, image->GetRequestedRegion());
87 
88  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
89 
90  it.GoToBegin();
91  while (!it.IsAtEnd())
92  {
93  if (it.Get() >= m_Threshold)
94  {
95  mitk::Point3D imagePoint;
96  mitk::Point3D worldPoint;
97 
98  imagePoint[0] = it.GetIndex()[0];
99  imagePoint[1] = it.GetIndex()[1];
100  imagePoint[2] = it.GetIndex()[2];
101 
102  m_Geometry->IndexToWorld(imagePoint, worldPoint);
103 
104  points->InsertNextPoint(worldPoint[0], worldPoint[1], worldPoint[2]);
105  m_NumberOfExtractedPoints++;
106  }
107  ++it;
108  }
109 
110  vtkSmartPointer<vtkPolyVertex> verts = vtkSmartPointer<vtkPolyVertex>::New();
111 
112  verts->GetPointIds()->SetNumberOfIds(m_NumberOfExtractedPoints);
113  for (int i = 0; i < m_NumberOfExtractedPoints; i++)
114  {
115  verts->GetPointIds()->SetId(i, i);
116  }
117 
118  vtkSmartPointer<vtkUnstructuredGrid> uGrid = vtkSmartPointer<vtkUnstructuredGrid>::New();
119  uGrid->Allocate(1);
120 
121  uGrid->InsertNextCell(verts->GetCellType(), verts->GetPointIds());
122  uGrid->SetPoints(points);
123 
124  m_UnstructGrid->SetVtkUnstructuredGrid(uGrid);
125 }
126 
128 {
129  this->m_Threshold = threshold;
130 }
131 
133 {
134  return this->m_Threshold;
135 }
136 
138 {
139  mitk::Image::ConstPointer inputImage = this->GetInput();
140 
141  m_UnstructGrid = this->GetOutput();
142 
143  itkDebugMacro(<< "GenerateOutputInformation()");
144 
145  if (inputImage.IsNull())
146  return;
147 }
#define MITK_ERROR
Definition: mitkLogMacros.h:24
void ExtractPoints(const itk::Image< TPixel, VImageDimension > *image)
virtual void SetInput(const mitk::Image *image)
static Pointer New()
itk::Image< double, 3 > InputImageType
Image class for storing images.
Definition: mitkImage.h:76
#define AccessByItk(mitkImage, itkImageTypeFunction)
Access a MITK image by an ITK image.
virtual bool IsInitialized() const
Check whether the data has been initialized, i.e., at least the Geometry and other header data has be...
mitk::BaseGeometry * GetGeometry(int t=0) const
Return the geometry, which is a TimeGeometry, of the data as non-const pointer.
Definition: mitkBaseData.h:129
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.