27 #ifndef CDPL_MATH_KABSCHALGORITHM_HPP
28 #define CDPL_MATH_KABSCHALGORITHM_HPP
88 template <
typename M1,
typename M2,
typename V>
90 bool do_center =
true, std::size_t max_svd_iter = 0)
94 typename V::SizeType>::Type SizeType;
96 SizeType dim = points().getSize1();
97 SizeType num_pts = points().getSize2();
99 CDPL_MATH_CHECK(dim == SizeType(ref_points().getSize1()) && num_pts == SizeType(ref_points().getSize2()),
103 "KabschAlgorithm: Number of points != number of weights",
Base::SizeError);
107 for (SizeType i = 0; i < num_pts; i++) {
109 w_sum += weights()(i);
115 prod(points, weights, centroid1);
116 prod(ref_points, weights, centroid2);
121 tmpPoints.resize(dim, num_pts,
false);
122 tmpPoints.assign(points);
124 tmpRefPoints.resize(dim, num_pts,
false);
125 tmpRefPoints.assign(ref_points);
127 for (SizeType i = 0; i < num_pts; i++) {
128 column(tmpPoints, i).minusAssign(centroid1) *= weights()(i) / w_sum;
129 column(tmpRefPoints, i).minusAssign(centroid2);
133 tmpPoints.resize(dim, num_pts,
false);
134 tmpPoints.assign(points);
136 for (SizeType i = 0; i < num_pts; i++)
137 column(tmpPoints, i) *= weights()(i) / w_sum;
140 covarMatrix.resize(dim, dim,
false);
143 prod(tmpPoints,
trans(tmpRefPoints), covarMatrix);
145 prod(tmpPoints,
trans(ref_points), covarMatrix);
147 return align(dim, do_center, max_svd_iter);
163 template <
typename M1,
typename M2>
165 bool do_center =
true, std::size_t max_svd_iter = 0)
170 SizeType dim = points().getSize1();
171 SizeType num_pts = points().getSize2();
173 CDPL_MATH_CHECK(dim == SizeType(ref_points().getSize1()) && num_pts == SizeType(ref_points().getSize2()),
180 tmpPoints.resize(dim, num_pts,
false);
181 tmpPoints.assign(points);
183 tmpRefPoints.resize(dim, num_pts,
false);
184 tmpRefPoints.assign(ref_points);
186 for (SizeType i = 0; i < num_pts; i++) {
187 column(tmpPoints, i).minusAssign(centroid1);
188 column(tmpRefPoints, i).minusAssign(centroid2);
192 covarMatrix.resize(dim, dim,
false);
195 prod(tmpPoints,
trans(tmpRefPoints), covarMatrix);
197 prod(points,
trans(ref_points), covarMatrix);
199 return align(dim, do_center, max_svd_iter);
212 template <
typename SizeType>
213 bool align(SizeType dim,
bool do_center, std::size_t max_svd_iter)
216 svdV.resize(dim, dim,
false);
218 if (!
svDecompose(covarMatrix, svdW, svdV, max_svd_iter))
224 SizeType xform_dim = dim + 1;
226 transform.resize(xform_dim, xform_dim,
false);
228 range(transform, 0, dim, 0, dim).assign(
prod(svdV,
trans(covarMatrix)));
236 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:70
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:164
const MatrixType & getTransform() const
Returns the rigid-body transformation produced by the most recent successful align() call.
Definition: KabschAlgorithm.hpp:206
Matrix< T > MatrixType
The matrix type used for the transformation, the covariance matrix and the working buffers.
Definition: KabschAlgorithm.hpp:68
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:89
T ValueType
The scalar value type.
Definition: KabschAlgorithm.hpp:66
Vector-expression proxy that views a single column of an underlying matrix.
Definition: MatrixProxy.hpp:320
CRTP base class for all matrix expression types.
Definition: Expression.hpp:104
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:455
Constant vector expression in which every element equals the same scalar value.
Definition: Vector.hpp:2625
CRTP base class for all vector expression types.
Definition: Expression.hpp:66
Dynamically-sized dense vector with configurable underlying storage.
Definition: Vector.hpp:430
Constant vector expression whose elements are all zero.
Definition: Vector.hpp:2312
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:1692
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:1259
E::ValueType det(const MatrixExpression< E > &e)
Returns the determinant of the matrix expression e.
Definition: Matrix.hpp:2987
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:1288
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:1480
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:49