Developer documentation
Version 3.0.3-105-gd3941f44
sd_stream.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_tractography_algorithms_sd_stream_h__
18#define __dwi_tractography_algorithms_sd_stream_h__
19
20#include "math/SH.h"
25
26
27namespace MR {
28namespace DWI {
29namespace Tractography {
30namespace Algorithms {
31
33
35 public:
36 class Shared : public SharedBase { MEMALIGN(Shared)
37 public:
38 Shared (const std::string& diff_path, DWI::Tractography::Properties& property_set) :
39 SharedBase (diff_path, property_set),
40 lmax (Math::SH::LforN (source.size(3)))
41 {
42 try {
44 } catch (Exception& e) {
45 e.display();
46 throw Exception ("Algorithm SD_STREAM expects as input a spherical harmonic (SH) image");
47 }
48
49 if (is_act() && act().backtrack())
50 throw Exception ("Backtracking not valid for deterministic algorithms");
51
54 rk4);
55 dot_threshold = std::cos (max_angle_1o);
56 set_num_points();
57 set_cutoff (Defaults::cutoff_fod * (is_act() ? Defaults::cutoff_act_multiplier : 1.0));
58
59 properties["method"] = "SDStream";
60
61 bool precomputed = true;
62 properties.set (precomputed, "sh_precomputed");
63 if (precomputed)
64 precomputer = new Math::SH::PrecomputedAL<float> (lmax);
65 }
66
67 ~Shared () {
68 if (precomputer)
69 delete precomputer;
70 }
71
72 float dot_threshold;
73 size_t lmax;
75
76 };
77
78
79
80
81
82
83 SDStream (const Shared& shared) :
84 MethodBase (shared),
85 S (shared),
86 source (S.source) { }
87
88 SDStream (const SDStream& that) :
89 MethodBase (that.S),
90 S (that.S),
91 source (S.source) { }
92
93
94 ~SDStream () { }
95
96
97
98 bool init() override
99 {
100 if (!get_data (source))
101 return (false);
102
103 if (!S.init_dir.allFinite()) {
104 if (!dir.allFinite())
105 dir = random_direction();
106 }
107 else
108 dir = S.init_dir;
109
110 dir.normalize();
111 if (!find_peak())
112 return false;
113
114 return true;
115 }
116
117
118
119 term_t next () override
120 {
121 if (!get_data (source))
122 return EXIT_IMAGE;
123
124 const Eigen::Vector3f prev_dir (dir);
125
126 if (!find_peak())
127 return MODEL;
128
129 if (prev_dir.dot (dir) < S.dot_threshold)
130 return HIGH_CURVATURE;
131
132 pos += dir * S.step_size;
133 return CONTINUE;
134 }
135
136
137 float get_metric (const Eigen::Vector3f& position, const Eigen::Vector3f& direction) override
138 {
139 if (!get_data (source, position))
140 return 0.0;
141 return FOD (direction);
142 }
143
144
145 protected:
146 const Shared& S;
148
149 float find_peak ()
150 {
151 float FOD = Math::SH::get_peak (values, S.lmax, dir, S.precomputer);
152 if (!std::isfinite (FOD) || FOD < S.threshold)
153 FOD = 0.0;
154 return FOD;
155 }
156
157 float FOD (const Eigen::Vector3f& d) const
158 {
159 return (S.precomputer ?
160 S.precomputer->value (values, d) :
161 Math::SH::value (values, d, S.lmax)
162 );
163 }
164
165
166};
167
168}
169}
170}
171}
172
173#endif
174
175
float FOD(const Eigen::Vector3f &d) const
Definition: sd_stream.h:157
Interpolator< Image< float > >::type source
Definition: sd_stream.h:147
Precomputed Associated Legrendre Polynomials - used to speed up SH calculation.
Definition: SH.h:400
constexpr double e
Definition: math.h:39
void check(const ImageType &H)
convenience function to check if an input image can contain SH coefficients
Definition: SH.h:745
size_t LforN(int N)
returns the largest lmax given N parameters
Definition: SH.h:70
VectorType::Scalar get_peak(const VectorType &sh, int lmax, UnitVectorType &unit_init_dir, PrecomputedAL< typename VectorType::Scalar > *precomputer=nullptr)
estimate direction & amplitude of SH peak
Definition: SH.h:515
VectorType::Scalar value(const VectorType &coefs, typename VectorType::Scalar cos_elevation, typename VectorType::Scalar cos_azimuth, typename VectorType::Scalar sin_azimuth, int lmax)
Definition: SH.h:233
Definition: base.h:24
#define MEMALIGN(...)
Definition: types.h:185