| 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 "noncovalent.h" |
| 7 |
|
| 8 |
#include <avogadro/core/array.h> |
| 9 |
#include <avogadro/core/atom.h> |
| 10 |
#include <avogadro/core/atomutilities.h> |
| 11 |
#include <avogadro/core/bond.h> |
| 12 |
#include <avogadro/core/elements.h> |
| 13 |
#include <avogadro/core/neighborperceiver.h> |
| 14 |
#include <avogadro/qtgui/molecule.h> |
| 15 |
#include <avogadro/rendering/widelinegeometry.h> |
| 16 |
#include <avogadro/rendering/geometrynode.h> |
| 17 |
#include <avogadro/rendering/groupnode.h> |
| 18 |
|
| 19 |
#include <QtCore/QSettings> |
| 20 |
#include <QtGui/QColor> |
| 21 |
#include <QtWidgets/QDoubleSpinBox> |
| 22 |
#include <QtWidgets/QFormLayout> |
| 23 |
#include <QtWidgets/QTabWidget> |
| 24 |
#include <QtWidgets/QVBoxLayout> |
| 25 |
#include <QtWidgets/QWidget> |
| 26 |
|
| 27 |
#include <algorithm> |
| 28 |
#include <cmath> |
| 29 |
#include <sstream> |
| 30 |
|
| 31 |
// bond angles |
| 32 |
#define M_TETRAHEDRAL (acosf(-1.0f / 3.0f)) |
| 33 |
#define M_TRIGONAL (2.0f * M_PI / 3.0f) |
| 34 |
|
| 35 |
namespace Avogadro::QtPlugins { |
| 36 |
|
| 37 |
using Core::Array; |
| 38 |
using Core::AtomHybridization; |
| 39 |
using Core::AtomUtilities; |
| 40 |
using Core::Bond; |
| 41 |
using Core::Elements; |
| 42 |
using Core::NeighborPerceiver; |
| 43 |
using QtGui::Molecule; |
| 44 |
using QtGui::PluginLayerManager; |
| 45 |
using Rendering::GeometryNode; |
| 46 |
using Rendering::GroupNode; |
| 47 |
using Rendering::WideLineGeometry; |
| 48 |
|
| 49 |
NonCovalent::NonCovalent(QObject* p) : ScenePlugin(p) |
| 50 |
{ |
| 51 |
m_layerManager = PluginLayerManager(m_name); |
| 52 |
|
| 53 |
QSettings settings; |
| 54 |
m_angleTolerancesDegrees = { |
| 55 |
settings.value("nonCovalent/angleTolerance0", 40.0).toDouble(), |
| 56 |
settings.value("nonCovalent/angleTolerance1", 40.0).toDouble(), |
| 57 |
settings.value("nonCovalent/angleTolerance2", 40.0).toDouble() |
| 58 |
}; |
| 59 |
m_maximumDistances = { |
| 60 |
settings.value("nonCovalent/maximumDistance0", 1.0).toDouble(), |
| 61 |
settings.value("nonCovalent/maximumDistance1", 2.0).toDouble(), |
| 62 |
settings.value("nonCovalent/maximumDistance2", 2.0).toDouble() |
| 63 |
}; |
| 64 |
auto hydrogenBColor = |
| 65 |
settings.value("nonCovalent/lineColor0", QColor(64, 192, 255)) |
| 66 |
.value<QColor>(); |
| 67 |
auto halogenBColor = |
| 68 |
settings.value("nonCovalent/lineColor1", QColor(128, 255, 64)) |
| 69 |
.value<QColor>(); |
| 70 |
auto chalcogenBColor = |
| 71 |
settings.value("nonCovalent/lineColor2", QColor(255, 192, 64)) |
| 72 |
.value<QColor>(); |
| 73 |
m_lineColors = { Vector3ub(hydrogenBColor.red(), hydrogenBColor.green(), |
| 74 |
hydrogenBColor.blue()), |
| 75 |
Vector3ub(halogenBColor.red(), halogenBColor.green(), |
| 76 |
halogenBColor.blue()), |
| 77 |
Vector3ub(chalcogenBColor.red(), chalcogenBColor.green(), |
| 78 |
chalcogenBColor.blue()) }; |
| 79 |
m_lineWidths = { settings.value("nonCovalent/lineWidth0", 5.0).toFloat(), |
| 80 |
settings.value("nonCovalent/lineWidth1", 5.0).toFloat(), |
| 81 |
settings.value("nonCovalent/lineWidth2", 5.0).toFloat() }; |
| 82 |
} |
| 83 |
|
| 84 |
NonCovalent::~NonCovalent() {} |
| 85 |
|
| 86 |
enum InteractionTypes |
| 87 |
{ |
| 88 |
NONE = -1, |
| 89 |
HYDROGEN_BOND = 0, |
| 90 |
HALOGEN_BOND = 1, |
| 91 |
CHALCOGEN_BOND = 2 |
| 92 |
}; |
| 93 |
|
| 94 |
static enum InteractionTypes getInteractionType(const Molecule& molecule, |
| 95 |
Index i) |
| 96 |
{ |
| 97 |
unsigned char inum = molecule.atomicNumber(i); |
| 98 |
switch (inum) { |
| 99 |
case 1: // hydrogen bond |
| 100 |
for (const Bond* b : molecule.bonds(i)) { |
| 101 |
Index j = b->getOtherAtom(i).index(); |
| 102 |
unsigned char jnum = molecule.atomicNumber(j); |
| 103 |
switch (jnum) { |
| 104 |
case 7: |
| 105 |
case 8: |
| 106 |
case 9: // F, O, N |
| 107 |
return HYDROGEN_BOND; |
| 108 |
} |
| 109 |
} |
| 110 |
break; |
| 111 |
case 9: |
| 112 |
case 17: |
| 113 |
case 35: |
| 114 |
case 53: // halogen bond |
| 115 |
for (const Bond* b : molecule.bonds(i)) { |
| 116 |
Index j = (b->atom1().index() == i ? b->atom2() : b->atom1()).index(); |
| 117 |
unsigned char jnum = molecule.atomicNumber(j); |
| 118 |
switch (jnum) { |
| 119 |
case 6: |
| 120 |
case 7: |
| 121 |
case 8: |
| 122 |
case 9: |
| 123 |
case 16: |
| 124 |
case 17: |
| 125 |
case 35: |
| 126 |
case 53: // F, O, N, Cl, Br, C, I, S |
| 127 |
return HALOGEN_BOND; |
| 128 |
} |
| 129 |
} |
| 130 |
break; |
| 131 |
case 8: |
| 132 |
case 16: |
| 133 |
case 34: |
| 134 |
case 52: // chalcogen bond |
| 135 |
for (const Bond* b : molecule.bonds(i)) { |
| 136 |
Index j = (b->atom1().index() == i ? b->atom2() : b->atom1()).index(); |
| 137 |
unsigned char jnum = molecule.atomicNumber(j); |
| 138 |
switch (jnum) { |
| 139 |
case 6: // C |
| 140 |
return CHALCOGEN_BOND; |
| 141 |
} |
| 142 |
} |
| 143 |
break; |
| 144 |
} |
| 145 |
return NONE; |
| 146 |
} |
| 147 |
|
| 148 |
static bool checkPairDonorIsValid(const Molecule& molecule, Index n, |
| 149 |
int interactionType) |
| 150 |
{ |
| 151 |
unsigned char nnum = molecule.atomicNumber(n); |
| 152 |
switch (interactionType) { |
| 153 |
case HYDROGEN_BOND: |
| 154 |
switch (nnum) { |
| 155 |
case 7: |
| 156 |
case 8: |
| 157 |
case 9: |
| 158 |
case 17: // F, O, N, Cl |
| 159 |
return true; |
| 160 |
} |
| 161 |
break; |
| 162 |
case HALOGEN_BOND: |
| 163 |
switch (nnum) { |
| 164 |
case 7: |
| 165 |
case 8: |
| 166 |
case 9: |
| 167 |
case 17: // F, O, N, Cl |
| 168 |
return true; |
| 169 |
} |
| 170 |
break; |
| 171 |
case CHALCOGEN_BOND: |
| 172 |
switch (nnum) { |
| 173 |
case 7: |
| 174 |
case 8: |
| 175 |
case 9: |
| 176 |
case 16: |
| 177 |
case 17: |
| 178 |
case 34: |
| 179 |
case 52: // F, O, N, Cl, S, Se, Te |
| 180 |
return true; |
| 181 |
} |
| 182 |
break; |
| 183 |
} |
| 184 |
return false; |
| 185 |
} |
| 186 |
|
| 187 |
static bool checkAtomPairNotBonded(const Molecule& molecule, Index i, Index n) |
| 188 |
{ |
| 189 |
Array<const Bond*> bonds = molecule.bonds(i); |
| 190 |
/* Return true if all of the bonds from i are to atoms other than n */ |
| 191 |
return std::all_of(bonds.begin(), bonds.end(), [i, n](const Bond* b) { |
| 192 |
return b->getOtherAtom(i).index() != n; |
| 193 |
}); |
| 194 |
} |
| 195 |
|
| 196 |
static float computeAngle(Vector3 a, Vector3 b) |
| 197 |
{ |
| 198 |
return acos(a.normalized().dot(b.normalized())); |
| 199 |
} |
| 200 |
|
| 201 |
static bool checkHoleVector(const Molecule& molecule, Index i, |
| 202 |
const Vector3& in, float angleTolerance) |
| 203 |
{ |
| 204 |
Array<const Bond*> bonds = molecule.bonds(i); |
| 205 |
Vector3 pos = molecule.atomPosition3d(i); |
| 206 |
/* Return true if any of the bonds to i forms a small enough angle |
| 207 |
* with 'in' at the opposite side of atom 'i' */ |
| 208 |
for (const Bond* b : bonds) { |
| 209 |
Index n = b->getOtherAtom(i).index(); |
| 210 |
Vector3 npos = molecule.atomPosition3d(n); |
| 211 |
float oppositeAngle = M_PI - computeAngle(in, npos - pos); |
| 212 |
if (oppositeAngle <= angleTolerance) |
| 213 |
return true; |
| 214 |
} |
| 215 |
return false; |
| 216 |
} |
| 217 |
|
| 218 |
static bool checkPairVector(const Molecule& molecule, Index n, |
| 219 |
const Vector3& in, float angleTolerance) |
| 220 |
{ |
| 221 |
AtomHybridization hybridization = |
| 222 |
AtomUtilities::perceiveHybridization(molecule.atom(n)); |
| 223 |
Array<const Bond*> bonds = molecule.bonds(n); |
| 224 |
size_t bondCount = bonds.size(); |
| 225 |
std::vector<Vector3> bondVectors; |
| 226 |
Vector3 pos = molecule.atomPosition3d(n); |
| 227 |
/* Compute all bond vectors around atom n */ |
| 228 |
for (const Bond* b : bonds) |
| 229 |
bondVectors.emplace_back( |
| 230 |
molecule.atomPosition3d(b->getOtherAtom(n).index()) - pos); |
| 231 |
float pairAngle; |
| 232 |
switch (hybridization) { |
| 233 |
case Core::SP3: |
| 234 |
switch (bondCount) { |
| 235 |
case 0: |
| 236 |
pairAngle = 0.0f; |
| 237 |
break; |
| 238 |
case 1: |
| 239 |
pairAngle = fabs(computeAngle(bondVectors[0], in) - M_TETRAHEDRAL); |
| 240 |
break; |
| 241 |
case 2: { |
| 242 |
Vector3 pairVector = AtomUtilities::generateNewBondVector( |
| 243 |
molecule.atom(n), bondVectors, hybridization); |
| 244 |
pairAngle = computeAngle(pairVector, in); |
| 245 |
bondVectors.push_back(pairVector); |
| 246 |
pairVector = AtomUtilities::generateNewBondVector( |
| 247 |
molecule.atom(n), bondVectors, hybridization); |
| 248 |
pairAngle = std::min(pairAngle, computeAngle(pairVector, in)); |
| 249 |
break; |
| 250 |
} |
| 251 |
case 3: { |
| 252 |
Vector3 pairVector = AtomUtilities::generateNewBondVector( |
| 253 |
molecule.atom(n), bondVectors, hybridization); |
| 254 |
pairAngle = computeAngle(pairVector, in); |
| 255 |
break; |
| 256 |
} |
| 257 |
default: |
| 258 |
return false; |
| 259 |
} |
| 260 |
break; |
| 261 |
case Core::SP2: |
| 262 |
switch (bondCount) { |
| 263 |
case 0: |
| 264 |
pairAngle = 0.0f; |
| 265 |
break; |
| 266 |
case 1: |
| 267 |
pairAngle = fabs(computeAngle(bondVectors[0], in) - M_TRIGONAL); |
| 268 |
break; |
| 269 |
case 2: { |
| 270 |
Vector3 pairVector = AtomUtilities::generateNewBondVector( |
| 271 |
molecule.atom(n), bondVectors, hybridization); |
| 272 |
pairAngle = computeAngle(pairVector, in); |
| 273 |
break; |
| 274 |
} |
| 275 |
default: |
| 276 |
return false; |
| 277 |
} |
| 278 |
break; |
| 279 |
case Core::SP: |
| 280 |
switch (bondCount) { |
| 281 |
case 0: |
| 282 |
pairAngle = 0.0f; |
| 283 |
break; |
| 284 |
case 1: { |
| 285 |
pairAngle = fabs(computeAngle(bondVectors[0], in) - M_PI); |
| 286 |
break; |
| 287 |
} |
| 288 |
default: |
| 289 |
return false; |
| 290 |
} |
| 291 |
break; |
| 292 |
default: |
| 293 |
return true; |
| 294 |
} |
| 295 |
return pairAngle <= angleTolerance; |
| 296 |
} |
| 297 |
|
| 298 |
void NonCovalent::process(const Molecule& molecule, Rendering::GroupNode& node) |
| 299 |
{ |
| 300 |
std::vector<Index> enabledAtoms; |
| 301 |
Array<Vector3> enabledPositions; |
| 302 |
Array<double> atomRadii; |
| 303 |
const size_t atomCount = molecule.atomCount(); |
| 304 |
for (Index i = 0; i < atomCount; ++i) { |
| 305 |
enabledAtoms.push_back(i); |
| 306 |
enabledPositions.push_back(molecule.atomPosition3d(i)); |
| 307 |
atomRadii.push_back(Elements::radiusVDW(molecule.atomicNumber(i))); |
| 308 |
} |
| 309 |
float absoluteMaxDistance = |
| 310 |
*std::max_element(m_maximumDistances.begin(), m_maximumDistances.end()); |
| 311 |
NeighborPerceiver perceiver(enabledPositions, absoluteMaxDistance); |
| 312 |
|
| 313 |
auto* geometry = new GeometryNode; |
| 314 |
node.addChild(geometry); |
| 315 |
std::array<WideLineGeometry*, 3> lineGroups; |
| 316 |
for (Index type = 0; type < 3; type++) { |
| 317 |
lineGroups[type] = new WideLineGeometry; |
| 318 |
lineGroups[type]->identifier().molecule = &molecule; |
| 319 |
lineGroups[type]->identifier().type = Rendering::BondType; |
| 320 |
geometry->addDrawable(lineGroups[type]); |
| 321 |
} |
| 322 |
Array<Index> neighbors; |
| 323 |
for (Index i : enabledAtoms) { |
| 324 |
enum InteractionTypes interactionType = getInteractionType(molecule, i); |
| 325 |
if (interactionType == NONE) |
| 326 |
continue; |
| 327 |
Vector3 pos = molecule.atomPosition3d(i); |
| 328 |
double radius = atomRadii[i]; |
| 329 |
perceiver.getNeighborsInclusiveInPlace(neighbors, pos); |
| 330 |
for (Index ni : neighbors) { |
| 331 |
Index n = enabledAtoms[ni]; |
| 332 |
if (!checkPairDonorIsValid(molecule, n, interactionType)) |
| 333 |
continue; |
| 334 |
|
| 335 |
Vector3 npos = molecule.atomPosition3d(n); |
| 336 |
double nradius = atomRadii[n]; |
| 337 |
Vector3 distance_vector = npos - pos; |
| 338 |
|
| 339 |
if (distance_vector.norm() > |
| 340 |
m_maximumDistances[interactionType] + radius + nradius) |
| 341 |
continue; |
| 342 |
if (!checkAtomPairNotBonded(molecule, i, n)) |
| 343 |
continue; |
| 344 |
|
| 345 |
float angleTolerance = |
| 346 |
m_angleTolerancesDegrees[interactionType] * M_PI / 180.0; |
| 347 |
if (!checkHoleVector(molecule, i, distance_vector, angleTolerance)) |
| 348 |
continue; |
| 349 |
if (!checkPairVector(molecule, n, -distance_vector, angleTolerance)) |
| 350 |
continue; |
| 351 |
|
| 352 |
lineGroups[interactionType]->addDashedLine( |
| 353 |
pos.cast<float>(), npos.cast<float>(), m_lineColors[interactionType], |
| 354 |
m_lineWidths[interactionType] * WideLineGeometry::lineWidthScale, 8); |
| 355 |
} |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
QWidget* NonCovalent::setupWidget() |
| 360 |
{ |
| 361 |
auto* widget = new QWidget(qobject_cast<QWidget*>(this->parent())); |
| 362 |
auto* v = new QVBoxLayout; |
| 363 |
auto* tabs = new QTabWidget; |
| 364 |
|
| 365 |
for (Index i = 0; i < 3; i++) { |
| 366 |
// angle tolerance |
| 367 |
auto* angle_spin = new QDoubleSpinBox; |
| 368 |
angle_spin->setRange(0.0, 180.0); |
| 369 |
angle_spin->setSingleStep(1.0); |
| 370 |
angle_spin->setDecimals(0); |
| 371 |
angle_spin->setSuffix(" °"); |
| 372 |
angle_spin->setValue(m_angleTolerancesDegrees[i]); |
| 373 |
QObject::connect( |
| 374 |
angle_spin, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, |
| 375 |
[this, i](float tolerance) { return setAngleTolerance(tolerance, i); }); |
| 376 |
|
| 377 |
// maximum distance |
| 378 |
auto* distance_spin = new QDoubleSpinBox; |
| 379 |
distance_spin->setRange(1.0, 10.0); |
| 380 |
distance_spin->setSingleStep(0.1); |
| 381 |
distance_spin->setDecimals(1); |
| 382 |
distance_spin->setSuffix(" Å"); |
| 383 |
distance_spin->setValue(m_maximumDistances[i]); |
| 384 |
QObject::connect( |
| 385 |
distance_spin, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, |
| 386 |
[this, i](float distance) { return setMaximumDistance(distance, i); }); |
| 387 |
|
| 388 |
// line width |
| 389 |
auto* lineWidth_spin = new QDoubleSpinBox; |
| 390 |
lineWidth_spin->setRange(1.0, 10.0); |
| 391 |
lineWidth_spin->setSingleStep(0.5); |
| 392 |
lineWidth_spin->setDecimals(1); |
| 393 |
lineWidth_spin->setValue(m_lineWidths[i]); |
| 394 |
QObject::connect(lineWidth_spin, |
| 395 |
QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, |
| 396 |
[this, i](float width) { return setLineWidth(width, i); }); |
| 397 |
|
| 398 |
auto* form = new QFormLayout; |
| 399 |
form->addRow(QObject::tr("Angle tolerance:"), angle_spin); |
| 400 |
form->addRow(QObject::tr("Maximum distance:"), distance_spin); |
| 401 |
form->addRow(QObject::tr("Line width:"), lineWidth_spin); |
| 402 |
|
| 403 |
auto* page = new QWidget; |
| 404 |
page->setLayout(form); |
| 405 |
|
| 406 |
switch (i) { |
| 407 |
case 0: |
| 408 |
tabs->addTab(page, tr("Hydrogen")); |
| 409 |
break; |
| 410 |
case 1: |
| 411 |
tabs->addTab(page, tr("Halogen")); |
| 412 |
break; |
| 413 |
case 2: |
| 414 |
tabs->addTab(page, tr("Chalcogen")); |
| 415 |
break; |
| 416 |
default:; |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
v->addWidget(tabs); |
| 421 |
v->addStretch(1); |
| 422 |
widget->setLayout(v); |
| 423 |
return widget; |
| 424 |
} |
| 425 |
|
| 426 |
void NonCovalent::setAngleTolerance(float angleTolerance, Index index) |
| 427 |
{ |
| 428 |
m_angleTolerancesDegrees[index] = float(angleTolerance); |
| 429 |
emit drawablesChanged(); |
| 430 |
|
| 431 |
QSettings settings; |
| 432 |
settings.setValue(QString("nonCovalent/angleTolerance%1").arg(index), |
| 433 |
angleTolerance); |
| 434 |
} |
| 435 |
|
| 436 |
void NonCovalent::setMaximumDistance(float maximumDistance, Index index) |
| 437 |
{ |
| 438 |
m_maximumDistances[index] = float(maximumDistance); |
| 439 |
emit drawablesChanged(); |
| 440 |
|
| 441 |
QSettings settings; |
| 442 |
settings.setValue(QString("nonCovalent/maximumDistance%1").arg(index), |
| 443 |
maximumDistance); |
| 444 |
} |
| 445 |
|
| 446 |
void NonCovalent::setLineWidth(float width, Index index) |
| 447 |
{ |
| 448 |
m_lineWidths[index] = width; |
| 449 |
emit drawablesChanged(); |
| 450 |
|
| 451 |
QSettings settings; |
| 452 |
settings.setValue(QString("nonCovalent/lineWidth%1").arg(index), width); |
| 453 |
} |
| 454 |
|
| 455 |
} // namespace Avogadro::QtPlugins |