Debian Codegraph @

avogadro/qtplugins/ballandstick/ballandstick.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 "ballandstick.h"
7
8 #include <avogadro/core/elements.h>
9 #include <avogadro/qtgui/molecule.h>
10 #include <avogadro/rendering/cylindergeometry.h>
11 #include <avogadro/rendering/geometrynode.h>
12 #include <avogadro/rendering/groupnode.h>
13 #include <avogadro/rendering/spheregeometry.h>
14
15 #include <QtCore/QSettings>
16 #include <QtWidgets/QCheckBox>
17 #include <QtWidgets/QFormLayout>
18 #include <QtWidgets/QHBoxLayout>
19 #include <QtWidgets/QLabel>
20 #include <QtWidgets/QSlider>
21 #include <QtWidgets/QVBoxLayout>
22 #include <QtWidgets/QWidget>
23
24 #include <iostream>
25
26 namespace Avogadro::QtPlugins {
27
28 using Core::Elements;
29 using QtGui::PluginLayerManager;
30 using Rendering::CylinderGeometry;
31 using Rendering::GeometryNode;
32 using Rendering::SphereGeometry;
33
34 struct LayerBallAndStick : Core::LayerData
35 {
36 QWidget* widget;
37 bool multiBonds;
38 bool showHydrogens;
39 float atomScale;
40 float bondRadius;
41 float opacity;
42
43 LayerBallAndStick()
44 {
45 widget = nullptr;
46 QSettings settings;
47
48 atomScale = settings.value("ballandstick/atomScale", 0.3).toDouble();
49 bondRadius = settings.value("ballandstick/bondRadius", 0.1).toDouble();
50 multiBonds = settings.value("ballandstick/multiBonds", true).toBool();
51 showHydrogens = settings.value("ballandstick/showHydrogens", true).toBool();
52 opacity = settings.value("ballandstick/opacity", 1.0).toDouble();
53 }
54
55 LayerBallAndStick(std::string settings)
56 {
57 widget = nullptr;
58 deserialize(settings);
59 }
60
61 ~LayerBallAndStick() override
62 {
63 if (widget)
64 widget->deleteLater();
65 }
66
67 std::string serialize() final
68 {
69 return boolToString(multiBonds) + " " + boolToString(showHydrogens) + " " +
70 std::to_string(atomScale) + " " + std::to_string(bondRadius) + " " +
71 std::to_string(opacity);
72 }
73
74 void deserialize(std::string text) final
75 {
76 std::stringstream ss(text);
77 std::string aux;
78 ss >> aux;
79 multiBonds = stringToBool(aux);
80 ss >> aux;
81 showHydrogens = stringToBool(aux);
82 ss >> aux;
83 atomScale = std::stof(aux);
84 ss >> aux;
85 bondRadius = std::stof(aux);
86 ss >> aux;
87 if (!aux.empty())
88 opacity = std::stof(aux); // backwards compatibility
89 }
90
91 LayerData* clone() final { return new LayerBallAndStick(serialize()); }
92
93 void setupWidget(BallAndStick* slot)
94 {
95 if (!widget) {
96 widget = new QWidget(qobject_cast<QWidget*>(slot->parent()));
97 auto* v = new QVBoxLayout;
98
99 auto* f = new QFormLayout;
100 auto* atomRadiusSlider = new QSlider(Qt::Horizontal);
101 atomRadiusSlider->setMinimum(1);
102 atomRadiusSlider->setMaximum(9);
103 atomRadiusSlider->setTickInterval(1);
104 atomRadiusSlider->setValue(atomScale * 10);
105 QObject::connect(atomRadiusSlider, &QSlider::valueChanged, slot,
106 &BallAndStick::atomRadiusChanged);
107 f->addRow(QObject::tr("Atom scale"), atomRadiusSlider);
108
109 auto* bondRadiusSlider = new QSlider(Qt::Horizontal);
110 bondRadiusSlider->setMinimum(1);
111 bondRadiusSlider->setMaximum(8);
112 bondRadiusSlider->setTickInterval(1);
113 bondRadiusSlider->setValue(bondRadius * 10);
114 QObject::connect(bondRadiusSlider, &QSlider::valueChanged, slot,
115 &BallAndStick::bondRadiusChanged);
116 f->addRow(QObject::tr("Bond scale"), bondRadiusSlider);
117
118 auto* opacitySlider = new QSlider(Qt::Horizontal);
119 opacitySlider->setMinimum(0);
120 opacitySlider->setMaximum(100);
121 opacitySlider->setTickInterval(1);
122 opacitySlider->setValue(static_cast<int>(opacity * 100));
123 QObject::connect(opacitySlider, &QSlider::valueChanged, slot,
124 &BallAndStick::opacityChanged);
125 f->addRow(QObject::tr("Opacity"), opacitySlider);
126
127 v->addLayout(f);
128
129 auto* check = new QCheckBox(QObject::tr("Show multiple bonds"));
130 check->setChecked(multiBonds);
131 QObject::connect(check, &QCheckBox::clicked, slot,
132 &BallAndStick::multiBonds);
133 v->addWidget(check);
134
135 check = new QCheckBox(QObject::tr("Show hydrogens"));
136 check->setChecked(showHydrogens);
137 QObject::connect(check, &QCheckBox::clicked, slot,
138 &BallAndStick::showHydrogens);
139 v->addWidget(check);
140
141 v->addStretch(1);
142 widget->setLayout(v);
143 }
144 }
145 };
146
147 BallAndStick::BallAndStick(QObject* p) : ScenePlugin(p), m_group(nullptr)
148 {
149 m_layerManager = PluginLayerManager(m_name);
150 }
151
152 BallAndStick::~BallAndStick() {}
153
154 void BallAndStick::process(const QtGui::Molecule& molecule,
155 Rendering::GroupNode& node)
156 {
157 m_layerManager.load<LayerBallAndStick>();
158 // Add a sphere node to contain all of the spheres.
159 m_group = &node;
160 auto* geometry = new GeometryNode;
161 node.addChild(geometry);
162 auto* spheres = new SphereGeometry;
163 spheres->identifier().molecule = reinterpret_cast<const void*>(&molecule);
164 spheres->identifier().type = Rendering::AtomType;
165 geometry->addDrawable(spheres);
166
167 // if we have to draw any translucent spheres, we need to add a separate
168 // geometry node for them
169 auto translucentSpheres = new SphereGeometry;
170 translucentSpheres->setRenderPass(Rendering::TranslucentPass);
171 translucentSpheres->identifier().molecule =
172 reinterpret_cast<const void*>(&molecule);
173 translucentSpheres->identifier().type = Rendering::AtomType;
174 geometry->addDrawable(translucentSpheres);
175
176 // for the selected atoms
177 auto selectedSpheres = new SphereGeometry;
178 selectedSpheres->setOpacity(0.42);
179 geometry->addDrawable(selectedSpheres);
180
181 for (Index i = 0; i < molecule.atomCount(); ++i) {
182 Core::Atom atom = molecule.atom(i);
183 if (!m_layerManager.atomEnabled(i)) {
184 continue;
185 }
186 unsigned char atomicNumber = atom.atomicNumber();
187
188 auto* interface = m_layerManager.getSetting<LayerBallAndStick>(
189 m_layerManager.getLayerID(i));
190 if (atomicNumber == 1 && !interface->showHydrogens)
191 continue;
192
193 Vector3ub color = atom.color();
194 auto radius = static_cast<float>(Elements::radiusVDW(atomicNumber));
195 float scale = interface->atomScale;
196
197 if (interface->opacity < 1.0f) {
198 translucentSpheres->addSphere(atom.position3d().cast<float>(), color,
199 radius * scale, i);
200 translucentSpheres->setOpacity(interface->opacity);
201 } else
202 spheres->addSphere(atom.position3d().cast<float>(), color, radius * scale,
203 i);
204
205 if (atom.selected()) {
206 // add the selected indicator
207 color = Vector3ub(0, 0, 255);
208 radius *= 1.2;
209 selectedSpheres->addSphere(atom.position3d().cast<float>(), color,
210 radius * scale, i);
211 }
212 }
213
214 auto* cylinders = new CylinderGeometry;
215 cylinders->identifier().molecule = &molecule;
216 cylinders->identifier().type = Rendering::BondType;
217 geometry->addDrawable(cylinders);
218
219 auto* translucentBonds = new CylinderGeometry;
220 translucentBonds->setRenderPass(Rendering::TranslucentPass);
221 translucentBonds->identifier().molecule = &molecule;
222 translucentBonds->identifier().type = Rendering::BondType;
223 float opacity = 1.0f; // for any translucent bonds
224 geometry->addDrawable(translucentBonds);
225
226 for (Index i = 0; i < molecule.bondCount(); ++i) {
227 Core::Bond bond = molecule.bond(i);
228 if (!m_layerManager.bondEnabled(bond.atom1().index(),
229 bond.atom2().index())) {
230 continue;
231 }
232
233 auto* interface1 = m_layerManager.getSetting<LayerBallAndStick>(
234 m_layerManager.getLayerID(bond.atom1().index()));
235 auto* interface2 = m_layerManager.getSetting<LayerBallAndStick>(
236 m_layerManager.getLayerID(bond.atom2().index()));
237
238 if (!interface1->showHydrogens && !interface2->showHydrogens &&
239 (bond.atom1().atomicNumber() == 1 ||
240 bond.atom2().atomicNumber() == 1)) {
241 continue;
242 }
243
244 bool doOpaque = true;
245 if (interface1->opacity < 1.0f || interface2->opacity < 1.0f) {
246 opacity = std::min(interface1->opacity, interface2->opacity);
247 translucentBonds->setOpacity(opacity);
248 doOpaque = false;
249 }
250
251 float bondRadius = (interface1->bondRadius + interface2->bondRadius) * 0.5f;
252
253 Vector3f pos1 = bond.atom1().position3d().cast<float>();
254 Vector3f pos2 = bond.atom2().position3d().cast<float>();
255 Vector3ub color1 = bond.atom1().color();
256 Vector3ub color2 = bond.atom2().color();
257 Vector3f bondVector = pos2 - pos1;
258 float bondLength = bondVector.norm();
259 bondVector /= bondLength;
260
261 switch (interface1->multiBonds || interface2->multiBonds ? bond.order()
262 : 1) {
263 case 3: {
264 Vector3f delta = bondVector.unitOrthogonal();
265 // Rotate 45 degrees around the bond vector.
266 Eigen::Quaternionf q;
267 q = Eigen::AngleAxisf(45.0f * DEG_TO_RAD_F, bondVector);
268 delta = q * delta * 2.0f * bondRadius;
269 if (doOpaque) {
270 cylinders->addCylinder(pos1 + delta, pos2 + delta, bondRadius * 1.15,
271 color1, color2, i);
272 cylinders->addCylinder(pos1 - delta, pos2 - delta, bondRadius * 1.15,
273 color1, color2, i);
274 } else {
275 translucentBonds->addCylinder(pos1 + delta, pos2 + delta,
276 bondRadius * 1.15, color1, color2, i);
277 translucentBonds->addCylinder(pos1 - delta, pos2 - delta,
278 bondRadius * 1.15, color1, color2, i);
279 }
280 // This relies upon the single bond case below for the third cylinder.
281 [[fallthrough]];
282 }
283 default:
284 case 1:
285 if (doOpaque) {
286 cylinders->addCylinder(pos1, pos2, m_bondRadius, color1, color2, i);
287 } else {
288 translucentBonds->addCylinder(pos1, pos2, m_bondRadius, color1,
289 color2, i);
290 }
291 break;
292 case 2: {
293 Vector3f delta = bondVector.unitOrthogonal();
294 // Rotate 45 degrees around the bond vector.
295 Eigen::Quaternionf q;
296 q = Eigen::AngleAxisf(45.0f * DEG_TO_RAD_F, bondVector);
297 delta = q * delta * bondRadius;
298 if (doOpaque) {
299 cylinders->addCylinder(pos1 + delta, pos2 + delta, bondRadius * 1.3,
300 color1, color2, i);
301 cylinders->addCylinder(pos1 - delta, pos2 - delta, bondRadius * 1.3,
302 color1, color2, i);
303 } else {
304 translucentBonds->addCylinder(pos1 + delta, pos2 + delta,
305 bondRadius * 1.3, color1, color2, i);
306 translucentBonds->addCylinder(pos1 - delta, pos2 - delta,
307 bondRadius * 1.3, color1, color2, i);
308 }
309 }
310 }
311 }
312 }
313
314 QWidget* BallAndStick::setupWidget()
315 {
316 auto* interface = m_layerManager.getSetting<LayerBallAndStick>();
317 interface->setupWidget(this);
318 return interface->widget;
319 }
320
321 void BallAndStick::opacityChanged(int opacity)
322 {
323 m_opacity = static_cast<float>(opacity) / 100.0f;
324 auto* interface = m_layerManager.getSetting<LayerBallAndStick>();
325 if (m_opacity != interface->opacity) {
326 interface->opacity = m_opacity;
327 emit drawablesChanged();
328 }
329
330 QSettings settings;
331 settings.setValue("ballandstick/opacity", m_opacity);
332 }
333
334 void BallAndStick::atomRadiusChanged(int value)
335 {
336 m_atomScale = static_cast<float>(value) / 10.0f;
337
338 auto* interface = m_layerManager.getSetting<LayerBallAndStick>();
339 if (m_atomScale != interface->atomScale) {
340 interface->atomScale = m_atomScale;
341 emit drawablesChanged();
342 }
343
344 QSettings settings;
345 settings.setValue("ballandstick/atomScale", m_atomScale);
346 }
347
348 void BallAndStick::bondRadiusChanged(int value)
349 {
350 m_bondRadius = static_cast<float>(value) / 10.0f;
351
352 auto* interface = m_layerManager.getSetting<LayerBallAndStick>();
353 if (m_bondRadius != interface->bondRadius) {
354 interface->bondRadius = m_bondRadius;
355 emit drawablesChanged();
356 }
357
358 QSettings settings;
359 settings.setValue("ballandstick/bondRadius", m_bondRadius);
360 }
361
362 void BallAndStick::multiBonds(bool show)
363 {
364 auto* interface = m_layerManager.getSetting<LayerBallAndStick>();
365 if (show != interface->multiBonds) {
366 interface->multiBonds = show;
367 emit drawablesChanged();
368 }
369 QSettings settings;
370 settings.setValue("ballandstick/multiBonds", show);
371 }
372
373 void BallAndStick::showHydrogens(bool show)
374 {
375 auto* interface = m_layerManager.getSetting<LayerBallAndStick>();
376 if (show != interface->showHydrogens) {
377 interface->showHydrogens = show;
378 emit drawablesChanged();
379 }
380 QSettings settings;
381 settings.setValue("ballandstick/showHydrogens", show);
382 }
383
384 } // namespace Avogadro::QtPlugins