13 #ifndef __itkMeshFromTensorImageFilter_hxx 14 #define __itkMeshFromTensorImageFilter_hxx 18 #include <vtkObjectFactory.h> 19 #include <vtkPoints.h> 20 #include <vtkFloatArray.h> 21 #include <vtkDoubleArray.h> 22 #include <vtkPointData.h> 23 #include <vtkStructuredPoints.h> 24 #include <vtkLookupTable.h> 25 #include <vtkFieldData.h> 26 #include <vtkArrowSource.h> 27 #include <vtkCubeSource.h> 28 #include <vtkCylinderSource.h> 29 #include <vtkSphereSource.h> 30 #include <vtkDiskSource.h> 31 #include <vtkLineSource.h> 32 #include <vtkSuperquadricSource.h> 33 #include <vtkProperty.h> 35 #include "itkImageRegionIteratorWithIndex.h" 39 template <
class TInputImage,
class TOutputMesh>
44 this->m_Glyph = vtkTensorGlyph::New();
45 this->m_Glyph->ClampScalingOn();
46 this->m_Glyph->ColorGlyphsOn();
49 this->SetGlyphShapeToSphere();
51 this->m_Glyph->SetScaleFactor(this->m_Scale);
56 template <
class TInputImage,
class TOutputMesh>
64 this->SetGlyphShapeToLine();
68 this->SetGlyphShapeToArrow();
72 this->SetGlyphShapeToDisk();
76 this->SetGlyphShapeToCylinder();
80 this->SetGlyphShapeToCube();
84 this->SetGlyphShapeToSphere();
87 case GLYPH_SUPERQUADRIC:
88 this->SetGlyphShapeToSuperquadric();
96 this->SetGlyphResolution( this->m_GlyphResolution );
100 template <
class TInputImage,
class TOutputMesh>
105 this->m_ShapeMode = GLYPH_LINE;
106 this->m_Shape = vtkLineSource::New();
107 this->m_Glyph->SetSourceConnection(this->m_Shape->GetOutputPort());
108 this->m_Shape->Delete();
111 template <
class TInputImage,
class TOutputMesh>
116 this->m_ShapeMode = GLYPH_DISK;
117 vtkDiskSource* source = vtkDiskSource::New();
118 source->SetInnerRadius (0.0);
119 this->m_Shape = source;
120 this->m_Glyph->SetSourceConnection(this->m_Shape->GetOutputPort());
121 this->m_Shape->Delete();
124 template <
class TInputImage,
class TOutputMesh>
129 this->m_ShapeMode = GLYPH_ARROW;
130 this->m_Shape = vtkArrowSource::New();
131 vtkArrowSource* arrow = vtkArrowSource::SafeDownCast (this->m_Shape);
136 this->m_Glyph->SetSourceConnection(this->m_Shape->GetOutputPort());
137 this->m_Shape->Delete();
140 template <
class TInputImage,
class TOutputMesh>
145 this->m_ShapeMode = GLYPH_CUBE;
146 this->m_Shape = vtkCubeSource::New();
147 this->m_Glyph->SetSourceConnection(this->m_Shape->GetOutputPort());
148 this->m_Shape->Delete();
151 template <
class TInputImage,
class TOutputMesh>
156 this->m_ShapeMode = GLYPH_CYLINDER;
157 this->m_Shape = vtkCylinderSource::New();
158 this->m_Glyph->SetSourceConnection(this->m_Shape->GetOutputPort());
159 this->m_Shape->Delete();
162 template <
class TInputImage,
class TOutputMesh>
167 this->m_ShapeMode = GLYPH_SPHERE;
168 this->m_Shape = vtkSphereSource::New();
169 this->m_Glyph->SetSourceConnection(this->m_Shape->GetOutputPort());
170 this->m_Shape->Delete();
173 template <
class TInputImage,
class TOutputMesh>
178 this->m_ShapeMode = GLYPH_SUPERQUADRIC;
179 this->m_Shape = vtkSuperquadricSource::New();
180 vtkSuperquadricSource::SafeDownCast (this->m_Shape)->SetPhiRoundness (0.3);
181 vtkSuperquadricSource::SafeDownCast (this->m_Shape)->SetThetaRoundness (0.3);
182 this->m_Glyph->SetSourceConnection(this->m_Shape->GetOutputPort());
183 this->m_Shape->Delete();
186 template <
class TInputImage,
class TOutputMesh>
192 this->m_GlyphResolution = res;
194 vtkArrowSource* arrowSource = 0;
195 vtkDiskSource* diskSource = 0;
196 vtkCylinderSource* cylinderSource = 0;
197 vtkSphereSource* sphereSource = 0;
198 vtkSuperquadricSource* quadricSource = 0;
200 switch(this->m_ShapeMode)
207 arrowSource = vtkArrowSource::SafeDownCast (this->m_Shape);
209 arrowSource->SetShaftResolution (res);
213 diskSource = vtkDiskSource::SafeDownCast (this->m_Shape);
215 diskSource->SetCircumferentialResolution(res);
219 cylinderSource = vtkCylinderSource::SafeDownCast (this->m_Shape);
221 cylinderSource->SetResolution (res);
228 sphereSource = vtkSphereSource::SafeDownCast (this->m_Shape);
231 sphereSource->SetThetaResolution (res);
232 sphereSource->SetPhiResolution (res);
236 case GLYPH_SUPERQUADRIC:
237 quadricSource = vtkSuperquadricSource::SafeDownCast (this->m_Shape);
240 quadricSource->SetThetaResolution (res);
241 quadricSource->SetPhiResolution (res);
250 this->m_Glyph->Modified();
254 template <
class TInputImage,
class TOutputMesh>
259 if (colorScheme==COLOR_BY_FA)
261 return tensor.
GetFA();
263 else if (colorScheme==COLOR_BY_MD)
265 return tensor.
GetMD();
267 else if (colorScheme==COLOR_BY_DIRECTION)
269 double fa = tensor.
GetFA();
278 for (
int i = 0; i < 3; ++i )
279 v1[i] = eigenVectors(i,2);
282 utl::RGBToIndex(std::fabs(v1[0]),std::fabs(v1[1]),std::fabs(v1[2]),ss);
357 template <
class TInputImage,
class TOutputMesh>
364 this->VerifyInputParameters();
366 this->SetGlyphShape(m_ShapeMode);
370 inputPixel.SetSize(inputPtr->GetNumberOfComponentsPerPixel());
374 auto spacing = inputPtr->GetSpacing();
375 auto origin = inputPtr->GetOrigin();
377 vtkSmartPointer<vtkPoints> tens_points = vtkPoints::New();
378 vtkSmartPointer<vtkFloatArray> tens_array = vtkFloatArray::New();
379 tens_array->SetNumberOfComponents(9);
380 tens_array->SetNumberOfTuples(size3d[0]*size3d[1]*size3d[2]);
382 vtkSmartPointer<vtkFloatArray> scalars = vtkFloatArray::New();
383 scalars->SetNumberOfComponents(1);
384 scalars->SetName(
"scalar_color");
386 vtkSmartPointer<vtkUnsignedCharArray> rgbaArray = vtkSmartPointer<vtkUnsignedCharArray>::New();
387 rgbaArray->SetNumberOfComponents(3);
388 rgbaArray->SetName(
"RGBA_color");
389 double rgba[3]={0,0,0};
393 ImageRegionConstIteratorWithIndex<InputImageType> inputIt(inputPtr, inputPtr->GetLargestPossibleRegion());
394 ImageRegionConstIteratorWithIndex<ScalarImageType> scalarIt;
396 scalarIt = ImageRegionConstIteratorWithIndex<ScalarImageType>(m_ScalarImage, m_ScalarImage->GetLargestPossibleRegion());
401 for (inputIt.GoToBegin(), scalarIt.GoToBegin();
403 ++inputIt, ++scalarIt)
406 auto inputIndex = inputIt.GetIndex();
408 if (!this->IsPixelIndexVisible(inputIndex))
413 auto inputVec = inputIt.Get();
414 if (inputVec.GetNorm()< 1e-10)
419 if (this->GetDebug())
425 inputPtr->TransformIndexToPhysicalPoint(inputIndex, inputPhysicalPoint);
427 tens_points->InsertPoint(off, inputPhysicalPoint.GetDataPointer());
429 for (
int i = 0; i < 6; ++i )
430 tensor[i] = inputVec[i];
431 tensor.
Flip(this->m_Flip[0], this->m_Flip[1], this->m_Flip[2]);
434 tens_array->InsertTuple9(off,vec9d[0],vec9d[1],vec9d[2],vec9d[3],vec9d[4],vec9d[5],vec9d[6],vec9d[7],vec9d[8]);
436 if (m_TensorColorScheme!=COLOR_NONE)
439 if (m_TensorColorScheme!=COLOR_BY_IMAGE && m_TensorColorScheme!=COLOR_BY_DIRECTION)
441 ss = GetScalarCodeFromTensor(tensor, m_TensorColorScheme);
442 scalars->InsertTuple1(off, ss);
444 else if (m_TensorColorScheme==COLOR_BY_IMAGE)
447 scalars->InsertTuple1(off, ss);
449 else if (m_TensorColorScheme==COLOR_BY_DIRECTION)
451 ss = GetScalarCodeFromTensor(tensor, m_TensorColorScheme);
452 scalars->InsertTuple1(off, ss);
461 if (this->GetDebug())
471 this->m_Mesh->SetPoints(tens_points);
472 this->m_Mesh->GetPointData()->SetTensors(tens_array);
473 if (m_TensorColorScheme!=COLOR_NONE)
480 this->m_Mesh->GetPointData()->SetScalars(scalars);
483 m_Glyph->SetInputData(this->m_Mesh);
484 m_Glyph->SetScaleFactor(this->m_Scale);
486 m_Glyph->ColorGlyphsOn();
487 m_Glyph->ClampScalingOn();
491 this->m_Mesh = m_Glyph->GetOutput();
NDArray is a N-Dimensional array class (row-major, c version)
bool IsImageEmpty(const SmartPointer< ImageType > &image)
virtual void GenerateData() ITK_OVERRIDE
void SetGlyphShapeToLine()
InputImageType::PointType InputImagePointType
void SetGlyphShapeToArrow()
void SetGlyphShapeToSphere()
void GetEigenValuesVectorsAnalytic(TArrayType &eigenValues, TMatrixType &eigenVectors) const
analytic way to calculate eigenValues (in ascending order) and eigenVectors. In mathematica, Eigenvectors[( { {a, b, c}, {b, d, e}, {c, e, f} } )]
static double GetScalarCodeFromTensor(const DiffusionTensor< double > &tensor, TensorColorSchemeType colorScheme)
RealValueType GetFA() const
void SetGlyphResolution(int res)
MeshFromTensorImageFilter()
void PrintVariableLengthVector(const VariableLengthVector< T >vec, const std::string &str="", const char *separate=" ", std::ostream &os=std::cout)
void Flip(const int flipx, const int flipy, const int flipz)
#define utlGlobalException(cond, expout)
InputImageType::ConstPointer InputImageConstPointer
#define utlPrintVar(cond,...)
std::vector< int > GetVectorImage3DVolumeSize(const SmartPointer< ImageType > &image)
void SetGlyphShapeToCube()
InputImageType::PixelType InputImagePixelType
void SetGlyphShapeToDisk()
void ConvertTensor6DTo9D(const V1Type &v6d, V2Type &v9d, int v6dStoreWay)
RealValueType GetMD() const
void SetGlyphShapeToCylinder()
void SetGlyphShape(GlyphShapeType i)
void RGBToIndex(double R, double G, double B, double &index)
#define utlShowPosition(cond)
tensor with some useful functions
void SetGlyphShapeToSuperquadric()