Debian Codegraph @

avogadro/qtplugins/centroid/centroid.cpp

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 #include "centroid.h"
7
8 #include <avogadro/core/elements.h>
9 #include <avogadro/qtgui/molecule.h>
10 #include <avogadro/qtgui/rwmolecule.h>
11
12 #include <QAction>
13
14 namespace Avogadro::QtPlugins {
15
16 using Core::Array;
17 using Core::Elements;
18
19 Centroid::Centroid(QObject* parent_)
20 : Avogadro::QtGui::ExtensionPlugin(parent_),
21 m_centroidAction(new QAction(tr("Add Centroid"), this)),
22 m_comAction(new QAction(tr("Add Center of Mass"), this)),
23 m_normalAction(
24 new QAction(tr("Add Perpendicular",
25 "add a point normal to the plane of the molecule"),
26 this))
27 {
28 m_centroidAction->setProperty("menu priority", 190);
29 m_comAction->setProperty("menu priority", 180);
30 m_normalAction->setProperty("menu priority", 170);
31
32 connect(m_centroidAction, SIGNAL(triggered()), SLOT(addCentroid()));
33 connect(m_comAction, SIGNAL(triggered()), SLOT(addCenterOfMass()));
34 connect(m_normalAction, SIGNAL(triggered()), SLOT(normal()));
35 }
36
37 QList<QAction*> Centroid::actions() const
38 {
39 QList<QAction*> result;
40 return result << m_centroidAction << m_comAction << m_normalAction;
41 }
42
43 QStringList Centroid::menuPath(QAction*) const
44 {
45 return QStringList() << tr("&Build");
46 }
47
48 void Centroid::setMolecule(QtGui::Molecule* mol)
49 {
50 m_molecule = mol;
51 }
52
53 void Centroid::addCentroid()
54 {
55 if (m_molecule == nullptr || m_molecule->atomCount() == 0)
56 return;
57
58 Vector3 center(0.0, 0.0, 0.0);
59 Index count = 0;
60 bool hasSelection = !m_molecule->isSelectionEmpty();
61 for (Index i = 0; i < m_molecule->atomCount(); ++i) {
62 if (hasSelection && !m_molecule->atomSelected(i))
63 continue;
64 // don't count dummy atoms
65 if (m_molecule->atomicNumber(i) == 0)
66 continue;
67
68 center += m_molecule->atomPosition3d(i);
69 ++count;
70 }
71 center /= count;
72
73 m_molecule->undoMolecule()->addAtom(0, center);
74 m_molecule->emitChanged(QtGui::Molecule::Atoms | QtGui::Molecule::Added);
75 }
76
77 void Centroid::addCenterOfMass()
78 {
79 if (m_molecule == nullptr || m_molecule->atomCount() == 0)
80 return;
81
82 Vector3 center(0.0, 0.0, 0.0);
83 Real totalMass = 0.0;
84 bool hasSelection = !m_molecule->isSelectionEmpty();
85
86 // we have to first find the centroid
87 for (Index i = 0; i < m_molecule->atomCount(); ++i) {
88 if (hasSelection && !m_molecule->atomSelected(i))
89 continue;
90 // skip it if it's a dummy atom
91 if (m_molecule->atomicNumber(i) == 0)
92 continue;
93
94 Real mass = Elements::mass(m_molecule->atomicNumber(i));
95 if (m_molecule->isotope(i) > 0)
96 mass = Elements::isotopeMass(m_molecule->atomicNumber(i),
97 m_molecule->isotope(i));
98 center += m_molecule->atomPosition3d(i) * mass;
99
100 totalMass += mass;
101 }
102 center /= totalMass;
103
104 m_molecule->undoMolecule()->addAtom(0, center);
105 m_molecule->emitChanged(QtGui::Molecule::Atoms | QtGui::Molecule::Added);
106 }
107
108 void Centroid::normal()
109 {
110 if (m_molecule == nullptr || m_molecule->atomCount() == 0)
111 return;
112
113 Vector3 newPos(0.0, 0.0, 0.0);
114 if (m_molecule->isSelectionEmpty()) {
115 auto pair = m_molecule->bestFitPlane();
116 newPos = pair.second * 2.0 + pair.first;
117 } else {
118 Array<Vector3> selectedAtoms;
119 for (Index i = 0; i < m_molecule->atomCount(); ++i) {
120 if (!m_molecule->atomSelected(i))
121 continue;
122
123 selectedAtoms.push_back(m_molecule->atomPosition3d(i));
124 }
125
126 auto pair = m_molecule->bestFitPlane(selectedAtoms);
127 newPos = pair.second * 2.0 + pair.first;
128 }
129 m_molecule->undoMolecule()->addAtom(0, newPos);
130
131 m_molecule->emitChanged(QtGui::Molecule::Atoms | QtGui::Molecule::Added);
132 }
133
134 } // namespace Avogadro::QtPlugins