IMP logo
IMP Reference Guide  develop.73004770ef,2026/11/23
The Integrative Modeling Platform
core/Gaussian.h
Go to the documentation of this file.
1 /**
2  * \file IMP/core/Gaussian.h
3  * \brief Decorator to hold Gaussian3D
4  *
5  * Copyright 2007-2023 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPCORE_GAUSSIAN_H
10 #define IMPCORE_GAUSSIAN_H
11 
12 #include <IMP/Object.h>
13 #include <IMP/core/core_config.h>
14 #include <IMP/algebra/Gaussian3D.h>
15 #include <IMP/Particle.h>
16 #include <IMP/Model.h>
17 #include <IMP/Decorator.h>
18 #include <IMP/decorator_macros.h>
19 #include <IMP/exception.h>
20 #include <IMP/core/rigid_bodies.h>
21 #include "internal/rigid_bodies.h"
22 #include <Eigen/Dense>
23 #include <limits>
24 #include <cereal/access.hpp>
25 #include <cereal/types/base_class.hpp>
26 
27 IMPCORE_BEGIN_NAMESPACE
28 
29 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
30 
31 /** little class to store an Eigen::Matrix3d */
32 class IMPCOREEXPORT Matrix3D : public IMP::Object{
33  Eigen::Matrix3d mat_;
34  // For a cached global covariance, the orientation (rigid body quaternion)
35  // it was computed for. Not serialized; NaN means unknown.
36  algebra::Vector4D quaternion_;
37  friend class cereal::access;
38 
39  template<class Archive> void serialize(Archive &ar) {
40  ar(cereal::base_class<Object>(this));
41  for (unsigned i = 0; i < 3; ++i) {
42  for (unsigned j = 0; j < 3; ++j) {
43  ar(mat_(i, j));
44  }
45  }
46  }
47  IMP_OBJECT_SERIALIZE_DECL(Matrix3D);
48 
49  public:
50  Matrix3D(Eigen::Matrix3d mat,
51  std::string name="Matrix3DDensityMap%1%"):Object(name),mat_(mat),
52  quaternion_(get_unknown_quaternion()) { }
53 
54  Matrix3D(Eigen::Matrix3d mat, const algebra::Vector4D &quaternion,
55  std::string name="Matrix3DDensityMap%1%")
56  : Object(name), mat_(mat), quaternion_(quaternion) {}
57 
58  Matrix3D() : IMP::Object(""), quaternion_(get_unknown_quaternion()) {}
59 
60  Eigen::Matrix3d get_mat() const {return mat_;}
61 
62  //! Replace the matrix, and the orientation it was computed for
63  void set_mat(const Eigen::Matrix3d &mat,
64  const algebra::Vector4D &quaternion) {
65  mat_ = mat;
66  quaternion_ = quaternion;
67  }
68 
69  //! Return true if this matrix was computed for the given orientation
70  bool get_is_for_quaternion(const algebra::Vector4D &q) const {
71  return quaternion_[0] == q[0] && quaternion_[1] == q[1]
72  && quaternion_[2] == q[2] && quaternion_[3] == q[3];
73  }
74 
75  private:
76  static algebra::Vector4D get_unknown_quaternion() {
77  double nan = std::numeric_limits<double>::quiet_NaN();
78  return algebra::Vector4D(nan, nan, nan, nan);
79  }
80 };
81 
82 #endif
83 
84 /** A decorator for a particle storing a Gaussian. */
85 class IMPCOREEXPORT Gaussian : public RigidBody {
86  // define variance and covariance keys
87  static void do_setup_particle(Model *m, ParticleIndex pi);
88  static void do_setup_particle(Model *m, ParticleIndex pi,
89  const algebra::Gaussian3D &g);
90 
91  public:
95 
96  static ObjectKey get_local_covariance_key();
97  static ObjectKey get_global_covariance_key();
98  static bool get_is_setup(Model *m, ParticleIndex pi) {
99  return m->get_has_attribute(get_local_covariance_key(), pi);
100  }
101 
102 
103  //! retrieve local covariance (as diagonal matrix)
104  Eigen::Matrix3d get_local_covariance() const {
105  /* Kind of evil, but dynamic_cast fails randomly here
106  on our RHEL 5 systems */
107  Matrix3D* local
108  = (Matrix3D*)get_model()->get_attribute(get_local_covariance_key(),
110  return local->get_mat();
111  }
112 
113  //! retrieve local variances as Vector3D
115  return algebra::Vector3D(get_local_covariance().diagonal()[0],
116  get_local_covariance().diagonal()[1],
117  get_local_covariance().diagonal()[2]);
118  }
119 
120  //! retrieve global covariance
121  /** This is R diag(variances) R^T for the current orientation R of the
122  Gaussian. It is cached, and recomputed whenever the orientation has
123  changed since (e.g. because a parent rigid body moved). */
124  Eigen::Matrix3d get_global_covariance() {
125  Model *m = get_model();
126  ObjectKey k = get_global_covariance_key();
128  if (m->get_has_attribute(k, pi)) {
129  Matrix3D *global = static_cast<Matrix3D*>(m->get_attribute(k, pi));
130  if (global->get_is_for_quaternion(get_rotation_quaternion())) {
131  return global->get_mat();
132  }
133  }
134  return get_updated_global_covariance();
135  };
136 
137  //! create Gaussian3D from these attributes
139  return algebra::Gaussian3D(get_reference_frame(),get_variances());
140 }
141 
142  //! Get the covariance attributes from a Gaussian3D object
143  void set_gaussian(const algebra::Gaussian3D &g);
144 
145  //! set the local-frame covariance.
146  void set_local_covariance(const Eigen::Vector3d covar) {
147  IMP_NEW(Matrix3D,local,(covar.asDiagonal()));
148  get_model()->set_attribute(get_local_covariance_key(),
150  local);
151  local->set_was_used(true);
152  // Force recalculation of global covariance on next access
154  }
155 
156  //! equivalent to set_local_covariance, used for backwards compatibility
158  set_local_covariance(Eigen::Vector3d(v.get_data()));
159  }
160 
161  //! set the global-frame covariance. does NOT update local frame!
162  /** The value is used until the orientation of the Gaussian changes. */
163  void set_global_covariance(Eigen::Matrix3d covar){
164  IMP_NEW(Matrix3D,global,(covar, get_rotation_quaternion()));
165  ObjectKey k = get_global_covariance_key();
167  if (!get_model()->get_has_attribute(k, pi)) {
168  get_model()->add_cache_attribute(k, pi, global);
169  } else {
170  get_model()->set_attribute(k, pi, global);
171  }
172  global->set_was_used(true);
173  }
174 
175  //! update the global covariance
176  void update_global_covariance();
177 
178 #ifndef SWIG
179  private:
180  // update_global_covariance(), then return it (kept out of line)
181  Eigen::Matrix3d get_updated_global_covariance();
182 
183  // current orientation, read in place (no copy) as this is on a hot path
184  const algebra::Vector4D &get_rotation_quaternion() const {
185  return access_quaternion_data(static_cast<const Model *>(get_model()))
186  [get_particle_index().get_index()];
187  }
188 
189  public:
190 #endif
191 
192 
193  ////! Evaluate the Gaussian at a point?
194  //Float get_probability_at_point(const algebra::Vector3D &point) const;
195 };
196 IMP_DECORATORS(Gaussian, Gaussians, Particles);
197 
198 IMPCORE_END_NAMESPACE
199 
200 #endif /* IMPCORE_GAUSSIAN_H */
The base class for decorators.
void add_cache_attribute(TypeKey attribute_key, ParticleIndex particle, Type value)
ParticleIndex get_particle_index() const
Returns the particle index decorated by this decorator.
Definition: Decorator.h:211
#define IMP_DECORATOR_SETUP_1(Name, FirstArgumentType, first_argument_name)
Model * get_model() const
Returns the Model containing the particle.
Definition: Decorator.h:214
Storage of a model, its restraints, constraints and particles.
VectorD< 4 > Vector4D
Definition: VectorD.h:411
Exception definitions and assertions.
void set_global_covariance(Eigen::Matrix3d covar)
set the global-frame covariance. does NOT update local frame!
#define IMP_NEW(Typename, varname, args)
Declare a ref counted pointer to a new object.
Definition: object_macros.h:74
void clear_particle_caches(ParticleIndex pi)
Clear all the cache attributes of a given particle.
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:86
A Gaussian distribution in 3D.
Definition: Gaussian3D.h:25
Common base class for heavy weight IMP objects.
Definition: Object.h:111
VectorD< 3 > Vector3D
Definition: VectorD.h:407
Eigen::Matrix3d get_global_covariance()
retrieve global covariance
functionality for defining rigid bodies
#define IMP_OBJECT_SERIALIZE_DECL(Name)
Declare methods needed for serialization of Object pointers.
Definition: object_macros.h:95
Helper macros for implementing Decorators.
void set_attribute(TypeKey attribute_key, ParticleIndex particle, Type value)
set the value of particle attribute with the specified key
#define IMP_DECORATOR_SETUP_0(Name)
Classes to handle individual model particles. (Note that implementation of inline functions is in int...
A shared base class to help in debugging and things.
#define IMP_DECORATOR_METHODS(Name, Parent)
void set_local_covariance(const Eigen::Vector3d covar)
set the local-frame covariance.
#define IMP_DECORATORS(Name, PluralName, Parent)
Define the types for storing sets of decorators.
Gaussian shape.
algebra::Gaussian3D get_gaussian() const
create Gaussian3D from these attributes
bool get_has_attribute(TypeKey attribute_key, ParticleIndex particle) const
return true if particle has attribute with the specified key
A decorator for a rigid body.
Definition: rigid_bodies.h:80
void set_variances(const algebra::Vector3D v)
equivalent to set_local_covariance, used for backwards compatibility
Type get_attribute(TypeKey attribute_key, ParticleIndex particle)
get the value of the particle attribute with the specified key
algebra::Vector3D get_variances() const
retrieve local variances as Vector3D
Eigen::Matrix3d get_local_covariance() const
retrieve local covariance (as diagonal matrix)
IMP::algebra::ReferenceFrame3D get_reference_frame() const
Definition: rigid_bodies.h:213