IMP  2.3.1
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-2014 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>
12 #include <IMP/SingletonPredicate.h>
13 #include <IMP/singleton_macros.h>
14 #include "XYZ.h"
15 
16 IMPCORE_BEGIN_NAMESPACE
17 //! Return 1 if the XYZ is in the bounding box, 0 otherwise.
20 
21  public:
23  std::string name =
24  "InBoundingBox3DSingletonPredicate%1%")
25  : SingletonPredicate(name), bb_(bb) {}
27  IMP_OVERRIDE {
28  return bb_.get_contains(XYZ(m, pi).get_coordinates()) ? 1 : 0;
29  }
33  ret += IMP::get_particles(m, pi);
34  return ret;
35  }
38 };
39 
40 //! Return the value of an int attribute as the predicate value.
42  IntKey bb_;
43 
44  public:
45  AttributeSingletonPredicate(IntKey bb, std::string name =
46  "AttributeSingletonPredicate%1%")
47  : SingletonPredicate(name), bb_(bb) {}
49  IMP_OVERRIDE {
50  return m->get_attribute(bb_, pi);
51  }
55  ret += IMP::get_particles(m, pi);
56  return ret;
57  }
60 };
61 
62 //! Return 1 if two XYZRs collide.
64  public:
65  IsCollisionPairPredicate(std::string name = "CollisionPairPredicate%1%")
66  : PairPredicate(name) {}
68  const kernel::ParticleIndexPair &pi) const
69  IMP_OVERRIDE {
70  Float sr =
71  m->get_sphere(pi[0]).get_radius() + m->get_sphere(pi[1]).get_radius();
72 #if IMP_HAS_CHECKS > 1
73  bool check_collisions = (get_distance(XYZR(m, pi[0]), XYZR(m, pi[1])) <= 0);
74 #endif
75  for (unsigned int i = 0; i < 3; ++i) {
76  double delta = std::abs(m->get_sphere(pi[0]).get_center()[i] -
77  m->get_sphere(pi[1]).get_center()[i]);
78  if (delta >= sr) {
79  IMP_INTERNAL_CHECK(!check_collisions, "Should be a collision");
80  return 0;
81  }
82  }
83  bool col =
84  algebra::get_squared_distance(m->get_sphere(pi[0]).get_center(),
85  m->get_sphere(pi[1]).get_center()) <
86  algebra::get_squared(sr);
87  IMP_INTERNAL_CHECK(col == check_collisions, "Don't match");
88  return col ? 1 : 0;
89  }
90 
94  ret += IMP::get_particles(m, pi);
95  return ret;
96  }
99 };
100 
101 /** Use a predicate to determine which score to apply. One can use this to,
102  for example, truncate a score using a bounding box.*/
103 template <class Predicate, class Score = SingletonScore>
107  int offset_;
108  Score *get_score(int val) const {
109  if (val < offset_ || val > scores_.size() + offset_) {
110  return nullptr;
111  } else {
112  return scores_[val + offset_];
113  }
114  }
115 
116  public:
117  PredicateSingletonScore(Predicate *pred,
118  std::string name = "PredicateScore%1%")
119  : SingletonScore(name), pred_(pred), offset_(0) {}
120  void set_singleton_score(int val, Score *score) {
121  IMP_USAGE_CHECK(val >= offset_,
122  "Negative predicate values not supported yet");
123  scores_.resize(std::max<int>(val + 1, scores_.size()));
124  scores_[val] = score;
125  }
126  virtual double evaluate_index(kernel::Model *m, kernel::ParticleIndex p,
128  virtual kernel::ModelObjectsTemp do_get_inputs(
129  kernel::Model *m, const kernel::ParticleIndexes &pis) const IMP_OVERRIDE;
132 };
133 
134 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
135 template <class Predicate, class Score>
138  DerivativeAccumulator *da) const {
139  int val = pred_->get_value(m, p);
140  Score *s = get_score(val);
141  if (s) {
142  return s->Score::evaluate_index(m, p, da);
143  } else {
144  return 0;
145  }
146 }
147 #endif
148 
149 /** Other overloads can be created as needed.*/
150 template <class Predicate, class Score>
151 inline PredicateSingletonScore<Predicate, Score> *
152 create_predicates_singleton_score(Predicate *pred, int val, Score *score) {
155  ret->set_score(val, score);
156  return ret;
157 }
158 
159 IMPCORE_END_NAMESPACE
160 
161 #endif /* IMPCORE_PREDICATES_H */
A base class for Keys.
Definition: kernel/Key.h:46
Class for adding derivatives from restraints to the model.
Return 1 if the XYZ is in the bounding box, 0 otherwise.
Definition: predicates.h:18
virtual kernel::ModelObjectsTemp do_get_inputs(kernel::Model *m, const kernel::ParticleIndexes &pi) const
Definition: predicates.h:30
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
ParticlesTemp get_particles(kernel::Model *m, const ParticleIndexes &ps)
virtual int get_value_index(kernel::Model *m, kernel::ParticleIndex pi) const
Compute the predicate and the derivative if needed.
Definition: predicates.h:26
virtual int get_value_index(kernel::Model *m, kernel::ParticleIndex pi) const
Compute the predicate and the derivative if needed.
Definition: predicates.h:48
double get_squared_distance(const VectorD< D > &v1, const VectorD< D > &v2)
Compute the squared distance between two vectors.
Definition: VectorD.h:201
Import IMP/kernel/singleton_macros.h in the namespace.
IMP::kernel::SingletonPredicate SingletonPredicate
A smart pointer to a reference counted object.
Definition: Pointer.h:87
virtual kernel::ModelObjectsTemp do_get_inputs(kernel::Model *m, const kernel::ParticleIndexes &pi) const
Definition: predicates.h:91
Simple XYZ decorator.
#define IMP_INTERNAL_CHECK(expr, message)
An assertion to check for internal errors in IMP. An IMP::ErrorException will be thrown.
Definition: check_macros.h:141
Import IMP/kernel/SingletonPredicate.h in the namespace.
A class to store an fixed array of same-typed values.
Definition: Array.h:33
double get_distance(const Plane3D &pln, const Vector3D &p)
Return the distance between a plane and a point in 3D.
Definition: Plane3D.h:71
Return the value of an int attribute as the predicate value.
Definition: predicates.h:41
virtual int get_value_index(kernel::Model *m, const kernel::ParticleIndexPair &pi) const
Compute the predicate and the derivative if needed.
Definition: predicates.h:67
#define IMP_PAIR_PREDICATE_METHODS(Name)
Define extra the functions needed for a PairPredicate.
A decorator for a particle with x,y,z coordinates.
Definition: XYZ.h:30
#define IMP_SINGLETON_PREDICATE_METHODS(Name)
Define extra the functions needed for a SingletonPredicate.
Abstract predicate function.
Abstract predicate function.
virtual kernel::ModelObjectsTemp do_get_inputs(kernel::Model *m, const kernel::ParticleIndexes &pi) const
Definition: predicates.h:52
Return 1 if two XYZRs collide.
Definition: predicates.h:63
IMP::kernel::PairPredicate PairPredicate
Abstract class for scoring object(s) of type Particle.
double Float
Basic floating-point value (could be float, double...)
Definition: types.h:20
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Definition: check_macros.h:170
PredicateSingletonScore< Predicate, Score > * create_predicates_singleton_score(Predicate *pred, int val, Score *score)
Definition: predicates.h:152
IMP::kernel::SingletonScore SingletonScore
#define IMP_OVERRIDE
Cause a compile error if this method does not override a parent method.
#define IMP_SINGLETON_SCORE_METHODS(Name)
Class for storing model, its restraints, constraints, and particles.
Definition: kernel/Model.h:73
A decorator for a particle with x,y,z coordinates and a radius.
Definition: XYZR.h:27