IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
The Integrative Modeling Platform
geometric_alignment.h
Go to the documentation of this file.
1/**
2 * \file IMP/algebra/geometric_alignment.h
3 * \brief align sets of points.
4 *
5 * Copyright 2007-2026 IMP Inventors. All rights reserved.
6 */
7
8#ifndef IMPALGEBRA_GEOMETRIC_ALIGNMENT_H
9#define IMPALGEBRA_GEOMETRIC_ALIGNMENT_H
10
11#include <IMP/algebra/algebra_config.h>
12#include "Vector3D.h"
13#include "SphereD.h"
14#include "Rotation3D.h"
15#include "Transformation3D.h"
16#include "Transformation2D.h"
17#include <IMP/check_macros.h>
18#include <IMP/log.h>
19#include <IMP/log_macros.h>
20
21IMPALGEBRA_BEGIN_NAMESPACE
22
23//! Compute the rigid transform bringing the first point set to the second
24/** The points are assumed to be corresponding (that is, from[0] is aligned
25 to to[0] etc.). The alignment computed is that which minimizes the
26 sum of squared distances between corresponding points. This uses the
27 Kabsch algorithm.
28
29 \return the
30 \f[ \textit{argmin}_T
31 \sum \left|T\left(f\left[i\right]\right)-t[i]\right|^2 \f]
32
33 If the point sets lie in a 1 or 2 dimensional subspace,
34 the alignment algorithm
35 is unstable and not guaranteed to work. A warning is printed in this
36 case.
37
38 \genericgeometry
39
40 \see Transformation3D
41 \see Vector3D
42 */
43template <class Vector3DsOrXYZs0, class Vector3DsOrXYZs1>
45get_transformation_aligning_first_to_second(const Vector3DsOrXYZs0& source,
46 const Vector3DsOrXYZs1& target) {
47 using algebra::get_vector_geometry;
48 IMP_INTERNAL_CHECK(source.size() == target.size(), "sizes don't match");
49 IMP_INTERNAL_CHECK(source.size() > 0, "Points are needed");
50 // compute the centroid of the points and transform
51 // point sets so that their centroids coincide
52
53 Vector3D center_source(0, 0, 0), center_target(0, 0, 0);
54 for (unsigned int i = 0; i < source.size(); ++i) {
55 // double x= p_it->x();
56 center_source += get_vector_geometry(source[i]);
57 center_target += get_vector_geometry(target[i]);
58 IMP_LOG_VERBOSE(i << ": (" << get_vector_geometry(source[i]) << ") ("
59 << get_vector_geometry(target[i]) << ")\n");
60 }
61 center_source = center_source / source.size();
62 center_target = center_target / target.size();
63
64 IMP_LOG_VERBOSE("Centers are (" << center_source << ") (" << center_target
65 << ")\n");
66 Vector3Ds shifted_source(source.size()), shifted_target(target.size());
67 for (unsigned int i = 0; i < source.size(); ++i) {
68 shifted_source[i] = get_vector_geometry(source[i]) - center_source;
69 shifted_target[i] = get_vector_geometry(target[i]) - center_target;
70 }
71
72 // covariance matrix
73 Eigen::Matrix3d H;
74 for (int i = 0; i < 3; i++) {
75 for (int j = 0; j < 3; j++) {
76 H(i, j) = 0;
77 }
78 }
79 for (unsigned int i = 0; i < source.size(); i++) {
80 for (int j = 0; j < 3; j++) {
81 for (int k = 0; k < 3; k++) {
82 H(j, k) += shifted_source[i][j] * shifted_target[i][k];
83 }
84 }
85 }
86
87 IMP_LOG_VERBOSE("H is " << H << std::endl);
88#if EIGEN_MAJOR_VERSION >= 5
89 auto svd = H.jacobiSvd<Eigen::ComputeFullV | Eigen::ComputeFullU>();
90#else
91 Eigen::JacobiSVD<Eigen::Matrix3d> svd =
92 H.jacobiSvd(Eigen::ComputeFullV | Eigen::ComputeFullU);
93#endif
94 Eigen::Matrix3d U = svd.matrixU(), V = svd.matrixV();
95 Eigen::Vector3d SV = svd.singularValues();
96 IMP_LOG_VERBOSE("SVD is " << U << std::endl << V << std::endl);
97
98 double det = SV[0] * SV[1] * SV[2];
100 if (det < .00001) {
101 IMP_LOG_TERSE("SOURCE:\n");
102 for (unsigned int i = 0; i < source.size(); ++i) {
103 IMP_LOG_TERSE(source[i] << std::endl);
104 }
105 IMP_LOG_TERSE("TO:\n");
106 for (unsigned int i = 0; i < target.size(); ++i) {
107 IMP_LOG_TERSE(target[i] << std::endl);
108 }
109 IMP_LOG_TERSE(H);
110 IMP_WARN("Degenerate point set. I may not be able to align them."
111 << std::endl);
112 }
113 }
114
116 Eigen::Matrix3d Sigma = Eigen::Matrix3d::Zero();
117
118 for (int i = 0; i < 3; ++i) {
119 Sigma(i, i) = SV[i];
120 }
121
122 IMP_LOG_VERBOSE("Reconstructed is " << U * Sigma * V.transpose()
123 << std::endl);
124 }
125
126 // the rotation matrix is R = VU^T
127 Eigen::Matrix3d rot = V * U.transpose();
128
129 // check for reflection
130 if (rot.determinant() < 0) {
131 IMP_LOG_VERBOSE("Flipping matrix" << std::endl);
132 Eigen::Matrix3d S = Eigen::Matrix3d::Zero();
133 S(0, 0) = S(1, 1) = 1;
134 S(2, 2) = -1;
135 rot = V * S * U.transpose();
136 }
137
138 IMP_LOG_VERBOSE("Rotation matrix is " << rot << std::endl);
139
140 Rotation3D rotation = get_rotation_from_matrix(rot);
141 IMP_LOG_VERBOSE("Rotation is " << rotation << std::endl);
142 Vector3D translation = center_target - rotation.get_rotated(center_source);
143
144 Transformation3D ret(rotation, translation);
145 return ret;
146}
147
148//! Builds the transformation required to obtain a set of points from
149//! the first one
150/**
151 \note The function assumes that the relative distances between points
152 are conserved.
153**/
154IMPALGEBRAEXPORT Transformation2D
156 const Vector2Ds& set_to);
157
158// implemented in Transformation2D
159
160IMPALGEBRA_END_NAMESPACE
161
162#endif /* IMPALGEBRA_GEOMETRIC_ALIGNMENT_H */
Simple 3D rotation class.
Simple 3D sphere class.
2D transformations. Copyright 2007-2026 IMP Inventors. All rights reserved.
Simple 3D transformation class.
Simple 3D vector class.
Helper macros for throwing and handling exceptions.
#define IMP_IF_CHECK(level)
Execute the code block if a certain level checks are on.
#define IMP_INTERNAL_CHECK(expr, message)
An assertion to check for internal errors in IMP. An IMP::ErrorException will be thrown.
3D rotation class.
Definition Rotation3D.h:52
Simple 3D transformation class.
Logging and error reporting support.
Logging and error reporting support.
#define IMP_IF_LOG(level)
Execute the code block if a certain level of logging is on.
Definition log_macros.h:49
#define IMP_LOG_VERBOSE(expr)
Definition log_macros.h:83
#define IMP_WARN(expr)
Write a warning to a log.
Definition log_macros.h:55
#define IMP_LOG_TERSE(expr)
Definition log_macros.h:72
IMP::algebra::Transformation3D get_transformation_aligning_first_to_second(const Vector3DsOrXYZs0 &source, const Vector3DsOrXYZs1 &target)
Compute the rigid transform bringing the first point set to the second.
Rotation3D get_rotation_from_matrix(double m00, double m01, double m02, double m10, double m11, double m12, double m20, double m21, double m22)
Generate a Rotation3D object from a rotation matrix.
Transformation2D get_transformation_aligning_pair(const Vector2Ds &set_from, const Vector2Ds &set_to)
Vector< VectorD< 3 > > Vector3Ds
Definition VectorD.h:409
Vector< VectorD< 2 > > Vector2Ds
Definition VectorD.h:405
@ USAGE
Definition enums.h:57
VectorD< 3 > Vector3D
Definition VectorD.h:407
@ VERBOSE
Produce copious output to allow someone to trace through the computation.
Definition enums.h:33