DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
utlVNL.h
Go to the documentation of this file.
1 
18 #ifndef __utlVNL_h
19 #define __utlVNL_h
20 
21 
22 #include "utlCore.h"
23 #include <vnl/vnl_matrix.h>
24 #include <vnl/algo/vnl_symmetric_eigensystem.h>
25 #include <vnl/algo/vnl_svd.h>
26 
27 namespace utl
28 {
29 
33 template <class T>
34 vnl_matrix<T>
35 SphericalToCartesian ( const vnl_matrix<T>& in )
36 {
37  utlAssert(in.columns()==3 || in.rows()==3, "wrong dimension");
38  vnl_matrix<T> out(in);
39  if (in.columns()==3)
40  {
41  for ( int i = 0; i < in.rows(); i += 1 )
42  utl::spherical2Cartesian(in(i,0), in(i,1), in(i,2), out(i,0), out(i,1), out(i,2));
43  }
44  else
45  {
46  for ( int i = 0; i < in.columns(); i += 1 )
47  utl::spherical2Cartesian(in(0,i), in(1,i), in(2,i), out(0,i), out(1,i), out(2,i));
48  }
49  return out;
50 }
51 
52 template <class T>
53 vnl_matrix<T>
54 CartesianToSpherical ( const vnl_matrix<T>& in )
55 {
56  utlAssert(in.columns()==3 || in.rows()==3, "wrong dimension");
57  vnl_matrix<T> out(in);
58  if (in.columns()==3)
59  {
60  for ( int i = 0; i < in.rows(); i += 1 )
61  utl::cartesian2Spherical(in(i,0), in(i,1), in(i,2), out(i,0), out(i,1), out(i,2));
62  }
63  else
64  {
65  for ( int i = 0; i < in.columns(); i += 1 )
66  utl::cartesian2Spherical(in(0,i), in(1,i), in(2,i), out(0,i), out(1,i), out(2,i));
67  }
68  return out;
69 }
70 
72 template <class T>
73 vnl_matrix<T>
74 NormalizeMinMax ( const vnl_matrix<T>& matrix )
75 {
76  std::vector<T> vec(matrix.rows());
77  vnl_matrix<T> result(matrix);
78  for ( int i = 0; i < matrix.columns(); i += 1 )
79  {
80  for ( int j = 0; j < matrix.rows(); j += 1 )
81  vec[j] = matrix(j,i);
82  vec = utl::NormalizeMinMax(vec);
83  for ( int j = 0; j < matrix.rows(); j += 1 )
84  result(j,i) = vec[j];
85  }
86  return result;
87 }
88 
89 template <class T>
90 vnl_matrix<T>
91 ConnectVnlMatrix ( const vnl_matrix<T>& m1, const vnl_matrix<T>& m2, const bool isConnectRow )
92 {
93  if (isConnectRow)
94  {
95  utlException(m1.columns()!=m2.columns(), "wrong column size! m1.columns()="<<m1.columns()<<", m2.columns()"<<m2.columns());
96  vnl_matrix<T> result(m1.rows()+m2.rows(), m1.columns());
97  int m1Rows = m1.rows();
98  for ( int j = 0; j < m1.columns(); j += 1 )
99  {
100  for ( int i = 0; i < m1.rows(); i += 1 )
101  result(i,j) = m1(i,j);
102  for ( int i = 0; i < m2.rows(); i += 1 )
103  result(i+m1Rows,j) = m2(i,j);
104  }
105  return result;
106  }
107  else
108  {
109  utlException(m1.rows()!=m2.rows(), "wrong row size! m1.rows()="<<m1.rows()<<", m2.rows()"<<m2.rows());
110  vnl_matrix<T> result(m1.rows(), m1.columns()+m2.columns());
111  int m1Columns = m1.columns();
112  for ( int i = 0; i < m1.rows(); i += 1 )
113  {
114  for ( int j = 0; j < m1.columns(); j += 1 )
115  result(i,j) = m1(i,j);
116  for ( int j = 0; j < m2.columns(); j += 1 )
117  result(i,j+m1Columns) = m2(i,j);
118  }
119  return result;
120  }
121 }
122 
123 template <class T>
124 vnl_vector<T>
125 ConnectVnlVector ( const vnl_vector<T>& m1, const vnl_vector<T>& m2 )
126 {
127  vnl_vector<T> result(m1.size()+m2.size());
128  int m1Size = m1.size();
129  for ( int i = 0; i < m1Size; i += 1 )
130  result[i] = m1[i];
131  for ( int i = 0; i < m2.size(); i += 1 )
132  result[i+m1Size] = m2[i];
133  return result;
134 }
135 
136 template <class T>
137 vnl_vector<T>
138 GetAbsoluteVnlVector ( const vnl_vector<T>& vec )
139 {
140  vnl_vector<T> result(vec);
141  for ( int i = 0; i < vec.size(); i += 1 )
142  result[i] = std::fabs(vec[i]);
143  return result;
144 }
145 
146 template <class T>
147 T
148 GetMedianVnlVector(const vnl_vector<T>& values)
149 {
150  vnl_vector<T> vec(values);
151  const unsigned int N = vec.size();
152  std::nth_element(vec.begin(),vec.begin()+N/2,vec.end());
153  return vec[N/2];
154 }
155 
156 template <class T>
157 std::vector<T>
158 GetVnlVectorStats ( const vnl_vector<T>& values )
159 {
160  return utl::GetContainerStats(values.begin(), values.end());
161 }
162 
163 template <class T>
164 std::vector<T>
165 GetVnlMatrixStats ( const vnl_matrix<T>& values )
166 {
167  return utl::GetContainerStats(values.begin(), values.end());
168 }
169 
171 template <class T>
172 inline double
173 GetMinAngle( const vnl_matrix<T>& grad ) // cartesian
174 {
175  utlException(grad.rows()<2 || grad.columns()!=3, "grad has wrong size!");
176  double angle = 90.0;
177 
178  /* finding minimum angle between samplings */
179  for(unsigned int i = 1; i < grad.rows(); i++)
180  {
181  double dot = grad(0,0)*grad(i,0) + grad(0,1)*grad(i,1) + grad(0,2)*grad(i,2);
182 
183  if(dot >= -1.0 - M_EPS && dot <= -1.0 + M_EPS)
184  dot = 180;
185  else if(dot <= 1.0 + M_EPS && dot >= 1.0 - M_EPS)
186  dot = 0;
187  else
188  dot = 180*std::acos(dot)/M_PI;
189 
190  if(dot < angle)
191  angle = dot;
192  }
193  return angle;
194 }
195 
197 template <class T>
198 vnl_matrix<T>
199 GetVnlSymmericMatrixPInverse ( const vnl_matrix<T>& mat, const double eps=1e-8)
200 {
201  utlException(mat.rows()!=mat.columns(), "wrong size! mat.rows()="<<mat.rows()<<", mat.columns()="<<mat.columns());
202  typedef vnl_symmetric_eigensystem< T > SymEigenSystemType;
203  SymEigenSystemType eig (mat);
204  unsigned n = eig.D.rows();
205  vnl_diag_matrix<T> invD(eig.D);
206  for (unsigned i=0; i<n; ++i)
207  {
208  if ( eig.D(i,i)>eps || eig.D(i,i)<-eps )
209  invD(i,i) = 1.0/eig.D(i,i);
210  else
211  invD(i, i) = 0.0;
212  }
213  return eig.V * invD * eig.V.transpose();
214 }
215 
217 template <class T>
218 vnl_matrix<T>
219 GetVnlMatrixPInverse ( const vnl_matrix<T>& mat, const double eps=1e-8)
220 {
221  vnl_svd<T> svd(mat);
222  svd.zero_out_absolute(eps);
223  return svd.pinverse();
224 }
225 
226 template <class T>
227 void
228 SaveVnlMatrix ( const vnl_matrix<T>& matrix, const std::string& file )
229 {
230  int NumberRows = matrix.rows(), NumberColumns=matrix.columns();
231  utl::SaveMatrix<vnl_matrix<T> >(matrix, NumberRows, NumberColumns, file);
232  return;
233 }
234 
235 template <class T>
236 void
237 PrintVnlMatrixStats ( const vnl_matrix<T>& matrix, const std::string& str="", const char* separate=" ", std::ostream& os=std::cout )
238 {
239  int NumberRows = matrix.rows(), NumberColumns=matrix.columns();
240  utl::PrintMatrixStats<vnl_matrix<T> >(matrix, NumberRows, NumberColumns, str, separate, os);
241 }
242 
243 template <class T>
244 void
245 PrintVnlMatrix ( const vnl_matrix<T>& matrix, const std::string& str="", const char* separate=" ", std::ostream& os=std::cout )
246 {
247  int NumberRows = matrix.rows(), NumberColumns=matrix.columns();
248  utl::PrintMatrix<vnl_matrix<T> >(matrix, NumberRows, NumberColumns, str, separate, os);
249 }
250 
251 template <class T>
252 void
253 PrintVnlVector ( const vnl_vector<T>& vec, const std::string& str="", const char* separate=" ", std::ostream& os=std::cout)
254 {
255  int NSize = vec.size();
256  utl::PrintContainer(vec.begin(), vec.end(), str, separate, os);
257 }
258 
259 template <class T>
260 vnl_vector<T>
261 GetValuesVnlVector ( const vnl_vector<T>& vec, const int colstart, const int n )
262 {
263  vnl_vector<T> result(n);
264  for ( int i = 0; i < n; i += 1 )
265  result[i] = vec[i+colstart];
266  return result;
267 }
268 
269 template <class T>
270 vnl_vector<T>
271 GetValuesVnlVector ( const vnl_vector<T>& vec, const std::vector<int>& index )
272 {
273  vnl_vector<T> result(index.size());
274  for ( int i = 0; i < index.size(); i += 1 )
275  result[i] = vec[index[i]];
276  return result;
277 }
278 
279 template <class T>
280 void
281 SetValuesVnlVector ( const vnl_vector<T>& subvec, vnl_vector<T>& vec, const int colstart )
282 {
283  utlException(subvec.size()+colstart>vec.size(), "wrong size! subvec.size()="<<subvec.size() << ", vec.size()="<<vec.size() );
284  for ( int i = 0; i < subvec.size(); i += 1 )
285  vec[i+colstart] = subvec[i];
286 }
287 
288 template <class T>
289 void
290 SetValuesVnlVector ( const vnl_vector<T>& subvec, vnl_vector<T>& vec, const std::vector<int>& index )
291 {
292  utlException(subvec.size()!=index.size(), "wrong size!");
293  for ( int i = 0; i < index.size(); i += 1 )
294  vec[index[i]] = subvec[i];
295 }
296 
297 template <class T>
298 vnl_matrix<T>
299 GetRowsVnlMatrix ( const vnl_matrix<T>& mat, const std::vector<int>& index )
300 {
301  utlException(index.size()>mat.rows(), "wrong size!");
302  vnl_matrix<T> result(index.size(), mat.columns());
303  for ( int i = 0; i < index.size(); i += 1 )
304  for ( int j = 0; j < mat.columns(); j += 1 )
305  result(i, j) = mat(index[i], j);
306  return result;
307 }
308 
309 template <class T>
310 vnl_matrix<T>
311 GetColumnsVnlMatrix ( const vnl_matrix<T>& mat, const std::vector<int>& index )
312 {
313  utlException(index.size()>mat.columns(), "wrong size!");
314  vnl_matrix<T> result(mat.rows(), index.size());
315  for ( int i = 0; i < mat.rows(); i += 1 )
316  for ( int j = 0; j < index.size(); j += 1 )
317  result(i, j) = mat(i, index[j]);
318  return result;
319 }
320 
321 template <class T>
322 void
323 SetRowsVnlMatrix ( const vnl_matrix<T>& submat, vnl_matrix<T>& mat, const std::vector<int>& index )
324 {
325  utlException(submat.columns()!=mat.columns(), "wrong size!");
326  utlException(submat.rows()!=index.size(), "wrong size!");
327  for ( int i = 0; i < index.size(); i += 1 )
328  for ( int j = 0; j < mat.columns(); j += 1 )
329  mat(index[i], j) = submat(i,j);
330 }
331 
332 template <class T>
333 void
334 SetColumnsVnlMatrix ( const vnl_matrix<T>& submat, vnl_matrix<T>& mat, const std::vector<int>& index )
335 {
336  utlException(submat.rows()!=mat.rows(), "wrong size!");
337  utlException(submat.columns()!=index.size(), "wrong size!");
338  for ( int i = 0; i < mat.rows(); i += 1 )
339  for ( int j = 0; j < index.size(); j += 1 )
340  mat(i, index[j]) = submat(i,j);
341 }
342 
343 template <class T>
344 std::vector<T>
345 VnlVectorToStdVector ( const vnl_vector<T>& vec )
346 {
347  std::vector<T> v(vec.size());
348  for ( int i = 0; i < vec.size(); i += 1 )
349  v[i] = vec[i];
350  return v;
351 }
352 
353 template <class T>
354 vnl_vector<T>
355 StdVectorToVnlVector ( const std::vector<T>& vec )
356 {
357  vnl_vector<T> v(vec.size());
358  for ( int i = 0; i < vec.size(); i += 1 )
359  v[i] = vec[i];
360  return v;
361 }
362 
363 template <class T>
364 vnl_matrix<T>
365 GetDiagonalMatrix ( const vnl_vector<T>& vec )
366 {
367  vnl_matrix<T> mat(vec.size(), vec.size());
368  mat.fill(0.0);
369  mat.set_diagonal(vec);
370  return mat;
371 }
372 
373 template <class T>
374 bool
375 IsMatrixSymmetric ( const vnl_matrix<T>& mat, const double eps=1e-10 )
376 {
377  if (mat.rows()!=mat.columns())
378  return false;
379  for ( int i = 1; i < mat.rows(); i += 1 )
380  {
381  for ( int j = 0; j < i; j += 1 )
382  {
383  if ( std::fabs(mat(i,j)- mat(j,i))>eps )
384  return false;
385  }
386  }
387  return true;
388 }
389 
392 }
393 
394 
395 #endif
void PrintContainer(IteratorType v1, IteratorType v2, const std::string &str="", const char *separate=" ", std::ostream &os=std::cout, bool showStats=true)
Definition: utlCore.h:1068
void PrintVnlVector(const vnl_vector< T > &vec, const std::string &str="", const char *separate=" ", std::ostream &os=std::cout)
Definition: utlVNL.h:253
vnl_matrix< T > GetDiagonalMatrix(const vnl_vector< T > &vec)
Definition: utlVNL.h:365
std::vector< double > GetContainerStats(IteratorType v1, IteratorType v2)
Definition: utlCore.h:921
void PrintVnlMatrix(const vnl_matrix< T > &matrix, const std::string &str="", const char *separate=" ", std::ostream &os=std::cout)
Definition: utlVNL.h:245
#define utlException(cond, expout)
Definition: utlCoreMacro.h:548
void spherical2Cartesian(const T r, const T theta, const T phi, T &x, T &y, T &z)
Definition: utlCore.h:1602
void SetColumnsVnlMatrix(const vnl_matrix< T > &submat, vnl_matrix< T > &mat, const std::vector< int > &index)
Definition: utlVNL.h:334
vnl_matrix< T > GetVnlSymmericMatrixPInverse(const vnl_matrix< T > &mat, const double eps=1e-8)
Definition: utlVNL.h:199
void SetRowsVnlMatrix(const vnl_matrix< T > &submat, vnl_matrix< T > &mat, const std::vector< int > &index)
Definition: utlVNL.h:323
vnl_matrix< T > GetVnlMatrixPInverse(const vnl_matrix< T > &mat, const double eps=1e-8)
Definition: utlVNL.h:219
NDArray< T, 2 > CartesianToSpherical(const NDArray< T, 2 > &in)
bool IsMatrixSymmetric(const vnl_matrix< T > &mat, const double eps=1e-10)
Definition: utlVNL.h:375
#define M_EPS
Definition: utlCoreMacro.h:54
#define M_PI
Definition: utlCoreMacro.h:57
Definition: utl.h:90
vnl_matrix< T > GetRowsVnlMatrix(const vnl_matrix< T > &mat, const std::vector< int > &index)
Definition: utlVNL.h:299
std::vector< T > GetVnlVectorStats(const vnl_vector< T > &values)
Definition: utlVNL.h:158
double GetMinAngle(const vnl_matrix< T > &grad)
Definition: utlVNL.h:173
NDArray< T, 2 > SphericalToCartesian(const NDArray< T, 2 > &in)
T GetMedianVnlVector(const vnl_vector< T > &values)
Definition: utlVNL.h:148
#define utlAssert(cond, expout)
Definition: utlCoreMacro.h:550
std::vector< T > GetVnlMatrixStats(const vnl_matrix< T > &values)
Definition: utlVNL.h:165
VectorType NormalizeMinMax(const VectorType &v, const int nSize)
Definition: utlCore.h:637
vnl_matrix< T > GetColumnsVnlMatrix(const vnl_matrix< T > &mat, const std::vector< int > &index)
Definition: utlVNL.h:311
vnl_vector< T > StdVectorToVnlVector(const std::vector< T > &vec)
Definition: utlVNL.h:355
vnl_vector< T > GetValuesVnlVector(const vnl_vector< T > &vec, const int colstart, const int n)
Definition: utlVNL.h:261
void cartesian2Spherical(const T x, const T y, const T z, T &r, T &theta, T &phi)
Definition: utlCore.h:1579
void SaveVnlMatrix(const vnl_matrix< T > &matrix, const std::string &file)
Definition: utlVNL.h:228
vnl_vector< T > ConnectVnlVector(const vnl_vector< T > &m1, const vnl_vector< T > &m2)
Definition: utlVNL.h:125
std::vector< T > VnlVectorToStdVector(const vnl_vector< T > &vec)
Definition: utlVNL.h:345
vnl_vector< T > GetAbsoluteVnlVector(const vnl_vector< T > &vec)
Definition: utlVNL.h:138
void SetValuesVnlVector(const vnl_vector< T > &subvec, vnl_vector< T > &vec, const int colstart)
Definition: utlVNL.h:281
vnl_matrix< T > ConnectVnlMatrix(const vnl_matrix< T > &m1, const vnl_matrix< T > &m2, const bool isConnectRow)
Definition: utlVNL.h:91
void PrintVnlMatrixStats(const vnl_matrix< T > &matrix, const std::string &str="", const char *separate=" ", std::ostream &os=std::cout)
Definition: utlVNL.h:237