| 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 "symmetryscene.h" |
| 7 |
|
| 8 |
#include <avogadro/qtgui/molecule.h> |
| 9 |
#include <avogadro/qtgui/rwmolecule.h> |
| 10 |
|
| 11 |
#include <avogadro/core/array.h> |
| 12 |
#include <avogadro/core/molecule.h> |
| 13 |
#include <avogadro/core/unitcell.h> |
| 14 |
#include <avogadro/rendering/arcsector.h> |
| 15 |
#include <avogadro/rendering/cylindergeometry.h> |
| 16 |
#include <avogadro/rendering/geometrynode.h> |
| 17 |
#include <avogadro/rendering/groupnode.h> |
| 18 |
#include <avogadro/rendering/meshgeometry.h> |
| 19 |
#include <avogadro/rendering/spheregeometry.h> |
| 20 |
|
| 21 |
#include <QtCore/QDebug> |
| 22 |
#include <QtCore/QVariant> |
| 23 |
|
| 24 |
#include <QVector3D> |
| 25 |
|
| 26 |
using namespace Avogadro; |
| 27 |
|
| 28 |
namespace Avogadro::QtPlugins { |
| 29 |
|
| 30 |
using Core::Array; |
| 31 |
using Rendering::ArcSector; |
| 32 |
using Rendering::CylinderGeometry; |
| 33 |
using Rendering::GeometryNode; |
| 34 |
using Rendering::GroupNode; |
| 35 |
using Rendering::MeshGeometry; |
| 36 |
using Rendering::SphereGeometry; |
| 37 |
|
| 38 |
SymmetryScene::SymmetryScene(QObject* p) |
| 39 |
: QtGui::ScenePlugin(p), m_enabled(true) |
| 40 |
{ |
| 41 |
} |
| 42 |
|
| 43 |
SymmetryScene::~SymmetryScene() {} |
| 44 |
|
| 45 |
void SymmetryScene::process(const QtGui::Molecule& molecule, |
| 46 |
Rendering::GroupNode& node) |
| 47 |
{ |
| 48 |
QVariant origo = molecule.property("SymmetryOrigo"); |
| 49 |
QVariant radius = molecule.property("SymmetryRadius"); |
| 50 |
QVariant inversion = molecule.property("SymmetryInversion"); |
| 51 |
QVariant properRotation = |
| 52 |
molecule.property("SymmetryProperRotationVariantList"); |
| 53 |
QVariant improperRotation = |
| 54 |
molecule.property("SymmetryImproperRotationVariantList"); |
| 55 |
QVariant reflection = molecule.property("SymmetryReflectionVariantList"); |
| 56 |
if (!origo.isValid() || !radius.isValid()) { |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
auto qorigo = origo.value<QVector3D>(); |
| 61 |
Vector3f forigo = Vector3f(qorigo.x(), qorigo.y(), qorigo.z()); |
| 62 |
float fradius = radius.toFloat(); |
| 63 |
|
| 64 |
auto* geometry = new GeometryNode; |
| 65 |
node.addChild(geometry); |
| 66 |
|
| 67 |
auto* spheres = new SphereGeometry; |
| 68 |
spheres->identifier().molecule = reinterpret_cast<const void*>(&molecule); |
| 69 |
spheres->identifier().type = Rendering::AtomType; |
| 70 |
geometry->addDrawable(spheres); |
| 71 |
|
| 72 |
auto* cylinders = new CylinderGeometry; |
| 73 |
cylinders->identifier().molecule = &molecule; |
| 74 |
cylinders->identifier().type = Rendering::BondType; |
| 75 |
geometry->addDrawable(cylinders); |
| 76 |
|
| 77 |
if (inversion.isValid()) { |
| 78 |
Vector3ub color(0, 0, 255); |
| 79 |
auto qvec = inversion.value<QVector3D>(); |
| 80 |
Vector3f fvec = Vector3f(qvec.x(), qvec.y(), qvec.z()); |
| 81 |
spheres->addSphere(fvec, color, 0.3f); |
| 82 |
} |
| 83 |
|
| 84 |
if (properRotation.isValid()) { |
| 85 |
Vector3ub color(255, 0, 0); |
| 86 |
QVariantList properRotationVariantList = properRotation.toList(); |
| 87 |
foreach (QVariant qv, properRotationVariantList) { |
| 88 |
auto qvec = qv.value<QVector3D>(); |
| 89 |
Vector3f fvec = Vector3f(qvec.x(), qvec.y(), qvec.z()); |
| 90 |
cylinders->addCylinder(forigo, forigo + 1.1 * fradius * fvec, 0.05f, |
| 91 |
color); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
if (improperRotation.isValid()) { |
| 96 |
QVariantList improperRotationVariantList = improperRotation.toList(); |
| 97 |
} |
| 98 |
|
| 99 |
if (reflection.isValid()) { |
| 100 |
Vector3ub color(255, 255, 0); |
| 101 |
QVariantList reflectionVariantList = reflection.toList(); |
| 102 |
|
| 103 |
foreach (QVariant qv, reflectionVariantList) { |
| 104 |
auto qvec = qv.value<QVector3D>(); |
| 105 |
// normal to the mirror plane |
| 106 |
Vector3f vecNormal = Vector3f(qvec.x(), qvec.y(), qvec.z()); |
| 107 |
|
| 108 |
// get an arbitrary vector in the plane, scaled by fradius |
| 109 |
Vector3f vecPlane; |
| 110 |
if (qvec.z() < qvec.x()) |
| 111 |
vecPlane = Vector3f(-qvec.y(), qvec.x(), 0); |
| 112 |
else |
| 113 |
vecPlane = Vector3f(0, -qvec.z(), qvec.y()); |
| 114 |
|
| 115 |
vecPlane = vecPlane.normalized() * fradius; |
| 116 |
|
| 117 |
auto* sect = new ArcSector; |
| 118 |
geometry->addDrawable(sect); |
| 119 |
sect->setColor(Vector3ub(color)); |
| 120 |
sect->setOpacity(127); // 50% |
| 121 |
sect->setRenderPass(Rendering::TranslucentPass); |
| 122 |
sect->setArcSector(forigo, vecPlane, vecNormal, 360.0f, 5.f); |
| 123 |
|
| 124 |
// cylinders->addCylinder(forigo - fvec * 0.025f, forigo + fvec * |
| 125 |
// 0.025f, fradius, color); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
void SymmetryScene::processEditable(const QtGui::RWMolecule& molecule, |
| 131 |
Rendering::GroupNode& node) |
| 132 |
{ |
| 133 |
process(molecule.molecule(), node); |
| 134 |
} |
| 135 |
|
| 136 |
bool SymmetryScene::isEnabled() const |
| 137 |
{ |
| 138 |
return m_enabled; |
| 139 |
} |
| 140 |
|
| 141 |
bool SymmetryScene::isActiveLayerEnabled() const |
| 142 |
{ |
| 143 |
return m_enabled; |
| 144 |
} |
| 145 |
|
| 146 |
void SymmetryScene::setEnabled(bool enable) |
| 147 |
{ |
| 148 |
m_enabled = enable; |
| 149 |
} |
| 150 |
} // namespace Avogadro::QtPlugins |