IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
The Integrative Modeling Platform
predicates.h
Go to the documentation of this file.
1/**
2 * \file IMP/core/predicates.h
3 * \brief Score particles based on a bounding box
4 *
5 * Copyright 2007-2022 IMP Inventors. All rights reserved.
6 */
7
8#ifndef IMPCORE_PREDICATES_H
9#define IMPCORE_PREDICATES_H
10
11#include <IMP/core/core_config.h>
14#include "XYZ.h"
15
16IMPCORE_BEGIN_NAMESPACE
17//! Return 1 if the XYZ is in the bounding box, 0 otherwise.
18class InBoundingBox3DSingletonPredicate : public SingletonPredicate {
20
21 public:
22 InBoundingBox3DSingletonPredicate(const algebra::BoundingBox3D &bb,
23 std::string name =
24 "InBoundingBox3DSingletonPredicate%1%")
25 : SingletonPredicate(name), bb_(bb) {}
26 virtual int get_value_index(Model *m, ParticleIndex pi) const
27 override {
28 return bb_.get_contains(XYZ(m, pi).get_coordinates()) ? 1 : 0;
29 }
31 Model *m, const ParticleIndexes &pi) const override {
33 ret += IMP::get_particles(m, pi);
34 return ret;
35 }
37 IMP_OBJECT_METHODS(InBoundingBox3DSingletonPredicate);
38};
39
40//! Return the value of an int attribute as the predicate value.
41class AttributeSingletonPredicate : public SingletonPredicate {
42 IntKey bb_;
43
44 public:
45 AttributeSingletonPredicate(IntKey bb, std::string name =
46 "AttributeSingletonPredicate%1%")
47 : SingletonPredicate(name), bb_(bb) {}
48 virtual int get_value_index(Model *m, ParticleIndex pi) const
49 override {
50 return m->get_attribute(bb_, pi);
51 }
53 Model *m, const ParticleIndexes &pi) const override {
55 ret += IMP::get_particles(m, pi);
56 return ret;
57 }
59 IMP_OBJECT_METHODS(AttributeSingletonPredicate);
60};
61
62//! Return 1 if two XYZRs collide.
63class IsCollisionPairPredicate : public PairPredicate {
64 public:
65 IsCollisionPairPredicate(std::string name = "CollisionPairPredicate%1%")
66 : PairPredicate(name) {}
67 virtual int get_value_index(Model *m,
68 const ParticleIndexPair &pi) const
69 override {
70 Float sr =
71 m->get_sphere(std::get<0>(pi)).get_radius()
72 + m->get_sphere(std::get<1>(pi)).get_radius();
73#if IMP_HAS_CHECKS > 1
74 bool check_collisions = (get_distance(XYZR(m, std::get<0>(pi)),
75 XYZR(m, std::get<1>(pi))) <= 0);
76#endif
77 for (unsigned int i = 0; i < 3; ++i) {
78 double delta = std::abs(m->get_sphere(std::get<0>(pi)).get_center()[i] -
79 m->get_sphere(std::get<1>(pi)).get_center()[i]);
80 if (delta >= sr) {
81 IMP_INTERNAL_CHECK(!check_collisions, "Should be a collision");
82 return 0;
83 }
84 }
85 bool col =
87 m->get_sphere(std::get<0>(pi)).get_center(),
88 m->get_sphere(std::get<1>(pi)).get_center()) <
89 algebra::get_squared(sr);
90 IMP_INTERNAL_CHECK(col == check_collisions, "Don't match");
91 return col ? 1 : 0;
92 }
93
95 Model *m, const ParticleIndexes &pi) const override {
97 ret += IMP::get_particles(m, pi);
98 return ret;
99 }
101 IMP_OBJECT_METHODS(IsCollisionPairPredicate);
102};
103
104/** Use a predicate to determine which score to apply. One can use this to,
105 for example, truncate a score using a bounding box.*/
106template <class Predicate, class Score = SingletonScore>
107class PredicateSingletonScore : public SingletonScore {
110 int offset_;
111 Score *get_score(int val) const {
112 if (val < offset_ || val > scores_.size() + offset_) {
113 return nullptr;
114 } else {
115 return scores_[val + offset_];
116 }
117 }
118
119 public:
120 PredicateSingletonScore(Predicate *pred,
121 std::string name = "PredicateScore%1%")
122 : SingletonScore(name), pred_(pred), offset_(0) {}
123 void set_singleton_score(int val, Score *score) {
124 IMP_USAGE_CHECK(val >= offset_,
125 "Negative predicate values not supported yet");
126 scores_.resize(std::max<int>(val + 1, scores_.size()));
127 scores_[val] = score;
128 }
129 virtual double evaluate_index(Model *m, ParticleIndex p,
130 DerivativeAccumulator *da) const override;
132 Model *m, const ParticleIndexes &pis) const override;
133 IMP_SINGLETON_SCORE_METHODS(PredicateSingletonScore);
134 IMP_OBJECT_METHODS(PredicateSingletonScore);
135};
136
137#if !defined(SWIG) && !defined(IMP_DOXYGEN)
138template <class Predicate, class Score>
140 Model *m, ParticleIndex p,
141 DerivativeAccumulator *da) const {
142 int val = pred_->get_value(m, p);
143 Score *s = get_score(val);
144 if (s) {
145 return s->Score::evaluate_index(m, p, da);
146 } else {
147 return 0;
148 }
149}
150#endif
151
152/** Other overloads can be created as needed.*/
153template <class Predicate, class Score>
154inline PredicateSingletonScore<Predicate, Score> *
155create_predicates_singleton_score(Predicate *pred, int val, Score *score) {
158 ret->set_score(val, score);
159 return ret;
160}
161
162IMPCORE_END_NAMESPACE
163
164#endif /* IMPCORE_PREDICATES_H */
Define SingletonPredicate.
Simple XYZ decorator.
#define IMP_INTERNAL_CHECK(expr, message)
An assertion to check for internal errors in IMP. An IMP::ErrorException will be thrown.
#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
A more IMP-like version of the std::vector.
Definition Vector.h:60
Return the value of an int attribute as the predicate value.
Definition predicates.h:41
virtual int get_value_index(Model *m, ParticleIndex pi) const override
Compute the predicate and the derivative if needed.
Definition predicates.h:48
virtual ModelObjectsTemp do_get_inputs(Model *m, const ParticleIndexes &pi) const override
Overload this method to specify the inputs.
Definition predicates.h:52
Return 1 if the XYZ is in the bounding box, 0 otherwise.
Definition predicates.h:18
virtual ModelObjectsTemp do_get_inputs(Model *m, const ParticleIndexes &pi) const override
Overload this method to specify the inputs.
Definition predicates.h:30
virtual int get_value_index(Model *m, ParticleIndex pi) const override
Compute the predicate and the derivative if needed.
Definition predicates.h:26
Return 1 if two XYZRs collide.
Definition predicates.h:63
virtual ModelObjectsTemp do_get_inputs(Model *m, const ParticleIndexes &pi) const override
Overload this method to specify the inputs.
Definition predicates.h:94
virtual int get_value_index(Model *m, const ParticleIndexPair &pi) const override
Compute the predicate and the derivative if needed.
Definition predicates.h:67
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
BoundingBoxD< 3 > BoundingBox3D
Typedef for Python.
double get_squared_distance(const VectorD< D > &v1, const VectorD< D > &v2)
Compute the squared distance between two vectors.
Definition VectorD.h:187
PredicateSingletonScore< Predicate, Score > * create_predicates_singleton_score(Predicate *pred, int val, Score *score)
Definition predicates.h:155
double get_distance(const Surface &s, const XYZR &d)
Get distance from sphere to surface.
Definition Surface.h:153
Index< ParticleIndexTag > ParticleIndex
Definition base_types.h:196
ParticlesTemp get_particles(Model *m, const ParticleIndexes &ps)
Get the particles from a list of indexes.
double Float
Basic floating-point value (could be float, double...).
Definition types.h:19
Vector< ParticleIndex, std::allocator< ParticleIndex > > ParticleIndexes
A list of particles, each identified by its ParticleIndex.
Definition base_types.h:199
Array< 2, ParticleIndex > ParticleIndexPair
An ordered pair of particles, each identified by its ParticleIndex.
Definition base_types.h:202
Key< 1 > IntKey
The type used to identify int attributes in the Particles.
Definition base_types.h:36
IMP::Vector< IMP::WeakPointer< ModelObject > > ModelObjectsTemp
Definition base_types.h:122
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
#define IMP_PAIR_PREDICATE_METHODS(Name)
Define extra the functions needed for a PairPredicate.
Macros for various classes.
#define IMP_SINGLETON_PREDICATE_METHODS(Name)
Define extra the functions needed for a SingletonPredicate.
#define IMP_SINGLETON_SCORE_METHODS(Name)
A smart pointer to a ref-counted Object that is a class member.
Definition Pointer.h:144
A smart pointer to a reference counted object.
Definition Pointer.h:88