17#ifndef __dwi_tractography_mapping_writer_h__
18#define __dwi_tractography_mapping_writer_h__
38 namespace Tractography {
67 virtual void finalise() { }
71 virtual bool operator() (
const SetVoxel&) {
return false; }
72 virtual bool operator() (
const SetVoxelDEC&) {
return false; }
73 virtual bool operator() (
const SetDixel&) {
return false; }
74 virtual bool operator() (
const SetVoxelTOD&) {
return false; }
92 std::unique_ptr<Image<float>>
counts;
101 template <
typename value_type>
110 auto loop =
Loop (buffer);
114 for (
auto l = loop (buffer); l; ++l )
115 buffer.value() = std::numeric_limits<value_type>::max();
125 for (
auto l = loop (buffer); l; ++l )
126 buffer.value() = std::numeric_limits<value_type>::max();
128 for (
auto l = loop (buffer); l; ++l )
129 buffer.value() = std::numeric_limits<value_type>::lowest();
153 void finalise ()
override {
155 auto loop =
Loop (buffer, 0, 3);
161 for (
auto l = loop (buffer, *
counts); l; ++l) {
162 const float total_weight =
counts->value();
164 auto value = get_dec();
167 value *= total_weight / norm;
175 for (
auto l = loop (buffer); l; ++l ) {
176 if (buffer.value() == std::numeric_limits<value_type>::max())
184 for (
auto l = loop (buffer, *
counts); l; ++l) {
190 for (
auto l = loop (buffer); l; ++l) {
191 auto value = get_dec();
192 if (
value.squaredNorm())
193 set_dec (
value.normalized());
198 for (
auto l = loop (buffer, *
counts); l; ++l) {
210 for (
auto l =
Loop (buffer) (buffer, *
counts); l; ++l) {
219 for (
auto l = loop (buffer); l; ++l) {
220 if (buffer.value() == -std::numeric_limits<value_type>::max())
224 for (
auto l =
Loop (buffer) (buffer); l; ++l) {
225 if (buffer.value() == -std::numeric_limits<value_type>::max())
232 throw Exception (
"Unknown / unhandled voxel statistic in MapWriter::finalise()");
240 bool operator() (
const SetVoxel& in)
override { receive_greyscale (in);
return true; }
241 bool operator() (
const SetVoxelDEC& in)
override { receive_dec (in);
return true; }
242 bool operator() (
const SetDixel& in)
override { receive_dixel (in);
return true; }
243 bool operator() (
const SetVoxelTOD& in)
override { receive_tod (in);
return true; }
245 bool operator() (
const Gaussian::SetVoxel& in)
override { receive_greyscale (in);
return true; }
247 bool operator() (
const Gaussian::SetDixel& in)
override { receive_dixel (in);
return true; }
256 template <
class Cont>
void receive_greyscale (
const Cont&);
257 template <
class Cont>
void receive_dec (
const Cont&);
258 template <
class Cont>
void receive_dixel (
const Cont&);
259 template <
class Cont>
void receive_tod (
const Cont&);
280 Eigen::Vector3d get_dec ();
281 void set_dec (
const Eigen::Vector3d&);
295 template <
typename value_type>
296 template <
class Cont>
300 for (
const auto& i : in) {
301 assign_pos_of (i).to (buffer);
304 switch (voxel_statistic) {
305 case V_SUM: add (weight, factor);
break;
306 case V_MIN: buffer.value() = std::min (
default_type (buffer.value()), factor);
break;
307 case V_MAX: buffer.value() = std::max (
default_type (buffer.value()), factor);
break;
309 add (weight, factor);
311 assign_pos_of (i).to (*counts);
312 counts->value() += weight;
315 throw Exception (
"Unknown / unhandled voxel statistic in MapWriter::receive_greyscale()");
322 template <
typename value_type>
323 template <
class Cont>
324 void MapWriter<value_type>::receive_dec (
const Cont& in)
326 assert (type ==
DEC);
327 for (
const auto& i : in) {
328 assign_pos_of (i).to (buffer);
331 auto scaled_colour = i.get_colour();
332 scaled_colour *= factor;
333 const auto current_value = get_dec();
334 switch (voxel_statistic) {
336 set_dec (current_value + (scaled_colour * weight));
338 assign_pos_of (i).to (*counts);
339 counts->value() += weight;
342 if (scaled_colour.squaredNorm() < current_value.squaredNorm())
343 set_dec (scaled_colour);
346 set_dec (current_value + (scaled_colour * weight));
349 if (scaled_colour.squaredNorm() > current_value.squaredNorm())
350 set_dec (scaled_colour);
353 throw Exception (
"Unknown / unhandled voxel statistic in MapWriter::receive_dec()");
360 template <
typename value_type>
361 template <
class Cont>
362 void MapWriter<value_type>::receive_dixel (
const Cont& in)
364 assert (type ==
DIXEL);
365 for (
const auto& i : in) {
366 assign_pos_of (i, 0, 3).to (buffer);
367 buffer.index(3) = i.get_dir();
370 switch (voxel_statistic) {
371 case V_SUM: add (weight, factor);
break;
372 case V_MIN: buffer.value() = std::min (
default_type (buffer.value()), factor);
break;
373 case V_MAX: buffer.value() = std::max (
default_type (buffer.value()), factor);
break;
375 add (weight, factor);
377 assign_pos_of (i, 0, 3).to (*counts);
378 counts->index(3) = i.get_dir();
379 counts->value() += weight;
382 throw Exception (
"Unknown / unhandled voxel statistic in MapWriter::receive_dixel()");
389 template <
typename value_type>
390 template <
class Cont>
391 void MapWriter<value_type>::receive_tod (
const Cont& in)
393 assert (type ==
TOD);
395 for (
const auto& i : in) {
396 assign_pos_of (i, 0, 3).to (buffer);
401 assign_pos_of (i, 0, 3).to (*counts);
402 switch (voxel_statistic) {
405 sh_coefs[
index] += i.get_tod()[
index] * weight * factor;
411 if (factor < counts->
value()) {
412 counts->value() = factor;
413 auto tod = i.get_tod();
420 if (factor > counts->value()) {
421 counts->value() = factor;
422 auto tod = i.get_tod();
430 sh_coefs[
index] += i.get_tod()[
index] * weight * factor;
432 counts->value() += weight;
435 throw Exception (
"Unknown / unhandled voxel statistic in MapWriter::receive_tod()");
446 if (weight && factor)
447 buffer.value() =
true;
450 template <
typename value_type>
453 buffer.value() += weight * factor;
460 template <
typename value_type>
461 Eigen::Vector3d MapWriter<value_type>::get_dec ()
463 assert (type ==
DEC);
464 Eigen::Vector3d
value;
465 buffer.index(3) = 0;
value[0] = buffer.value();
466 buffer.index(3)++;
value[1] = buffer.value();
467 buffer.index(3)++;
value[2] = buffer.value();
471 template <
typename value_type>
472 void MapWriter<value_type>::set_dec (
const Eigen::Vector3d&
value)
474 assert (type ==
DEC);
475 buffer.index(3) = 0; buffer.value() =
value[0];
476 buffer.index(3)++; buffer.value() =
value[1];
477 buffer.index(3)++; buffer.value() =
value[2];
484 template <
typename value_type>
487 assert (type ==
TOD);
488 sh_coefs.resize (buffer.size(3));
489 for (
auto l =
Loop (3) (buffer); l; ++l)
490 sh_coefs[buffer.index(3)] = buffer.value();
493 template <
typename value_type>
496 assert (type ==
TOD);
497 assert (sh_coefs.size() == buffer.size(3));
498 for (
auto l =
Loop (3) (buffer); l; ++l)
499 buffer.value() = sh_coefs[buffer.index(3)];
const std::string output_image_name
const vox_stat_t voxel_statistic
std::unique_ptr< Image< float > > counts
functions and classes related to image data input/output
FORCE_INLINE LoopAlongAxes Loop()
VectorType::Scalar value(const VectorType &coefs, typename VectorType::Scalar cos_elevation, typename VectorType::Scalar cos_azimuth, typename VectorType::Scalar sin_azimuth, int lmax)
const char * writer_dims[]
MR::default_type value_type
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
std::string str(const T &value, int precision=0)
std::enable_if< is_adapter_type< typenamestd::remove_reference< ImageType >::type >::value, std::string >::type save(ImageType &&x, const std::string &filename, bool use_multi_threading=true)
save contents of an existing image to file (for debugging only)