Medical Imaging Interaction Toolkit  2018.4.99-389bf124
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 (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 
14 
15 #include <vtkPoints.h>
16 #include <vtkPolyVertex.h>
17 #include <vtkSmartPointer.h>
18 #include <vtkUnstructuredGrid.h>
19 
20 #include <itkImageRegionIterator.h>
21 
22 #include <mitkImageAccessByItk.h>
23 
24 mitk::ImageToUnstructuredGridFilter::ImageToUnstructuredGridFilter() : m_NumberOfExtractedPoints(0), m_Threshold(-0.1)
25 {
26  this->m_UnstructGrid = mitk::UnstructuredGrid::New();
27 }
28 
30 {
31 }
32 
34 {
35  mitk::UnstructuredGrid::Pointer unstructGrid = this->GetOutput();
36  const mitk::Image *image = this->GetInput();
37 
38  if (image == nullptr || !image->IsInitialized())
39  {
40  MITK_ERROR << "Wrong input image set" << std::endl;
41  return;
42  }
43 
44  m_Geometry = image->GetGeometry();
45 
47 
49 }
50 
52 {
53  this->ProcessObject::SetNthInput(0, const_cast<mitk::Image *>(image));
54 }
55 
57 {
58  if (this->GetNumberOfInputs() < 1)
59  {
60  MITK_ERROR << "No input set" << std::endl;
61  return nullptr;
62  }
63 
64  return static_cast<const mitk::Image *>(this->ProcessObject::GetInput(0));
65 }
66 
68 {
69  if (this->GetNumberOfInputs() < 1)
70  {
71  MITK_ERROR << "No input set" << std::endl;
72  return nullptr;
73  }
74 
75  return static_cast<mitk::Image *>(this->ProcessObject::GetInput(0));
76 }
77 
78 template <typename TPixel, unsigned int VImageDimension>
79 void mitk::ImageToUnstructuredGridFilter::ExtractPoints(const itk::Image<TPixel, VImageDimension> *image)
80 {
81  typedef itk::Image<TPixel, VImageDimension> InputImageType;
82  typename itk::ImageRegionConstIterator<InputImageType> it(image, image->GetRequestedRegion());
83 
84  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
85 
86  it.GoToBegin();
87  while (!it.IsAtEnd())
88  {
89  if (it.Get() >= m_Threshold)
90  {
91  mitk::Point3D imagePoint;
92  mitk::Point3D worldPoint;
93 
94  imagePoint[0] = it.GetIndex()[0];
95  imagePoint[1] = it.GetIndex()[1];
96  imagePoint[2] = it.GetIndex()[2];
97 
98  m_Geometry->IndexToWorld(imagePoint, worldPoint);
99 
100  points->InsertNextPoint(worldPoint[0], worldPoint[1], worldPoint[2]);
102  }
103  ++it;
104  }
105 
106  vtkSmartPointer<vtkPolyVertex> verts = vtkSmartPointer<vtkPolyVertex>::New();
107 
108  verts->GetPointIds()->SetNumberOfIds(m_NumberOfExtractedPoints);
109  for (int i = 0; i < m_NumberOfExtractedPoints; i++)
110  {
111  verts->GetPointIds()->SetId(i, i);
112  }
113 
114  vtkSmartPointer<vtkUnstructuredGrid> uGrid = vtkSmartPointer<vtkUnstructuredGrid>::New();
115  uGrid->Allocate(1);
116 
117  uGrid->InsertNextCell(verts->GetCellType(), verts->GetPointIds());
118  uGrid->SetPoints(points);
119 
120  m_UnstructGrid->SetVtkUnstructuredGrid(uGrid);
121 }
122 
124 {
125  this->m_Threshold = threshold;
126 }
127 
129 {
130  return this->m_Threshold;
131 }
132 
134 {
135  mitk::Image::ConstPointer inputImage = this->GetInput();
136 
137  m_UnstructGrid = this->GetOutput();
138 
139  itkDebugMacro(<< "GenerateOutputInformation()");
140 
141  if (inputImage.IsNull())
142  return;
143 }
void IndexToWorld(const mitk::Vector3D &vec_units, mitk::Vector3D &vec_mm) const
Convert (continuous or discrete) index coordinates of a vector vec_units to world coordinates (in mm)...
#define MITK_ERROR
Definition: mitkLogMacros.h:20
void ExtractPoints(const itk::Image< TPixel, VImageDimension > *image)
OutputType * GetOutput()
virtual void SetInput(const mitk::Image *image)
static Pointer New()
Image class for storing images.
Definition: mitkImage.h:72
#define AccessByItk(mitkImage, itkImageTypeFunction)
Access a MITK image by an ITK image.
mitk::Image::Pointer 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:138
itk::SmartPointer< Self > Pointer
Definition: mitkBaseData.h:41