Skip to content

Commit

Permalink
Merge pull request #1537 from ghutchis/fix-density-color-crash
Browse files Browse the repository at this point in the history
Fix electron density calculation (and crash)
  • Loading branch information
ghutchis authored Dec 16, 2023
2 parents 29b7fc3 + c5aaf89 commit e92522c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 33 deletions.
31 changes: 14 additions & 17 deletions avogadro/qtplugins/meshes/meshes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

#include <QtCore/QDebug>
#include <QtCore/QSettings>
#include <QtWidgets/QSlider>
#include <QtWidgets/QFormLayout>
#include <QtWidgets/QSlider>
#include <QtWidgets/QVBoxLayout>

#include <algorithm>
Expand All @@ -36,17 +36,15 @@ Meshes::Meshes(QObject* p) : ScenePlugin(p), m_setupWidget(nullptr)
// out of 255
m_opacity = settings.value("meshes/opacity", 150).toUInt();

auto color =
settings.value("meshes/color1", QColor(Qt::red)).value<QColor>();
auto color = settings.value("meshes/color1", QColor(Qt::red)).value<QColor>();
m_color1[0] = static_cast<unsigned char>(color.red());
m_color1[1] = static_cast<unsigned char>(color.green());
m_color1[2] = static_cast<unsigned char>(color.blue());

color =
settings.value("meshes/color2", QColor(Qt::blue)).value<QColor>();
color = settings.value("meshes/color2", QColor(Qt::blue)).value<QColor>();
m_color2[0] = static_cast<unsigned char>(color.red());
m_color2[1] = static_cast<unsigned char>(color.green());
m_color2[2] = static_cast<unsigned char>(color.blue());
m_color2[2] = static_cast<unsigned char>(color.blue());
}

Meshes::~Meshes() {}
Expand All @@ -67,7 +65,7 @@ void Meshes::process(const QtGui::Molecule& mol, GroupNode& node)
if (mol.meshCount()) {
auto* geometry = new GeometryNode;
node.addChild(geometry);

const Mesh* mesh = mol.mesh(0);

/// @todo Allow use of MeshGeometry without an index array when all vertices
Expand All @@ -84,20 +82,19 @@ void Meshes::process(const QtGui::Molecule& mol, GroupNode& node)
mesh1->setOpacity(m_opacity);

if (hasColors) {
auto colors = mesh->colors();
Core::Array<Vector3ub> colorsRGB(colors.size());
for (size_t i = 0; i < colors.size(); i++)
colorsRGB[i] = Vector3ub(
colors[i].red() * 255, colors[i].green() * 255, colors[i].blue() * 255
);
mesh1->addVertices(mesh->vertices(), mesh->normals(), colorsRGB);
auto colors = mesh->colors();
Core::Array<Vector3ub> colorsRGB(colors.size());
for (size_t i = 0; i < colors.size(); i++)
colorsRGB[i] = Vector3ub(colors[i].red() * 255, colors[i].green() * 255,
colors[i].blue() * 255);
mesh1->addVertices(mesh->vertices(), mesh->normals(), colorsRGB);
} else { // probably a molecular orbital
mesh1->setColor(m_color1);
mesh1->addVertices(mesh->vertices(), mesh->normals());
}
mesh1->addTriangles(indices);
mesh1->setRenderPass(m_opacity == 255 ? Rendering::SolidPass
: Rendering::TranslucentPass);
: Rendering::TranslucentPass);

if (mol.meshCount() >= 2) { // it's a molecular orbital, two parts
auto* mesh2 = new MeshGeometry;
Expand All @@ -115,7 +112,7 @@ void Meshes::process(const QtGui::Molecule& mol, GroupNode& node)
mesh2->addVertices(mesh->vertices(), mesh->normals());
mesh2->addTriangles(indices);
mesh2->setRenderPass(m_opacity == 255 ? Rendering::SolidPass
: Rendering::TranslucentPass);
: Rendering::TranslucentPass);
}
}
}
Expand Down Expand Up @@ -189,4 +186,4 @@ QWidget* Meshes::setupWidget()
return m_setupWidget;
}

} // namespace Avogadro
} // namespace Avogadro::QtPlugins
6 changes: 3 additions & 3 deletions avogadro/qtplugins/propertytables/propertyview.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ class PropertyView : public QTableView
explicit PropertyView(PropertyType type, QWidget* parent = 0);

void selectionChanged(const QItemSelection& selected,
const QItemSelection& previous);
const QItemSelection& previous) override;
void setMolecule(QtGui::Molecule* molecule);
void setSourceModel(PropertyModel* model) { m_model = model; }
void hideEvent(QHideEvent* event);
void hideEvent(QHideEvent* event) override;
void contextMenuEvent(QContextMenuEvent* event) override;

protected:
// copy the selected properties to the clipboard
void keyPressEvent(QKeyEvent* event);
void keyPressEvent(QKeyEvent* event) override;

private:
PropertyType m_type;
Expand Down
10 changes: 5 additions & 5 deletions avogadro/qtplugins/spacegroup/spacegroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ class SpaceGroup : public Avogadro::QtGui::ExtensionPlugin
explicit SpaceGroup(QObject* parent_ = nullptr);
~SpaceGroup();

QString name() const { return tr("SpaceGroup"); }
QString description() const;
QList<QAction*> actions() const;
QStringList menuPath(QAction*) const;
QString name() const override { return tr("SpaceGroup"); }
QString description() const override;
QList<QAction*> actions() const override;
QStringList menuPath(QAction*) const override;

bool handleCommand(const QString& command,
const QVariantMap& options) override;

void registerCommands() override;

public slots:
void setMolecule(QtGui::Molecule* mol);
void setMolecule(QtGui::Molecule* mol) override;

void moleculeChanged(unsigned int changes);

Expand Down
4 changes: 4 additions & 0 deletions avogadro/qtplugins/surfaces/surfacedialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ void SurfaceDialog::surfaceComboChanged(int n)
if (type == Surfaces::Type::MolecularOrbital ||
type == Surfaces::Type::FromFile) {
m_ui->orbitalCombo->setEnabled(true);
m_ui->propertyCombo->setEnabled(false);
m_ui->propertyCombo->setCurrentIndex(0); // None
m_ui->colormapCombo->setEnabled(false);
} else {
m_ui->orbitalCombo->setEnabled(false);
m_ui->propertyCombo->setEnabled(true);
}
}

Expand Down
15 changes: 7 additions & 8 deletions avogadro/qtplugins/surfaces/surfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,6 @@ bool Surfaces::handleCommand(const QString& command, const QVariantMap& options)
if (m_molecule == nullptr)
return false; // No molecule to handle the command.

qDebug() << "handle surface cmd:" << command << options;

// Set up some defaults for the options.
int index = -1;
int homo = -1;
Expand Down Expand Up @@ -593,7 +591,7 @@ void Surfaces::calculateQM(Type type, int index, bool beta, float isoValue,
} else {
m_slaterConcurrent->calculateElectronDensity(m_cube);
}
} else if (type == ElectronDensity) {
} else if (type == SpinDensity) {
progressText = tr("Calculating spin density");
m_cube->setName("Spin Density");
m_cube->setCubeType(Core::Cube::Type::SpinDensity);
Expand Down Expand Up @@ -693,8 +691,6 @@ void Surfaces::displayMesh()
if (!m_cube)
return;

// qDebug() << " running displayMesh";

if (m_dialog != nullptr)
m_smoothingPasses = m_dialog->smoothingPassesValue();
else
Expand All @@ -706,7 +702,7 @@ void Surfaces::displayMesh()
m_meshGenerator1 = new QtGui::MeshGenerator;
connect(m_meshGenerator1, SIGNAL(finished()), SLOT(meshFinished()));
}
m_meshGenerator1->initialize(m_cube, m_mesh1, -m_isoValue, m_smoothingPasses);
m_meshGenerator1->initialize(m_cube, m_mesh1, m_isoValue, m_smoothingPasses);

bool isMO = false;
// if it's from a file we should "play it safe"
Expand All @@ -722,8 +718,8 @@ void Surfaces::displayMesh()
m_meshGenerator2 = new QtGui::MeshGenerator;
connect(m_meshGenerator2, SIGNAL(finished()), SLOT(meshFinished()));
}
m_meshGenerator2->initialize(m_cube, m_mesh2, m_isoValue, m_smoothingPasses,
true);
m_meshGenerator2->initialize(m_cube, m_mesh2, -m_isoValue,
m_smoothingPasses, true);
}

// Start the mesh generation - this needs an improved mutex with a read lock
Expand Down Expand Up @@ -797,6 +793,9 @@ void Surfaces::colorMeshByPotential()
const auto colormap = getColormapFromString(m_dialog->colormapName());

const auto positionsf = m_mesh1->vertices();
if (positionsf.empty())
return;

Core::Array<Vector3> positions(positionsf.size());
std::transform(positionsf.begin(), positionsf.end(), positions.begin(),
[](const Vector3f& pos) { return pos.cast<double>(); });
Expand Down

0 comments on commit e92522c

Please sign in to comment.