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