DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
itkMeshFromPeaksImageFilter.hxx
Go to the documentation of this file.
1 
12 #ifndef __itkMeshFromPeaksImageFilter_hxx
13 #define __itkMeshFromPeaksImageFilter_hxx
14 
16 #include <vtkTubeFilter.h>
17 
18 namespace itk
19 {
20 template <class TInputImage, class TOutputMesh>
23 {
24  m_PeakType = XYZV;
25  m_TubeRadius = 0.1*this->m_Scale;
26  m_MaxNumberOfPeaks = -1;
27 
28  this->m_ColorScheme = Superclass::FIXED;
29  m_ColorPeak[0]=255, m_ColorPeak[1]=0, m_ColorPeak[2]=0;
30 }
31 
32 template <class TInputImage, class TOutputMesh>
33 typename LightObject::Pointer
36 {
37  typename LightObject::Pointer loPtr = Superclass::InternalClone();
38 
39  typename Self::Pointer rval = dynamic_cast<Self *>(loPtr.GetPointer());
40  if(rval.IsNull())
41  {
42  itkExceptionMacro(<< "downcast to type " << this->GetNameOfClass()<< " failed.");
43  }
44 
45  rval->m_PeakType = m_PeakType;
46  rval->m_TubeRadius = m_TubeRadius;
47  rval->m_MaxNumberOfPeaks = m_MaxNumberOfPeaks;
48 
49  for ( int i = 0; i < 3; ++i )
50  rval->m_ColorPeak[i] = m_ColorPeak[i];
51 
52  return loPtr;
53 }
54 
55 
56 template <class TInputImage, class TOutputMesh>
59 {
60  VerifyInputParameters();
61 
62  // Pointers
63  InputImageConstPointer inputPtr = this->GetInput();
64 
65  // Input Image Parameters
66  InputImageRegionType inputRegion = inputPtr->GetRequestedRegion();
67  InputImageIndexType inputIndex;
68  InputImagePointType inputPhysicalPoint;
69  InputImagePixelType inputPixel;
70 
71  // iterator for the input image
72  ImageRegionConstIterator<InputImageType> inputIt(inputPtr, inputPtr->GetRequestedRegion());
73 
74  vtkSmartPointer<OutputMeshPointsType> outputMeshPoints = vtkSmartPointer<OutputMeshPointsType>::New();
75  vtkSmartPointer<OutputMeshCellArrayType> outputMeshCellArray = vtkSmartPointer<OutputMeshCellArrayType>::New();
76  vtkSmartPointer<OutputMeshRGBType> outputMeshRGB = vtkSmartPointer<OutputMeshRGBType>::New();
77  outputMeshRGB->SetNumberOfComponents(3);
78  outputMeshRGB->SetName("RGB_scalars");
79 
80  double outputMeshPoint1[3];
81  double outputMeshPoint2[3];
82  unsigned int numberOfCellPoints = 2;
83 
84  unsigned long offset=0;
85  unsigned char RGB[3];
86  unsigned int numberOfComponentsPerOrientation = PeakContainerHelper::GetDimensionPerPeak(m_PeakType);
87  int numberOfPeaks = PeakContainerHelper::GetNumberOfPeaks(m_PeakType, inputPtr->GetNumberOfComponentsPerPixel());
88  int realNumberOfPeaks = m_MaxNumberOfPeaks>0 ? utl::min(numberOfPeaks, m_MaxNumberOfPeaks) : numberOfPeaks;
89  double norm = 0;
90 
91  std::vector<double> peak(3);
92 
93  inputIt.GoToBegin();
94  while( !inputIt.IsAtEnd() )
95  {
96  inputIndex = inputIt.GetIndex();
97  if (!this->IsPixelIndexVisible(inputIndex))
98  {
99  ++inputIt;
100  continue;
101  }
102 
103  inputPtr->TransformIndexToPhysicalPoint(inputIndex, inputPhysicalPoint);
104  inputPixel = inputIt.Get();
105 
106  for (unsigned int k=0;k<realNumberOfPeaks;k++) //isotropic component?
107  {
108  norm = 0;
109  peak = PeakContainerHelper::GetPeak(inputPixel, k, m_PeakType);
110  for ( int d = 0; d < 3; ++d )
111  peak[d] = this->m_Flip[d]? -peak[d] : peak[d];
112 
113  for (unsigned int d=0; d<3; d++)
114  {
115  norm += peak[d]*peak[d];
116 
117  outputMeshPoint1[d] = peak[d] * this->m_Scale + inputPhysicalPoint[d];
118  outputMeshPoint2[d] = -peak[d] * this->m_Scale + inputPhysicalPoint[d];
119  if (this->m_ColorScheme==Superclass::DIRECTION)
120  RGB[d] = static_cast<VTK_TYPE_NAME_UNSIGNED_CHAR>(std::fabs(peak[d])*255.0);
121  else
122  RGB[d] = static_cast<VTK_TYPE_NAME_UNSIGNED_CHAR>(m_ColorPeak[d]);
123  }
124 
125  if ( norm > 0 )
126  {
127  outputMeshPoints->InsertNextPoint( outputMeshPoint1 );
128  outputMeshPoints->InsertNextPoint( outputMeshPoint2 );
129  outputMeshRGB->InsertNextTupleValue(RGB);
130  outputMeshRGB->InsertNextTupleValue(RGB);
131 
132  outputMeshCellArray->InsertNextCell(numberOfCellPoints);
133 
134  for( vtkIdType p = 0; p < numberOfCellPoints; p++ )
135  outputMeshCellArray->InsertCellPoint( offset + p );
136 
137  offset += numberOfCellPoints;
138  }
139 
140  }
141 
142  ++inputIt;
143  }
144 
145  this->m_Mesh->SetPoints( outputMeshPoints );
146  this->m_Mesh->SetLines( outputMeshCellArray );
147  this->m_Mesh->GetPointData()->SetScalars( outputMeshRGB );
148 
149  if (m_TubeRadius>0)
150  {
151  vtkSmartPointer<vtkTubeFilter> tubeFilter = vtkTubeFilter::New();
152  tubeFilter->SetInputData(this->m_Mesh);
153  tubeFilter->SetRadius(m_TubeRadius);
154  tubeFilter->SetNumberOfSides(6);
155  tubeFilter->SetCapping(1);
156  tubeFilter->Update();
157  this->m_Mesh = tubeFilter->GetOutput();
158  }
159 }
160 
161 
162 }
163 
164 #endif
165 
InputImageType::RegionType InputImageRegionType
Created "08-26-2016.
InputImageType::PointType InputImagePointType
const T & min(const T &a, const T &b)
Return the minimum between a and b.
Definition: utlCore.h:257
Compute mesh from SH coefficients.
InputImageType::ConstPointer InputImageConstPointer
static VectorType GetPeak(const ContainerType &vec, const int index, const PeakType peakType)
LightObject::Pointer InternalClone() const ITK_OVERRIDE
static int GetNumberOfPeaks(const PeakType peakType, const int dimension)
virtual void GenerateData() ITK_OVERRIDE
InputImageType::PixelType InputImagePixelType
InputImageType::IndexType InputImageIndexType
static int GetDimensionPerPeak(const PeakType peakType)