Developer documentation
Version 3.0.3-105-gd3941f44
fact.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_fact_h__
18#define __dwi_tractography_algorithms_fact_h__
19
20#include "interp/masked.h"
21#include "interp/nearest.h"
22
27
28
29namespace MR
30{
31 namespace DWI
32 {
33 namespace Tractography
34 {
35 namespace Algorithms
36 {
37
38 using namespace MR::DWI::Tractography::Tracking;
39
40 class FACT : public MethodBase { MEMALIGN(FACT)
41 public:
42 class Shared : public SharedBase { MEMALIGN(Shared)
43 public:
44 Shared (const std::string& diff_path, DWI::Tractography::Properties& property_set) :
45 SharedBase (diff_path, property_set),
46 num_vec (source.size(3)/3) {
47
48 if (source.size(3) % 3)
49 throw Exception ("Number of volumes in FACT algorithm input image should be a multiple of 3");
50
51 if (is_act() && act().backtrack())
52 throw Exception ("Backtracking not valid for deterministic algorithms");
53
54 if (rk4)
55 throw Exception ("4th-order Runge-Kutta integration not valid for FACT algorithm");
56
58 set_num_points();
59 set_cutoff (Defaults::cutoff_fixel * (is_act() ? Defaults::cutoff_act_multiplier : 1.0));
60 dot_threshold = std::cos (max_angle_1o);
61
62 properties["method"] = "FACT";
63 }
64
65 ~Shared () { }
66
67 size_t num_vec;
68 float dot_threshold;
69 };
70
71
72
73
74
75
76 FACT (const Shared& shared) :
77 MethodBase (shared),
78 S (shared),
79 source (S.source) { }
80
81 FACT (const FACT& that) :
82 MethodBase (that.S),
83 S (that.S),
84 source (S.source) { }
85
86 ~FACT () { }
87
88
89
90 bool init() override
91 {
92 if (!get_data (source)) return false;
93 if (S.init_dir.allFinite())
94 dir = S.init_dir;
95 else
96 dir = random_direction();
97 return select_fixel (dir) >= S.threshold;
98 }
99
100 term_t next () override
101 {
102 if (!get_data (source))
103 return EXIT_IMAGE;
104
105 const float max_norm = select_fixel (dir);
106
107 if (max_norm < S.threshold)
108 return MODEL;
109
110 pos += S.step_size * dir;
111 return CONTINUE;
112 }
113
114 float get_metric (const Eigen::Vector3f& position, const Eigen::Vector3f& direction) override
115 {
116 if (!get_data (source, position))
117 return 0.0;
118 Eigen::Vector3f d (direction);
119 return select_fixel (d);
120 }
121
122
123 protected:
124 const Shared& S;
126
127 float select_fixel (Eigen::Vector3f& d) const
128 {
129
130 int idx = -1;
131 float max_abs_dot = 0.0, max_dot = 0.0, max_norm = 0.0;
132
133 for (size_t n = 0; n < S.num_vec; ++n) {
134 Eigen::Vector3f v (values[3*n], values[3*n+1], values[3*n+2]);
135 float norm = v.norm();
136 float dot = v.dot(d) / norm;
137 float abs_dot = abs (dot);
138 if (abs_dot < S.dot_threshold) continue;
139 if (max_abs_dot < abs_dot) {
140 max_abs_dot = abs_dot;
141 max_dot = dot;
142 max_norm = norm;
143 idx = n;
144 }
145 }
146
147 if (idx < 0) return (0.0);
148
149 d = { values[3*idx], values[3*idx+1], values[3*idx+2] };
150 d.normalize();
151 if (max_dot < 0.0)
152 d = -d;
153
154 return max_norm;
155 }
156
157 };
158
159 }
160 }
161 }
162}
163
164#endif
165
166
Interp::Masked< Interp::Nearest< Image< float > > > source
Definition: fact.h:125
float select_fixel(Eigen::Vector3f &d) const
Definition: fact.h:127
Implicit masking for interpolator class.
Definition: masked.h:43
Definition: base.h:24
constexpr std::enable_if< std::is_arithmetic< X >::value &&std::is_unsigned< X >::value, X >::type abs(X x)
Definition: types.h:297
#define MEMALIGN(...)
Definition: types.h:185