DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
itkPeakContainerHelper.h
Go to the documentation of this file.
1 
18 #ifndef __itkPeakContainerHelper_h
19 #define __itkPeakContainerHelper_h
20 
21 #include "utlCore.h"
22 #include "itkVariableLengthVector.h"
23 
24 
25 namespace itk
26 {
27 
28 typedef enum
29 {
31  XYZV=0,
33  XYZ,
38 }PeakType;
39 
48 {
49 public:
50 
51  static std::string GetString(const PeakType peakType)
52  {
53  if (peakType==XYZV)
54  return std::string("XYZV");
55  else if (peakType==XYZ)
56  return std::string("XYZ");
57  else if (peakType==NXYZV)
58  return std::string("NXYZV");
59  else if (peakType==NXYZ)
60  return std::string("NXYZ");
61  else
62  {
63  utlGlobalException(true, "wrong peakType");
64  return "";
65  }
66  }
67  static PeakType GetPeakType(const std::string& str)
68  {
69  if (str=="XYZV")
70  return XYZV;
71  else if (str=="XYZ")
72  return XYZ;
73  else if (str=="NXYZV")
74  return NXYZV;
75  else if (str=="NXYZ")
76  return NXYZ;
77  else
78  {
79  utlGlobalException(true, "wrong peakType");
80  }
81  }
82 
83  static int GetDimensionPerPeak(const PeakType peakType)
84  {
85  if (peakType==XYZV || peakType==NXYZV)
86  return 4;
87  else if (peakType==XYZ || peakType==NXYZ)
88  return 3;
89  else
90  {
91  utlGlobalException(true, "wrong peakType");
92  return -1;
93  }
94  }
95 
96  static int GetDimension(const PeakType peakType, const int numberOfPeaks)
97  {
98  int d = GetDimensionPerPeak(peakType);
99  if (peakType==XYZ || peakType==XYZV)
100  return d*numberOfPeaks;
101  else if (peakType==NXYZ || peakType==NXYZV)
102  return d*numberOfPeaks+1;
103  else
104  {
105  utlGlobalException(true, "wrong peakType");
106  return -1;
107  }
108  }
109 
110  static int GetNumberOfPeaks(const PeakType peakType, const int dimension)
111  {
112  int d = GetDimensionPerPeak(peakType);
113  int off=0;
114  if (peakType==XYZ || peakType==XYZV)
115  off = 0;
116  else if (peakType==NXYZ || peakType==NXYZV)
117  off = 1;
118  else
119  {
120  utlGlobalException(true, "wrong peakType");
121  return -1;
122  }
123  utlSAException(!utl::IsInt((1.0*dimension-off)/double(d)))(d)(off)(dimension).msg("wrong size");
124  return (dimension-off)/d;
125  }
126 
127  template< class ContainerType >
128  static int GetNumberOfPeaks(const PeakType peakType, const int dimension, const ContainerType& vec)
129  {
130  int nn = GetNumberOfPeaks(peakType, dimension);
131  int num=0;
132  for ( int i = 0; i < nn; i += 1 )
133  {
134  std::vector<double> p = GetPeak(vec, i, peakType);
135  if (p[0]*p[0]+p[1]*p[1]+p[2]*p[2])
136  num++;
137  }
138  return num;
139  }
140 
141  template< class VectorType=std::vector<double>, class ContainerType >
142  static VectorType GetPeak(const ContainerType& vec, const int index, const PeakType peakType)
143  {
144  VectorType peak(3);
145  int d = GetDimensionPerPeak(peakType);
146  int off=0;
147  if (peakType==XYZ || peakType==XYZV)
148  off = 0;
149  else if (peakType==NXYZ || peakType==NXYZV)
150  off = 1;
151  else
152  utlException(true, "wrong peakType");
153 
154  peak[0]=vec[d*index+off+0];
155  peak[1]=vec[d*index+off+1];
156  peak[2]=vec[d*index+off+2];
157  return peak;
158  }
159 
160  template< class VectorType, class ContainerType >
161  static void SetPeak(const VectorType& peak, ContainerType& vec, const int index, const PeakType peakType)
162  {
163  int d = GetDimensionPerPeak(peakType);
164  int off=0;
165  if (peakType==XYZ || peakType==XYZV)
166  off = 0;
167  else if (peakType==NXYZ || peakType==NXYZV)
168  off = 1;
169  else
170  utlException(true, "wrong peakType");
171 
172  vec[d*index+off+0]=peak[0];
173  vec[d*index+off+1]=peak[1];
174  vec[d*index+off+2]=peak[2];
175  }
176 
177  template< class ContainerType >
178  static double GetPeakValue(const ContainerType& vec, const int index, const PeakType peakType)
179  {
180  utlException(peakType==XYZ || peakType==NXYZ, "no value for peakType=" << peakType);
181  int d = GetDimensionPerPeak(peakType);
182  int off=0;
183  if (peakType==XYZV)
184  off = 0;
185  else if (peakType==NXYZV)
186  off = 1;
187  else
188  utlException(true, "wrong peakType");
189 
190  return vec[d*index+off+3];
191  }
192 
193  template< class ContainerType >
194  static void SetPeakValue(const double peakValue, ContainerType& vec, const int index, const PeakType peakType)
195  {
196  utlException(peakType==XYZ || peakType==NXYZ, "no value for peakType=" << peakType);
197  int d = GetDimensionPerPeak(peakType);
198  int off=0;
199  if (peakType==XYZV)
200  off = 0;
201  else if (peakType==NXYZV)
202  off = 1;
203  else
204  utlException(true, "wrong peakType");
205 
206  vec[d*index+off+3]=peakValue;
207  }
208 
209 
210 };
211 
212 }
213 
214 
215 #endif
216 
static double GetPeakValue(const ContainerType &vec, const int index, const PeakType peakType)
static int GetDimension(const PeakType peakType, const int numberOfPeaks)
#define utlException(cond, expout)
Definition: utlCoreMacro.h:548
static PeakType GetPeakType(const std::string &str)
static std::string GetString(const PeakType peakType)
#define utlGlobalException(cond, expout)
Definition: utlCoreMacro.h:372
bool IsInt(const std::string &input, const double epss=1e-10)
Definition: utlCore.h:792
static int GetNumberOfPeaks(const PeakType peakType, const int dimension, const ContainerType &vec)
static VectorType GetPeak(const ContainerType &vec, const int index, const PeakType peakType)
static int GetNumberOfPeaks(const PeakType peakType, const int dimension)
#define utlSAException(expr)
Definition: utlCoreMacro.h:543
static void SetPeakValue(const double peakValue, ContainerType &vec, const int index, const PeakType peakType)
static int GetDimensionPerPeak(const PeakType peakType)
a VariableLengthVector to contain peaks with different storing types
static void SetPeak(const VectorType &peak, ContainerType &vec, const int index, const PeakType peakType)