Developer documentation
Version 3.0.3-105-gd3941f44
track_contribution.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_sift_track_contribution_h__
18#define __dwi_tractography_sift_track_contribution_h__
19
20
21#include <cstdint>
22
23#include "header.h"
24#include "min_mem_array.h"
25
26#include "math/math.h"
27
28
29namespace MR
30{
31 namespace DWI
32 {
33 namespace Tractography
34 {
35 namespace SIFT
36 {
37
38
39
40
42 { MEMALIGN(Track_fixel_contribution)
43 public:
44 Track_fixel_contribution (const uint32_t fixel_index, const float length)
45 {
46 const uint32_t length_as_int = std::min (uint32_t(255), uint32_t(std::round (scale_to_storage * length)));
47 data = (fixel_index & 0x00FFFFFF) | (length_as_int << 24);
48 }
49
51 data (0) { }
52
53 uint32_t get_fixel_index() const { return (data & 0x00FFFFFF); }
54 float get_length() const { return (uint32_t((data & 0xFF000000) >> 24) * scale_from_storage); }
55
56
57 bool add (const float length)
58 {
59 // Allow summing of multiple contributions to a fixel, UNLESS it would cause truncation, in which
60 // case keep them separate
61 const uint32_t increment = std::round (scale_to_storage * length);
62 const uint32_t existing = (data & 0xFF000000) >> 24;
63 if (existing + increment > 255)
64 return false;
65 data = (data & 0x00FFFFFF) | ((existing + increment) << 24);
66 return true;
67 }
68
69
70 static void set_scaling (const Header& H)
71 {
72 const float max_length = std::sqrt (Math::pow2 (H.spacing(0)) + Math::pow2 (H.spacing(1)) + Math::pow2 (H.spacing(2)));
73 // TODO Newer mapping performs chordal approximation of length
74 // Should technically take this into account when setting scaling
75 scale_to_storage = 255.0 / max_length;
76 scale_from_storage = max_length / 255.0;
77 min_length_for_storage = 0.5 / scale_to_storage;
78 }
79
80
81 // Minimum length that will be non-zero once converted to an integer for word-sharing storage
82 static float min() { return min_length_for_storage; }
83
84
85 private:
86 uint32_t data;
87
88 static float scale_to_storage, scale_from_storage, min_length_for_storage;
89
90 };
91
92
93
94
95/*
96// This is a 'safe' version of Track_fixel_contribution that does not use byte-sharing, but requires double the RAM
97// Simply comment the class above and un-comment this one to use
98class Track_fixel_contribution
99{ MEMALIGN(Track_fixel_contribution)
100 public:
101 Track_fixel_contribution (const uint32_t fixel_index, const float length) :
102 fixel (fixel_index),
103 length (length) { }
104
105 Track_fixel_contribution() :
106 fixel (0),
107 length (0.0) { }
108
109
110 bool add (const float length) { value += length; return true; }
111
112 uint32_t get_fixel_index() const { return fixel; }
113 float get_length() const { return length; }
114
115
116 static void set_scaling (const Image::Info& in) { min_length_for_storage = 0.0; }
117 static float min() { return min_length_for_storage; }
118
119
120 private:
121 uint32_t fixel;
122 float length;
123
124 static float scale_to_storage, scale_from_storage, min_length_for_storage;
125
126};
127*/
128
129
130
131 class TrackContribution : public Min_mem_array<Track_fixel_contribution>
132 { MEMALIGN(TrackContribution)
133
134 public:
135 TrackContribution (const vector<Track_fixel_contribution>& in, const float c, const float l) :
137 total_contribution (c),
138 total_length (l) { }
139
142 total_contribution (0.0),
143 total_length (0.0) { }
144
146
147 float get_total_contribution() const { return total_contribution; }
148 float get_total_length () const { return total_length; }
149
150 private:
151 const float total_contribution, total_length;
152
153 };
154
155
156
157
158 }
159 }
160 }
161}
162
163
164#endif
165
166
constexpr T pow2(const T &v)
Definition: math.h:53
constexpr I round(const T x)
Definition: math.h:64
PointType::Scalar length(const vector< PointType > &tck)
Definition: streamline.h:134
Definition: base.h:24
Eigen::MatrixXd H