27 #ifndef CDPL_MATH_KABSCHALGORITHM_HPP
28 #define CDPL_MATH_KABSCHALGORITHM_HPP
96 template <
typename M1,
typename M2,
typename V>
98 bool do_center =
true, std::size_t max_svd_iter = 0)
102 typename V::SizeType>::Type SizeType;
104 SizeType dim = points().getSize1();
105 SizeType num_pts = points().getSize2();
107 CDPL_MATH_CHECK(dim == SizeType(ref_points().getSize1()) && num_pts == SizeType(ref_points().getSize2()),
111 "KabschAlgorithm: Number of points != number of weights",
Base::SizeError);
115 for (SizeType i = 0; i < num_pts; i++) {
117 w_sum += weights()(i);
123 prod(points, weights, centroid1);
124 prod(ref_points, weights, centroid2);
129 tmpPoints.resize(dim, num_pts,
false);
130 tmpPoints.assign(points);
132 tmpRefPoints.resize(dim, num_pts,
false);
133 tmpRefPoints.assign(ref_points);
135 for (SizeType i = 0; i < num_pts; i++) {
136 column(tmpPoints, i).minusAssign(centroid1) *= weights()(i) / w_sum;
137 column(tmpRefPoints, i).minusAssign(centroid2);
141 tmpPoints.resize(dim, num_pts,
false);
142 tmpPoints.assign(points);
144 for (SizeType i = 0; i < num_pts; i++)
145 column(tmpPoints, i) *= weights()(i) / w_sum;
148 covarMatrix.resize(dim, dim,
false);
151 prod(tmpPoints,
trans(tmpRefPoints), covarMatrix);
153 prod(tmpPoints,
trans(ref_points), covarMatrix);
155 return align(dim, do_center, max_svd_iter);
171 template <
typename M1,
typename M2>
173 bool do_center =
true, std::size_t max_svd_iter = 0)
178 SizeType dim = points().getSize1();
179 SizeType num_pts = points().getSize2();
181 CDPL_MATH_CHECK(dim == SizeType(ref_points().getSize1()) && num_pts == SizeType(ref_points().getSize2()),
188 tmpPoints.resize(dim, num_pts,
false);
189 tmpPoints.assign(points);
191 tmpRefPoints.resize(dim, num_pts,
false);
192 tmpRefPoints.assign(ref_points);
194 for (SizeType i = 0; i < num_pts; i++) {
195 column(tmpPoints, i).minusAssign(centroid1);
196 column(tmpRefPoints, i).minusAssign(centroid2);
200 covarMatrix.resize(dim, dim,
false);
203 prod(tmpPoints,
trans(tmpRefPoints), covarMatrix);
205 prod(points,
trans(ref_points), covarMatrix);
207 return align(dim, do_center, max_svd_iter);
220 template <
typename SizeType>
221 bool align(SizeType dim,
bool do_center, std::size_t max_svd_iter)
224 svdV.resize(dim, dim,
false);
226 if (!
svDecompose(covarMatrix, svdW, svdV, max_svd_iter))
232 SizeType xform_dim = dim + 1;
234 transform.resize(xform_dim, xform_dim,
false);
236 range(transform, 0, dim, 0, dim).assign(
prod(svdV,
trans(covarMatrix)));
244 range(last_col, 0, dim).assign(centroid2 -
prod(
range(transform, 0, dim, 0, dim), centroid1));
Definition of exception classes.
Definition of various preprocessor macros for error checking.
#define CDPL_MATH_CHECK(expr, msg, e)
Throws the exception e with message msg when the boolean expression expr evaluates to false.
Definition: Check.hpp:47
Definition of matrix proxy types.
Definition of matrix data types.
Implementation of matrix singular value decomposition and associated operations.
Definition of type traits.
Definition of vector proxy types.
Definition of vector data types.
Thrown to indicate that the size of a (multidimensional) array is not correct.
Definition: Base/Exceptions.hpp:133
Thrown to indicate errors caused by some invalid value.
Definition: Base/Exceptions.hpp:76
Implementation of the Kabsch algorithm [KABA].
Definition: KabschAlgorithm.hpp:62
Vector< T > VectorType
The vector type used for the centroids and singular-value vectors.
Definition: KabschAlgorithm.hpp:78
bool align(const MatrixExpression< M1 > &points, const MatrixExpression< M2 > &ref_points, bool do_center=true, std::size_t max_svd_iter=0)
Computes the rigid body transformation that aligns a set of -dimensional points points with a corres...
Definition: KabschAlgorithm.hpp:172
const MatrixType & getTransform() const
Returns the rigid-body transformation produced by the most recent successful align() call.
Definition: KabschAlgorithm.hpp:214
Matrix< T > MatrixType
The matrix type used for the transformation, the covariance matrix and the working buffers.
Definition: KabschAlgorithm.hpp:73
bool align(const MatrixExpression< M1 > &points, const MatrixExpression< M2 > &ref_points, const VectorExpression< V > &weights, bool do_center=true, std::size_t max_svd_iter=0)
Computes the rigid body transformation that aligns a set of -dimensional points points with a corres...
Definition: KabschAlgorithm.hpp:97
T ValueType
The scalar value type.
Definition: KabschAlgorithm.hpp:68
Vector expression proxy that views a single column of an underlying matrix.
Definition: MatrixProxy.hpp:346
CRTP base class of all matrix expression types.
Definition: Expression.hpp:108
Vector expression proxy that views a single row of an underlying matrix.
Definition: MatrixProxy.hpp:53
Dynamically-sized dense row-major matrix with configurable underlying storage.
Definition: Matrix.hpp:510
Constant vector expression in which every element equals the same scalar value.
Definition: Vector.hpp:2883
CRTP base class of all vector expression types.
Definition: Expression.hpp:68
Dynamically-sized dense vector with configurable underlying storage.
Definition: Vector.hpp:480
Constant vector expression whose elements are all zero.
Definition: Vector.hpp:2524
constexpr unsigned int T
Specifies Hydrogen (Tritium).
Definition: AtomType.hpp:67
MatrixTranspose< E > trans(MatrixExpression< E > &e)
Returns a mutable Math::MatrixTranspose view of the matrix expression e.
Definition: MatrixExpression.hpp:1943
MatrixColumn< M > column(MatrixExpression< M > &e, typename MatrixColumn< M >::SizeType j)
Returns a mutable column proxy for column j of the matrix expression e.
Definition: MatrixProxy.hpp:1400
E::ValueType det(const MatrixExpression< E > &e)
Returns the determinant of the matrix expression e.
Definition: Matrix.hpp:3311
bool svDecompose(MatrixExpression< A > &a, VectorExpression< W > &w, MatrixExpression< V > &v, std::size_t max_iter=0)
Computes the Singular Value Decomposition [WSVD] of a -dimensional matrix a.
Definition: SVDecomposition.hpp:70
MatrixRange< E > range(MatrixExpression< E > &e, const typename MatrixRange< E >::RangeType &r1, const typename MatrixRange< E >::RangeType &r2)
Returns a mutable matrix range proxy viewing rows in r1 and columns in r2 of e.
Definition: MatrixProxy.hpp:1429
Matrix1VectorBinaryTraits< E1, E2, MatrixVectorProduct< E1, E2 > >::ResultType prod(const MatrixExpression< E1 > &e1, const VectorExpression< E2 > &e2)
Returns the matrix-vector product as a vector expression (named-function form of operator*).
Definition: MatrixExpression.hpp:1731
The namespace of the Chemical Data Processing Library.
Trait that resolves the common arithmetic type of T1 and T2 via std::common_type.
Definition: CommonType.hpp:46
std::common_type< T1, T2 >::type Type
The common type.
Definition: CommonType.hpp:51