IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
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>
17#include <IMP/UnaryFunction.h>
19#include "XYZ.h"
20#include "XYZR.h"
21
22IMPCORE_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 */
30template <class UF>
31class GenericBoundingSphere3DSingletonScore : public SingletonScore {
33 algebra::Sphere3D sphere_;
34
35 public:
36 GenericBoundingSphere3DSingletonScore(UF *f, const algebra::Sphere3D &sphere);
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
48 IMP_SINGLETON_SCORE_METHODS(GenericBoundingSphere3DSingletonScore);
49 IMP_OBJECT_METHODS(GenericBoundingSphere3DSingletonScore);
50};
51
52#if !defined(SWIG) && !defined(IMP_DOXYGEN)
53template <class UF>
54GenericBoundingSphere3DSingletonScore<UF>::GenericBoundingSphere3DSingletonScore(
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}
63template <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
100IMP_GENERIC_OBJECT(BoundingSphere3DSingletonScore, bounding_sphere_3d_singleton_score,
101 UnaryFunction,
102 (UnaryFunction *f, const algebra::Sphere3D &sphere),
103 (f, sphere));
104
105IMPCORE_END_NAMESPACE
106
107#endif /* IMPCORE_BOUNDING_SPHERE_3D_SINGLETON_SCORE_H */
Define SingletonScore.
Simple 3D sphere class.
Single variable function.
Decorator for a sphere-like particle.
Simple XYZ decorator.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Class for adding derivatives from restraints to the model.
Class for storing model, its restraints, constraints, and particles.
Definition Model.h:111
Abstract class for scoring object(s) of type ParticleIndex.
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.
A decorator for a particle with x,y,z coordinates and a radius.
Definition XYZR.h:27
A decorator for a particle with x,y,z coordinates.
Definition XYZ.h:30
Compile-time generic restraint and constraint support.
#define IMP_LOG_VERBOSE(expr)
Definition log_macros.h:83
#define IMP_OBJECT_LOG
Set the log level to the object's log level.
Definition log_macros.h:284
SphereD< 3 > Sphere3D
Typedef for Python.
Definition SphereD.h:103
GenericBoundingSphere3DSingletonScore< UnaryFunction > BoundingSphere3DSingletonScore
Index< ParticleIndexTag > ParticleIndex
Definition base_types.h:196
ParticlesTemp get_particles(Model *m, const ParticleIndexes &ps)
Get the particles from a list of indexes.
Vector< ParticleIndex, std::allocator< ParticleIndex > > ParticleIndexes
A list of particles, each identified by its ParticleIndex.
Definition base_types.h:199
VectorD< 3 > Vector3D
Definition VectorD.h:407
std::pair< double, double > DerivativePair
A pair representing a function value with its first derivative.
Definition types.h:22
IMP::Vector< IMP::WeakPointer< ModelObject > > ModelObjectsTemp
Definition base_types.h:122
#define IMP_GENERIC_OBJECT(Name, lcname, targument, carguments, cparguments)
Typedefs a default instantiation for a generic (templated) object.
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Macros for various classes.
#define IMP_SINGLETON_SCORE_METHODS(Name)
A smart pointer to a ref-counted Object that is a class member.
Definition Pointer.h:144