IMP logo
IMP Reference Guide  2.22.0
The Integrative Modeling Platform
score_functor/SurfaceDistancePairScore.h
Go to the documentation of this file.
1 /**
2  * \file IMP/score_functor/SurfaceDistancePairScore.h
3  * \brief A Score on the distance between a pair of particles.
4  *
5  * Copyright 2007-2022 IMP Inventors. All rights reserved.
6  */
7 
8 #ifndef IMPSCORE_FUNCTOR_SURFACE_DISTANCE_PAIR_SCORE_H
9 #define IMPSCORE_FUNCTOR_SURFACE_DISTANCE_PAIR_SCORE_H
10 
11 #include <IMP/score_functor/score_functor_config.h>
12 #include <IMP/score_functor/internal/surface_helpers.h>
13 #include <IMP/score_functor/internal/direction_helpers.h>
14 #include <IMP/PairScore.h>
15 #include <IMP/pair_macros.h>
16 
17 IMPSCOREFUNCTOR_BEGIN_NAMESPACE
18 
19 //! Create efficient surface distance-based pair scores.
20 /** This class allows one to create efficient distance-based
21  pair scores between a surface and a point in C++ by simply
22  writing a functor (Score) that does the scoring.
23 
24  \note In the passed `ParticleIndexPair`, the first particle must
25  be for the surface.
26 
27  \see SurfaceDistancePairScore
28  \see DistancePairScore
29  \see Score
30 */
31 template <class DistanceScoreT>
33  private:
34  DistanceScoreT ds_;
35 
36  //! Get the distance from the surface to the point.
37  virtual double get_distance(const algebra::Vector3D &center,
38  const algebra::Vector3D &normal,
39  const algebra::Vector3D &point,
40  algebra::Vector3D *delta) const {
41  return internal::get_distance_from_surface(center, normal, point, delta);
42  }
43 
44  public:
45  typedef DistanceScoreT DistanceScore;
46 
48  const DistanceScore &t0,
49  std::string name = "FunctorSurfaceDistancePairScore %1%")
50  : PairScore(name), ds_(t0) {}
51 
52  //! Compute the score and the derivative if needed
53  /** \note In the passed `ParticleIndexPair`, the first particle must
54  be for the surface.
55  */
56  virtual double evaluate_index(Model *m,
57  const ParticleIndexPair &pip,
58  DerivativeAccumulator *da) const override;
59 
60  virtual ModelObjectsTemp do_get_inputs(
61  Model *m, const ParticleIndexes &pis) const override;
62 
63  DistanceScoreT& get_score_functor()
64  { return ds_; }
65 
68 };
69 
70 #ifndef IMP_DOXYGEN
71 template <class DistanceScore>
73  Model *m, const ParticleIndexPair &p,
74  DerivativeAccumulator *da) const {
75  algebra::Vector3D delta; // normal vector from surface to point
76 
77  double dist = get_distance(
78  m->get_sphere(std::get<0>(p)).get_center(),
79  internal::get_direction(m, std::get<0>(p)),
80  m->get_sphere(std::get<1>(p)).get_center(), &delta);
81 
82  // Using squared distance for trivial check currently doesn't work for surfaces
83  // if (ds_.get_is_trivially_zero(m, p, dist * dist)) {
84  // return 0;
85  // }
86 
87  if (da) {
88  std::pair<double, double> sp = ds_.get_score_and_derivative(m, p, dist);
89  m->add_to_coordinate_derivatives(std::get<0>(p), -delta * sp.second, *da);
90  m->add_to_coordinate_derivatives(std::get<1>(p), delta * sp.second, *da);
91  return sp.first;
92  } else {
93  return ds_.get_score(m, p, dist);
94  }
95 }
96 
97 template <class DistanceScore>
98 inline ModelObjectsTemp SurfaceDistancePairScore<
99  DistanceScore>::do_get_inputs(
100  Model *m, const ParticleIndexes &pis) const {
101  ModelObjectsTemp ret;
102  ret += ds_.get_inputs(m, pis);
103  return ret;
104 }
105 #endif
106 
107 
108 //! Create efficient surface height-based pair scores.
109 /** This class allows one to create efficient height-based
110  pair scores between a surface and a point in C++ by simply
111  writing a functor (Score) that does the scoring.
112 
113  \note In the passed `ParticleIndexPair`, the first particle must
114  be for the surface.
115 
116  \see SurfaceDistancePairScore
117  \see DistancePairScore
118  \see Score
119 */
120 #if defined(SWIG) || defined(IMP_DOXYGEN)
121 template <class DistanceScoreT>
123  public:
125  const DistanceScore &t0,
126  std::string name = "FunctorSurfaceHeightPairScore %1%");
127 };
128 #else
129 template <class DistanceScore>
131  DistanceScore> {
132  virtual double get_distance(const algebra::Vector3D &center,
133  const algebra::Vector3D &normal,
134  const algebra::Vector3D &point,
135  algebra::Vector3D *delta) const override {
136  return internal::get_height_above_surface(center, normal, point, delta);
137  }
138  public:
139  SurfaceHeightPairScore(
140  const DistanceScore &t0,
141  std::string name = "FunctorSurfaceHeightPairScore %1%")
142  : SurfaceDistancePairScore<DistanceScore>(t0, name) {}
143 };
144 #endif
145 
146 
147 //! Create efficient surface depth-based pair scores.
148 /** This class allows one to create efficient depth-based
149  pair scores between a surface and a point in C++ by simply
150  writing a functor (Score) that does the scoring.
151 
152  \note In the passed `ParticleIndexPair`, the first particle must
153  be for the surface.
154 
155  \see SurfaceDistancePairScore
156  \see DistancePairScore
157  \see Score
158 */
159 #if defined(SWIG) || defined(IMP_DOXYGEN)
160 template <class DistanceScoreT>
162  public:
164  const DistanceScore &t0,
165  std::string name = "FunctorSurfaceDepthPairScore %1%");
166 };
167 #else
168 template <class DistanceScore>
170  DistanceScore> {
171  virtual double get_distance(const algebra::Vector3D &center,
172  const algebra::Vector3D &normal,
173  const algebra::Vector3D &point,
174  algebra::Vector3D *delta) const override {
175  return internal::get_depth_below_surface(center, normal, point, delta);
176  }
177  public:
178  SurfaceDepthPairScore(
179  const DistanceScore &t0,
180  std::string name = "FunctorSurfaceDepthPairScore %1%")
181  : SurfaceDistancePairScore<DistanceScore>(t0, name) {}
182 };
183 #endif
184 
185 IMPSCOREFUNCTOR_END_NAMESPACE
186 
187 #endif /* IMPSCORE_FUNCTOR_SURFACE_DISTANCE_PAIR_SCORE_H */
Abstract class for scoring object(s) of type ParticleIndexPair.
Definition: PairScore.h:44
Macros for various classes.
Create efficient surface distance-based pair scores.
#define IMP_PAIR_SCORE_METHODS(Name)
Definition: pair_macros.h:25
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
Create efficient surface depth-based pair scores.
Create efficient surface height-based pair scores.
A more IMP-like version of the std::vector.
Definition: Vector.h:50
IMP::Vector< IMP::WeakPointer< ModelObject > > ModelObjectsTemp
Definition: base_types.h:106
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:86
Define PairScore.
VectorD< 3 > Vector3D
Definition: VectorD.h:408
double get_distance(const Line3D &s, const Vector3D &p)
Get closest distance between a line and a point.
Class for adding derivatives from restraints to the model.