DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
MeshFromTensors.cxx
Go to the documentation of this file.
1 
13 #include "MeshFromTensorsCLP.h"
14 
15 #include "vtkPolyDataWriter.h"
16 
18 
19 #include "utl.h"
20 #include "utlVTK.h"
21 
22 #include "vtkPolyDataViewer.h"
23 
27  int
28 main (int argc, char const* argv[])
29 {
30  PARSE_ARGS;
31 
32  typedef double PixelType;
33  typedef itk::VectorImage<PixelType, 3> InputImageType;
34 
35  // Time Probe
36  itk::TimeProbe clock;
37 
38  utlGlobalException(_BoxView.size()!=6, "need 6 parameters in --box");
39  utlGlobalException(_SliceView.size()!=3, "need 3 parameters in --box");
40  utlGlobalException(_Flip.size()!=3, "need 3 parameters in --flip");
41 
42  InputImageType::Pointer inputImage;
43 
44  if (utl::IsEndingWith(_InputFile, ".txt"))
45  {
46  std::vector<double> tensor;
47  utl::ReadVector(_InputFile, tensor, " \t,");
48  InputImageType::PixelType pixel(tensor.size());
49  utl::VectorToVector(tensor, pixel, tensor.size());
50  inputImage = itk::GenerateImageFromSingleVoxel<InputImageType>(pixel);
51  }
52  else if (utl::IsFileExist(_InputFile))
53  itk::ReadVectorImage(_InputFile, inputImage);
54  else
55  {
56  std::vector<double> tensor, tensor6d(6), tensor9d(9);
57  utl::SetVector(_InputFile, tensor);
58  utlGlobalException(tensor.size()!=6 && tensor.size()!=9, "size should be 6 or 9");
59  if (tensor.size()==9)
60  {
61  tensor9d = tensor;
63  }
64  else
65  {
66  tensor6d = tensor;
68  }
69  utl::PrintVector(tensor9d, "tensor");
70  InputImageType::PixelType pixel(tensor6d.size());
71  utl::VectorToVector(tensor6d, pixel, tensor6d.size());
72  inputImage = itk::GenerateImageFromSingleVoxel<InputImageType>(pixel);
73  }
74 
75  typedef itk::MeshFromTensorImageFilter<InputImageType> MeshCreatorType;
76  MeshCreatorType::Pointer filter = MeshCreatorType::New();
77 
78  filter->SetInput( inputImage );
79 
80  typedef MeshCreatorType::ScalarImageType ScalarImageType;
81  ScalarImageType::Pointer scalarImage = ScalarImageType::New();
82  if (_ScalarImageFileArg.isSet())
83  {
84  itk::ReadImage(_ScalarImageFile, scalarImage);
85  filter->SetScalarImage(scalarImage);
86  }
87 
88  if (_GlyphType == "LINE") { filter->SetShapeMode(MeshCreatorType::GLYPH_LINE); }
89  if (_GlyphType == "ARROW") { filter->SetShapeMode(MeshCreatorType::GLYPH_ARROW); }
90  if (_GlyphType == "DISK") { filter->SetShapeMode(MeshCreatorType::GLYPH_DISK); }
91  if (_GlyphType == "CYLINDER") { filter->SetShapeMode(MeshCreatorType::GLYPH_CYLINDER); }
92  if (_GlyphType == "CUBE") { filter->SetShapeMode(MeshCreatorType::GLYPH_CUBE); }
93  if (_GlyphType == "ELLIPSOID") { filter->SetShapeMode(MeshCreatorType::GLYPH_SPHERE); }
94  if (_GlyphType == "SUPERQUADRIC") { filter->SetShapeMode(MeshCreatorType::GLYPH_SUPERQUADRIC); }
95 
96  if (_ColorScheme == "NONE") { filter->SetTensorColorScheme(MeshCreatorType::COLOR_NONE); }
97  if (_ColorScheme == "FA") { filter->SetTensorColorScheme(MeshCreatorType::COLOR_BY_FA); }
98  if (_ColorScheme == "MD") { filter->SetTensorColorScheme(MeshCreatorType::COLOR_BY_MD); }
99  if (_ColorScheme == "DIRECTION") { filter->SetTensorColorScheme(MeshCreatorType::COLOR_BY_DIRECTION); }
100  if (_ColorScheme == "IMAGE") { filter->SetTensorColorScheme(MeshCreatorType::COLOR_BY_IMAGE); }
101 
102  filter->SetScale( _Scale );
103  filter->SetFlip(_Flip[0], _Flip[1], _Flip[2]);
104  filter->SetBoxView(_BoxView[0], _BoxView[1], _BoxView[2], _BoxView[3], _BoxView[4], _BoxView[5]);
105  filter->SetSliceView(_SliceView[0], _SliceView[1], _SliceView[2]);
106 
107  // itk::CommandProgressUpdate::Pointer observer =itk::CommandProgressUpdate::New();
108  // if (_ShowProgressArg.isSet())
109  // filter->AddObserver( itk::ProgressEvent(), observer );
110  filter->SetGlyphResolution(_GlyphResolution);
111  filter->SetDebug(_DebugArg.isSet());
112 
113  std::cout << "Generating mesh ... " << std::flush;
114  clock.Start();
115  filter->Update();
116  clock.Stop();
117  std::cout << clock.GetMean() << "s elapsed" << std::endl;
118 
119  MeshCreatorType::OutputMeshPolyDataType* mesh = filter->GetOutput();
120 
121  if (_OutputFileArg.isSet())
122  {
123  utl::WriteVtkPolyData(mesh, _OutputFile);
124  }
125  else
126  {
127  utlGlobalException(_WindowSize.size()!=2, "wrong window size");
128  utlGlobalException(_Angle.size()!=2, "wrong angle size");
129  utlGlobalException(_BackgroundColor.size()!=3, "wrong size of background color");
130  if (_ColorScheme=="DIRECTION")
131  {
132  // huerange (0,1) used for direction coloring using RGBToIndex
133  vtk::VisualizePolyDataWithScalarRange(mesh, _ScalarRange, {0.0,1.0}, _Angle, _WindowSize, !_NoNormalArg.isSet(), !_NoLightingArg.isSet(), _Zoom, _PNGFile, _BackgroundColor);
134  }
135  else
136  {
137  // huerange (0.0667,0) used for scalars
138  vtk::VisualizePolyDataWithScalarRange(mesh, _ScalarRange, {0.6667,0.0}, _Angle, _WindowSize, !_NoNormalArg.isSet(), !_NoLightingArg.isSet(), _Zoom, _PNGFile, _BackgroundColor);
139  }
140  }
141 
142  return EXIT_SUCCESS;
143 }
helper functions specifically used in dmritool
void VectorToVector(const T1 &v1, T2 &v2, const int N)
Definition: utlCore.h:1699
Created "06-08-2017.
Created "06-06-2017.
void ReadVectorImage(const std::string &filename, SmartPointer< VectorImage< PixelType, 3 > > &image, const std::string &printInfo="Reading Image:")
Definition: utl.h:66
bool IsFileExist(const std::string &file)
Definition: utlCore.h:529
Created "06-09-2017.
bool ReadImage(const std::string &filename, SmartPointer< ImageType > &image, const std::string &printInfo="Reading Image:")
Definition: utlITK.h:110
#define utlGlobalException(cond, expout)
Definition: utlCoreMacro.h:372
Compute mesh from a tensor image.
void ReadVector(const std::string &vectorStr, std::vector< T > &vec, const char *cc=" ")
Definition: utlCore.h:1159
void PrintVector(const std::vector< T > &vec, const std::string &str="", const char *separate=" ", std::ostream &os=std::cout, bool showStats=true)
Definition: utlCore.h:1002
void ConvertTensor6DTo9D(const V1Type &v6d, V2Type &v9d, int v6dStoreWay)
Definition: utlDMRI.h:257
void VisualizePolyDataWithScalarRange(const std::vector< vtkPolyData * > &meshes, const std::vector< double > &opacity, const std::vector< double > &scalarRange={-1.0,-1.0}, const std::vector< double > &hueRange={0.6667, 0.0}, const std::vector< double > &angle={0.0, 0.0}, const std::vector< int > &windowSize={600, 600}, const bool useNormal=true, const bool lighting=true, const double zoom=1.0, const std::string &pngfile="", const std::vector< double > &bgColor={0, 0, 0})
bool IsEndingWith(const std::string &fullString, const std::string &ending)
Definition: utlCore.h:537
int SetVector(const char *s, std::vector< T > &vec, const int least_num=0, const char &c=',')
Definition: utlCore.h:1115
int main(int argc, char const *argv[])
Gnerate mesh from peaks.
void ConvertTensor9DTo6D(const V1Type &v9d, V2Type &v6d, int v6dStoreWay)
Definition: utlDMRI.h:315
void WriteVtkPolyData(vtkPolyData *mesh, const std::string &filename)
Definition: utlVTK.h:45