Developer documentation
Version 3.0.3-105-gd3941f44
robust_estimators.h
Go to the documentation of this file.
1/* Copyright (c) 2008-2022 the MRtrix3 contributors.
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 *
7 * Covered Software is provided under this License on an "as is"
8 * basis, without warranty of any kind, either expressed, implied, or
9 * statutory, including, without limitation, warranties that the
10 * Covered Software is free of defects, merchantable, fit for a
11 * particular purpose or non-infringing.
12 * See the Mozilla Public License v. 2.0 for more details.
13 *
14 * For more details, see http://www.mrtrix.org/.
15 */
16
17#ifndef __registration_metric_robust_estimators_h__
18#define __registration_metric_robust_estimators_h__
19
20namespace MR
21{
22 namespace Math
23 {
24 template <typename T> inline int sgn(T val) {
25 return (T(0) < val) - (val < T(0));
26 }
27 }
28 namespace Registration
29 {
30 namespace Metric
31 {
32 class L1 { NOMEMALIGN
33 public:
35 default_type& residual,
36 default_type& slope) {
37 residual = abs(x);
38 slope = Math::sgn(x);
39 }
40
41 void operator() (const Eigen::Matrix<default_type, Eigen::Dynamic, 1>& x,
42 Eigen::Matrix<default_type, Eigen::Dynamic, 1>& residual,
43 Eigen::Matrix<default_type, Eigen::Dynamic, 1>& slope) {
44 const ssize_t len = x.size();
45 residual = x.cwiseAbs();
46 slope.resize(len);
47 for (ssize_t i = 0; i < len; ++i){
48 slope[i] = Math::sgn(x[i]);
49 }
50 }
51 };
52
53 class L2 { NOMEMALIGN
54 public:
56 default_type& residual,
57 default_type& slope) {
58 residual = x * x;
59 slope = x;
60 }
61
62 void operator() (const Eigen::Matrix<default_type, Eigen::Dynamic, 1>& x,
63 Eigen::Matrix<default_type, Eigen::Dynamic, 1>& residual,
64 Eigen::Matrix<default_type, Eigen::Dynamic, 1>& slope) {
65 residual = x.cwiseAbs2(); //.squaredNorm(); //.array().square();
66 slope = x;
67 }
68 };
69
70 // least powers: residual = |x|^power with power between 1 and 2
71 class LP { NOMEMALIGN
72 public:
73 LP (const default_type p) : power(p) {assert (power>=1.0 && power <= 2.0);}
74 LP () : power(1.2) {}
75
77 default_type& residual,
78 default_type& slope) {
79 residual = std::pow(abs(x), power);
80 slope = Math::sgn(x) * std::pow(abs(x), power - 1.0);
81 }
82
83 void operator() (const Eigen::Matrix<default_type, Eigen::Dynamic, 1>& x,
84 Eigen::Matrix<default_type, Eigen::Dynamic, 1>& residual,
85 Eigen::Matrix<default_type, Eigen::Dynamic, 1>& slope) {
86 residual = x.cwiseAbs().array().pow(power);
87 slope = x.cwiseAbs().array().pow(power -1.0);
88 const ssize_t len = x.size();
89 for (ssize_t i = 0; i < len; ++i){
90 slope[i] *= Math::sgn(x[i]);
91 }
92 }
93
94 private:
95 default_type power;
96 };
97
98 }
99 }
100}
101#endif
void operator()(const default_type &x, default_type &residual, default_type &slope)
void operator()(const default_type &x, default_type &residual, default_type &slope)
void operator()(const default_type &x, default_type &residual, default_type &slope)
LP(const default_type p)
#define NOMEMALIGN
Definition: memory.h:22
int sgn(T val)
Definition: base.h:24
double default_type
the default type used throughout MRtrix
Definition: types.h:228
constexpr std::enable_if< std::is_arithmetic< X >::value &&std::is_unsigned< X >::value, X >::type abs(X x)
Definition: types.h:297