/****************************************************************************** This source file is part of the Avogadro project. This source code is released under the 3-Clause BSD License, (see "LICENSE"). ******************************************************************************/ #ifndef AVOGADRO_CORE_ATOM_H #define AVOGADRO_CORE_ATOM_H #include "avogadrocore.h" #include "elements.h" #include "vector.h" namespace Avogadro::Core { enum AtomHybridization { PerceivedOctaheadral = -6, PerceivedTrigonalBipyramidal = -5, PerceivedSquarePlanar = -4, PerceivedSP3 = -3, PerceivedSP2 = -2, PerceivedSP = -1, HybridizationUnknown = 0, SP = 1, SP2 = 2, SP3 = 3, SquarePlanar = 4, TrigonalBipyramidal = 5, Octahedral = 6 }; enum AtomicNumber : unsigned char { Hydrogen = 1, Carbon = 6, Nitrogen = 7, Oxygen = 8 }; /** * @class Atom atom.h * The atom class represents an atom in a molecule. * To use the appropriate atom implementation for a specific molecule * implementation, use the [MoleculeClass]::AtomType typedef. */ template class AtomTemplate { public: using MoleculeType = Molecule_T; /** Creates a new, invalid atom object. */ AtomTemplate(); /** * Creates a new atom object representing the atom at index @p i in molecule * @p m. */ AtomTemplate(MoleculeType* m, Index i); /** * @return True if @a this and @a other share the same index and molecule. */ bool operator==(const AtomTemplate& other) const; /** * @return True if @a this and @a other do not share the same index or * molecule. */ bool operator!=(const AtomTemplate& other) const; /** * Prefix increment operator. Increment this Atom's index by 1 and return a * self-reference. Check isValid() before calling any other methods. */ AtomTemplate& operator++(); /** * Postfix increment operator. Increment this Atom's index by 1 and return a * copy of the current Atom. Check isValid() before calling any other methods. */ AtomTemplate operator++(int); /** * Prefix decrement operator. Decrement this Atom's index by 1 and return a * self-reference. Check isValid() before calling any other methods. */ AtomTemplate& operator--(); /** * Postfix decrement operator. Decrement this Atom's index by 1 and return a * copy of the current Atom. Check isValid() before calling any other methods. */ AtomTemplate operator--(int); /** * @return True if the molecule is set and the index is less than the number * of atoms. */ bool isValid() const; /** * @return The molecule that contains this Atom. */ MoleculeType* molecule() const; /** * @return The index of this atom in molecule(). */ Index index() const; /** * The proton count of the atom. * @{ */ void setAtomicNumber(unsigned char num); unsigned char atomicNumber() const; /** @} */ /** * @return The mass of this atom */ Real mass() const; /** * The 2D position of this atom. * @note Not all molecule types support 2D positions. If the current * MoleculeType class does not, calling this method will result in a * compilation error. * @{ */ void setPosition2d(const Vector2& pos); Vector2 position2d() const; /** @} */ /** * The 3D position of this atom. * @{ */ void setPosition3d(const Vector3& pos); Vector3 position3d() const; /** @} */ /** * The hybridization / coordination of this atom * @{ */ void setHybridization(AtomHybridization hyb); AtomHybridization hybridization() const; /** @} */ /** * The formal charge of this atom * @{ */ void setFormalCharge(signed char charge); signed char formalCharge() const; /** @} */ /** * The isotope of this atom * @{ */ void setIsotope(unsigned short isotope); unsigned short isotope() const; /** @} */ /** * The color of this atom * @{ */ void setColor(Vector3ub color); Vector3ub color() const; /** @} */ /** * The layer of this atom * @{ */ void setLayer(size_t layer); size_t layer() const; /** @} */ /** * Is the atom selected. * {@ */ void setSelected(bool selected); bool selected() const; /** @} */ /** * The force on this atom. * {@ */ void setForceVector(const Vector3& force); Vector3 forceVector() const; void setLabel(const std::string& label); std::string label() const; /** @} */ private: MoleculeType* m_molecule; Index m_index; }; template AtomTemplate::AtomTemplate() : m_molecule(nullptr), m_index(MaxIndex) { } template AtomTemplate::AtomTemplate(MoleculeType* m, Index i) : m_molecule(m), m_index(i) { } template bool AtomTemplate::operator==( const AtomTemplate& other) const { return m_molecule == other.m_molecule && m_index == other.m_index; } template bool AtomTemplate::operator!=( const AtomTemplate& other) const { return m_molecule != other.m_molecule || m_index != other.m_index; } template AtomTemplate& AtomTemplate::operator++() { ++m_index; return *this; } template AtomTemplate AtomTemplate::operator++(int) { AtomTemplate result(m_molecule, m_index++); return result; } template AtomTemplate& AtomTemplate::operator--() { --m_index; return *this; } template AtomTemplate AtomTemplate::operator--(int) { AtomTemplate result(m_molecule, m_index--); return result; } template bool AtomTemplate::isValid() const { return m_molecule && m_index < m_molecule->atomCount(); } template typename AtomTemplate::MoleculeType* AtomTemplate::molecule() const { return m_molecule; } template Index AtomTemplate::index() const { return m_index; } template void AtomTemplate::setAtomicNumber(unsigned char num) { m_molecule->setAtomicNumber(m_index, num); } template unsigned char AtomTemplate::atomicNumber() const { return m_molecule->atomicNumber(m_index); } template Real AtomTemplate::mass() const { // TODO: handle isotopes return Elements::mass(atomicNumber()); } template void AtomTemplate::setPosition2d(const Vector2& pos) { m_molecule->setAtomPosition2d(m_index, pos); } template Vector2 AtomTemplate::position2d() const { return m_molecule->atomPositions2d().size() > 0 ? m_molecule->atomPositions2d()[m_index] : Vector2::Zero(); } template void AtomTemplate::setPosition3d(const Vector3& pos) { m_molecule->setAtomPosition3d(m_index, pos); } template Vector3 AtomTemplate::position3d() const { return m_molecule->atomPositions3d().size() > 0 ? m_molecule->atomPositions3d()[m_index] : Vector3::Zero(); } template void AtomTemplate::setHybridization(AtomHybridization hyb) { m_molecule->setHybridization(m_index, hyb); } template AtomHybridization AtomTemplate::hybridization() const { return m_molecule->hybridization(m_index); } template void AtomTemplate::setFormalCharge(signed char charge) { m_molecule->setFormalCharge(m_index, charge); } template signed char AtomTemplate::formalCharge() const { return m_molecule->formalCharge(m_index); } template void AtomTemplate::setIsotope(unsigned short isotope) { m_molecule->setIsotope(m_index, isotope); } template unsigned short AtomTemplate::isotope() const { return m_molecule->isotope(m_index); } template void AtomTemplate::setColor(Vector3ub color) { m_molecule->setColor(m_index, std::move(color)); } template Vector3ub AtomTemplate::color() const { return m_molecule->color(m_index); } template void AtomTemplate::setLayer(size_t layer) { m_molecule->setLayer(m_index, layer); } template size_t AtomTemplate::layer() const { return m_molecule->layer(m_index); } template void AtomTemplate::setSelected(bool selected) { m_molecule->setAtomSelected(m_index, selected); } template bool AtomTemplate::selected() const { return m_molecule->atomSelected(m_index); } template void AtomTemplate::setForceVector(const Vector3& force) { m_molecule->setForceVector(m_index, force); } template Vector3 AtomTemplate::forceVector() const { return m_molecule->forceVectors().size() > 0 ? m_molecule->forceVectors()[m_index] : Vector3::Zero(); } template void AtomTemplate::setLabel(const std::string& label) { m_molecule->setAtomLabel(m_index, label); } template std::string AtomTemplate::label() const { return m_molecule->atomLabel(m_index); } } // namespace Avogadro::Core #endif // AVOGADRO_CORE_ATOM_H