IMP logo
IMP Reference Guide  develop.4eee3cf66f,2026/08/01
The Integrative Modeling Platform
Refiner.h
Go to the documentation of this file.
1 /**
2  * \file IMP/Refiner.h
3  * \brief Refine a particle into a list of particles.
4  *
5  * Copyright 2007-2026 IMP Inventors. All rights reserved.
6  */
7 
8 #ifndef IMPKERNEL_REFINER_H
9 #define IMPKERNEL_REFINER_H
10 
11 #include <IMP/kernel_config.h>
12 #include "base_types.h"
13 #include "Particle.h"
14 #include "internal/IndexingIterator.h"
15 #include <IMP/deprecation_macros.h>
16 #include <IMP/check_macros.h>
17 #include "model_object_helpers.h"
18 #include <cereal/access.hpp>
19 #include <cereal/types/base_class.hpp>
20 
21 IMPKERNEL_BEGIN_NAMESPACE
22 
23 class Particle;
24 class DerivativeAccumulator;
25 
26 //! Abstract class to implement hierarchical methods.
27 /** The job of this class is to take a single particle and, if
28  appropriate, return a list of particles. These lists can
29  reflect existing relationships, such as the
30  IMP::core::LeavesRefiner, or arbitrary relationships set up
31  for a particular purpose, such as IMP::core::TableRefiner.
32 
33  @note it is assumed that refined particles belong to the same model
34  as the coarse particle
35 */
36 class IMPKERNELEXPORT Refiner : public ParticleInputs, public Object {
37  struct Accessor;
38  bool is_by_ref_;
39 
40  friend class cereal::access;
41  template<class Archive> void serialize(Archive &ar) {
42  // ParticleInputs has no members, so nothing to serialize
43  ar(cereal::base_class<Object>(this), is_by_ref_);
44  }
45 
46  public:
47  //! Constructs the refiner
48  /**
49  @param name object name for refiner
50  @param is_by_ref if true, this refiner is expected to support
51  the get_refined_indexes_by_ref method,
52  for refiners that support faster cached
53  list of particles, etc.
54  */
55  Refiner(std::string name = "Refiner %1%", bool is_by_ref = false);
56 
57  //! Return true if this refiner can refine that particle
58  /** This should not throw, so be careful what fields are touched.
59  */
60  virtual bool get_can_refine(Particle *) const { return false; }
61 
62  //! Refine the passed particle into a set of particles.
63  /** As a precondition can_refine_particle(a) should be true.
64 
65  @param a coarse particle to be refined
66  */
67  virtual const ParticlesTemp get_refined(Particle *a) const = 0;
68 
69  //! Return the indexes of the particles returned by get_refined()
70  /** Return the indexes of the particles returned by get_refined()
71  for particle pi in model m.
72 
73  @param m,pi model and particle index of coarse particle to be refined
74 
75  @note assumes that the refined particles are also in model m
76  */
77  virtual ParticleIndexes get_refined_indexes
78  (Model *m, ParticleIndex pi) const;
79 
80  //! Return the indexes of the particles returned by get_refined()
81  /** Return the indexes of the particles returned by get_refined()
82  for particle pi in model m by reference (possibly faster).
83 
84  @param m,pi model and particle index of coarse particle to be refined
85 
86  @note assumes that get_is_by_ref() is true.
87  @note assumes that the refined particles are also in model m
88  */
89  virtual ParticleIndexes const& get_refined_indexes_by_ref
90  (Model *m, ParticleIndex pi) const
91  {
92  IMP_ALWAYS_CHECK(false,
93  "This refiner does not support"
94  " get_refined_indexes_by_ref()",
96  IMP_UNUSED(m);
97  IMP_UNUSED(pi);
98  }
99 
100  //! returns true if this refiner supports
101  //! get_refined_indexes_by_ref() (e.g. FixedRefiner)
102  bool get_is_by_ref_supported() { return is_by_ref_; };
103 
104  //! Get the ith refined particle.
105  /** As a precondition can_refine_particle(a) should be true.
106  */
107  virtual Particle *get_refined(Particle *a, unsigned int i) const {
108  return get_refined(a)[i];
109  }
110 
111  /** As a precondition can_refine_particle(a) should be true.
112  */
113  virtual unsigned int get_number_of_refined(Particle *a) const {
114  return get_refined(a).size();
115  }
116 
117 #ifndef SWIG
118  /** @name Iterating through the set of refined particles
119 
120  Using iterators can be more efficient than using the bulk
121  get_refined(), however it is not necessarily so.
122  @{
123  */
124  typedef internal::IndexingIterator<Accessor> RefinedIterator;
125  RefinedIterator refined_begin(Particle *a) const;
126  RefinedIterator refined_end(Particle *a) const;
127 /** @} */
128 #endif
129 };
130 //! a collection of Refiner objects
132 
133 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
134 struct Refiner::Accessor {
135  // can't reference count since swig memory management is broken
136  Particle *p_;
137  const Refiner *r_;
138  Accessor(Particle *p, const Refiner *r) : p_(p), r_(r) {}
139  Accessor() {}
140  typedef Particle *result_type;
141  Particle *operator()(unsigned int i) const { return r_->get_refined(p_, i); }
142  bool operator==(const Accessor &o) const { return p_ == o.p_ && r_ == o.r_; }
143 };
144 #endif
145 
146 inline Refiner::RefinedIterator Refiner::refined_begin(Particle *a) const {
147  return RefinedIterator(Accessor(a, this), 0);
148 }
149 inline Refiner::RefinedIterator Refiner::refined_end(Particle *a) const {
150  return RefinedIterator(Accessor(a, this), get_number_of_refined(a));
151 }
152 
153 IMPKERNEL_END_NAMESPACE
154 
155 #endif /* IMPKERNEL_REFINER_H */
Control display of deprecation information.
Basic types used by IMP.
virtual Particle * get_refined(Particle *a, unsigned int i) const
Get the ith refined particle.
Definition: Refiner.h:107
Base class for objects that take particle arguments and read from them.
A more IMP-like version of the std::vector.
Definition: Vector.h:50
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:86
virtual bool get_can_refine(Particle *) const
Return true if this refiner can refine that particle.
Definition: Refiner.h:60
Common base class for heavy weight IMP objects.
Definition: Object.h:111
#define IMP_UNUSED(variable)
Classes used in the construction of ModelObjects.
virtual unsigned int get_number_of_refined(Particle *a) const
Definition: Refiner.h:113
Classes to handle individual model particles. (Note that implementation of inline functions is in int...
#define IMP_OBJECTS(Name, PluralName)
Define the types for storing lists of object pointers.
Definition: object_macros.h:44
Helper macros for throwing and handling exceptions.
Abstract class to implement hierarchical methods.
Definition: Refiner.h:36
Class to handle individual particles of a Model object.
Definition: Particle.h:45
bool get_is_by_ref_supported()
Definition: Refiner.h:102
#define IMP_ALWAYS_CHECK(condition, message, exception_name)
Throw an exception if a check fails.
Definition: check_macros.h:61
An exception for an invalid value being passed to IMP.
Definition: exception.h:136