IMP logo
IMP Reference Guide  2.20.2
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 <cereal/access.hpp>
24 #include <cereal/types/base_class.hpp>
25 
26 IMPCORE_BEGIN_NAMESPACE
27 
28 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
29 
30 /** little class to store an Eigen::Matrix3d */
31 class IMPCOREEXPORT Matrix3D : public IMP::Object{
32  Eigen::Matrix3d mat_;
33  friend class cereal::access;
34 
35  template<class Archive> void serialize(Archive &ar) {
36  ar(cereal::base_class<Object>(this));
37  for (unsigned i = 0; i < 3; ++i) {
38  for (unsigned j = 0; j < 3; ++j) {
39  ar(mat_(i, j));
40  }
41  }
42  }
43  IMP_OBJECT_SERIALIZE_DECL(Matrix3D);
44 
45  public:
46  Matrix3D(Eigen::Matrix3d mat,
47  std::string name="Matrix3DDensityMap%1%"):Object(name),mat_(mat){ }
48 
49  Matrix3D() : IMP::Object("") {}
50 
51  Eigen::Matrix3d get_mat() const {return mat_;}
52 };
53 
54 #endif
55 
56 /** A decorator for a particle storing a Gaussian. */
57 class IMPCOREEXPORT Gaussian : public RigidBody {
58  // define variance and covariance keys
59  static void do_setup_particle(Model *m, ParticleIndex pi);
60  static void do_setup_particle(Model *m, ParticleIndex pi,
61  const algebra::Gaussian3D &g);
62 
63  public:
67 
68  static ObjectKey get_local_covariance_key();
69  static ObjectKey get_global_covariance_key();
70  static bool get_is_setup(Model *m, ParticleIndex pi) {
71  return m->get_has_attribute(get_local_covariance_key(), pi);
72  }
73 
74 
75  //! retrieve local covariance (as diagonal matrix)
76  Eigen::Matrix3d get_local_covariance() const {
77  /* Kind of evil, but dynamic_cast fails randomly here
78  on our RHEL 5 systems */
79  Matrix3D* local
80  = (Matrix3D*)get_model()->get_attribute(get_local_covariance_key(),
82  return local->get_mat();
83  }
84 
85  //! retrieve local variances as Vector3D
87  return algebra::Vector3D(get_local_covariance().diagonal()[0],
88  get_local_covariance().diagonal()[1],
89  get_local_covariance().diagonal()[2]);
90  }
91 
92  //! retrieve global covariance
93  Eigen::Matrix3d get_global_covariance() {
94  ObjectKey k = get_global_covariance_key();
96  if (!get_model()->get_has_attribute(k, pi)) {
97  update_global_covariance();
98  }
99  Matrix3D* global = (Matrix3D*)get_model()->get_attribute(k, pi);
100  return global->get_mat();
101  };
102 
103  //! create Gaussian3D from these attributes
105  return algebra::Gaussian3D(get_reference_frame(),get_variances());
106 }
107 
108  //! Get the covariance attributes from a Gaussian3D object
109  void set_gaussian(const algebra::Gaussian3D &g);
110 
111  //! set the local-frame covariance.
112  void set_local_covariance(const Eigen::Vector3d covar) {
113  IMP_NEW(Matrix3D,local,(covar.asDiagonal()));
114  get_model()->set_attribute(get_local_covariance_key(),
116  local);
117  local->set_was_used(true);
118  // Force recalculation of global covariance on next access
120  }
121 
122  //! equivalent to set_local_covariance, used for backwards compatibility
124  set_local_covariance(Eigen::Vector3d(v.get_data()));
125  }
126 
127  //! set the global-frame covariance. does NOT update local frame!
128  void set_global_covariance(Eigen::Matrix3d covar){
129  IMP_NEW(Matrix3D,global,(covar));
130  ObjectKey k = get_global_covariance_key();
132  if (!get_model()->get_has_attribute(k, pi)) {
133  get_model()->add_cache_attribute(k, pi, global);
134  } else {
135  get_model()->set_attribute(k, pi, global);
136  }
137  global->set_was_used(true);
138  }
139 
140  //! update the global covariance
141  void update_global_covariance();
142 
143 
144  ////! Evaluate the Gaussian at a point?
145  //Float get_probability_at_point(const algebra::Vector3D &point) const;
146 };
147 IMP_DECORATORS(Gaussian, Gaussians, Particles);
148 
149 IMPCORE_END_NAMESPACE
150 
151 #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.
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
Eigen::Matrix3d get_global_covariance()
retrieve global covariance
Definition: core/Gaussian.h:93
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)
VectorD< 3 > Vector3D
Definition: VectorD.h:425
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:82
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
Definition: core/Gaussian.h:86
Eigen::Matrix3d get_local_covariance() const
retrieve local covariance (as diagonal matrix)
Definition: core/Gaussian.h:76
IMP::algebra::ReferenceFrame3D get_reference_frame() const
Definition: rigid_bodies.h:215