Developer documentation
Version 3.0.3-105-gd3941f44
cubic_spline.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 __math_cubic_spline_h__
18#define __math_cubic_spline_h__
19namespace MR
20{
21 namespace Math
22 {
24 {
25 Value = 1,
28 };
29
30
31 template <typename T> class CubicSpline
33 public:
34 using BasisMatrix = Eigen::Matrix<T, 4, 4>;
35 using WeightVector = Eigen::Matrix<T, 1, 4>;
36 WeightVector weights, deriv_weights;
37
38 void set (T position) {
39 (this->*(_internal_set)) (position);
40 }
41
42 T coef (size_t i) const {
43 return (weights[i]);
44 }
45
46 protected:
47 static const BasisMatrix cubic_poly_derivative_operator;
48 const BasisMatrix basis_matrix;
49 const BasisMatrix deriv_basis_matrix;
50
51 CubicSpline (SplineProcessingType processType, const BasisMatrix basis_matrix, const BasisMatrix deriv_basis_matrix) :
54 {
55 switch (processType) {
57 _internal_set = &CubicSpline::_set_value;
58 break;
59 // Could be used for partial derviative so we need to calculate both deriv and value weights
62 _internal_set = &CubicSpline::_set_value_deriv;
63 break;
64 default:
65 break;
66 }
67 }
68
69 private:
70
71 CubicSpline () {}
72
73 // Function pointer to the internal set function depening on processing type
74 void (CubicSpline::*_internal_set) (T);
75
76 inline void _set_value (T position) {
77 const T p2 = Math::pow2(position);
78 const auto vec = WeightVector (p2 * position, p2, position, 1.0);
79 weights = (vec * basis_matrix);
80 }
81
82 inline void _set_value_deriv (T position) {
83 const T p2 = Math::pow2(position);
84 const auto vec = WeightVector (position * p2, p2, position, 1.0);
85 weights = (vec * basis_matrix);
86 deriv_weights = (vec * deriv_basis_matrix);
87 }
88 };
89
90
91 // Hermite spline implementation
92 template <typename T> class HermiteSpline :
94 public:
95 using BasisMatrix = typename CubicSpline<T>::BasisMatrix;
96 static const BasisMatrix hermite_basis_mtrx;
97 static const BasisMatrix hermite_derivative_basis_mtrx;
98
100 : CubicSpline<T> (processType, hermite_basis_mtrx, hermite_derivative_basis_mtrx) {}
101 };
102
103
104 // Uniform bspline implementation
105 template <typename T> class UniformBSpline :
107 public:
108 using BasisMatrix = typename CubicSpline<T>::BasisMatrix;
109 static const BasisMatrix uniform_bspline_basis_mtrx;
110 static const BasisMatrix uniform_bspline_derivative_basis_mtrx;
111
113 : CubicSpline<T> (processType, uniform_bspline_basis_mtrx, uniform_bspline_derivative_basis_mtrx) {}
114 };
115
116
117
118 // Initialise our static const matrices
119 template <typename T>
120 const typename CubicSpline<T>::BasisMatrix
122 0, 0, 0, 0,
123 3, 0, 0, 0,
124 0, 2, 0, 0,
125 0, 0, 1, 0).finished());
126
127
128 // Hermite spline
129 template <typename T>
130 const typename HermiteSpline<T>::BasisMatrix
132 -0.5, 1.5, -1.5, 0.5,
133 1, -2.5, 2, -0.5,
134 -0.5, 0, 0.5, 0,
135 0, 1, 0, 0).finished());
136
137 template <typename T>
138 const typename HermiteSpline<T>::BasisMatrix
140
141
142 // Uniform b-spline
143 template <typename T>
144 const typename UniformBSpline<T>::BasisMatrix
145 UniformBSpline<T>::uniform_bspline_basis_mtrx((1/6.0) * (BasisMatrix() <<
146 -1, 3, -3, 1,
147 3, -6, 3, 0,
148 -3, 0, 3, 0,
149 1, 4, 1, 0).finished());
150
151 template <typename T>
152 const typename UniformBSpline<T>::BasisMatrix
154
155 }
156}
157
158
159#endif
160
static const BasisMatrix cubic_poly_derivative_operator
Definition: cubic_spline.h:47
CubicSpline(SplineProcessingType processType, const BasisMatrix basis_matrix, const BasisMatrix deriv_basis_matrix)
Definition: cubic_spline.h:51
const BasisMatrix deriv_basis_matrix
Definition: cubic_spline.h:49
const BasisMatrix basis_matrix
Definition: cubic_spline.h:48
constexpr T pow2(const T &v)
Definition: math.h:53
SplineProcessingType
Definition: cubic_spline.h:24
@ ValueAndDerivative
Definition: cubic_spline.h:27
Definition: base.h:24
#define MEMALIGN(...)
Definition: types.h:185