Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
itkTractsToFiberEndingsImageFilter.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 ===================================================================*/
17 
18 // VTK
19 #include <vtkPolyLine.h>
20 #include <vtkCellArray.h>
21 #include <vtkCellData.h>
22 #include <boost/progress.hpp>
23 
24 namespace itk{
25 
26  template< class OutputImageType >
28  : m_InvertImage(false)
29  , m_UpsamplingFactor(1)
30  , m_InputImage(NULL)
31  , m_UseImageGeometry(false)
32  , m_BinaryOutput(false)
33  {
34 
35  }
36 
37  template< class OutputImageType >
39  {
40  }
41 
42  template< class OutputImageType >
44  {
45  itk::Point<float, 3> itkPoint;
46  itkPoint[0] = point[0];
47  itkPoint[1] = point[1];
48  itkPoint[2] = point[2];
49  return itkPoint;
50  }
51 
52  template< class OutputImageType >
54  {
55  // generate upsampled image
56  mitk::BaseGeometry::Pointer geometry = m_FiberBundle->GetGeometry();
57  typename OutputImageType::Pointer outImage = this->GetOutput();
58 
59  // calculate new image parameters
60  itk::Vector<double, 3> newSpacing;
61  mitk::Point3D newOrigin;
62  itk::Matrix<double, 3, 3> newDirection;
63  ImageRegion<3> upsampledRegion;
64  if (m_UseImageGeometry && !m_InputImage.IsNull())
65  {
66  newSpacing = m_InputImage->GetSpacing()/m_UpsamplingFactor;
67  upsampledRegion = m_InputImage->GetLargestPossibleRegion();
68  newOrigin = m_InputImage->GetOrigin();
69  typename OutputImageType::RegionType::SizeType size = upsampledRegion.GetSize();
70  size[0] *= m_UpsamplingFactor;
71  size[1] *= m_UpsamplingFactor;
72  size[2] *= m_UpsamplingFactor;
73  upsampledRegion.SetSize(size);
74  newDirection = m_InputImage->GetDirection();
75  }
76  else
77  {
78  newSpacing = geometry->GetSpacing()/m_UpsamplingFactor;
79  newOrigin = geometry->GetOrigin();
80  mitk::Geometry3D::BoundsArrayType bounds = geometry->GetBounds();
81  newOrigin[0] += bounds.GetElement(0);
82  newOrigin[1] += bounds.GetElement(2);
83  newOrigin[2] += bounds.GetElement(4);
84 
85  for (int i=0; i<3; i++)
86  for (int j=0; j<3; j++)
87  newDirection[j][i] = geometry->GetMatrixColumn(i)[j];
88  upsampledRegion.SetSize(0, geometry->GetExtent(0)*m_UpsamplingFactor);
89  upsampledRegion.SetSize(1, geometry->GetExtent(1)*m_UpsamplingFactor);
90  upsampledRegion.SetSize(2, geometry->GetExtent(2)*m_UpsamplingFactor);
91  }
92  typename OutputImageType::RegionType::SizeType upsampledSize = upsampledRegion.GetSize();
93 
94  // apply new image parameters
95  outImage->SetSpacing( newSpacing );
96  outImage->SetOrigin( newOrigin );
97  outImage->SetDirection( newDirection );
98  outImage->SetRegions( upsampledRegion );
99  outImage->Allocate();
100 
101  int w = upsampledSize[0];
102  int h = upsampledSize[1];
103  int d = upsampledSize[2];
104 
105  // set/initialize output
106  OutPixelType* outImageBufferPointer = (OutPixelType*)outImage->GetBufferPointer();
107  for (int i=0; i<w*h*d; i++)
108  outImageBufferPointer[i] = 0;
109 
110  // resample fiber bundle
111  float minSpacing = 1;
112  if(newSpacing[0]<newSpacing[1] && newSpacing[0]<newSpacing[2])
113  minSpacing = newSpacing[0];
114  else if (newSpacing[1] < newSpacing[2])
115  minSpacing = newSpacing[1];
116  else
117  minSpacing = newSpacing[2];
118 
119  vtkSmartPointer<vtkPolyData> fiberPolyData = m_FiberBundle->GetFiberPolyData();
120  vtkSmartPointer<vtkCellArray> vLines = fiberPolyData->GetLines();
121  vLines->InitTraversal();
122 
123  int numFibers = m_FiberBundle->GetNumFibers();
124  boost::progress_display disp(numFibers);
125  for( int i=0; i<numFibers; i++ )
126  {
127  ++disp;
128  vtkIdType numPoints(0);
129  vtkIdType* points(NULL);
130  vLines->GetNextCell ( numPoints, points );
131 
132  // fill output image
133  if (numPoints>0)
134  {
135  itk::Point<float, 3> vertex = GetItkPoint(fiberPolyData->GetPoint(points[0]));
136  itk::Index<3> index;
137  outImage->TransformPhysicalPointToIndex(vertex, index);
138  if (m_BinaryOutput)
139  outImage->SetPixel(index, 1);
140  else
141  outImage->SetPixel(index, outImage->GetPixel(index)+1);
142  }
143 
144  if (numPoints>2)
145  {
146  itk::Point<float, 3> vertex = GetItkPoint(fiberPolyData->GetPoint(points[numPoints-1]));
147  itk::Index<3> index;
148  outImage->TransformPhysicalPointToIndex(vertex, index);
149  if (m_BinaryOutput)
150  outImage->SetPixel(index, 1);
151  else
152  outImage->SetPixel(index, outImage->GetPixel(index)+1);
153  }
154  }
155 
156  if (m_InvertImage)
157  for (int i=0; i<w*h*d; i++)
158  outImageBufferPointer[i] = 1-outImageBufferPointer[i];
159  }
160 }
itk::SmartPointer< Self > Pointer
BoundingBoxType::BoundsArrayType BoundsArrayType
itk::Point< float, 3 > GetItkPoint(double point[3])