Developer documentation
Version 3.0.3-105-gd3941f44
tensor.h
Go to the documentation of this file.
1/* Copyright (c) 2008-2022 the MRtrix3 contributors.
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 *
7 * Covered Software is provided under this License on an "as is"
8 * basis, without warranty of any kind, either expressed, implied, or
9 * statutory, including, without limitation, warranties that the
10 * Covered Software is free of defects, merchantable, fit for a
11 * particular purpose or non-infringing.
12 * See the Mozilla Public License v. 2.0 for more details.
13 *
14 * For more details, see http://www.mrtrix.org/.
15 */
16
17#ifndef __dwi_tensor_h__
18#define __dwi_tensor_h__
19
20#include "types.h"
21
22#include "dwi/shells.h"
23
24namespace MR
25{
26 namespace DWI
27 {
28
29 template <typename T, class MatrixType>
30 inline Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> grad2bmatrix (const MatrixType& grad, bool dki = false)
31 {
32 std::unique_ptr<DWI::Shells> shells;
33 try {
34 shells.reset (new DWI::Shells (grad));
35 } catch (...) {
36 WARN ("Unable to separate diffusion gradient table into shells; tensor estimation success uncertain");
37 }
38 if (shells) {
39 if (dki) {
40 if (shells->count() < 3)
41 throw Exception ("Kurtosis tensor estimation requires at least 3 b-value shells");
42 } else {
43 if (shells->count() < 2)
44 throw Exception ("Tensor estimation requires at least 2 b-values");
45 }
46 }
47
48 Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> bmat (grad.rows(), (dki ? 22 : 7));
49 for (ssize_t i = 0; i < grad.rows(); ++i) {
50 bmat (i,0) = grad(i,3) * grad(i,0) * grad(i,0);
51 bmat (i,1) = grad(i,3) * grad(i,1) * grad(i,1);
52 bmat (i,2) = grad(i,3) * grad(i,2) * grad(i,2);
53 bmat (i,3) = grad(i,3) * grad(i,0) * grad(i,1) * T(2.0);
54 bmat (i,4) = grad(i,3) * grad(i,0) * grad(i,2) * T(2.0);
55 bmat (i,5) = grad(i,3) * grad(i,1) * grad(i,2) * T(2.0);
56 bmat (i,6) = T(-1.0);
57 if (dki) {
58 bmat (i,7) = -grad(i,3) * grad(i,3) * grad(i,0) * grad(i,0) * grad(i,0) * grad(i,0) * T(1.0)/T(6.0);
59 bmat (i,8) = -grad(i,3) * grad(i,3) * grad(i,1) * grad(i,1) * grad(i,1) * grad(i,1) * T(1.0)/T(6.0);
60 bmat (i,9) = -grad(i,3) * grad(i,3) * grad(i,2) * grad(i,2) * grad(i,2) * grad(i,2) * T(1.0)/T(6.0);
61 bmat (i,10) = -grad(i,3) * grad(i,3) * grad(i,0) * grad(i,0) * grad(i,0) * grad(i,1) * T(2.0)/T(3.0);
62 bmat (i,11) = -grad(i,3) * grad(i,3) * grad(i,0) * grad(i,0) * grad(i,0) * grad(i,2) * T(2.0)/T(3.0);
63 bmat (i,12) = -grad(i,3) * grad(i,3) * grad(i,0) * grad(i,1) * grad(i,1) * grad(i,1) * T(2.0)/T(3.0);
64 bmat (i,13) = -grad(i,3) * grad(i,3) * grad(i,0) * grad(i,2) * grad(i,2) * grad(i,2) * T(2.0)/T(3.0);
65 bmat (i,14) = -grad(i,3) * grad(i,3) * grad(i,1) * grad(i,1) * grad(i,1) * grad(i,2) * T(2.0)/T(3.0);
66 bmat (i,15) = -grad(i,3) * grad(i,3) * grad(i,1) * grad(i,2) * grad(i,2) * grad(i,2) * T(2.0)/T(3.0);
67 bmat (i,16) = -grad(i,3) * grad(i,3) * grad(i,0) * grad(i,0) * grad(i,1) * grad(i,1);
68 bmat (i,17) = -grad(i,3) * grad(i,3) * grad(i,0) * grad(i,0) * grad(i,2) * grad(i,2);
69 bmat (i,18) = -grad(i,3) * grad(i,3) * grad(i,1) * grad(i,1) * grad(i,2) * grad(i,2);
70 bmat (i,19) = -grad(i,3) * grad(i,3) * grad(i,0) * grad(i,0) * grad(i,1) * grad(i,2) * T(2.0);
71 bmat (i,20) = -grad(i,3) * grad(i,3) * grad(i,0) * grad(i,1) * grad(i,1) * grad(i,2) * T(2.0);
72 bmat (i,21) = -grad(i,3) * grad(i,3) * grad(i,0) * grad(i,1) * grad(i,2) * grad(i,2) * T(2.0);
73 }
74 }
75 return bmat;
76 }
77
78 template <class MatrixType, class VectorTypeOut, class VectorTypeIn>
79 inline void dwi2tensor (VectorTypeOut& dt, const MatrixType& binv, VectorTypeIn& dwi)
80 {
81 using T = typename VectorTypeIn::Scalar;
82 for (ssize_t i = 0; i < dwi.size(); ++i)
83 dwi[i] = dwi[i] > T(0.0) ? -std::log (dwi[i]) : T(0.0);
84 dt = binv * dwi;
85 }
86
87
88 template <class VectorType> inline typename VectorType::Scalar tensor2ADC (const VectorType& dt)
89 {
90 using T = typename VectorType::Scalar;
91 return (dt[0]+dt[1]+dt[2]) / T (3.0);
92 }
93
94
95 template <class VectorType> inline typename VectorType::Scalar tensor2FA (const VectorType& dt)
96 {
97 using T = typename VectorType::Scalar;
98 T trace = tensor2ADC (dt);
99 T a[] = { dt[0]-trace, dt[1]-trace, dt[2]-trace };
100 trace = dt[0]*dt[0] + dt[1]*dt[1] + dt[2]*dt[2] + T (2.0) * (dt[3]*dt[3] + dt[4]*dt[4] + dt[5]*dt[5]);
101 return trace ?
102 std::sqrt (T (1.5) * (a[0]*a[0]+a[1]*a[1]+a[2]*a[2] + T (2.0) * (dt[3]*dt[3]+dt[4]*dt[4]+dt[5]*dt[5])) / trace) :
103 T (0.0);
104 }
105
106
107 template <class VectorType> inline typename VectorType::Scalar tensor2RA (const VectorType& dt)
108 {
109 using T = typename VectorType::Scalar;
110 T trace = tensor2ADC (dt);
111 T a[] = { dt[0]-trace, dt[1]-trace, dt[2]-trace };
112 return trace ?
113 sqrt ( (a[0]*a[0]+a[1]*a[1]+a[2]*a[2]+ T (2.0) * (dt[3]*dt[3]+dt[4]*dt[4]+dt[5]*dt[5])) /T (3.0)) / trace :
114 T (0.0);
115 }
116
117 }
118}
119
120#endif
#define WARN(msg)
Definition: exception.h:73
VectorType::Scalar tensor2RA(const VectorType &dt)
Definition: tensor.h:107
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > grad2bmatrix(const MatrixType &grad, bool dki=false)
Definition: tensor.h:30
void dwi2tensor(VectorTypeOut &dt, const MatrixType &binv, VectorTypeIn &dwi)
Definition: tensor.h:79
VectorType::Scalar tensor2FA(const VectorType &dt)
Definition: tensor.h:95
VectorType::Scalar tensor2ADC(const VectorType &dt)
Definition: tensor.h:88
Definition: base.h:24
InputImageType dwi