| 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 "cartoons.h" |
| 7 |
|
| 8 |
#include <QtCore/QSettings> |
| 9 |
#include <QtWidgets/QCheckBox> |
| 10 |
#include <QtWidgets/QVBoxLayout> |
| 11 |
#include <QtWidgets/QWidget> |
| 12 |
#include <avogadro/core/molecule.h> |
| 13 |
#include <avogadro/core/residue.h> |
| 14 |
#include <avogadro/qtgui/rwmolecule.h> |
| 15 |
#include <avogadro/rendering/beziergeometry.h> |
| 16 |
#include <avogadro/rendering/bsplinegeometry.h> |
| 17 |
#include <avogadro/rendering/cartoongeometry.h> |
| 18 |
#include <avogadro/rendering/cylindergeometry.h> |
| 19 |
#include <avogadro/rendering/geometrynode.h> |
| 20 |
#include <avogadro/rendering/groupnode.h> |
| 21 |
#include <avogadro/rendering/spheregeometry.h> |
| 22 |
|
| 23 |
#include <functional> |
| 24 |
#include <utility> |
| 25 |
#include <vector> |
| 26 |
|
| 27 |
namespace Avogadro::QtPlugins { |
| 28 |
|
| 29 |
using Core::Atom; |
| 30 |
using Core::AtomicNumber; |
| 31 |
using Core::Residue; |
| 32 |
using QtGui::Molecule; |
| 33 |
using QtGui::PluginLayerManager; |
| 34 |
using Rendering::BezierGeometry; |
| 35 |
using Rendering::BSplineGeometry; |
| 36 |
using Rendering::Cartoon; |
| 37 |
using Rendering::CylinderGeometry; |
| 38 |
using Rendering::GeometryNode; |
| 39 |
using Rendering::SphereGeometry; |
| 40 |
using std::list; |
| 41 |
using std::map; |
| 42 |
using std::reference_wrapper; |
| 43 |
using std::vector; |
| 44 |
|
| 45 |
struct LayerCartoon : Core::LayerData |
| 46 |
{ |
| 47 |
QWidget* widget; |
| 48 |
bool showBackbone; |
| 49 |
bool showTrace; |
| 50 |
bool showTube; |
| 51 |
bool showRibbon; |
| 52 |
bool showSimpleCartoon; |
| 53 |
bool showCartoon; |
| 54 |
bool showRope; |
| 55 |
|
| 56 |
using JumpTable = void (Cartoons::*)(bool); |
| 57 |
JumpTable jumpTable[7]; |
| 58 |
|
| 59 |
std::string serialize() final |
| 60 |
{ |
| 61 |
return boolToString(showBackbone) + " " + boolToString(showTrace) + " " + |
| 62 |
boolToString(showTube) + " " + boolToString(showRibbon) + " " + |
| 63 |
boolToString(showSimpleCartoon) + " " + boolToString(showCartoon) + |
| 64 |
" " + boolToString(showRope); |
| 65 |
} |
| 66 |
void deserialize(std::string text) final |
| 67 |
{ |
| 68 |
std::stringstream ss(text); |
| 69 |
std::string aux; |
| 70 |
ss >> aux; |
| 71 |
showBackbone = stringToBool(aux); |
| 72 |
ss >> aux; |
| 73 |
showTrace = stringToBool(aux); |
| 74 |
ss >> aux; |
| 75 |
showTube = stringToBool(aux); |
| 76 |
ss >> aux; |
| 77 |
showRibbon = stringToBool(aux); |
| 78 |
ss >> aux; |
| 79 |
showSimpleCartoon = stringToBool(aux); |
| 80 |
ss >> aux; |
| 81 |
showCartoon = stringToBool(aux); |
| 82 |
ss >> aux; |
| 83 |
showRope = stringToBool(aux); |
| 84 |
} |
| 85 |
|
| 86 |
void setupWidget(Cartoons* slot) |
| 87 |
{ |
| 88 |
if (!widget) { |
| 89 |
widget = new QWidget(qobject_cast<QWidget*>(slot->parent())); |
| 90 |
auto* v = new QVBoxLayout; |
| 91 |
QStringList boxesText; |
| 92 |
boxesText << QObject::tr("Backbone", "protein rendering style") |
| 93 |
<< QObject::tr("Trace", "protein rendering style") |
| 94 |
<< QObject::tr("Tube", "protein rendering style") |
| 95 |
<< QObject::tr("Ribbon", "protein rendering style") |
| 96 |
<< QObject::tr("Simple Cartoon", "protein rendering style") |
| 97 |
<< QObject::tr("Cartoon", "protein rendering style") |
| 98 |
<< QObject::tr("Rope", "protein rendering style"); |
| 99 |
vector<reference_wrapper<bool>> boxesBools = { |
| 100 |
showBackbone, showTrace, showTube, showRibbon, |
| 101 |
showSimpleCartoon, showCartoon, showRope |
| 102 |
}; |
| 103 |
jumpTable[0] = &Cartoons::showBackbone; |
| 104 |
jumpTable[1] = &Cartoons::showTrace; |
| 105 |
jumpTable[2] = &Cartoons::showTube; |
| 106 |
jumpTable[3] = &Cartoons::showRibbon; |
| 107 |
jumpTable[4] = &Cartoons::showSimpleCartoon; |
| 108 |
jumpTable[5] = &Cartoons::showCartoon; |
| 109 |
jumpTable[6] = &Cartoons::showRope; |
| 110 |
for (int i = 0; i < boxesText.size(); ++i) { |
| 111 |
auto* check = new QCheckBox(boxesText[i]); |
| 112 |
check->setChecked(boxesBools[i]); |
| 113 |
QObject::connect(check, &QCheckBox::clicked, slot, jumpTable[i]); |
| 114 |
v->addWidget(check); |
| 115 |
} |
| 116 |
// make sure there's empty space at the bottom,s otherwise the |
| 117 |
v->addStretch(1); |
| 118 |
widget->setLayout(v); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
LayerCartoon() : widget(nullptr) |
| 123 |
{ |
| 124 |
QSettings settings; |
| 125 |
showBackbone = settings.value("cartoon/backbone", false).toBool(); |
| 126 |
showCartoon = settings.value("cartoon/cartoon", true).toBool(); |
| 127 |
showTrace = settings.value("cartoon/trace", false).toBool(); |
| 128 |
showTube = settings.value("cartoon/tube", false).toBool(); |
| 129 |
showRibbon = settings.value("cartoon/ribbon", false).toBool(); |
| 130 |
showRope = settings.value("cartoon/rope", false).toBool(); |
| 131 |
showSimpleCartoon = settings.value("cartoon/simplecartoon", false).toBool(); |
| 132 |
} |
| 133 |
|
| 134 |
LayerCartoon(std::string settings) |
| 135 |
{ |
| 136 |
widget = nullptr; |
| 137 |
deserialize(settings); |
| 138 |
} |
| 139 |
|
| 140 |
~LayerCartoon() override |
| 141 |
{ |
| 142 |
if (widget) |
| 143 |
widget->deleteLater(); |
| 144 |
} |
| 145 |
|
| 146 |
LayerData* clone() final { return new LayerCartoon(serialize()); } |
| 147 |
}; |
| 148 |
|
| 149 |
struct BackboneResidue |
| 150 |
{ |
| 151 |
BackboneResidue() = default; |
| 152 |
BackboneResidue(const Vector3f p, const Vector3ub& c1, const Vector3ub& c2, |
| 153 |
const size_t& g, size_t id, bool sel, |
| 154 |
Residue::SecondaryStructure sec) |
| 155 |
: pos(p), color1(c1), color2(c2), group(g), residueID(id), selected(sel), |
| 156 |
secondaryStructure(sec) |
| 157 |
|
| 158 |
{ |
| 159 |
} |
| 160 |
Vector3f pos; |
| 161 |
Vector3ub color1; |
| 162 |
Vector3ub color2; |
| 163 |
size_t group; |
| 164 |
size_t residueID; |
| 165 |
bool selected; |
| 166 |
Residue::SecondaryStructure secondaryStructure; |
| 167 |
}; |
| 168 |
|
| 169 |
using AtomsPairList = list<BackboneResidue>; |
| 170 |
|
| 171 |
Cartoons::Cartoons(QObject* parent) : ScenePlugin(parent), m_group(nullptr) |
| 172 |
{ |
| 173 |
m_layerManager = PluginLayerManager(m_name); |
| 174 |
} |
| 175 |
|
| 176 |
void addBackBone(map<size_t, AtomsPairList>& result, |
| 177 |
map<size_t, BackboneResidue>& previousCA, const Atom& caAtom, |
| 178 |
const Vector3ub& color, Index group, |
| 179 |
Residue::SecondaryStructure sec) |
| 180 |
{ |
| 181 |
Vector3ub color1; |
| 182 |
if (result.find(group) == result.end()) { |
| 183 |
result[group] = AtomsPairList(); |
| 184 |
color1 = Vector3ub(0, 0, 0); |
| 185 |
} else { |
| 186 |
color1 = previousCA[group].color2; |
| 187 |
} |
| 188 |
|
| 189 |
Vector3f ca = caAtom.position3d().cast<float>(); |
| 190 |
BackboneResidue backBone = BackboneResidue( |
| 191 |
ca, color1, color, group, caAtom.index(), caAtom.selected(), sec); |
| 192 |
// the 1ยบ insertion will always be incompleated, so fix it |
| 193 |
if (result[group].size() == 1) { |
| 194 |
result[group].front().color1 = color1; |
| 195 |
} |
| 196 |
previousCA[group] = backBone; |
| 197 |
result[group].push_back(backBone); |
| 198 |
} |
| 199 |
|
| 200 |
map<size_t, AtomsPairList> Cartoons::getBackboneByResidues( |
| 201 |
const Molecule& molecule, size_t layer) |
| 202 |
{ |
| 203 |
const auto& graph = molecule.graph(); |
| 204 |
map<size_t, AtomsPairList> result; |
| 205 |
map<size_t, BackboneResidue> previousAtom; |
| 206 |
for (const auto& residue : molecule.residues()) { |
| 207 |
if (!residue.isHeterogen()) { |
| 208 |
Atom caAtom = residue.atomByName("CA"); |
| 209 |
Atom oAtom = residue.atomByName("O"); |
| 210 |
if (caAtom.isValid() && oAtom.isValid() && |
| 211 |
m_layerManager.atomEnabled(layer, caAtom.index()) && |
| 212 |
m_layerManager.atomEnabled(layer, oAtom.index())) { |
| 213 |
// get the group ID and check if it's initialized in the map |
| 214 |
size_t group = graph.getConnectedID(caAtom.index()); |
| 215 |
addBackBone(result, previousAtom, caAtom, residue.color(), group, |
| 216 |
residue.secondaryStructure()); |
| 217 |
} else { // maybe DNA |
| 218 |
Atom c3Atom = residue.atomByName("C3'"); |
| 219 |
Atom o3Atom = residue.atomByName("O3'"); |
| 220 |
if (c3Atom.isValid() && o3Atom.isValid() && |
| 221 |
m_layerManager.atomEnabled(layer, c3Atom.index()) && |
| 222 |
m_layerManager.atomEnabled(layer, o3Atom.index())) { |
| 223 |
size_t group = graph.getConnectedID(c3Atom.index()); |
| 224 |
addBackBone(result, previousAtom, c3Atom, residue.color(), group, |
| 225 |
residue.secondaryStructure()); |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
return result; |
| 231 |
} |
| 232 |
|
| 233 |
map<size_t, AtomsPairList> Cartoons::getBackboneManually( |
| 234 |
const Molecule& molecule, size_t layer) |
| 235 |
{ |
| 236 |
// manual filter |
| 237 |
const auto& graph = molecule.graph(); |
| 238 |
map<size_t, AtomsPairList> result; |
| 239 |
map<size_t, BackboneResidue> previousCA; |
| 240 |
|
| 241 |
for (size_t i = 0; i < molecule.atomCount(); ++i) { |
| 242 |
const auto atom = molecule.atom(i); |
| 243 |
if (atom.atomicNumber() == AtomicNumber::Carbon && |
| 244 |
m_layerManager.atomEnabled(layer, atom.index())) { |
| 245 |
size_t group = graph.getConnectedID(atom.index()); |
| 246 |
addBackBone(result, previousCA, atom, atom.color(), group, |
| 247 |
Residue::SecondaryStructure::undefined); |
| 248 |
} |
| 249 |
} |
| 250 |
return result; |
| 251 |
} |
| 252 |
|
| 253 |
void renderBackbone(const AtomsPairList& backbone, const Molecule& molecule, |
| 254 |
Rendering::GroupNode& node, float radius) |
| 255 |
{ |
| 256 |
auto* geometry = new GeometryNode; |
| 257 |
node.addChild(geometry); |
| 258 |
|
| 259 |
auto* spheres = new SphereGeometry; |
| 260 |
spheres->identifier().molecule = reinterpret_cast<const void*>(&molecule); |
| 261 |
spheres->identifier().type = Rendering::AtomType; |
| 262 |
geometry->addDrawable(spheres); |
| 263 |
|
| 264 |
auto* cylinders = new CylinderGeometry; |
| 265 |
cylinders->identifier().molecule = &molecule; |
| 266 |
cylinders->identifier().type = Rendering::AtomType; |
| 267 |
geometry->addDrawable(cylinders); |
| 268 |
|
| 269 |
Index i = 0; |
| 270 |
for (auto it = backbone.begin(); it != backbone.end(); ++it) { |
| 271 |
const auto& bone = *it; |
| 272 |
auto color1 = bone.color1; |
| 273 |
auto color2 = bone.color2; |
| 274 |
if (bone.selected) { |
| 275 |
color1 += Vector3ub(155, 0, 0); |
| 276 |
color2 += Vector3ub(155, 0, 0); |
| 277 |
} |
| 278 |
const Vector3f& pos = bone.pos; |
| 279 |
spheres->addSphere(pos, color1, radius, bone.residueID); |
| 280 |
if (std::next(it) != backbone.end()) { |
| 281 |
const auto& nextBone = *std::next(it); |
| 282 |
const Vector3f& pos2 = nextBone.pos; |
| 283 |
cylinders->addCylinder(pos, pos2, radius, color1, color2, bone.residueID); |
| 284 |
} |
| 285 |
++i; |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
void renderRope(const AtomsPairList& backbone, const Molecule& molecule, |
| 290 |
Rendering::GroupNode& node, float radius) |
| 291 |
{ |
| 292 |
auto* geometry = new GeometryNode; |
| 293 |
node.addChild(geometry); |
| 294 |
|
| 295 |
auto* bezier = new BezierGeometry; |
| 296 |
bezier->identifier().molecule = &molecule; |
| 297 |
bezier->identifier().type = Rendering::AtomType; |
| 298 |
geometry->addDrawable(bezier); |
| 299 |
|
| 300 |
Vector3ub color = Vector3ub::Zero(); |
| 301 |
for (const auto& bone : backbone) { |
| 302 |
bezier->addPoint(bone.pos, bone.color1 + color, radius, bone.group, |
| 303 |
bone.residueID); |
| 304 |
color = bone.selected ? Vector3ub(155, 0, 0) : Vector3ub::Zero(); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
void renderTube(const AtomsPairList& backbone, const Molecule& molecule, |
| 309 |
Rendering::GroupNode& node, float radius) |
| 310 |
{ |
| 311 |
auto* geometry = new GeometryNode; |
| 312 |
node.addChild(geometry); |
| 313 |
|
| 314 |
auto* bezier = new BSplineGeometry; |
| 315 |
bezier->identifier().molecule = &molecule; |
| 316 |
bezier->identifier().type = Rendering::AtomType; |
| 317 |
geometry->addDrawable(bezier); |
| 318 |
|
| 319 |
Vector3ub color = Vector3ub::Zero(); |
| 320 |
for (const auto& bone : backbone) { |
| 321 |
bezier->addPoint(bone.pos, bone.color1 + color, radius, bone.group, |
| 322 |
bone.residueID); |
| 323 |
color = bone.selected ? Vector3ub(155, 0, 0) : Vector3ub::Zero(); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
void renderSimpleCartoon(const AtomsPairList& backbone, |
| 328 |
const Molecule& molecule, Rendering::GroupNode& node, |
| 329 |
float radius) |
| 330 |
{ |
| 331 |
auto* geometry = new GeometryNode; |
| 332 |
node.addChild(geometry); |
| 333 |
|
| 334 |
auto* cartoon = new Cartoon; |
| 335 |
cartoon->identifier().molecule = &molecule; |
| 336 |
cartoon->identifier().type = Rendering::AtomType; |
| 337 |
geometry->addDrawable(cartoon); |
| 338 |
Vector3ub color = Vector3ub::Zero(); |
| 339 |
for (const auto& bone : backbone) { |
| 340 |
cartoon->CurveGeometry::addPoint(bone.pos, bone.color1 + color, radius, |
| 341 |
bone.group, bone.residueID); |
| 342 |
color = bone.selected ? Vector3ub(155, 0, 0) : Vector3ub::Zero(); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
void renderCartoon(const AtomsPairList& backbone, const Molecule& molecule, |
| 347 |
Rendering::GroupNode& node, float radius) |
| 348 |
{ |
| 349 |
auto* geometry = new GeometryNode; |
| 350 |
node.addChild(geometry); |
| 351 |
|
| 352 |
auto* cartoon = new Cartoon(radius * 0.2f, radius * 1.5f); |
| 353 |
cartoon->identifier().molecule = &molecule; |
| 354 |
cartoon->identifier().type = Rendering::AtomType; |
| 355 |
geometry->addDrawable(cartoon); |
| 356 |
Vector3ub color = Vector3ub::Zero(); |
| 357 |
for (const auto& bone : backbone) { |
| 358 |
cartoon->addPoint(bone.pos, bone.color1 + color, bone.group, bone.residueID, |
| 359 |
bone.secondaryStructure); |
| 360 |
color = bone.selected ? Vector3ub(155, 0, 0) : Vector3ub::Zero(); |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
void Cartoons::process(const Molecule& molecule, Rendering::GroupNode& node) |
| 365 |
{ |
| 366 |
m_layerManager.load<LayerCartoon>(); |
| 367 |
m_group = &node; |
| 368 |
for (size_t layer = 0; layer < m_layerManager.layerCount(); ++layer) { |
| 369 |
auto* interface = m_layerManager.getSetting<LayerCartoon>(layer); |
| 370 |
if (interface->showBackbone || interface->showTrace || |
| 371 |
interface->showTube || interface->showRibbon || |
| 372 |
interface->showSimpleCartoon || interface->showCartoon || |
| 373 |
interface->showRope) { |
| 374 |
map<size_t, AtomsPairList> backbones; |
| 375 |
if (molecule.residues().size() > 0) { |
| 376 |
backbones = getBackboneByResidues(molecule, layer); |
| 377 |
} |
| 378 |
if (backbones.size() == 0) { |
| 379 |
continue; // maybe something in a different layer |
| 380 |
} |
| 381 |
size_t i = 0; |
| 382 |
for (const auto& group : backbones) { |
| 383 |
const auto& backbone = group.second; |
| 384 |
if (interface->showBackbone) { |
| 385 |
renderBackbone(backbone, molecule, node, 0.1f); |
| 386 |
} |
| 387 |
if (interface->showTrace) { |
| 388 |
renderTube(backbone, molecule, node, -0.15f); |
| 389 |
} |
| 390 |
if (interface->showTube) { |
| 391 |
renderTube(backbone, molecule, node, 0.15f); |
| 392 |
} |
| 393 |
if (interface->showRibbon) { |
| 394 |
renderCartoon(backbone, molecule, node, |
| 395 |
-1.0f * Cartoon::ELIPSE_RATIO); |
| 396 |
} |
| 397 |
if (interface->showSimpleCartoon) { |
| 398 |
renderSimpleCartoon(backbone, molecule, node, 1.0f); |
| 399 |
} |
| 400 |
if (interface->showCartoon) { |
| 401 |
renderCartoon(backbone, molecule, node, 1.0f); |
| 402 |
} |
| 403 |
if (interface->showRope) { |
| 404 |
renderRope(backbone, molecule, node, 1.0f); |
| 405 |
} |
| 406 |
++i; |
| 407 |
} |
| 408 |
} |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
QWidget* Cartoons::setupWidget() |
| 413 |
{ |
| 414 |
auto* interface = m_layerManager.getSetting<LayerCartoon>(); |
| 415 |
interface->setupWidget(this); |
| 416 |
return interface->widget; |
| 417 |
} |
| 418 |
|
| 419 |
void Cartoons::showBackbone(bool show) |
| 420 |
{ |
| 421 |
auto* interface = m_layerManager.getSetting<LayerCartoon>(); |
| 422 |
if (show != interface->showBackbone) { |
| 423 |
interface->showBackbone = show; |
| 424 |
emit drawablesChanged(); |
| 425 |
} |
| 426 |
QSettings settings; |
| 427 |
settings.setValue("cartoon/backbone", show); |
| 428 |
} |
| 429 |
|
| 430 |
void Cartoons::showTrace(bool show) |
| 431 |
{ |
| 432 |
auto* interface = m_layerManager.getSetting<LayerCartoon>(); |
| 433 |
if (show != interface->showTrace) { |
| 434 |
interface->showTrace = show; |
| 435 |
emit drawablesChanged(); |
| 436 |
} |
| 437 |
QSettings settings; |
| 438 |
settings.setValue("cartoon/trace", show); |
| 439 |
} |
| 440 |
|
| 441 |
void Cartoons::showTube(bool show) |
| 442 |
{ |
| 443 |
auto* interface = m_layerManager.getSetting<LayerCartoon>(); |
| 444 |
if (show != interface->showTube) { |
| 445 |
interface->showTube = show; |
| 446 |
emit drawablesChanged(); |
| 447 |
} |
| 448 |
QSettings settings; |
| 449 |
settings.setValue("cartoon/tube", show); |
| 450 |
} |
| 451 |
|
| 452 |
void Cartoons::showRibbon(bool show) |
| 453 |
{ |
| 454 |
auto* interface = m_layerManager.getSetting<LayerCartoon>(); |
| 455 |
if (show != interface->showRibbon) { |
| 456 |
interface->showRibbon = show; |
| 457 |
emit drawablesChanged(); |
| 458 |
} |
| 459 |
QSettings settings; |
| 460 |
settings.setValue("cartoon/ribbon", show); |
| 461 |
} |
| 462 |
|
| 463 |
void Cartoons::showSimpleCartoon(bool show) |
| 464 |
{ |
| 465 |
auto* interface = m_layerManager.getSetting<LayerCartoon>(); |
| 466 |
if (show != interface->showSimpleCartoon) { |
| 467 |
interface->showSimpleCartoon = show; |
| 468 |
emit drawablesChanged(); |
| 469 |
} |
| 470 |
QSettings settings; |
| 471 |
settings.setValue("cartoon/simpleCartoon", show); |
| 472 |
} |
| 473 |
|
| 474 |
void Cartoons::showCartoon(bool show) |
| 475 |
{ |
| 476 |
auto* interface = m_layerManager.getSetting<LayerCartoon>(); |
| 477 |
if (show != interface->showCartoon) { |
| 478 |
interface->showCartoon = show; |
| 479 |
emit drawablesChanged(); |
| 480 |
} |
| 481 |
QSettings settings; |
| 482 |
settings.setValue("cartoon/cartoon", show); |
| 483 |
} |
| 484 |
|
| 485 |
void Cartoons::showRope(bool show) |
| 486 |
{ |
| 487 |
auto* interface = m_layerManager.getSetting<LayerCartoon>(); |
| 488 |
if (show != interface->showRope) { |
| 489 |
interface->showRope = show; |
| 490 |
emit drawablesChanged(); |
| 491 |
} |
| 492 |
QSettings settings; |
| 493 |
settings.setValue("cartoon/rope", show); |
| 494 |
} |
| 495 |
|
| 496 |
} // namespace Avogadro::QtPlugins |