| 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 "templatetool.h" |
| 7 |
#include "templatetoolkeymap.h" |
| 8 |
#include "templatetoolwidget.h" |
| 9 |
|
| 10 |
#include <avogadro/core/atom.h> |
| 11 |
#include <avogadro/core/bond.h> |
| 12 |
#include <avogadro/core/elements.h> |
| 13 |
#include <avogadro/core/molecule.h> |
| 14 |
#include <avogadro/core/vector.h> |
| 15 |
|
| 16 |
#include <avogadro/io/cjsonformat.h> |
| 17 |
#include <avogadro/io/fileformatmanager.h> |
| 18 |
|
| 19 |
#include <avogadro/qtgui/hydrogentools.h> |
| 20 |
#include <avogadro/qtgui/molecule.h> |
| 21 |
#include <avogadro/qtgui/rwmolecule.h> |
| 22 |
|
| 23 |
#include <avogadro/qtopengl/glwidget.h> |
| 24 |
|
| 25 |
#include <avogadro/rendering/camera.h> |
| 26 |
#include <avogadro/rendering/geometrynode.h> |
| 27 |
#include <avogadro/rendering/glrenderer.h> |
| 28 |
|
| 29 |
#include <avogadro/rendering/groupnode.h> |
| 30 |
#include <avogadro/rendering/textlabel2d.h> |
| 31 |
#include <avogadro/rendering/textlabel3d.h> |
| 32 |
#include <avogadro/rendering/textproperties.h> |
| 33 |
|
| 34 |
#include <QAction> |
| 35 |
#include <QtGui/QClipboard> |
| 36 |
#include <QtGui/QIcon> |
| 37 |
#include <QtGui/QKeyEvent> |
| 38 |
#include <QtGui/QMouseEvent> |
| 39 |
#include <QtGui/QWheelEvent> |
| 40 |
#include <QtWidgets/QApplication> |
| 41 |
#include <QtWidgets/QComboBox> |
| 42 |
#include <QtWidgets/QWidget> |
| 43 |
|
| 44 |
#include <QtCore/QDebug> |
| 45 |
#include <QtCore/QFile> |
| 46 |
#include <QtCore/QMimeData> |
| 47 |
#include <QtCore/QTimer> |
| 48 |
|
| 49 |
#include <limits> |
| 50 |
|
| 51 |
namespace { |
| 52 |
const unsigned char INVALID_ATOMIC_NUMBER = |
| 53 |
std::numeric_limits<unsigned char>::max(); |
| 54 |
} |
| 55 |
|
| 56 |
namespace Avogadro { |
| 57 |
namespace QtPlugins { |
| 58 |
|
| 59 |
using QtGui::Molecule; |
| 60 |
using QtGui::RWAtom; |
| 61 |
using QtGui::RWBond; |
| 62 |
using QtGui::RWMolecule; |
| 63 |
using QtOpenGL::GLWidget; |
| 64 |
|
| 65 |
using Avogadro::Core::Elements; |
| 66 |
using Avogadro::Io::CjsonFormat; |
| 67 |
using Avogadro::Rendering::GeometryNode; |
| 68 |
using Avogadro::Rendering::GroupNode; |
| 69 |
using Avogadro::Rendering::Identifier; |
| 70 |
using Avogadro::Rendering::TextLabel2D; |
| 71 |
using Avogadro::Rendering::TextLabel3D; |
| 72 |
using Avogadro::Rendering::TextProperties; |
| 73 |
|
| 74 |
TemplateTool::TemplateTool(QObject* parent_) |
| 75 |
: QtGui::ToolPlugin(parent_), m_activateAction(new QAction(this)), |
| 76 |
m_molecule(nullptr), m_glWidget(nullptr), m_renderer(nullptr), |
| 77 |
m_toolWidget(new TemplateToolWidget(qobject_cast<QWidget*>(parent_))), |
| 78 |
m_pressedButtons(Qt::NoButton), |
| 79 |
m_clickedAtomicNumber(INVALID_ATOMIC_NUMBER), m_bondAdded(false), |
| 80 |
m_fixValenceLater(false), m_layerManager("Template") |
| 81 |
{ |
| 82 |
QString shortcut = tr("Ctrl+3", "control-key 3"); |
| 83 |
m_activateAction->setText(tr("Template")); |
| 84 |
m_activateAction->setToolTip( |
| 85 |
tr("Template Tool\t(%1)\n\n" |
| 86 |
"Insert fragments, including metal centers.\n" |
| 87 |
"Select an element and coordination geometry, " |
| 88 |
"then click to insert a fragment.\n\n" |
| 89 |
"Select a ligand or functional group and click " |
| 90 |
"on a hydrogen atom to attach it.") |
| 91 |
.arg(shortcut)); |
| 92 |
setIcon(); |
| 93 |
reset(); |
| 94 |
} |
| 95 |
|
| 96 |
TemplateTool::~TemplateTool() {} |
| 97 |
|
| 98 |
void TemplateTool::setIcon(bool darkTheme) |
| 99 |
{ |
| 100 |
if (darkTheme) |
| 101 |
m_activateAction->setIcon(QIcon(":/icons/template_dark.svg")); |
| 102 |
else |
| 103 |
m_activateAction->setIcon(QIcon(":/icons/template_light.svg")); |
| 104 |
} |
| 105 |
|
| 106 |
QWidget* TemplateTool::toolWidget() const |
| 107 |
{ |
| 108 |
return m_toolWidget; |
| 109 |
} |
| 110 |
|
| 111 |
QUndoCommand* TemplateTool::mousePressEvent(QMouseEvent* e) |
| 112 |
{ |
| 113 |
clearKeyPressBuffer(); |
| 114 |
if (!m_renderer || !m_molecule) |
| 115 |
return nullptr; |
| 116 |
|
| 117 |
updatePressedButtons(e, false); |
| 118 |
m_clickPosition = e->pos(); |
| 119 |
|
| 120 |
if (m_layerManager.activeLayerLocked()) { |
| 121 |
// revert to navigation mode |
| 122 |
return nullptr; |
| 123 |
} |
| 124 |
|
| 125 |
// check if we have modifier keys |
| 126 |
// if so, revert to navigation mode |
| 127 |
// e.g., rotate, zoom |
| 128 |
if (e->modifiers() == Qt::ShiftModifier || e->modifiers() == Qt::AltModifier) |
| 129 |
return nullptr; |
| 130 |
|
| 131 |
m_molecule->setInteractive(true); |
| 132 |
|
| 133 |
if (m_pressedButtons & Qt::LeftButton) { |
| 134 |
m_clickedObject = m_renderer->hit(e->pos().x(), e->pos().y()); |
| 135 |
|
| 136 |
switch (m_clickedObject.type) { |
| 137 |
case Rendering::InvalidType: |
| 138 |
emptyLeftClick(e); |
| 139 |
return nullptr; |
| 140 |
case Rendering::AtomType: |
| 141 |
atomLeftClick(e); |
| 142 |
return nullptr; |
| 143 |
default: |
| 144 |
break; |
| 145 |
} |
| 146 |
} else if (m_pressedButtons & Qt::RightButton) { |
| 147 |
m_clickedObject = m_renderer->hit(e->pos().x(), e->pos().y()); |
| 148 |
|
| 149 |
switch (m_clickedObject.type) { |
| 150 |
case Rendering::AtomType: |
| 151 |
atomRightClick(e); |
| 152 |
return nullptr; |
| 153 |
default: |
| 154 |
break; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
return nullptr; |
| 159 |
} |
| 160 |
|
| 161 |
QUndoCommand* TemplateTool::mouseReleaseEvent(QMouseEvent* e) |
| 162 |
{ |
| 163 |
if (!m_renderer) |
| 164 |
return nullptr; |
| 165 |
|
| 166 |
updatePressedButtons(e, true); |
| 167 |
|
| 168 |
if (m_molecule) { |
| 169 |
m_molecule->setInteractive(false); |
| 170 |
} |
| 171 |
|
| 172 |
if (m_clickedObject.type == Rendering::InvalidType) |
| 173 |
return nullptr; |
| 174 |
|
| 175 |
switch (e->button()) { |
| 176 |
case Qt::LeftButton: |
| 177 |
case Qt::RightButton: |
| 178 |
reset(); |
| 179 |
e->accept(); |
| 180 |
break; |
| 181 |
default: |
| 182 |
break; |
| 183 |
} |
| 184 |
|
| 185 |
return nullptr; |
| 186 |
} |
| 187 |
|
| 188 |
QUndoCommand* TemplateTool::mouseMoveEvent(QMouseEvent* e) |
| 189 |
{ |
| 190 |
return nullptr; |
| 191 |
} |
| 192 |
|
| 193 |
QUndoCommand* TemplateTool::keyPressEvent(QKeyEvent* e) |
| 194 |
{ |
| 195 |
if (e->text().isEmpty()) |
| 196 |
return nullptr; |
| 197 |
|
| 198 |
e->accept(); |
| 199 |
|
| 200 |
// check which tab is currently active |
| 201 |
int currentTab = m_toolWidget->currentTab(); |
| 202 |
|
| 203 |
// if it's arrow keys, change tabs |
| 204 |
if (e->key() == Qt::Key_Left || e->key() == Qt::Key_Right || |
| 205 |
e->key() == Qt::Key_BracketLeft || e->key() == Qt::Key_BracketRight) { |
| 206 |
// cycle through tabs |
| 207 |
// the widget will handle any wrap-around |
| 208 |
if (e->key() == Qt::Key_Left || e->key() == Qt::Key_BracketLeft) { |
| 209 |
currentTab--; |
| 210 |
} else { |
| 211 |
currentTab++; |
| 212 |
} |
| 213 |
m_toolWidget->setCurrentTab(currentTab); |
| 214 |
return nullptr; |
| 215 |
} |
| 216 |
|
| 217 |
// Set a timer to clear the buffer on first keypress: |
| 218 |
if (m_keyPressBuffer.isEmpty()) |
| 219 |
QTimer::singleShot(2000, this, SLOT(clearKeyPressBuffer())); |
| 220 |
|
| 221 |
m_keyPressBuffer.append(e->text()); |
| 222 |
|
| 223 |
// Check for matches before clearing buffer |
| 224 |
// (allows multi-character shortcuts like "bpy", "acac", "edta") |
| 225 |
if (currentTab == 1) { |
| 226 |
const auto& keyMap = ligandKeyMap(); |
| 227 |
auto it = keyMap.find(m_keyPressBuffer); |
| 228 |
if (it != keyMap.end()) { |
| 229 |
m_toolWidget->setLigand(it->second); |
| 230 |
return nullptr; |
| 231 |
} |
| 232 |
} else if (currentTab == 2) { |
| 233 |
const auto& keyMap = groupKeyMap(); |
| 234 |
auto it = keyMap.find(m_keyPressBuffer); |
| 235 |
if (it != keyMap.end()) { |
| 236 |
m_toolWidget->setGroup(it->second); |
| 237 |
return nullptr; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
// Clear buffer if too long (elements/coordination use max 2 chars) |
| 242 |
if (m_keyPressBuffer.size() >= 5) { |
| 243 |
clearKeyPressBuffer(); |
| 244 |
return nullptr; |
| 245 |
} |
| 246 |
|
| 247 |
if (currentTab == 0) { |
| 248 |
// if it's + or -, change the formal charge |
| 249 |
if (e->key() == Qt::Key_Plus || e->key() == Qt::Key_Minus) { |
| 250 |
int formalCharge = m_toolWidget->formalCharge(); |
| 251 |
if (e->key() == Qt::Key_Plus) |
| 252 |
formalCharge++; |
| 253 |
else |
| 254 |
formalCharge--; |
| 255 |
m_toolWidget->setFormalCharge(formalCharge); |
| 256 |
clearKeyPressBuffer(); |
| 257 |
return nullptr; |
| 258 |
} |
| 259 |
|
| 260 |
// metal center -- interpret as an element |
| 261 |
int atomicNum = |
| 262 |
Core::Elements::atomicNumberFromSymbol(m_keyPressBuffer.toStdString()); |
| 263 |
|
| 264 |
if (atomicNum != Avogadro::InvalidElement) |
| 265 |
m_toolWidget->setAtomicNumber(static_cast<unsigned char>(atomicNum)); |
| 266 |
else { |
| 267 |
// if it's a number, try a coordination number |
| 268 |
bool ok = false; |
| 269 |
int coordinationNumber = m_keyPressBuffer.toInt(&ok); |
| 270 |
if (ok) { |
| 271 |
unsigned char geometry = 0; |
| 272 |
switch (coordinationNumber) { |
| 273 |
// 1, 2, 3, 4, 4, 5, 5, 6, 6, 7, 8 are valid |
| 274 |
case 1: |
| 275 |
break; |
| 276 |
case 2: |
| 277 |
geometry = 1; |
| 278 |
break; |
| 279 |
case 3: |
| 280 |
geometry = 2; |
| 281 |
break; |
| 282 |
case 4: |
| 283 |
geometry = 3; |
| 284 |
break; |
| 285 |
case 44: |
| 286 |
geometry = 4; |
| 287 |
break; |
| 288 |
case 5: |
| 289 |
geometry = 5; |
| 290 |
break; |
| 291 |
case 55: |
| 292 |
geometry = 6; |
| 293 |
break; |
| 294 |
case 6: |
| 295 |
geometry = 7; |
| 296 |
break; |
| 297 |
case 66: |
| 298 |
geometry = 8; |
| 299 |
break; |
| 300 |
case 7: |
| 301 |
geometry = 9; |
| 302 |
break; |
| 303 |
case 8: |
| 304 |
geometry = 10; |
| 305 |
break; |
| 306 |
default: |
| 307 |
// do nothing, invalid coordination number |
| 308 |
clearKeyPressBuffer(); |
| 309 |
break; |
| 310 |
} |
| 311 |
m_toolWidget->setCoordination(geometry); |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
// Ligand and group tab matches are handled above before the buffer size check |
| 316 |
|
| 317 |
return nullptr; |
| 318 |
} |
| 319 |
|
| 320 |
void TemplateTool::draw(Rendering::GroupNode&) {} |
| 321 |
|
| 322 |
void TemplateTool::updatePressedButtons(QMouseEvent* e, bool release) |
| 323 |
{ |
| 324 |
/// @todo Use modifier keys on mac |
| 325 |
if (release) |
| 326 |
m_pressedButtons &= e->buttons(); |
| 327 |
else |
| 328 |
m_pressedButtons |= e->buttons(); |
| 329 |
} |
| 330 |
|
| 331 |
void TemplateTool::reset() |
| 332 |
{ |
| 333 |
if (m_fixValenceLater) { |
| 334 |
Index a1 = m_newObject.index; |
| 335 |
Index a2 = m_bondedAtom.index; |
| 336 |
Index a3 = m_clickedObject.index; |
| 337 |
|
| 338 |
// order them |
| 339 |
if (a1 > a2) |
| 340 |
std::swap(a1, a2); |
| 341 |
if (a1 > a3) |
| 342 |
std::swap(a1, a3); |
| 343 |
if (a2 > a3) |
| 344 |
std::swap(a2, a3); |
| 345 |
|
| 346 |
// This preserves the order so they are adjusted in order. |
| 347 |
Core::Array<Index> atomIds; |
| 348 |
atomIds.push_back(a3); |
| 349 |
atomIds.push_back(a2); |
| 350 |
atomIds.push_back(a1); |
| 351 |
// This function checks to make sure the ids are valid, so no need |
| 352 |
// to check out here. |
| 353 |
m_molecule->adjustHydrogens(atomIds); |
| 354 |
|
| 355 |
Molecule::MoleculeChanges changes = Molecule::Atoms | Molecule::Added; |
| 356 |
changes |= Molecule::Bonds | Molecule::Added | Molecule::Removed; |
| 357 |
|
| 358 |
m_molecule->emitChanged(changes); |
| 359 |
|
| 360 |
m_fixValenceLater = false; |
| 361 |
} |
| 362 |
|
| 363 |
m_clickedObject = Identifier(); |
| 364 |
m_newObject = Identifier(); |
| 365 |
m_bondedAtom = Identifier(); |
| 366 |
m_clickPosition = QPoint(); |
| 367 |
m_pressedButtons = Qt::NoButton; |
| 368 |
m_clickedAtomicNumber = INVALID_ATOMIC_NUMBER; |
| 369 |
m_bondAdded = false; |
| 370 |
|
| 371 |
emit drawablesChanged(); |
| 372 |
} |
| 373 |
|
| 374 |
void TemplateTool::emptyLeftClick(QMouseEvent* e) |
| 375 |
{ |
| 376 |
// Get the coordinates of the clicked position |
| 377 |
if (m_renderer == nullptr) |
| 378 |
return; |
| 379 |
|
| 380 |
m_toolWidget->selectedUIDs().clear(); |
| 381 |
Vector2f windowPos(e->localPos().x(), e->localPos().y()); |
| 382 |
Vector3f atomPos = m_renderer->camera().unProject(windowPos); |
| 383 |
// center of inserted template |
| 384 |
Vector3 center(0.0f, 0.0f, 0.0f); |
| 385 |
|
| 386 |
CjsonFormat ff; |
| 387 |
Molecule templateMolecule; |
| 388 |
|
| 389 |
// before we do anything, check if it's a metal or a ligand |
| 390 |
// in the dialog |
| 391 |
int currentTab = m_toolWidget->currentTab(); |
| 392 |
if (currentTab == 0) { // metal center |
| 393 |
QFile templ(":/templates/centers/" + m_toolWidget->coordinationString() + |
| 394 |
".cjson"); |
| 395 |
if (!templ.open(QFile::ReadOnly | QFile::Text)) |
| 396 |
return; |
| 397 |
QTextStream templateStream(&templ); |
| 398 |
|
| 399 |
if (!ff.readString(templateStream.readAll().toStdString(), |
| 400 |
templateMolecule)) |
| 401 |
return; |
| 402 |
|
| 403 |
// Add the atom and hydrogens around it following template |
| 404 |
[[maybe_unused]] size_t centerIndex = 0; |
| 405 |
for (size_t i = 0; i < templateMolecule.atomCount(); i++) { |
| 406 |
if (templateMolecule.atomicNumber(i) != 1) { |
| 407 |
center = templateMolecule.atomPosition3d(i); |
| 408 |
centerIndex = i; |
| 409 |
templateMolecule.setAtomicNumber(i, m_toolWidget->atomicNumber()); |
| 410 |
templateMolecule.setFormalCharge(i, m_toolWidget->formalCharge()); |
| 411 |
continue; |
| 412 |
} |
| 413 |
} |
| 414 |
// done with metal center and coordination |
| 415 |
} else { |
| 416 |
// ligand |
| 417 |
// check if it's clipboard first |
| 418 |
if (m_toolWidget->ligandString() == tr("Clipboard")) { |
| 419 |
const QMimeData* mimeData(QApplication::clipboard()->mimeData()); |
| 420 |
|
| 421 |
if (!mimeData) { |
| 422 |
return; |
| 423 |
} |
| 424 |
|
| 425 |
// Try to find a reader that can handle the available mime-types. |
| 426 |
Io::FileFormatManager& mgr = Io::FileFormatManager::instance(); |
| 427 |
QStringList mimeTypes(mimeData->formats()); |
| 428 |
Io::FileFormat* pastedFormat = nullptr; |
| 429 |
QByteArray pastedData; |
| 430 |
Io::FileFormat::Operations ops(Io::FileFormat::Read | |
| 431 |
Io::FileFormat::String); |
| 432 |
foreach (const QString& mimeType, mimeTypes) { |
| 433 |
if ((pastedFormat = |
| 434 |
mgr.newFormatFromMimeType(mimeType.toStdString(), ops))) { |
| 435 |
pastedData = mimeData->data(mimeType); |
| 436 |
break; |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
// No mime-type match, default to cjson. |
| 441 |
if (!pastedFormat && mimeData->hasText()) { |
| 442 |
pastedFormat = new Io::CjsonFormat; |
| 443 |
pastedData = mimeData->text().toLatin1(); |
| 444 |
} |
| 445 |
|
| 446 |
if (pastedFormat == nullptr) |
| 447 |
return; |
| 448 |
|
| 449 |
// we have a format, so try to insert the new bits into the molecule |
| 450 |
bool success = pastedFormat->readString( |
| 451 |
std::string(pastedData.constData(), pastedData.size()), |
| 452 |
templateMolecule); |
| 453 |
|
| 454 |
if (!success) |
| 455 |
return; |
| 456 |
|
| 457 |
center = templateMolecule.centerOfGeometry(); |
| 458 |
// change the dummy atom(s) to hydrogen |
| 459 |
for (size_t i = 0; i < templateMolecule.atomCount(); i++) { |
| 460 |
if (templateMolecule.atomicNumber(i) == 0) { |
| 461 |
templateMolecule.setAtomicNumber(i, 1); |
| 462 |
templateMolecule.setFormalCharge(i, 0); |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
// done with clipboard ligands |
| 467 |
} else { // a ligand file |
| 468 |
QString path; |
| 469 |
if (m_toolWidget->ligandString().endsWith(".cjson")) { |
| 470 |
// we already have the full path .. from the insert browser |
| 471 |
path = m_toolWidget->ligandString(); |
| 472 |
} else { |
| 473 |
path = ":/templates/ligands/" + m_toolWidget->ligandString() + ".cjson"; |
| 474 |
} |
| 475 |
|
| 476 |
QFile templ(path); |
| 477 |
if (!templ.open(QFile::ReadOnly | QFile::Text)) |
| 478 |
return; |
| 479 |
QTextStream templateStream(&templ); |
| 480 |
|
| 481 |
if (!ff.readString(templateStream.readAll().toStdString(), |
| 482 |
templateMolecule)) |
| 483 |
return; |
| 484 |
|
| 485 |
center = templateMolecule.centerOfGeometry(); |
| 486 |
// change the dummy atom(s) to hydrogen |
| 487 |
for (size_t i = 0; i < templateMolecule.atomCount(); i++) { |
| 488 |
if (templateMolecule.atomicNumber(i) == 0) { |
| 489 |
templateMolecule.setAtomicNumber(i, 1); |
| 490 |
templateMolecule.setFormalCharge(i, 0); |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
// done with ligand |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
// move the template to the clicked position |
| 499 |
for (size_t i = 0; i < templateMolecule.atomCount(); i++) { |
| 500 |
Vector3 pos = |
| 501 |
templateMolecule.atomPosition3d(i) - center + atomPos.cast<double>(); |
| 502 |
templateMolecule.setAtomPosition3d(i, pos); |
| 503 |
} |
| 504 |
|
| 505 |
size_t firstIndex = m_molecule->atomCount(); |
| 506 |
m_molecule->appendMolecule(templateMolecule, tr("Insert Template")); |
| 507 |
|
| 508 |
Molecule::MoleculeChanges changes = |
| 509 |
Molecule::Atoms | Molecule::Bonds | Molecule::Added; |
| 510 |
|
| 511 |
// m_fixValenceLater = true; // add hydrogens |
| 512 |
m_fixValenceLater = false; |
| 513 |
|
| 514 |
// Update the clicked object |
| 515 |
m_clickedObject.type = Rendering::AtomType; |
| 516 |
m_clickedObject.molecule = m_molecule; |
| 517 |
m_clickedObject.index = firstIndex; |
| 518 |
|
| 519 |
// Emit changed signal |
| 520 |
m_molecule->emitChanged(changes); |
| 521 |
|
| 522 |
e->accept(); |
| 523 |
} |
| 524 |
|
| 525 |
Vector3 rotateLigandCoords(Vector3 in, Vector3 centerVector, Vector3 outVector) |
| 526 |
{ |
| 527 |
if (centerVector.norm() == 0.0 || outVector.norm() == 0.0) |
| 528 |
return in; |
| 529 |
Vector3 axis = centerVector.cross(outVector); |
| 530 |
if (axis.norm() < 1e-12) { // vectors are parallel, let's pick an arbitrary |
| 531 |
// perpendicular axis |
| 532 |
Matrix3 rotx = |
| 533 |
Eigen::AngleAxisd(M_PI / 2.0, Vector3(1.0, 0.0, 0.0)).toRotationMatrix(); |
| 534 |
Matrix3 roty = |
| 535 |
Eigen::AngleAxisd(M_PI / 2.0, Vector3(0.0, 1.0, 0.0)).toRotationMatrix(); |
| 536 |
axis = centerVector.cross(rotx * outVector); |
| 537 |
if (axis.norm() < 1e-12) |
| 538 |
axis = centerVector.cross(roty * outVector); |
| 539 |
} |
| 540 |
axis.normalize(); |
| 541 |
double cosine = |
| 542 |
centerVector.dot(outVector) / centerVector.norm() / outVector.norm(); |
| 543 |
double angle = (abs(cosine) < 1.0) ? acos(cosine) : 0.0; |
| 544 |
Matrix3 rot = Eigen::AngleAxisd(angle, axis).toRotationMatrix(); |
| 545 |
return rot * in; |
| 546 |
} |
| 547 |
|
| 548 |
Matrix3 applyKabsch(std::vector<Vector3> templatePoints, |
| 549 |
std::vector<Vector3> moleculePoints) |
| 550 |
{ |
| 551 |
assert(templatePoints.size() == moleculePoints.size()); |
| 552 |
MatrixX TP(templatePoints.size(), 3); |
| 553 |
MatrixX MP(templatePoints.size(), 3); |
| 554 |
for (size_t i = 0; i < templatePoints.size(); i++) { |
| 555 |
TP.row(i) = templatePoints[i]; |
| 556 |
MP.row(i) = moleculePoints[i]; |
| 557 |
} |
| 558 |
Matrix3 H = TP.transpose() * MP; |
| 559 |
Eigen::JacobiSVD<MatrixX> svd(H, Eigen::ComputeFullU | Eigen::ComputeFullV); |
| 560 |
MatrixX U = svd.matrixU(); |
| 561 |
Matrix3 V = svd.matrixV(); |
| 562 |
Matrix3 Idd = Matrix3::Identity(); |
| 563 |
Idd(2, 2) = copysign(1.0, (V * U.transpose()).determinant()); |
| 564 |
Matrix3 r = V * Idd * U.transpose(); |
| 565 |
return r; |
| 566 |
} |
| 567 |
|
| 568 |
void TemplateTool::atomLeftClick(QMouseEvent*) |
| 569 |
{ |
| 570 |
size_t selectedIndex = m_clickedObject.index; |
| 571 |
// if it's a valid selected atom and a hydrogen or dummy atom |
| 572 |
if (m_molecule->atom(selectedIndex).isValid() && |
| 573 |
(m_molecule->atomicNumber(selectedIndex) == 1 || |
| 574 |
m_molecule->atomicNumber(selectedIndex) == 0)) { |
| 575 |
m_toolWidget->selectedUIDs().push_back( |
| 576 |
m_molecule->atomUniqueId(selectedIndex)); |
| 577 |
if (static_cast<int>(m_toolWidget->selectedUIDs().size()) != |
| 578 |
m_toolWidget->denticity()) |
| 579 |
return; |
| 580 |
|
| 581 |
// Get the ligand template |
| 582 |
// - check if we should use the clipboard |
| 583 |
// - otherwise use the template |
| 584 |
Molecule templateMolecule; |
| 585 |
|
| 586 |
if (m_toolWidget->ligandString() == tr("Clipboard")) { |
| 587 |
const QMimeData* mimeData(QApplication::clipboard()->mimeData()); |
| 588 |
|
| 589 |
if (!mimeData) { |
| 590 |
return; |
| 591 |
} |
| 592 |
|
| 593 |
// Try to find a reader that can handle the available mime-types. |
| 594 |
Io::FileFormatManager& mgr = Io::FileFormatManager::instance(); |
| 595 |
QStringList mimeTypes(mimeData->formats()); |
| 596 |
Io::FileFormat* pastedFormat = nullptr; |
| 597 |
QByteArray pastedData; |
| 598 |
Io::FileFormat::Operations ops(Io::FileFormat::Read | |
| 599 |
Io::FileFormat::String); |
| 600 |
foreach (const QString& mimeType, mimeTypes) { |
| 601 |
if ((pastedFormat = |
| 602 |
mgr.newFormatFromMimeType(mimeType.toStdString(), ops))) { |
| 603 |
pastedData = mimeData->data(mimeType); |
| 604 |
break; |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
// No mime-type match, default to cjson. |
| 609 |
if (!pastedFormat && mimeData->hasText()) { |
| 610 |
pastedFormat = new Io::CjsonFormat; |
| 611 |
pastedData = mimeData->text().toLatin1(); |
| 612 |
} |
| 613 |
|
| 614 |
if (pastedFormat == nullptr) |
| 615 |
return; |
| 616 |
|
| 617 |
// we have a format, so try to insert the new bits into the molecule |
| 618 |
bool success = pastedFormat->readString( |
| 619 |
std::string(pastedData.constData(), pastedData.size()), |
| 620 |
templateMolecule); |
| 621 |
|
| 622 |
if (!success) |
| 623 |
return; |
| 624 |
|
| 625 |
} else { |
| 626 |
QString path; |
| 627 |
if (m_toolWidget->ligandString().endsWith(".cjson")) { |
| 628 |
// we already have the full path .. from the insert browser |
| 629 |
path = m_toolWidget->ligandString(); |
| 630 |
} else { |
| 631 |
path = ":/templates/ligands/" + m_toolWidget->ligandString() + ".cjson"; |
| 632 |
} |
| 633 |
|
| 634 |
QFile templ(path); |
| 635 |
if (!templ.open(QFile::ReadOnly | QFile::Text)) |
| 636 |
return; |
| 637 |
QTextStream templateStream(&templ); |
| 638 |
|
| 639 |
CjsonFormat ff; |
| 640 |
|
| 641 |
if (!ff.readString(templateStream.readAll().toStdString(), |
| 642 |
templateMolecule)) |
| 643 |
return; |
| 644 |
} |
| 645 |
|
| 646 |
// Find dummy atom in template and get all necessary info |
| 647 |
// for haptic ligands, we pick the dummy atom that's |
| 648 |
// furthest from the centroid of the carbon atoms |
| 649 |
Vector3 centroid(0.0, 0.0, 0.0); |
| 650 |
unsigned carbonCount = 0; |
| 651 |
for (size_t i = 0; i < templateMolecule.atomCount(); ++i) { |
| 652 |
if (templateMolecule.atomicNumber(i) == 6) { |
| 653 |
carbonCount++; |
| 654 |
centroid += templateMolecule.atomPosition3d(i); |
| 655 |
} |
| 656 |
} |
| 657 |
if (carbonCount > 1) |
| 658 |
centroid = centroid / carbonCount; |
| 659 |
|
| 660 |
size_t templateDummyIndex = 0; |
| 661 |
std::vector<size_t> templateLigandIndices; |
| 662 |
std::vector<size_t> templateLigandUIDs; |
| 663 |
float maxDistance = 0.0; |
| 664 |
for (size_t i = 0; i < templateMolecule.atomCount(); ++i) { |
| 665 |
// in some ligands (e.g., haptic) we might have two dummy atoms |
| 666 |
// so we only select the one furthest from the ligand centroid |
| 667 |
if (templateMolecule.atomicNumber(i) == 0) { |
| 668 |
Vector3 delta = templateMolecule.atomPosition3d(i) - centroid; |
| 669 |
if (delta.squaredNorm() < maxDistance) |
| 670 |
continue; // too close to the centroid |
| 671 |
|
| 672 |
maxDistance = delta.squaredNorm(); |
| 673 |
templateDummyIndex = i; |
| 674 |
templateLigandIndices.clear(); |
| 675 |
templateLigandUIDs.clear(); |
| 676 |
for (const auto& bond : templateMolecule.bonds(i)) { |
| 677 |
size_t newIndex = bond.getOtherAtom(i).index(); |
| 678 |
templateLigandIndices.push_back(newIndex); |
| 679 |
templateLigandUIDs.push_back(templateMolecule.atomUniqueId(newIndex)); |
| 680 |
} |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
// Find center atom in our current molecule and get all necessary info |
| 685 |
// - first check to see if there is a bond |
| 686 |
Vector3 moleculeLigandOutVector(0.0, 0.0, 0.0); |
| 687 |
Vector3 displacement(0.0, 0.0, 0.0); |
| 688 |
[[maybe_unused]] Vector3 centerPosition = |
| 689 |
m_molecule->atomPosition3d(selectedIndex); |
| 690 |
size_t moleculeCenterIndex = selectedIndex; |
| 691 |
size_t moleculeCenterUID = m_molecule->atomUniqueId(moleculeCenterIndex); |
| 692 |
|
| 693 |
if (m_molecule->bonds(selectedIndex).size() != 0) { |
| 694 |
moleculeCenterIndex = |
| 695 |
m_molecule->bonds(selectedIndex)[0].getOtherAtom(selectedIndex).index(); |
| 696 |
moleculeCenterUID = m_molecule->atomUniqueId(moleculeCenterIndex); |
| 697 |
for (size_t UID : m_toolWidget->selectedUIDs()) { |
| 698 |
size_t index = m_molecule->atomByUniqueId(UID).index(); |
| 699 |
Vector3 newPos = m_molecule->atomPosition3d(index); |
| 700 |
moleculeLigandOutVector += |
| 701 |
newPos - m_molecule->atomPosition3d(moleculeCenterIndex); |
| 702 |
} |
| 703 |
|
| 704 |
// Estimate and try to realize bond distances |
| 705 |
for (size_t i = 0; i < templateLigandIndices.size(); i++) { |
| 706 |
unsigned char ligandAtomicNumber = |
| 707 |
templateMolecule.atomicNumber(templateLigandIndices[i]); |
| 708 |
ligandAtomicNumber = (ligandAtomicNumber == 0) ? 6 : ligandAtomicNumber; |
| 709 |
// Estimate as the sum of covalent radii |
| 710 |
double bondDistance = Elements::radiusCovalent(ligandAtomicNumber) + |
| 711 |
Elements::radiusCovalent( |
| 712 |
m_molecule->atomicNumber(moleculeCenterIndex)); |
| 713 |
Vector3 inVector = |
| 714 |
templateMolecule.atomPosition3d(templateDummyIndex) - |
| 715 |
templateMolecule.atomPosition3d(templateLigandIndices[i]); |
| 716 |
Vector3 correctionVector = inVector; |
| 717 |
correctionVector.normalize(); |
| 718 |
correctionVector *= bondDistance - inVector.norm(); |
| 719 |
displacement += correctionVector; |
| 720 |
} |
| 721 |
displacement *= 1.0 / templateLigandIndices.size(); |
| 722 |
} else { |
| 723 |
// direction can be random |
| 724 |
displacement = Eigen::Vector3d::Random(); |
| 725 |
} |
| 726 |
Vector3 newPos = |
| 727 |
templateMolecule.atomPosition3d(templateDummyIndex) + displacement; |
| 728 |
templateMolecule.setAtomPosition3d(templateDummyIndex, newPos); |
| 729 |
|
| 730 |
// Translate template so dummy atom is brought to center atom |
| 731 |
for (size_t i = 0; i < templateMolecule.atomCount(); i++) { |
| 732 |
if (i != templateDummyIndex) { |
| 733 |
templateMolecule.setAtomPosition3d( |
| 734 |
i, templateMolecule.atomPosition3d(i) - |
| 735 |
templateMolecule.atomPosition3d(templateDummyIndex) + |
| 736 |
m_molecule->atomPosition3d(moleculeCenterIndex)); |
| 737 |
} |
| 738 |
} |
| 739 |
|
| 740 |
if (m_molecule->bonds(selectedIndex).size() != 0) { |
| 741 |
// Create arrays with the points to align and apply Kabsch algorithm |
| 742 |
std::vector<Vector3> templateLigandPositions; |
| 743 |
for (size_t index : templateLigandIndices) |
| 744 |
templateLigandPositions.push_back( |
| 745 |
templateMolecule.atomPosition3d(index) - |
| 746 |
m_molecule->atomPosition3d(moleculeCenterIndex)); |
| 747 |
std::vector<Vector3> moleculeLigandPositions; |
| 748 |
for (size_t UID : m_toolWidget->selectedUIDs()) |
| 749 |
moleculeLigandPositions.push_back( |
| 750 |
m_molecule->atomPosition3d(m_molecule->atomByUniqueId(UID).index()) - |
| 751 |
m_molecule->atomPosition3d(moleculeCenterIndex)); |
| 752 |
Matrix3 rotation = |
| 753 |
applyKabsch(templateLigandPositions, moleculeLigandPositions); |
| 754 |
for (size_t i = 0; i < templateMolecule.atomCount(); i++) { |
| 755 |
if (i != templateDummyIndex) { |
| 756 |
templateMolecule.setAtomPosition3d( |
| 757 |
i, rotation * (templateMolecule.atomPosition3d(i) - |
| 758 |
m_molecule->atomPosition3d(moleculeCenterIndex)) + |
| 759 |
m_molecule->atomPosition3d(moleculeCenterIndex)); |
| 760 |
} |
| 761 |
} |
| 762 |
|
| 763 |
// Rotate partially aligned template to align "out" vectors |
| 764 |
Vector3 templateLigandOutVector(0.0, 0.0, 0.0); |
| 765 |
for (size_t index : templateLigandIndices) { |
| 766 |
Vector3 pos = templateMolecule.atomPosition3d(index); |
| 767 |
templateLigandOutVector += |
| 768 |
pos - m_molecule->atomPosition3d(moleculeCenterIndex); |
| 769 |
} |
| 770 |
for (size_t i = 0; i < templateMolecule.atomCount(); i++) { |
| 771 |
if (templateMolecule.atomicNumber(i) != 0) { |
| 772 |
templateMolecule.setAtomPosition3d( |
| 773 |
i, rotateLigandCoords( |
| 774 |
templateMolecule.atomPosition3d(i) - |
| 775 |
m_molecule->atomPosition3d(moleculeCenterIndex), |
| 776 |
templateLigandOutVector, moleculeLigandOutVector) + |
| 777 |
m_molecule->atomPosition3d(moleculeCenterIndex)); |
| 778 |
} |
| 779 |
} |
| 780 |
} |
| 781 |
|
| 782 |
// Remove dummy atoms |
| 783 |
for (size_t i = 0; i < templateMolecule.atomCount(); i++) { |
| 784 |
if (templateMolecule.atomicNumber(i) == 0) { |
| 785 |
templateMolecule.removeAtom(i); |
| 786 |
i--; // repeat index to counteract swapping |
| 787 |
} |
| 788 |
} |
| 789 |
|
| 790 |
std::vector<size_t> templateNewLigandIndices; |
| 791 |
for (size_t UID : templateLigandUIDs) { |
| 792 |
auto atom = templateMolecule.atomByUniqueId(UID); |
| 793 |
if (atom.isValid()) |
| 794 |
templateNewLigandIndices.push_back(atom.index()); |
| 795 |
} |
| 796 |
|
| 797 |
// Remove selected atoms and insert ligand |
| 798 |
// (unless there wasn't a bond to begin with) |
| 799 |
if (m_molecule->bonds(selectedIndex).size() != 0) { |
| 800 |
for (size_t UID : m_toolWidget->selectedUIDs()) |
| 801 |
m_molecule->removeAtom(m_molecule->atomByUniqueId(UID).index()); |
| 802 |
} |
| 803 |
size_t moleculeBaseIndex = m_molecule->atomCount(); |
| 804 |
m_molecule->appendMolecule(templateMolecule, tr("Insert Ligand")); |
| 805 |
|
| 806 |
// Create new bonds |
| 807 |
size_t moleculeCenterNewIndex = |
| 808 |
m_molecule->atomByUniqueId(moleculeCenterUID).index(); |
| 809 |
for (size_t index : templateNewLigandIndices) |
| 810 |
m_molecule->addBond(index + moleculeBaseIndex, moleculeCenterNewIndex); |
| 811 |
|
| 812 |
m_toolWidget->selectedUIDs().clear(); |
| 813 |
} |
| 814 |
} |
| 815 |
|
| 816 |
void TemplateTool::atomRightClick(QMouseEvent* e) |
| 817 |
{ |
| 818 |
e->accept(); |
| 819 |
m_molecule->removeAtom(m_clickedObject.index); |
| 820 |
m_molecule->emitChanged(Molecule::Atoms | Molecule::Removed); |
| 821 |
} |
| 822 |
|
| 823 |
void TemplateTool::atomLeftDrag(QMouseEvent*) |
| 824 |
{ |
| 825 |
// by default, don't allow drags for bonds |
| 826 |
return; |
| 827 |
} |
| 828 |
|
| 829 |
} // namespace QtPlugins |
| 830 |
} // namespace Avogadro |