File: fixelreorient.cpp

package info (click to toggle)
mrtrix3 3.0.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,300 kB
  • sloc: cpp: 130,470; python: 9,603; sh: 597; makefile: 62; xml: 47
file content (101 lines) | stat: -rw-r--r-- 4,207 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* Copyright (c) 2008-2025 the MRtrix3 contributors.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Covered Software is provided under this License on an "as is"
 * basis, without warranty of any kind, either expressed, implied, or
 * statutory, including, without limitation, warranties that the
 * Covered Software is free of defects, merchantable, fit for a
 * particular purpose or non-infringing.
 * See the Mozilla Public License v. 2.0 for more details.
 *
 * For more details, see http://www.mrtrix.org/.
 */

#include "command.h"
#include "progressbar.h"
#include "algo/loop.h"
#include "image.h"
#include "adapter/jacobian.h"
#include "registration/warp/helpers.h"

#include "fixel/helpers.h"
#include "fixel/keys.h"

using namespace MR;
using namespace App;

using Fixel::index_type;

void usage ()
{
  AUTHOR = "David Raffelt (david.raffelt@florey.edu.au)";

  SYNOPSIS = "Reorient fixel directions";

  DESCRIPTION
  + "Reorientation is performed by transforming the vector representing "
    "the fixel direction with the Jacobian (local affine transform) computed at each voxel in the warp, "
    "then re-normalising the vector.";

  ARGUMENTS
  + Argument ("fixel_in", "the input fixel directory").type_directory_in()
  + Argument ("warp", "a 4D deformation field used to perform reorientation. "
                      "Reorientation is performed by applying the Jacobian affine transform in each voxel in the warp, "
                      "then re-normalising the vector representing the fixel direction").type_image_in ()
  + Argument ("fixel_out", "the output fixel directory. If the the input and output directories are the same, the existing directions file will "
                           "be replaced (providing the -force option is supplied). If a new directory is supplied then the fixel directions and all "
                           "other fixel data will be copied to the new directory.").type_directory_out();
}


void run ()
{
  std::string input_fixel_directory = argument[0];
  Fixel::check_fixel_directory (input_fixel_directory);

  auto input_index_image = Fixel::find_index_header (input_fixel_directory).get_image <index_type>();

  Header warp_header = Header::open (argument[1]);
  Registration::Warp::check_warp (warp_header);
  check_dimensions (input_index_image, warp_header, 0, 3);
  Adapter::Jacobian<Image<float> > jacobian (warp_header.get_image<float>());

  std::string output_fixel_directory = argument[2];
  Fixel::check_fixel_directory (output_fixel_directory, true);

  // scratch buffer so inplace reorientation can be performed if desired
  Image<float> input_directions_image;
  std::string output_directions_filename;
  {
    auto tmp = Fixel::find_directions_header (input_fixel_directory).get_image<float>();
    input_directions_image = Image<float>::scratch(tmp);
    threaded_copy (tmp, input_directions_image);
    output_directions_filename = Path::basename(tmp.name());
  }

  auto output_directions_image = Image<float>::create (Path::join(output_fixel_directory, output_directions_filename), input_directions_image).with_direct_io();

  for (auto i = Loop ("reorienting fixel directions", input_index_image, 0, 3)(input_index_image, jacobian); i; ++i) {
    input_index_image.index(3) = 0;
    index_type num_fixels_in_voxel = input_index_image.value();
    if (num_fixels_in_voxel) {
      input_index_image.index(3) = 1;
      index_type index = input_index_image.value();
      Eigen::Matrix<float, 3, 3> transform = jacobian.value().inverse();
      for (index_type f = 0; f < num_fixels_in_voxel; ++f) {
        input_directions_image.index(0) = index + f;
        output_directions_image.index(0) = index + f;
        output_directions_image.row(1) = (transform * Eigen::Vector3f (input_directions_image.row(1))).normalized();
      }
    }
  }

  if (output_fixel_directory != input_fixel_directory) {
    Fixel::copy_index_file (input_fixel_directory, output_fixel_directory);
    Fixel::copy_all_data_files (input_fixel_directory, output_fixel_directory);
  }
}