Developer documentation
Version 3.0.3-105-gd3941f44
set.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_directions_set_h__
18#define __dwi_directions_set_h__
19
20
21#include <cstdint>
22
23#include "progressbar.h"
24#include "types.h"
26
27
28namespace MR {
29 namespace DWI {
30 namespace Directions {
31
32
33
34 using index_type = unsigned int;
35
36
37 class Set { MEMALIGN(Set)
38
39 public:
40
41 explicit Set (const std::string& path) :
42 dir_mask_bytes (0),
43 dir_mask_excess_bits (0),
44 dir_mask_excess_bits_mask (0)
45 {
46 auto matrix = load_matrix (path);
47
48 if (matrix.cols() != 2 && matrix.cols() != 3)
49 throw Exception ("Text file \"" + path + "\"does not contain directions as either azimuth-elevation pairs or XYZ triplets");
50
51 initialise (matrix);
52 }
53
54 explicit Set (const size_t d) :
55 dir_mask_bytes (0),
56 dir_mask_excess_bits (0),
57 dir_mask_excess_bits_mask (0)
58 {
59 Eigen::MatrixXd az_el_pairs;
60 load_predefined (az_el_pairs, d);
61 initialise (az_el_pairs);
62 }
63
64 Set (const Set& that) = default;
65
66 Set (Set&& that) :
67 unit_vectors (std::move (that.unit_vectors)),
68 adj_dirs (std::move (that.adj_dirs)),
69 dir_mask_bytes (that.dir_mask_bytes),
70 dir_mask_excess_bits (that.dir_mask_excess_bits),
71 dir_mask_excess_bits_mask (that.dir_mask_excess_bits_mask)
72 {
73 that.dir_mask_bytes = that.dir_mask_excess_bits = that.dir_mask_excess_bits_mask = 0;
74 }
75
76 // TODO Want to generalise this template, but it was causing weird compilation issues
77 template <class MatrixType>
78 explicit Set (const Eigen::Matrix<MatrixType, Eigen::Dynamic, Eigen::Dynamic>& m) :
79 dir_mask_bytes (0),
80 dir_mask_excess_bits (0),
81 dir_mask_excess_bits_mask (0)
82 {
83 initialise (m);
84 }
85
86 size_t size () const { return unit_vectors.size(); }
87 const Eigen::Vector3d& get_dir (const size_t i) const { assert (i < size()); return unit_vectors[i]; }
88 const vector<index_type>& get_adj_dirs (const size_t i) const { assert (i < size()); return adj_dirs[i]; }
89 bool dirs_are_adjacent (const index_type one, const index_type two) const {
90 assert (one < size());
91 assert (two < size());
92 for (const auto& i : adj_dirs[one]) {
93 if (i == two)
94 return true;
95 }
96 return false;
97 }
98
99 index_type get_min_linkage (const index_type one, const index_type two) const;
100
101 const vector<Eigen::Vector3d>& get_dirs() const { return unit_vectors; }
102 const Eigen::Vector3d& operator[] (const size_t i) const { assert (i < size()); return unit_vectors[i]; }
103
104
105 protected:
106
108 vector< vector<index_type> > adj_dirs; // Note: not self-inclusive
109
110
111 private:
112
113 size_t dir_mask_bytes, dir_mask_excess_bits;
114 uint8_t dir_mask_excess_bits_mask;
115 friend class Mask;
116
117 Set ();
118
119 void load_predefined (Eigen::MatrixXd& az_el_pairs, const size_t);
120 template <class MatrixType>
121 void initialise (const Eigen::Matrix<MatrixType, Eigen::Dynamic, Eigen::Dynamic>&);
122 void initialise_adjacency();
123 void initialise_mask();
124
125 };
126
127
128
129 template <class MatrixType>
130 void Set::initialise (const Eigen::Matrix<MatrixType, Eigen::Dynamic, Eigen::Dynamic>& in)
131 {
132 unit_vectors.resize (in.rows());
133 if (in.cols() == 2) {
134 for (size_t i = 0; i != size(); ++i) {
135 const default_type azimuth = in(i, 0);
136 const default_type elevation = in(i, 1);
137 const default_type sin_elevation = std::sin (elevation);
138 unit_vectors[i] = { std::cos (azimuth) * sin_elevation, std::sin (azimuth) * sin_elevation, std::cos (elevation) };
139 }
140 } else if (in.cols() == 3) {
141 for (size_t i = 0; i != size(); ++i)
142 unit_vectors[i] = { default_type(in(i,0)), default_type(in(i,1)), default_type(in(i,2)) };
143 } else {
144 assert (0);
145 }
146 initialise_adjacency();
147 initialise_mask();
148 }
149
150
151
152
153
154
155 class FastLookupSet : public Set { MEMALIGN(FastLookupSet)
156
157 public:
158
159 FastLookupSet (const std::string& path) :
160 Set (path)
161 {
162 initialise();
163 }
164
165 FastLookupSet (const size_t d) :
166 Set (d)
167 {
168 initialise();
169 }
170
172 Set (std::move (that)),
173 grid_lookup (std::move (that.grid_lookup)),
174 num_az_grids (that.num_az_grids),
175 num_el_grids (that.num_el_grids),
176 total_num_angle_grids (that.total_num_angle_grids),
177 az_grid_step (that.az_grid_step),
178 el_grid_step (that.el_grid_step),
179 az_begin (that.az_begin),
180 el_begin (that.el_begin) { }
181
182 index_type select_direction (const Eigen::Vector3d&) const;
183
184
185
186 private:
187
188 vector< vector<index_type> > grid_lookup;
189 unsigned int num_az_grids, num_el_grids, total_num_angle_grids;
190 default_type az_grid_step, el_grid_step;
191 default_type az_begin, el_begin;
192
193 FastLookupSet ();
194
195 index_type select_direction_slow (const Eigen::Vector3d&) const;
196
197 void initialise();
198
199 size_t dir2gridindex (const Eigen::Vector3d&) const;
200
201 void test_lookup() const;
202
203 };
204
205
206
207 }
208 }
209}
210
211#endif
212
vector< Eigen::Vector3d > unit_vectors
Definition: set.h:107
vector< vector< index_type > > adj_dirs
Definition: set.h:108
unsigned int index_type
Definition: set.h:34
Definition: base.h:24
Eigen::Matrix< ValueType, Eigen::Dynamic, Eigen::Dynamic > load_matrix(const std::string &filename)
read matrix data into an Eigen::Matrix filename
Definition: math.h:196
double default_type
the default type used throughout MRtrix
Definition: types.h:228