DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
MeshFromSHCoefficients.cxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Mesh From Basis Weights
4 
5  Copyright (c) Pew-Thian Yap, Jian Cheng. All rights reserved.
6  See http://www.unc.edu/~ptyap/ for details.
7 
8  This software is distributed WITHOUT ANY WARRANTY; without even
9  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10  PURPOSE. See the above copyright notices for more information.
11 
12  =========================================================================*/
13 
14 
16 #include "MeshFromSHCoefficientsCLP.h"
17 
18 #include "vtkPolyDataWriter.h"
19 
21 
22 #include "utl.h"
23 #include "utlVTK.h"
24 
25 #include "vtkPolyDataViewer.h"
26 
27 int
28 main(int argc, char *argv[])
29 {
30  PARSE_ARGS;
31 
32  // Time Probe
33  itk::TimeProbe clock;
34 
35  utlGlobalException(_BoxView.size()!=6, "need 6 parameters in --box");
36  utlGlobalException(_SliceView.size()!=3, "need 3 parameters in --box");
37  utlGlobalException(_Flip.size()!=3, "need 3 parameters in --flip");
38 
39  // Define Variables
40  typedef double PixelType;
41  typedef itk::VectorImage<PixelType, 3> InputImageType;
42 
43  InputImageType::Pointer inputImage;
44  if (utl::IsEndingWith(_InputFile, ".txt"))
45  {
46  std::vector<double> shReadVec, shVec;
47  if (_ZonalSHArg.isSet())
48  {
49  utl::ReadVector(_InputFile, shReadVec, " \t,");
50  int shRank = (shReadVec.size()-1)*2;
51  shVec.resize(utl::RankToDimSH(shRank));
52  std::fill(shVec.begin(), shVec.end(), 0.0);
53  for ( int l = 0; l <= shRank; l=l+2 )
54  shVec[ utl::GetIndexSHj(l,0) ] = shReadVec[l/2];
55  }
56  else
57  {
58  utl::ReadVector(_InputFile, shVec, " \t,");
59  int shRank = utl::DimToRankSH(shVec.size());
60  }
61  if (_Debug)
62  utl::PrintVector(shVec, "shVec");
63 
64  InputImageType::PixelType pixel(shVec.size());
65  utl::VectorToVector(shVec, pixel, shVec.size());
66  inputImage = itk::GenerateImageFromSingleVoxel<InputImageType>(pixel);
67  }
68  else
69  itk::ReadVectorImage(_InputFile, inputImage);
70 
71 
72  // Compute MaxOrder if not set
73  if ( !_MaxOrderArg.isSet() )
74  _MaxOrder = utl::DimToRankSH(inputImage->GetNumberOfComponentsPerPixel());
75 
76  // Check Parameters
77  unsigned numberOfBasisFunctions = (_MaxOrder + 1) * (_MaxOrder + 2) / 2;
78  itkAssertOrThrowMacro(numberOfBasisFunctions <=inputImage->GetNumberOfComponentsPerPixel(), "MaxOrder too large. numberOfBasisFunctions="<< numberOfBasisFunctions
79  <<", inputImage->GetNumberOfComponentsPerPixel()="<<inputImage->GetNumberOfComponentsPerPixel());
80 
81  // Create mesh
83  MeshCreatorType::Pointer filter = MeshCreatorType::New();
84  filter->SetScale( _Scale );
85 
86  if (_Normalization=="NONE") filter->SetNormalization( MeshCreatorType::NONE );
87  if (_Normalization=="MIN_MAX") filter->SetNormalization( MeshCreatorType::MIN_MAX );
88  if (_Normalization=="UNIT_MAX") filter->SetNormalization( MeshCreatorType::UNIT_MAX );
89  if (_Normalization=="UNIT_INTEGRAL") filter->SetNormalization( MeshCreatorType::UNIT_INTEGRAL );
90 
91  filter->SetPow(_Pow);
92  filter->SetRemoveNegativeValues(_RemoveNegativeValuesArg.isSet());
93  filter->SetStretch(!_NoStretchArg.isSet());
94  filter->SetMaxOrder( _MaxOrder );
95  filter->SetInput( inputImage );
96 
97  filter->SetFlip(_Flip[0], _Flip[1], _Flip[2]);
98  filter->SetBoxView(_BoxView[0], _BoxView[1], _BoxView[2], _BoxView[3], _BoxView[4], _BoxView[5]);
99  filter->SetSliceView(_SliceView[0], _SliceView[1], _SliceView[2]);
100 
101  if (_BasicShape == "TETRAHEDRON") { filter->SetBasicShape(MeshCreatorType::SphereTessellatorType::TETRAHEDRON); }
102  if (_BasicShape == "OCTAHEDRON") { filter->SetBasicShape(MeshCreatorType::SphereTessellatorType::OCTAHEDRON); }
103  if (_BasicShape == "ICOSAHEDRON") { filter->SetBasicShape(MeshCreatorType::SphereTessellatorType::ICOSAHEDRON); }
104  filter->SetTessellationOrder(_TessellationOrder);
105 
106  if (_ColorScheme == "DIRECTION") { filter->SetColorScheme(MeshCreatorType::DIRECTION); }
107  if (_ColorScheme == "MAGNITUDE") { filter->SetColorScheme(MeshCreatorType::MAGNITUDE); }
108 
110  if (_ShowProgressArg.isSet())
111  filter->AddObserver( itk::ProgressEvent(), observer );
112  if (_SingleThreadArg.isSet())
113  filter->SetNumberOfThreads(1);
114  filter->SetDebug(_DebugArg.isSet());
115 
116  std::cout << "Generating mesh ... " << std::flush;
117  clock.Start();
118  filter->Update();
119  clock.Stop();
120  std::cout << clock.GetMean() << "s elapsed" << std::endl;
121 
122  MeshCreatorType::OutputMeshPolyDataType* mesh = filter->GetOutput();
123 
124  if (_OutputFileArg.isSet())
125  {
126  utl::WriteVtkPolyData(mesh, _OutputFile);
127  }
128  else
129  {
130  utlGlobalException(_WindowSize.size()!=2, "wrong window size");
131  utlGlobalException(_Angle.size()!=2, "wrong angle size");
132  utlGlobalException(_BackgroundColor.size()!=3, "wrong size of background color");
133  vtk::VisualizePolyData(mesh, _Angle, _WindowSize, !_NoNormalArg.isSet(), !_NoLightingArg.isSet(), _Zoom, _PNGFile, _BackgroundColor);
134  }
135 
136  return EXIT_SUCCESS;
137 }
138 
helper functions specifically used in dmritool
void VectorToVector(const T1 &v1, T2 &v2, const int N)
Definition: utlCore.h:1699
int GetIndexSHj(const int l, const int m)
Definition: utlDMRI.h:204
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
int DimToRankSH(const int dimm)
Definition: utlDMRI.h:182
#define utlGlobalException(cond, expout)
Definition: utlCoreMacro.h:372
int RankToDimSH(const int shRank)
Definition: utlDMRI.h:193
void VisualizePolyData(vtkPolyData *mesh, 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})
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
bool IsEndingWith(const std::string &fullString, const std::string &ending)
Definition: utlCore.h:537
int main(int argc, char *argv[])
void WriteVtkPolyData(vtkPolyData *mesh, const std::string &filename)
Definition: utlVTK.h:45