IMP  2.0.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-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  public:
22  std::string name
23  ="InBoundingBox3DSingletonPredicate%1%"):
24  SingletonPredicate(name), bb_(bb){}
26  return bb_.get_contains(XYZ(m,pi)
27  .get_coordinates())?1:0;
28  ,{
29  ModelObjectsTemp ret;
30  ret+= IMP::get_particles(m, pi);
31  return ret;
32  });
33 
34 };
35 /** Return the value of an int attribute as the predicate value.*/
37  IntKey bb_;
38  public:
40  std::string name
41  ="AttributeSingletonPredicate%1%"):
42  SingletonPredicate(name), bb_(bb){}
44  return m->get_attribute(bb_, pi) ,{
45  ModelObjectsTemp ret;
46  ret+= IMP::get_particles(m, pi);
47  return ret;
48  });
49 };
50 
51 /** Use a predicate to determine which score to apply. One can use this to,
52  for example, truncate a score using a bounding box.*/
53 template <class Predicate, class Score=SingletonScore>
55  OwnerPointer<Predicate> pred_;
57  int offset_;
58  Score *get_score(int val) const {
59  if (val < offset_ || val > scores_.size()+offset_) {
60  return nullptr;
61  } else {
62  return scores_[val+offset_];
63  }
64  }
65  public:
66  PredicateSingletonScore(Predicate* pred,
67  std::string name="PredicateScore%1%"):
68  SingletonScore(name), pred_(pred), offset_(0){}
69  void set_singleton_score(int val, Score *score) {
70  IMP_USAGE_CHECK(val>=offset_,
71  "Negative predicate values not supported yet");
72  scores_.resize(std::max<int>(val+1, scores_.size()));
73  scores_[val]=score;
74  }
76 };
77 
78 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
79 template <class Predicate, class Score>
82  DerivativeAccumulator *da) const {
83  int val= pred_->get_value(m,p);
84  Score *s= get_score(val);
85  if (s) {
86  return s->Score::evaluate_index(m, p, da);
87  } else {
88  return 0;
89  }
90 }
91 template <class Predicate, class Score>
92 inline void PredicateSingletonScore<Predicate, Score>
93 ::do_show(std::ostream &) const {
94 }
95 #endif
96 
97 /** Other overloads can be created as needed.*/
98 template <class Predicate, class Score>
99 inline PredicateSingletonScore<Predicate, Score>*
101  int val, Score *score) {
102  Pointer<PredicateSingletonScore<Predicate, Score> > ret
104  ret->set_score(val, score);
105  return ret;
106 }
107 
108 IMPCORE_END_NAMESPACE
109 
110 #endif /* IMPCORE_PREDICATES_H */