IMP logo
IMP Reference Guide  develop.4785481560,2024/03/29
The Integrative Modeling Platform
DOFValues.h
Go to the documentation of this file.
1 /**
2 1;95;0c * \file IMP/kinematics/DOFValues.h
3  * \brief a simple class for storage of DOF values.
4  *
5  * \authors Dina Schneidman, Barak Raveh
6  *
7  * Copyright 2007-2022 IMP Inventors. All rights reserved.
8  *
9  */
10 
11 #ifndef IMPKINEMATICS_DOF_VALUES_H
12 #define IMPKINEMATICS_DOF_VALUES_H
13 
14 #include "DOF.h"
15 #include <IMP/algebra/constants.h>
16 #include <cereal/access.hpp>
17 #include <cereal/types/base_class.hpp>
18 #include <cereal/types/vector.hpp>
19 
20 IMPKINEMATICS_BEGIN_NAMESPACE
21 
22 //! A class that holds DOF values for DOFs
23 class IMPKINEMATICSEXPORT DOFValues : public std::vector<double> {
24  public:
25  //! Constructor from DOFs
26  DOFValues(const DOFs& dofs) {
27  reserve(dofs.size());
28  for (unsigned int i = 0; i < dofs.size(); i++)
29  push_back(dofs[i]->get_value());
30  }
31 
32  //! Empty Constructor
33  DOFValues() {}
34 
35  // TODO: provide an option for more general distance definition
36  // through RRT class
37  double get_distance2(const DOFValues& other_dof_values) const {
38  double dist = 0.0;
39  for(unsigned int i=0; i<size(); i++) {
40  double diff1 = ((*this)[i] - other_dof_values[i]);
41  double diff2 = std::fabs(diff1 - 2.0*IMP::algebra::PI);
42  double diff3 = std::fabs(diff1 + 2.0*IMP::algebra::PI);
43  double diff = std::min(std::fabs(diff1), std::min(diff2, diff3));
44  dist += (diff*diff);
45  }
46  return dist / size();
47  }
48 
49  double get_distance2(const DOFValues& other_dof_values,
50  const std::vector<bool>& active_dofs) const {
51  double dist = 0.0;
52  unsigned int asize = 0;
53  for(unsigned int i=0; i<size(); i++) {
54  if(active_dofs.size() == 0 || active_dofs[i]) {
55  double diff1 = ((*this)[i] - other_dof_values[i]);
56  double diff2 = std::fabs(diff1 - 2.0*IMP::algebra::PI);
57  double diff3 = std::fabs(diff1 + 2.0*IMP::algebra::PI);
58  double diff = std::min(std::fabs(diff1), std::min(diff2, diff3));
59  dist += (diff*diff);
60  asize++;
61  }
62  }
63  return dist / asize;
64  }
65 
66  double get_distance(const DOFValues& other_dof_values) const {
67  return sqrt(get_distance2(other_dof_values));
68  }
69 
70  double get_distance(const DOFValues& other_dof_values,
71  const std::vector<bool>& active_dofs) const {
72  return sqrt(get_distance2(other_dof_values, active_dofs));
73  }
74 
75  public:
76  IMP_SHOWABLE_INLINE(DOFValues, {
77  out << "[";
78  if (size() > 0) {
79  out << operator[](0);
80  }
81  for (unsigned int i = 1; i < size(); i++) {
82  out << "," << operator[](i);
83  }
84  out << "]";
85  });
86 
87 private:
88  friend class cereal::access;
89 
90  template<class Archive> void serialize(Archive &ar) {
91  ar(cereal::base_class<std::vector<double> >(this));
92  }
93 };
94 
96 
97 IMPKINEMATICS_END_NAMESPACE
98 
99 // std::vector may serialize with load/save methods instead; force
100 // cereal to use our 'serialize' method
101 CEREAL_SPECIALIZE_FOR_ALL_ARCHIVES(
102  IMP::kinematics::DOFValues, cereal::specialization::member_serialize);
103 
104 #endif /* IMPKINEMATICS_DOF_VALUES_H */
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
static const double PI
the constant pi
single degree of freedom
DOFValues()
Empty Constructor.
Definition: DOFValues.h:33
#define IMP_VALUES(Name, PluralName)
Define the type for storing sets of values.
Definition: value_macros.h:23
DOFValues(const DOFs &dofs)
Constructor from DOFs.
Definition: DOFValues.h:26
A class that holds DOF values for DOFs.
Definition: DOFValues.h:23
Various useful constants.
double get_distance(const Line3D &s, const Vector3D &p)
Get closest distance between a line and a point.