Blame view

main.cpp 10.3 KB
0c9bf8ae   dmayerich   Case-sensitive er...
1
  #include <fstream>

4198e3be   dmayerich   Added polystyrene.
2
  #include <iostream>

0c9bf8ae   dmayerich   Case-sensitive er...
3
4
  using namespace std;

  #include "interactivemie.h"

887d4441   David Mayerich   updates from STIM...
5
  #include <QtWidgets/QApplication>

4198e3be   dmayerich   Added polystyrene.
6
7
  #include <QGraphicsScene>

  #include <QGraphicsView>

39a7d6e9   dmayerich   Added dialog supp...
8
  #include <QGraphicsPixmapItem>

4198e3be   dmayerich   Added polystyrene.
9
  #include <QLayout>

0c9bf8ae   dmayerich   Case-sensitive er...
10
  #include "qtSpectrumDisplay.h"

8ffb8373   dmayerich   Improved material...
11
  //#include "qwtSpectrumDisplay.h"

0c9bf8ae   dmayerich   Case-sensitive er...
12
13
14
15
  #include "globals.h"

  #include "rtsGUIConsole.h"

  #include "PerformanceData.h"

  #include <complex>

8ffb8373   dmayerich   Improved material...
16
  #include <sstream>

0c9bf8ae   dmayerich   Case-sensitive er...
17
18
  //#include <direct.h>

  

8ffb8373   dmayerich   Improved material...
19
  

0c9bf8ae   dmayerich   Case-sensitive er...
20
21
  PerformanceData PD;

  

4198e3be   dmayerich   Added polystyrene.
22
  qtSpectrumDisplay* gpSpectrumDisplay;

8ffb8373   dmayerich   Improved material...
23
  //qwtSpectrumDisplay* SpectrumDisplay;

4198e3be   dmayerich   Added polystyrene.
24
25
26
  

  QGraphicsScene* distortionScene = NULL;

  QGraphicsView* distortionWindow = NULL;

39a7d6e9   dmayerich   Added dialog supp...
27
  QGraphicsPixmapItem* pixmapItem = NULL;

0c9bf8ae   dmayerich   Case-sensitive er...
28
29
30
31
32
  

  vector<vector<SpecPair> > RefSpectrum;

  vector<SpecPair> SimSpectrum;

  vector<SpecPair> EtaK;

  vector<SpecPair> EtaN;

4198e3be   dmayerich   Added polystyrene.
33
34
35
36
37
38
39
40
  

  //source spectrum

  bool useSourceSpectrum = false;

  vector<SpecPair> SourceSpectrum;

  

  //resample the source based on the material samples (EtaK and EtaN)

  vector<SpecPair> SourceResampled;

  

0c9bf8ae   dmayerich   Case-sensitive er...
41
42
43
44
45
46
47
48
49
  int currentSpec = 0;

  

  double nuMin = 800;

  double nuMax = 4000;

  double dNu = 2;

  

  double aMin = 0;

  double aMax = 1;

  

8ffb8373   dmayerich   Improved material...
50
51
52
  double nMag = 1.0;

  double kMax = 1.0;

  

0c9bf8ae   dmayerich   Case-sensitive er...
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  double scaleI0 = 1.0;

  double refSlope = 0.0;

  

  bool dispRefSpec = true;

  bool dispSimSpec = true;

  bool dispSimK = true;

  bool dispMatK = true;

  bool dispSimN = true;

  bool dispMatN = true;

  double dispScaleK = 1.0;

  double dispScaleN = 1.0;

  SpecType dispSimType = AbsorbanceSpecType;

  bool dispNormalize = false;

  double dispNormFactor = 1.0;

  

  

  //material parameters

  double radius = 4.0f;

  double baseIR = 1.49f;

  double cA = 1.0;

  //vector<SpecPair> KMaterial;

  //vector<SpecPair> NMaterial;

  bool applyMaterial = true;

  vector<Material> MaterialList;

8ffb8373   dmayerich   Improved material...
77
  int currentMaterial = -1;

0c9bf8ae   dmayerich   Case-sensitive er...
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  

  //optical parameters

  double cNAi = 0.0;

  double cNAo = 0.6;

  double oNAi = 0.0;

  double oNAo = 0.6;

  OpticsType opticsMode = TransmissionOpticsType;

  bool pointDetector = false;

  int objectiveSamples = 200;

  

  //fitting parameters

  double minMSE = 0.00001;

  int maxFitIter = 20;

  

  void TempSimSpectrum()

  {

  	SpecPair temp;

  	for(int i=800; i<4000; i++)

  	{

  		temp.nu = i;

  		temp.A = sin((double)i/200);

  		SimSpectrum.push_back(temp);

  	}

8ffb8373   dmayerich   Improved material...
101
  	}

0c9bf8ae   dmayerich   Case-sensitive er...
102
  

8ffb8373   dmayerich   Improved material...
103
  	void UpdateDisplay() {

0c9bf8ae   dmayerich   Case-sensitive er...
104
  	gpSpectrumDisplay->updateGL();

8ffb8373   dmayerich   Improved material...
105
  	//SpectrumDisplay->replot();

0c9bf8ae   dmayerich   Case-sensitive er...
106
107
  }

  

8ffb8373   dmayerich   Improved material...
108
  void LoadMaterial(string fileName, string materialName)

0c9bf8ae   dmayerich   Case-sensitive er...
109
  {

8ffb8373   dmayerich   Improved material...
110
111
112
113
114
115
  	//open the file

  	ifstream inFile(fileName.c_str());

  	if(!inFile)

  	{

  		cout<<"Error loading material: "<<fileName<<endl;

  		return;

0c9bf8ae   dmayerich   Case-sensitive er...
116
117
  	}

  

8ffb8373   dmayerich   Improved material...
118
119
120
121
122
  	Material newMaterial;

  	newMaterial.validN = false;

  	newMaterial.validK = false;

  	//read the header

  	string units;

887d4441   David Mayerich   updates from STIM...
123
  	//getline(inFile, units, '\t');

8ffb8373   dmayerich   Improved material...
124
125
126
127
128
129
  	char c = 0;

  	while(c != '\n')

  	{

  		inFile.get(c);

  		if(c == 'n') newMaterial.validN = true;

  		if(c == 'k') newMaterial.validK = true;

0c9bf8ae   dmayerich   Case-sensitive er...
130
  	}

0c9bf8ae   dmayerich   Case-sensitive er...
131
  

8ffb8373   dmayerich   Improved material...
132
133
134
135
  	//read the entire refractive index (both real and imaginary)

  	float nu;

  	float n;

  	float k;

0c9bf8ae   dmayerich   Case-sensitive er...
136
  

8ffb8373   dmayerich   Improved material...
137
138
139
140
141
142
143
144
145
146
147
148
149
150
  	while(inFile>>nu)

  	{

  		n = 1.0;

  		k = 0.0;

  		if(newMaterial.validN)

  			inFile>>n;

  		if(newMaterial.validK)

  			inFile>>k;

  		//ignore the rest of the line

  		inFile.ignore();

  

  		newMaterial.nu.push_back(nu);

  		newMaterial.eta.push_back(complex<double>(n, k));

  	}

0c9bf8ae   dmayerich   Case-sensitive er...
151
  

8ffb8373   dmayerich   Improved material...
152
153
154
155
156
  	//iterate through the material to compute the necessary bounds for display

  	float minN = 9999;

  	float maxN = -9999;

  	float sumN = 0;

  	float maxK = 0;

887d4441   David Mayerich   updates from STIM...
157
  	for(unsigned int i = 0; i<newMaterial.nu.size(); i++)

0c9bf8ae   dmayerich   Case-sensitive er...
158
  	{

8ffb8373   dmayerich   Improved material...
159
160
161
162
163
164
165
  		n = newMaterial.eta[i].real();

  		k = newMaterial.eta[i].imag();

  

  		if(n < minN) minN = n;

  		if(n > maxN) maxN = n;

  		if(k > maxK) maxK = k;

  		sumN += n;

0c9bf8ae   dmayerich   Case-sensitive er...
166
  	}

8ffb8373   dmayerich   Improved material...
167
  	float meanN = sumN / newMaterial.nu.size();

0c9bf8ae   dmayerich   Case-sensitive er...
168
  

8ffb8373   dmayerich   Improved material...
169
170
171
172
173
  	nMag = max(maxN - meanN, meanN - minN);

  	kMax = maxK;

  	baseIR = meanN;

  

  	//set the name of the material object

0c9bf8ae   dmayerich   Case-sensitive er...
174
  	newMaterial.name = materialName;

0c9bf8ae   dmayerich   Case-sensitive er...
175
  

8ffb8373   dmayerich   Improved material...
176
  	//add it to the material list

0c9bf8ae   dmayerich   Case-sensitive er...
177
  	MaterialList.push_back(newMaterial);

8ffb8373   dmayerich   Improved material...
178
  	currentMaterial = MaterialList.size() - 1;

0c9bf8ae   dmayerich   Case-sensitive er...
179
180
  }

  

4198e3be   dmayerich   Added polystyrene.
181
182
  void LoadSource(string fileNameSource)

  {

8ffb8373   dmayerich   Improved material...
183
  	SourceSpectrum = LoadSpectrum(fileNameSource);

4198e3be   dmayerich   Added polystyrene.
184
185
186
187
188
189
  }

  

  void ResampleSource()

  {

  	//clear the current resampled spectrum

  	SourceResampled.clear();

8ffb8373   dmayerich   Improved material...
190
  

4198e3be   dmayerich   Added polystyrene.
191
192
193
  	//get the number of source and material samples

  	int nMatSamples = EtaK.size();

  	int nSourceSamples = SourceSpectrum.size();

8ffb8373   dmayerich   Improved material...
194
  

4198e3be   dmayerich   Added polystyrene.
195
196
197
198
  	for(int i=0; i<nMatSamples; i++)

  	{

  		SpecPair newSample;

  		newSample.nu = EtaK[i].nu;

8ffb8373   dmayerich   Improved material...
199
  

4198e3be   dmayerich   Added polystyrene.
200
201
202
203
204
205
206
207
208
209
  		//iterate through the SourceSpectrum to find the bounding wavelengths

  		if(newSample.nu < SourceSpectrum[0].nu)

  			newSample.A = 0.0;

  		else if(newSample.nu > SourceSpectrum[nSourceSamples-1].nu)

  			newSample.A = 0.0;

  		else

  		{

  			int k=1;

  			while(SourceSpectrum[k].nu < newSample.nu)

  				k++;

8ffb8373   dmayerich   Improved material...
210
  

4198e3be   dmayerich   Added polystyrene.
211
212
213
214
  			//interpolate

  			float a = (newSample.nu - SourceSpectrum[k-1].nu)/(SourceSpectrum[k].nu - SourceSpectrum[k-1].nu);

  			newSample.A = a * SourceSpectrum[k].A + (1.0 - a) * SourceSpectrum[k-1].A;

  		}

8ffb8373   dmayerich   Improved material...
215
  

4198e3be   dmayerich   Added polystyrene.
216
217
218
219
  		//insert the new spectral point into the resampled spectrum

  		SourceResampled.push_back(newSample);

  		//cout<<newSample.nu<<"   "<<newSample.A<<endl;

  	}

8ffb8373   dmayerich   Improved material...
220
  

4198e3be   dmayerich   Added polystyrene.
221
222
  }

  

8ffb8373   dmayerich   Improved material...
223
  void FitDisplay() {

0c9bf8ae   dmayerich   Case-sensitive er...
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
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
284
285
286
  	double minA = 99999.0;

  	double maxA = -99999.0;

  	double k, n;

  

  	if(dispSimSpec)

  		for(unsigned int i=0; i<SimSpectrum.size(); i++)

  		{

  			if(SimSpectrum[i].A < minA)

  				minA = SimSpectrum[i].A;

  			if(SimSpectrum[i].A > maxA)

  				maxA = SimSpectrum[i].A;

  		}

  

  	if(dispRefSpec && RefSpectrum.size() > 0)

  		for(unsigned int i=0; i<RefSpectrum[currentSpec].size(); i++)

  		{

  			if(RefSpectrum[currentSpec][i].A < minA)

  				minA = RefSpectrum[currentSpec][i].A;

  			if(RefSpectrum[currentSpec][i].A > maxA)

  				maxA = RefSpectrum[currentSpec][i].A;

  		}

  	if(dispMatK)

  		for(unsigned int i=0; i<EtaK.size(); i++)

  		{

  			k = MaterialList[currentMaterial].eta[i].imag() * dispScaleK;

  			if(k < minA)

  				minA = k;

  			if(k > maxA)

  				maxA = k;

  		}

  	if(dispSimK)

  		for(unsigned int i=0; i<EtaK.size(); i++)

  		{

  			k = EtaK[i].A * dispScaleK;

  			if(k < minA)

  				minA = k;

  			if(EtaK[i].A > maxA)

  				maxA = k;

  		}

  	if(dispMatN)

  		for(unsigned int i=0; i<EtaN.size(); i++)

  		{

  			n = (MaterialList[currentMaterial].eta[i].real() - baseIR) * dispScaleN;

  			if(n < minA)

  				minA = n;

  			if(n > maxA)

  				maxA = n;

  		}

  	if(dispSimN)

  		for(unsigned int i=0; i<EtaN.size(); i++)

  		{

  			n = (EtaN[i].A - baseIR) * dispScaleN;

  			if(n < minA)

  				minA = n;

  			if(n > maxA)

  				maxA = n;

  		}

  

  	aMin = minA;

  	aMax = maxA;

  	UpdateDisplay();

  }

  

8ffb8373   dmayerich   Improved material...
287
  void ChangeAbsorbance() {

0c9bf8ae   dmayerich   Case-sensitive er...
288
289
  

  	//compute the real part of the index of refraction

39a7d6e9   dmayerich   Added dialog supp...
290
  

0c9bf8ae   dmayerich   Case-sensitive er...
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
  	//copy the absorbance values into a linear array

  	int nSamples = MaterialList[currentMaterial].eta.size();

  	double startNu = MaterialList[currentMaterial].nu.front();

  	double endNu = MaterialList[currentMaterial].nu.back();

  	double* k = (double*)malloc(sizeof(double) * nSamples);

  	double* n = (double*)malloc(sizeof(double) * nSamples);

  	for(int i=0; i<nSamples; i++)

  		k[i] = MaterialList[currentMaterial].eta[i].imag() * cA;

  

  	//NMaterial.clear();

  	EtaK.clear();

  	EtaN.clear();

  	//use Kramers Kronig to determine the real part of the index of refraction

  	cudaKramersKronig(n, k, nSamples, startNu, endNu, baseIR);

  

  	//copy the real part of the index of refraction into the vector

  	SpecPair temp;

  

  	//load the imaginary IR from the absorbance data

  	double nu;

8ffb8373   dmayerich   Improved material...
311
  	for(int i=0; i<nSamples; i++) {

0c9bf8ae   dmayerich   Case-sensitive er...
312
  		nu = MaterialList[currentMaterial].nu[i];

8ffb8373   dmayerich   Improved material...
313
  		if(nu >= nuMin && nu <= nuMax) {

0c9bf8ae   dmayerich   Case-sensitive er...
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
  			temp.nu = nu;

  			temp.A = k[i];

  			EtaK.push_back(temp);

  			//temp.A = NMaterial[i].A;

  			temp.A = n[i];

  			EtaN.push_back(temp);

  		}

  	}

  

  	free(k);

  	free(n);

  }

  

  void SetMaterial()

  {

  	EtaK.clear();

  	EtaN.clear();

8ffb8373   dmayerich   Improved material...
331
  	if(currentMaterial == -1) return;

0c9bf8ae   dmayerich   Case-sensitive er...
332
333
334
335
336
337
338
339
  

  	int nSamples = MaterialList[currentMaterial].eta.size();

  	double nu;

  	SpecPair temp;

  

  	//initialize the current nuMin and nuMax values

  	nuMin = MaterialList[currentMaterial].nu[0];

  	nuMax = nuMin;

8ffb8373   dmayerich   Improved material...
340
  	for(int i=0; i<nSamples; i++) {

0c9bf8ae   dmayerich   Case-sensitive er...
341
342
343
344
345
346
347
  		nu = MaterialList[currentMaterial].nu[i];

  		//if(nu >= nuMin && nu <= nuMax){

  

  		//update the min and max values for display

  		if(nu < nuMin) nuMin = nu;

  		if(nu > nuMax) nuMax = nu;

  

8ffb8373   dmayerich   Improved material...
348
349
350
351
352
  		temp.nu = nu;

  		temp.A = MaterialList[currentMaterial].eta[i].imag();

  		EtaK.push_back(temp);

  		temp.A = MaterialList[currentMaterial].eta[i].real();

  		EtaN.push_back(temp);

0c9bf8ae   dmayerich   Case-sensitive er...
353
354
  	}

  	cA = 1.0;

8ffb8373   dmayerich   Improved material...
355
  

4198e3be   dmayerich   Added polystyrene.
356
357
358
  	//resample the source spectrum

  	if(SourceSpectrum.size() != 0)

  		ResampleSource();

0c9bf8ae   dmayerich   Case-sensitive er...
359
360
361
362
363
364
365
  

  }

  

  

  

  int main(int argc, char *argv[])

  {

39a7d6e9   dmayerich   Added dialog supp...
366
  

0c9bf8ae   dmayerich   Case-sensitive er...
367
368
369
  

  	//load the default project file (any previous optical settings)

  	LoadState();

39a7d6e9   dmayerich   Added dialog supp...
370
  

8ffb8373   dmayerich   Improved material...
371
  

0c9bf8ae   dmayerich   Case-sensitive er...
372
  	//load the default materials

887d4441   David Mayerich   updates from STIM...
373
374
375
376
377
378
379
  	//LoadMaterial("etaToluene.txt", "Toluene");

  	//LoadMaterial("kPolystyrene.txt", "Polystyrene");
  	LoadMaterial("etaPolystyrene.txt", "Polystyrene");
  	LoadMaterial("etaPMMA.txt", "PMMA");

  	//LoadMaterial("kPMMA.txt", "PMMA");

  	//LoadMaterial("kPolyethylene.txt", "Polyethylene");

  	//LoadMaterial("kPTFE.txt", "Teflon");

8ffb8373   dmayerich   Improved material...
380
381
382
383
384
  

  

  	//LoadMaterial("eta_TolueneK.txt", "eta_TolueneN.txt", "Toluene");

  	//LoadMaterial("kPMMA.txt", "PMMA");

  	//LoadMaterial("eta_polystyreneK.txt", "Polystyrene");

0c9bf8ae   dmayerich   Case-sensitive er...
385
386
  	//LoadMaterial("../../../../data/materials/rtsSU8_k.txt", "../../../../data/materials/rtsSU8_n.txt", "SU8");

  	SetMaterial();

8ffb8373   dmayerich   Improved material...
387
388
  

  

4198e3be   dmayerich   Added polystyrene.
389
390
  	//load a mid-infrared source

  	LoadSource("source_midIR.txt");

8ffb8373   dmayerich   Improved material...
391
  

4198e3be   dmayerich   Added polystyrene.
392
  	ResampleSource();

0c9bf8ae   dmayerich   Case-sensitive er...
393
394
395
396
  

  	//compute the analytical solution for the Mie scattered spectrum

  	SimulateSpectrum();

  

4198e3be   dmayerich   Added polystyrene.
397
  	QApplication a(argc, argv);

8ffb8373   dmayerich   Improved material...
398
399
400
  

  	//SpectrumDisplay = new qwtSpectrumDisplay();

  

0c9bf8ae   dmayerich   Case-sensitive er...
401
  	InteractiveMie w;

4198e3be   dmayerich   Added polystyrene.
402
  	w.show() ;

0c9bf8ae   dmayerich   Case-sensitive er...
403
  	w.move(0, 0);

4198e3be   dmayerich   Added polystyrene.
404
405
406
407
  	QSize uiFrame = w.frameSize() + QSize(10, 10);

  	cout<<"Frame: "<<uiFrame.width()<<endl;

  	QSize uiNoFrame = w.size();

  	cout<<"No Frame: "<<uiNoFrame.width()<<endl;

39a7d6e9   dmayerich   Added dialog supp...
408
  	int frameHeight = uiFrame.height() - uiNoFrame.height();

0c9bf8ae   dmayerich   Case-sensitive er...
409
410
  

  	//activate a console for output

4198e3be   dmayerich   Added polystyrene.
411
412
413
414
  	RedirectIOToConsole(0, uiFrame.height(), uiFrame.width(), 400);

  	printf("Frame height: %d\n", frameHeight);

  

  	//set the size and position of the spectrum window

39a7d6e9   dmayerich   Added dialog supp...
415
416
  	int visWinSize = uiFrame.height()/2 - frameHeight;

  	//create the far field window

0c9bf8ae   dmayerich   Case-sensitive er...
417
  	gpSpectrumDisplay = new qtSpectrumDisplay();

39a7d6e9   dmayerich   Added dialog supp...
418
419
  	gpSpectrumDisplay->resize(visWinSize*2, visWinSize);

  	gpSpectrumDisplay->move(uiFrame.width(), 0);

4198e3be   dmayerich   Added polystyrene.
420
421
  	gpSpectrumDisplay->show();

  

8ffb8373   dmayerich   Improved material...
422
423
  

  

4198e3be   dmayerich   Added polystyrene.
424
425
426
427
428
429
430
431
  	//distortion dialog box

  	distortionDialog = new qtDistortionDialog();

  	distortionDialog->move(0, 0);

  

  	//display the distortion map

  	distortionScene = new QGraphicsScene();

  	distortionWindow = new QGraphicsView(distortionScene);

  	distortionWindow->move(uiFrame.width(), visWinSize);

0c9bf8ae   dmayerich   Case-sensitive er...
432
433
434
435
436
437
  

  	//refresh the UI

  	w.refreshUI();

  

  	return a.exec();

  }