Developer documentation
Version 3.0.3-105-gd3941f44
base.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_seeding_base_h__
18#define __dwi_tractography_seeding_base_h__
19
20
21#include "image.h"
22#include "file/path.h"
23#include "algo/threaded_loop.h"
24
25
26
27// These constants set how many times a tracking algorithm should attempt to propagate
28// from a given seed point, based on the mechanism used to provide the seed point
29//
30// Update 12/03/2017: By default a greater number of attempts will be made to
31// find an appropriate direction in which to initiate tracking from all
32// seeding mechanisms
33//
34// Mechanisms that provide random seed locations
35#define MAX_TRACKING_SEED_ATTEMPTS_RANDOM 1000
36//
37// Dynamic seeding also provides the mean direction of the fixel, so only a small number of
38// attempts should be required to find a direction above the FOD amplitude threshold;
39// this will however depend on this threshold as well as the angular threshold
40#define MAX_TRACKING_SEED_ATTEMPTS_DYNAMIC 1000
41//
42// GM-WM interface seeding incurs a decent overhead when generating the seed points;
43// therefore want to make maximal use of each seed point generated, bearing in mind that
44// the FOD amplitudes may be small there.
45#define MAX_TRACKING_SEED_ATTEMPTS_GMWMI 1000
46//
47// Mechanisms that provide a fixed number of seed points; hence the maximum effort should
48// be made to find an appropriate tracking direction from every seed point provided
49#define MAX_TRACKING_SEED_ATTEMPTS_FIXED 1000
50
51
52
53
54namespace MR
55{
56 namespace DWI
57 {
58 namespace Tractography
59 {
60 namespace Seeding
61 {
62
63
64
65
66 template <class ImageType>
67 uint32_t get_count (ImageType& data)
68 {
69 std::atomic<uint32_t> count (0);
70 ThreadedLoop (data).run ([&] (ImageType& v) { if (v.value()) count.fetch_add (1, std::memory_order_relaxed); }, data);
71 return count;
72 }
73
74
75 template <class ImageType>
76 float get_volume (ImageType& data)
77 {
78 std::atomic<default_type> volume (0.0);
79 ThreadedLoop (data).run (
80 [&] (decltype(data)& v) {
81 const typename ImageType::value_type value = v.value();
82 if (value) {
83 default_type current = volume.load (std::memory_order_relaxed);
84 default_type target;
85 do {
86 target = current + value;
87 } while (!volume.compare_exchange_weak (current, target, std::memory_order_relaxed));
88 }
89 }, data);
90 return volume;
91 }
92
93
94
95
96 // Common interface for providing streamline seeds
97 class Base { MEMALIGN(Base)
98
99 public:
100 Base (const std::string& in, const std::string& desc, const size_t attempts) :
101 volume (0.0),
102 count (0),
103 type (desc),
104 name (Path::exists (in) ? Path::basename (in) : in),
105 max_attempts (attempts) { }
106
107 virtual ~Base() { }
108
109 default_type vol() const { return volume; }
110 uint32_t num() const { return count; }
111 bool is_finite() const { return count; }
112 const std::string& get_type() const { return type; }
113 const std::string& get_name() const { return name; }
114 size_t get_max_attempts() const { return max_attempts; }
115
116 virtual bool get_seed (Eigen::Vector3f&) const = 0;
117 virtual bool get_seed (Eigen::Vector3f& p, Eigen::Vector3f&) { return get_seed (p); }
118
119 friend inline std::ostream& operator<< (std::ostream& stream, const Base& B) {
120 stream << B.name;
121 return (stream);
122 }
123
124
125 protected:
126 // Finite seeds are defined by the number of seeds; non-limited are defined by volume
127 float volume;
128 uint32_t count;
129 mutable std::mutex mutex;
130 const std::string type; // Text describing the type of seed this is
131
132 private:
133 const std::string name; // Could be an image path, or spherical coordinates
134 const size_t max_attempts; // Maximum number of times the tracking algorithm should attempt to start from each provided seed point
135
136 };
137
138
139
140
141
142 }
143 }
144 }
145}
146
147#endif
148
friend std::ostream & operator<<(std::ostream &stream, const Base &B)
Definition: base.h:119
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
uint32_t get_count(ImageType &data)
Definition: base.h:67
float get_volume(ImageType &data)
Definition: base.h:76
std::pair< int, int > current()
Definition: gl.h:135
MR::default_type value_type
Definition: typedefs.h:33
std::string basename(const std::string &name)
Definition: path.h:60
bool exists(const std::string &path)
Definition: path.h:88
Definition: base.h:24
double default_type
the default type used throughout MRtrix
Definition: types.h:228
ThreadedLoopRunOuter< decltype(Loop(vector< size_t >()))> ThreadedLoop(const HeaderType &source, const vector< size_t > &outer_axes, const vector< size_t > &inner_axes)
Multi-threaded loop object.