17 #ifndef __itkEvaluateTractogramDirectionsFilter_cpp
18 #define __itkEvaluateTractogramDirectionsFilter_cpp
21 #include <itkImageRegionConstIterator.h>
22 #include <itkImageRegionIterator.h>
23 #include <itkImageDuplicator.h>
24 #include <boost/progress.hpp>
26 #define _USE_MATH_DEFINES
31 template<
class PixelType >
34 m_ReferenceImageSet(NULL),
35 m_IgnoreMissingDirections(false),
37 m_UseInterpolation(false)
39 this->SetNumberOfOutputs(1);
42 template<
class PixelType >
45 itk::Vector<PixelType, 3> itkVector;
46 itkVector[0] = point[0];
47 itkVector[1] = point[1];
48 itkVector[2] = point[2];
52 template<
class PixelType >
55 vnl_vector_fixed<PixelType, 3> vnlVector;
56 vnlVector[0] = point[0];
57 vnlVector[1] = point[1];
58 vnlVector[2] = point[2];
62 template<
class PixelType >
65 vnl_vector_fixed<PixelType, 3> vnlVector;
66 vnlVector[0] = vector[0];
67 vnlVector[1] = vector[1];
68 vnlVector[2] = vector[2];
72 template<
class PixelType >
75 itk::Point<PixelType, 3> itkPoint;
76 itkPoint[0] = point[0];
77 itkPoint[1] = point[1];
78 itkPoint[2] = point[2];
82 template<
class PixelType >
85 if (m_Tractogram.IsNull() || m_ReferenceImageSet.IsNull())
88 if (m_UseInterpolation)
89 MITK_INFO <<
"Using trilinear interpolation";
91 MITK_INFO <<
"Using nearest neighbor interpolation";
93 if (m_IgnoreMissingDirections)
94 MITK_INFO <<
"Ignoring missing directions";
96 MITK_INFO <<
"Penalizing missing directions";
100 outputImage->SetOrigin( m_ReferenceImageSet->GetElement(0)->GetOrigin() );
101 outputImage->SetRegions( m_ReferenceImageSet->GetElement(0)->GetLargestPossibleRegion() );
102 outputImage->SetSpacing( m_ReferenceImageSet->GetElement(0)->GetSpacing() );
103 outputImage->SetDirection( m_ReferenceImageSet->GetElement(0)->GetDirection() );
104 outputImage->Allocate();
105 outputImage->FillBuffer(0.0);
108 counterImage->SetOrigin( m_ReferenceImageSet->GetElement(0)->GetOrigin() );
109 counterImage->SetRegions( m_ReferenceImageSet->GetElement(0)->GetLargestPossibleRegion() );
110 counterImage->SetSpacing( m_ReferenceImageSet->GetElement(0)->GetSpacing() );
111 counterImage->SetDirection( m_ReferenceImageSet->GetElement(0)->GetDirection() );
112 counterImage->Allocate();
113 counterImage->FillBuffer(0.0);
115 std::vector< DoubleImageType::Pointer > directionFound;
116 for (
int i=0; i<m_ReferenceImageSet->Size(); i++)
119 check->SetOrigin( m_ReferenceImageSet->GetElement(0)->GetOrigin() );
120 check->SetRegions( m_ReferenceImageSet->GetElement(0)->GetLargestPossibleRegion() );
121 check->SetSpacing( m_ReferenceImageSet->GetElement(0)->GetSpacing() );
122 check->SetDirection( m_ReferenceImageSet->GetElement(0)->GetDirection() );
124 check->FillBuffer(90);
125 directionFound.push_back(check);
128 if (m_MaskImage.IsNull())
131 m_MaskImage->SetOrigin( outputImage->GetOrigin() );
132 m_MaskImage->SetRegions( outputImage->GetLargestPossibleRegion() );
133 m_MaskImage->SetSpacing( outputImage->GetSpacing() );
134 m_MaskImage->SetDirection( outputImage->GetDirection() );
135 m_MaskImage->Allocate();
136 m_MaskImage->FillBuffer(1);
139 m_MeanAngularError = 0.0;
140 m_MedianAngularError = 0;
141 m_MaxAngularError = 0.0;
143 m_VarAngularError = 0.0;
144 m_AngularErrorVector.clear();
146 float minSpacing = 1;
147 if(outputImage->GetSpacing()[0]<outputImage->GetSpacing()[1] && outputImage->GetSpacing()[0]<outputImage->GetSpacing()[2])
148 minSpacing = outputImage->GetSpacing()[0];
149 else if (outputImage->GetSpacing()[1] < outputImage->GetSpacing()[2])
150 minSpacing = outputImage->GetSpacing()[1];
152 minSpacing = outputImage->GetSpacing()[2];
154 fiberBundle->ResampleFibers(minSpacing/10);
157 vtkSmartPointer<vtkPolyData> fiberPolyData = fiberBundle->GetFiberPolyData();
158 boost::progress_display disp( fiberBundle->GetNumFibers() );
159 for(
int i=0; i<fiberBundle->GetNumFibers(); i++ )
161 vtkCell* cell = fiberPolyData->GetCell(i);
162 int numPoints = cell->GetNumberOfPoints();
163 vtkPoints* points = cell->GetPoints();
168 for(
int j=0; j<numPoints; j++)
170 double* temp = points->GetPoint(j);
171 itk::Point<PixelType, 3> vertex = GetItkPoint(temp);
172 itk::Vector<PixelType> v = GetItkVector(temp);
174 itk::Vector<PixelType, 3> dir(3);
176 dir = GetItkVector(points->GetPoint(j+1))-v;
178 dir = v-GetItkVector(points->GetPoint(j-1));
180 vnl_vector_fixed< PixelType, 3 > fiberDir = GetVnlVector(dir);
181 fiberDir.normalize();
184 itk::ContinuousIndex<float, 3> contIndex;
185 m_MaskImage->TransformPhysicalPointToIndex(vertex, idx);
186 m_MaskImage->TransformPhysicalPointToContinuousIndex(vertex, contIndex);
188 if (!m_UseInterpolation)
190 if (!m_MaskImage->GetLargestPossibleRegion().IsInside(idx) || m_MaskImage->GetPixel(idx)<=0)
195 for (
int k=0; k<m_ReferenceImageSet->Size(); k++)
197 vnl_vector_fixed< PixelType, 3 > refDir = m_ReferenceImageSet->GetElement(k)->GetPixel(idx).GetVnlVector();
198 if (refDir.magnitude()>m_Eps)
204 double tempAngle = acos(fabs(dot_product(refDir, fiberDir)))*180.0/
M_PI;
205 directionFound.at(k)->SetPixel(idx, tempAngle);
207 if (tempAngle < angle)
214 directionFound.at(usedIndex)->SetPixel(idx, -1);
215 else if (m_IgnoreMissingDirections)
218 counterImage->SetPixel(idx, counterImage->GetPixel(idx)+1);
219 outputImage->SetPixel(idx, outputImage->GetPixel(idx)+angle);
223 double frac_x = contIndex[0] - idx[0];
224 double frac_y = contIndex[1] - idx[1];
225 double frac_z = contIndex[2] - idx[2];
244 for (
int x=0; x<2; x++)
247 for (
int y=0; y<2; y++)
250 for (
int z=0; z<2; z++)
254 newIdx[0] = idx[0]+x;
255 newIdx[1] = idx[1]+y;
256 newIdx[2] = idx[2]+z;
258 double frac = frac_x*frac_y*frac_z;
261 if (!m_MaskImage->GetLargestPossibleRegion().IsInside(newIdx) || m_MaskImage->GetPixel(newIdx)<=0)
266 for (
int k=0; k<m_ReferenceImageSet->Size(); k++)
268 vnl_vector_fixed< PixelType, 3 > refDir = m_ReferenceImageSet->GetElement(k)->GetPixel(newIdx).GetVnlVector();
269 if (refDir.magnitude()>m_Eps)
275 double tempAngle = acos(fabs(dot_product(refDir, fiberDir)))*180.0/
M_PI;
276 directionFound.at(k)->SetPixel(newIdx, tempAngle);
278 if (tempAngle < angle)
285 directionFound.at(usedIndex)->SetPixel(newIdx, -1);
286 else if (m_IgnoreMissingDirections)
289 counterImage->SetPixel(newIdx, counterImage->GetPixel(newIdx)+1);
290 outputImage->SetPixel(newIdx, outputImage->GetPixel(newIdx)+frac*angle);
298 ImageRegionIterator< OutputImageType > oit(outputImage, outputImage->GetLargestPossibleRegion());
299 ImageRegionIterator< UCharImageType > mit(m_MaskImage, m_MaskImage->GetLargestPossibleRegion());
300 ImageRegionIterator< DoubleImageType > cit(counterImage, counterImage->GetLargestPossibleRegion());
303 if (!m_IgnoreMissingDirections)
304 MITK_INFO <<
"Creatings statistics and accounting for missing directions";
307 boost::progress_display disp2(outputImage->GetLargestPossibleRegion().GetNumberOfPixels());
308 while( !oit.IsAtEnd() )
312 if ( cit.Get()>m_Eps )
313 oit.Set(oit.Get()/cit.Get());
315 if (!m_IgnoreMissingDirections)
317 int missingCount = 0;
319 if ( cit.Get()>m_Eps )
322 for (
int i=0; i<directionFound.size(); i++)
324 if ( directionFound.at(i)->GetPixel(oit.GetIndex())>0 && m_ReferenceImageSet->GetElement(i)->GetPixel(oit.GetIndex()).GetVnlVector().magnitude()>m_Eps )
326 oit.Set(oit.Get()+directionFound.at(i)->GetPixel(oit.GetIndex()));
331 oit.Set(oit.Get()/missingCount);
334 if (oit.Get()>m_MaxAngularError)
335 m_MaxAngularError = oit.Get();
336 if (oit.Get()<m_MinAngularError)
337 m_MinAngularError = oit.Get();
346 this->SetNthOutput(0, outputImage);
351 #endif // __itkEvaluateTractogramDirectionsFilter_cpp
itk::SmartPointer< Self > Pointer
itk::Vector< PixelType, 3 > GetItkVector(double point[3])
vnl_vector_fixed< PixelType, 3 > GetVnlVector(double point[3])
EvaluateTractogramDirectionsFilter()
itk::Point< PixelType, 3 > GetItkPoint(double point[3])
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.