Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow semi-transparent ball-and-sticks and vdw rendering #1783

Merged
merged 6 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions avogadro/qtgui/pluginlayermanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <avogadro/core/layermanager.h>
#include <cassert>
#include <iostream>

namespace Avogadro {
namespace QtGui {
Expand Down Expand Up @@ -79,22 +80,31 @@ class AVOGADROQTGUI_EXPORT PluginLayerManager : protected Core::LayerManager
/** @return custom data T derived from LayerData. if @p layer is equal to
* MaxIndex returns activeLayer */
template <typename T>
T& getSetting(size_t layer = MaxIndex)
T* getSetting(size_t layer = MaxIndex)
{
auto info = m_molToInfo[m_activeMolecule];

if (layer == MaxIndex) {
layer = info->layer.activeLayer();
}

// std::cout << "name: " << m_name << " layer " << layer << " " <<
// info->layer.activeLayer() << std::endl;

assert(layer <= info->layer.maxLayer());
if (info->settings.find(m_name) == info->settings.end()) {
std::cout << " hit the end? " << std::endl;
info->settings[m_name] = Core::Array<Core::LayerData*>();
}

// do we need to create new layers in the array?
while (info->settings[m_name].size() < layer + 1) {
std::cout << " loop " << info->settings[m_name].size() << " " << layer
<< std::endl;
info->settings[m_name].push_back(new T());
}
auto result = static_cast<T*>(info->settings[m_name][layer]);
return *result;
auto* result = static_cast<T*>(info->settings[m_name][layer]);
return result;
}

private:
Expand Down
126 changes: 98 additions & 28 deletions avogadro/qtplugins/ballandstick/ballandstick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>

#include <iostream>

namespace Avogadro::QtPlugins {

using Core::Elements;
Expand All @@ -37,15 +39,19 @@
bool showHydrogens;
float atomScale;
float bondRadius;
float opacity;

LayerBallAndStick()
{
widget = nullptr;
QSettings settings;
std::cout << " new settings " << std::endl;

atomScale = settings.value("ballandstick/atomScale", 0.3).toDouble();
bondRadius = settings.value("ballandstick/bondRadius", 0.1).toDouble();
multiBonds = settings.value("ballandstick/multiBonds", true).toBool();
showHydrogens = settings.value("ballandstick/showHydrogens", true).toBool();
opacity = settings.value("ballandstick/opacity", 1.0).toDouble();
}

~LayerBallAndStick() override
Expand All @@ -56,12 +62,17 @@

std::string serialize() final
{
std::cout << " serialize " << std::endl;

return boolToString(multiBonds) + " " + boolToString(showHydrogens) + " " +
std::to_string(atomScale) + " " + std::to_string(bondRadius);
std::to_string(atomScale) + " " + std::to_string(bondRadius) + " " +
std::to_string(opacity);
}

void deserialize(std::string text) final
{
std::cout << " deserialize " << text << std::endl;

std::stringstream ss(text);
std::string aux;
ss >> aux;
Expand All @@ -72,6 +83,9 @@
atomScale = std::stof(aux);
ss >> aux;
bondRadius = std::stof(aux);
ss >> aux;
if (!aux.empty())
opacity = std::stof(aux); // backwards compatibility
}

void setupWidget(BallAndStick* slot)
Expand All @@ -98,6 +112,16 @@
QObject::connect(bondRadiusSlider, &QSlider::valueChanged, slot,
&BallAndStick::bondRadiusChanged);
f->addRow(QObject::tr("Bond scale"), bondRadiusSlider);

auto* opacitySlider = new QSlider(Qt::Horizontal);
opacitySlider->setMinimum(0);
opacitySlider->setMaximum(100);
opacitySlider->setTickInterval(1);
opacitySlider->setValue(static_cast<int>(opacity * 100));
QObject::connect(opacitySlider, &QSlider::valueChanged, slot,
&BallAndStick::opacityChanged);
f->addRow(QObject::tr("Opacity"), opacitySlider);

v->addLayout(f);

auto* check = new QCheckBox(QObject::tr("Show multiple bonds"));
Expand Down Expand Up @@ -134,11 +158,22 @@
auto* geometry = new GeometryNode;
node.addChild(geometry);
auto* spheres = new SphereGeometry;
auto selectedSpheres = new SphereGeometry;
selectedSpheres->setOpacity(0.42);
spheres->identifier().molecule = reinterpret_cast<const void*>(&molecule);
spheres->identifier().type = Rendering::AtomType;
geometry->addDrawable(spheres);

// if we have to draw any translucent spheres, we need to add a separate
// geometry node for them
auto translucentSpheres = new SphereGeometry;
translucentSpheres->setRenderPass(Rendering::TranslucentPass);
translucentSpheres->identifier().molecule =
reinterpret_cast<const void*>(&molecule);
translucentSpheres->identifier().type = Rendering::AtomType;
geometry->addDrawable(translucentSpheres);

// for the selected atoms
auto selectedSpheres = new SphereGeometry;
selectedSpheres->setOpacity(0.42);
geometry->addDrawable(selectedSpheres);

for (Index i = 0; i < molecule.atomCount(); ++i) {
Expand All @@ -147,17 +182,28 @@
continue;
}
unsigned char atomicNumber = atom.atomicNumber();
auto& interface = m_layerManager.getSetting<LayerBallAndStick>(

auto* interface = m_layerManager.getSetting<LayerBallAndStick>(
m_layerManager.getLayerID(i));
if (atomicNumber == 1 && !interface.showHydrogens)
if (atomicNumber == 1 && !interface->showHydrogens)
continue;

Vector3ub color = atom.color();
auto radius = static_cast<float>(Elements::radiusVDW(atomicNumber));
float scale = interface.atomScale;
spheres->addSphere(atom.position3d().cast<float>(), color, radius * scale,
i);
float scale = interface->atomScale;
std::cout << " atom " << i << " radius " << radius << " scale " << scale
<< " opacity " << interface->opacity << std::endl;

if (interface->opacity < 1.0f) {
translucentSpheres->addSphere(atom.position3d().cast<float>(), color,
radius * scale, i);
translucentSpheres->setOpacity(interface->opacity);
} else
spheres->addSphere(atom.position3d().cast<float>(), color, radius * scale,
i);

if (atom.selected()) {
// add the selected indicator
color = Vector3ub(0, 0, 255);
radius *= 1.2;
selectedSpheres->addSphere(atom.position3d().cast<float>(), color,
Expand All @@ -169,25 +215,32 @@
cylinders->identifier().molecule = &molecule;
cylinders->identifier().type = Rendering::BondType;
geometry->addDrawable(cylinders);

auto* translucentBonds = new CylinderGeometry;
translucentBonds->setRenderPass(Rendering::TranslucentPass);
translucentBonds->identifier().molecule = &molecule;
translucentBonds->identifier().type = Rendering::BondType;
geometry->addDrawable(translucentBonds);

for (Index i = 0; i < molecule.bondCount(); ++i) {
Core::Bond bond = molecule.bond(i);
if (!m_layerManager.bondEnabled(bond.atom1().index(),
bond.atom2().index())) {
continue;
}

auto& interface1 = m_layerManager.getSetting<LayerBallAndStick>(
auto* interface1 = m_layerManager.getSetting<LayerBallAndStick>(
m_layerManager.getLayerID(bond.atom1().index()));
auto& interface2 = m_layerManager.getSetting<LayerBallAndStick>(
auto* interface2 = m_layerManager.getSetting<LayerBallAndStick>(
m_layerManager.getLayerID(bond.atom2().index()));

if (!interface1.showHydrogens && !interface2.showHydrogens &&
if (!interface1->showHydrogens && !interface2->showHydrogens &&
(bond.atom1().atomicNumber() == 1 ||
bond.atom2().atomicNumber() == 1)) {
continue;
}

float bondRadius = (interface1.bondRadius + interface2.bondRadius) * 0.5f;
float bondRadius = (interface1->bondRadius + interface2->bondRadius) * 0.5f;

Vector3f pos1 = bond.atom1().position3d().cast<float>();
Vector3f pos2 = bond.atom2().position3d().cast<float>();
Expand All @@ -197,7 +250,8 @@
float bondLength = bondVector.norm();
bondVector /= bondLength;

switch (interface1.multiBonds || interface2.multiBonds ? bond.order() : 1) {
switch (interface1->multiBonds || interface2->multiBonds ? bond.order()
: 1) {
case 3: {
Vector3f delta = bondVector.unitOrthogonal();
// Rotate 45 degrees around the bond vector.
Expand Down Expand Up @@ -232,18 +286,34 @@

QWidget* BallAndStick::setupWidget()
{
auto& interface = m_layerManager.getSetting<LayerBallAndStick>();
interface.setupWidget(this);
return interface.widget;
auto* interface = m_layerManager.getSetting<LayerBallAndStick>();
interface->setupWidget(this);
return interface->widget;
}

void BallAndStick::opacityChanged(int opacity)
{
m_opacity = static_cast<float>(opacity) / 100.0f;
auto* interface = m_layerManager.getSetting<LayerBallAndStick>();
if (m_opacity != interface->opacity) {
Fixed Show fixed Hide fixed
Dismissed Show dismissed Hide dismissed
std::cout << " previous opacity " << interface->opacity << " new opacity "
<< m_opacity << std::endl;

interface->opacity = m_opacity;
emit drawablesChanged();
}

QSettings settings;
settings.setValue("ballandstick/opacity", m_opacity);
}

void BallAndStick::atomRadiusChanged(int value)
{
m_atomScale = static_cast<float>(value) / 10.0f;

auto& interface = m_layerManager.getSetting<LayerBallAndStick>();
if (m_atomScale != interface.atomScale) {
interface.atomScale = m_atomScale;
auto* interface = m_layerManager.getSetting<LayerBallAndStick>();
if (m_atomScale != interface->atomScale) {
Dismissed Show dismissed Hide dismissed
interface->atomScale = m_atomScale;
emit drawablesChanged();
}

Expand All @@ -255,9 +325,9 @@
{
m_bondRadius = static_cast<float>(value) / 10.0f;

auto& interface = m_layerManager.getSetting<LayerBallAndStick>();
if (m_bondRadius != interface.bondRadius) {
interface.bondRadius = m_bondRadius;
auto* interface = m_layerManager.getSetting<LayerBallAndStick>();
if (m_bondRadius != interface->bondRadius) {
Dismissed Show dismissed Hide dismissed
interface->bondRadius = m_bondRadius;
emit drawablesChanged();
}

Expand All @@ -267,9 +337,9 @@

void BallAndStick::multiBonds(bool show)
{
auto& interface = m_layerManager.getSetting<LayerBallAndStick>();
if (show != interface.multiBonds) {
interface.multiBonds = show;
auto* interface = m_layerManager.getSetting<LayerBallAndStick>();
if (show != interface->multiBonds) {
interface->multiBonds = show;
emit drawablesChanged();
}
QSettings settings;
Expand All @@ -278,9 +348,9 @@

void BallAndStick::showHydrogens(bool show)
{
auto& interface = m_layerManager.getSetting<LayerBallAndStick>();
if (show != interface.showHydrogens) {
interface.showHydrogens = show;
auto* interface = m_layerManager.getSetting<LayerBallAndStick>();
if (show != interface->showHydrogens) {
interface->showHydrogens = show;
emit drawablesChanged();
}
QSettings settings;
Expand Down
2 changes: 2 additions & 0 deletions avogadro/qtplugins/ballandstick/ballandstick.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ public slots:
void bondRadiusChanged(int value);
void multiBonds(bool show);
void showHydrogens(bool show);
void opacityChanged(int value);

private:
Rendering::GroupNode* m_group;
std::string m_name = "Ball and Stick";
float m_atomScale = 0.3f;
float m_bondRadius = 0.1f;
float m_opacity = 1.0f;
};

} // end namespace QtPlugins
Expand Down
Loading
Loading