IMP Reference Guide  develop.b99932598c,2021/06/14 The Integrative Modeling Platform
algebra/distance.h
Go to the documentation of this file.
1 /**
2  * \file IMP/algebra/distance.h
3  * \brief Distance metrics.
4  *
6  *
7  */
8
9 #ifndef IMPALGEBRA_DISTANCE_H
10 #define IMPALGEBRA_DISTANCE_H
11
12 #include <IMP/algebra/algebra_config.h>
13 #include "Transformation3D.h"
14 #include "VectorD.h"
15 #include "IMP/base_types.h"
16
17 IMPALGEBRA_BEGIN_NAMESPACE
18
19 //! Calculate the root mean square deviation between two sets of 3D points.
20 /**
21  \note the function assumes correspondence between the two sets of
22  points and does not perform rigid alignment.
23
24  \genericgeometry
25  */
26 template <class Vector3DsOrXYZs0, class Vector3DsOrXYZs1>
28  const Vector3DsOrXYZs0& m1,
29  const Vector3DsOrXYZs1& m2) {
30  using algebra::get_vector_geometry;
31  IMP_USAGE_CHECK(std::distance(m1.begin(), m1.end()) ==
32  std::distance(m2.begin(), m2.end()),
33  "The input sets of XYZ points "
34  << "should be of the same size");
35  double rmsd = 0.0;
36  typename Vector3DsOrXYZs0::const_iterator it0 = m1.begin();
37  typename Vector3DsOrXYZs1::const_iterator it1 = m2.begin();
38  for (; it0 != m1.end(); ++it0, ++it1) {
39  Vector3D tred = tr.get_transformed(get_vector_geometry(*it0));
40  rmsd += get_squared_distance(tred, get_vector_geometry(*it1));
41  }
42  return std::sqrt(rmsd / m1.size());
43 }
44
45 /** See get_rmsd_transforming_first(). */
46 template <class Vector3DsOrXYZs0, class Vector3DsOrXYZs1>
47 inline double get_rmsd(const Vector3DsOrXYZs0& m1, const Vector3DsOrXYZs1& m2) {
49 }
50
51
52 template <class Vector3DsOrXYZs0, class Vector3DsOrXYZs1>
53 inline double get_weighted_rmsd_transforming_first(const Transformation3D& tr,
54  const Vector3DsOrXYZs0& m1,
55  const Vector3DsOrXYZs1& m2,
56  const Floats& weights) {
57  using algebra::get_vector_geometry;
58  IMP_USAGE_CHECK(std::distance(m1.begin(), m1.end()) ==
59  std::distance(m2.begin(), m2.end()),
60  "The input sets of XYZ points "
61  << "should be of the same size");
62  IMP_USAGE_CHECK(std::distance(m1.begin(), m1.end()) ==
63  std::distance(weights.begin(), weights.end()),
64  "The input sets of XYZ points and the weights "
65  << "should be of the same size");
66
67  double rmsd = 0.0;
68  double weight_sum = 0.0;
69  typename Vector3DsOrXYZs0::const_iterator it0 = m1.begin();
70  typename Vector3DsOrXYZs1::const_iterator it1 = m2.begin();
71  Floats::const_iterator it3 = weights.begin();
72  for (; it0 != m1.end(); ++it0, ++it1, ++it3) {
73  Vector3D tred = tr.get_transformed(get_vector_geometry(*it0));
74  rmsd += (*it3)*get_squared_distance(tred, get_vector_geometry(*it1));
75  weight_sum += *it3;
76  }
77  return std::sqrt(rmsd / weight_sum);
78 }
79
80
81 /** See get_weighted_rmsd_transforming_first(). */
82 template <class Vector3DsOrXYZs0, class Vector3DsOrXYZs1>
83 inline double get_weighted_rmsd(const Vector3DsOrXYZs0& m1, const Vector3DsOrXYZs1& m2,
84  const Floats& weights) {
85  return get_weighted_rmsd_transforming_first(get_identity_transformation_3d(), m1, m2, weights);
86 }
87
88
89 IMPALGEBRA_END_NAMESPACE
90
91 #endif /* IMPALGEBRA_DISTANCE_H */
Simple 3D transformation class.
Basic types used by IMP.
double get_squared_distance(const VectorD< D > &v1, const VectorD< D > &v2)
Compute the squared distance between two vectors.
Definition: VectorD.h:201
double get_weighted_rmsd(const Vector3DsOrXYZs0 &m1, const Vector3DsOrXYZs1 &m2, const Floats &weights)
double get_rmsd(const Vector3DsOrXYZs0 &m1, const Vector3DsOrXYZs1 &m2)
Simple D vector class.
Vector3D get_transformed(const Vector3D &o) const
Transform.
Simple 3D transformation class.
Transformation3D get_identity_transformation_3d()
Return a transformation that does not do anything.
VectorD< 3 > Vector3D
Definition: VectorD.h:421
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Definition: check_macros.h:168
double get_rmsd_transforming_first(const Transformation3D &tr, const Vector3DsOrXYZs0 &m1, const Vector3DsOrXYZs1 &m2)
Calculate the root mean square deviation between two sets of 3D points.