DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
itkMeshFromFiberTractsFilter.hxx
Go to the documentation of this file.
1 
11 #ifndef __itkMeshFromFiberTractsFilter_hxx
12 #define __itkMeshFromFiberTractsFilter_hxx
13 
15 
16 #include <vtkPoints.h>
17 #include <vtkFloatArray.h>
18 #include <vtkDoubleArray.h>
19 #include <vtkPointData.h>
20 #include <vtkTubeFilter.h>
21 #include <vtkCellArray.h>
22 
23 #include "utlNDArray.h"
24 #include "utlCore.h"
25 #include "utlDMRI.h"
26 
27 namespace itk
28 {
29 
30 typename LightObject::Pointer
33 {
34  typename LightObject::Pointer loPtr = Superclass::InternalClone();
35 
36  typename Self::Pointer rval = dynamic_cast<Self *>(loPtr.GetPointer());
37  if(rval.IsNull())
38  {
39  itkExceptionMacro(<< "downcast to type " << this->GetNameOfClass()<< " failed.");
40  }
41 
42  rval->m_FiberTracts = m_FiberTracts;
43  rval->m_ColorScheme = m_ColorScheme;
44  rval->m_ShapeMode = m_ShapeMode;
45  rval->m_TubeRadius = m_TubeRadius;
46  rval->m_Mesh = m_Mesh;
47  rval->m_Flip = m_Flip;
48 
49  for ( int i = 0; i < 3; ++i )
50  rval->m_Color[i] = m_Color[i];
51 
52  return loPtr;
53 }
54 
55 void
58 {
59  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
60  vtkSmartPointer<vtkCellArray> lines = vtkSmartPointer<vtkCellArray>::New();
61  // vtkSmartPointer<vtkDoubleArray> scalars = vtkSmartPointer<vtkDoubleArray>::New();
62  vtkSmartPointer<vtkUnsignedCharArray> scalars = vtkSmartPointer<vtkUnsignedCharArray>::New();
63  scalars->SetNumberOfComponents(3);
64  scalars->SetName("rgb_color");
65  double rgb[3];
66 
67 
68  int numFibers = m_FiberTracts->GetNumberOfFibers();
69 
70  utl::Vector<double> dirMean(3), dir(3);
71 
72  long count=0;
73  double color = 0;
74  for ( int n = 0; n < numFibers; ++n )
75  {
76  auto fiber = m_FiberTracts->GetFiber(n);
77  int numPoints = fiber->GetNumberOfPoints();
78 
79  utlPrintVar(this->GetDebug(), n, numPoints);
80 
81 
82  if (numPoints>0)
83  {
84 
85  lines->InsertNextCell(numPoints);
86 
87  if (m_ColorScheme==COLOR_BY_MEAN_DIRECTION)
88  dirMean.Fill(0.0);
89 
90  for ( int i = 0; i < numPoints; ++i )
91  {
92  auto vertex = fiber->GetPoint(i);
93  utl::FlipVector(vertex, m_Flip, 3);
94  points->InsertPoint(count, vertex[0], vertex[1], vertex[2]);
95  lines->InsertCellPoint(count);
96 
97  if (m_ColorScheme==COLOR_FIXED)
98  {
99  scalars->InsertTuple(count, m_Color);
100  }
101  else if (m_ColorScheme==COLOR_BY_POINT_DIRECTION)
102  {
103  auto diff = fiber->GetDirection(i);
104  utl::FlipVector(diff, m_Flip, 3);
105  utl::VectorToVector(diff, dir, 3);
106  double dirNorm = dir.GetTwoNorm();
107  if (dirNorm>0)
108  dir /= dirNorm;
109 
110  for ( int d = 0; d < 3; ++d )
111  rgb[d] = std::fabs(dir[d])*255.0;
112  scalars->InsertTuple(count, rgb);
113  }
114  else if (m_ColorScheme==COLOR_BY_MEAN_DIRECTION)
115  {
116  auto diff = fiber->GetDirection(i);
117  utl::FlipVector(diff, m_Flip, 3);
118  utl::VectorToVector(diff, dir, 3);
119  double dirNorm = dir.GetTwoNorm();
120  if (dirNorm>0)
121  dir /= dirNorm;
122  dirMean += dir;
123  }
124  else if (m_ColorScheme==COLOR_BY_IMAGE)
125  {
126  utlGlobalException(true, "TODO");
127 
128  }
129  else if (m_ColorScheme==COLOR_BY_SCALARS)
130  {
131  utlGlobalException(true, "TODO");
132 
133  }
134  else if (m_ColorScheme==COLOR_BY_PROPERTY)
135  {
136  utlGlobalException(true, "TODO");
137 
138  }
139 
140  count++;
141  }
142 
143  if (m_ColorScheme==COLOR_BY_MEAN_DIRECTION)
144  {
145  double dirNorm = dirMean.GetTwoNorm();
146  if (dirNorm>0)
147  dirMean /= dirNorm;
148  for ( int d = 0; d < 3; ++d )
149  rgb[d] = std::fabs(dirMean[d])*255.0;
150  for ( int i = 0; i < numPoints; ++i )
151  scalars->InsertTuple(count-i, rgb);
152  }
153  else if (m_ColorScheme==COLOR_BY_ENDPOINTS_DIRECTION)
154  {
155  VertexType p0 = fiber->GetPoint(0);
156  VertexType p1 = fiber->GetPoint(numPoints-1);
157  auto diff = p1-p0;
158  utl::VectorToVector(diff, dir, 3);
159  double dirNorm = dir.GetTwoNorm();
160  if (dirNorm>0)
161  dir /= dirNorm;
162  for ( int d = 0; d < 3; ++d )
163  rgb[d] = std::fabs(dir[d])*255.0;
164  for ( int i = 0; i < numPoints; ++i )
165  scalars->InsertTuple(count-i, rgb);
166  }
167 
168  }
169  }
170 
171 
172  m_Mesh->SetPoints( points );
173  m_Mesh->SetLines( lines );
174 
175  if (m_ColorScheme!=COLOR_NONE)
176  (m_Mesh->GetPointData())->SetScalars(scalars);
177 
178  m_Mesh->Squeeze();
179 
180  if (m_TubeRadius>0 && m_ShapeMode==GLYPH_TUBE)
181  {
182  vtkSmartPointer<vtkTubeFilter> tubeFilter = vtkTubeFilter::New();
183  tubeFilter->SetInputData(this->m_Mesh);
184  tubeFilter->SetRadius(m_TubeRadius);
185  tubeFilter->SetNumberOfSides(6);
186  tubeFilter->Update();
187  this->m_Mesh = tubeFilter->GetOutput();
188  }
189 }
190 
191 }
192 
193 
194 #endif
195 
196 
NDArray<T,1> is a vector class which uses blas mkl.
Definition: utlVector.h:36
void VectorToVector(const T1 &v1, T2 &v2, const int N)
Definition: utlCore.h:1699
double GetTwoNorm() const
Definition: utlNDArray.h:1013
void FlipVector(T1 &vec, const T2 &flip, const int N)
Definition: utlCore.h:1690
#define utlGlobalException(cond, expout)
Definition: utlCoreMacro.h:372
#define utlPrintVar(cond,...)
Definition: utlCoreMacro.h:309
Created "08-23-2017.
LightObject::Pointer InternalClone() const ITK_OVERRIDE