00001 /** 00002 * \file TypedPairScore.h 00003 * \brief Delegate to another PairScore depending on particle types. 00004 * 00005 * Copyright 2007-2010 IMP Inventors. All rights reserved. 00006 */ 00007 00008 #ifndef IMPCORE_TYPED_PAIR_SCORE_H 00009 #define IMPCORE_TYPED_PAIR_SCORE_H 00010 00011 #include "core_config.h" 00012 #include <IMP/PairScore.h> 00013 00014 IMPCORE_BEGIN_NAMESPACE 00015 00016 //! Delegate to another PairScore depending on particle types. 00017 /** Each particle is given an integer type, which is used at evaluate time 00018 to select a PairScore to use for a given pair of particles. (The ordering 00019 of the particles does not matter.) After creating the object, call 00020 set_pair_score() to tell it the PairScore objects you want to use for each 00021 pair of particle types. 00022 */ 00023 class IMPCOREEXPORT TypedPairScore : public PairScore 00024 { 00025 // The key used for the particle types. 00026 IntKey typekey_; 00027 typedef std::map<std::pair<Int,Int>, 00028 IMP::internal::OwnerPointer<PairScore> > ScoreMap; 00029 // Mapping from particle types to PairScores. 00030 ScoreMap score_map_; 00031 // Whether to throw an exception for invalid particle types. 00032 bool allow_invalid_types_; 00033 00034 PairScore *get_pair_score(const ParticlePair &pp) const; 00035 public: 00036 //! Constructor. 00037 /** \param[in] typekey The IntKey used to denote the type of each particle. 00038 \param[in] allow_invalid_types Desired behavior for particle pairs that 00039 have types not covered by set_pair_score(). If true, the score 00040 returned for these pairs is zero. If false, evaluate() raises 00041 a ValueException. 00042 */ 00043 TypedPairScore(IntKey typekey, bool allow_invalid_types=true); 00044 00045 //! Set the particle's type. 00046 /** At evaluate time, if a given particle does not have the typekey 00047 attribute, this method is called. Here it does nothing, but it could 00048 be overridden in a subclass to automatically set the type of a particle, 00049 e.g. from other particle attributes such as an atom or residue name. 00050 */ 00051 virtual void set_particle_type(Particle *p) const {} 00052 00053 //! Set the PairScore to delegate to for a given pair of particle types. 00054 /** \param[in] ps PairScore to use at evaluate time. 00055 \param[in] atype First particle type. 00056 \param[in] btype Second particle type. 00057 */ 00058 void set_pair_score(PairScore *ps, Int atype, Int btype) { 00059 score_map_[std::pair<Int,Int>(std::min(atype, btype), 00060 std::max(atype, btype))] 00061 = IMP::internal::OwnerPointer<PairScore>(ps); 00062 } 00063 00064 IMP_PAIR_SCORE(TypedPairScore); 00065 }; 00066 00067 IMPCORE_END_NAMESPACE 00068 00069 #endif /* IMPCORE_TYPED_PAIR_SCORE_H */