Debian Codegraph @

avogadro/core/atom.h

raw
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_ATOM_H
7 #define AVOGADRO_CORE_ATOM_H
8
9 #include "avogadrocore.h"
10 #include "elements.h"
11 #include "vector.h"
12
13 namespace Avogadro::Core {
14
15 enum AtomHybridization
16 {
17 PerceivedOctaheadral = -6,
18 PerceivedTrigonalBipyramidal = -5,
19 PerceivedSquarePlanar = -4,
20 PerceivedSP3 = -3,
21 PerceivedSP2 = -2,
22 PerceivedSP = -1,
23 HybridizationUnknown = 0,
24 SP = 1,
25 SP2 = 2,
26 SP3 = 3,
27 SquarePlanar = 4,
28 TrigonalBipyramidal = 5,
29 Octahedral = 6
30 };
31
32 enum AtomicNumber : unsigned char
33 {
34 Hydrogen = 1,
35 Carbon = 6,
36 Nitrogen = 7,
37 Oxygen = 8
38 };
39
40 /**
41 * @class Atom atom.h <avogadro/core/atom.h>
42 * The atom class represents an atom in a molecule.
43 * To use the appropriate atom implementation for a specific molecule
44 * implementation, use the [MoleculeClass]::AtomType typedef.
45 */
46 template <class Molecule_T>
47 class AtomTemplate
48 {
49 public:
50 using MoleculeType = Molecule_T;
51
52 /** Creates a new, invalid atom object. */
53 AtomTemplate();
54
55 /**
56 * Creates a new atom object representing the atom at index @p i in molecule
57 * @p m.
58 */
59 AtomTemplate(MoleculeType* m, Index i);
60
61 /**
62 * @return True if @a this and @a other share the same index and molecule.
63 */
64 bool operator==(const AtomTemplate<MoleculeType>& other) const;
65
66 /**
67 * @return True if @a this and @a other do not share the same index or
68 * molecule.
69 */
70 bool operator!=(const AtomTemplate<MoleculeType>& other) const;
71
72 /**
73 * Prefix increment operator. Increment this Atom's index by 1 and return a
74 * self-reference. Check isValid() before calling any other methods.
75 */
76 AtomTemplate<MoleculeType>& operator++();
77
78 /**
79 * Postfix increment operator. Increment this Atom's index by 1 and return a
80 * copy of the current Atom. Check isValid() before calling any other methods.
81 */
82 AtomTemplate<MoleculeType> operator++(int);
83
84 /**
85 * Prefix decrement operator. Decrement this Atom's index by 1 and return a
86 * self-reference. Check isValid() before calling any other methods.
87 */
88 AtomTemplate<MoleculeType>& operator--();
89
90 /**
91 * Postfix decrement operator. Decrement this Atom's index by 1 and return a
92 * copy of the current Atom. Check isValid() before calling any other methods.
93 */
94 AtomTemplate<MoleculeType> operator--(int);
95
96 /**
97 * @return True if the molecule is set and the index is less than the number
98 * of atoms.
99 */
100 bool isValid() const;
101
102 /**
103 * @return The molecule that contains this Atom.
104 */
105 MoleculeType* molecule() const;
106
107 /**
108 * @return The index of this atom in molecule().
109 */
110 Index index() const;
111
112 /**
113 * The proton count of the atom.
114 * @{
115 */
116 void setAtomicNumber(unsigned char num);
117 unsigned char atomicNumber() const;
118 /** @} */
119
120 /**
121 * @return The mass of this atom
122 */
123 Real mass() const;
124
125 /**
126 * The 2D position of this atom.
127 * @note Not all molecule types support 2D positions. If the current
128 * MoleculeType class does not, calling this method will result in a
129 * compilation error.
130 * @{
131 */
132 void setPosition2d(const Vector2& pos);
133 Vector2 position2d() const;
134 /** @} */
135
136 /**
137 * The 3D position of this atom.
138 * @{
139 */
140 void setPosition3d(const Vector3& pos);
141 Vector3 position3d() const;
142 /** @} */
143
144 /**
145 * The hybridization / coordination of this atom
146 * @{
147 */
148 void setHybridization(AtomHybridization hyb);
149 AtomHybridization hybridization() const;
150 /** @} */
151
152 /**
153 * The formal charge of this atom
154 * @{
155 */
156 void setFormalCharge(signed char charge);
157 signed char formalCharge() const;
158 /** @} */
159
160 /**
161 * The isotope of this atom
162 * @{
163 */
164 void setIsotope(unsigned short isotope);
165 unsigned short isotope() const;
166 /** @} */
167
168 /**
169 * The color of this atom
170 * @{
171 */
172 void setColor(Vector3ub color);
173 Vector3ub color() const;
174 /** @} */
175
176 /**
177 * The layer of this atom
178 * @{
179 */
180 void setLayer(size_t layer);
181 size_t layer() const;
182 /** @} */
183
184 /**
185 * Is the atom selected.
186 * {@
187 */
188 void setSelected(bool selected);
189 bool selected() const;
190 /** @} */
191
192 /**
193 * The force on this atom.
194 * {@
195 */
196 void setForceVector(const Vector3& force);
197 Vector3 forceVector() const;
198
199 void setLabel(const std::string& label);
200 std::string label() const;
201 /** @} */
202
203 private:
204 MoleculeType* m_molecule;
205 Index m_index;
206 };
207
208 template <class Molecule_T>
209 AtomTemplate<Molecule_T>::AtomTemplate()
210 : m_molecule(nullptr), m_index(MaxIndex)
211 {
212 }
213
214 template <class Molecule_T>
215 AtomTemplate<Molecule_T>::AtomTemplate(MoleculeType* m, Index i)
216 : m_molecule(m), m_index(i)
217 {
218 }
219
220 template <class Molecule_T>
221 bool AtomTemplate<Molecule_T>::operator==(
222 const AtomTemplate<MoleculeType>& other) const
223 {
224 return m_molecule == other.m_molecule && m_index == other.m_index;
225 }
226
227 template <class Molecule_T>
228 bool AtomTemplate<Molecule_T>::operator!=(
229 const AtomTemplate<MoleculeType>& other) const
230 {
231 return m_molecule != other.m_molecule || m_index != other.m_index;
232 }
233
234 template <class Molecule_T>
235 AtomTemplate<Molecule_T>& AtomTemplate<Molecule_T>::operator++()
236 {
237 ++m_index;
238 return *this;
239 }
240
241 template <class Molecule_T>
242 AtomTemplate<Molecule_T> AtomTemplate<Molecule_T>::operator++(int)
243 {
244 AtomTemplate<MoleculeType> result(m_molecule, m_index++);
245 return result;
246 }
247
248 template <class Molecule_T>
249 AtomTemplate<Molecule_T>& AtomTemplate<Molecule_T>::operator--()
250 {
251 --m_index;
252 return *this;
253 }
254
255 template <class Molecule_T>
256 AtomTemplate<Molecule_T> AtomTemplate<Molecule_T>::operator--(int)
257 {
258 AtomTemplate<MoleculeType> result(m_molecule, m_index--);
259 return result;
260 }
261
262 template <class Molecule_T>
263 bool AtomTemplate<Molecule_T>::isValid() const
264 {
265 return m_molecule && m_index < m_molecule->atomCount();
266 }
267
268 template <class Molecule_T>
269 typename AtomTemplate<Molecule_T>::MoleculeType*
270 AtomTemplate<Molecule_T>::molecule() const
271 {
272 return m_molecule;
273 }
274
275 template <class Molecule_T>
276 Index AtomTemplate<Molecule_T>::index() const
277 {
278 return m_index;
279 }
280
281 template <class Molecule_T>
282 void AtomTemplate<Molecule_T>::setAtomicNumber(unsigned char num)
283 {
284 m_molecule->setAtomicNumber(m_index, num);
285 }
286
287 template <class Molecule_T>
288 unsigned char AtomTemplate<Molecule_T>::atomicNumber() const
289 {
290 return m_molecule->atomicNumber(m_index);
291 }
292
293 template <class Molecule_T>
294 Real AtomTemplate<Molecule_T>::mass() const
295 {
296 // TODO: handle isotopes
297 return Elements::mass(atomicNumber());
298 }
299
300 template <class Molecule_T>
301 void AtomTemplate<Molecule_T>::setPosition2d(const Vector2& pos)
302 {
303 m_molecule->setAtomPosition2d(m_index, pos);
304 }
305
306 template <class Molecule_T>
307 Vector2 AtomTemplate<Molecule_T>::position2d() const
308 {
309 return m_molecule->atomPositions2d().size() > 0
310 ? m_molecule->atomPositions2d()[m_index]
311 : Vector2::Zero();
312 }
313
314 template <class Molecule_T>
315 void AtomTemplate<Molecule_T>::setPosition3d(const Vector3& pos)
316 {
317 m_molecule->setAtomPosition3d(m_index, pos);
318 }
319
320 template <class Molecule_T>
321 Vector3 AtomTemplate<Molecule_T>::position3d() const
322 {
323 return m_molecule->atomPositions3d().size() > 0
324 ? m_molecule->atomPositions3d()[m_index]
325 : Vector3::Zero();
326 }
327
328 template <class Molecule_T>
329 void AtomTemplate<Molecule_T>::setHybridization(AtomHybridization hyb)
330 {
331 m_molecule->setHybridization(m_index, hyb);
332 }
333
334 template <class Molecule_T>
335 AtomHybridization AtomTemplate<Molecule_T>::hybridization() const
336 {
337 return m_molecule->hybridization(m_index);
338 }
339
340 template <class Molecule_T>
341 void AtomTemplate<Molecule_T>::setFormalCharge(signed char charge)
342 {
343 m_molecule->setFormalCharge(m_index, charge);
344 }
345
346 template <class Molecule_T>
347 signed char AtomTemplate<Molecule_T>::formalCharge() const
348 {
349 return m_molecule->formalCharge(m_index);
350 }
351
352 template <class Molecule_T>
353 void AtomTemplate<Molecule_T>::setIsotope(unsigned short isotope)
354 {
355 m_molecule->setIsotope(m_index, isotope);
356 }
357
358 template <class Molecule_T>
359 unsigned short AtomTemplate<Molecule_T>::isotope() const
360 {
361 return m_molecule->isotope(m_index);
362 }
363
364 template <class Molecule_T>
365 void AtomTemplate<Molecule_T>::setColor(Vector3ub color)
366 {
367 m_molecule->setColor(m_index, std::move(color));
368 }
369
370 template <class Molecule_T>
371 Vector3ub AtomTemplate<Molecule_T>::color() const
372 {
373 return m_molecule->color(m_index);
374 }
375
376 template <class Molecule_T>
377 void AtomTemplate<Molecule_T>::setLayer(size_t layer)
378 {
379 m_molecule->setLayer(m_index, layer);
380 }
381
382 template <class Molecule_T>
383 size_t AtomTemplate<Molecule_T>::layer() const
384 {
385 return m_molecule->layer(m_index);
386 }
387
388 template <class Molecule_T>
389 void AtomTemplate<Molecule_T>::setSelected(bool selected)
390 {
391 m_molecule->setAtomSelected(m_index, selected);
392 }
393
394 template <class Molecule_T>
395 bool AtomTemplate<Molecule_T>::selected() const
396 {
397 return m_molecule->atomSelected(m_index);
398 }
399
400 template <class Molecule_T>
401 void AtomTemplate<Molecule_T>::setForceVector(const Vector3& force)
402 {
403 m_molecule->setForceVector(m_index, force);
404 }
405
406 template <class Molecule_T>
407 Vector3 AtomTemplate<Molecule_T>::forceVector() const
408 {
409 return m_molecule->forceVectors().size() > 0
410 ? m_molecule->forceVectors()[m_index]
411 : Vector3::Zero();
412 }
413
414 template <class Molecule_T>
415 void AtomTemplate<Molecule_T>::setLabel(const std::string& label)
416 {
417 m_molecule->setAtomLabel(m_index, label);
418 }
419
420 template <class Molecule_T>
421 std::string AtomTemplate<Molecule_T>::label() const
422 {
423 return m_molecule->atomLabel(m_index);
424 }
425
426 } // namespace Avogadro::Core
427
428 #endif // AVOGADRO_CORE_ATOM_H