Developer documentation
Version 3.0.3-105-gd3941f44
calibrator.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_iFOD_calibrator_h__
18#define __dwi_tractography_algorithms_iFOD_calibrator_h__
19
20#include "types.h"
21#include "math/SH.h"
24
25#define SQRT_3_OVER_2 0.866025403784439
26#define NUM_CALIBRATE 1000
27
28namespace MR {
29 namespace DWI {
30 namespace Tractography {
31 namespace Algorithms {
32
33 using namespace MR::DWI::Tractography::Tracking;
34
35 FORCE_INLINE vector<Eigen::Vector3f> direction_grid (float max_angle, float spacing)
36 {
37 const float maxR = Math::pow2 (max_angle / spacing);
39 ssize_t extent = std::ceil (max_angle / spacing);
40
41 for (ssize_t i = -extent; i <= extent; ++i) {
42 for (ssize_t j = -extent; j <= extent; ++j) {
43 float x = i + 0.5*j, y = SQRT_3_OVER_2*j;
44 float n = Math::pow2(x) + Math::pow2(y);
45 if (n > maxR)
46 continue;
47 n = spacing * std::sqrt (n);
48 float z = std::cos (n);
49 if (n) n = spacing * std::sin (n) / n;
50 list.push_back ({ n*x, n*y, z });
51 }
52 }
53
54 return (list);
55 }
56
57 namespace {
58 class Pair { NOMEMALIGN
59 public:
60 Pair (float elevation, float amplitude) : el (elevation), amp (amplitude) { }
61 float el, amp;
62 };
63 }
64
65 template <class Method>
66 void calibrate (Method& method)
67 {
68 typename Method::Calibrate calibrate_func (method);
69 const float sqrt3 = std::sqrt (3.0);
70 const float max_angle = std::isfinite (method.S.max_angle_ho) ? method.S.max_angle_ho : method.S.max_angle_1o;
71
72 vector<Pair> amps;
73 for (float el = 0.0; el < max_angle; el += 0.001) {
74 amps.push_back (Pair (el, calibrate_func (el)));
75 if (!std::isfinite (amps.back().amp) || amps.back().amp <= 0.0) break;
76 }
77 float zero = amps.back().el;
78
79 float N_min = Inf, theta_min = NaN, ratio = NaN;
80 for (size_t i = 1; i < amps.size(); ++i) {
81 float N = Math::pow2 (max_angle);
82 float Ns = N * (1.0+amps[0].amp/amps[i].amp)/(2.0*Math::pow2(zero));
83 auto dirs = direction_grid (max_angle + amps[i].el, sqrt3*amps[i].el);
84 N = Ns+dirs.size();
85 //std::cout << amps[i].el << " " << amps[i].amp << " " << Ns << " " << dirs.size() << " " << Ns+dirs.size() << "\n";
86 if (N > 0.0 && N < N_min) {
87 N_min = N;
88 theta_min = amps[i].el;
89 ratio = amps[0].amp / amps[i].amp;
90 }
91 }
92
93 method.calibrate_list = direction_grid (max_angle+theta_min, sqrt3*theta_min);
94 method.calibrate_ratio = ratio;
95
96 INFO ("rejection sampling will use " + str (method.calibrate_list.size())
97 + " directions with a ratio of " + str (method.calibrate_ratio) + " (predicted number of samples per step = " + str (N_min) + ")");
98 }
99
100
101 }
102 }
103 }
104}
105
106#endif
107
108
109
110
#define SQRT_3_OVER_2
Definition: calibrator.h:25
float amp
Definition: calibrator.h:61
float el
Definition: calibrator.h:61
#define INFO(msg)
Definition: exception.h:74
constexpr T pow2(const T &v)
Definition: math.h:53
constexpr I ceil(const T x)
template function with cast to different type
Definition: math.h:86
#define NOMEMALIGN
Definition: memory.h:22
void calibrate(Method &method)
Definition: calibrator.h:66
FORCE_INLINE vector< Eigen::Vector3f > direction_grid(float max_angle, float spacing)
Definition: calibrator.h:35
Definition: base.h:24
std::string str(const T &value, int precision=0)
Definition: mrtrix.h:247
constexpr default_type Inf
Definition: types.h:231
constexpr default_type NaN
Definition: types.h:230
#define FORCE_INLINE
Definition: types.h:156