Skip to content

Commit

Permalink
Merge pull request #1624 from ghutchis/fix-centroid-etc-crash
Browse files Browse the repository at this point in the history
Fix crash from centroids with empty molecule
  • Loading branch information
ghutchis authored Feb 17, 2024
2 parents 24b26ed + 5d0be36 commit 36de39b
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions avogadro/qtplugins/centroid/centroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ Centroid::Centroid(QObject* parent_)
: Avogadro::QtGui::ExtensionPlugin(parent_),
m_centroidAction(new QAction(tr("Add Centroid"), this)),
m_comAction(new QAction(tr("Add Center of Mass"), this)),
m_normalAction(new QAction(
tr("Add Perpendicular", "add a point normal to the plane of the molecule"),
this))
m_normalAction(
new QAction(tr("Add Perpendicular",
"add a point normal to the plane of the molecule"),
this))
{
m_centroidAction->setProperty("menu priority", 190);
m_comAction->setProperty("menu priority", 180);
Expand Down Expand Up @@ -52,6 +53,9 @@ void Centroid::setMolecule(QtGui::Molecule* mol)

void Centroid::addCentroid()
{
if (m_molecule == nullptr || m_molecule->atomCount() == 0)
return;

if (m_molecule->isSelectionEmpty()) {
m_molecule->addAtom(0, m_molecule->centerOfGeometry());
} else {
Expand All @@ -74,6 +78,9 @@ void Centroid::addCentroid()

void Centroid::addCenterOfMass()
{
if (m_molecule == nullptr || m_molecule->atomCount() == 0)
return;

if (m_molecule->isSelectionEmpty()) {
m_molecule->addAtom(0, m_molecule->centerOfMass());
} else {
Expand Down Expand Up @@ -102,6 +109,9 @@ void Centroid::addCenterOfMass()

void Centroid::normal()
{
if (m_molecule == nullptr || m_molecule->atomCount() == 0)
return;

if (m_molecule->isSelectionEmpty()) {
auto pair = m_molecule->bestFitPlane();
m_molecule->addAtom(0.0, pair.second * 2.0);
Expand Down

0 comments on commit 36de39b

Please sign in to comment.