| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#ifndef AVOGADRO_CORE_BOND_H |
| 7 |
#define AVOGADRO_CORE_BOND_H |
| 8 |
|
| 9 |
#include "avogadrocore.h" |
| 10 |
|
| 11 |
#include "atom.h" |
| 12 |
|
| 13 |
namespace Avogadro::Core { |
| 14 |
|
| 15 |
/** |
| 16 |
* @class Bond bond.h <avogadro/core/bond.h> |
| 17 |
* The Bond class represents a bond in a molecule. |
| 18 |
* To use the appropriate bond implementation for a specific molecule |
| 19 |
* implementation, use the [MoleculeClass]::BondType typedef. |
| 20 |
*/ |
| 21 |
template <class Molecule_T> |
| 22 |
class BondTemplate |
| 23 |
{ |
| 24 |
public: |
| 25 |
using MoleculeType = Molecule_T; |
| 26 |
using AtomType = typename Molecule_T::AtomType; |
| 27 |
|
| 28 |
/** Creates a new, invalid bond object. */ |
| 29 |
BondTemplate() = default; |
| 30 |
|
| 31 |
/** |
| 32 |
* Creates a bond object representing a bond at index @p i in molecule @p m. |
| 33 |
*/ |
| 34 |
BondTemplate(MoleculeType* m, Index i); |
| 35 |
|
| 36 |
/** |
| 37 |
* @return True if @a this and @a other share the same index and molecule. |
| 38 |
*/ |
| 39 |
bool operator==(const BondTemplate<MoleculeType>& other) const; |
| 40 |
|
| 41 |
/** |
| 42 |
* @return True if @a this and @a other do not share the same index or |
| 43 |
* molecule. |
| 44 |
*/ |
| 45 |
bool operator!=(const BondTemplate<MoleculeType>& other) const; |
| 46 |
|
| 47 |
/** |
| 48 |
* Prefix increment operator. Increment this Bond's index by 1 and return a |
| 49 |
* self-reference. Check isValid() before calling any other methods. |
| 50 |
*/ |
| 51 |
BondTemplate<MoleculeType>& operator++(); |
| 52 |
|
| 53 |
/** |
| 54 |
* Postfix increment operator. Increment this Bond's index by 1 and return a |
| 55 |
* copy of the current Atom. Check isValid() before calling any other methods. |
| 56 |
*/ |
| 57 |
BondTemplate<MoleculeType> operator++(int); |
| 58 |
|
| 59 |
/** |
| 60 |
* Prefix decrement operator. Decrement this Bond's index by 1 and return a |
| 61 |
* self-reference. Check isValid() before calling any other methods. |
| 62 |
*/ |
| 63 |
BondTemplate<MoleculeType>& operator--(); |
| 64 |
|
| 65 |
/** |
| 66 |
* Postfix decrement operator. Decrement this Bond's index by 1 and return a |
| 67 |
* copy of the current Atom. Check isValid() before calling any other methods. |
| 68 |
*/ |
| 69 |
BondTemplate<MoleculeType> operator--(int); |
| 70 |
|
| 71 |
/** |
| 72 |
* @return True if the molecule is set and the index is less than the number |
| 73 |
* of bonds. |
| 74 |
*/ |
| 75 |
bool isValid() const; |
| 76 |
|
| 77 |
/** |
| 78 |
* @return The molecule that contains this Bond. |
| 79 |
*/ |
| 80 |
MoleculeType* molecule() const; |
| 81 |
|
| 82 |
/** |
| 83 |
* @return The index of this bond in molecule(). |
| 84 |
*/ |
| 85 |
Index index() const; |
| 86 |
|
| 87 |
/** |
| 88 |
* An atom in the bond, such that atom1().index() < atom2.index(). |
| 89 |
* @{ |
| 90 |
*/ |
| 91 |
AtomType atom1() const; |
| 92 |
AtomType atom2() const; |
| 93 |
/** @} */ |
| 94 |
|
| 95 |
/** |
| 96 |
* @return The atom in the bond such that returned.index() != index. |
| 97 |
*/ |
| 98 |
AtomType getOtherAtom(Index index) const; |
| 99 |
|
| 100 |
/** |
| 101 |
* @return The atom in the bond such that returned.index() != atom.index(). |
| 102 |
*/ |
| 103 |
AtomType getOtherAtom(AtomType atom) const; |
| 104 |
|
| 105 |
/** |
| 106 |
* The bond's order (single = 1, double = 2, etc.) |
| 107 |
* @{ |
| 108 |
*/ |
| 109 |
void setOrder(unsigned char o); |
| 110 |
unsigned char order() const; |
| 111 |
/** @} */ |
| 112 |
|
| 113 |
/** |
| 114 |
* The length of the bond or 0.0 if the bond is invalid. |
| 115 |
*/ |
| 116 |
Real length() const; |
| 117 |
|
| 118 |
/** |
| 119 |
* A label for the bond (if any) |
| 120 |
*/ |
| 121 |
std::string label() const; |
| 122 |
|
| 123 |
private: |
| 124 |
MoleculeType* m_molecule = nullptr; |
| 125 |
Index m_index = MaxIndex; |
| 126 |
}; |
| 127 |
|
| 128 |
template <class Molecule_T> |
| 129 |
BondTemplate<Molecule_T>::BondTemplate(MoleculeType* m, Index i) |
| 130 |
: m_molecule(m), m_index(i) |
| 131 |
{ |
| 132 |
} |
| 133 |
|
| 134 |
template <class Molecule_T> |
| 135 |
bool BondTemplate<Molecule_T>::operator==( |
| 136 |
const BondTemplate<MoleculeType>& other) const |
| 137 |
{ |
| 138 |
return m_molecule == other.m_molecule && m_index == other.m_index; |
| 139 |
} |
| 140 |
|
| 141 |
template <class Molecule_T> |
| 142 |
bool BondTemplate<Molecule_T>::operator!=( |
| 143 |
const BondTemplate<MoleculeType>& other) const |
| 144 |
{ |
| 145 |
return m_molecule != other.m_molecule || m_index != other.m_index; |
| 146 |
} |
| 147 |
|
| 148 |
template <class Molecule_T> |
| 149 |
BondTemplate<Molecule_T>& BondTemplate<Molecule_T>::operator++() |
| 150 |
{ |
| 151 |
++m_index; |
| 152 |
return *this; |
| 153 |
} |
| 154 |
|
| 155 |
template <class Molecule_T> |
| 156 |
BondTemplate<Molecule_T> BondTemplate<Molecule_T>::operator++(int) |
| 157 |
{ |
| 158 |
BondTemplate<MoleculeType> result(m_molecule, m_index++); |
| 159 |
return result; |
| 160 |
} |
| 161 |
|
| 162 |
template <class Molecule_T> |
| 163 |
BondTemplate<Molecule_T>& BondTemplate<Molecule_T>::operator--() |
| 164 |
{ |
| 165 |
--m_index; |
| 166 |
return *this; |
| 167 |
} |
| 168 |
|
| 169 |
template <class Molecule_T> |
| 170 |
BondTemplate<Molecule_T> BondTemplate<Molecule_T>::operator--(int) |
| 171 |
{ |
| 172 |
BondTemplate<MoleculeType> result(m_molecule, m_index--); |
| 173 |
return result; |
| 174 |
} |
| 175 |
|
| 176 |
template <class Molecule_T> |
| 177 |
bool BondTemplate<Molecule_T>::isValid() const |
| 178 |
{ |
| 179 |
return m_molecule && m_index < m_molecule->bondCount(); |
| 180 |
} |
| 181 |
|
| 182 |
template <class Molecule_T> |
| 183 |
typename BondTemplate<Molecule_T>::MoleculeType* |
| 184 |
BondTemplate<Molecule_T>::molecule() const |
| 185 |
{ |
| 186 |
return m_molecule; |
| 187 |
} |
| 188 |
|
| 189 |
template <class Molecule_T> |
| 190 |
Index BondTemplate<Molecule_T>::index() const |
| 191 |
{ |
| 192 |
return m_index; |
| 193 |
} |
| 194 |
|
| 195 |
template <class Molecule_T> |
| 196 |
typename BondTemplate<Molecule_T>::AtomType BondTemplate<Molecule_T>::atom1() |
| 197 |
const |
| 198 |
{ |
| 199 |
return AtomType(m_molecule, m_molecule->bondPairs()[m_index].first); |
| 200 |
} |
| 201 |
|
| 202 |
template <class Molecule_T> |
| 203 |
typename BondTemplate<Molecule_T>::AtomType BondTemplate<Molecule_T>::atom2() |
| 204 |
const |
| 205 |
{ |
| 206 |
return AtomType(m_molecule, m_molecule->bondPairs()[m_index].second); |
| 207 |
} |
| 208 |
|
| 209 |
template <class Molecule_T> |
| 210 |
typename BondTemplate<Molecule_T>::AtomType |
| 211 |
BondTemplate<Molecule_T>::getOtherAtom(Index index) const |
| 212 |
{ |
| 213 |
if (atom1().index() == index) |
| 214 |
return atom2(); |
| 215 |
else |
| 216 |
return atom1(); |
| 217 |
} |
| 218 |
|
| 219 |
template <class Molecule_T> |
| 220 |
typename BondTemplate<Molecule_T>::AtomType |
| 221 |
BondTemplate<Molecule_T>::getOtherAtom( |
| 222 |
BondTemplate<Molecule_T>::AtomType atom) const |
| 223 |
{ |
| 224 |
return getOtherAtom(atom.index()); |
| 225 |
} |
| 226 |
|
| 227 |
template <class Molecule_T> |
| 228 |
void BondTemplate<Molecule_T>::setOrder(unsigned char o) |
| 229 |
{ |
| 230 |
m_molecule->setBondOrder(m_index, o); |
| 231 |
} |
| 232 |
|
| 233 |
template <class Molecule_T> |
| 234 |
unsigned char BondTemplate<Molecule_T>::order() const |
| 235 |
{ |
| 236 |
return m_molecule->bondOrders()[m_index]; |
| 237 |
} |
| 238 |
|
| 239 |
template <class Molecule_T> |
| 240 |
Real BondTemplate<Molecule_T>::length() const |
| 241 |
{ |
| 242 |
if (!isValid()) |
| 243 |
return 0.0; |
| 244 |
|
| 245 |
return (atom1().position3d() - atom2().position3d()).norm(); |
| 246 |
} |
| 247 |
|
| 248 |
template <class Molecule_T> |
| 249 |
std::string BondTemplate<Molecule_T>::label() const |
| 250 |
{ |
| 251 |
return m_molecule->bondLabel(m_index); |
| 252 |
} |
| 253 |
|
| 254 |
} // namespace Avogadro::Core |
| 255 |
|
| 256 |
#endif // AVOGADRO_CORE_BOND_H |