moldyn.cpp 8.57 KB
Newer Older
1
/**
Tobias WEBER's avatar
Tobias WEBER committed
2
 * atom dynamics
3
4
5
6
7
 * @author Tobias Weber <tweber@ill.fr>
 * @date Dec-2019
 * @license GPLv3, see 'LICENSE' file
 */

Tobias WEBER's avatar
Tobias WEBER committed
8
#include "moldyn.h"
9

Tobias WEBER's avatar
Tobias WEBER committed
10
#include <QtWidgets/QApplication>
Tobias WEBER's avatar
Tobias WEBER committed
11
#include <QtWidgets/QGridLayout>
Tobias WEBER's avatar
Tobias WEBER committed
12
13
#include <QtWidgets/QFileDialog>
#include <QtWidgets/QMessageBox>
14

Tobias WEBER's avatar
Tobias WEBER committed
15
16
#include <iostream>
#include <tuple>
17

Tobias WEBER's avatar
Tobias WEBER committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "libs/algos.h"
#include "libs/helper.h"

using namespace m_ops;

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


// ----------------------------------------------------------------------------
MolDynDlg::MolDynDlg(QWidget* pParent) : QMainWindow{pParent},
	m_sett{new QSettings{"tobis_stuff", "moldyn"}}
{
	setWindowTitle("Molecular Dynamics");
	this->setObjectName("moldyn");

	m_status = new QStatusBar(this);
	this->setStatusBar(m_status);


Tobias WEBER's avatar
Tobias WEBER committed
38
39
40
41
42
43
44
	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
45
46
47
48
49
50
51
52
53
	// menu bar
	{
		m_menu = new QMenuBar(this);
		m_menu->setNativeMenuBar(m_sett ? m_sett->value("native_gui", false).toBool() : false);

		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
54
		//auto acSave = new QAction("Save...", menuFile);
Tobias WEBER's avatar
Tobias WEBER committed
55
56
57
58
59
		auto acExit = new QAction("Exit", menuFile);

		menuFile->addAction(acNew);
		menuFile->addSeparator();
		menuFile->addAction(acLoad);
Tobias WEBER's avatar
Tobias WEBER committed
60
		//menuFile->addAction(acSave);
Tobias WEBER's avatar
Tobias WEBER committed
61
62
63
64
65
		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
66
		//connect(acSave, &QAction::triggered, this, &MolDynDlg::Save);
Tobias WEBER's avatar
Tobias WEBER committed
67
68
69
70
71
72
73
74
75
76
77
78
		connect(acExit, &QAction::triggered, this, &QDialog::close);

		m_menu->addMenu(menuFile);
		this->setMenuBar(m_menu);
	}


	// plot widget
	{
		m_plot = new GlPlot(this);
		m_plot->setSizePolicy(QSizePolicy{QSizePolicy::Expanding, QSizePolicy::Expanding});

Tobias WEBER's avatar
Tobias WEBER committed
79
		m_plot->GetImpl()->EnablePicker(1);
Tobias WEBER's avatar
Tobias WEBER committed
80
81
82
83
84
85
86
87
88
89
90
		m_plot->GetImpl()->SetLight(0, m::create<t_vec3_gl>({ 5, 5, 5 }));
		m_plot->GetImpl()->SetLight(1, m::create<t_vec3_gl>({ -5, -5, -5 }));
		m_plot->GetImpl()->SetCoordMax(1.);
		m_plot->GetImpl()->SetCamBase(m::create<t_mat_gl>({1,0,0,0,  0,0,1,0,  0,-1,0,-1.5,  0,0,0,1}),
			m::create<t_vec_gl>({1,0,0,0}), m::create<t_vec_gl>({0,0,1,0}));

		connect(m_plot, &GlPlot::AfterGLInitialisation, this, &MolDynDlg::AfterGLInitialisation);
		connect(m_plot->GetImpl(), &GlPlot_impl::PickerIntersection, this, &MolDynDlg::PickerIntersection);
		connect(m_plot, &GlPlot::MouseDown, this, &MolDynDlg::PlotMouseDown);
		connect(m_plot, &GlPlot::MouseUp, this, &MolDynDlg::PlotMouseUp);

Tobias WEBER's avatar
Tobias WEBER committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
		//this->setCentralWidget(m_plot);
		pMainGrid->addWidget(m_plot, 0,0,1,1);
	}


	// controls
	{
		m_slider = new QSlider(Qt::Horizontal, this);
		m_slider->setSizePolicy(QSizePolicy{QSizePolicy::Expanding, QSizePolicy::Minimum});
		m_slider->setMinimum(0);
		m_slider->setTracking(1);

		connect(m_slider, &QSlider::valueChanged, this, &MolDynDlg::SliderValueChanged);

		pMainGrid->addWidget(m_slider, 1,0,1,1);
Tobias WEBER's avatar
Tobias WEBER committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
	}


	// 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
122
 * add an atom
Tobias WEBER's avatar
Tobias WEBER committed
123
 */
124
std::size_t MolDynDlg::Add3DItem(const t_vec& vec, const t_vec& col, t_real scale, const std::string& label)
Tobias WEBER's avatar
Tobias WEBER committed
125
{
Tobias WEBER's avatar
Tobias WEBER committed
126
	auto obj = m_plot->GetImpl()->AddLinkedObject(m_sphere, 0,0,0, col[0],col[1],col[2],1);
Tobias WEBER's avatar
Tobias WEBER committed
127
	Change3DItem(obj, &vec, &col, &scale, &label);
128
	return obj;
Tobias WEBER's avatar
Tobias WEBER committed
129
}
Tobias WEBER's avatar
Tobias WEBER committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144


/**
 * change an atom
 */
void MolDynDlg::Change3DItem(std::size_t obj, const t_vec *vec, const t_vec *col, const t_real *scale, const std::string *label)
{
	t_mat_gl mat = m::hom_translation<t_mat_gl>((*vec)[0], (*vec)[1], (*vec)[2]);
	if(scale) mat *= m::hom_scaling<t_mat_gl>(*scale, *scale, *scale);

	m_plot->GetImpl()->SetObjectMatrix(obj, mat);
	if(col) m_plot->GetImpl()->SetObjectCol(obj, (*col)[0], (*col)[1], (*col)[2], 1);
	if(label) m_plot->GetImpl()->SetObjectLabel(obj, *label);
	if(label) m_plot->GetImpl()->SetObjectDataString(obj, *label);
}
Tobias WEBER's avatar
Tobias WEBER committed
145
146
147
148
149
150
151
152
// ----------------------------------------------------------------------------




// ----------------------------------------------------------------------------
void MolDynDlg::New()
{
Tobias WEBER's avatar
Tobias WEBER committed
153
	m_mol.Clear();
154
155
	m_sphereHandles.clear();
	// TODO: clear 3d objects
Tobias WEBER's avatar
Tobias WEBER committed
156
157
158
159
160
}


void MolDynDlg::Load()
{
161
162
	if(!m_plot) return;

Tobias WEBER's avatar
Tobias WEBER committed
163
164
165
166
167
168
169
170
	try
	{
		QString dirLast = m_sett->value("dir", "").toString();
		QString filename = QFileDialog::getOpenFileName(this, "Load File", dirLast, "Molecular Dynamics File (*)");
		if(filename=="" || !QFile::exists(filename))
			return;
		m_sett->setValue("dir", QFileInfo(filename).path());

Tobias WEBER's avatar
Tobias WEBER committed
171
		if(!m_mol.LoadFile(filename.toStdString(), m_frameskip))
Tobias WEBER's avatar
Tobias WEBER committed
172
173
		{
			QMessageBox::critical(this, "Molecular Dynamics", "Error loading file.");
Tobias WEBER's avatar
Tobias WEBER committed
174
			return;
Tobias WEBER's avatar
Tobias WEBER committed
175
176
		}

Tobias WEBER's avatar
Tobias WEBER committed
177
178
		m_slider->setMaximum(m_mol.GetFrameCount());

Tobias WEBER's avatar
Tobias WEBER committed
179

Tobias WEBER's avatar
Tobias WEBER committed
180
		// atom colors
Tobias WEBER's avatar
Tobias WEBER committed
181
182
183
184
185
186
187
188
189
190
		std::vector<t_vec> cols =
		{
			m::create<t_vec>({1, 0, 0}),
			m::create<t_vec>({0, 0, 1}),
			m::create<t_vec>({0, 0.5, 0}),
			m::create<t_vec>({0, 0.5, 0.5}),
			m::create<t_vec>({0.5, 0.5, 0}),
			m::create<t_vec>({0, 0, 0}),
		};

Tobias WEBER's avatar
Tobias WEBER committed
191
		// add atoms to 3d view
Tobias WEBER's avatar
Tobias WEBER committed
192
193
194
		if(m_mol.GetFrameCount())
		{
			const auto& frame = m_mol.GetFrame(0);
195
			m_sphereHandles.reserve(frame.GetNumAtoms());
Tobias WEBER's avatar
Tobias WEBER committed
196
197
198
199
200

			for(std::size_t atomidx=0; atomidx<frame.GetNumAtoms(); ++atomidx)
			{
				const auto& coords = frame.GetCoords(atomidx);
				for(const t_vec& vec : coords)
201
				{
Tobias WEBER's avatar
Tobias WEBER committed
202
					std::size_t handle = Add3DItem(vec, cols[atomidx % cols.size()], m_atomscale, m_mol.GetAtomName(atomidx));
203
204
					m_sphereHandles.push_back(handle);
				}
Tobias WEBER's avatar
Tobias WEBER committed
205
206
207
208
209
210
211
212
			}
		}
	}
	catch(const std::exception& ex)
	{
		QMessageBox::critical(this, "Molecular Dynamics", ex.what());
	}

Tobias WEBER's avatar
Tobias WEBER committed
213
	m_plot->update();
Tobias WEBER's avatar
Tobias WEBER committed
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
}


void MolDynDlg::Save()
{
}
// ----------------------------------------------------------------------------



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

Tobias WEBER's avatar
Tobias WEBER committed
232
233
234
235
236
237
238
239
	if(pos)
		m_curPickedObj = long(objIdx);
	else
		m_curPickedObj = -1;


	if(m_curPickedObj > 0)
	{
240
241
		const std::string& label = m_plot->GetImpl()->GetObjectDataString(m_curPickedObj);
		SetStatusMsg(label);
Tobias WEBER's avatar
Tobias WEBER committed
242
243
244
	}
	else
	{
Tobias WEBER's avatar
Tobias WEBER committed
245
		SetStatusMsg("");
Tobias WEBER's avatar
Tobias WEBER committed
246
247
248
249
250
251
252
253
	}
}



/**
 * set status label text in 3d dialog
 */
Tobias WEBER's avatar
Tobias WEBER committed
254
void MolDynDlg::SetStatusMsg(const std::string& msg)
Tobias WEBER's avatar
Tobias WEBER committed
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
{
	if(!m_status) return;

	m_status->showMessage(msg.c_str());
}



/**
 * mouse button pressed
 */
void MolDynDlg::PlotMouseDown(bool left, bool mid, bool right)
{
	if(left && m_curPickedObj > 0)
	{
	}
}


/**
 * mouse button released
 */
void MolDynDlg::PlotMouseUp(bool left, bool mid, bool right)
{
}
// ----------------------------------------------------------------------------



Tobias WEBER's avatar
Tobias WEBER committed
284
285
void MolDynDlg::SliderValueChanged(int val)
{
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
	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);

	std::size_t counter = 0;
	for(std::size_t atomidx=0; atomidx<frame.GetNumAtoms(); ++atomidx)
	{
		const auto& coords = frame.GetCoords(atomidx);
		for(const t_vec& vec : coords)
		{
			std::size_t obj = m_sphereHandles[counter];
Tobias WEBER's avatar
Tobias WEBER committed
302
			Change3DItem(obj, &vec, nullptr, &m_atomscale);
303
304
305
306
307
308

			++counter;
		}
	}

	m_plot->update();
Tobias WEBER's avatar
Tobias WEBER committed
309
310
311
312
}



Tobias WEBER's avatar
Tobias WEBER committed
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// ----------------------------------------------------------------------------
void MolDynDlg::AfterGLInitialisation()
{
	if(!m_plot) return;

	// reference sphere for linked objects
	m_sphere = m_plot->GetImpl()->AddSphere(0.05, 0.,0.,0., 1.,1.,1.,1.);
	m_plot->GetImpl()->SetObjectVisible(m_sphere, false);

	// GL device info
	auto [strGlVer, strGlShaderVer, strGlVendor, strGlRenderer]
		= m_plot->GetImpl()->GetGlDescr();
	std::cout << "GL Version: " << strGlVer << ", Shader Version: " << strGlShaderVer << "." << std::endl;
	std::cout << "GL Device: " << strGlRenderer << ", " << strGlVendor << "." << std::endl;
}


void MolDynDlg::closeEvent(QCloseEvent *evt)
{
	if(m_sett)
	{
		m_sett->setValue("geo", saveGeometry());
	}
}
// ----------------------------------------------------------------------------





// ----------------------------------------------------------------------------
344
345
346

int main(int argc, char** argv)
{
Tobias WEBER's avatar
Tobias WEBER committed
347
348
	set_gl_format(1, _GL_MAJ_VER, _GL_MIN_VER, 8);
	tl2::set_locales();
349

Tobias WEBER's avatar
Tobias WEBER committed
350
351
352
	auto app = std::make_unique<QApplication>(argc, argv);
	auto dlg = std::make_unique<MolDynDlg>(nullptr);
	dlg->show();
353

Tobias WEBER's avatar
Tobias WEBER committed
354
	return app->exec();
355
}
Tobias WEBER's avatar
Tobias WEBER committed
356
// ----------------------------------------------------------------------------