forked from MRtrix3/mrtrix3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesting_diff_peaks.cpp
86 lines (71 loc) · 3.09 KB
/
testing_diff_peaks.cpp
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
/*
* Copyright (c) 2008-2018 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/
*
* MRtrix3 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* For more details, see http://www.mrtrix.org/
*/
#include "command.h"
#include "progressbar.h"
#include "datatype.h"
#include "image.h"
#include "algo/threaded_loop.h"
using namespace MR;
using namespace App;
void usage ()
{
AUTHOR = "Robert E. Smith ([email protected])";
SYNOPSIS = "Compare two peak images for differences, within specified tolerance";
ARGUMENTS
+ Argument ("peaks1", "a peaks image.").type_image_in ()
+ Argument ("peaks2", "another peaks image.").type_image_in ()
+ Argument ("tolerance", "the dot product difference to consider acceptable").type_float (0.0);
}
void run ()
{
auto in1 = Image<double>::open (argument[0]);
auto in2 = Image<double>::open (argument[1]);
check_dimensions (in1, in2);
if (in1.ndim() != 4)
throw Exception ("images \"" + in1.name() + "\" and \"" + in2.name() + "\" are not 4D");
if (in1.size(3) % 3)
throw Exception ("images \"" + in1.name() + "\" and \"" + in2.name() + "\" do not contain XYZ peak directions");
for (size_t i = 0; i < in1.ndim(); ++i) {
if (std::isfinite (in1.size(i)))
if (in1.size(i) != in2.size(i))
throw Exception ("images \"" + in1.name() + "\" and \"" + in2.name() + "\" do not have matching voxel spacings " +
str(in1.size(i)) + " vs " + str(in2.size(i)));
}
for (size_t i = 0; i < 3; ++i) {
for (size_t j = 0; j < 4; ++j) {
if (abs (in1.transform().matrix()(i,j) - in2.transform().matrix()(i,j)) > 0.0001)
throw Exception ("images \"" + in1.name() + "\" and \"" + in2.name() + "\" do not have matching header transforms "
+ "\n" + str(in1.transform().matrix()) + "vs \n " + str(in2.transform().matrix()) + ")");
}
}
double tol = argument[2];
ThreadedLoop (in1, 0, 3)
.run ([&tol] (decltype(in1)& a, decltype(in2)& b)
{
for (size_t i = 0; i != size_t(a.size(3)); i += 3) {
Eigen::Vector3 veca, vecb;
for (size_t axis = 0; axis != 3; ++axis) {
a.index(3) = b.index(3) = i + axis;
veca[axis] = a.value();
vecb[axis] = b.value();
}
const double norma = veca.norm(), normb = vecb.norm();
veca.normalize(); vecb.normalize();
const double dp = abs (veca.dot (vecb));
if (1.0 - dp > tol || abs (norma - normb) > tol)
throw Exception ("images \"" + a.name() + "\" and \"" + b.name() + "\" do not match within specified precision of " + str(tol) + " ( [" + str(veca.transpose().cast<float>()) + "] vs [" + str(vecb.transpose().cast<float>()) + "], norms [" + str(norma) + " " + str(normb) + "], dot product = " + str(dp) + ")");
}
}, in1, in2);
CONSOLE ("data checked OK");
}