17#ifndef __dwi_tractography_mapping_voxel_h__
18#define __dwi_tractography_mapping_voxel_h__
31 namespace Tractography {
38 inline Eigen::Vector3i
round (
const Eigen::Matrix<T, 3, 1>& p)
40 assert (p.allFinite());
44 template <
class HeaderType>
45 inline bool check (
const Eigen::Vector3i& V,
const HeaderType&
H)
47 return (V[0] >= 0 && V[0] <
H.size(0) && V[1] >= 0 && V[1] <
H.size(1) && V[2] >= 0 && V[2] <
H.size(2));
50 inline Eigen::Vector3d
vec2DEC (
const Eigen::Vector3d& d)
52 return {
abs(d[0]),
abs(d[1]),
abs(d[2]) };
58 class Voxel :
public Eigen::Vector3i
61 Voxel (
const int x,
const int y,
const int z) : Eigen::Vector3i (x,y,z), length (1.0f) { }
62 Voxel (
const Eigen::Vector3i& that) : Eigen::Vector3i (that), length (1.0f) { }
63 Voxel (
const Eigen::Vector3i& v,
const default_type l) : Eigen::Vector3i (v), length (l) { }
64 Voxel () : length (0.0) { setZero(); }
65 bool operator< (
const Voxel& V)
const {
return (((*
this)[2] == V[2]) ? (((*
this)[1] == V[1]) ? ((*
this)[0] < V[0]) : ((*
this)[1] < V[1])) : ((*
this)[2] < V[2])); }
67 void operator+= (
const default_type l)
const { length += l; }
68 void normalize()
const { length = 1.0; }
82 colour (0.0, 0.0, 0.0) { }
84 VoxelDEC (
const Eigen::Vector3i& V) :
86 colour (0.0, 0.0, 0.0) { }
88 VoxelDEC (
const Eigen::Vector3i& V,
const Eigen::Vector3d& d) :
92 VoxelDEC (
const Eigen::Vector3i& V,
const Eigen::Vector3d& d,
const float l) :
96 VoxelDEC& operator= (
const VoxelDEC& V) { Voxel::operator= (V); colour = V.colour;
return *
this; }
97 VoxelDEC& operator= (
const Eigen::Vector3i& V) { Voxel::operator= (V); colour = { 0.0, 0.0, 0.0 };
return *
this; }
100 bool operator== (
const VoxelDEC& V)
const {
return Voxel::operator== (V); }
101 bool operator< (
const VoxelDEC& V)
const {
return Voxel::operator< (V); }
103 void normalize()
const { colour.normalize(); Voxel::normalize(); }
104 void set_dir (
const Eigen::Vector3d& i) { colour =
vec2DEC (i); }
105 void add (
const Eigen::Vector3d& i,
const default_type l)
const { Voxel::operator+= (l); colour +=
vec2DEC (i); }
106 void operator+= (
const Eigen::Vector3d& i)
const { Voxel::operator+= (1.0); colour +=
vec2DEC (i); }
107 const Eigen::Vector3d& get_colour()
const {
return colour; }
110 mutable Eigen::Vector3d colour;
124 dir (0.0, 0.0, 0.0) { }
126 VoxelDir (
const Eigen::Vector3i& V) :
128 dir (0.0, 0.0, 0.0) { }
130 VoxelDir (
const Eigen::Vector3i& V,
const Eigen::Vector3d& d) :
138 VoxelDir& operator= (
const VoxelDir& V) { Voxel::operator= (V); dir = V.dir;
return *
this; }
139 VoxelDir& operator= (
const Eigen::Vector3i& V) { Voxel::operator= (V); dir = { 0.0, 0.0, 0.0 };
return *
this; }
141 bool operator== (
const VoxelDir& V)
const {
return Voxel::operator== (V); }
142 bool operator< (
const VoxelDir& V)
const {
return Voxel::operator< (V); }
144 void normalize()
const { dir.normalize(); Voxel::normalize(); }
145 void set_dir (
const Eigen::Vector3d& i) { dir = i; }
146 void add (
const Eigen::Vector3d& i,
const default_type l)
const { Voxel::operator+= (l); dir += i * (dir.dot(i) < 0.0 ? -1.0 : 1.0); }
147 void operator+= (
const Eigen::Vector3d& i)
const { Voxel::operator+= (1.0); dir += i * (dir.dot(i) < 0.0 ? -1.0 : 1.0); }
148 const Eigen::Vector3d& get_dir()
const {
return dir; }
151 mutable Eigen::Vector3d dir;
169 Dixel (
const Eigen::Vector3i& V) :
173 Dixel (
const Eigen::Vector3i& V,
const dir_index_type b) :
181 void set_dir (
const size_t b) { dir = b; }
183 bool valid()
const {
return (dir != invalid); }
184 dir_index_type get_dir()
const {
return dir; }
186 Dixel& operator= (
const Dixel& V) { Voxel::operator= (V); dir = V.dir;
return *
this; }
187 Dixel& operator= (
const Eigen::Vector3i& V) { Voxel::operator= (V); dir = invalid;
return *
this; }
188 bool operator== (
const Dixel& V)
const {
return (Voxel::operator== (V) ? (dir == V.dir) :
false); }
189 bool operator< (
const Dixel& V)
const {
return (Voxel::operator== (V) ? (dir < V.dir) : Voxel::operator< (V)); }
190 void operator+= (
const float l)
const { Voxel::operator+= (l); }
195 static const dir_index_type invalid;
208 using vector_type = Eigen::Matrix<default_type, Eigen::Dynamic, 1>;
214 VoxelTOD (
const Eigen::Vector3i& V) :
226 VoxelTOD& operator= (
const VoxelTOD& V) { Voxel::operator= (V); sh_coefs = V.sh_coefs;
return (*
this); }
227 VoxelTOD& operator= (
const Eigen::Vector3i& V) { Voxel::operator= (V); sh_coefs.resize(0);
return (*
this); }
230 bool operator== (
const VoxelTOD& V)
const {
return Voxel::operator== (V); }
231 bool operator< (
const VoxelTOD& V)
const {
return Voxel::operator< (V); }
233 void normalize()
const
236 sh_coefs *= multiplier;
239 void set_tod (
const vector_type& i) { sh_coefs = i; }
242 assert (i.size() == sh_coefs.size());
245 Voxel::operator+= (l);
249 assert (i.size() == sh_coefs.size());
251 Voxel::operator+= (1.0);
253 const vector_type& get_tod()
const {
return sh_coefs; }
291 iterator existing = std::set<Voxel>::find (v);
292 if (existing == std::set<Voxel>::end())
293 std::set<Voxel>::insert (v);
295 (*existing) += v.get_length();
299 const Voxel temp (v, l);
314 iterator existing = std::set<VoxelDEC>::find (v);
315 if (existing == std::set<VoxelDEC>::end())
316 std::set<VoxelDEC>::insert (v);
318 existing->add (v.get_colour(), v.get_length());
320 inline void insert (
const Eigen::Vector3i& v,
const Eigen::Vector3d& d)
341 iterator existing = std::set<VoxelDir>::find (v);
342 if (existing == std::set<VoxelDir>::end())
343 std::set<VoxelDir>::insert (v);
345 existing->add (v.get_dir(), v.get_length());
347 inline void insert (
const Eigen::Vector3i& v,
const Eigen::Vector3d& d)
369 iterator existing = std::set<Dixel>::find (v);
370 if (existing == std::set<Dixel>::end())
371 std::set<Dixel>::insert (v);
373 (*existing) += v.get_length();
377 const Dixel temp (v, d);
382 const Dixel temp (v, d, l);
400 iterator existing = std::set<VoxelTOD>::find (v);
401 if (existing == std::set<VoxelTOD>::end())
402 std::set<VoxelTOD>::insert (v);
404 (*existing) += v.get_tod();
Array & operator=(const MR::Helper::ConstRow< ImageType > &row)
void insert(const Dixel &v)
void insert(const Eigen::Vector3i &v, const dir_index_type d)
Dixel::dir_index_type dir_index_type
void insert(const Eigen::Vector3i &v, const dir_index_type d, const default_type l)
void insert(const Eigen::Vector3i &v, const Eigen::Vector3d &d)
void insert(const VoxelDEC &v)
void insert(const Eigen::Vector3i &v, const Eigen::Vector3d &d, const default_type l)
void insert(const Eigen::Vector3i &v, const Eigen::Vector3d &d, const default_type l)
void insert(const Eigen::Vector3i &v, const Eigen::Vector3d &d)
void insert(const VoxelDir &v)
void insert(const Voxel &v)
void insert(const Eigen::Vector3i &v, const default_type l)
void insert(const Eigen::Vector3i &v, const vector_type &t)
VoxelTOD::vector_type vector_type
void insert(const VoxelTOD &v)
void insert(const Eigen::Vector3i &v, const vector_type &t, const default_type l)
Eigen::Vector3i round(const Eigen::Matrix< T, 3, 1 > &p)
bool check(const Eigen::Vector3i &V, const HeaderType &H)
std::ostream & operator<<(std::ostream &, const Voxel &)
Eigen::Vector3d vec2DEC(const Eigen::Vector3d &d)
Eigen::Array< value_type, Eigen::Dynamic, 1 > vector_type
void set(HeaderType &header, const List &stride)
set the strides of header from a vector<ssize_t>
double default_type
the default type used throughout MRtrix
constexpr std::enable_if< std::is_arithmetic< X >::value &&std::is_unsigned< X >::value, X >::type abs(X x)