pol.cpp 19.2 KB
Newer Older
1
2
3
4
5
/**
 * Calculation of polarisation vector
 * @author Tobias Weber <tweber@ill.fr>
 * @date Oct-2018
 * @license GPLv3, see 'LICENSE' file
Tobias WEBER's avatar
Tobias WEBER committed
6
 * @desc The present version was forked on 8-Nov-2018 from my privately developed "magtools" project (https://github.com/t-weber/magtools).
7
8
9
 */

#include <QtCore/QSettings>
Tobias WEBER's avatar
Tobias WEBER committed
10
#include <QtCore/QDir>
11
12
13
14
15
16
17
18
19
20
#include <QtWidgets/QApplication>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QTabWidget>

#include <locale>
#include <iostream>
#include <vector>
#include <string>
Tobias WEBER's avatar
Tobias WEBER committed
21
#include <optional>
22

23
24
25
26
#include "tlibs2/libs/glplot.h"
#include "tlibs2/libs/math20.h"
#include "tlibs2/libs/str.h"
#include "tlibs2/libs/helper.h"
27
28
29
30
31
32
33

#include <boost/version.hpp>
#include <boost/config.hpp>
#include <boost/algorithm/string/replace.hpp>
namespace algo = boost::algorithm;


Tobias WEBER's avatar
Tobias WEBER committed
34
using namespace tl2_ops;
35
36
37

using t_real = double;
using t_cplx = std::complex<t_real>;
Tobias WEBER's avatar
Tobias WEBER committed
38
using t_vec = tl2::vec<t_cplx, std::vector>;
Tobias WEBER's avatar
Tobias WEBER committed
39
using t_mat = tl2::mat<t_cplx, std::vector>;
40
41
using t_matvec = std::vector<t_mat>;

Tobias WEBER's avatar
Tobias WEBER committed
42
43
44
45
46
47
using t_real_gl = tl2::t_real_gl;
using t_vec2_gl = tl2::t_vec2_gl;
using t_vec3_gl = tl2::t_vec3_gl;
using t_vec_gl = tl2::t_vec_gl;
using t_mat_gl = tl2::t_mat_gl;

48
49
50
51
52

// ----------------------------------------------------------------------------
class PolDlg : public QDialog
{ /*Q_OBJECT*/
private:
Tobias WEBER's avatar
Tobias WEBER committed
53
	QSettings m_sett{"takin", "pol"};
54
	int m_prec = 6;		// precision
55

Tobias WEBER's avatar
Tobias WEBER committed
56
	std::shared_ptr<tl2::GlPlot> m_plot{std::make_shared<tl2::GlPlot>(this)};
Tobias WEBER's avatar
Tobias WEBER committed
57
	QLabel *m_labelGlInfos[4] = { nullptr, nullptr, nullptr, nullptr };
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

	QLineEdit* m_editNRe = new QLineEdit("0", this);
	QLineEdit* m_editNIm = new QLineEdit("0", this);

	QLineEdit* m_editMPerpReX = new QLineEdit("0", this);
	QLineEdit* m_editMPerpReY = new QLineEdit("1", this);
	QLineEdit* m_editMPerpReZ = new QLineEdit("0", this);
	QLineEdit* m_editMPerpImX = new QLineEdit("0", this);
	QLineEdit* m_editMPerpImY = new QLineEdit("0", this);
	QLineEdit* m_editMPerpImZ = new QLineEdit("0", this);

	QLineEdit* m_editPiX = new QLineEdit("1", this);
	QLineEdit* m_editPiY = new QLineEdit("0", this);
	QLineEdit* m_editPiZ = new QLineEdit("0", this);
	QLineEdit* m_editPfX = new QLineEdit(this);
	QLineEdit* m_editPfY = new QLineEdit(this);
	QLineEdit* m_editPfZ = new QLineEdit(this);

	QLabel* m_labelStatus = new QLabel(this);


	// 3d object handles
	std::size_t m_arrow_pi = 0;
	std::size_t m_arrow_pf = 0;
	std::size_t m_arrow_M_Re = 0;
	std::size_t m_arrow_M_Im = 0;

	bool m_3dobjsReady = false;
	bool m_mouseDown[3] = {false, false, false};
Tobias WEBER's avatar
Tobias WEBER committed
87
88
	std::optional<std::size_t> m_curPickedObj{};
	std::optional<std::size_t> m_curDraggedObj{};
89
90
91


protected:
92
	virtual void closeEvent(QCloseEvent *) override
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
	{
		// save window size and position
		m_sett.setValue("geo", saveGeometry());

		// save values
		m_sett.setValue("n_re", m_editNRe->text().toDouble());
		m_sett.setValue("n_im", m_editNIm->text().toDouble());
		m_sett.setValue("mx_re", m_editMPerpReX->text().toDouble());
		m_sett.setValue("my_re", m_editMPerpReY->text().toDouble());
		m_sett.setValue("mz_re", m_editMPerpReZ->text().toDouble());
		m_sett.setValue("mx_im", m_editMPerpImX->text().toDouble());
		m_sett.setValue("my_im", m_editMPerpImY->text().toDouble());
		m_sett.setValue("mz_im", m_editMPerpImZ->text().toDouble());
		m_sett.setValue("pix", m_editPiX->text().toDouble());
		m_sett.setValue("piy", m_editPiY->text().toDouble());
		m_sett.setValue("piz", m_editPiZ->text().toDouble());
	}


protected slots:
	/**
	 * called after the plotter has initialised
	 */
	void AfterGLInitialisation()
	{
		// GL device info
Tobias WEBER's avatar
Tobias WEBER committed
119
		auto [strGlVer, strGlShaderVer, strGlVendor, strGlRenderer]
Tobias WEBER's avatar
Tobias WEBER committed
120
			= m_plot->GetRenderer()->GetGlDescr();
Tobias WEBER's avatar
Tobias WEBER committed
121
122
123
124
		m_labelGlInfos[0]->setText(QString("GL Version: ") + strGlVer.c_str() + QString("."));
		m_labelGlInfos[1]->setText(QString("GL Shader Version: ") + strGlShaderVer.c_str() + QString("."));
		m_labelGlInfos[2]->setText(QString("GL Vendor: ") + strGlVendor.c_str() + QString("."));
		m_labelGlInfos[3]->setText(QString("GL Device: ") + strGlRenderer.c_str() + QString("."));
125
126
127
128


		if(!m_3dobjsReady)		// create 3d objects
		{
Tobias WEBER's avatar
Tobias WEBER committed
129
130
131
132
			m_arrow_pi = m_plot->GetRenderer()->AddArrow(0.05, 1., 0.,0.,0.5,  0.,0.,0.85,1.);
			m_arrow_pf = m_plot->GetRenderer()->AddArrow(0.05, 1., 0.,0.,0.5,  0.,0.5,0.,1.);
			m_arrow_M_Re = m_plot->GetRenderer()->AddArrow(0.05, 1., 0.,0.,0.5,  0.85,0.,0.,1.);
			m_arrow_M_Im = m_plot->GetRenderer()->AddArrow(0.05, 1., 0.,0.,0.5,  0.85,0.25,0.,1.);
133

Tobias WEBER's avatar
Tobias WEBER committed
134
135
136
137
			m_plot->GetRenderer()->SetObjectLabel(m_arrow_pi, "P_i");
			m_plot->GetRenderer()->SetObjectLabel(m_arrow_pf, "P_f");
			m_plot->GetRenderer()->SetObjectLabel(m_arrow_M_Re, "Re{M_perp}");
			m_plot->GetRenderer()->SetObjectLabel(m_arrow_M_Im, "Im{M_perp}");
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183

			m_3dobjsReady = true;
			CalcPol();
		}
	}


	/**
	 * get the length of a vector
	 */
	t_real GetArrowLen(std::size_t objIdx) const
	{
		if(objIdx == m_arrow_pi)
		{
			return std::sqrt(std::pow(m_editPiX->text().toDouble(), 2.)
				+ std::pow(m_editPiY->text().toDouble(), 2.)
				+ std::pow(m_editPiZ->text().toDouble(), 2.));
		}
		else if(objIdx == m_arrow_pf)
		{
			return std::sqrt(std::pow(m_editPfX->text().toDouble(), 2.)
				+ std::pow(m_editPfY->text().toDouble(), 2.)
				+ std::pow(m_editPfZ->text().toDouble(), 2.));
		}
		else if(objIdx == m_arrow_M_Re)
		{
			return std::sqrt(std::pow(m_editMPerpReX->text().toDouble(), 2.)
				+ std::pow(m_editMPerpReY->text().toDouble(), 2.)
				+ std::pow(m_editMPerpReZ->text().toDouble(), 2.));
		}
		else if(objIdx == m_arrow_M_Im)
		{
			return std::sqrt(std::pow(m_editMPerpImX->text().toDouble(), 2.)
				+ std::pow(m_editMPerpImY->text().toDouble(), 2.)
				+ std::pow(m_editMPerpImZ->text().toDouble(), 2.));
		}

		return -1.;
	}


	/**
	 * called when the mouse hovers over an object
	 */
	void PickerIntersection(const t_vec3_gl* pos, std::size_t objIdx, const t_vec3_gl* posSphere)
	{
Tobias WEBER's avatar
Tobias WEBER committed
184
185
		m_curPickedObj.reset();

186
187
		if(pos)
		{	// object selected?
Tobias WEBER's avatar
Tobias WEBER committed
188
			m_curPickedObj = objIdx;
189
190
191
192
193

			if(objIdx == m_arrow_pi) m_labelStatus->setText("P_i");
			else if(objIdx == m_arrow_pf) m_labelStatus->setText("P_f");
			else if(objIdx == m_arrow_M_Re) m_labelStatus->setText("Re{M_perp}");
			else if(objIdx == m_arrow_M_Im) m_labelStatus->setText("Im{M_perp}");
Tobias WEBER's avatar
Tobias WEBER committed
194
			else m_curPickedObj.reset();
195
196
		}

Tobias WEBER's avatar
Tobias WEBER committed
197
		if(m_curPickedObj)
198
199
200
201
202
203
204
205
206
207
		{
			setCursor(Qt::CrossCursor);
		}
		else
		{
			m_labelStatus->setText("");
			setCursor(Qt::ArrowCursor);
		}


Tobias WEBER's avatar
Tobias WEBER committed
208
		if(posSphere && m_mouseDown[0] && m_curDraggedObj)
209
210
211
212
		{	// picker intersecting unit sphere and mouse dragged?

			t_vec3_gl posSph = *posSphere;

Tobias WEBER's avatar
Tobias WEBER committed
213
			if(*m_curDraggedObj == m_arrow_pi)
214
			{
215
216
217
				m_editPiX->setText(tl2::var_to_str(posSph[0], m_prec).c_str());
				m_editPiY->setText(tl2::var_to_str(posSph[1], m_prec).c_str());
				m_editPiZ->setText(tl2::var_to_str(posSph[2], m_prec).c_str());
218
219
				CalcPol();
			}
Tobias WEBER's avatar
Tobias WEBER committed
220
			else if(*m_curDraggedObj == m_arrow_M_Re)
221
			{
222
223
224
				m_editMPerpReX->setText(tl2::var_to_str(posSph[0], m_prec).c_str());
				m_editMPerpReY->setText(tl2::var_to_str(posSph[1], m_prec).c_str());
				m_editMPerpReZ->setText(tl2::var_to_str(posSph[2], m_prec).c_str());
225
226
				CalcPol();
			}
Tobias WEBER's avatar
Tobias WEBER committed
227
			else if(*m_curDraggedObj == m_arrow_M_Im)
228
			{
229
230
231
				m_editMPerpImX->setText(tl2::var_to_str(posSph[0], m_prec).c_str());
				m_editMPerpImY->setText(tl2::var_to_str(posSph[1], m_prec).c_str());
				m_editMPerpImZ->setText(tl2::var_to_str(posSph[2], m_prec).c_str());
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
				CalcPol();
			}
		}
	}


	/**
	 * mouse button pressed
	 */
	void MouseDown(bool left, bool mid, bool right)
	{
		if(left) m_mouseDown[0] = true;
		if(mid) m_mouseDown[1] = true;
		if(right) m_mouseDown[2] = true;

		if(m_mouseDown[0])
		{
Tobias WEBER's avatar
Tobias WEBER committed
249
250
251
252
			if((m_curDraggedObj = m_curPickedObj))
			{
				auto lenVec = GetArrowLen(*m_curDraggedObj);
				if(lenVec > 0.)
Tobias WEBER's avatar
Tobias WEBER committed
253
					m_plot->GetRenderer()->SetPickerSphereRadius(lenVec);
Tobias WEBER's avatar
Tobias WEBER committed
254
			}
255
256
257
258
259
260
261
262
263
264
265
266
267
268
		}
	}


	/**
	 * mouse button released
	 */
	void MouseUp(bool left, bool mid, bool right)
	{
		if(left) m_mouseDown[0] = false;
		if(mid) m_mouseDown[1] = false;
		if(right) m_mouseDown[2] = false;

		if(!m_mouseDown[0])
Tobias WEBER's avatar
Tobias WEBER committed
269
			m_curDraggedObj.reset();
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
	}


public:
	PolDlg() = delete;

	/**
	 * create UI
	 */
	PolDlg(QWidget* pParent) : QDialog{pParent, Qt::Window}
	{
		setWindowTitle("Polarisation Vectors");
		setSizeGripEnabled(true);

		auto tabs = new QTabWidget(this);


		{	// plot panel
			auto plotpanel = new QWidget(this);

			auto labelN = new QLabel("Re{N}, Im{N}:", plotpanel);
			auto labelMPerpRe = new QLabel("Re{M_perp}:", plotpanel);
			auto labelMPerpIm = new QLabel("Im{M_perp}:", plotpanel);
			auto labelPi = new QLabel("P_i:", plotpanel);
			auto labelPf = new QLabel("P_f:", plotpanel);


			for(auto* label : {labelMPerpRe, labelMPerpIm, labelPi, labelPf})
				label->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

			for(auto* editPf : {m_editPfX, m_editPfY, m_editPfZ})
				editPf->setReadOnly(true);

			m_labelStatus->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
304
			m_labelStatus->setFrameStyle(int(QFrame::Sunken) | int(QFrame::Panel));
305
306
307
308
309
310
311
312
313
314
315
			m_labelStatus->setLineWidth(1);


			// connections
			for(auto* edit : {m_editNRe, m_editNIm,
				m_editMPerpReX, m_editMPerpReY, m_editMPerpReZ,
				m_editMPerpImX, m_editMPerpImY, m_editMPerpImZ,
				m_editPiX, m_editPiY, m_editPiZ,
				m_editPfX, m_editPfY, m_editPfZ})
				connect(edit, &QLineEdit::textEdited, this, &PolDlg::CalcPol);

Tobias WEBER's avatar
Tobias WEBER committed
316
317
			connect(m_plot.get(), &tl2::GlPlot::AfterGLInitialisation, this, &PolDlg::AfterGLInitialisation);
			connect(m_plot->GetRenderer(), &tl2::GlPlotRenderer::PickerIntersection, this, &PolDlg::PickerIntersection);
318

Tobias WEBER's avatar
Tobias WEBER committed
319
320
			connect(m_plot.get(), &tl2::GlPlot::MouseDown, this, &PolDlg::MouseDown);
			connect(m_plot.get(), &tl2::GlPlot::MouseUp, this, &PolDlg::MouseUp);
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377


			auto pGrid = new QGridLayout(plotpanel);
			pGrid->setSpacing(4);
			pGrid->setContentsMargins(4,4,4,4);

			pGrid->addWidget(m_plot.get(), 0,0,1,4);

			pGrid->addWidget(labelN, 1,0,1,1);
			pGrid->addWidget(labelMPerpRe, 2,0,1,1);
			pGrid->addWidget(labelMPerpIm, 3,0,1,1);
			pGrid->addWidget(labelPi, 4,0,1,1);
			pGrid->addWidget(labelPf, 5,0,1,1);

			pGrid->addWidget(m_editNRe, 1,1,1,1);
			pGrid->addWidget(m_editNIm, 1,2,1,1);

			pGrid->addWidget(m_editMPerpReX, 2,1,1,1);
			pGrid->addWidget(m_editMPerpReY, 2,2,1,1);
			pGrid->addWidget(m_editMPerpReZ, 2,3,1,1);
			pGrid->addWidget(m_editMPerpImX, 3,1,1,1);
			pGrid->addWidget(m_editMPerpImY, 3,2,1,1);
			pGrid->addWidget(m_editMPerpImZ, 3,3,1,1);

			pGrid->addWidget(m_editPiX, 4,1,1,1);
			pGrid->addWidget(m_editPiY, 4,2,1,1);
			pGrid->addWidget(m_editPiZ, 4,3,1,1);
			pGrid->addWidget(m_editPfX, 5,1,1,1);
			pGrid->addWidget(m_editPfY, 5,2,1,1);
			pGrid->addWidget(m_editPfZ, 5,3,1,1);

			pGrid->addWidget(m_labelStatus, 6,0,1,4);

			// restore last values
			if(m_sett.contains("n_re")) m_editNRe->setText(m_sett.value("n_re").toString());
			if(m_sett.contains("n_im")) m_editNIm->setText(m_sett.value("n_im").toString());
			if(m_sett.contains("mx_re")) m_editMPerpReX->setText(m_sett.value("mx_re").toString());
			if(m_sett.contains("my_re")) m_editMPerpReY->setText(m_sett.value("my_re").toString());
			if(m_sett.contains("mz_re")) m_editMPerpReZ->setText(m_sett.value("mz_re").toString());
			if(m_sett.contains("mx_im")) m_editMPerpImX->setText(m_sett.value("mx_im").toString());
			if(m_sett.contains("my_im")) m_editMPerpImY->setText(m_sett.value("my_im").toString());
			if(m_sett.contains("mz_im")) m_editMPerpImZ->setText(m_sett.value("mz_im").toString());
			if(m_sett.contains("pix")) m_editPiX->setText(m_sett.value("pix").toString());
			if(m_sett.contains("piy")) m_editPiY->setText(m_sett.value("piy").toString());
			if(m_sett.contains("piz")) m_editPiZ->setText(m_sett.value("piz").toString());


			tabs->addTab(plotpanel, "Calculation");
		}


		{	// info panel
			auto infopanel = new QWidget(this);
			auto pGrid = new QGridLayout(infopanel);
			pGrid->setSpacing(4);
			pGrid->setContentsMargins(4,4,4,4);

Tobias WEBER's avatar
Tobias WEBER committed
378
379
380
			auto sep1 = new QFrame(infopanel); sep1->setFrameStyle(QFrame::HLine);
			auto sep2 = new QFrame(infopanel); sep2->setFrameStyle(QFrame::HLine);
			auto sep3 = new QFrame(infopanel); sep3->setFrameStyle(QFrame::HLine);
381

Tobias WEBER's avatar
Tobias WEBER committed
382
383
384
385
386
			for(int i=0; i<4; ++i)
			{
				m_labelGlInfos[i] = new QLabel("", infopanel);
				m_labelGlInfos[i]->setSizePolicy(QSizePolicy::Ignored, m_labelGlInfos[i]->sizePolicy().verticalPolicy());
			}
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411

			std::string strBoost = BOOST_LIB_VERSION;
			algo::replace_all(strBoost, "_", ".");

			auto labelTitle = new QLabel("Polarisation Calculator", infopanel);
			auto fontTitle = labelTitle->font();
			fontTitle.setBold(true);
			labelTitle->setFont(fontTitle);
			labelTitle->setAlignment(Qt::AlignHCenter);

			auto labelAuthor = new QLabel("Written by Tobias Weber <tweber@ill.fr>.", infopanel);
			labelAuthor->setAlignment(Qt::AlignHCenter);

			auto labelDate = new QLabel("November 2018.", infopanel);
			labelDate->setAlignment(Qt::AlignHCenter);

			int y = 0;
			pGrid->addWidget(labelTitle, y++,0, 1,1);
			pGrid->addWidget(labelAuthor, y++,0, 1,1);
			pGrid->addWidget(labelDate, y++,0, 1,1);
			pGrid->addItem(new QSpacerItem(16,16, QSizePolicy::Minimum, QSizePolicy::Fixed), y++,0, 1,1);
			pGrid->addWidget(sep1, y++,0, 1,1);
			pGrid->addWidget(new QLabel(QString("Compiler: ") + QString(BOOST_COMPILER) + ".", infopanel), y++,0, 1,1);
			pGrid->addWidget(new QLabel(QString("C++ Library: ") + QString(BOOST_STDLIB) + ".", infopanel), y++,0, 1,1);
			pGrid->addWidget(new QLabel(QString("Build Date: ") + QString(__DATE__) + ", " + QString(__TIME__) + ".", infopanel), y++,0, 1,1);
Tobias WEBER's avatar
Tobias WEBER committed
412
413
414
415
416
417
			pGrid->addWidget(sep2, y++,0, 1,1);
			pGrid->addWidget(new QLabel(QString("Qt Version: ") + QString(QT_VERSION_STR) + ".", infopanel), y++,0, 1,1);
			pGrid->addWidget(new QLabel(QString("Boost Version: ") + strBoost.c_str() + ".", infopanel), y++,0, 1,1);
			pGrid->addWidget(sep3, y++,0, 1,1);
			for(int i=0; i<4; ++i)
				pGrid->addWidget(m_labelGlInfos[i], y++,0, 1,1);
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
			pGrid->addItem(new QSpacerItem(16,16, QSizePolicy::Minimum, QSizePolicy::Expanding), y++,0, 1,1);

			tabs->addTab(infopanel, "Infos");
		}


		auto pmainGrid = new QGridLayout(this);
		pmainGrid->setSpacing(4);
		pmainGrid->setContentsMargins(4,4,4,4);
		pmainGrid->addWidget(tabs, 0,0, 1,1);


		// restory window size and position
		if(m_sett.contains("geo"))
			restoreGeometry(m_sett.value("geo").toByteArray());
		else
			resize(800, 600);

		// have scattering plane in horizontal plane
Tobias WEBER's avatar
Tobias WEBER committed
437
438
439
440
		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(5.);
		m_plot->GetRenderer()->SetCamBase(tl2::create<t_mat_gl>({1,0,0,0,  0,0,1,0,  0,-1,0,-5,  0,0,0,1}),
Tobias WEBER's avatar
Tobias WEBER committed
441
			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
442

443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
		CalcPol();
	}


	/**
	 * calculate final polarisation vector
	 */
	void CalcPol()
	{
		// get values from line edits
		t_real NRe = t_real(m_editNRe->text().toDouble());
		t_real NIm = t_real(m_editNIm->text().toDouble());

		t_real MPerpReX = t_real(m_editMPerpReX->text().toDouble());
		t_real MPerpReY = t_real(m_editMPerpReY->text().toDouble());
		t_real MPerpReZ = t_real(m_editMPerpReZ->text().toDouble());
		t_real MPerpImX = t_real(m_editMPerpImX->text().toDouble());
		t_real MPerpImY = t_real(m_editMPerpImY->text().toDouble());
		t_real MPerpImZ = t_real(m_editMPerpImZ->text().toDouble());

		t_real PiX = t_real(m_editPiX->text().toDouble());
		t_real PiY = t_real(m_editPiY->text().toDouble());
		t_real PiZ = t_real(m_editPiZ->text().toDouble());

		const t_cplx N(NRe, NIm);
Tobias WEBER's avatar
Tobias WEBER committed
468
		const t_vec Mperp = tl2::create<t_vec>({
469
470
471
			t_cplx(MPerpReX,MPerpImX),
			t_cplx(MPerpReY,MPerpImY),
			t_cplx(MPerpReZ,MPerpImZ) });
Tobias WEBER's avatar
Tobias WEBER committed
472
		const t_vec Pi = tl2::create<t_vec>({PiX, PiY, PiZ});
473
474

		// calculate final polarisation vector and intensity
Tobias WEBER's avatar
Tobias WEBER committed
475
476
		auto [I, P_f] = tl2::blume_maleev_indir<t_mat, t_vec, t_cplx>(Pi, Mperp, N);
		//auto [I, P_f] = tl2::blume_maleev<t_vec, t_cplx>(Pi, Mperp, N);
477
478

		// set final polarisation
479
480
481
		m_editPfX->setText(tl2::var_to_str(P_f[0].real(), m_prec).c_str());
		m_editPfY->setText(tl2::var_to_str(P_f[1].real(), m_prec).c_str());
		m_editPfZ->setText(tl2::var_to_str(P_f[2].real(), m_prec).c_str());
482
483
484
485
486
487


		// update 3d objects
		if(m_3dobjsReady)
		{
			// P_i
Tobias WEBER's avatar
Tobias WEBER committed
488
			t_mat_gl matPi = tl2::get_arrow_matrix<t_vec_gl, t_mat_gl, t_real_gl>(
Tobias WEBER's avatar
Tobias WEBER committed
489
				tl2::create<t_vec_gl>({t_real_gl(PiX), t_real_gl(PiY), t_real_gl(PiZ)}), 	// to
490
				1., 								// scale
Tobias WEBER's avatar
Tobias WEBER committed
491
492
				tl2::create<t_vec_gl>({0,0,0.5}),		// translate
				tl2::create<t_vec_gl>({0,0,1}));		// from
Tobias WEBER's avatar
Tobias WEBER committed
493
			m_plot->GetRenderer()->SetObjectMatrix(m_arrow_pi, matPi);
494
495

			// P_f
Tobias WEBER's avatar
Tobias WEBER committed
496
			t_mat_gl matPf = tl2::get_arrow_matrix<t_vec_gl, t_mat_gl, t_real_gl>(
Tobias WEBER's avatar
Tobias WEBER committed
497
				tl2::create<t_vec_gl>({t_real_gl(P_f[0].real()), t_real_gl(P_f[1].real()), t_real_gl(P_f[2].real())}), 	// to
498
				1., 								// scale
Tobias WEBER's avatar
Tobias WEBER committed
499
500
				tl2::create<t_vec_gl>({0,0,0.5}),		// translate
				tl2::create<t_vec_gl>({0,0,1}));		// from
Tobias WEBER's avatar
Tobias WEBER committed
501
			m_plot->GetRenderer()->SetObjectMatrix(m_arrow_pf, matPf);
502
503
504

			// Re(M)
			const t_real_gl lenReM = t_real_gl(std::sqrt(MPerpReX*MPerpReX + MPerpReY*MPerpReY + MPerpReZ*MPerpReZ));
Tobias WEBER's avatar
Tobias WEBER committed
505
			t_mat_gl matMRe = tl2::get_arrow_matrix<t_vec_gl, t_mat_gl, t_real_gl>(
Tobias WEBER's avatar
Tobias WEBER committed
506
				tl2::create<t_vec_gl>({t_real_gl(MPerpReX), t_real_gl(MPerpReY), t_real_gl(MPerpReZ)}), 	// to
507
				lenReM,								// scale
Tobias WEBER's avatar
Tobias WEBER committed
508
509
				tl2::create<t_vec_gl>({0,0,0.5}),		// translate
				tl2::create<t_vec_gl>({0,0,1}));		// from
Tobias WEBER's avatar
Tobias WEBER committed
510
511
			m_plot->GetRenderer()->SetObjectMatrix(m_arrow_M_Re, matMRe);
			m_plot->GetRenderer()->SetObjectVisible(m_arrow_M_Re, !tl2::equals(lenReM, t_real_gl(0)));
512
513
514

			// Im(M)
			const t_real_gl lenImM = t_real_gl(std::sqrt(MPerpImX*MPerpImX + MPerpImY*MPerpImY + MPerpImZ*MPerpImZ));
Tobias WEBER's avatar
Tobias WEBER committed
515
			t_mat_gl matMIm = tl2::get_arrow_matrix<t_vec_gl, t_mat_gl, t_real_gl>(
Tobias WEBER's avatar
Tobias WEBER committed
516
				tl2::create<t_vec_gl>({t_real_gl(MPerpImX), t_real_gl(MPerpImY), t_real_gl(MPerpImZ)}), 	// to
517
				lenImM,								// scale
Tobias WEBER's avatar
Tobias WEBER committed
518
519
				tl2::create<t_vec_gl>({0,0,0.5}),		// translate
				tl2::create<t_vec_gl>({0,0,1}));		// from
Tobias WEBER's avatar
Tobias WEBER committed
520
521
			m_plot->GetRenderer()->SetObjectMatrix(m_arrow_M_Im, matMIm);
			m_plot->GetRenderer()->SetObjectVisible(m_arrow_M_Im, !tl2::equals(lenImM, t_real_gl(0)));
Tobias WEBER's avatar
Tobias WEBER committed
522
523

			m_plot->update();
524
525
526
527
528
529
530
531
532
		}
	}
};
// ----------------------------------------------------------------------------




// ----------------------------------------------------------------------------
Tobias WEBER's avatar
Tobias WEBER committed
533
534
535
#ifndef BUILD_LIB	// build application


536
537
int main(int argc, char** argv)
{
Tobias WEBER's avatar
Tobias WEBER committed
538
	tl2::set_gl_format(1, _GL_MAJ_VER, _GL_MIN_VER, 8);
Tobias WEBER's avatar
Tobias WEBER committed
539
	tl2::set_locales();
Tobias WEBER's avatar
Tobias WEBER committed
540

Tobias WEBER's avatar
Tobias WEBER committed
541
	QApplication::addLibraryPath(QString(".") + QDir::separator() + "qtplugins");
542
543
544
545
546
547
	auto app = std::make_unique<QApplication>(argc, argv);
	auto dlg = std::make_unique<PolDlg>(nullptr);
	dlg->show();

	return app->exec();
}
Tobias WEBER's avatar
Tobias WEBER committed
548
549
550
551
552
553
554


#else	// build library


#include <boost/dll/alias.hpp>

Tobias WEBER's avatar
Tobias WEBER committed
555

Tobias WEBER's avatar
Tobias WEBER committed
556
557
558
/**
 * initialise plugin
 */
Tobias WEBER's avatar
Tobias WEBER committed
559
bool init()
Tobias WEBER's avatar
Tobias WEBER committed
560
{
Tobias WEBER's avatar
Tobias WEBER committed
561
	tl2::set_gl_format(1, _GL_MAJ_VER, _GL_MIN_VER, 8);
Tobias WEBER's avatar
Tobias WEBER committed
562
	tl2::set_locales();
Tobias WEBER's avatar
Tobias WEBER committed
563
564
565

	return true;
}
Tobias WEBER's avatar
Tobias WEBER committed
566

Tobias WEBER's avatar
Tobias WEBER committed
567

Tobias WEBER's avatar
Tobias WEBER committed
568
569
/**
 * plugin descriptor
Tobias WEBER's avatar
Tobias WEBER committed
570
 * type; title; description
Tobias WEBER's avatar
Tobias WEBER committed
571
 */
Tobias WEBER's avatar
Tobias WEBER committed
572
const char* descr()
Tobias WEBER's avatar
Tobias WEBER committed
573
{
Tobias WEBER's avatar
Tobias WEBER committed
574
	return "dlg;Polarisation Vectors;Calculates polarisation vectors.";
Tobias WEBER's avatar
Tobias WEBER committed
575
576
577
578
579
580
}


/**
 * create the plugin main dialog
 */
Tobias WEBER's avatar
Tobias WEBER committed
581
QDialog* create(QWidget *pParent)
Tobias WEBER's avatar
Tobias WEBER committed
582
583
{
	//std::cout << "In " << __FUNCTION__ << std::endl;
Tobias WEBER's avatar
Tobias WEBER committed
584
585
586
587
588
589
590
591
592
593
594
	return new PolDlg(pParent);
}


/**
 * destroy the plugin main dialog
 */
void destroy(QDialog* dlg)
{
	//std::cout << "In " << __FUNCTION__ << std::endl;
	if(dlg) delete dlg;
Tobias WEBER's avatar
Tobias WEBER committed
595
596
}

Tobias WEBER's avatar
Tobias WEBER committed
597
598

BOOST_DLL_ALIAS(init, tl_init);
Tobias WEBER's avatar
Tobias WEBER committed
599
BOOST_DLL_ALIAS(descr, tl_descr);
Tobias WEBER's avatar
Tobias WEBER committed
600
BOOST_DLL_ALIAS(create, tl_create);
Tobias WEBER's avatar
Tobias WEBER committed
601
BOOST_DLL_ALIAS(destroy, tl_destroy);
Tobias WEBER's avatar
Tobias WEBER committed
602
603
604


#endif
605
// ----------------------------------------------------------------------------