| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
|
| 4 |
Adapted from Avogadro 1.x with the following authors' permission: |
| 5 |
Copyright (C) 2007 by Shahzad Ali |
| 6 |
Copyright (C) 2007 by Ross Braithwaite |
| 7 |
Copyright (C) 2007 by James Bunt |
| 8 |
Copyright (C) 2007,2008 by Marcus D. Hanwell |
| 9 |
Copyright (C) 2006,2007 by Benoit Jacob |
| 10 |
|
| 11 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 12 |
******************************************************************************/ |
| 13 |
|
| 14 |
#include "bondcentrictool.h" |
| 15 |
|
| 16 |
#include <avogadro/qtopengl/glwidget.h> |
| 17 |
|
| 18 |
#include <avogadro/rendering/arcsector.h> |
| 19 |
#include <avogadro/rendering/arcstrip.h> |
| 20 |
#include <avogadro/rendering/geometrynode.h> |
| 21 |
#include <avogadro/rendering/glrenderer.h> |
| 22 |
#include <avogadro/rendering/groupnode.h> |
| 23 |
#include <avogadro/rendering/linestripgeometry.h> |
| 24 |
#include <avogadro/rendering/meshgeometry.h> |
| 25 |
#include <avogadro/rendering/quad.h> |
| 26 |
#include <avogadro/rendering/quadoutline.h> |
| 27 |
#include <avogadro/rendering/textlabel3d.h> |
| 28 |
#include <avogadro/rendering/textproperties.h> |
| 29 |
|
| 30 |
#include <avogadro/core/array.h> |
| 31 |
#include <avogadro/core/atom.h> |
| 32 |
#include <avogadro/core/elements.h> |
| 33 |
#include <avogadro/core/vector.h> |
| 34 |
#include <avogadro/qtgui/molecule.h> |
| 35 |
#include <avogadro/qtgui/rwmolecule.h> |
| 36 |
|
| 37 |
#include <QAction> |
| 38 |
#include <QIcon> |
| 39 |
#include <QMouseEvent> |
| 40 |
|
| 41 |
#include <Eigen/Geometry> |
| 42 |
|
| 43 |
#include <cmath> |
| 44 |
|
| 45 |
#ifndef M_PI |
| 46 |
#define M_PI 3.14159265358979323846 |
| 47 |
#endif |
| 48 |
|
| 49 |
namespace Avogadro::QtPlugins { |
| 50 |
|
| 51 |
using Core::Array; |
| 52 |
using QtGui::Molecule; |
| 53 |
using QtGui::RWAtom; |
| 54 |
using QtGui::RWBond; |
| 55 |
using QtGui::RWMolecule; |
| 56 |
using Rendering::ArcSector; |
| 57 |
using Rendering::ArcStrip; |
| 58 |
using Rendering::GeometryNode; |
| 59 |
using Rendering::Quad; |
| 60 |
using Rendering::QuadOutline; |
| 61 |
|
| 62 |
namespace { |
| 63 |
const std::string degreeString("°"); |
| 64 |
/// @todo Add wide character support to text renderer. |
| 65 |
const std::string angstromString("Å"); |
| 66 |
|
| 67 |
// Lookup for coloring bond angles: |
| 68 |
const Vector3ub& getColor(size_t i) |
| 69 |
{ |
| 70 |
static std::vector<Vector3ub> colors; |
| 71 |
if (colors.empty()) { |
| 72 |
colors.emplace_back(255, 64, 32); |
| 73 |
colors.emplace_back(64, 255, 32); |
| 74 |
colors.emplace_back(32, 64, 255); |
| 75 |
colors.emplace_back(255, 255, 32); |
| 76 |
colors.emplace_back(255, 32, 255); |
| 77 |
colors.emplace_back(32, 255, 255); |
| 78 |
colors.emplace_back(255, 128, 0); |
| 79 |
colors.emplace_back(128, 255, 0); |
| 80 |
colors.emplace_back(0, 255, 128); |
| 81 |
colors.emplace_back(0, 128, 255); |
| 82 |
colors.emplace_back(255, 0, 128); |
| 83 |
colors.emplace_back(128, 0, 255); |
| 84 |
} |
| 85 |
|
| 86 |
return colors[i % colors.size()]; |
| 87 |
} |
| 88 |
|
| 89 |
// Returns unsigned, smallest angle between v1 and v2 |
| 90 |
inline float vectorAngleDegrees(const Vector3f& v1, const Vector3f& v2) |
| 91 |
{ |
| 92 |
const float crossProductNorm(v1.cross(v2).norm()); |
| 93 |
const float dotProduct(v1.dot(v2)); |
| 94 |
return std::atan2(crossProductNorm, dotProduct) * RAD_TO_DEG_F; |
| 95 |
} |
| 96 |
|
| 97 |
// Returns signed, smallest angle between v1 and v2. Sign is determined from a |
| 98 |
// right hand rule around axis. |
| 99 |
inline float vectorAngleDegrees(const Vector3f& v1, const Vector3f& v2, |
| 100 |
const Vector3f& axis) |
| 101 |
{ |
| 102 |
const Vector3f crossProduct(v1.cross(v2)); |
| 103 |
const float crossProductNorm(crossProduct.norm()); |
| 104 |
const float dotProduct(v1.dot(v2)); |
| 105 |
const float signDet(crossProduct.dot(axis)); |
| 106 |
const float angle(std::atan2(crossProductNorm, dotProduct) * RAD_TO_DEG_F); |
| 107 |
return signDet > 0.f ? angle : -angle; |
| 108 |
} |
| 109 |
|
| 110 |
} // namespace |
| 111 |
|
| 112 |
BondCentricTool::BondCentricTool(QObject* parent_) |
| 113 |
: QtGui::ToolPlugin(parent_), m_activateAction(new QAction(this)), |
| 114 |
m_molecule(nullptr), m_renderer(nullptr), m_moveState(IgnoreMove), |
| 115 |
m_planeSnapIncr(10.f), m_snapPlaneToBonds(true) |
| 116 |
{ |
| 117 |
QString shortcut = tr("Ctrl+6", "control-key 6"); |
| 118 |
m_activateAction->setText(tr("Bond-Centric Manipulation")); |
| 119 |
m_activateAction->setToolTip( |
| 120 |
tr("Bond Centric Manipulation Tool\t(%1)\n\n" |
| 121 |
"Left Mouse:\tClick and drag to rotate the view.\n" |
| 122 |
"Middle Mouse:\tClick and drag to zoom in or out.\n" |
| 123 |
"Right Mouse:\tClick and drag to move the view.\n" |
| 124 |
"Double-Click:\tReset the view.\n\n" |
| 125 |
"Left Click & Drag on a Bond to set the Manipulation Plane\n" |
| 126 |
"Left Click & Drag one of the Atoms in the Bond to change the angle\n" |
| 127 |
"Right Click & Drag one of the Atoms in the Bond to change the length") |
| 128 |
.arg(shortcut)); |
| 129 |
setIcon(); |
| 130 |
} |
| 131 |
|
| 132 |
BondCentricTool::~BondCentricTool() {} |
| 133 |
|
| 134 |
void BondCentricTool::setIcon(bool darkTheme) |
| 135 |
{ |
| 136 |
if (darkTheme) |
| 137 |
m_activateAction->setIcon(QIcon(":/icons/bondcentric_dark.svg")); |
| 138 |
else |
| 139 |
m_activateAction->setIcon(QIcon(":/icons/bondcentric_light.svg")); |
| 140 |
} |
| 141 |
|
| 142 |
QWidget* BondCentricTool::toolWidget() const |
| 143 |
{ |
| 144 |
return nullptr; |
| 145 |
} |
| 146 |
|
| 147 |
void BondCentricTool::setMolecule(QtGui::Molecule* mol) |
| 148 |
{ |
| 149 |
if (mol && mol->undoMolecule() != m_molecule) { |
| 150 |
m_molecule = mol->undoMolecule(); |
| 151 |
reset(); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
void BondCentricTool::setEditMolecule(QtGui::RWMolecule* mol) |
| 156 |
{ |
| 157 |
if (m_molecule != mol) { |
| 158 |
m_molecule = mol; |
| 159 |
reset(); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
void BondCentricTool::setGLWidget(QtOpenGL::GLWidget*) {} |
| 164 |
|
| 165 |
void BondCentricTool::setGLRenderer(Rendering::GLRenderer* ren) |
| 166 |
{ |
| 167 |
m_renderer = ren; |
| 168 |
} |
| 169 |
|
| 170 |
QUndoCommand* BondCentricTool::mousePressEvent(QMouseEvent* e) |
| 171 |
{ |
| 172 |
// Don't start a new operation if we're already working: |
| 173 |
if (m_moveState != IgnoreMove) |
| 174 |
return nullptr; |
| 175 |
|
| 176 |
Rendering::Identifier ident = m_renderer->hit(e->pos().x(), e->pos().y()); |
| 177 |
|
| 178 |
// If no hits, return. Also ensure that the hit molecule is the one we expect. |
| 179 |
if (!ident.isValid() || ident.molecule != &m_molecule->molecule()) |
| 180 |
return nullptr; |
| 181 |
|
| 182 |
// If the hit is a left click on a bond, make it the selected bond and map |
| 183 |
// mouse movements to the bond plane rotation. |
| 184 |
if (ident.type == Rendering::BondType && e->button() == Qt::LeftButton) |
| 185 |
return initRotatePlane(e, ident); |
| 186 |
|
| 187 |
// Return if selectedBond is not valid or the hit is not on a bond: |
| 188 |
if (!m_selectedBond.isValid() || ident.type != Rendering::AtomType) |
| 189 |
return nullptr; |
| 190 |
|
| 191 |
// Test if the atom is in the selected bond, or one bond removed. |
| 192 |
RWAtom clickedAtom = m_molecule->atom(ident.index); |
| 193 |
RWBond selectedBond = m_selectedBond.bond(); |
| 194 |
bool atomIsInBond = bondContainsAtom(selectedBond, clickedAtom); |
| 195 |
bool atomIsNearBond = false; |
| 196 |
RWAtom anchorAtom; |
| 197 |
if (!atomIsInBond) { |
| 198 |
Array<RWBond> bonds = m_molecule->bonds(clickedAtom); |
| 199 |
for (auto& bond : bonds) { |
| 200 |
RWAtom atom = bond.getOtherAtom(clickedAtom); |
| 201 |
if (bondContainsAtom(selectedBond, atom)) { |
| 202 |
anchorAtom = atom; |
| 203 |
atomIsNearBond = true; |
| 204 |
break; |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
if (!atomIsInBond && !atomIsNearBond) |
| 210 |
return nullptr; |
| 211 |
|
| 212 |
if (m_molecule) { |
| 213 |
m_molecule->setInteractive(true); |
| 214 |
} |
| 215 |
|
| 216 |
// If the hit is a left click on an atom in the selected bond, prepare to |
| 217 |
// rotate the clicked bond around the other atom in the bond. |
| 218 |
if (atomIsInBond && e->button() == Qt::LeftButton) |
| 219 |
return initRotateBondedAtom(e, clickedAtom); |
| 220 |
|
| 221 |
// If the hit is a right click on an atom in the selected bond, prepare to |
| 222 |
// change the bond length. |
| 223 |
if (atomIsInBond && e->button() == Qt::RightButton) |
| 224 |
return initAdjustBondLength(e, clickedAtom); |
| 225 |
|
| 226 |
// Is the hit a left click on an atom bonded to an atom in selectedBond? |
| 227 |
if (atomIsNearBond && |
| 228 |
(e->button() == Qt::LeftButton || e->button() == Qt::RightButton)) { |
| 229 |
return initRotateNeighborAtom(e, clickedAtom, anchorAtom); |
| 230 |
} |
| 231 |
|
| 232 |
return nullptr; |
| 233 |
} |
| 234 |
|
| 235 |
QUndoCommand* BondCentricTool::mouseDoubleClickEvent(QMouseEvent* e) |
| 236 |
{ |
| 237 |
if (m_selectedBond.isValid() && e->button() == Qt::LeftButton) { |
| 238 |
reset(); |
| 239 |
emit drawablesChanged(); |
| 240 |
} |
| 241 |
return nullptr; |
| 242 |
} |
| 243 |
|
| 244 |
QUndoCommand* BondCentricTool::mouseMoveEvent(QMouseEvent* e) |
| 245 |
{ |
| 246 |
if (m_moveState == IgnoreMove) |
| 247 |
return nullptr; |
| 248 |
|
| 249 |
QUndoCommand* result = nullptr; |
| 250 |
|
| 251 |
switch (m_moveState) { |
| 252 |
case RotatePlane: |
| 253 |
result = rotatePlane(e); |
| 254 |
break; |
| 255 |
case RotateBondedAtom: |
| 256 |
result = rotateBondedAtom(e); |
| 257 |
break; |
| 258 |
case AdjustBondLength: |
| 259 |
result = adjustBondLength(e); |
| 260 |
break; |
| 261 |
case RotateNeighborAtom: |
| 262 |
result = rotateNeighborAtom(e); |
| 263 |
break; |
| 264 |
default: |
| 265 |
break; |
| 266 |
} |
| 267 |
|
| 268 |
return result; |
| 269 |
} |
| 270 |
|
| 271 |
QUndoCommand* BondCentricTool::mouseReleaseEvent(QMouseEvent*) |
| 272 |
{ |
| 273 |
if (m_moveState != IgnoreMove) { |
| 274 |
reset(KeepBond); |
| 275 |
emit drawablesChanged(); |
| 276 |
|
| 277 |
if (m_molecule) { |
| 278 |
m_molecule->setInteractive(false); // allow an undo now |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
return nullptr; |
| 283 |
} |
| 284 |
|
| 285 |
void BondCentricTool::draw(Rendering::GroupNode& node) |
| 286 |
{ |
| 287 |
RWBond selectedBond = m_selectedBond.bond(); |
| 288 |
|
| 289 |
if (!selectedBond.isValid()) |
| 290 |
return; |
| 291 |
|
| 292 |
auto* geo = new GeometryNode; |
| 293 |
node.addChild(geo); |
| 294 |
|
| 295 |
switch (m_moveState) { |
| 296 |
default: |
| 297 |
case IgnoreMove: |
| 298 |
case RotatePlane: |
| 299 |
drawBondQuad(*geo, selectedBond); |
| 300 |
drawAtomBondAngles(*geo, selectedBond.atom1(), selectedBond); |
| 301 |
drawAtomBondAngles(*geo, selectedBond.atom2(), selectedBond); |
| 302 |
break; |
| 303 |
|
| 304 |
case RotateBondedAtom: { |
| 305 |
drawBondQuad(*geo, selectedBond); |
| 306 |
|
| 307 |
RWAtom otherAtom = selectedBond.getOtherAtom(m_clickedAtom.atom()); |
| 308 |
if (otherAtom.isValid()) { |
| 309 |
drawAtomBondAngles(*geo, otherAtom, selectedBond); |
| 310 |
} |
| 311 |
|
| 312 |
break; |
| 313 |
} |
| 314 |
|
| 315 |
case AdjustBondLength: |
| 316 |
drawBondQuad(*geo, selectedBond); |
| 317 |
drawBondLengthLabel(*geo, selectedBond); |
| 318 |
break; |
| 319 |
|
| 320 |
case RotateNeighborAtom: { |
| 321 |
RWAtom clickedAtom = m_clickedAtom.atom(); |
| 322 |
RWAtom anchorAtom = m_anchorAtom.atom(); |
| 323 |
RWBond otherBond = m_molecule->bond(clickedAtom, anchorAtom); |
| 324 |
if (otherBond.isValid()) |
| 325 |
drawBondAngle(*geo, selectedBond, otherBond); |
| 326 |
break; |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
void BondCentricTool::reset(BondCentricTool::ResetBondBehavior bond) |
| 332 |
{ |
| 333 |
if (bond == ResetBond) |
| 334 |
m_selectedBond.reset(); |
| 335 |
|
| 336 |
m_clickedAtom.reset(); |
| 337 |
m_anchorAtom.reset(); |
| 338 |
m_moveState = IgnoreMove; |
| 339 |
m_clickedPoint = QPoint(); |
| 340 |
} |
| 341 |
|
| 342 |
void BondCentricTool::initializeBondVectors() |
| 343 |
{ |
| 344 |
RWBond bond = m_selectedBond.bond(); |
| 345 |
if (bond.isValid()) { |
| 346 |
m_bondVector = (bond.atom2().position3d().cast<float>() - |
| 347 |
bond.atom1().position3d().cast<float>()) |
| 348 |
.normalized(); |
| 349 |
m_planeNormalMouse = m_bondVector.unitOrthogonal(); |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
void BondCentricTool::updateBondVector() |
| 354 |
{ |
| 355 |
RWBond bond = m_selectedBond.bond(); |
| 356 |
if (bond.isValid()) { |
| 357 |
m_bondVector = (bond.atom2().position3d().cast<float>() - |
| 358 |
bond.atom1().position3d().cast<float>()) |
| 359 |
.normalized(); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
QUndoCommand* BondCentricTool::initRotatePlane( |
| 364 |
QMouseEvent* e, const Rendering::Identifier& ident) |
| 365 |
{ |
| 366 |
RWBond selectedBond = m_molecule->bond(ident.index); |
| 367 |
// Get unique id: |
| 368 |
Index bondUniqueId = m_molecule->bondUniqueId(selectedBond); |
| 369 |
if (bondUniqueId == MaxIndex) |
| 370 |
return nullptr; // Something went horribly wrong. |
| 371 |
|
| 372 |
// Reset the bond vector/plane normal if the bond changed |
| 373 |
if (bondUniqueId != m_selectedBond.uniqueIdentifier()) { |
| 374 |
m_selectedBond = |
| 375 |
QtGui::RWMolecule::PersistentBondType(m_molecule, bondUniqueId); |
| 376 |
initializeBondVectors(); |
| 377 |
} |
| 378 |
updatePlaneSnapAngles(); |
| 379 |
updateSnappedPlaneNormal(); |
| 380 |
if (!m_selectedBond.isValid()) |
| 381 |
return nullptr; |
| 382 |
e->accept(); |
| 383 |
m_moveState = RotatePlane; |
| 384 |
m_clickedPoint = e->pos(); |
| 385 |
m_lastDragPoint = e->pos(); |
| 386 |
emit drawablesChanged(); |
| 387 |
return nullptr; |
| 388 |
} |
| 389 |
|
| 390 |
QUndoCommand* BondCentricTool::initRotateBondedAtom( |
| 391 |
QMouseEvent* e, const QtGui::RWAtom& clickedAtom) |
| 392 |
{ |
| 393 |
m_clickedAtom = RWMolecule::PersistentAtomType(clickedAtom); |
| 394 |
if (!m_clickedAtom.isValid()) |
| 395 |
return nullptr; |
| 396 |
e->accept(); |
| 397 |
m_moveState = RotateBondedAtom; |
| 398 |
m_clickedPoint = e->pos(); |
| 399 |
m_lastDragPoint = e->pos(); |
| 400 |
resetFragment(); |
| 401 |
emit drawablesChanged(); |
| 402 |
return nullptr; |
| 403 |
} |
| 404 |
|
| 405 |
QUndoCommand* BondCentricTool::initAdjustBondLength( |
| 406 |
QMouseEvent* e, const QtGui::RWAtom& clickedAtom) |
| 407 |
{ |
| 408 |
m_clickedAtom = RWMolecule::PersistentAtomType(clickedAtom); |
| 409 |
if (!m_clickedAtom.isValid()) |
| 410 |
return nullptr; |
| 411 |
e->accept(); |
| 412 |
m_moveState = AdjustBondLength; |
| 413 |
m_clickedPoint = e->pos(); |
| 414 |
m_lastDragPoint = e->pos(); |
| 415 |
resetFragment(); |
| 416 |
emit drawablesChanged(); |
| 417 |
return nullptr; |
| 418 |
} |
| 419 |
|
| 420 |
QUndoCommand* BondCentricTool::initRotateNeighborAtom( |
| 421 |
QMouseEvent* e, const QtGui::RWAtom& clickedAtom, |
| 422 |
const QtGui::RWAtom& anchorAtom) |
| 423 |
{ |
| 424 |
m_clickedAtom = RWMolecule::PersistentAtomType(clickedAtom); |
| 425 |
m_anchorAtom = RWMolecule::PersistentAtomType(anchorAtom); |
| 426 |
if (!m_clickedAtom.isValid() || !m_anchorAtom.isValid()) |
| 427 |
return nullptr; |
| 428 |
e->accept(); |
| 429 |
m_moveState = RotateNeighborAtom; |
| 430 |
m_clickedPoint = e->pos(); |
| 431 |
m_lastDragPoint = e->pos(); |
| 432 |
resetFragment(); |
| 433 |
emit drawablesChanged(); |
| 434 |
return nullptr; |
| 435 |
} |
| 436 |
|
| 437 |
QUndoCommand* BondCentricTool::rotatePlane(QMouseEvent* e) |
| 438 |
{ |
| 439 |
// The bond should be valid. |
| 440 |
const RWBond selectedBond = m_selectedBond.bond(); |
| 441 |
if (!selectedBond.isValid()) |
| 442 |
return nullptr; |
| 443 |
|
| 444 |
const QPoint deltaDrag = e->pos() - m_lastDragPoint; |
| 445 |
const Rendering::Camera& camera(m_renderer->camera()); |
| 446 |
|
| 447 |
// Atomic position in world coordinates |
| 448 |
const Vector3 beginPos(selectedBond.atom1().position3d()); |
| 449 |
const Vector3 endPos(selectedBond.atom2().position3d()); |
| 450 |
|
| 451 |
// Various quantities in window coordinates. |
| 452 |
const Vector3f beginWin(camera.project(beginPos.cast<float>())); |
| 453 |
const Vector3f endWin(camera.project(endPos.cast<float>())); |
| 454 |
Vector3f bondVecWin(endWin - beginWin); |
| 455 |
bondVecWin.z() = 0.f; |
| 456 |
// Points into the viewing volume from camera: |
| 457 |
const Vector3f zAxisWin(0.f, 0.f, 1.f); |
| 458 |
// In plane of screen, orthogonal to bond: |
| 459 |
const Vector3f orthoWin(zAxisWin.cross(bondVecWin).normalized()); |
| 460 |
const Vector3f dragWin(static_cast<float>(deltaDrag.x()), |
| 461 |
static_cast<float>(deltaDrag.y()), 0.f); |
| 462 |
|
| 463 |
// Compute the rotation. Not quite sure what's going on here, this is just |
| 464 |
// ported from Avogadro 1. It doesn't seem right that rotation would be in |
| 465 |
// degrees (it's the result of a dot product) and I think the fact that the |
| 466 |
// DEG_TO_RAD conversion results in a useful angle is just a happy |
| 467 |
// coincidence. But it works quite well. |
| 468 |
const float rotation = dragWin.dot(orthoWin) / orthoWin.norm(); |
| 469 |
const Eigen::AngleAxisf rotator(rotation * DEG_TO_RAD_F, m_bondVector); |
| 470 |
|
| 471 |
// Rotate |
| 472 |
m_planeNormalMouse = rotator * m_planeNormalMouse; |
| 473 |
updateSnappedPlaneNormal(); |
| 474 |
emit drawablesChanged(); |
| 475 |
|
| 476 |
m_lastDragPoint = e->pos(); |
| 477 |
return nullptr; |
| 478 |
} |
| 479 |
|
| 480 |
QUndoCommand* BondCentricTool::rotateBondedAtom(QMouseEvent* e) |
| 481 |
{ |
| 482 |
// Ensure that the mouse has moved a reasonable amount: |
| 483 |
if ((m_lastDragPoint - e->pos()).manhattanLength() < 2) |
| 484 |
return nullptr; |
| 485 |
|
| 486 |
RWBond bond = m_selectedBond.bond(); |
| 487 |
RWAtom clickedAtom = m_clickedAtom.atom(); |
| 488 |
RWAtom centerAtom = bond.getOtherAtom(clickedAtom); |
| 489 |
|
| 490 |
// Sanity check: |
| 491 |
if (!bond.isValid() || !clickedAtom.isValid() || !centerAtom.isValid()) |
| 492 |
return nullptr; |
| 493 |
|
| 494 |
// Compute the transformation: |
| 495 |
// - Rotation axis is m_planeNormal |
| 496 |
// - Rotation angle is: |
| 497 |
// - magnitude is angle between initial click and current pos around |
| 498 |
// center atom (performed in 2D). |
| 499 |
// - sign is based on whether m_planeNormal is pointing into/out of the |
| 500 |
// screen. |
| 501 |
const Rendering::Camera& camera(m_renderer->camera()); |
| 502 |
|
| 503 |
// Get the window coordinates of the relevant points |
| 504 |
const Vector3f centerPos(centerAtom.position3d().cast<float>()); |
| 505 |
const Vector3f centerWin(camera.project(centerPos)); |
| 506 |
const Vector2f centerWin2(centerWin.head<2>()); |
| 507 |
const Vector2f lastDragWin( |
| 508 |
static_cast<float>(m_lastDragPoint.x()), |
| 509 |
static_cast<float>(camera.height() - m_lastDragPoint.y())); |
| 510 |
const Vector2f dragWin(static_cast<float>(e->pos().x()), |
| 511 |
static_cast<float>(camera.height() - e->pos().y())); |
| 512 |
|
| 513 |
// Compute the angle between last drag and current drag positions |
| 514 |
const Vector2f lastDragWinVec((lastDragWin - centerWin2).normalized()); |
| 515 |
const Vector2f dragWinVec((dragWin - centerWin2).normalized()); |
| 516 |
const float crossProductNorm(lastDragWinVec.x() * dragWinVec.y() - |
| 517 |
lastDragWinVec.y() * dragWinVec.x()); |
| 518 |
const float dotProduct(lastDragWinVec.dot(dragWinVec)); |
| 519 |
const float angle(std::atan2(crossProductNorm, dotProduct)); |
| 520 |
|
| 521 |
// Figure out if the sign needs to be reversed: |
| 522 |
const Vector3f centerPlusNormal(centerPos + m_planeNormal); |
| 523 |
const Vector3f centerPlusNormalWin(camera.project(centerPlusNormal)); |
| 524 |
bool reverseSign = (centerPlusNormalWin.z() - centerWin.z()) >= 0; |
| 525 |
|
| 526 |
// Build transform |
| 527 |
m_transform.setIdentity(); |
| 528 |
m_transform.translate(centerPos); |
| 529 |
m_transform.rotate( |
| 530 |
Eigen::AngleAxisf(reverseSign ? -angle : angle, m_planeNormal)); |
| 531 |
m_transform.translate(-centerPos); |
| 532 |
|
| 533 |
// Build the fragment if needed: |
| 534 |
if (m_fragment.empty()) |
| 535 |
buildFragment(bond, clickedAtom); |
| 536 |
|
| 537 |
// Perform transformation |
| 538 |
transformFragment(); |
| 539 |
updateBondVector(); |
| 540 |
m_molecule->emitChanged(Molecule::Modified | Molecule::Atoms); |
| 541 |
emit drawablesChanged(); |
| 542 |
|
| 543 |
m_lastDragPoint = e->pos(); |
| 544 |
return nullptr; |
| 545 |
} |
| 546 |
|
| 547 |
QUndoCommand* BondCentricTool::adjustBondLength(QMouseEvent* e) |
| 548 |
{ |
| 549 |
// Ensure that the mouse has moved a reasonable amount: |
| 550 |
if ((m_lastDragPoint - e->pos()).manhattanLength() < 2) |
| 551 |
return nullptr; |
| 552 |
|
| 553 |
RWBond selectedBond = m_selectedBond.bond(); |
| 554 |
RWAtom clickedAtom = m_clickedAtom.atom(); |
| 555 |
|
| 556 |
// Sanity check: |
| 557 |
if (!selectedBond.isValid() || !clickedAtom.isValid()) |
| 558 |
return nullptr; |
| 559 |
|
| 560 |
const Rendering::Camera& camera(m_renderer->camera()); |
| 561 |
RWAtom otherAtom = selectedBond.getOtherAtom(clickedAtom); |
| 562 |
|
| 563 |
const Vector2f curPosWin(static_cast<float>(e->pos().x()), |
| 564 |
static_cast<float>(e->pos().y())); |
| 565 |
const Vector2f lastPosWin(static_cast<float>(m_lastDragPoint.x()), |
| 566 |
static_cast<float>(m_lastDragPoint.y())); |
| 567 |
|
| 568 |
const Vector3f bond(clickedAtom.position3d().cast<float>() - |
| 569 |
otherAtom.position3d().cast<float>()); |
| 570 |
const Vector3f mouse(camera.unProject(curPosWin) - |
| 571 |
camera.unProject(lastPosWin)); |
| 572 |
|
| 573 |
const Vector3f displacement((mouse.dot(bond) / bond.squaredNorm()) * bond); |
| 574 |
|
| 575 |
// Build transform |
| 576 |
m_transform.setIdentity(); |
| 577 |
m_transform.translate(displacement); |
| 578 |
|
| 579 |
// Build the fragment if needed: |
| 580 |
if (m_fragment.empty()) |
| 581 |
buildFragment(selectedBond, clickedAtom); |
| 582 |
|
| 583 |
// Perform transformation |
| 584 |
transformFragment(); |
| 585 |
m_molecule->emitChanged(QtGui::Molecule::Modified | QtGui::Molecule::Atoms); |
| 586 |
emit drawablesChanged(); |
| 587 |
|
| 588 |
m_lastDragPoint = e->pos(); |
| 589 |
return nullptr; |
| 590 |
} |
| 591 |
|
| 592 |
QUndoCommand* BondCentricTool::rotateNeighborAtom(QMouseEvent* e) |
| 593 |
{ |
| 594 |
// Ensure that the mouse has moved a reasonable amount: |
| 595 |
if ((m_lastDragPoint - e->pos()).manhattanLength() < 2) |
| 596 |
return nullptr; |
| 597 |
|
| 598 |
RWBond selectedBond = m_selectedBond.bond(); |
| 599 |
// Atom that was clicked |
| 600 |
RWAtom clickedAtom = m_clickedAtom.atom(); |
| 601 |
// Atom in selected bond also attached to clickedAtom |
| 602 |
RWAtom anchorAtom = m_anchorAtom.atom(); |
| 603 |
// The "other" atom in selected bond |
| 604 |
RWAtom otherAtom = selectedBond.getOtherAtom(anchorAtom); |
| 605 |
|
| 606 |
// Sanity check: |
| 607 |
if (!selectedBond.isValid() || !anchorAtom.isValid() || |
| 608 |
!otherAtom.isValid() || !clickedAtom.isValid()) { |
| 609 |
return nullptr; |
| 610 |
} |
| 611 |
|
| 612 |
const Rendering::Camera& camera(m_renderer->camera()); |
| 613 |
|
| 614 |
// Compute the angle between last drag and current drag positions |
| 615 |
const Vector3f center(anchorAtom.position3d().cast<float>()); |
| 616 |
const Vector3f centerProj(camera.project(center)); |
| 617 |
const Vector2f centerWin(centerProj.head<2>()); |
| 618 |
const Vector2f curWin(static_cast<float>(e->pos().x()), |
| 619 |
static_cast<float>(camera.height() - e->pos().y())); |
| 620 |
const Vector2f lastWin( |
| 621 |
static_cast<float>(m_lastDragPoint.x()), |
| 622 |
static_cast<float>(camera.height() - m_lastDragPoint.y())); |
| 623 |
const Vector2f curVecWin((curWin - centerWin).normalized()); |
| 624 |
const Vector2f lastVecWin((lastWin - centerWin).normalized()); |
| 625 |
const float crossProductNorm(lastVecWin.x() * curVecWin.y() - |
| 626 |
lastVecWin.y() * curVecWin.x()); |
| 627 |
const float dotProduct(lastVecWin.dot(curVecWin)); |
| 628 |
const float angle(std::atan2(crossProductNorm, dotProduct)); |
| 629 |
|
| 630 |
// Figure out if the sign needs to be reversed: |
| 631 |
const Vector3f other(otherAtom.position3d().cast<float>()); |
| 632 |
const Vector3f otherProj(camera.project(other)); |
| 633 |
const bool reverseSign = otherProj.z() <= centerProj.z(); |
| 634 |
|
| 635 |
// Axis of rotation |
| 636 |
const Vector3f axis((center - other).normalized()); |
| 637 |
|
| 638 |
// Build transform |
| 639 |
m_transform.setIdentity(); |
| 640 |
m_transform.translate(center); |
| 641 |
m_transform.rotate(Eigen::AngleAxisf(reverseSign ? -angle : angle, axis)); |
| 642 |
m_transform.translate(-center); |
| 643 |
|
| 644 |
// Build the fragment if needed: |
| 645 |
if (m_fragment.empty()) |
| 646 |
buildFragment(selectedBond, anchorAtom); |
| 647 |
|
| 648 |
// Perform transformation |
| 649 |
transformFragment(); |
| 650 |
updateBondVector(); |
| 651 |
m_molecule->emitChanged(QtGui::Molecule::Modified | QtGui::Molecule::Atoms); |
| 652 |
emit drawablesChanged(); |
| 653 |
|
| 654 |
m_lastDragPoint = e->pos(); |
| 655 |
|
| 656 |
return nullptr; |
| 657 |
} |
| 658 |
|
| 659 |
void BondCentricTool::drawBondQuad(Rendering::GeometryNode& node, |
| 660 |
const RWBond& bond) const |
| 661 |
{ |
| 662 |
const Vector3f atom1Pos(bond.atom1().position3d().cast<float>()); |
| 663 |
const Vector3f atom2Pos(bond.atom2().position3d().cast<float>()); |
| 664 |
Vector3f offset(m_bondVector.cross(m_planeNormal)); |
| 665 |
|
| 666 |
const Vector3f v1(atom1Pos + offset); |
| 667 |
const Vector3f v2(atom2Pos + offset); |
| 668 |
const Vector3f v3(atom1Pos - offset); |
| 669 |
const Vector3f v4(atom2Pos - offset); |
| 670 |
|
| 671 |
Quad* quad = new Quad; |
| 672 |
node.addDrawable(quad); |
| 673 |
quad->setColor(Vector3ub(63, 127, 255)); |
| 674 |
quad->setOpacity(127); |
| 675 |
quad->setRenderPass(Rendering::TranslucentPass); |
| 676 |
quad->setQuad(v1, v2, v3, v4); |
| 677 |
|
| 678 |
auto* quadOutline = new QuadOutline; |
| 679 |
node.addDrawable(quadOutline); |
| 680 |
quadOutline->setColor(Vector3ub(63, 127, 255)); |
| 681 |
quadOutline->setRenderPass(Rendering::OpaquePass); |
| 682 |
quadOutline->setQuad(v1, v2, v3, v4, 1.f); |
| 683 |
|
| 684 |
// If the plane is rotating, show a hint for the unsnapped plane. |
| 685 |
if (m_moveState == RotatePlane) { |
| 686 |
Vector3f moffset(m_bondVector.cross(m_planeNormalMouse)); |
| 687 |
|
| 688 |
const Vector3f mv1(atom1Pos + moffset); |
| 689 |
const Vector3f mv2(atom2Pos + moffset); |
| 690 |
const Vector3f mv3(atom1Pos - moffset); |
| 691 |
const Vector3f mv4(atom2Pos - moffset); |
| 692 |
|
| 693 |
auto* mouseQuadOutline = new QuadOutline; |
| 694 |
node.addDrawable(mouseQuadOutline); |
| 695 |
mouseQuadOutline->setColor(Vector3ub(255, 255, 255)); |
| 696 |
mouseQuadOutline->setOpacity(127); |
| 697 |
mouseQuadOutline->setRenderPass(Rendering::TranslucentPass); |
| 698 |
mouseQuadOutline->setQuad(mv1, mv2, mv3, mv4, 1.f); |
| 699 |
} |
| 700 |
} |
| 701 |
|
| 702 |
void BondCentricTool::drawBondAngle(Rendering::GeometryNode& node, |
| 703 |
const QtGui::RWBond& selectedBond, |
| 704 |
const QtGui::RWBond& movingBond) const |
| 705 |
{ |
| 706 |
// Draw the selected bond quad as usual |
| 707 |
drawBondQuad(node, selectedBond); |
| 708 |
|
| 709 |
// Determine the atom shared between the bonds (atom1). |
| 710 |
RWAtom atom1; |
| 711 |
RWAtom atom2; |
| 712 |
if (selectedBond.atom1() == movingBond.atom1() || |
| 713 |
selectedBond.atom2() == movingBond.atom1()) { |
| 714 |
atom1 = movingBond.atom1(); |
| 715 |
atom2 = movingBond.atom2(); |
| 716 |
} else if (selectedBond.atom1() == movingBond.atom2() || |
| 717 |
selectedBond.atom2() == movingBond.atom2()) { |
| 718 |
atom1 = movingBond.atom2(); |
| 719 |
atom2 = movingBond.atom1(); |
| 720 |
} |
| 721 |
|
| 722 |
if (!atom1.isValid()) |
| 723 |
return; |
| 724 |
|
| 725 |
// Add another quad in the plane normal to |
| 726 |
// m_bondVector.cross(movingBondVector) |
| 727 |
const Vector3f a1(atom1.position3d().cast<float>()); |
| 728 |
const Vector3f a2(atom2.position3d().cast<float>()); |
| 729 |
const Vector3f movingBondVector(a2 - a1); |
| 730 |
const Vector3f movingBondUnitVector(movingBondVector.normalized()); |
| 731 |
// calculate a vector in the plane spanned by movingBondVector and |
| 732 |
// m_bondVector that is orthogonal to m_bondVector, then project |
| 733 |
// movingBondVector onto it. This is used to calculate the 'new' a2. |
| 734 |
const Vector3f movingBondNormal(m_bondVector.cross(movingBondUnitVector)); |
| 735 |
const Vector3f newA2Direction(movingBondNormal.cross(m_bondVector)); |
| 736 |
const Vector3f movingBondVectorProj(movingBondVector.dot(newA2Direction) * |
| 737 |
newA2Direction); |
| 738 |
const Vector3f newA2(a1 + movingBondVectorProj); |
| 739 |
const Vector3f& movingBondOffset(m_bondVector); |
| 740 |
const Vector3f v1(a1 + movingBondOffset); |
| 741 |
const Vector3f v2(newA2 + movingBondOffset); |
| 742 |
const Vector3f v3(a1 - movingBondOffset); |
| 743 |
const Vector3f v4(newA2 - movingBondOffset); |
| 744 |
|
| 745 |
Quad* quad = new Quad; |
| 746 |
node.addDrawable(quad); |
| 747 |
quad->setColor(Vector3ub(63, 127, 255)); |
| 748 |
quad->setOpacity(127); |
| 749 |
quad->setRenderPass(Rendering::TranslucentPass); |
| 750 |
quad->setQuad(v1, v2, v3, v4); |
| 751 |
|
| 752 |
auto* quadOutline = new QuadOutline; |
| 753 |
node.addDrawable(quadOutline); |
| 754 |
quadOutline->setColor(Vector3ub(63, 127, 255)); |
| 755 |
quadOutline->setRenderPass(Rendering::OpaquePass); |
| 756 |
quadOutline->setQuad(v1, v2, v3, v4, 1.f); |
| 757 |
|
| 758 |
// Add an arc and label to show a bit more info: |
| 759 |
const Vector3f selectedBondOffset(m_planeNormal.cross(m_bondVector)); |
| 760 |
const float radius(movingBondVector.norm() * 0.75f); |
| 761 |
Vector3f startEdge(newA2Direction * radius); |
| 762 |
Vector3f normal(m_bondVector); |
| 763 |
float angle = vectorAngleDegrees(startEdge, selectedBondOffset, normal); |
| 764 |
float displayAngle = std::fabs(angle); |
| 765 |
|
| 766 |
auto* sect = new ArcSector; |
| 767 |
node.addDrawable(sect); |
| 768 |
sect->setColor(Vector3ub(255, 127, 63)); |
| 769 |
sect->setOpacity(127); |
| 770 |
sect->setRenderPass(Rendering::TranslucentPass); |
| 771 |
sect->setArcSector(a1, startEdge, normal, angle, 5.f); |
| 772 |
|
| 773 |
auto* arc = new ArcStrip; |
| 774 |
node.addDrawable(arc); |
| 775 |
arc->setColor(Vector3ub(255, 127, 63)); |
| 776 |
arc->setRenderPass(Rendering::OpaquePass); |
| 777 |
arc->setArc(a1, startEdge, normal, angle, 5.f, 1.f); |
| 778 |
|
| 779 |
const Vector3f& textPos(a1); |
| 780 |
|
| 781 |
auto* label = new Rendering::TextLabel3D; |
| 782 |
label->setText(QString("%L1°").arg(displayAngle, 5, 'f', 1).toStdString()); |
| 783 |
label->setRenderPass(Rendering::Overlay3DPass); |
| 784 |
label->setAnchor(textPos); |
| 785 |
node.addDrawable(label); |
| 786 |
|
| 787 |
Rendering::TextProperties tprop; |
| 788 |
tprop.setAlign(Rendering::TextProperties::HCenter, |
| 789 |
Rendering::TextProperties::VCenter); |
| 790 |
tprop.setFontFamily(Rendering::TextProperties::SansSerif); |
| 791 |
tprop.setColorRgb(255, 200, 64); |
| 792 |
label->setTextProperties(tprop); |
| 793 |
} |
| 794 |
|
| 795 |
void BondCentricTool::drawBondLengthLabel(Rendering::GeometryNode& node, |
| 796 |
const QtGui::RWBond& bond) |
| 797 |
{ |
| 798 |
const Vector3f startPos(bond.atom1().position3d().cast<float>()); |
| 799 |
const Vector3f endPos(bond.atom2().position3d().cast<float>()); |
| 800 |
const Vector3f bondCenter((startPos + endPos) * 0.5f); |
| 801 |
const Vector3f bondVector(endPos - startPos); |
| 802 |
|
| 803 |
auto* label = new Rendering::TextLabel3D; |
| 804 |
label->setText( |
| 805 |
QString("%L1 Å").arg(bondVector.norm(), 4, 'f', 2).toStdString()); |
| 806 |
label->setRenderPass(Rendering::Overlay3DPass); |
| 807 |
label->setAnchor(bondCenter); |
| 808 |
node.addDrawable(label); |
| 809 |
|
| 810 |
Rendering::TextProperties tprop; |
| 811 |
tprop.setAlign(Rendering::TextProperties::HCenter, |
| 812 |
Rendering::TextProperties::VCenter); |
| 813 |
tprop.setFontFamily(Rendering::TextProperties::SansSerif); |
| 814 |
tprop.setColorRgb(255, 200, 64); |
| 815 |
label->setTextProperties(tprop); |
| 816 |
} |
| 817 |
|
| 818 |
void BondCentricTool::drawAtomBondAngles(Rendering::GeometryNode& node, |
| 819 |
const RWAtom& atom, |
| 820 |
const RWBond& anchorBond) |
| 821 |
{ |
| 822 |
const Array<RWBond> bonds = m_molecule->bonds(atom); |
| 823 |
auto bondIter(bonds.begin()); |
| 824 |
auto bondEnd(bonds.end()); |
| 825 |
size_t count = 0; |
| 826 |
while (bondIter != bondEnd) { |
| 827 |
if (*bondIter != anchorBond) |
| 828 |
drawAtomBondAngle(node, atom, anchorBond, *bondIter, getColor(count++)); |
| 829 |
++bondIter; |
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
void BondCentricTool::drawAtomBondAngle(Rendering::GeometryNode& node, |
| 834 |
const QtGui::RWAtom& atom, |
| 835 |
const QtGui::RWBond& anchorBond, |
| 836 |
const QtGui::RWBond& otherBond, |
| 837 |
const Vector3ub& color) |
| 838 |
{ |
| 839 |
const RWAtom otherAtom = otherBond.getOtherAtom(atom); |
| 840 |
const RWAtom otherAnchorAtom = anchorBond.getOtherAtom(atom); |
| 841 |
|
| 842 |
const Vector3f atomPos(atom.position3d().cast<float>()); |
| 843 |
const Vector3f otherAtomPos(otherAtom.position3d().cast<float>()); |
| 844 |
const Vector3f otherAnchorAtomPos(otherAnchorAtom.position3d().cast<float>()); |
| 845 |
|
| 846 |
const Vector3f otherVector(otherAtomPos - atomPos); |
| 847 |
const Vector3f anchorVector(otherAnchorAtomPos - atomPos); |
| 848 |
const Vector3f anchorUnitVector(anchorVector.normalized()); |
| 849 |
|
| 850 |
const float radius(otherVector.norm() * 0.75f); |
| 851 |
const Vector3f& origin(atomPos); |
| 852 |
const Vector3f start(anchorUnitVector * radius); |
| 853 |
const Vector3f axis(anchorVector.cross(otherVector).normalized()); |
| 854 |
const float angle = vectorAngleDegrees(otherVector, anchorVector); |
| 855 |
const Vector3f& labelPos(otherAtomPos); |
| 856 |
|
| 857 |
auto* sect = new ArcSector; |
| 858 |
node.addDrawable(sect); |
| 859 |
sect->setColor(color); |
| 860 |
sect->setOpacity(127); |
| 861 |
sect->setRenderPass(Rendering::TranslucentPass); |
| 862 |
sect->setArcSector(origin, start, axis, angle, 5.f); |
| 863 |
|
| 864 |
auto* arc = new ArcStrip; |
| 865 |
node.addDrawable(arc); |
| 866 |
arc->setColor(color); |
| 867 |
arc->setRenderPass(Rendering::OpaquePass); |
| 868 |
arc->setArc(origin, start, axis, angle, 5.f, 1.f); |
| 869 |
|
| 870 |
auto* label = new Rendering::TextLabel3D; |
| 871 |
label->setText(QString("%L1°").arg(angle, 6, 'f', 1).toStdString()); |
| 872 |
label->setRenderPass(Rendering::Overlay3DPass); |
| 873 |
label->setAnchor(labelPos); |
| 874 |
node.addDrawable(label); |
| 875 |
|
| 876 |
Rendering::TextProperties tprop; |
| 877 |
tprop.setAlign(Rendering::TextProperties::HCenter, |
| 878 |
Rendering::TextProperties::VCenter); |
| 879 |
tprop.setFontFamily(Rendering::TextProperties::SansSerif); |
| 880 |
tprop.setColorRgb(color); |
| 881 |
label->setTextProperties(tprop); |
| 882 |
} |
| 883 |
|
| 884 |
inline bool BondCentricTool::bondContainsAtom(const QtGui::RWBond& bond, |
| 885 |
const QtGui::RWAtom& atom) const |
| 886 |
{ |
| 887 |
return atom == bond.atom1() || atom == bond.atom2(); |
| 888 |
} |
| 889 |
|
| 890 |
inline void BondCentricTool::transformFragment() const |
| 891 |
{ |
| 892 |
// Convert the internal float matrix to use the same precision as the atomic |
| 893 |
// coordinates. |
| 894 |
Eigen::Transform<Real, 3, Eigen::Affine> transform(m_transform.cast<Real>()); |
| 895 |
for (int it : m_fragment) { |
| 896 |
RWAtom atom = m_molecule->atomByUniqueId(it); |
| 897 |
if (atom.isValid()) { |
| 898 |
Vector3 pos = atom.position3d(); |
| 899 |
pos = transform * pos; |
| 900 |
atom.setPosition3d(pos); |
| 901 |
} |
| 902 |
} |
| 903 |
} |
| 904 |
|
| 905 |
void BondCentricTool::updatePlaneSnapAngles() |
| 906 |
{ |
| 907 |
m_planeSnapRef = m_bondVector.unitOrthogonal(); |
| 908 |
m_planeSnapAngles.clear(); |
| 909 |
|
| 910 |
// Add bond angles if requested: |
| 911 |
RWBond selectedBond = m_selectedBond.bond(); |
| 912 |
if (m_snapPlaneToBonds && selectedBond.isValid()) { |
| 913 |
const RWAtom atom1 = selectedBond.atom1(); |
| 914 |
const RWAtom atom2 = selectedBond.atom2(); |
| 915 |
for (int i = 0; i < 2; ++i) { |
| 916 |
const RWAtom& atom = i == 0 ? atom1 : atom2; |
| 917 |
const Vector3f atomPos(atom.position3d().cast<float>()); |
| 918 |
const Array<RWBond> bonds = m_molecule->bonds(atom); |
| 919 |
for (auto bond : bonds) { |
| 920 |
if (bond != selectedBond) { |
| 921 |
const RWAtom otherAtom(bond.getOtherAtom(atom)); |
| 922 |
const Vector3f otherAtomPos(otherAtom.position3d().cast<float>()); |
| 923 |
const Vector3f otherBondVector(otherAtomPos - atomPos); |
| 924 |
// Project otherBondVector into the plane normal to m_bondVector |
| 925 |
// (e.g. the rejection of otherBondVector onto m_bondVector) |
| 926 |
const Vector3f rej( |
| 927 |
otherBondVector - |
| 928 |
(otherBondVector.dot(m_bondVector) * m_bondVector)); |
| 929 |
float angle(vectorAngleDegrees(m_planeSnapRef, rej, m_bondVector)); |
| 930 |
m_planeSnapAngles.insert(angle); |
| 931 |
angle += 180.f; |
| 932 |
if (angle > 180.f) |
| 933 |
angle -= 360.f; |
| 934 |
m_planeSnapAngles.insert(angle); |
| 935 |
} |
| 936 |
} |
| 937 |
} |
| 938 |
} |
| 939 |
|
| 940 |
// Add default increments only if they are more than 5 degrees away |
| 941 |
// from a bond angle. |
| 942 |
const float minDist(5.f); |
| 943 |
for (float angle = -180.f; angle < 180.f; angle += m_planeSnapIncr) { |
| 944 |
auto upper(m_planeSnapAngles.lower_bound(angle)); |
| 945 |
if (upper != m_planeSnapAngles.end()) { |
| 946 |
if (*upper - minDist < angle) |
| 947 |
continue; |
| 948 |
if (upper != m_planeSnapAngles.begin()) { |
| 949 |
auto lower(upper); |
| 950 |
std::advance(lower, -1); |
| 951 |
if (*lower + minDist > angle) |
| 952 |
continue; |
| 953 |
} |
| 954 |
m_planeSnapAngles.insert(angle); |
| 955 |
} |
| 956 |
} |
| 957 |
} |
| 958 |
|
| 959 |
// There may be some weirdness around +/-180 since we don't check for |
| 960 |
// wrapping, but it should be fine for this use case. |
| 961 |
void BondCentricTool::updateSnappedPlaneNormal() |
| 962 |
{ |
| 963 |
const Vector3f mousePlaneVector(m_planeNormalMouse.cross(m_bondVector)); |
| 964 |
const float angle( |
| 965 |
vectorAngleDegrees(m_planeSnapRef, mousePlaneVector, m_bondVector)); |
| 966 |
float snappedAngle(angle); |
| 967 |
auto upper(m_planeSnapAngles.lower_bound(angle)); |
| 968 |
if (upper != m_planeSnapAngles.end()) { |
| 969 |
if (upper != m_planeSnapAngles.begin()) { |
| 970 |
auto lower(upper); |
| 971 |
std::advance(lower, -1); |
| 972 |
float upperDist = std::fabs(angle - *upper); |
| 973 |
float lowerDist = std::fabs(angle - *lower); |
| 974 |
snappedAngle = upperDist < lowerDist ? *upper : *lower; |
| 975 |
} else { |
| 976 |
snappedAngle = *upper; |
| 977 |
} |
| 978 |
} |
| 979 |
|
| 980 |
if (angle == snappedAngle) { |
| 981 |
// If the angle didn't change, keep on keepin' on: |
| 982 |
m_planeNormal = m_planeNormalMouse; |
| 983 |
} else { |
| 984 |
// Otherwise, update the vector. |
| 985 |
const Vector3f planeVector = |
| 986 |
Eigen::AngleAxisf(snappedAngle * DEG_TO_RAD_F, m_bondVector) * |
| 987 |
m_planeSnapRef; |
| 988 |
m_planeNormal = planeVector.cross(m_bondVector); |
| 989 |
} |
| 990 |
} |
| 991 |
|
| 992 |
inline bool BondCentricTool::fragmentHasAtom(int uid) const |
| 993 |
{ |
| 994 |
return std::find(m_fragment.begin(), m_fragment.end(), uid) != |
| 995 |
m_fragment.end(); |
| 996 |
} |
| 997 |
|
| 998 |
void BondCentricTool::buildFragment(const QtGui::RWBond& bond, |
| 999 |
const QtGui::RWAtom& startAtom) |
| 1000 |
{ |
| 1001 |
m_fragment.clear(); |
| 1002 |
if (!buildFragmentRecurse(bond, startAtom, startAtom)) { |
| 1003 |
// If this returns false, then a cycle has been found. Only move startAtom |
| 1004 |
// in this case. |
| 1005 |
m_fragment.clear(); |
| 1006 |
} |
| 1007 |
m_fragment.push_back(m_molecule->atomUniqueId(startAtom)); |
| 1008 |
} |
| 1009 |
|
| 1010 |
bool BondCentricTool::buildFragmentRecurse(const QtGui::RWBond& bond, |
| 1011 |
const QtGui::RWAtom& startAtom, |
| 1012 |
const QtGui::RWAtom& currentAtom) |
| 1013 |
{ |
| 1014 |
// does our cycle include both bonded atoms? |
| 1015 |
const RWAtom bondedAtom(bond.getOtherAtom(startAtom)); |
| 1016 |
|
| 1017 |
Array<RWBond> bonds = m_molecule->bonds(currentAtom); |
| 1018 |
|
| 1019 |
for (auto& it : bonds) { |
| 1020 |
if (it != bond) { // Skip the current bond |
| 1021 |
const RWAtom nextAtom = it.getOtherAtom(currentAtom); |
| 1022 |
if (nextAtom != startAtom && nextAtom != bondedAtom) { |
| 1023 |
// Skip atoms that have already been added. This prevents infinite |
| 1024 |
// recursion on cycles in the fragments |
| 1025 |
int uid = m_molecule->atomUniqueId(nextAtom); |
| 1026 |
if (!fragmentHasAtom(uid)) { |
| 1027 |
m_fragment.push_back(uid); |
| 1028 |
if (!buildFragmentRecurse(it, startAtom, nextAtom)) |
| 1029 |
return false; |
| 1030 |
} |
| 1031 |
} else if (nextAtom == bondedAtom) { |
| 1032 |
// If we've found the bonded atom, the bond is in a cycle |
| 1033 |
return false; |
| 1034 |
} |
| 1035 |
} // *it != bond |
| 1036 |
} // foreach bond |
| 1037 |
return true; |
| 1038 |
} |
| 1039 |
|
| 1040 |
} // namespace Avogadro::QtPlugins |