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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
/* Copyright (c) 2008-2022 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 "stride.h"
#include "header.h"
namespace MR
{
namespace Stride
{
using namespace App;
const OptionGroup Options = OptionGroup ("Stride options")
+ Option ("strides",
"specify the strides of the output data in memory; either "
"as a comma-separated list of (signed) integers, or "
"as a template image from which the strides shall be extracted and used. "
"The actual strides produced will depend on whether the output image "
"format can support it.")
+ Argument ("spec").type_various();
List& sanitise (List& current, const List& desired, const vector<ssize_t>& dims)
{
// remove duplicates
for (size_t i = 0; i < current.size()-1; ++i) {
if (dims[i] == 1) current[i] = 0;
if (!current[i]) continue;
for (size_t j = i+1; j < current.size(); ++j) {
if (!current[j]) continue;
if (abs (current[i]) == abs (current[j]))
current[j] = 0;
}
}
ssize_t desired_max = 0;
for (size_t i = 0; i < desired.size(); ++i)
if (abs (desired[i]) > desired_max)
desired_max = abs (desired[i]);
ssize_t in_max = 0;
for (size_t i = 0; i < current.size(); ++i)
if (abs (current[i]) > in_max)
in_max = abs (current[i]);
in_max += desired_max + 1;
for (size_t i = 0; i < current.size(); ++i)
if (dims[i] > 1 && desired[i])
current[i] = desired[i];
else if (current[i])
current[i] += current[i] < 0 ? -desired_max : desired_max;
else
current[i] = in_max++;
symbolise (current);
return current;
}
List __from_command_line (const List& current)
{
List strides;
auto opt = App::get_options ("strides");
if (!opt.size())
return strides;
try {
auto header = Header::open (std::string(opt[0][0]));
strides = get_symbolic (header);
}
catch (Exception& E) {
E.display (3);
try {
auto tmp = parse_ints<int> (opt[0][0]);
for (auto x : tmp)
strides.push_back (x);
}
catch (Exception& E) {
E.display(3);
throw Exception ("argument \"" + std::string(opt[0][0]) + "\" to option \"-strides\" is not a list of strides or an image");
}
}
if (strides.size() > current.size())
WARN ("too many axes supplied to -strides option - ignoring remaining strides");
strides.resize (current.size(), 0);
for (const auto x : strides)
if (abs(x) > int (current.size()))
throw Exception ("strides specified exceed image dimensions: got " + str(opt[0][0]) + ", but image has " + str(current.size()) + " axes");
for (size_t i = 0; i < strides.size()-1; ++i) {
if (!strides[1]) continue;
for (size_t j = i+1; j < strides.size(); ++j)
if (abs (strides[i]) == abs (strides[j]))
throw Exception ("duplicate entries provided to \"-strides\" option: " + str(opt[0][0]));
}
List prev = get_symbolic (current);
for (size_t i = 0; i < strides.size(); ++i)
if (strides[i] != 0)
prev[i] = 0;
prev = get_symbolic (prev);
ssize_t max_remaining = 0;
for (const auto x : prev)
if (abs(x) > max_remaining)
max_remaining = abs(x);
struct FindStride { NOMEMALIGN
FindStride (List::value_type value) : x (abs(value)) { }
bool operator() (List::value_type a) { return abs (a) == x; }
const List::value_type x;
};
ssize_t next_avail = 0;
for (ssize_t next = 1; next <= max_remaining; ++next) {
auto p = std::find_if (prev.begin(), prev.end(), FindStride (next));
assert (p != prev.end());
List::value_type s;
while (1) {
s = *p + ( *p > 0 ? next_avail : -next_avail );
if (std::find_if (strides.begin(), strides.end(), FindStride (s)) == strides.end())
break;
++next_avail;
}
strides[std::distance (prev.begin(), p)] = s;
}
return strides;
}
}
}
|