| 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 "aligntool.h" |
| 7 |
|
| 8 |
#include <avogadro/core/contrastcolor.h> |
| 9 |
#include <avogadro/core/vector.h> |
| 10 |
|
| 11 |
#include <avogadro/qtgui/molecule.h> |
| 12 |
#include <avogadro/qtgui/rwmolecule.h> |
| 13 |
|
| 14 |
#include <avogadro/qtopengl/glwidget.h> |
| 15 |
|
| 16 |
#include <avogadro/rendering/camera.h> |
| 17 |
#include <avogadro/rendering/geometrynode.h> |
| 18 |
#include <avogadro/rendering/glrenderer.h> |
| 19 |
#include <avogadro/rendering/groupnode.h> |
| 20 |
#include <avogadro/rendering/scene.h> |
| 21 |
#include <avogadro/rendering/textlabel2d.h> |
| 22 |
#include <avogadro/rendering/textlabel3d.h> |
| 23 |
#include <avogadro/rendering/textproperties.h> |
| 24 |
|
| 25 |
#include <QAction> |
| 26 |
#include <QtCore/QDebug> |
| 27 |
#include <QtGui/QIcon> |
| 28 |
#include <QtGui/QKeyEvent> |
| 29 |
#include <QtGui/QMouseEvent> |
| 30 |
#include <QtGui/QWheelEvent> |
| 31 |
#include <QtWidgets/QComboBox> |
| 32 |
#include <QtWidgets/QGridLayout> |
| 33 |
#include <QtWidgets/QHBoxLayout> |
| 34 |
#include <QtWidgets/QLabel> |
| 35 |
#include <QtWidgets/QPushButton> |
| 36 |
|
| 37 |
using Avogadro::Core::contrastColor; |
| 38 |
using Avogadro::Core::Elements; |
| 39 |
using Avogadro::Rendering::GeometryNode; |
| 40 |
using Avogadro::Rendering::Identifier; |
| 41 |
using Avogadro::Rendering::TextLabel3D; |
| 42 |
using Avogadro::Rendering::TextProperties; |
| 43 |
|
| 44 |
namespace Avogadro::QtPlugins { |
| 45 |
|
| 46 |
AlignTool::AlignTool(QObject* parent_) |
| 47 |
: QtGui::ToolPlugin(parent_), m_activateAction(new QAction(this)), |
| 48 |
m_molecule(nullptr), m_renderer(nullptr), m_axis(0), m_alignType(0), |
| 49 |
m_toolWidget(nullptr) |
| 50 |
{ |
| 51 |
m_activateAction->setText(tr("Align")); |
| 52 |
m_activateAction->setToolTip( |
| 53 |
tr("Align Molecules\n\n" |
| 54 |
"Left Mouse:\tSelect up to two atoms.\n" |
| 55 |
"\tThe first atom is centered at the origin.\n" |
| 56 |
"\tThe second atom is aligned to the selected axis.\n" |
| 57 |
"Right Mouse:\tReset alignment.\n" |
| 58 |
"Double-Click:\tCenter the atom at the origin.")); |
| 59 |
setIcon(); |
| 60 |
} |
| 61 |
|
| 62 |
AlignTool::~AlignTool() |
| 63 |
{ |
| 64 |
if (m_toolWidget) |
| 65 |
m_toolWidget->deleteLater(); |
| 66 |
} |
| 67 |
|
| 68 |
void AlignTool::setIcon(bool darkTheme) |
| 69 |
{ |
| 70 |
if (darkTheme) |
| 71 |
m_activateAction->setIcon(QIcon(":/icons/align_dark.svg")); |
| 72 |
else |
| 73 |
m_activateAction->setIcon(QIcon(":/icons/align_light.svg")); |
| 74 |
} |
| 75 |
|
| 76 |
QWidget* AlignTool::toolWidget() const |
| 77 |
{ |
| 78 |
if (!m_toolWidget) { |
| 79 |
m_toolWidget = new QWidget; |
| 80 |
|
| 81 |
auto* labelAxis = new QLabel(tr("Axis:"), m_toolWidget); |
| 82 |
labelAxis->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); |
| 83 |
labelAxis->setMaximumHeight(15); |
| 84 |
|
| 85 |
// Combo box to select desired aixs to align to |
| 86 |
auto* comboAxis = new QComboBox(m_toolWidget); |
| 87 |
comboAxis->addItem("x"); |
| 88 |
comboAxis->addItem("y"); |
| 89 |
comboAxis->addItem("z"); |
| 90 |
comboAxis->setCurrentIndex(m_axis); |
| 91 |
|
| 92 |
// Button to actually perform actions |
| 93 |
auto* buttonAlign = new QPushButton(m_toolWidget); |
| 94 |
buttonAlign->setText(tr("Align")); |
| 95 |
connect(buttonAlign, SIGNAL(clicked()), this, SLOT(align())); |
| 96 |
|
| 97 |
auto* gridLayout = new QGridLayout(); |
| 98 |
gridLayout->addWidget(labelAxis, 0, 0, 1, 1, Qt::AlignRight); |
| 99 |
auto* hLayout = new QHBoxLayout; |
| 100 |
hLayout->addWidget(comboAxis); |
| 101 |
hLayout->addStretch(1); |
| 102 |
gridLayout->addLayout(hLayout, 0, 1); |
| 103 |
|
| 104 |
auto* hLayout3 = new QHBoxLayout(); |
| 105 |
hLayout3->addStretch(1); |
| 106 |
hLayout3->addWidget(buttonAlign); |
| 107 |
hLayout3->addStretch(1); |
| 108 |
auto* layout = new QVBoxLayout(); |
| 109 |
layout->addLayout(gridLayout); |
| 110 |
layout->addLayout(hLayout3); |
| 111 |
layout->addStretch(1); |
| 112 |
m_toolWidget->setLayout(layout); |
| 113 |
|
| 114 |
connect(comboAxis, SIGNAL(currentIndexChanged(int)), this, |
| 115 |
SLOT(axisChanged(int))); |
| 116 |
|
| 117 |
connect(m_toolWidget, SIGNAL(destroyed()), this, |
| 118 |
SLOT(toolWidgetDestroyed())); |
| 119 |
} |
| 120 |
|
| 121 |
return m_toolWidget; |
| 122 |
} |
| 123 |
|
| 124 |
void AlignTool::axisChanged(int axis) |
| 125 |
{ |
| 126 |
// Axis to use - x=0, y=1, z=2 |
| 127 |
m_axis = axis; |
| 128 |
} |
| 129 |
|
| 130 |
void AlignTool::alignChanged(int align) |
| 131 |
{ |
| 132 |
// Type of alignment - 0=everything, 1=molecule |
| 133 |
m_alignType = align; |
| 134 |
} |
| 135 |
|
| 136 |
void AlignTool::align() |
| 137 |
{ |
| 138 |
if (m_atoms.size() == 0) |
| 139 |
return; |
| 140 |
|
| 141 |
if (m_atoms.size() >= 1) |
| 142 |
shiftAtomToOrigin(m_atoms[0].index); |
| 143 |
if (m_atoms.size() == 2) |
| 144 |
alignAtomToAxis(m_atoms[1].index, m_axis); |
| 145 |
} |
| 146 |
|
| 147 |
void AlignTool::shiftAtomToOrigin(Index atomIndex) |
| 148 |
{ |
| 149 |
// Shift the atom to the origin |
| 150 |
Vector3 shift = m_molecule->atom(atomIndex).position3d(); |
| 151 |
const Core::Array<Vector3>& coords = m_molecule->atomPositions3d(); |
| 152 |
Core::Array<Vector3> newCoords(coords.size()); |
| 153 |
for (Index i = 0; i < coords.size(); ++i) |
| 154 |
newCoords[i] = coords[i] - shift; |
| 155 |
|
| 156 |
m_molecule->setAtomPositions3d(newCoords, tr("Align at Origin")); |
| 157 |
m_molecule->emitChanged(QtGui::Molecule::Atoms); |
| 158 |
} |
| 159 |
|
| 160 |
void AlignTool::alignAtomToAxis(Index atomIndex, int axis) |
| 161 |
{ |
| 162 |
// Align the atom to the specified axis |
| 163 |
[[maybe_unused]] Vector3 align = m_molecule->atom(atomIndex).position3d(); |
| 164 |
const Core::Array<Vector3>& coords = m_molecule->atomPositions3d(); |
| 165 |
Core::Array<Vector3> newCoords(coords.size()); |
| 166 |
|
| 167 |
[[maybe_unused]] double alpha; |
| 168 |
double beta, gamma; |
| 169 |
alpha = beta = gamma = 0.0; |
| 170 |
|
| 171 |
Vector3 pos = m_molecule->atom(atomIndex).position3d(); |
| 172 |
pos.normalize(); |
| 173 |
Vector3 axisVector; |
| 174 |
|
| 175 |
if (axis == 0) // x-axis |
| 176 |
axisVector = Vector3(1., 0., 0.); |
| 177 |
else if (axis == 1) // y-axis |
| 178 |
axisVector = Vector3(0., 1., 0.); |
| 179 |
else if (axis == 2) // z-axis |
| 180 |
axisVector = Vector3(0., 0., 1.); |
| 181 |
|
| 182 |
// Calculate the angle of the atom from the axis |
| 183 |
double angle = acos(axisVector.dot(pos)); |
| 184 |
|
| 185 |
// Get the vector for the rotation |
| 186 |
axisVector = axisVector.cross(pos); |
| 187 |
axisVector.normalize(); |
| 188 |
|
| 189 |
// Now to rotate the fragment |
| 190 |
for (Index i = 0; i < coords.size(); ++i) |
| 191 |
newCoords[i] = Eigen::AngleAxisd(-angle, axisVector) * coords[i]; |
| 192 |
|
| 193 |
m_molecule->setAtomPositions3d(newCoords, tr("Align to Axis")); |
| 194 |
m_molecule->emitChanged(QtGui::Molecule::Atoms); |
| 195 |
} |
| 196 |
|
| 197 |
void AlignTool::toolWidgetDestroyed() |
| 198 |
{ |
| 199 |
m_toolWidget = nullptr; |
| 200 |
} |
| 201 |
|
| 202 |
QUndoCommand* AlignTool::mousePressEvent(QMouseEvent* e) |
| 203 |
{ |
| 204 |
// If the click is released on an atom, add it to the list |
| 205 |
if (e->button() != Qt::LeftButton || !m_renderer) |
| 206 |
return nullptr; |
| 207 |
|
| 208 |
Identifier hit = m_renderer->hit(e->pos().x(), e->pos().y()); |
| 209 |
|
| 210 |
// Now add the atom on release. |
| 211 |
if (hit.type == Rendering::AtomType) { |
| 212 |
if (toggleAtom(hit)) |
| 213 |
emit drawablesChanged(); |
| 214 |
e->accept(); |
| 215 |
} |
| 216 |
|
| 217 |
return nullptr; |
| 218 |
} |
| 219 |
|
| 220 |
QUndoCommand* AlignTool::mouseDoubleClickEvent(QMouseEvent* e) |
| 221 |
{ |
| 222 |
// Reset the atom list |
| 223 |
if (e->button() == Qt::LeftButton && !m_atoms.isEmpty()) { |
| 224 |
m_atoms.clear(); |
| 225 |
emit drawablesChanged(); |
| 226 |
e->accept(); |
| 227 |
} |
| 228 |
return nullptr; |
| 229 |
} |
| 230 |
|
| 231 |
bool AlignTool::toggleAtom(const Rendering::Identifier& atom) |
| 232 |
{ |
| 233 |
int ind = m_atoms.indexOf(atom); |
| 234 |
if (ind >= 0) { |
| 235 |
m_atoms.remove(ind); |
| 236 |
return true; |
| 237 |
} |
| 238 |
|
| 239 |
if (m_atoms.size() >= 2) |
| 240 |
return false; |
| 241 |
|
| 242 |
m_atoms.push_back(atom); |
| 243 |
return true; |
| 244 |
} |
| 245 |
|
| 246 |
void AlignTool::draw(Rendering::GroupNode& node) |
| 247 |
{ |
| 248 |
if (m_atoms.size() == 0) |
| 249 |
return; |
| 250 |
|
| 251 |
// check to make sure we have atoms for all of these |
| 252 |
for (int i = 0; i < m_atoms.size(); ++i) { |
| 253 |
Identifier& ident = m_atoms[i]; |
| 254 |
if (ident.type == Rendering::AtomType && |
| 255 |
ident.index >= m_molecule->atomCount()) |
| 256 |
return; |
| 257 |
} |
| 258 |
|
| 259 |
auto* geo = new GeometryNode; |
| 260 |
node.addChild(geo); |
| 261 |
|
| 262 |
// Add labels, extract positions |
| 263 |
QVector<Vector3> positions(m_atoms.size(), Vector3()); |
| 264 |
|
| 265 |
TextProperties atomLabelProp; |
| 266 |
atomLabelProp.setFontFamily(TextProperties::SansSerif); |
| 267 |
atomLabelProp.setAlign(TextProperties::HCenter, TextProperties::VCenter); |
| 268 |
|
| 269 |
for (int i = 0; i < m_atoms.size(); ++i) { |
| 270 |
Identifier& ident = m_atoms[i]; |
| 271 |
Q_ASSERT(ident.type == Rendering::AtomType); |
| 272 |
Q_ASSERT(ident.molecule != nullptr); |
| 273 |
|
| 274 |
auto atom = m_molecule->atom(ident.index); |
| 275 |
Q_ASSERT(atom.isValid()); |
| 276 |
unsigned char atomicNumber(atom.atomicNumber()); |
| 277 |
positions[i] = atom.position3d(); |
| 278 |
|
| 279 |
// get the color of the atom |
| 280 |
const unsigned char* color = Elements::color(atomicNumber); |
| 281 |
atomLabelProp.setColorRgb(contrastColor(Vector3ub(color)).data()); |
| 282 |
|
| 283 |
auto* label = new TextLabel3D; |
| 284 |
label->setText(QString("#%1").arg(i + 1).toStdString()); |
| 285 |
label->setTextProperties(atomLabelProp); |
| 286 |
label->setAnchor(positions[i].cast<float>()); |
| 287 |
label->setRadius( |
| 288 |
static_cast<float>(Elements::radiusCovalent(atomicNumber)) + 0.1f); |
| 289 |
geo->addDrawable(label); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
void AlignTool::registerCommands() |
| 294 |
{ |
| 295 |
emit registerCommand("centerAtom", tr("Center the atom at the origin.")); |
| 296 |
emit registerCommand( |
| 297 |
"alignAtom", |
| 298 |
tr("Rotate the molecule to align the atom to the specified axis.")); |
| 299 |
} |
| 300 |
|
| 301 |
bool AlignTool::handleCommand(const QString& command, |
| 302 |
const QVariantMap& options) |
| 303 |
{ |
| 304 |
if (m_molecule == nullptr) |
| 305 |
return false; // No molecule to handle the command. |
| 306 |
|
| 307 |
if (command == "centerAtom") { |
| 308 |
if (options.contains("id")) { |
| 309 |
Index atomIndex = options["id"].toInt(); |
| 310 |
if (atomIndex < m_molecule->atomCount()) |
| 311 |
shiftAtomToOrigin(atomIndex); |
| 312 |
return true; |
| 313 |
} else if (options.contains("index")) { |
| 314 |
Index atomIndex = options["index"].toInt(); |
| 315 |
if (atomIndex < m_molecule->atomCount()) |
| 316 |
shiftAtomToOrigin(atomIndex); |
| 317 |
return true; |
| 318 |
} |
| 319 |
return false; |
| 320 |
} else if (command == "alignAtom") { |
| 321 |
int axis = -1; |
| 322 |
if (options.contains("axis") && options["axis"].type() == QVariant::Int) { |
| 323 |
axis = options["axis"].toInt(); |
| 324 |
} else if (options.contains("axis") && |
| 325 |
options["axis"].type() == QVariant::String) { |
| 326 |
QString axisString = options["axis"].toString(); |
| 327 |
if (axisString == "x") |
| 328 |
axis = 0; |
| 329 |
else if (axisString == "y") |
| 330 |
axis = 1; |
| 331 |
else if (axisString == "z") |
| 332 |
axis = 2; |
| 333 |
} |
| 334 |
|
| 335 |
if (axis >= 0 && axis < 3) { |
| 336 |
if (options.contains("id")) { |
| 337 |
Index atomIndex = options["id"].toInt(); |
| 338 |
if (atomIndex < m_molecule->atomCount()) |
| 339 |
alignAtomToAxis(atomIndex, axis); |
| 340 |
return true; |
| 341 |
} else if (options.contains("index")) { |
| 342 |
Index atomIndex = options["index"].toInt(); |
| 343 |
if (atomIndex < m_molecule->atomCount()) |
| 344 |
alignAtomToAxis(atomIndex, axis); |
| 345 |
return true; |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
return false; // invalid options |
| 350 |
} |
| 351 |
|
| 352 |
return true; // nothing to handle |
| 353 |
} |
| 354 |
|
| 355 |
} // namespace Avogadro::QtPlugins |