IMP logo
IMP Reference Guide  develop.08ecd6469d,2026/04/02
The Integrative Modeling Platform
BoundingSphere3DSingletonScore.h
Go to the documentation of this file.
1 /**
2  * \file IMP/core/BoundingSphere3DSingletonScore.h
3  * \brief Score particles based on a bounding sphere - score would typically
4  * increase as particles are exiting the sphere boundaries, and must
5  * be zero within the sphere and positive outside of it.
6  *
7  * Copyright 2007-2026 IMP Inventors. All rights reserved.
8  */
9 
10 #ifndef IMPCORE_BOUNDING_SPHERE_3D_SINGLETON_SCORE_H
11 #define IMPCORE_BOUNDING_SPHERE_3D_SINGLETON_SCORE_H
12 
13 #include <IMP/core/core_config.h>
14 #include <IMP/generic.h>
15 #include <IMP/SingletonScore.h>
16 #include <IMP/singleton_macros.h>
17 #include <IMP/UnaryFunction.h>
18 #include <IMP/algebra/Sphere3D.h>
19 #include "XYZ.h"
20 #include "XYZR.h"
21 
22 IMPCORE_BEGIN_NAMESPACE
23 
24 //! Score XYZ or XYZR particles based on how far outside a sphere they are.
25 /** The radius of the particle is taken into account if it is XYZR decorated.
26  A particle that is contained within the bounding sphere has
27  a score of 0. The UnaryFunction passed should return 0 when given
28  a feature size of 0 and a positive value when the feature is positive.
29  */
30 template <class UF>
33  algebra::Sphere3D sphere_;
34 
35  public:
37 
38  virtual double evaluate_index(Model *m, ParticleIndex p,
39  DerivativeAccumulator *da) const override;
41  Model *m, const ParticleIndexes &pis) const override {
42  return IMP::get_particles(m, pis);
43  }
44 
45  UF *get_unary_function() const { return f_; }
46  algebra::Sphere3D get_sphere() const { return sphere_; }
47 
50 };
51 
52 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
53 template <class UF>
55  UF *f, const algebra::Sphere3D &sphere)
56  : SingletonScore("BoundingSphere3DSingletonScore%1%"), f_(f),
57  sphere_(sphere) {
58  IMP_USAGE_CHECK(std::abs(f_->evaluate(0)) < .000001,
59  "The unary function should return "
60  " 0 when passed a value of 0. Not "
61  << f_->evaluate(0));
62 }
63 template <class UF>
65  Model *m, ParticleIndex pi,
66  DerivativeAccumulator *da) const {
68  static const double MIN_DISTANCE = .000001;
69 
70  core::XYZ xyz(m, pi);
71  algebra::Vector3D v_from_origin= (xyz.get_coordinates() - sphere_.get_center());
72  double d_from_origin= v_from_origin.get_magnitude();
73  double d_from_surface= d_from_origin - sphere_.get_radius();
74  if(core::XYZR::get_is_setup(m,pi)){
75  core::XYZR xyzr(m,pi);
76  d_from_surface += xyzr.get_radius();
77  }
78  bool inside = d_from_surface<MIN_DISTANCE; // use MIN_DISTANCE for numerical stability
79  if (inside) {
80  return 0.0;
81  }
82  IMP_LOG_VERBOSE("Particle " << Showable(pi) << " is outside sphere: " << xyz
83  << " of " << sphere_ << std::endl);
84  if (da) {
85  IMP::DerivativePair dp= f_->evaluate_with_derivative(d_from_surface);
86  algebra::Vector3D deriv= v_from_origin.get_unit_vector() * dp.second;
87  xyz.add_to_derivatives(deriv, *da);
88  return dp.first;
89  } else {
90  return f_->evaluate(d_from_surface);
91  }
92 }
93 
94 #endif
95 
96 
97 //! Score particles based on how far outside a sphere they are by
98 //! applying f to the distance.
99 //! \see GenericBoundingSphere3DSingletonScore
100 IMP_GENERIC_OBJECT(BoundingSphere3DSingletonScore, bounding_sphere_3d_singleton_score,
101  UnaryFunction,
102  (UnaryFunction *f, const algebra::Sphere3D &sphere),
103  (f, sphere));
104 
105 IMPCORE_END_NAMESPACE
106 
107 #endif /* IMPCORE_BOUNDING_SPHERE_3D_SINGLETON_SCORE_H */
SphereD< 3 > Sphere3D
Typedef for Python.
Definition: SphereD.h:104
GenericBoundingSphere3DSingletonScore< UnaryFunction > BoundingSphere3DSingletonScore
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
#define IMP_OBJECT_LOG
Set the log level to the object's log level.
Definition: log_macros.h:284
Single variable function.
Macros for various classes.
ParticlesTemp get_particles(Model *m, const ParticleIndexes &ps)
Get the particles from a list of indexes.
#define IMP_LOG_VERBOSE(expr)
Definition: log_macros.h:83
A more IMP-like version of the std::vector.
Definition: Vector.h:50
Simple XYZ decorator.
#define IMP_GENERIC_OBJECT(Name, lcname, targument, carguments, cparguments)
Typedefs a default instantiation for a generic (templated) object.
Definition: object_macros.h:60
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:86
Abstract class for scoring object(s) of type ParticleIndex.
#define IMP_SINGLETON_SCORE_METHODS(Name)
A decorator for a particle with x,y,z coordinates.
Definition: XYZ.h:30
Define SingletonScore.
std::pair< double, double > DerivativePair
A pair representing a function value with its first derivative.
Definition: types.h:22
Score XYZ or XYZR particles based on how far outside a sphere they are.
VectorD< 3 > Vector3D
Definition: VectorD.h:408
virtual ModelObjectsTemp do_get_inputs(Model *m, const ParticleIndexes &pis) const override
Overload this method to specify the inputs.
virtual double evaluate_index(Model *m, ParticleIndex p, DerivativeAccumulator *da) const override
Compute the score and the derivative if needed.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Definition: check_macros.h:168
Simple 3D sphere class.
Decorator for a sphere-like particle.
Class for adding derivatives from restraints to the model.
Compile-time generic restraint and constraint support.
A decorator for a particle with x,y,z coordinates and a radius.
Definition: XYZR.h:27