Developer documentation
Version 3.0.3-105-gd3941f44
tck2nodes.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_connectome_tck2nodes_h__
18#define __dwi_tractography_connectome_tck2nodes_h__
19
20
21#include "image.h"
22#include "types.h"
23#include "interp/linear.h"
24#include "interp/nearest.h"
25
27
29
30
31namespace MR {
32namespace DWI {
33namespace Tractography {
34namespace Connectome {
35
36
37
38
39// Provides a common interface for assigning a streamline to the relevant parcellation node pair
40// Note that this class is NOT copy-constructed, so derivative classes must be thread-safe
42
43 public:
44 Tck2nodes_base (const Image<node_t>& nodes_data, const bool pair) :
45 nodes (nodes_data),
47 pair (pair) { }
48
49 Tck2nodes_base (const Tck2nodes_base& that) = default;
50
51 virtual ~Tck2nodes_base() { }
52
53 NodePair operator() (const Tractography::Streamline<>& tck) const
54 {
55 assert (pair);
57 const node_t node_one = select_node (tck, v, false);
58 const node_t node_two = select_node (tck, v, true);
59 return std::make_pair (node_one, node_two);
60 }
61
62 bool operator() (const Streamline<>& tck, vector<node_t>& out) const
63 {
64 assert (!pair);
66 select_nodes (tck, v, out);
67 return true;
68 }
69
70 bool provides_pair() const { return pair; }
71
72
73 protected:
75 std::shared_ptr<Transform> transform;
76 const bool pair;
77
78 virtual node_t select_node (const Tractography::Streamline<>& tck, Image<node_t>& v, const bool end) const {
79 throw Exception ("Calling empty virtual function Tck2nodes_base::select_node()");
80 }
81
82 virtual void select_nodes (const Streamline<>& tck, Image<node_t>& v, vector<node_t>& out) const {
83 throw Exception ("Calling empty virtual function Tck2nodes_base::select_nodes()");
84 }
85
86 class voxel_type : public Eigen::Array<int,3,1>
88 public:
89 using Eigen::Array<int,3,1>::Array;
90 bool operator< (const Eigen::Array<int,3,1>& that) const {
91 return ((*this)[2] == that[2]) ? (((*this)[1] == that[1]) ? ((*this)[0] < that[0]) : ((*this)[1] < that[1])) : ((*this)[2] < that[2]);
92 }
93 };
94
95};
96
97
98
99
100// Specific implementations of assignment methodologies
101
102// Most basic: look up the voxel value at the voxel containing the streamline endpoint
104
105 public:
106 Tck2nodes_end_voxels (const Image<node_t>& nodes_data) :
107 Tck2nodes_base (nodes_data, true) { }
108
110 Tck2nodes_base (that) { }
111
113
114 private:
115 node_t select_node (const Tractography::Streamline<float>&, Image<node_t>&, const bool) const override;
116
117};
118
119
120
121
122
123// Radial search
125
126 public:
127 Tck2nodes_radial (const Image<node_t>& nodes_data, const default_type radius) :
128 Tck2nodes_base (nodes_data, true),
129 max_dist (radius),
130 max_add_dist (std::sqrt (Math::pow2 (0.5 * nodes.spacing(2)) + Math::pow2 (0.5 * nodes.spacing(1)) + Math::pow2 (0.5 * nodes.spacing(0))))
131 {
132 initialise_search ();
133 }
134
135 Tck2nodes_radial (const Tck2nodes_radial& that) :
136 Tck2nodes_base (that),
137 radial_search (that.radial_search),
138 max_dist (that.max_dist),
139 max_add_dist (that.max_add_dist) { }
140
142
143 private:
144 node_t select_node (const Tractography::Streamline<>&, Image<node_t>&, const bool) const override;
145
146 void initialise_search ();
147 vector<voxel_type> radial_search;
148 const default_type max_dist;
149 // Distances are sub-voxel from the precise streamline termination point, so the search order is imperfect.
150 // This parameter controls when to stop the radial search because no voxel within the search space can be closer
151 // than the closest voxel with non-zero node index processed thus far.
152 const default_type max_add_dist;
153
155
156};
157
158
159
160// Do a reverse-search from the track endpoints inwards
162{ MEMALIGN (Tck2nodes_revsearch)
163
164 public:
165 Tck2nodes_revsearch (const Image<node_t>& nodes_data, const default_type length) :
166 Tck2nodes_base (nodes_data, true),
167 max_dist (length) { }
168
170 Tck2nodes_base (that),
171 max_dist (that.max_dist) { }
172
174
175 private:
176 node_t select_node (const Tractography::Streamline<>&, Image<node_t>&, const bool) const override;
177
178 const default_type max_dist;
179
180};
181
182
183
184// Forward search - form a diamond-like shape emanating from the streamline endpoint in the direction of the tangent
186{ MEMALIGN(Tck2nodes_forwardsearch)
187
188 public:
189 Tck2nodes_forwardsearch (const Image<node_t>& nodes_data, const default_type length) :
190 Tck2nodes_base (nodes_data, true),
191 max_dist (length),
192 angle_limit (Math::pi_4) { } // 45 degree limit
193
195 Tck2nodes_base (that),
196 max_dist (that.max_dist),
197 angle_limit (that.angle_limit) { }
198
200
201 private:
202 node_t select_node (const Tractography::Streamline<>&, Image<node_t>&, const bool) const override;
203
204 const default_type max_dist;
205 const default_type angle_limit;
206
207 default_type get_cf (const Eigen::Vector3d&, const Eigen::Vector3d&, const voxel_type&) const;
208
209};
210
211
212
213
214
215// Class that obtains a list of all nodes overlapped by the streamline
217{ MEMALIGN(Tck2nodes_all_voxels)
218 public:
219 Tck2nodes_all_voxels (const Image<node_t>& nodes_data) :
220 Tck2nodes_base (nodes_data, false) { }
221
223 Tck2nodes_base (that) { }
224
226
227 private:
228 void select_nodes (const Streamline<>&, Image<node_t>&, vector<node_t>&) const override;
229
230};
231
232
233
234
235
236
237}
238}
239}
240}
241
242
243#endif
244
virtual node_t select_node(const Tractography::Streamline<> &tck, Image< node_t > &v, const bool end) const
Definition: tck2nodes.h:78
virtual void select_nodes(const Streamline<> &tck, Image< node_t > &v, vector< node_t > &out) const
Definition: tck2nodes.h:82
std::shared_ptr< Transform > transform
Definition: tck2nodes.h:75
default_type spacing(size_t axis) const
Definition: image.h:67
constexpr T pow2(const T &v)
Definition: math.h:53
constexpr double pi_4
Definition: math.h:42
std::pair< node_t, node_t > NodePair
Definition: connectome.h:41
MR::Connectome::node_t node_t
Definition: connectome.h:40
PointType::Scalar length(const vector< PointType > &tck)
Definition: streamline.h:134
Definition: base.h:24
double default_type
the default type used throughout MRtrix
Definition: types.h:228
#define MEMALIGN(...)
Definition: types.h:185