| 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 "focus.h" |
| 7 |
|
| 8 |
#include <avogadro/qtgui/molecule.h> |
| 9 |
#include <avogadro/qtopengl/glwidget.h> |
| 10 |
#include <avogadro/rendering/camera.h> |
| 11 |
|
| 12 |
#include <QAction> |
| 13 |
|
| 14 |
namespace Avogadro::QtPlugins { |
| 15 |
|
| 16 |
using Avogadro::QtGui::ExtensionPlugin; |
| 17 |
|
| 18 |
Focus::Focus(QObject* parent_) |
| 19 |
: ExtensionPlugin(parent_), |
| 20 |
m_focusSelectionAction(new QAction(tr("Focus Selection"), this)), |
| 21 |
m_unfocusAction(new QAction(tr("Unfocus"), this)) |
| 22 |
{ |
| 23 |
m_focusSelectionAction->setProperty("menu priority", 200); |
| 24 |
m_unfocusAction->setProperty("menu priority", 200); |
| 25 |
|
| 26 |
connect(m_focusSelectionAction, SIGNAL(triggered()), SLOT(focusSelection())); |
| 27 |
connect(m_unfocusAction, SIGNAL(triggered()), SLOT(unfocus())); |
| 28 |
} |
| 29 |
|
| 30 |
Focus::~Focus() {} |
| 31 |
|
| 32 |
QList<QAction*> Focus::actions() const |
| 33 |
{ |
| 34 |
QList<QAction*> result; |
| 35 |
return result << m_focusSelectionAction << m_unfocusAction; |
| 36 |
} |
| 37 |
|
| 38 |
QStringList Focus::menuPath(QAction*) const |
| 39 |
{ |
| 40 |
return QStringList() << tr("&View"); |
| 41 |
} |
| 42 |
|
| 43 |
void Focus::setMolecule(QtGui::Molecule* mol) |
| 44 |
{ |
| 45 |
m_molecule = mol; |
| 46 |
} |
| 47 |
|
| 48 |
void Focus::setCamera(Rendering::Camera* camera) |
| 49 |
{ |
| 50 |
m_camera = camera; |
| 51 |
} |
| 52 |
|
| 53 |
void Focus::setScene(Rendering::Scene* scene) |
| 54 |
{ |
| 55 |
m_scene = scene; |
| 56 |
} |
| 57 |
|
| 58 |
void Focus::setActiveWidget(QWidget* widget) |
| 59 |
{ |
| 60 |
if (widget != nullptr) { |
| 61 |
m_glWidget = widget; |
| 62 |
connect(this, SIGNAL(updateRequested()), m_glWidget, SLOT(requestUpdate())); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
void Focus::newFocus(Eigen::Vector3f point, float distance) |
| 67 |
{ |
| 68 |
Eigen::Vector3f cameraPoint = -m_camera->modelView().translation(); |
| 69 |
Eigen::Vector3f vectorOfSight = point - cameraPoint; |
| 70 |
float currentDistance = vectorOfSight.norm(); |
| 71 |
float scaleFactor = distance / currentDistance; |
| 72 |
vectorOfSight *= 1.0f - scaleFactor; |
| 73 |
cameraPoint += vectorOfSight; |
| 74 |
m_camera->setIdentity(); |
| 75 |
m_camera->lookAt(cameraPoint, point, Eigen::Vector3f(0, 1, 0)); |
| 76 |
m_camera->setFocus(point); |
| 77 |
|
| 78 |
// if orthographic, adjust the scale |
| 79 |
if (m_camera->projectionType() == Rendering::Projection::Orthographic) { |
| 80 |
m_camera->setOrthographicScale(scaleFactor * 0.85f); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
void Focus::focusSelection() |
| 85 |
{ |
| 86 |
if (!m_molecule || !m_camera) |
| 87 |
return; |
| 88 |
if (m_molecule->atomPositions3d().size() != m_molecule->atomCount()) |
| 89 |
return; |
| 90 |
if (m_molecule->isSelectionEmpty()) |
| 91 |
return; |
| 92 |
|
| 93 |
Eigen::Vector3f selectionCenter(0, 0, 0); |
| 94 |
std::vector<Index> selection; |
| 95 |
for (Index i = 0; i < m_molecule->atomCount(); ++i) { |
| 96 |
if (m_molecule->atomSelected(i)) { |
| 97 |
selectionCenter += m_molecule->atomPosition3d(i).cast<float>(); |
| 98 |
selection.push_back(i); |
| 99 |
} |
| 100 |
} |
| 101 |
selectionCenter /= selection.size(); |
| 102 |
float selectionRadius = 0.0f; |
| 103 |
for (Index i : selection) { |
| 104 |
Eigen::Vector3f pos = m_molecule->atomPosition3d(i).cast<float>(); |
| 105 |
float distance = (pos - selectionCenter).norm(); |
| 106 |
if (distance > selectionRadius) |
| 107 |
selectionRadius = distance; |
| 108 |
} |
| 109 |
|
| 110 |
newFocus(selectionCenter, selectionRadius + 10.0f); |
| 111 |
emit updateRequested(); |
| 112 |
} |
| 113 |
|
| 114 |
void Focus::unfocus() |
| 115 |
{ |
| 116 |
if (!m_camera || !m_scene) |
| 117 |
return; |
| 118 |
|
| 119 |
// same as glrenderer resetCamera |
| 120 |
auto center = m_scene->center(); |
| 121 |
auto radius = 1.5f * m_scene->radius(); |
| 122 |
|
| 123 |
m_camera->setFocus(center); |
| 124 |
m_camera->setIdentity(); |
| 125 |
m_camera->translate(-center); |
| 126 |
m_camera->preTranslate(-radius * Vector3f::UnitZ()); |
| 127 |
|
| 128 |
if (m_camera->projectionType() == Rendering::Projection::Orthographic) { |
| 129 |
// We want the scene radius to fit comfortably with some padding |
| 130 |
float optimalScale = radius / 10.0f; |
| 131 |
m_camera->setOrthographicScale(optimalScale); |
| 132 |
} |
| 133 |
emit updateRequested(); |
| 134 |
} |
| 135 |
|
| 136 |
} // namespace Avogadro::QtPlugins |