moldyn.cpp 39.6 KB
Newer Older
1
/**
Tobias WEBER's avatar
Tobias WEBER committed
2
 * atom dynamics tool
3
4
5
 * @author Tobias Weber <tweber@ill.fr>
 * @date Dec-2019
 * @license GPLv3, see 'LICENSE' file
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 * ----------------------------------------------------------------------------
 * mag-core (part of the Takin software suite)
 * Copyright (C) 2018-2021  Tobias WEBER (Institut Laue-Langevin (ILL),
 *                          Grenoble, France).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * ----------------------------------------------------------------------------
24
25
 */

Tobias WEBER's avatar
Tobias WEBER committed
26
#include "moldyn.h"
27

Tobias WEBER's avatar
Tobias WEBER committed
28
#include <QtWidgets/QApplication>
Tobias WEBER's avatar
Tobias WEBER committed
29
#include <QtWidgets/QGridLayout>
Tobias WEBER's avatar
Tobias WEBER committed
30
#include <QtWidgets/QFileDialog>
Tobias WEBER's avatar
Tobias WEBER committed
31
#include <QtWidgets/QSpinBox>
Tobias WEBER's avatar
Tobias WEBER committed
32
#include <QtWidgets/QComboBox>
Tobias WEBER's avatar
Tobias WEBER committed
33
#include <QtWidgets/QMessageBox>
34
#include <QtWidgets/QProgressDialog>
35

Tobias WEBER's avatar
Tobias WEBER committed
36
37
#include <iostream>
#include <tuple>
Tobias WEBER's avatar
Tobias WEBER committed
38
#include <memory>
39

40
41
#include "tlibs2/libs/algos.h"
#include "tlibs2/libs/helper.h"
Tobias WEBER's avatar
Tobias WEBER committed
42

Tobias WEBER's avatar
Tobias WEBER committed
43
using namespace tl2_ops;
Tobias WEBER's avatar
Tobias WEBER committed
44
45
46
47
48

constexpr t_real g_eps = 1e-6;
constexpr int g_prec = 6;


Tobias WEBER's avatar
Tobias WEBER committed
49
#define PROG_NAME "Molecular Dynamics Tool"
50
#define PROG_VER "0.2"
Tobias WEBER's avatar
Tobias WEBER committed
51

52
53
#define DATA_SEP "#|#"	// data separation string

Tobias WEBER's avatar
Tobias WEBER committed
54

Tobias WEBER's avatar
Tobias WEBER committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

// ----------------------------------------------------------------------------
/**
 * File dialog with options
 */
class MolDynFileDlg : public QFileDialog
{
	public:
		MolDynFileDlg(QWidget *parent, const QString& title, const QString& dir, const QString& filter)
			: QFileDialog(parent, title, dir, filter)
		{
			// options panel with frame skip
			QLabel *labelFrameSkip = new QLabel("Frame Skip: ", this);
			m_spinFrameSkip = new QSpinBox(this);
			m_spinFrameSkip->setValue(10);
Tobias WEBER's avatar
Tobias WEBER committed
70
71
			m_spinFrameSkip->setSingleStep(1);
			m_spinFrameSkip->setRange(0, 9999999);
Tobias WEBER's avatar
Tobias WEBER committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

			labelFrameSkip->setSizePolicy(QSizePolicy{QSizePolicy::Fixed, QSizePolicy::Fixed});
			m_spinFrameSkip->setSizePolicy(QSizePolicy{QSizePolicy::Expanding, QSizePolicy::Fixed});

			QWidget *pPanel = new QWidget();
			auto pPanelGrid = new QGridLayout(pPanel);
			pPanelGrid->setSpacing(2);
			pPanelGrid->setContentsMargins(4,4,4,4);

			pPanelGrid->addWidget(labelFrameSkip, 0,0,1,1);
			pPanelGrid->addWidget(m_spinFrameSkip, 0,1,1,1);

			// add the options panel to the layout
			setOptions(QFileDialog::DontUseNativeDialog);
			QGridLayout *pGrid = reinterpret_cast<QGridLayout*>(layout());
			if(pGrid)
				pGrid->addWidget(pPanel, pGrid->rowCount(), 0, 1, pGrid->columnCount());
		}


		int GetFrameSkip() const
		{
			return m_spinFrameSkip->value();
		}


	private:
		QSpinBox *m_spinFrameSkip = nullptr;
};
Tobias WEBER's avatar
Tobias WEBER committed
101
// ----------------------------------------------------------------------------
Tobias WEBER's avatar
Tobias WEBER committed
102
103
104



Tobias WEBER's avatar
Tobias WEBER committed
105
106
// ----------------------------------------------------------------------------
MolDynDlg::MolDynDlg(QWidget* pParent) : QMainWindow{pParent},
Tobias WEBER's avatar
Tobias WEBER committed
107
	m_sett{new QSettings{"takin", "moldyn"}}
Tobias WEBER's avatar
Tobias WEBER committed
108
{
109
	setWindowTitle(QString(PROG_NAME) + QString(" Version ") + QString(PROG_VER));
Tobias WEBER's avatar
Tobias WEBER committed
110
111
112
	this->setObjectName("moldyn");

	m_status = new QStatusBar(this);
Tobias WEBER's avatar
Tobias WEBER committed
113
	m_statusCurAtom = new QLabel(m_status);
Tobias WEBER's avatar
Tobias WEBER committed
114
	m_statusAtoms = new QLabel(m_status);
Tobias WEBER's avatar
Tobias WEBER committed
115
	m_status->addWidget(m_statusCurAtom);
Tobias WEBER's avatar
Tobias WEBER committed
116
	m_status->addPermanentWidget(m_statusAtoms);
Tobias WEBER's avatar
Tobias WEBER committed
117
118
119
	this->setStatusBar(m_status);


Tobias WEBER's avatar
Tobias WEBER committed
120
121
122
123
124
125
126
	QWidget *pMainPanel = new QWidget();
	auto pMainGrid = new QGridLayout(pMainPanel);
	pMainGrid->setSpacing(2);
	pMainGrid->setContentsMargins(4,4,4,4);
	this->setCentralWidget(pMainPanel);


Tobias WEBER's avatar
Tobias WEBER committed
127
128
129
130
131
	// menu bar
	{
		m_menu = new QMenuBar(this);
		m_menu->setNativeMenuBar(m_sett ? m_sett->value("native_gui", false).toBool() : false);

Tobias WEBER's avatar
Tobias WEBER committed
132

Tobias WEBER's avatar
Tobias WEBER committed
133
		// File
Tobias WEBER's avatar
Tobias WEBER committed
134
135
136
137
		auto menuFile = new QMenu("File", m_menu);

		auto acNew = new QAction("New", menuFile);
		auto acLoad = new QAction("Load...", menuFile);
Tobias WEBER's avatar
Tobias WEBER committed
138
		auto acSaveAs = new QAction("Save As...", menuFile);
Tobias WEBER's avatar
Tobias WEBER committed
139
		auto acExit = new QAction("Quit", menuFile);
Tobias WEBER's avatar
Tobias WEBER committed
140
141
142
143

		menuFile->addAction(acNew);
		menuFile->addSeparator();
		menuFile->addAction(acLoad);
Tobias WEBER's avatar
Tobias WEBER committed
144
		menuFile->addAction(acSaveAs);
Tobias WEBER's avatar
Tobias WEBER committed
145
146
147
148
149
		menuFile->addSeparator();
		menuFile->addAction(acExit);

		connect(acNew, &QAction::triggered, this, &MolDynDlg::New);
		connect(acLoad, &QAction::triggered, this, &MolDynDlg::Load);
Tobias WEBER's avatar
Tobias WEBER committed
150
		connect(acSaveAs, &QAction::triggered, this, &MolDynDlg::SaveAs);
Tobias WEBER's avatar
Tobias WEBER committed
151
152
		connect(acExit, &QAction::triggered, this, &QDialog::close);

Tobias WEBER's avatar
Tobias WEBER committed
153
154

		// Edit
Tobias WEBER's avatar
Tobias WEBER committed
155
		auto menuEdit = new QMenu("Edit", m_menu);
Tobias WEBER's avatar
Tobias WEBER committed
156
		auto acSelectAll = new QAction("Select All", menuEdit);
Tobias WEBER's avatar
Tobias WEBER committed
157
158
		auto acSelectNone = new QAction("Select None", menuEdit);

Tobias WEBER's avatar
Tobias WEBER committed
159
		menuEdit->addAction(acSelectAll);
Tobias WEBER's avatar
Tobias WEBER committed
160
161
		menuEdit->addAction(acSelectNone);

Tobias WEBER's avatar
Tobias WEBER committed
162
		connect(acSelectAll, &QAction::triggered, this, &MolDynDlg::SelectAll);
Tobias WEBER's avatar
Tobias WEBER committed
163
164
165
166
167
		connect(acSelectNone, &QAction::triggered, this, &MolDynDlg::SelectNone);


		// Calculate
		auto menuCalc = new QMenu("Calculate", m_menu);
Tobias WEBER's avatar
Tobias WEBER committed
168
169
170
		auto acCalcDist = new QAction("Distance Between Selected Atoms...", menuEdit);
		auto acCalcPos = new QAction("Positions Of Selected Atoms...", menuEdit);
		auto acCalcDeltaDist = new QAction("Distances to Initial Position of Selected Atoms...", menuEdit);
Tobias WEBER's avatar
Tobias WEBER committed
171
		auto acCalcHull = new QAction("Convex Hull of Selected Atoms", menuEdit);
Tobias WEBER's avatar
Tobias WEBER committed
172
173

		menuCalc->addAction(acCalcDist);
Tobias WEBER's avatar
Tobias WEBER committed
174
175
		menuCalc->addAction(acCalcPos);
		menuCalc->addAction(acCalcDeltaDist);
176
#ifdef USE_QHULL
Tobias WEBER's avatar
Tobias WEBER committed
177
178
		menuCalc->addSeparator();
		menuCalc->addAction(acCalcHull);
179
#endif
Tobias WEBER's avatar
Tobias WEBER committed
180
181

		connect(acCalcDist, &QAction::triggered, this, &MolDynDlg::CalculateDistanceBetweenAtoms);
Tobias WEBER's avatar
Tobias WEBER committed
182
183
		connect(acCalcPos, &QAction::triggered, this, &MolDynDlg::CalculatePositionsOfAtoms);
		connect(acCalcDeltaDist, &QAction::triggered, this, &MolDynDlg::CalculateDeltaDistancesOfAtoms);
Tobias WEBER's avatar
Tobias WEBER committed
184
		connect(acCalcHull, &QAction::triggered, this, &MolDynDlg::CalculateConvexHullOfAtoms);
Tobias WEBER's avatar
Tobias WEBER committed
185
186
187
188
189
190
191
192
193
194
195
196


		// Help
		auto menuHelp = new QMenu("Help", m_menu);
		auto acHelpInfo = new QAction("Infos...", menuHelp);

		menuHelp->addAction(acHelpInfo);

		connect(acHelpInfo, &QAction::triggered, this, [this]()
		{
			QString strHelp;

197
			strHelp = QString{PROG_NAME} + " v" + QString{PROG_VER} + ", part of the Takin 2 package.\n";
Tobias WEBER's avatar
Tobias WEBER committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
			strHelp += "Written by Tobias Weber <tweber@ill.fr>\nin December 2019.\n\n";

			strHelp += "This program is free software: you can redistribute it and/or modify "
				"it under the terms of the GNU General Public License as published by "
				"the Free Software Foundation, version 3 of the License.\n\n"
				"This program is distributed in the hope that it will be useful, "
				"but WITHOUT ANY WARRANTY; without even the implied warranty of "
				"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the "
				"GNU General Public License for more details.\n\n"
				"You should have received a copy of the GNU General Public License "
				"along with this program.  If not, see <http://www.gnu.org/licenses/>.";

			QMessageBox::information(this, PROG_NAME, strHelp);
		});
Tobias WEBER's avatar
Tobias WEBER committed
212
213


Tobias WEBER's avatar
Tobias WEBER committed
214
		m_menu->addMenu(menuFile);
Tobias WEBER's avatar
Tobias WEBER committed
215
216
		m_menu->addMenu(menuEdit);
		m_menu->addMenu(menuCalc);
Tobias WEBER's avatar
Tobias WEBER committed
217
		m_menu->addMenu(menuHelp);
Tobias WEBER's avatar
Tobias WEBER committed
218
219
220
221
		this->setMenuBar(m_menu);
	}


Tobias WEBER's avatar
Tobias WEBER committed
222
223
224
225
226
	// context menus
	{
		m_atomContextMenu = new QMenu(this);
		m_atomContextMenu->setTitle("Atoms");
		m_atomContextMenu->addAction("Delete Atom", this, &MolDynDlg::DeleteAtomUnderCursor);
227
228
229
230
        m_atomContextMenu->addSeparator();
        m_atomContextMenu->addAction("Delete Selected Atoms", this, &MolDynDlg::DeleteSelectedAtoms);
        m_atomContextMenu->addAction("Delete All But Selected Atoms", this, &MolDynDlg::OnlyKeepSelectedAtoms);
        m_atomContextMenu->addSeparator();
Tobias WEBER's avatar
Tobias WEBER committed
231
232
		m_atomContextMenu->addAction("Delete All Atoms Of Selected Type", this, &MolDynDlg::DeleteAllAtomsOfSameType);
		m_atomContextMenu->addAction("Only Keep Atoms Of Selected Type", this, &MolDynDlg::KeepAtomsOfSameType);
Tobias WEBER's avatar
Tobias WEBER committed
233
234
		m_atomContextMenu->addSeparator();
		m_atomContextMenu->addAction("Select All Atoms of Same Type", this, &MolDynDlg::SelectAtomsOfSameType);
Tobias WEBER's avatar
Tobias WEBER committed
235
236
237
	}


Tobias WEBER's avatar
Tobias WEBER committed
238
239
	// plot widget
	{
Tobias WEBER's avatar
Tobias WEBER committed
240
		m_plot = new tl2::GlPlot(this);
Tobias WEBER's avatar
Tobias WEBER committed
241
242
		m_plot->setSizePolicy(QSizePolicy{QSizePolicy::Expanding, QSizePolicy::Expanding});

Tobias WEBER's avatar
Tobias WEBER committed
243
244
245
246
247
		m_plot->GetRenderer()->EnablePicker(1);
		m_plot->GetRenderer()->SetLight(0, tl2::create<t_vec3_gl>({ 5, 5, 5 }));
		m_plot->GetRenderer()->SetLight(1, tl2::create<t_vec3_gl>({ -5, -5, -5 }));
		m_plot->GetRenderer()->SetCoordMax(1.);
		m_plot->GetRenderer()->SetCamBase(tl2::create<t_mat_gl>({1,0,0,0,  0,0,1,0,  0,-1,0,-1.5,  0,0,0,1}),
Tobias WEBER's avatar
Tobias WEBER committed
248
			tl2::create<t_vec_gl>({1,0,0,0}), tl2::create<t_vec_gl>({0,0,1,0}));
Tobias WEBER's avatar
Tobias WEBER committed
249

Tobias WEBER's avatar
Tobias WEBER committed
250
251
252
253
254
255
		connect(m_plot, &tl2::GlPlot::AfterGLInitialisation, this, &MolDynDlg::AfterGLInitialisation);
		connect(m_plot, &tl2::GlPlot::GLInitialisationFailed, this, &MolDynDlg::GLInitialisationFailed);
		connect(m_plot->GetRenderer(), &tl2::GlPlotRenderer::PickerIntersection, this, &MolDynDlg::PickerIntersection);
		connect(m_plot, &tl2::GlPlot::MouseDown, this, &MolDynDlg::PlotMouseDown);
		connect(m_plot, &tl2::GlPlot::MouseUp, this, &MolDynDlg::PlotMouseUp);
		connect(m_plot, &tl2::GlPlot::MouseClick, this, &MolDynDlg::PlotMouseClick);
Tobias WEBER's avatar
Tobias WEBER committed
256

Tobias WEBER's avatar
Tobias WEBER committed
257
		//this->setCentralWidget(m_plot);
Tobias WEBER's avatar
Tobias WEBER committed
258
		pMainGrid->addWidget(m_plot, 0,0,1,9);
Tobias WEBER's avatar
Tobias WEBER committed
259
260
261
262
263
	}


	// controls
	{
Tobias WEBER's avatar
Tobias WEBER committed
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
		auto labCoordSys = new QLabel("Coordinates:", this);
		auto labFrames = new QLabel("Frames:", this);
		auto labScale = new QLabel("Scale:", this);
		labCoordSys->setSizePolicy(QSizePolicy{QSizePolicy::Fixed, QSizePolicy::Fixed});
		labFrames->setSizePolicy(QSizePolicy{QSizePolicy::Fixed, QSizePolicy::Fixed});
		labScale->setSizePolicy(QSizePolicy{QSizePolicy::Fixed, QSizePolicy::Fixed});

		auto comboCoordSys = new QComboBox(this);
		comboCoordSys->addItem("Fractional Units (rlu)");
		comboCoordSys->addItem("Lab Units (A)");
		comboCoordSys->setFocusPolicy(Qt::StrongFocus);

		m_spinScale = new QDoubleSpinBox(this);
		m_spinScale->setDecimals(4);
		m_spinScale->setRange(1e-4, 1e4);
		m_spinScale->setSingleStep(0.1);
		m_spinScale->setValue(0.4);
		m_spinScale->setFocusPolicy(Qt::StrongFocus);

Tobias WEBER's avatar
Tobias WEBER committed
283
284
285
286
287
288
289
		m_sliderFrame = new QSlider(Qt::Horizontal, this);
		m_sliderFrame->setSizePolicy(QSizePolicy{QSizePolicy::Expanding, QSizePolicy::Minimum});
		m_sliderFrame->setMinimum(0);
		m_sliderFrame->setSingleStep(1);
		m_sliderFrame->setPageStep(10);
		m_sliderFrame->setTracking(1);
		m_sliderFrame->setFocusPolicy(Qt::StrongFocus);
Tobias WEBER's avatar
Tobias WEBER committed
290

Tobias WEBER's avatar
Tobias WEBER committed
291
		connect(m_sliderFrame, &QSlider::valueChanged, this, &MolDynDlg::SliderValueChanged);
Tobias WEBER's avatar
Tobias WEBER committed
292
293
294
		connect(comboCoordSys, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, [this](int val)
		{
			if(this->m_plot)
Tobias WEBER's avatar
Tobias WEBER committed
295
				this->m_plot->GetRenderer()->SetCoordSys(val);
Tobias WEBER's avatar
Tobias WEBER committed
296
297
298
299
300
301
		});
		connect(m_spinScale, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged), this, [this](double val)
		{
			if(!this->m_plot) return;

			// hack to trigger update
Tobias WEBER's avatar
Tobias WEBER committed
302
			SliderValueChanged(m_sliderFrame->value());
Tobias WEBER's avatar
Tobias WEBER committed
303
304
305
306
307
308
309
		});

		pMainGrid->addWidget(labCoordSys, 1,0,1,1);
		pMainGrid->addWidget(comboCoordSys, 1,1,1,1);
		pMainGrid->addWidget(labScale, 1,2,1,1);
		pMainGrid->addWidget(m_spinScale, 1,3,1,1);
		pMainGrid->addWidget(labFrames, 1,4,1,1);
Tobias WEBER's avatar
Tobias WEBER committed
310
		pMainGrid->addWidget(m_sliderFrame, 1,5,1,4);
Tobias WEBER's avatar
Tobias WEBER committed
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
	}


	// restore window size and position
	if(m_sett && m_sett->contains("geo"))
		restoreGeometry(m_sett->value("geo").toByteArray());
	else
		resize(600, 500);

	m_ignoreChanges = 0;
}



// ----------------------------------------------------------------------------
/**
Tobias WEBER's avatar
Tobias WEBER committed
327
 * add an atom
Tobias WEBER's avatar
Tobias WEBER committed
328
 */
Tobias WEBER's avatar
Tobias WEBER committed
329
std::size_t MolDynDlg::Add3DAtom(const t_vec& vec, const t_vec& col, t_real scale, const std::string& typelabel, int atomindex)
Tobias WEBER's avatar
Tobias WEBER committed
330
{
Tobias WEBER's avatar
Tobias WEBER committed
331
	auto obj = m_plot->GetRenderer()->AddLinkedObject(m_sphere, 0,0,0, col[0],col[1],col[2],1);
Tobias WEBER's avatar
Tobias WEBER committed
332
	Change3DAtom(obj, &vec, &col, &scale, &typelabel, atomindex);
333
	return obj;
Tobias WEBER's avatar
Tobias WEBER committed
334
}
Tobias WEBER's avatar
Tobias WEBER committed
335
336
337
338
339


/**
 * change an atom
 */
Tobias WEBER's avatar
Tobias WEBER committed
340
void MolDynDlg::Change3DAtom(std::size_t obj, const t_vec *vec, const t_vec *col, const t_real *scale,
341
	const std::string *label, int atomindex)
Tobias WEBER's avatar
Tobias WEBER committed
342
{
Tobias WEBER's avatar
Tobias WEBER committed
343
344
	if(vec)
	{
Tobias WEBER's avatar
Tobias WEBER committed
345
346
347
348
		t_mat_gl mat = tl2::hom_translation<t_mat_gl>(
			t_real_gl((*vec)[0]), t_real_gl((*vec)[1]), t_real_gl((*vec)[2]));
		if(scale) mat *= tl2::hom_scaling<t_mat_gl>(
			t_real_gl(*scale), t_real_gl(*scale), t_real_gl(*scale));
Tobias WEBER's avatar
Tobias WEBER committed
349
		m_plot->GetRenderer()->SetObjectMatrix(obj, mat);
Tobias WEBER's avatar
Tobias WEBER committed
350
	}
Tobias WEBER's avatar
Tobias WEBER committed
351

Tobias WEBER's avatar
Tobias WEBER committed
352
	if(col) m_plot->GetRenderer()->SetObjectCol(obj, (*col)[0], (*col)[1], (*col)[2], 1);
353
354
355
356
357
	if(label)
	{
		std::ostringstream ostrData;
		ostrData << *label << DATA_SEP << atomindex;

Tobias WEBER's avatar
Tobias WEBER committed
358
359
		m_plot->GetRenderer()->SetObjectLabel(obj, *label);
		m_plot->GetRenderer()->SetObjectDataString(obj, ostrData.str());
360
	}
Tobias WEBER's avatar
Tobias WEBER committed
361
}
Tobias WEBER's avatar
Tobias WEBER committed
362
363
364
365
// ----------------------------------------------------------------------------



Tobias WEBER's avatar
Tobias WEBER committed
366
// ----------------------------------------------------------------------------
Tobias WEBER's avatar
Tobias WEBER committed
367
368
369
370
371
372
373
374
375

std::vector<std::tuple<std::size_t, std::size_t>> MolDynDlg::GetSelectedAtoms()
{
	// get selected atoms
	std::vector<std::tuple<std::size_t, std::size_t>> objs;

	for(const auto& obj : m_sphereHandles)
	{
		// continue if object isn't selected
Tobias WEBER's avatar
Tobias WEBER committed
376
		if(!m_plot->GetRenderer()->GetObjectHighlight(obj))
Tobias WEBER's avatar
Tobias WEBER committed
377
378
			continue;

Tobias WEBER's avatar
Tobias WEBER committed
379
		//const auto [typelabel, _atomSubTypeIdx] = SplitDataString(m_plot->GetRenderer()->GetObjectDataString(obj));
Tobias WEBER's avatar
Tobias WEBER committed
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395

		// get indices for selected atoms
		const auto [bOk, atomTypeIdx, atomSubTypeIdx, sphereIdx] = GetAtomIndexFromHandle(obj);
		if(!bOk /*|| _atomSubTypeIdx != atomSubTypeIdx*/)
		{
			QMessageBox::critical(this, PROG_NAME, "Atom handle not found, data is corrupted.");
			return std::vector<std::tuple<std::size_t, std::size_t>>{};
		}

		objs.emplace_back(std::make_tuple(atomTypeIdx, atomSubTypeIdx));
	}

	return objs;
}


Tobias WEBER's avatar
Tobias WEBER committed
396
397
398
399
400
/**
 * calculate the distance between selected atoms
 */
void MolDynDlg::CalculateDistanceBetweenAtoms()
{
Tobias WEBER's avatar
Tobias WEBER committed
401
	try
Tobias WEBER's avatar
Tobias WEBER committed
402
	{
Tobias WEBER's avatar
Tobias WEBER committed
403
		// get selected atoms
Tobias WEBER's avatar
Tobias WEBER committed
404
		std::vector<std::tuple<std::size_t, std::size_t>> objs = GetSelectedAtoms();
Tobias WEBER's avatar
Tobias WEBER committed
405

Tobias WEBER's avatar
Tobias WEBER committed
406
		if(objs.size() <= 1)
Tobias WEBER's avatar
Tobias WEBER committed
407
		{
Tobias WEBER's avatar
Tobias WEBER committed
408
			QMessageBox::critical(this, PROG_NAME, "At least two atoms have to be selected.");
Tobias WEBER's avatar
Tobias WEBER committed
409
410
411
412
			return;
		}


Tobias WEBER's avatar
Tobias WEBER committed
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
		// create file
		QString dirLast = m_sett->value("dir", "").toString();
		QString filename = QFileDialog::getSaveFileName(this, "Save File", dirLast, "Data File (*.dat)");
		if(filename == "")
			return;

		std::ofstream ofstr(filename.toStdString());
		if(!ofstr)
		{
			QMessageBox::critical(this, PROG_NAME, "Cannot open file.");
			return;
		}

		ofstr.precision(g_prec);
		m_sett->setValue("dir", QFileInfo(filename).path());


		// get coordinates of first atom
		auto [firstObjTypeIdx, firstObjSubTypeIdx] = objs[0];
		auto firstObjCoords = m_mol.GetAtomCoords(firstObjTypeIdx, firstObjSubTypeIdx);


435
436
		// output data file header infos
		ofstr << "#\n";
Tobias WEBER's avatar
Tobias WEBER committed
437
438
		ofstr << "# Column 1: Frame\n";
		ofstr << "# Columns 2, 3, ...: Distances to first atom (A)\n";
439
440
441
442
443
444
445
446
447
448
449
		ofstr << "# Atoms: ";

		for(std::size_t objIdx=0; objIdx<objs.size(); ++objIdx)
		{
			auto [objTypeIdx, objSubTypeIdx] = objs[objIdx];
			ofstr << m_mol.GetAtomName(objTypeIdx) << "#" << (objSubTypeIdx+1);
			if(objIdx < objs.size()-1)
				ofstr << ", ";
		}
		ofstr << "\n#\n";

Tobias WEBER's avatar
Tobias WEBER committed
450
451
452
453
454
455
456
457
458
459
460

		// progress dialog
		std::shared_ptr<QProgressDialog> dlgProgress = std::make_shared<QProgressDialog>(
			"Calculating...", "Cancel", 1, objs.size(), this);
		dlgProgress->setWindowModality(Qt::WindowModal);

		// get distances to other selected atoms
		for(std::size_t objIdx=1; objIdx<objs.size(); ++objIdx)
		{
			auto [objTypeIdx, objSubTypeIdx] = objs[objIdx];
			const auto objCoords = m_mol.GetAtomCoords(objTypeIdx, objSubTypeIdx);
Tobias WEBER's avatar
Tobias WEBER committed
461

Tobias WEBER's avatar
Tobias WEBER committed
462
463
			for(std::size_t frameidx=0; frameidx<m_mol.GetFrameCount(); ++frameidx)
			{
Tobias WEBER's avatar
Tobias WEBER committed
464
				t_real dist = tl2::get_dist_uc(m_crystA, firstObjCoords[frameidx], objCoords[frameidx]);
Tobias WEBER's avatar
Tobias WEBER committed
465

Tobias WEBER's avatar
Tobias WEBER committed
466
				ofstr
Tobias WEBER's avatar
Tobias WEBER committed
467
468
469
470
471
472
473
474
475
476
477
478
479
					<< std::left << std::setw(g_prec*1.5) << frameidx << " "
					<< std::left << std::setw(g_prec*1.5) << dist << "\n";
			}

			dlgProgress->setValue(objIdx+1);
			if(dlgProgress->wasCanceled())
			{
				ofstr << "\n# WARNING: Calculation aborted by user.\n";
				break;
			}
		}
	}
	catch(const std::exception& ex)
Tobias WEBER's avatar
Tobias WEBER committed
480
	{
Tobias WEBER's avatar
Tobias WEBER committed
481
		QMessageBox::critical(this, PROG_NAME, ex.what());
Tobias WEBER's avatar
Tobias WEBER committed
482
	}
Tobias WEBER's avatar
Tobias WEBER committed
483
484
485
486
487
488
489
490
491
492
493
}


/**
 * calculate positions of selected atoms
 */
void MolDynDlg::CalculatePositionsOfAtoms()
{
	try
	{
		// get selected atoms
Tobias WEBER's avatar
Tobias WEBER committed
494
		std::vector<std::tuple<std::size_t, std::size_t>> objs = GetSelectedAtoms();
Tobias WEBER's avatar
Tobias WEBER committed
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517

		if(objs.size() <= 0)
		{
			QMessageBox::critical(this, PROG_NAME, "At least one atom has to be selected.");
			return;
		}


		// create file
		QString dirLast = m_sett->value("dir", "").toString();
		QString filename = QFileDialog::getSaveFileName(this, "Save File", dirLast, "Data File (*.dat)");
		if(filename == "")
			return;

		std::ofstream ofstr(filename.toStdString());
		if(!ofstr)
		{
			QMessageBox::critical(this, PROG_NAME, "Cannot open file.");
			return;
		}

		ofstr.precision(g_prec);
		m_sett->setValue("dir", QFileInfo(filename).path());
Tobias WEBER's avatar
Tobias WEBER committed
518
519


520
521
		// output data file header infos
		ofstr << "#\n";
Tobias WEBER's avatar
Tobias WEBER committed
522
523
		ofstr << "# Column 1: Frame\n";
		ofstr << "# Columns 2, 3, 4: x, y, z position of atom  (A)\n";
524
525
526
527
528
529
530
531
532
533
534
		ofstr << "# Atoms: ";

		for(std::size_t objIdx=0; objIdx<objs.size(); ++objIdx)
		{
			auto [objTypeIdx, objSubTypeIdx] = objs[objIdx];
			ofstr << m_mol.GetAtomName(objTypeIdx) << "#" << (objSubTypeIdx+1);
			if(objIdx < objs.size()-1)
				ofstr << ", ";
		}
		ofstr << "\n#\n";

Tobias WEBER's avatar
Tobias WEBER committed
535
536
537
538
539

		// progress dialog
		std::shared_ptr<QProgressDialog> dlgProgress = std::make_shared<QProgressDialog>(
			"Calculating...", "Cancel", 0, m_mol.GetFrameCount(), this);
		dlgProgress->setWindowModality(Qt::WindowModal);
Tobias WEBER's avatar
Tobias WEBER committed
540

Tobias WEBER's avatar
Tobias WEBER committed
541
542
543
544
		// iterate all selected atoms
		for(std::size_t frameidx=0; frameidx<m_mol.GetFrameCount(); ++frameidx)
		{
			ofstr << std::left << std::setw(g_prec*1.5) << frameidx << " ";
Tobias WEBER's avatar
Tobias WEBER committed
545

Tobias WEBER's avatar
Tobias WEBER committed
546
547
548
549
			for(std::size_t objIdx=0; objIdx<objs.size(); ++objIdx)
			{
				auto [objTypeIdx, objSubTypeIdx] = objs[objIdx];
				const t_vec& coords = m_mol.GetAtomCoords(objTypeIdx, objSubTypeIdx, frameidx);
Tobias WEBER's avatar
Tobias WEBER committed
550

551
				ofstr
Tobias WEBER's avatar
Tobias WEBER committed
552
553
554
555
					<< std::left << std::setw(g_prec*1.5) << coords[0] << " "
					<< std::left << std::setw(g_prec*1.5) << coords[1] << " "
					<< std::left << std::setw(g_prec*1.5) << coords[2] << "  ";
			}
Tobias WEBER's avatar
Tobias WEBER committed
556

Tobias WEBER's avatar
Tobias WEBER committed
557
			ofstr << "\n";
Tobias WEBER's avatar
Tobias WEBER committed
558

Tobias WEBER's avatar
Tobias WEBER committed
559
560
561
562
563
564
565
566
567
			dlgProgress->setValue(frameidx+1);
			if(dlgProgress->wasCanceled())
			{
				ofstr << "\n# WARNING: Calculation aborted by user.\n";
				break;
			}
		}
	}
	catch(const std::exception& ex)
Tobias WEBER's avatar
Tobias WEBER committed
568
	{
Tobias WEBER's avatar
Tobias WEBER committed
569
570
571
572
573
574
575
576
577
578
579
580
581
		QMessageBox::critical(this, PROG_NAME, ex.what());
	}
}


/**
 * calculate distance to initial positions of selected atoms
 */
void MolDynDlg::CalculateDeltaDistancesOfAtoms()
{
	try
	{
		// get selected atoms
Tobias WEBER's avatar
Tobias WEBER committed
582
		std::vector<std::tuple<std::size_t, std::size_t>> objs = GetSelectedAtoms();
Tobias WEBER's avatar
Tobias WEBER committed
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607

		if(objs.size() <= 0)
		{
			QMessageBox::critical(this, PROG_NAME, "At least one atom has to be selected.");
			return;
		}


		// create file
		QString dirLast = m_sett->value("dir", "").toString();
		QString filename = QFileDialog::getSaveFileName(this, "Save File", dirLast, "Data File (*.dat)");
		if(filename == "")
			return;

		std::ofstream ofstr(filename.toStdString());
		if(!ofstr)
		{
			QMessageBox::critical(this, PROG_NAME, "Cannot open file.");
			return;
		}

		ofstr.precision(g_prec);
		m_sett->setValue("dir", QFileInfo(filename).path());


608
609
		// output data file header infos
		ofstr << "#\n";
Tobias WEBER's avatar
Tobias WEBER committed
610
		ofstr << "# Column 1: Frame\n";
611
612
613
614
615
616
617
618
619
620
621
		ofstr << "# Column 2, 3, ...: Distance deltas (A)\n";
		ofstr << "# Atoms: ";

		for(std::size_t objIdx=0; objIdx<objs.size(); ++objIdx)
		{
			auto [objTypeIdx, objSubTypeIdx] = objs[objIdx];
			ofstr << m_mol.GetAtomName(objTypeIdx) << "#" << (objSubTypeIdx+1);
			if(objIdx < objs.size()-1)
				ofstr << ", ";
		}
		ofstr << "\n#\n";
Tobias WEBER's avatar
Tobias WEBER committed
622

Tobias WEBER's avatar
Tobias WEBER committed
623
624
625
626
627
628
629

		// progress dialog
		std::shared_ptr<QProgressDialog> dlgProgress = std::make_shared<QProgressDialog>(
			"Calculating...", "Cancel", 0, m_mol.GetFrameCount(), this);
		dlgProgress->setWindowModality(Qt::WindowModal);

		// iterate all selected atoms
Tobias WEBER's avatar
Tobias WEBER committed
630
631
		for(std::size_t frameidx=0; frameidx<m_mol.GetFrameCount(); ++frameidx)
		{
Tobias WEBER's avatar
Tobias WEBER committed
632
633
634
635
636
637
638
639
			ofstr << std::left << std::setw(g_prec*1.5) << frameidx << " ";

			for(std::size_t objIdx=0; objIdx<objs.size(); ++objIdx)
			{
				auto [objTypeIdx, objSubTypeIdx] = objs[objIdx];
			 	const t_vec& coords = m_mol.GetAtomCoords(objTypeIdx, objSubTypeIdx, frameidx);
				const t_vec& coordsInitial = m_mol.GetAtomCoords(objTypeIdx, objSubTypeIdx, 0);

Tobias WEBER's avatar
Tobias WEBER committed
640
				t_real dist = tl2::get_dist_uc(m_crystA, coords, coordsInitial);
Tobias WEBER's avatar
Tobias WEBER committed
641

Tobias WEBER's avatar
Tobias WEBER committed
642
643
644
645
646
647
648
649
650
651
652
				ofstr << std::left << std::setw(g_prec*1.5) << dist << " ";
			}

			ofstr << "\n";

			dlgProgress->setValue(frameidx+1);
			if(dlgProgress->wasCanceled())
			{
				ofstr << "\n# WARNING: Calculation aborted by user.\n";
				break;
			}
Tobias WEBER's avatar
Tobias WEBER committed
653
654
		}
	}
Tobias WEBER's avatar
Tobias WEBER committed
655
656
657
658
	catch(const std::exception& ex)
	{
		QMessageBox::critical(this, PROG_NAME, ex.what());
	}
Tobias WEBER's avatar
Tobias WEBER committed
659
}
Tobias WEBER's avatar
Tobias WEBER committed
660
661
662
663
664
665
666
667


/**
 * calculate the convex hull of selected atoms
 */
void MolDynDlg::CalculateConvexHullOfAtoms()
{
	// get selected atoms
668
	Hull hull;
Tobias WEBER's avatar
Tobias WEBER committed
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
	hull.vertices = GetSelectedAtoms();

	if(hull.vertices.size() <= 3)
	{
		QMessageBox::critical(this, PROG_NAME, "At least four atoms have to be selected.");
		return;
	}

	// mark indices for hull calculation
	m_hulls.emplace_back(std::move(hull));
	CalculateConvexHulls();
}


/**
 * calculate all convex hulls for the atom indices given in m_hulls
 */
void MolDynDlg::CalculateConvexHulls()
{
688
#ifdef USE_QHULL
Tobias WEBER's avatar
Tobias WEBER committed
689
690
	std::size_t frameidx = m_sliderFrame->value();

691
	for(auto& hull : m_hulls)
Tobias WEBER's avatar
Tobias WEBER committed
692
	{
693
694
695
		// remove old plot object
		if(hull.plotObj)
		{
Tobias WEBER's avatar
Tobias WEBER committed
696
			m_plot->GetRenderer()->RemoveObject(*hull.plotObj);
697
698
699
			hull.plotObj.reset();
		}

Tobias WEBER's avatar
Tobias WEBER committed
700
701
702
703
704
705
706
707
708
		std::vector<t_vec> vertices;

		for(const auto [objTypeIdx, objSubTypeIdx] : hull.vertices)
		{
			const t_vec& coords = m_mol.GetAtomCoords(objTypeIdx, objSubTypeIdx, frameidx);
			vertices.push_back(coords);
		}


Tobias WEBER's avatar
Tobias WEBER committed
709
		auto [polys, normals, dists] = tl2_qh::get_convexhull<t_vec>(vertices);
Tobias WEBER's avatar
Tobias WEBER committed
710
711
712
713
714
715


		std::vector<t_vec3_gl> glvertices;
		std::vector<t_vec3_gl> glnormals;

		for(std::size_t polyidx=0; polyidx<polys.size(); ++polyidx)
Tobias WEBER's avatar
Tobias WEBER committed
716
		{
Tobias WEBER's avatar
Tobias WEBER committed
717
718
719
			const auto& poly = polys[polyidx];
			const auto& normal = normals[polyidx];

Tobias WEBER's avatar
Tobias WEBER committed
720
			for(const auto& vert : poly)
Tobias WEBER's avatar
Tobias WEBER committed
721
722
723
				glvertices.emplace_back(tl2::convert<t_vec3_gl, t_vec>(vert));

			glnormals.emplace_back(tl2::convert<t_vec3_gl, t_vec>(normal));
Tobias WEBER's avatar
Tobias WEBER committed
724
725
		}

Tobias WEBER's avatar
Tobias WEBER committed
726
		hull.plotObj = m_plot->GetRenderer()->AddTriangleObject(glvertices, glnormals, 0,0,1,0.5);
Tobias WEBER's avatar
Tobias WEBER committed
727

Tobias WEBER's avatar
Tobias WEBER committed
728
729
		// TODO
	}
730
731
732
733

#else
	QMessageBox::critical(this, "Error", "Calculation of convex hull is disabled.");
#endif
Tobias WEBER's avatar
Tobias WEBER committed
734
}
Tobias WEBER's avatar
Tobias WEBER committed
735
736
737
// ----------------------------------------------------------------------------


Tobias WEBER's avatar
Tobias WEBER committed
738

Tobias WEBER's avatar
Tobias WEBER committed
739

Tobias WEBER's avatar
Tobias WEBER committed
740
741
742
// ----------------------------------------------------------------------------
void MolDynDlg::New()
{
Tobias WEBER's avatar
Tobias WEBER committed
743
	m_mol.Clear();
Tobias WEBER's avatar
Tobias WEBER committed
744

745
746
747
	for(auto& hull : m_hulls)
	{
		if(hull.plotObj)
Tobias WEBER's avatar
Tobias WEBER committed
748
			m_plot->GetRenderer()->RemoveObject(*hull.plotObj);
749
	}
Tobias WEBER's avatar
Tobias WEBER committed
750

751
	m_hulls.clear();
Tobias WEBER's avatar
Tobias WEBER committed
752
753

	for(const auto& obj : m_sphereHandles)
Tobias WEBER's avatar
Tobias WEBER committed
754
		m_plot->GetRenderer()->RemoveObject(obj);
Tobias WEBER's avatar
Tobias WEBER committed
755

756
	m_sphereHandles.clear();
Tobias WEBER's avatar
Tobias WEBER committed
757
	m_sliderFrame->setValue(0);
Tobias WEBER's avatar
Tobias WEBER committed
758
759

	m_plot->update();
Tobias WEBER's avatar
Tobias WEBER committed
760
761
762
763
764
}


void MolDynDlg::Load()
{
765
766
	if(!m_plot) return;

Tobias WEBER's avatar
Tobias WEBER committed
767
768
769
	try
	{
		QString dirLast = m_sett->value("dir", "").toString();
Tobias WEBER's avatar
Tobias WEBER committed
770
771
772
773
774
775
		auto filedlg = std::make_shared<MolDynFileDlg>(this, "Load File", dirLast, "Molecular Dynamics File (*)");
		if(!filedlg->exec())
			return;
		auto files = filedlg->selectedFiles();
		if(!files.size())
			return;
Tobias WEBER's avatar
Tobias WEBER committed
776

Tobias WEBER's avatar
Tobias WEBER committed
777
		QString filename = files[0];
Tobias WEBER's avatar
Tobias WEBER committed
778
		if(filename == "" || !QFile::exists(filename))
Tobias WEBER's avatar
Tobias WEBER committed
779
780
781
			return;
		m_sett->setValue("dir", QFileInfo(filename).path());

Tobias WEBER's avatar
Tobias WEBER committed
782
		New();
783

Tobias WEBER's avatar
Tobias WEBER committed
784

785
786
787
		std::shared_ptr<QProgressDialog> dlgProgress = std::make_shared<QProgressDialog>(
			"Loading \"" + QFileInfo(filename).fileName() + "\"...", "Cancel", 0, 1000, this);
		bool bCancelled = 0;
Tobias WEBER's avatar
Tobias WEBER committed
788
		auto progressHandler = [dlgProgress, &bCancelled](t_real percentage) -> bool
789
790
791
792
		{
			dlgProgress->setValue(int(percentage*10));
			bCancelled = dlgProgress->wasCanceled();
			return !bCancelled;
Tobias WEBER's avatar
Tobias WEBER committed
793
794
		};
		m_mol.SubscribeToLoadProgress(progressHandler);
795
796
		dlgProgress->setWindowModality(Qt::WindowModal);

Tobias WEBER's avatar
Tobias WEBER committed
797

Tobias WEBER's avatar
Tobias WEBER committed
798
		if(!m_mol.LoadFile(filename.toStdString(), filedlg->GetFrameSkip()))
Tobias WEBER's avatar
Tobias WEBER committed
799
		{
800
801
			// only show error if not explicitely cancelled
			if(!bCancelled)
Tobias WEBER's avatar
Tobias WEBER committed
802
				QMessageBox::critical(this, PROG_NAME, "Error loading file.");
Tobias WEBER's avatar
Tobias WEBER committed
803
			return;
Tobias WEBER's avatar
Tobias WEBER committed
804
805
		}

Tobias WEBER's avatar
Tobias WEBER committed
806
807

		m_mol.UnsubscribeFromLoadProgress(&progressHandler);
Tobias WEBER's avatar
Tobias WEBER committed
808
		m_sliderFrame->setMaximum(m_mol.GetFrameCount() - 1);
Tobias WEBER's avatar
Tobias WEBER committed
809

Tobias WEBER's avatar
Tobias WEBER committed
810

Tobias WEBER's avatar
Tobias WEBER committed
811
812
813
814
815
		// crystal A and B matrices
		const t_vec& _a = m_mol.GetBaseA();
		const t_vec& _b = m_mol.GetBaseB();
		const t_vec& _c = m_mol.GetBaseC();

Tobias WEBER's avatar
Tobias WEBER committed
816
		m_crystA = tl2::create<t_mat>({
Tobias WEBER's avatar
Tobias WEBER committed
817
818
819
820
821
			_a[0],	_b[0],	_c[0],
			_a[1],	_b[1],	_c[1],
			_a[2], 	_b[2],	_c[2] });

		bool ok = true;
Tobias WEBER's avatar
Tobias WEBER committed
822
		std::tie(m_crystB, ok) = tl2::inv(m_crystA);
Tobias WEBER's avatar
Tobias WEBER committed
823
824
		if(!ok)
		{
Tobias WEBER's avatar
Tobias WEBER committed
825
			m_crystB = tl2::unit<t_mat>();
Tobias WEBER's avatar
Tobias WEBER committed
826
			QMessageBox::critical(this, PROG_NAME, "Error: Cannot invert A matrix.");
Tobias WEBER's avatar
Tobias WEBER committed
827
828
		}

Tobias WEBER's avatar
Tobias WEBER committed
829
		m_crystB /= t_real_gl(2)*tl2::pi<t_real_gl>;
Tobias WEBER's avatar
Tobias WEBER committed
830
		t_mat_gl matA{m_crystA};
Tobias WEBER's avatar
Tobias WEBER committed
831
		m_plot->GetRenderer()->SetBTrafo(m_crystB, &matA);
Tobias WEBER's avatar
Tobias WEBER committed
832
833
834
835
836

		std::cout << "A matrix: " << m_crystA << ", \n"
			<< "B matrix: " << m_crystB << "." << std::endl;


Tobias WEBER's avatar
Tobias WEBER committed
837
		// atom colors
Tobias WEBER's avatar
Tobias WEBER committed
838
839
		std::vector<t_vec> cols =
		{
Tobias WEBER's avatar
Tobias WEBER committed
840
841
842
843
844
845
			tl2::create<t_vec>({1, 0, 0}),
			tl2::create<t_vec>({0, 0, 1}),
			tl2::create<t_vec>({0, 0.5, 0}),
			tl2::create<t_vec>({0, 0.5, 0.5}),
			tl2::create<t_vec>({0.5, 0.5, 0}),
			tl2::create<t_vec>({0, 0, 0}),
Tobias WEBER's avatar
Tobias WEBER committed
846
847
		};

Tobias WEBER's avatar
Tobias WEBER committed
848
		// add atoms to 3d view
Tobias WEBER's avatar
Tobias WEBER committed
849
850
851
		if(m_mol.GetFrameCount())
		{
			const auto& frame = m_mol.GetFrame(0);
Tobias WEBER's avatar
Tobias WEBER committed
852
			m_sphereHandles.reserve(frame.GetNumAtomTypes());
Tobias WEBER's avatar
Tobias WEBER committed
853

854
			for(std::size_t atomtypeidx=0; atomtypeidx<frame.GetNumAtomTypes(); ++atomtypeidx)
Tobias WEBER's avatar
Tobias WEBER committed
855
			{
856
857
858
				const auto& coords = frame.GetCoords(atomtypeidx);

				int atomidx = 0;
Tobias WEBER's avatar
Tobias WEBER committed
859
				for(const t_vec& vec : coords)
860
				{
Tobias WEBER's avatar
Tobias WEBER committed
861
					t_real atomscale = m_spinScale->value();
862

863
					std::size_t handle = Add3DAtom(vec, cols[atomtypeidx % cols.size()], atomscale,
864
						m_mol.GetAtomName(atomtypeidx), atomidx);
865
					m_sphereHandles.push_back(handle);
866
867

					++atomidx;
868
				}
Tobias WEBER's avatar
Tobias WEBER committed
869
870
871
872
873
			}
		}
	}
	catch(const std::exception& ex)
	{
Tobias WEBER's avatar
Tobias WEBER committed
874
		QMessageBox::critical(this, PROG_NAME, ex.what());
Tobias WEBER's avatar
Tobias WEBER committed
875
876
	}

Tobias WEBER's avatar
Tobias WEBER committed
877

Tobias WEBER's avatar
Tobias WEBER committed
878
	UpdateAtomsStatusMsg();
Tobias WEBER's avatar
Tobias WEBER committed
879
	m_plot->update();
Tobias WEBER's avatar
Tobias WEBER committed
880
881
882
}


Tobias WEBER's avatar
Tobias WEBER committed
883
void MolDynDlg::SaveAs()
Tobias WEBER's avatar
Tobias WEBER committed
884
{
Tobias WEBER's avatar
Tobias WEBER committed
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
	try
	{
		QString dirLast = m_sett->value("dir", "").toString();
		QString filename = QFileDialog::getSaveFileName(this, "Save File", dirLast, "Molecular Dynamics File (*)");
		if(filename == "")
			return;
		m_sett->setValue("dir", QFileInfo(filename).path());


		std::shared_ptr<QProgressDialog> dlgProgress = std::make_shared<QProgressDialog>(
			"Saving \"" + QFileInfo(filename).fileName() + "\"...", "Cancel", 0, 1000, this);
		bool bCancelled = 0;
		auto progressHandler = [dlgProgress, &bCancelled](t_real percentage) -> bool
		{
			dlgProgress->setValue(int(percentage*10));
			bCancelled = dlgProgress->wasCanceled();
			return !bCancelled;
		};
		m_mol.SubscribeToSaveProgress(progressHandler);
		dlgProgress->setWindowModality(Qt::WindowModal);


		if(!m_mol.SaveFile(filename.toStdString()))
		{
Tobias WEBER's avatar
Tobias WEBER committed
909
			QMessageBox::critical(this, PROG_NAME, "Error saving file.");
Tobias WEBER's avatar
Tobias WEBER committed
910
911
912
913
914
915
916
		}


		m_mol.UnsubscribeFromSaveProgress(&progressHandler);
	}
	catch(const std::exception& ex)
	{
Tobias WEBER's avatar
Tobias WEBER committed
917
		QMessageBox::critical(this, PROG_NAME, ex.what());
Tobias WEBER's avatar
Tobias WEBER committed
918
	}
Tobias WEBER's avatar
Tobias WEBER committed
919
920
921
922
923
924
925
926
927
928
929
}
// ----------------------------------------------------------------------------



// ----------------------------------------------------------------------------
/**
 * mouse hovers over 3d object
 */
void MolDynDlg::PickerIntersection(const t_vec3_gl* pos, std::size_t objIdx, const t_vec3_gl* posSphere)
{
930
931
	if(!m_plot) return;

Tobias WEBER's avatar
Tobias WEBER committed
932
933
934
935
936
937
938
939
	if(pos)
		m_curPickedObj = long(objIdx);
	else
		m_curPickedObj = -1;


	if(m_curPickedObj > 0)
	{
Tobias WEBER's avatar
Tobias WEBER committed
940
		const auto [typelabel, atomidx] = SplitDataString(m_plot->GetRenderer()->GetObjectDataString(m_curPickedObj));
941
942
943

		std::ostringstream ostrLabel;
		ostrLabel << "Current Atom: " << typelabel << " #" << (atomidx+1);
Tobias WEBER's avatar
Tobias WEBER committed
944

945
		m_statusCurAtom->setText(ostrLabel.str().c_str());
Tobias WEBER's avatar
Tobias WEBER committed
946
		//SetStatusMsg(label);
Tobias WEBER's avatar
Tobias WEBER committed
947
948
949
	}
	else
	{
Tobias WEBER's avatar
Tobias WEBER committed
950
951
		m_statusCurAtom->setText("");
		//SetStatusMsg("");
Tobias WEBER's avatar
Tobias WEBER committed
952
953
954
955
956
957
958
959
	}
}



/**
 * set status label text in 3d dialog
 */
Tobias WEBER's avatar
Tobias WEBER committed
960
void MolDynDlg::SetStatusMsg(const std::string& msg)
Tobias WEBER's avatar
Tobias WEBER committed
961
962
{
	if(!m_status) return;
963
	m_status->showMessage(msg.c_str(), 2000);
Tobias WEBER's avatar
Tobias WEBER committed
964
965
966
967
}



Tobias WEBER's avatar
Tobias WEBER committed
968
969
void  MolDynDlg::UpdateAtomsStatusMsg()
{
Tobias WEBER's avatar
Tobias WEBER committed
970
	if(!m_statusAtoms || !m_sliderFrame) return;
Tobias WEBER's avatar
Tobias WEBER committed
971
972
973
974
975
976
977
978
979
980

	// Atoms
	std::string numAtoms = std::to_string(m_mol.GetNumAtomsTotal()) + " atoms.";

	// Selected
	if(m_plot)
	{
		std::size_t numSelected = 0;

		for(auto handle : m_sphereHandles)
981
        {
Tobias WEBER's avatar
Tobias WEBER committed
982
			if(m_plot->GetRenderer()->GetObjectHighlight(handle))
Tobias WEBER's avatar
Tobias WEBER committed
983
				++numSelected;
984
        }
Tobias WEBER's avatar
Tobias WEBER committed
985
986
987
988
989

		numAtoms += " " + std::to_string(numSelected) + " selected.";
	}

	// Frames
Tobias WEBER's avatar
Tobias WEBER committed
990
	numAtoms += " Frame " + std::to_string(m_sliderFrame->value()+1) + " of " + std::to_string(m_mol.GetFrameCount()) + ".";
Tobias WEBER's avatar
Tobias WEBER committed
991
992
993
994
995
996

	m_statusAtoms->setText(numAtoms.c_str());
}



Tobias WEBER's avatar
Tobias WEBER committed
997
998
999
1000
1001
/**
 * mouse button pressed
 */
void MolDynDlg::PlotMouseDown(bool left, bool mid, bool right)
{
Tobias WEBER's avatar
Tobias WEBER committed
1002
1003
	if(!m_plot) return;

Tobias WEBER's avatar
Tobias WEBER committed
1004
1005
	if(left && m_curPickedObj > 0)
	{
Tobias WEBER's avatar
Tobias WEBER committed
1006
		m_plot->GetRenderer()->SetObjectHighlight(m_curPickedObj, !m_plot->GetRenderer()->GetObjectHighlight(m_curPickedObj));
Tobias WEBER's avatar
Tobias WEBER committed
1007
		UpdateAtomsStatusMsg();
Tobias WEBER's avatar
Tobias WEBER committed
1008
		m_plot->update();
Tobias WEBER's avatar
Tobias WEBER committed
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
	}
}


/**
 * mouse button released
 */
void MolDynDlg::PlotMouseUp(bool left, bool mid, bool right)
{
}
Tobias WEBER's avatar
Tobias WEBER committed
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028


/**
 * mouse button clicked
 */
void MolDynDlg::PlotMouseClick(bool left, bool mid, bool right)
{
	// show context menu
	if(right && m_curPickedObj > 0)
	{
Tobias WEBER's avatar
Tobias WEBER committed
1029
		const auto [typelabel, atomidx] = SplitDataString(m_plot->GetRenderer()->GetObjectDataString(m_curPickedObj));
1030
1031

		QString atomLabel = typelabel.c_str();
Tobias WEBER's avatar
Tobias WEBER committed
1032
		m_atomContextMenu->actions()[0]->setText("Delete This \"" + atomLabel + "\" Atom");
1033
1034
1035
		m_atomContextMenu->actions()[5]->setText("Delete All \"" + atomLabel + "\" Atoms");
		m_atomContextMenu->actions()[6]->setText("Delete All But \"" + atomLabel + "\" Atoms");
		m_atomContextMenu->actions()[8]->setText("Select All \"" + atomLabel + "\" Atoms");
Tobias WEBER's avatar
Tobias WEBER committed
1036
1037
1038
1039
1040
1041

		auto ptGlob = QCursor::pos();
		ptGlob.setY(ptGlob.y() + 8);
		m_atomContextMenu->popup(ptGlob);
	}
}
Tobias WEBER's avatar
Tobias WEBER committed
1042
1043


Tobias WEBER's avatar
Tobias WEBER committed
1044
1045
1046
// ----------------------------------------------------------------------------


Tobias WEBER's avatar
Tobias WEBER committed
1047
1048
1049
1050
1051
1052
1053
1054
/**
 * select all atoms
 */
void MolDynDlg::SelectAll()
{
	if(!m_plot) return;

	for(auto handle : m_sphereHandles)
Tobias WEBER's avatar
Tobias WEBER committed
1055
		m_plot->GetRenderer()->SetObjectHighlight(handle, 1);
Tobias WEBER's avatar
Tobias WEBER committed
1056

Tobias WEBER's avatar
Tobias WEBER committed
1057
	UpdateAtomsStatusMsg();
Tobias WEBER's avatar
Tobias WEBER committed
1058
1059
1060
1061
	m_plot->update();
}


Tobias WEBER's avatar
Tobias WEBER committed
1062
1063
1064
1065
1066
1067
1068
1069
/**
 * unselect all atoms
 */
void MolDynDlg::SelectNone()
{
	if(!m_plot) return;

	for(auto handle : m_sphereHandles)
Tobias WEBER's avatar
Tobias WEBER committed
1070
		m_plot->GetRenderer()->SetObjectHighlight(handle, 0);
Tobias WEBER's avatar
Tobias WEBER committed
1071

Tobias WEBER's avatar
Tobias WEBER committed
1072
	UpdateAtomsStatusMsg();
Tobias WEBER's avatar
Tobias WEBER committed
1073
1074
1075
	m_plot->update();
}

Tobias WEBER's avatar
Tobias WEBER committed
1076

Tobias WEBER's avatar
Tobias WEBER committed
1077
1078
void MolDynDlg::SliderValueChanged(int val)
{
1079
1080
1081
1082
1083
1084
1085
1086
	if(!m_plot) return;

	if(val < 0 || val >= m_mol.GetFrameCount())
		return;


	// update atom position with selected frame
	const auto& frame = m_mol.GetFrame(val);
Tobias WEBER's avatar
Tobias WEBER committed
1087
	t_real atomscale = m_spinScale->value();
1088
1089

	std::size_t counter = 0;
1090
	for(std::size_t atomtypeidx=0; atomtypeidx<frame.GetNumAtomTypes(); ++atomtypeidx)
1091
	{
1092
1093
1094
		const auto& coords = frame.GetCoords(atomtypeidx);

		int atomidx = 0;
1095
1096
1097
		for(const t_vec& vec : coords)
		{
			std::size_t obj = m_sphereHandles[counter];
Tobias WEBER's avatar
Tobias WEBER committed
1098
			Change3DAtom(obj, &vec, nullptr, &atomscale, nullptr, atomidx);
1099
1100

			++counter;
1101
			++atomidx;
1102
1103
1104
		}
	}

Tobias WEBER's avatar
Tobias WEBER committed
1105
#ifdef USE_QHULL
1106
	CalculateConvexHulls();
Tobias WEBER's avatar
Tobias WEBER committed
1107
#endif
Tobias WEBER's avatar
Tobias WEBER committed
1108
	UpdateAtomsStatusMsg();
1109
	m_plot->update();
Tobias WEBER's avatar
Tobias WEBER committed
1110
1111
1112
}


Tobias WEBER's avatar
Tobias WEBER committed
1113
1114
// ----------------------------------------------------------------------------

1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
/**
 * extract [typelabel, atomindex] from the atom data strings
 */
std::tuple<std::string, int> MolDynDlg::SplitDataString(const std::string& data) const
{
	std::size_t idx = data.rfind(DATA_SEP);

	// no separator found, just return atom type string
	if(idx == std::string::npos)
		return std::make_tuple(data, -1);

	// split string
	std::string atomtype = data.substr(0, idx);
	std::string atomidx = data.substr(idx + std::strlen(DATA_SEP));

	return std::make_tuple(atomtype, std::stoi(atomidx));
}

Tobias WEBER's avatar
Tobias WEBER committed
1133

Tobias WEBER's avatar
Tobias WEBER committed
1134
/**
Tobias WEBER's avatar
Tobias WEBER committed
1135
1136
 * get the index of the atom in the m_mol data structure
 * from the handle of the displayed 3d object
Tobias WEBER's avatar
Tobias WEBER committed
1137
 */
1138
std::tuple<bool, std::size_t, std::size_t, std::size_t>
Tobias WEBER's avatar
Tobias WEBER committed
1139
MolDynDlg::GetAtomIndexFromHandle(std::size_t handle) const
Tobias WEBER's avatar
Tobias WEBER committed
1140
{
Tobias WEBER's avatar
Tobias WEBER committed
1141
	// find handle in sphere handle vector
Tobias WEBER's avatar
Tobias WEBER committed
1142
	auto iter = std::find(m_sphereHandles.begin(), m_sphereHandles.end(), handle);
Tobias WEBER's avatar
Tobias WEBER committed
1143
	if(iter == m_sphereHandles.end())
Tobias WEBER's avatar
Tobias WEBER committed
1144
		return std::make_tuple(0, 0, 0, 0);
Tobias WEBER's avatar
Tobias WEBER committed
1145
1146

	std::size_t sphereIdx = iter - m_sphereHandles.begin();
Tobias WEBER's avatar
Tobias WEBER committed
1147

Tobias WEBER's avatar
Tobias WEBER committed
1148
1149
	std::size_t atomCountsSoFar = 0;
	std::size_t atomTypeIdx = 0;
Tobias WEBER's avatar
Tobias WEBER committed
1150
	for(atomTypeIdx=0; atomTypeIdx<m_mol.GetNumAtomTypes(); ++atomTypeIdx)
Tobias WEBER's avatar
Tobias WEBER committed
1151
1152
1153
1154
1155
1156
1157
1158
	{
		std::size_t numAtoms = m_mol.GetAtomNum(atomTypeIdx);
		if(atomCountsSoFar + numAtoms > sphereIdx)
			break;

		atomCountsSoFar += numAtoms;
	}

Tobias WEBER's avatar
Tobias WEBER committed
1159
1160
1161
1162
1163
1164
	std::size_t atomSubTypeIdx = sphereIdx-atomCountsSoFar;

	return std::make_tuple(1, atomTypeIdx, atomSubTypeIdx, sphereIdx);
}


Tobias WEBER's avatar
Tobias WEBER committed
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
/**
 * select all atoms of the same type as the one under the cursor
 */
void MolDynDlg::SelectAtomsOfSameType()
{
	// nothing under cursor
	if(m_curPickedObj <= 0)
		return;

	// atom type to be selected
Tobias WEBER's avatar
Tobias WEBER committed
1175
	const auto [atomLabel, atomidx] = SplitDataString(m_plot->GetRenderer()->GetObjectDataString(m_curPickedObj));
Tobias WEBER's avatar
Tobias WEBER committed
1176
1177

	for(auto handle : m_sphereHandles)
1178
    {
Tobias WEBER's avatar
Tobias WEBER committed
1179
		const auto [typelabel, atomidx] = SplitDataString(m_plot->