| 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 "surfaces.h" |
| 7 |
#include "surfacedialog.h" |
| 8 |
|
| 9 |
// Header only, but duplicate symbols if included globally... |
| 10 |
namespace { |
| 11 |
#include <gif.h> |
| 12 |
} |
| 13 |
|
| 14 |
#include <gwavi.h> |
| 15 |
|
| 16 |
#include <avogadro/calc/chargemanager.h> |
| 17 |
|
| 18 |
#include <avogadro/core/color3f.h> |
| 19 |
#include <avogadro/core/variant.h> |
| 20 |
#include <avogadro/core/vector.h> |
| 21 |
|
| 22 |
#include <avogadro/core/cube.h> |
| 23 |
#include <avogadro/core/mesh.h> |
| 24 |
#include <avogadro/core/neighborperceiver.h> |
| 25 |
|
| 26 |
#include <avogadro/qtgui/gaussiansetconcurrent.h> |
| 27 |
#include <avogadro/qtgui/meshgenerator.h> |
| 28 |
#include <avogadro/qtgui/molecule.h> |
| 29 |
#include <avogadro/qtgui/rwlayermanager.h> |
| 30 |
#include <avogadro/qtgui/slatersetconcurrent.h> |
| 31 |
#include <avogadro/qtgui/timedprogressdialog.h> |
| 32 |
|
| 33 |
#include <avogadro/qtopengl/activeobjects.h> |
| 34 |
#include <avogadro/qtopengl/glwidget.h> |
| 35 |
|
| 36 |
#include <avogadro/core/basisset.h> |
| 37 |
#include <avogadro/core/gaussiansettools.h> |
| 38 |
|
| 39 |
#include <avogadro/io/fileformatmanager.h> |
| 40 |
#include <avogadro/quantumio/gamessus.h> |
| 41 |
#include <avogadro/quantumio/gaussiancube.h> |
| 42 |
#include <avogadro/quantumio/gaussianfchk.h> |
| 43 |
#include <avogadro/quantumio/genericjson.h> |
| 44 |
#include <avogadro/quantumio/genericoutput.h> |
| 45 |
#include <avogadro/quantumio/molden.h> |
| 46 |
#include <avogadro/quantumio/mopacaux.h> |
| 47 |
#include <avogadro/quantumio/nwchemjson.h> |
| 48 |
#include <avogadro/quantumio/nwchemlog.h> |
| 49 |
#include <avogadro/quantumio/orca.h> |
| 50 |
#include <avogadro/quantumio/qcschema.h> |
| 51 |
|
| 52 |
#include <QAction> |
| 53 |
#include <QOpenGLFramebufferObject> |
| 54 |
#include <QtConcurrent/QtConcurrentMap> |
| 55 |
#include <QtConcurrent/QtConcurrentRun> |
| 56 |
#include <QtCore/QBuffer> |
| 57 |
#include <QtCore/QCoreApplication> |
| 58 |
#include <QtCore/QDebug> |
| 59 |
#include <QtCore/QProcess> |
| 60 |
#include <QtWidgets/QFileDialog> |
| 61 |
#include <QtWidgets/QMessageBox> |
| 62 |
|
| 63 |
#include <QGuiApplication> |
| 64 |
#include <QScreen> |
| 65 |
|
| 66 |
using namespace tinycolormap; |
| 67 |
|
| 68 |
namespace Avogadro::QtPlugins { |
| 69 |
|
| 70 |
using Core::Array; |
| 71 |
using Core::Cube; |
| 72 |
using Core::GaussianSet; |
| 73 |
using Core::NeighborPerceiver; |
| 74 |
using QtGui::GaussianSetConcurrent; |
| 75 |
using QtGui::Molecule; |
| 76 |
using QtGui::SlaterSetConcurrent; |
| 77 |
|
| 78 |
class Surfaces::PIMPL |
| 79 |
{ |
| 80 |
public: |
| 81 |
GifWriter* gifWriter = nullptr; |
| 82 |
gwavi_t* gwaviWriter = nullptr; |
| 83 |
}; |
| 84 |
|
| 85 |
Surfaces::Surfaces(QObject* p) : ExtensionPlugin(p), d(new PIMPL()) |
| 86 |
{ |
| 87 |
auto action = new QAction(this); |
| 88 |
action->setText(tr("Create Surfaces…")); |
| 89 |
connect(action, SIGNAL(triggered()), SLOT(surfacesActivated())); |
| 90 |
connect(&m_displayMeshWatcher, SIGNAL(finished()), SLOT(displayMesh())); |
| 91 |
connect(&m_performEDTStepWatcher, SIGNAL(finished()), SLOT(performEDTStep())); |
| 92 |
|
| 93 |
m_actions.push_back(action); |
| 94 |
|
| 95 |
// Register quantum file formats |
| 96 |
Io::FileFormatManager::registerFormat(new QuantumIO::GAMESSUSOutput); |
| 97 |
Io::FileFormatManager::registerFormat(new QuantumIO::GaussianFchk); |
| 98 |
Io::FileFormatManager::registerFormat(new QuantumIO::GaussianCube); |
| 99 |
Io::FileFormatManager::registerFormat(new QuantumIO::GenericJson); |
| 100 |
Io::FileFormatManager::registerFormat(new QuantumIO::GenericOutput); |
| 101 |
Io::FileFormatManager::registerFormat(new QuantumIO::MoldenFile); |
| 102 |
Io::FileFormatManager::registerFormat(new QuantumIO::MopacAux); |
| 103 |
Io::FileFormatManager::registerFormat(new QuantumIO::NWChemJson); |
| 104 |
Io::FileFormatManager::registerFormat(new QuantumIO::NWChemLog); |
| 105 |
Io::FileFormatManager::registerFormat(new QuantumIO::ORCAOutput); |
| 106 |
Io::FileFormatManager::registerFormat(new QuantumIO::QCSchema); |
| 107 |
} |
| 108 |
|
| 109 |
Surfaces::~Surfaces() |
| 110 |
{ |
| 111 |
delete d; |
| 112 |
// delete m_cube; // should be freed by the molecule |
| 113 |
} |
| 114 |
|
| 115 |
void Surfaces::registerCommands() |
| 116 |
{ |
| 117 |
// register some scripting commands |
| 118 |
emit registerCommand("renderVDW", tr("Render the van der Waals surface.")); |
| 119 |
emit registerCommand("renderVanDerWaals", |
| 120 |
tr("Render the van der Waals molecular surface.")); |
| 121 |
emit registerCommand("renderSolventAccessible", |
| 122 |
tr("Render the solvent-accessible molecular surface.")); |
| 123 |
emit registerCommand("renderSolventExcluded", |
| 124 |
tr("Render the solvent-excluded molecular surface.")); |
| 125 |
emit registerCommand("renderOrbital", tr("Render a molecular orbital.")); |
| 126 |
emit registerCommand("renderMO", tr("Render a molecular orbital.")); |
| 127 |
emit registerCommand("renderElectronDensity", |
| 128 |
tr("Render the electron density.")); |
| 129 |
emit registerCommand("renderSpinDensity", tr("Render the spin density.")); |
| 130 |
emit registerCommand("renderCube", |
| 131 |
tr("Render a cube supplied with the file.")); |
| 132 |
} |
| 133 |
|
| 134 |
bool Surfaces::handleCommand(const QString& command, const QVariantMap& options) |
| 135 |
{ |
| 136 |
if (m_molecule == nullptr) |
| 137 |
return false; // No molecule to handle the command. |
| 138 |
|
| 139 |
// Set up some defaults for the options. |
| 140 |
int index = -1; |
| 141 |
int homo = -1; |
| 142 |
float isoValue = 0.03; |
| 143 |
float cubeResolution = resolution(); // use default resolution |
| 144 |
|
| 145 |
if (options.contains("resolution") && |
| 146 |
options["resolution"].canConvert<float>()) { |
| 147 |
bool ok; |
| 148 |
float res = options["resolution"].toFloat(&ok); |
| 149 |
if (ok) |
| 150 |
cubeResolution = res; |
| 151 |
} |
| 152 |
if (options.contains("isovalue") && options["isovalue"].canConvert<float>()) { |
| 153 |
bool ok; |
| 154 |
float iso = options["isovalue"].toFloat(&ok); |
| 155 |
if (ok) |
| 156 |
isoValue = iso; |
| 157 |
} |
| 158 |
|
| 159 |
if (m_basis != nullptr) |
| 160 |
homo = m_basis->homo(); |
| 161 |
if (options.contains("orbital")) { |
| 162 |
// check if options contains "homo" or "lumo" |
| 163 |
bool ok = false; |
| 164 |
if (options["orbital"].canConvert<int>()) { |
| 165 |
// internally, we count orbitals from zero |
| 166 |
// if the conversion worked, ok = true |
| 167 |
// and we'll skip the next conditional |
| 168 |
index = options.value("orbital").toInt(&ok) - 1; |
| 169 |
} |
| 170 |
if (!ok && options["orbital"].canConvert<QString>()) { |
| 171 |
// should be something like "homo-1" or "lumo+2" |
| 172 |
QString name = options["orbital"].toString(); |
| 173 |
QString expression, modifier; |
| 174 |
if (name.contains("homo", Qt::CaseInsensitive)) { |
| 175 |
index = homo; // modified by the expression after the string |
| 176 |
expression = name.remove("homo", Qt::CaseInsensitive); |
| 177 |
} else if (name.contains("lumo", Qt::CaseInsensitive)) { |
| 178 |
index = homo + 1; // again modified by the expression |
| 179 |
expression = name.remove("lumo", Qt::CaseInsensitive); |
| 180 |
} |
| 181 |
// modify HOMO / LUMO based on "+ number" or "- number" |
| 182 |
if (expression.contains('-')) { |
| 183 |
modifier = expression.remove('-'); |
| 184 |
bool ok1; |
| 185 |
int n = modifier.toInt(&ok1); |
| 186 |
if (ok1) |
| 187 |
index = index - n; |
| 188 |
} else if (expression.contains('+')) { |
| 189 |
modifier = expression.remove('+'); |
| 190 |
bool ok2; |
| 191 |
int n = modifier.toInt(&ok2); |
| 192 |
if (ok2) |
| 193 |
index = index + n; |
| 194 |
} |
| 195 |
index = index - 1; // start from zero |
| 196 |
} |
| 197 |
} |
| 198 |
bool beta = false; |
| 199 |
if (options.contains("spin")) { |
| 200 |
beta = options["spin"].toString().contains("beta"); |
| 201 |
} |
| 202 |
|
| 203 |
if ((command.compare("renderVanDerWaals", Qt::CaseInsensitive) == 0) || |
| 204 |
(command.compare("renderVDW", Qt::CaseInsensitive) == 0)) { |
| 205 |
calculateEDT(VanDerWaals, cubeResolution); |
| 206 |
return true; |
| 207 |
} else if (command.compare("renderSolventAccessible", Qt::CaseInsensitive) == |
| 208 |
0) { |
| 209 |
calculateEDT(SolventAccessible, cubeResolution); |
| 210 |
return true; |
| 211 |
} else if (command.compare("renderSolventExcluded", Qt::CaseInsensitive) == |
| 212 |
0) { |
| 213 |
calculateEDT(SolventExcluded, cubeResolution); |
| 214 |
return true; |
| 215 |
} else if ((command.compare("renderOrbital", Qt::CaseInsensitive) == 0) || |
| 216 |
(command.compare("renderMO", Qt::CaseInsensitive) == 0)) { |
| 217 |
calculateQM(MolecularOrbital, index, beta, isoValue, cubeResolution); |
| 218 |
return true; |
| 219 |
} else if (command.compare("renderElectronDensity", Qt::CaseInsensitive) == |
| 220 |
0) { |
| 221 |
calculateQM(ElectronDensity, index, beta, isoValue, cubeResolution); |
| 222 |
return true; |
| 223 |
} else if (command.compare("renderSpinDensity", Qt::CaseInsensitive) == 0) { |
| 224 |
calculateQM(SpinDensity, index, beta, isoValue, cubeResolution); |
| 225 |
return true; |
| 226 |
} |
| 227 |
|
| 228 |
return false; |
| 229 |
} |
| 230 |
|
| 231 |
void Surfaces::setMolecule(QtGui::Molecule* mol) |
| 232 |
{ |
| 233 |
if (m_molecule != nullptr) { |
| 234 |
m_molecule->disconnect(this); |
| 235 |
} |
| 236 |
|
| 237 |
m_cube = nullptr; |
| 238 |
m_mesh1 = nullptr; |
| 239 |
m_mesh2 = nullptr; |
| 240 |
m_molecule = mol; |
| 241 |
|
| 242 |
if (mol->basisSet()) { |
| 243 |
m_basis = mol->basisSet(); |
| 244 |
} else if (mol->cubes().size() != 0) { |
| 245 |
m_cubes = mol->cubes(); |
| 246 |
// calculate the mesh for the first cube |
| 247 |
if (m_cubes.size() > 0) { |
| 248 |
calculateCube(0, m_isoValue); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
if (m_molecule != nullptr) { |
| 253 |
connect(m_molecule, SIGNAL(changed(uint)), SLOT(moleculeChanged(uint))); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
void Surfaces::moleculeChanged(unsigned int changes) |
| 258 |
{ |
| 259 |
auto currentCubes = m_cubes.size(); |
| 260 |
|
| 261 |
if (changes & Molecule::Added || changes & Molecule::Removed) { |
| 262 |
m_cubes = m_molecule->cubes(); |
| 263 |
m_basis = m_molecule->basisSet(); |
| 264 |
} |
| 265 |
|
| 266 |
// if we now have more cubes than before |
| 267 |
// calculate the new mesh |
| 268 |
if (currentCubes == 0 && m_cubes.size() > 0) { |
| 269 |
calculateCube(0, m_isoValue); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
QList<QAction*> Surfaces::actions() const |
| 274 |
{ |
| 275 |
return m_actions; |
| 276 |
} |
| 277 |
|
| 278 |
QStringList Surfaces::menuPath(QAction*) const |
| 279 |
{ |
| 280 |
QStringList path; |
| 281 |
path << tr("&Analyze"); |
| 282 |
return path; |
| 283 |
} |
| 284 |
|
| 285 |
void Surfaces::surfacesActivated() |
| 286 |
{ |
| 287 |
if (!m_dialog) { |
| 288 |
m_dialog = new SurfaceDialog(qobject_cast<QWidget*>(parent())); |
| 289 |
connect(m_dialog, SIGNAL(calculateClickedSignal()), |
| 290 |
SLOT(calculateSurface())); |
| 291 |
connect(m_dialog, SIGNAL(recordClicked()), SLOT(recordMovie())); |
| 292 |
connect(m_dialog, SIGNAL(stepChanged(int)), SLOT(stepChanged(int))); |
| 293 |
} |
| 294 |
|
| 295 |
if (m_basis) { |
| 296 |
// we have quantum data, set up the dialog accordingly |
| 297 |
auto gaussian = dynamic_cast<Core::GaussianSet*>(m_basis); |
| 298 |
bool beta = false; |
| 299 |
if (gaussian) { |
| 300 |
auto b = gaussian->moMatrix(GaussianSet::Beta); |
| 301 |
if (b.rows() > 0 && b.cols() > 0) |
| 302 |
beta = true; |
| 303 |
} |
| 304 |
m_dialog->setupBasis(m_basis->electronCount(), |
| 305 |
m_basis->molecularOrbitalCount(), beta); |
| 306 |
} |
| 307 |
// only show cubes from files so we don't duplicate orbitals |
| 308 |
if (m_cubes.size() > 0) { |
| 309 |
QStringList cubeNames; |
| 310 |
for (auto* cube : m_cubes) { |
| 311 |
if (cube->cubeType() == Core::Cube::Type::FromFile) { |
| 312 |
// check if there's a name |
| 313 |
if (cube->name().empty()) { |
| 314 |
// use a numbered name |
| 315 |
int number = 1; |
| 316 |
QString name; |
| 317 |
name = tr("Cube %1", "3D Volume Data Set").arg(number); |
| 318 |
cubeNames << name; |
| 319 |
} else { |
| 320 |
cubeNames << cube->name().c_str(); |
| 321 |
} |
| 322 |
} |
| 323 |
} |
| 324 |
m_dialog->setupCubes(cubeNames); |
| 325 |
} |
| 326 |
m_dialog->setupSteps(m_molecule->coordinate3dCount()); |
| 327 |
|
| 328 |
const auto identifiers = |
| 329 |
Calc::ChargeManager::instance().identifiersForMolecule(*m_molecule); |
| 330 |
std::set<std::pair<std::string, std::string>> chargeModels; |
| 331 |
for (const auto& identifier : identifiers) |
| 332 |
chargeModels.emplace( |
| 333 |
Calc::ChargeManager::instance().nameForModel(identifier), identifier); |
| 334 |
m_dialog->setupModels(chargeModels); |
| 335 |
|
| 336 |
m_dialog->show(); |
| 337 |
m_dialog->raise(); |
| 338 |
m_dialog->activateWindow(); |
| 339 |
} |
| 340 |
|
| 341 |
float Surfaces::resolution(float specified) |
| 342 |
{ |
| 343 |
if (specified != 0.0) |
| 344 |
return specified; |
| 345 |
|
| 346 |
if (m_dialog != nullptr && !m_dialog->automaticResolution()) |
| 347 |
return m_dialog->resolution(); |
| 348 |
|
| 349 |
float r = 0.02 * powf(m_molecule->atomCount(), 1.0f / 3.0f); |
| 350 |
float minimum = 0.05; |
| 351 |
float maximum = 0.5; |
| 352 |
|
| 353 |
if (m_dialog != nullptr) { |
| 354 |
switch (m_dialog->surfaceType()) { |
| 355 |
case SolventExcluded: |
| 356 |
minimum = 0.1; |
| 357 |
break; |
| 358 |
default:; |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
r = std::clamp(r, minimum, maximum); |
| 363 |
return r; |
| 364 |
} |
| 365 |
|
| 366 |
void Surfaces::calculateSurface() |
| 367 |
{ |
| 368 |
if (!m_dialog) |
| 369 |
return; |
| 370 |
|
| 371 |
Type type = m_dialog->surfaceType(); |
| 372 |
// Each calculate* method handles its own cube creation/cleanup. |
| 373 |
|
| 374 |
switch (type) { |
| 375 |
case VanDerWaals: |
| 376 |
case SolventAccessible: |
| 377 |
case SolventExcluded: |
| 378 |
calculateEDT(); |
| 379 |
break; |
| 380 |
|
| 381 |
case ElectronDensity: |
| 382 |
case MolecularOrbital: |
| 383 |
case ElectrostaticPotential: |
| 384 |
case SpinDensity: |
| 385 |
calculateQM(); |
| 386 |
break; |
| 387 |
|
| 388 |
case FromFile: |
| 389 |
default: |
| 390 |
calculateCube(); |
| 391 |
break; |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
float inline square(float x) |
| 396 |
{ |
| 397 |
return x * x; |
| 398 |
} |
| 399 |
|
| 400 |
void Surfaces::calculateEDT(Type type, float defaultResolution) |
| 401 |
{ |
| 402 |
if (type == Unknown && m_dialog != nullptr) |
| 403 |
type = m_dialog->surfaceType(); |
| 404 |
|
| 405 |
// Reset state to avoid stale meshes/cubes from prior calculations |
| 406 |
// (e.g., switching from MO to VdW would leave m_mesh2 rendering) |
| 407 |
m_molecule->clearCubes(); |
| 408 |
m_molecule->clearMeshes(); |
| 409 |
m_cube = nullptr; |
| 410 |
m_mesh1 = nullptr; |
| 411 |
m_mesh2 = nullptr; |
| 412 |
m_molecule->emitChanged(Molecule::Atoms | Molecule::Added); |
| 413 |
|
| 414 |
if (!m_cube) |
| 415 |
m_cube = m_molecule->addCube(); |
| 416 |
|
| 417 |
// Set this cube as the active cube for volume rendering |
| 418 |
m_molecule->setActiveCubeIndex(m_molecule->cubeCount() - 1); |
| 419 |
|
| 420 |
QFuture future = QtConcurrent::run([=]() { |
| 421 |
double probeRadius = 0.0; |
| 422 |
switch (type) { |
| 423 |
case VanDerWaals: |
| 424 |
m_cube->setCubeType(Core::Cube::Type::VdW); |
| 425 |
break; |
| 426 |
case SolventAccessible: |
| 427 |
m_cube->setCubeType(Core::Cube::Type::SolventAccessible); |
| 428 |
break; |
| 429 |
case SolventExcluded: |
| 430 |
probeRadius = 1.4; |
| 431 |
m_cube->setCubeType(Core::Cube::Type::SolventExcluded); |
| 432 |
break; |
| 433 |
default: |
| 434 |
break; |
| 435 |
} |
| 436 |
|
| 437 |
// first, make a list of all atom positions and radii |
| 438 |
Array<Vector3> atomPositions = m_molecule->atomPositions3d(); |
| 439 |
auto* atoms = new std::vector<std::pair<Vector3, double>>(); |
| 440 |
double max_radius = probeRadius; |
| 441 |
QtGui::RWLayerManager layerManager; |
| 442 |
for (size_t i = 0; i < m_molecule->atomCount(); i++) { |
| 443 |
if (!layerManager.visible(m_molecule->layer(i))) |
| 444 |
continue; // ignore invisible atoms |
| 445 |
auto radius = |
| 446 |
Core::Elements::radiusVDW(m_molecule->atomicNumber(i)) + probeRadius; |
| 447 |
atoms->emplace_back(atomPositions[i], radius); |
| 448 |
if (radius > max_radius) |
| 449 |
max_radius = radius; |
| 450 |
} |
| 451 |
|
| 452 |
double padding = max_radius + probeRadius + 0.2; |
| 453 |
m_cube->setLimits(*m_molecule, resolution(defaultResolution), padding); |
| 454 |
m_cube->fill(-1.0); |
| 455 |
|
| 456 |
const float res = resolution(defaultResolution); |
| 457 |
const Vector3 min = m_cube->min(); |
| 458 |
|
| 459 |
// then, for each atom, set cubes around it up to a certain radius |
| 460 |
QFuture innerFuture = |
| 461 |
QtConcurrent::map(*atoms, [=](std::pair<Vector3, double>& in) { |
| 462 |
double startPosX = in.first(0) - in.second; |
| 463 |
double endPosX = in.first(0) + in.second; |
| 464 |
int startIndexX = (startPosX - min(0)) / res; |
| 465 |
int endIndexX = (endPosX - min(0)) / res + 1; |
| 466 |
for (int indexX = startIndexX; indexX < endIndexX; indexX++) { |
| 467 |
double posX = indexX * res + min(0); |
| 468 |
double radiusXsq = square(in.second) - square(posX - in.first(0)); |
| 469 |
if (radiusXsq < 0.0) |
| 470 |
continue; |
| 471 |
double radiusX = sqrt(radiusXsq); |
| 472 |
double startPosY = in.first(1) - radiusX; |
| 473 |
double endPosY = in.first(1) + radiusX; |
| 474 |
int startIndexY = (startPosY - min(1)) / res; |
| 475 |
int endIndexY = (endPosY - min(1)) / res + 1; |
| 476 |
for (int indexY = startIndexY; indexY < endIndexY; indexY++) { |
| 477 |
double posY = indexY * res + min(1); |
| 478 |
double lengthXYsq = square(radiusX) - square(posY - in.first(1)); |
| 479 |
if (lengthXYsq < 0.0) |
| 480 |
continue; |
| 481 |
double lengthXY = sqrt(lengthXYsq); |
| 482 |
double startPosZ = in.first(2) - lengthXY; |
| 483 |
double endPosZ = in.first(2) + lengthXY; |
| 484 |
int startIndexZ = (startPosZ - min(2)) / res; |
| 485 |
int endIndexZ = (endPosZ - min(2)) / res + 1; |
| 486 |
m_cube->fillStripe(indexX, indexY, startIndexZ, endIndexZ - 1, |
| 487 |
1.0f); |
| 488 |
} |
| 489 |
} |
| 490 |
}); |
| 491 |
|
| 492 |
innerFuture.waitForFinished(); |
| 493 |
}); |
| 494 |
|
| 495 |
// SolventExcluded requires an extra pass |
| 496 |
if (type == SolventExcluded) { |
| 497 |
m_performEDTStepWatcher.setFuture(future); |
| 498 |
} else { |
| 499 |
m_displayMeshWatcher.setFuture(future); |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
void Surfaces::performEDTStep() |
| 504 |
{ |
| 505 |
QFuture future = QtConcurrent::run([=]() { |
| 506 |
const double probeRadius = 1.4; |
| 507 |
const double scaledProbeRadius = probeRadius / resolution(); |
| 508 |
|
| 509 |
// make a list of all "outside" cubes in contact with an "inside" cube |
| 510 |
// these are the only ones that can be "nearest" to an "inside" cube |
| 511 |
Array<Vector3> relativePositions; |
| 512 |
// also make a list of all "inside" cubes |
| 513 |
auto* insideIndices = new std::vector<Vector3i>; |
| 514 |
Vector3i size = m_cube->dimensions(); |
| 515 |
relativePositions.reserve(size(0) * size(1) * 4); // O(n^2) |
| 516 |
insideIndices->reserve(size(0) * size(1) * size(2)); // O(n^3) |
| 517 |
for (int z = 0; z < size(2); z++) { |
| 518 |
int zp = std::max(z - 1, 0); |
| 519 |
int zn = std::min(z + 1, size(2) - 1); |
| 520 |
for (int y = 0; y < size(1); y++) { |
| 521 |
int yp = std::max(y - 1, 0); |
| 522 |
int yn = std::min(y + 1, size(1) - 1); |
| 523 |
for (int x = 0; x < size(0); x++) { |
| 524 |
if (m_cube->value(x, y, z) > 0.0) { |
| 525 |
insideIndices->emplace_back(x, y, z); |
| 526 |
continue; |
| 527 |
} |
| 528 |
int xp = std::max(x - 1, 0); |
| 529 |
int xn = std::min(x + 1, size(0) - 1); |
| 530 |
if (m_cube->value(xp, y, z) > 0.0 || m_cube->value(xn, y, z) > 0.0 || |
| 531 |
m_cube->value(x, yp, z) > 0.0 || m_cube->value(x, yn, z) > 0.0 || |
| 532 |
m_cube->value(x, y, zp) > 0.0 || m_cube->value(x, y, zn) > 0.0) { |
| 533 |
relativePositions.push_back(Vector3(x, y, z)); |
| 534 |
} |
| 535 |
} |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
// pass the list to a NeighborPerceiver so it's faster to look up |
| 540 |
NeighborPerceiver perceiver(relativePositions, scaledProbeRadius); |
| 541 |
|
| 542 |
// now, exclude all "inside" cubes too close to any "outside" cube |
| 543 |
thread_local Array<Index>* neighbors = nullptr; |
| 544 |
QFuture innerFuture = QtConcurrent::map(*insideIndices, [=](Vector3i& in) { |
| 545 |
Vector3 pos = in.cast<double>(); |
| 546 |
if (neighbors == nullptr) |
| 547 |
neighbors = new Array<Index>; |
| 548 |
perceiver.getNeighborsInclusiveInPlace(*neighbors, pos); |
| 549 |
for (Index neighbor : *neighbors) { |
| 550 |
const Vector3& npos = relativePositions[neighbor]; |
| 551 |
float distance = (npos - pos).norm(); |
| 552 |
if (distance <= scaledProbeRadius) { |
| 553 |
m_cube->setValue(in(0), in(1), in(2), -1.0f); |
| 554 |
break; |
| 555 |
} |
| 556 |
} |
| 557 |
}); |
| 558 |
|
| 559 |
innerFuture.waitForFinished(); |
| 560 |
}); |
| 561 |
|
| 562 |
m_displayMeshWatcher.setFuture(future); |
| 563 |
} |
| 564 |
|
| 565 |
void Surfaces::calculateQM(Type type, int index, bool beta, float isoValue, |
| 566 |
float defaultResolution) |
| 567 |
{ |
| 568 |
if (!m_basis) |
| 569 |
return; // nothing to do |
| 570 |
|
| 571 |
if (m_dialog != nullptr) { |
| 572 |
beta = m_dialog->beta(); // we're using the GUI |
| 573 |
} |
| 574 |
|
| 575 |
// TODO: check if we already calculated the requested cube |
| 576 |
|
| 577 |
// Reset state a little more frequently, minimal cost, avoid bugs. |
| 578 |
m_molecule->clearCubes(); |
| 579 |
m_molecule->clearMeshes(); |
| 580 |
m_cube = nullptr; |
| 581 |
m_mesh1 = nullptr; |
| 582 |
m_mesh2 = nullptr; |
| 583 |
m_molecule->emitChanged(Molecule::Atoms | Molecule::Added); |
| 584 |
bool connectSlots = false; |
| 585 |
|
| 586 |
// set up QtConcurrent calculators for Gaussian or Slater basis sets |
| 587 |
if (dynamic_cast<GaussianSet*>(m_basis)) { |
| 588 |
if (!m_gaussianConcurrent) { |
| 589 |
m_gaussianConcurrent = new GaussianSetConcurrent(this); |
| 590 |
connectSlots = true; |
| 591 |
} |
| 592 |
m_gaussianConcurrent->setMolecule(m_molecule); |
| 593 |
} else { |
| 594 |
if (!m_slaterConcurrent) { |
| 595 |
m_slaterConcurrent = new SlaterSetConcurrent(this); |
| 596 |
connectSlots = true; |
| 597 |
} |
| 598 |
m_slaterConcurrent->setMolecule(m_molecule); |
| 599 |
} |
| 600 |
|
| 601 |
// TODO: Check to see if this cube or surface has already been computed |
| 602 |
if (!m_progressDialog) { |
| 603 |
m_progressDialog = |
| 604 |
new QtGui::TimedProgressDialog(qobject_cast<QWidget*>(parent())); |
| 605 |
m_progressDialog->setWindowModality(Qt::NonModal); |
| 606 |
connectSlots = true; |
| 607 |
} |
| 608 |
|
| 609 |
if (!m_cube) |
| 610 |
m_cube = m_molecule->addCube(); |
| 611 |
|
| 612 |
// Set this cube as the active cube for volume rendering |
| 613 |
m_molecule->setActiveCubeIndex(m_molecule->cubeCount() - 1); |
| 614 |
|
| 615 |
if (type == Unknown) |
| 616 |
type = m_dialog->surfaceType(); |
| 617 |
|
| 618 |
if (index == -1 && m_dialog != nullptr) |
| 619 |
index = m_dialog->surfaceIndex(); |
| 620 |
if (isoValue == 0.0 && m_dialog != nullptr) |
| 621 |
m_isoValue = m_dialog->isosurfaceValue(); |
| 622 |
else |
| 623 |
m_isoValue = isoValue; |
| 624 |
float cubeResolution = resolution(defaultResolution); |
| 625 |
float padding = 5.0; |
| 626 |
// TODO: allow extra padding for some molecules / properties |
| 627 |
m_cube->setLimits(*m_molecule, cubeResolution, padding); |
| 628 |
|
| 629 |
QString progressText; |
| 630 |
if (type == ElectronDensity) { |
| 631 |
progressText = tr("Calculating electron density"); |
| 632 |
m_cube->setName("Electron Density"); |
| 633 |
m_cube->setCubeType(Core::Cube::Type::ElectronDensity); |
| 634 |
if (dynamic_cast<GaussianSet*>(m_basis)) { |
| 635 |
m_gaussianConcurrent->calculateElectronDensity(m_cube); |
| 636 |
} else { |
| 637 |
m_slaterConcurrent->calculateElectronDensity(m_cube); |
| 638 |
} |
| 639 |
} else if (type == SpinDensity) { |
| 640 |
progressText = tr("Calculating spin density"); |
| 641 |
m_cube->setName("Spin Density"); |
| 642 |
m_cube->setCubeType(Core::Cube::Type::SpinDensity); |
| 643 |
if (dynamic_cast<GaussianSet*>(m_basis)) { |
| 644 |
m_gaussianConcurrent->calculateSpinDensity(m_cube); |
| 645 |
} else { |
| 646 |
m_slaterConcurrent->calculateSpinDensity(m_cube); |
| 647 |
} |
| 648 |
} else if (type == MolecularOrbital) { |
| 649 |
progressText = tr("Calculating molecular orbital %L1").arg(index); |
| 650 |
m_cube->setName("Molecular Orbital " + std::to_string(index + 1)); |
| 651 |
m_cube->setCubeType(Core::Cube::Type::MO); |
| 652 |
if (dynamic_cast<GaussianSet*>(m_basis)) { |
| 653 |
m_gaussianConcurrent->calculateMolecularOrbital(m_cube, index, beta); |
| 654 |
} else { |
| 655 |
m_slaterConcurrent->calculateMolecularOrbital(m_cube, index); |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
// Set up the progress dialog. |
| 660 |
if (dynamic_cast<GaussianSet*>(m_basis)) { |
| 661 |
m_progressDialog->setWindowTitle(progressText); |
| 662 |
m_progressDialog->setRange( |
| 663 |
m_gaussianConcurrent->watcher().progressMinimum(), |
| 664 |
m_gaussianConcurrent->watcher().progressMaximum()); |
| 665 |
m_progressDialog->setValue(m_gaussianConcurrent->watcher().progressValue()); |
| 666 |
m_progressDialog->show(); |
| 667 |
|
| 668 |
if (connectSlots) { |
| 669 |
connect(&m_gaussianConcurrent->watcher(), |
| 670 |
SIGNAL(progressValueChanged(int)), m_progressDialog, |
| 671 |
SLOT(setValue(int))); |
| 672 |
connect(&m_gaussianConcurrent->watcher(), |
| 673 |
SIGNAL(progressRangeChanged(int, int)), m_progressDialog, |
| 674 |
SLOT(setRange(int, int))); |
| 675 |
connect(m_gaussianConcurrent, SIGNAL(finished()), SLOT(displayMesh())); |
| 676 |
connect(m_progressDialog, SIGNAL(canceled()), |
| 677 |
&m_gaussianConcurrent->watcher(), SLOT(cancel())); |
| 678 |
} |
| 679 |
} else { |
| 680 |
// slaters |
| 681 |
m_progressDialog->setWindowTitle(progressText); |
| 682 |
m_progressDialog->setRange(m_slaterConcurrent->watcher().progressMinimum(), |
| 683 |
m_slaterConcurrent->watcher().progressMaximum()); |
| 684 |
m_progressDialog->setValue(m_slaterConcurrent->watcher().progressValue()); |
| 685 |
m_progressDialog->show(); |
| 686 |
|
| 687 |
connect(&m_slaterConcurrent->watcher(), SIGNAL(progressValueChanged(int)), |
| 688 |
m_progressDialog, SLOT(setValue(int))); |
| 689 |
connect(&m_slaterConcurrent->watcher(), |
| 690 |
SIGNAL(progressRangeChanged(int, int)), m_progressDialog, |
| 691 |
SLOT(setRange(int, int))); |
| 692 |
connect(m_progressDialog, SIGNAL(canceled()), |
| 693 |
&m_slaterConcurrent->watcher(), SLOT(cancel())); |
| 694 |
connect(m_slaterConcurrent, SIGNAL(finished()), SLOT(displayMesh())); |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
void Surfaces::calculateCube(int index, float isoValue) |
| 699 |
{ |
| 700 |
if (m_cubes.size() == 0) |
| 701 |
return; |
| 702 |
|
| 703 |
if (index == -1 && m_dialog != nullptr) |
| 704 |
index = m_dialog->surfaceIndex(); |
| 705 |
if (index < 0 || index >= static_cast<int>(m_cubes.size())) |
| 706 |
return; |
| 707 |
|
| 708 |
// check bounds |
| 709 |
m_cube = m_cubes[index]; |
| 710 |
if (m_cube == nullptr) |
| 711 |
return; |
| 712 |
|
| 713 |
// Set this cube as the active cube for volume rendering |
| 714 |
m_molecule->setActiveCubeIndex(index); |
| 715 |
|
| 716 |
if (isoValue == 0.0 && m_dialog != nullptr) |
| 717 |
m_isoValue = m_dialog->isosurfaceValue(); |
| 718 |
else |
| 719 |
m_isoValue = isoValue; |
| 720 |
displayMesh(); |
| 721 |
} |
| 722 |
|
| 723 |
void Surfaces::stepChanged(int n) |
| 724 |
{ |
| 725 |
if (!m_molecule || !m_basis) |
| 726 |
return; |
| 727 |
|
| 728 |
qDebug() << "\n\t==== Step changed to" << n << "===="; |
| 729 |
auto g = dynamic_cast<GaussianSet*>(m_basis); |
| 730 |
if (g) { |
| 731 |
g->setActiveSetStep(n - 1); |
| 732 |
m_molecule->clearCubes(); |
| 733 |
m_molecule->clearMeshes(); |
| 734 |
m_cube = nullptr; |
| 735 |
m_mesh1 = nullptr; |
| 736 |
m_mesh2 = nullptr; |
| 737 |
m_molecule->emitChanged(Molecule::Atoms | Molecule::Added); |
| 738 |
} |
| 739 |
} |
| 740 |
|
| 741 |
void Surfaces::displayMesh() |
| 742 |
{ |
| 743 |
if (!m_cube) |
| 744 |
return; |
| 745 |
|
| 746 |
if (m_dialog != nullptr) |
| 747 |
m_smoothingPasses = m_dialog->smoothingPassesValue(); |
| 748 |
else |
| 749 |
m_smoothingPasses = 0; |
| 750 |
|
| 751 |
if (!m_mesh1) |
| 752 |
m_mesh1 = m_molecule->addMesh(); |
| 753 |
if (!m_meshGenerator1) { |
| 754 |
m_meshGenerator1 = new QtGui::MeshGenerator; |
| 755 |
connect(m_meshGenerator1, SIGNAL(finished()), SLOT(meshFinished())); |
| 756 |
} |
| 757 |
m_meshGenerator1->initialize(m_cube, m_mesh1, m_isoValue, m_smoothingPasses); |
| 758 |
|
| 759 |
bool isMO = false; |
| 760 |
// if it's from a file we should "play it safe" |
| 761 |
if (m_cube->cubeType() == Cube::Type::MO || |
| 762 |
m_cube->cubeType() == Cube::Type::FromFile) { |
| 763 |
isMO = true; |
| 764 |
} |
| 765 |
|
| 766 |
if (isMO) { |
| 767 |
if (!m_mesh2) |
| 768 |
m_mesh2 = m_molecule->addMesh(); |
| 769 |
if (!m_meshGenerator2) { |
| 770 |
m_meshGenerator2 = new QtGui::MeshGenerator; |
| 771 |
connect(m_meshGenerator2, SIGNAL(finished()), SLOT(meshFinished())); |
| 772 |
} |
| 773 |
m_meshGenerator2->initialize(m_cube, m_mesh2, -m_isoValue, |
| 774 |
m_smoothingPasses, true); |
| 775 |
} |
| 776 |
|
| 777 |
// Start the mesh generation - this needs an improved mutex with a read lock |
| 778 |
// to function as expected. Write locks are exclusive, read locks can have |
| 779 |
// many read locks but no write lock. |
| 780 |
m_meshGenerator1->start(); |
| 781 |
if (isMO) |
| 782 |
m_meshGenerator2->start(); |
| 783 |
|
| 784 |
// Track how many meshes are left to show. |
| 785 |
if (isMO) |
| 786 |
m_meshesLeft = 2; |
| 787 |
else |
| 788 |
m_meshesLeft = 1; |
| 789 |
} |
| 790 |
|
| 791 |
Core::Color3f Surfaces::chargeGradient(double value, double clamp, |
| 792 |
ColormapType colormap) const |
| 793 |
{ |
| 794 |
// okay, typically color scales have blue at the bottom, red at the top. |
| 795 |
// so we need to invert, so blue is positive charge, red is negative charge. |
| 796 |
// we also need to scale the color to the range of the charge. |
| 797 |
double scaledValue = value / clamp; // from -1 to 1.0 |
| 798 |
double scaledValue2 = |
| 799 |
1.0 - ((scaledValue + 1.0) / 2.0); // from 0 to 1.0 red to blue |
| 800 |
|
| 801 |
auto color = tinycolormap::GetColor(scaledValue2, colormap); |
| 802 |
Core::Color3f r(float(color.r()), color.g(), color.b()); |
| 803 |
|
| 804 |
return r; |
| 805 |
} |
| 806 |
|
| 807 |
ColormapType Surfaces::getColormapFromString(const QString& name) const |
| 808 |
{ |
| 809 |
// Just do all of them, even though we won't use them all |
| 810 |
if (name == tr("Parula", "colormap")) |
| 811 |
return ColormapType::Parula; |
| 812 |
else if (name == tr("Heat", "colormap")) |
| 813 |
return ColormapType::Heat; |
| 814 |
else if (name == tr("Hot", "colormap")) |
| 815 |
return ColormapType::Hot; |
| 816 |
else if (name == tr("Gray", "colormap")) |
| 817 |
return ColormapType::Gray; |
| 818 |
else if (name == tr("Magma", "colormap")) |
| 819 |
return ColormapType::Magma; |
| 820 |
else if (name == tr("Inferno", "colormap")) |
| 821 |
return ColormapType::Inferno; |
| 822 |
else if (name == tr("Plasma", "colormap")) |
| 823 |
return ColormapType::Plasma; |
| 824 |
else if (name == tr("Viridis", "colormap")) |
| 825 |
return ColormapType::Viridis; |
| 826 |
else if (name == tr("Cividis", "colormap")) |
| 827 |
return ColormapType::Cividis; |
| 828 |
else if (name == tr("Spectral", "colormap")) |
| 829 |
return ColormapType::Spectral; |
| 830 |
else if (name == tr("Coolwarm", "colormap")) |
| 831 |
return ColormapType::Coolwarm; |
| 832 |
else if (name == tr("Balance", "colormap")) |
| 833 |
return ColormapType::Balance; |
| 834 |
else if (name == tr("Blue-DarkRed", "colormap")) |
| 835 |
return ColormapType::BlueDkRed; |
| 836 |
else if (name == tr("Turbo", "colormap")) |
| 837 |
return ColormapType::Turbo; |
| 838 |
|
| 839 |
return ColormapType::Turbo; |
| 840 |
} |
| 841 |
|
| 842 |
void Surfaces::colorMeshByPotential() |
| 843 |
{ |
| 844 |
const auto model = m_dialog->colorModel().toStdString(); |
| 845 |
const auto colormap = getColormapFromString(m_dialog->colormapName()); |
| 846 |
|
| 847 |
const auto positionsf = m_mesh1->vertices(); |
| 848 |
if (positionsf.empty()) |
| 849 |
return; |
| 850 |
|
| 851 |
Core::Array<Vector3> positions(positionsf.size()); |
| 852 |
std::transform(positionsf.begin(), positionsf.end(), positions.begin(), |
| 853 |
[](const Vector3f& pos) { return pos.cast<double>(); }); |
| 854 |
const auto potentials = |
| 855 |
Calc::ChargeManager::instance().potentials(model, *m_molecule, positions); |
| 856 |
|
| 857 |
double minPotential = *std::min_element(potentials.begin(), potentials.end()); |
| 858 |
double maxPotential = *std::max_element(potentials.begin(), potentials.end()); |
| 859 |
double clamp = std::max(std::abs(minPotential), std::abs(maxPotential)); |
| 860 |
|
| 861 |
Core::Array<Core::Color3f> colors(positions.size()); |
| 862 |
for (size_t i = 0; i < potentials.size(); i++) |
| 863 |
colors[i] = chargeGradient(potentials[i], clamp, colormap); |
| 864 |
|
| 865 |
m_mesh1->setColors(colors); |
| 866 |
} |
| 867 |
|
| 868 |
void Surfaces::colorMesh() |
| 869 |
{ |
| 870 |
if (m_dialog == nullptr) |
| 871 |
return; |
| 872 |
|
| 873 |
switch (m_dialog->colorProperty()) { |
| 874 |
case None: |
| 875 |
break; |
| 876 |
case ByElectrostaticPotential: |
| 877 |
colorMeshByPotential(); |
| 878 |
break; |
| 879 |
} |
| 880 |
} |
| 881 |
|
| 882 |
void Surfaces::meshFinished() |
| 883 |
{ |
| 884 |
--m_meshesLeft; |
| 885 |
if (m_meshesLeft == 0) { |
| 886 |
colorMesh(); |
| 887 |
|
| 888 |
// finished, so request to enable the mesh display type |
| 889 |
QStringList displayTypes; |
| 890 |
displayTypes << tr("Surfaces"); |
| 891 |
emit requestActiveDisplayTypes(displayTypes); |
| 892 |
|
| 893 |
if (m_recordingMovie) { |
| 894 |
// Move to the next frame. |
| 895 |
qDebug() << "Let's get to the next frame…"; |
| 896 |
m_molecule->emitChanged(QtGui::Molecule::Added); |
| 897 |
movieFrame(); |
| 898 |
} else { |
| 899 |
if (m_dialog != nullptr) |
| 900 |
m_dialog->reenableCalculateButton(); |
| 901 |
|
| 902 |
m_molecule->emitChanged(QtGui::Molecule::Added); |
| 903 |
|
| 904 |
emit commandFinished(tr( |
| 905 |
"Surface Finished", "finished rendering surface or molecular orbital")); |
| 906 |
} |
| 907 |
} |
| 908 |
} |
| 909 |
|
| 910 |
void Surfaces::recordMovie() |
| 911 |
{ |
| 912 |
QString baseFileName; |
| 913 |
if (m_molecule) |
| 914 |
baseFileName = m_molecule->data("fileName").toString().c_str(); |
| 915 |
|
| 916 |
QString selectedFilter = tr("Movie AVI (*.avi)"); |
| 917 |
QString baseName = QFileDialog::getSaveFileName( |
| 918 |
qobject_cast<QWidget*>(parent()), tr("Export Movie"), "", |
| 919 |
tr("Movie MP4 (*.mp4);;Movie AVI (*.avi);;GIF (*.gif)"), &selectedFilter); |
| 920 |
|
| 921 |
if (baseName.isEmpty()) { |
| 922 |
m_dialog->enableRecord(); |
| 923 |
return; |
| 924 |
} |
| 925 |
|
| 926 |
QFileInfo fileInfo(baseName); |
| 927 |
if (!fileInfo.suffix().isEmpty()) |
| 928 |
baseName = fileInfo.absolutePath() + "/" + fileInfo.baseName(); |
| 929 |
|
| 930 |
m_baseFileName = baseName; |
| 931 |
m_numberLength = static_cast<int>( |
| 932 |
ceil(log10(static_cast<float>(m_molecule->coordinate3dCount()) + 1))); |
| 933 |
|
| 934 |
m_recordingMovie = true; |
| 935 |
m_currentFrame = 1; |
| 936 |
m_frameCount = m_molecule->coordinate3dCount(); |
| 937 |
|
| 938 |
// Figure out the save type, and work accordingly... |
| 939 |
if (selectedFilter == tr("GIF (*.gif)")) { |
| 940 |
d->gwaviWriter = nullptr; |
| 941 |
d->gifWriter = new GifWriter; |
| 942 |
GifBegin(d->gifWriter, (baseName + ".gif").toLatin1().data(), 800, 600, |
| 943 |
100 / 4); |
| 944 |
} else if (selectedFilter == tr("Movie AVI (*.avi)")) { |
| 945 |
d->gifWriter = nullptr; |
| 946 |
d->gwaviWriter = gwavi_open((baseName + ".avi").toLatin1().data(), 800, 600, |
| 947 |
"MJPG", 4, nullptr); |
| 948 |
} else { |
| 949 |
d->gwaviWriter = nullptr; |
| 950 |
d->gifWriter = nullptr; |
| 951 |
} |
| 952 |
|
| 953 |
stepChanged(m_currentFrame); |
| 954 |
m_dialog->setStep(m_currentFrame); |
| 955 |
calculateSurface(); |
| 956 |
} |
| 957 |
|
| 958 |
void Surfaces::movieFrame() |
| 959 |
{ |
| 960 |
// Not ideal, need to let things update asynchronously, complete, before we |
| 961 |
// capture the frame. When appropriate move to the next frame or complete. |
| 962 |
QCoreApplication::sendPostedEvents(); |
| 963 |
QCoreApplication::processEvents(); |
| 964 |
|
| 965 |
auto glWidget = QtOpenGL::ActiveObjects::instance().activeGLWidget(); |
| 966 |
if (!glWidget) { |
| 967 |
QMessageBox::warning( |
| 968 |
qobject_cast<QWidget*>(parent()), tr("Avogadro"), |
| 969 |
tr("Couldn't find the active render widget, failing.")); |
| 970 |
m_recordingMovie = false; |
| 971 |
m_dialog->enableRecord(); |
| 972 |
return; |
| 973 |
} |
| 974 |
glWidget->resize(800 / glWidget->devicePixelRatio(), |
| 975 |
600 / glWidget->devicePixelRatio()); |
| 976 |
QImage exportImage; |
| 977 |
glWidget->raise(); |
| 978 |
glWidget->repaint(); |
| 979 |
if (QOpenGLFramebufferObject::hasOpenGLFramebufferObjects()) { |
| 980 |
exportImage = glWidget->grabFramebuffer(); |
| 981 |
} else { |
| 982 |
auto* screen = QGuiApplication::primaryScreen(); |
| 983 |
auto pixmap = screen->grabWindow(glWidget->winId()); |
| 984 |
exportImage = pixmap.toImage(); |
| 985 |
} |
| 986 |
|
| 987 |
if (d->gifWriter) { |
| 988 |
int pixelCount = exportImage.width() * exportImage.height(); |
| 989 |
auto* imageData = new uint8_t[pixelCount * 4]; |
| 990 |
int imageIndex = 0; |
| 991 |
for (int j = 0; j < exportImage.height(); ++j) { |
| 992 |
for (int k = 0; k < exportImage.width(); ++k) { |
| 993 |
QColor color = exportImage.pixel(k, j); |
| 994 |
imageData[imageIndex] = static_cast<uint8_t>(color.red()); |
| 995 |
imageData[imageIndex + 1] = static_cast<uint8_t>(color.green()); |
| 996 |
imageData[imageIndex + 2] = static_cast<uint8_t>(color.blue()); |
| 997 |
imageData[imageIndex + 3] = static_cast<uint8_t>(color.alpha()); |
| 998 |
imageIndex += 4; |
| 999 |
} |
| 1000 |
} |
| 1001 |
GifWriteFrame(d->gifWriter, imageData, 800, 600, 100 / 4); |
| 1002 |
delete[] imageData; |
| 1003 |
} else if (d->gwaviWriter) { |
| 1004 |
QByteArray ba; |
| 1005 |
QBuffer buffer(&ba); |
| 1006 |
buffer.open(QIODevice::WriteOnly); |
| 1007 |
exportImage.save(&buffer, "JPG"); |
| 1008 |
if (gwavi_add_frame( |
| 1009 |
d->gwaviWriter, |
| 1010 |
reinterpret_cast<const unsigned char*>(buffer.data().data()), |
| 1011 |
buffer.size()) == -1) { |
| 1012 |
QMessageBox::warning(qobject_cast<QWidget*>(parent()), tr("Avogadro"), |
| 1013 |
tr("Error: cannot add frame to video.")); |
| 1014 |
} |
| 1015 |
} else { |
| 1016 |
QString fileName = QString::number(m_currentFrame); |
| 1017 |
while (fileName.length() < m_numberLength) |
| 1018 |
fileName.prepend('0'); |
| 1019 |
fileName.prepend(m_baseFileName); |
| 1020 |
fileName.append(".png"); |
| 1021 |
qDebug() << "Writing to" << fileName; |
| 1022 |
|
| 1023 |
if (!exportImage.save(fileName)) { |
| 1024 |
QMessageBox::warning(qobject_cast<QWidget*>(parent()), tr("Avogadro"), |
| 1025 |
tr("Cannot save file %1.").arg(fileName)); |
| 1026 |
return; |
| 1027 |
} |
| 1028 |
} |
| 1029 |
|
| 1030 |
// Increment current frame. |
| 1031 |
++m_currentFrame; |
| 1032 |
if (m_currentFrame <= m_frameCount) { |
| 1033 |
qDebug() << "Starting next frame…"; |
| 1034 |
stepChanged(m_currentFrame); |
| 1035 |
m_dialog->setStep(m_currentFrame); |
| 1036 |
calculateSurface(); |
| 1037 |
} else { |
| 1038 |
qDebug() << "We are done! Make some movies."; |
| 1039 |
if (d->gifWriter) { |
| 1040 |
GifEnd(d->gifWriter); |
| 1041 |
delete d->gifWriter; |
| 1042 |
d->gifWriter = nullptr; |
| 1043 |
} else if (d->gwaviWriter) { |
| 1044 |
gwavi_close(d->gwaviWriter); |
| 1045 |
d->gwaviWriter = nullptr; |
| 1046 |
} else { |
| 1047 |
QProcess proc; |
| 1048 |
QStringList args; |
| 1049 |
args << "-y" |
| 1050 |
<< "-r" << QString::number(10) << "-i" |
| 1051 |
<< m_baseFileName + "%0" + QString::number(m_numberLength) + "d.png" |
| 1052 |
<< "-c:v" |
| 1053 |
<< "libx264" |
| 1054 |
<< "-r" |
| 1055 |
<< "30" |
| 1056 |
<< "-pix_fmt" |
| 1057 |
<< "yuv420p" << m_baseFileName + ".mp4"; |
| 1058 |
proc.execute("avconv", args); |
| 1059 |
} |
| 1060 |
/* |
| 1061 |
args.clear(); |
| 1062 |
args << "-dispose" |
| 1063 |
<< "Background" |
| 1064 |
<< "-delay" << QString::number(100 / 10) |
| 1065 |
<< m_baseFileName + "%0" + QString::number(m_numberLength) + "d.png[0-" |
| 1066 |
+ |
| 1067 |
QString::number(m_molecule->coordinate3dCount() - 1) + "]" |
| 1068 |
<< m_baseFileName + ".gif"; |
| 1069 |
proc.execute("convert", args); |
| 1070 |
*/ |
| 1071 |
|
| 1072 |
m_recordingMovie = false; |
| 1073 |
m_dialog->enableRecord(); |
| 1074 |
} |
| 1075 |
} |
| 1076 |
|
| 1077 |
} // namespace Avogadro::QtPlugins |