source: trunk/FACT++/gui/BasicGlCamera.cc@ 12018

Last change on this file since 12018 was 12012, checked in by lyard, 13 years ago
Now take the config files from methods instead of files
File size: 38.1 KB
Line 
1#include "BasicGlCamera.h"
2#include <math.h>
3#include <fstream>
4#include <iostream>
5#include <string>
6#include <sstream>
7#include "src/tools.h"
8
9const PixelMapEntry PixelMap::empty = { 0, 0, 0, 0, 0, 0 };
10
11//static variables
12PixelMap BasicGlCamera::fPixelMap;
13GLfloat BasicGlCamera::pixelsCoords[MAX_NUM_PIXELS][3];
14PixelsNeighbors BasicGlCamera::neighbors[MAX_NUM_PIXELS];
15int BasicGlCamera::hardwareMapping[NPIX];
16GLfloat BasicGlCamera::verticesList[NPIX*6][2];
17vector<edge> BasicGlCamera::patchesIndices[160];
18int BasicGlCamera::verticesIndices[NPIX][6];
19int BasicGlCamera::pixelsPatch[NPIX];
20int BasicGlCamera::softwareMapping[NPIX];
21int BasicGlCamera::patches[160][9];
22
23//Coordinates of an hexagon of radius 1 and center 0
24GLfloat hexcoords[6][2] = {{-1./sqrt(3.), 1},
25 { 1./sqrt(3.), 1},
26 { 2./sqrt(3.), 0},
27 { 1./sqrt(3.), -1},
28 {-1./sqrt(3.), -1},
29 {-2./sqrt(3.), 0}};
30
31
32
33 BasicGlCamera::BasicGlCamera(QWidget* cParent) : QGLWidget(cParent)
34 {
35 setFormat(QGLFormat(QGL::DoubleBuffer));// | QGL::DepthBuffer));
36 fWhite = -1;
37 fWhitePatch = -1;
38 fMin = -1;
39 fMax = -1;
40 autoRefresh = false;
41 logScale = false;
42 cameraRotation = 0;
43 unitsText = "";
44 titleText = "";
45 dataText = "";
46 pixelContourColour[0] = 0.1f;
47 pixelContourColour[1] = 0.1f;
48 pixelContourColour[2] = 0.1f;
49 patchesCoulour[0] = 0.1f;
50 patchesCoulour[1] = 0.1f;
51 patchesCoulour[2] = 0.1f;
52 highlightedPatchesCoulour[0] = 0.6f;
53 highlightedPatchesCoulour[1] = 0.6f;
54 highlightedPatchesCoulour[2] = 0.6f;
55 highlightedPixelsCoulour[0] = 0.8f;
56 highlightedPixelsCoulour[1] = 0.8f;
57 highlightedPixelsCoulour[2] = 0.8f;
58
59 regularPalettePlease(true);
60
61
62 hexRadius = 0.015f;
63 hexTolerance = hexRadius/100.0f;
64 viewSize = 1.0f;
65 calculatePixelsCoords();
66
67/* ifstream fin2("MasterList-v3.txt");
68 if (!fin2.is_open())
69 {
70 cout << "Error: file \"MasterList-v3\" missing. aborting." << endl;
71 exit(-1);
72 }
73 int l = 0;
74 string buf;
75 while (getline(fin2, buf, '\n'))
76 {
77 buf = Tools::Trim(buf);
78 if (buf[0]=='#')
79 continue;
80
81 unsigned int softid, hardid, dummy;
82
83 stringstream str(buf);
84
85 str >> softid;
86 str >> dummy;
87 str >> hardid;
88
89 if (softid>=1440)
90 continue;
91
92 hardwareMapping[softid] = hardid;
93 softwareMapping[hardid] = softid;
94
95 l++;
96 }*/
97 buildVerticesList();
98/*
99 ifstream fin1("Trigger-Patches.txt");
100 if (!fin1.is_open())
101 {
102 cout << "Error: file \"Trigger-Patches.txt\" missing. Aborting." << endl;
103 exit(-1);
104 }
105 l=0;
106 while (getline(fin1, buf, '\n'))
107 {
108 buf = Tools::Trim(buf);
109 if (buf[0]=='#')
110 continue;
111
112 stringstream str(buf);
113 for (int i=0; i<9; i++)
114 {
115 unsigned int n;
116 str >> n;
117
118 if (n>=1440)
119 continue;
120
121 patches[l][i] = hardwareMapping[n];
122 }
123 l++;
124 }
125
126 //now construct the correspondance between pixels and patches
127 for (int i=0;i<NTMARK;i++)
128 for (int j=0;j<9;j++)
129 pixelsPatch[softwareMapping[patches[i][j]]] = i;
130
131 for (int i=0;i<1440;i++)
132 updateNeighbors(i);
133
134 buildPatchesIndices();
135*/////////////////////////////////
136 ss[0] = 0; ss[1] = 0.25f; ss[2] = 0.5f; ss[3] = 0.75f; ss[4] = 1.0f;
137 rr[0] = 0.15; rr[1] = 0; rr[2] = 0; rr[3] = 1.0f; rr[4] = 0.85f;
138 gg[0] = 0.15; gg[1] = 0; gg[2] = 1; gg[3] = 0; gg[4] = 0.85f;
139 bb[0] = 0.15; bb[1] = 1; bb[2] = 0; bb[3] = 0; bb[4] = 0.85f;
140
141 fPixelStride = 1;
142 fcSlice = 0;
143 fData.resize(1440);
144 for (int i=0;i<NPIX;i++)
145 fData[i] = (double)i;///1.44;//(double)(i)/(double)(ACTUAL_NUM_PIXELS);
146
147 setFont(QFont("Arial", 8));
148 int buttonShift=0;
149 scaleLabel = new QLabel("Scale", this);
150// buttonShift += scaleLabel->height();
151
152 linearButton = new QRadioButton("Linear", this);
153 linearButton->move(scaleLabel->width(), buttonShift);
154 buttonShift += linearButton->height();
155
156 logButton = new QRadioButton("Log", this);
157 logButton->move(scaleLabel->width(), buttonShift);
158 buttonShift += logButton->height()*1.1f;
159
160 colorPaletteLabel = new QLabel("Colour\nPalette", this);
161 colorPaletteLabel->move(0, buttonShift);
162 // buttonShift += colorPaletteLabel->height();
163
164 regularPaletteButton = new QRadioButton("Regular", this);
165 regularPaletteButton->move(colorPaletteLabel->width(), buttonShift);
166 buttonShift += regularPaletteButton->height();
167
168 prettyPaletteButton = new QRadioButton("Pretty", this);
169 prettyPaletteButton->move(colorPaletteLabel->width(), buttonShift);
170 buttonShift += prettyPaletteButton->height();
171
172 greyScalePaletteButton = new QRadioButton("Grey Scale", this);
173 greyScalePaletteButton->move(colorPaletteLabel->width(), buttonShift);
174 buttonShift += greyScalePaletteButton->height();
175
176 glowingPaletteButton = new QRadioButton("Glowing", this);
177 glowingPaletteButton->move(colorPaletteLabel->width(), buttonShift);
178 buttonShift += glowingPaletteButton->height()*1.1f;
179
180 rotationLabel = new QLabel("Camera\nRotation", this);
181 rotationLabel->move(0, buttonShift);
182 // buttonShift += rotationLabel->height();
183
184 unsigned short utf16Array;
185 utf16Array = 0x00b0;
186 QString degreeSymbol(QString::fromUtf16(&utf16Array, 1));
187 QString zerostr("0" + degreeSymbol);
188 zeroRotationButton = new QRadioButton(zerostr, this);
189 zeroRotationButton->move(rotationLabel->width(), buttonShift);
190 buttonShift += zeroRotationButton->height();
191 QString minus90str("-90" + degreeSymbol);
192 minus90RotationButton = new QRadioButton(minus90str, this);
193 minus90RotationButton->move(rotationLabel->width(), buttonShift);
194 buttonShift += minus90RotationButton->height();
195 QString plus90str("+90"+degreeSymbol);
196 plus90Rotationbutton = new QRadioButton(plus90str, this);
197 plus90Rotationbutton->move(rotationLabel->width(), buttonShift);
198
199
200 scaleGroup = new QButtonGroup(this);
201 colorGroup = new QButtonGroup(this);
202 rotationGroup = new QButtonGroup(this);
203 scaleGroup->addButton(linearButton);
204 scaleGroup->addButton(logButton);
205 colorGroup->addButton(regularPaletteButton);
206 colorGroup->addButton(prettyPaletteButton);
207 colorGroup->addButton(greyScalePaletteButton);
208 colorGroup->addButton(glowingPaletteButton);
209 rotationGroup->addButton(zeroRotationButton);
210 rotationGroup->addButton(minus90RotationButton);
211 rotationGroup->addButton(plus90Rotationbutton);
212
213 linearButton->setChecked(true);
214 regularPaletteButton->setChecked(true);
215 zeroRotationButton->setChecked(true);
216
217// linearButton->palette.setColor();
218
219 linearButton->setAutoFillBackground(true);
220 logButton->setAutoFillBackground(true);
221 regularPaletteButton->setAutoFillBackground(true);
222 prettyPaletteButton->setAutoFillBackground(true);
223 greyScalePaletteButton->setAutoFillBackground(true);
224 glowingPaletteButton->setAutoFillBackground(true);
225 zeroRotationButton->setAutoFillBackground(true);
226 minus90RotationButton->setAutoFillBackground(true);
227 plus90Rotationbutton->setAutoFillBackground(true);
228 scaleLabel->setAutoFillBackground(true);
229 colorPaletteLabel->setAutoFillBackground(true);
230 rotationLabel->setAutoFillBackground(true);
231
232 linearButton->hide();
233 logButton->hide();
234 regularPaletteButton->hide();
235 prettyPaletteButton->hide();
236 greyScalePaletteButton->hide();
237 glowingPaletteButton->hide();
238 zeroRotationButton->hide();
239 minus90RotationButton->hide();
240 plus90Rotationbutton->hide();
241 scaleLabel->hide();
242 colorPaletteLabel->hide();
243 rotationLabel->hide();
244
245 connect(linearButton, SIGNAL(toggled(bool)),
246 this, SLOT(linearScalePlease(bool)));
247 connect(logButton, SIGNAL(toggled(bool)),
248 this, SLOT(logScalePlease(bool)));
249 connect(regularPaletteButton, SIGNAL(toggled(bool)),
250 this, SLOT(regularPalettePlease(bool)));
251 connect(prettyPaletteButton, SIGNAL(toggled(bool)),
252 this, SLOT(prettyPalettePlease(bool)));
253 connect(greyScalePaletteButton, SIGNAL(toggled(bool)),
254 this, SLOT(greyScalePalettePlease(bool)));
255 connect(glowingPaletteButton, SIGNAL(toggled(bool)),
256 this, SLOT(glowingPalettePlease(bool)));
257 connect(zeroRotationButton, SIGNAL(toggled(bool)),
258 this, SLOT(zeroRotationPlease(bool)));
259 connect(minus90RotationButton, SIGNAL(toggled(bool)),
260 this, SLOT(plus90RotationPlease(bool)));
261 connect(plus90Rotationbutton, SIGNAL(toggled(bool)),
262 this, SLOT(minus90RotationPlease(bool)));
263
264 connect(this, SIGNAL(signalUpdateCamera()),
265 this, SLOT(timedUpdate()));
266 }
267 BasicGlCamera::~BasicGlCamera()
268 {
269 }
270 void BasicGlCamera::assignPixelMap(PixelMap& map)
271 {
272 fPixelMap = map;
273 for (auto i=fPixelMap.begin();i!=fPixelMap.end(); i++)
274 {
275 hardwareMapping[i->index] = i->hw();
276 softwareMapping[i->hw()] = i->index;
277 }
278 }
279 void BasicGlCamera::assignTriggerPatchesMap(vector<int>& pa)
280 {
281 vector<vector<int> > intervec;
282 intervec.resize(160);
283 int i=0;
284 for (auto it=pa.begin(); it != pa.end(); it++)
285 {
286 intervec[*it].push_back(hardwareMapping[i]);
287 i++;
288 }
289 for (i=0;i<160;i++)
290 {
291 for (int j=0;j<9;j++)
292 {
293 patches[i][j] = intervec[i][j];
294 }
295 }
296
297 //now construct the correspondance between pixels and patches
298 for (int i=0;i<NTMARK;i++)
299 for (int j=0;j<9;j++)
300 pixelsPatch[softwareMapping[patches[i][j]]] = i;
301
302 for (int i=0;i<1440;i++)
303 updateNeighbors(i);
304
305 buildPatchesIndices();
306 }
307 void BasicGlCamera::setPatchColor(int id, float color[3])
308 {
309 for (int i=0;i<9;i++)
310 for (int j=0;j<3;j++)
311 pixelsColor[softwareMapping[patches[id][i]]][j] = color[j];
312 }
313 void BasicGlCamera::setUnits(const string& units)
314 {
315 unitsText = units;
316 pixelColorUpToDate = false;
317 if (isVisible() && autoRefresh)
318 updateGL();
319 }
320 void BasicGlCamera::setTitle(const string& title)
321 {
322 titleText = title;
323 pixelColorUpToDate = false;
324 if (isVisible() && autoRefresh)
325 updateGL();
326 }
327 void BasicGlCamera::SetWhite(int idx)
328 {
329 fWhite = idx;
330 fWhitePatch = pixelsPatch[fWhite];
331 if (isVisible() && autoRefresh)
332 updateGL();
333// CalculatePatchColor();
334 }
335 void BasicGlCamera::SetMin(int64_t min)
336 {
337// cout << "min: " << min << endl;
338 fMin = min;
339 pixelColorUpToDate = false;
340 if (isVisible() && autoRefresh)
341 updateGL();
342 }
343 void BasicGlCamera::SetMax(int64_t max)
344 {
345// cout << "max: " << max << endl;
346 fMax = max;
347 pixelColorUpToDate = false;
348 if (isVisible() && autoRefresh)
349 updateGL();
350 }
351 void BasicGlCamera::linearScalePlease(bool checked)
352 {
353 if (!checked) return;
354 logScale = false;
355 pixelColorUpToDate = false;
356 emit colorPaletteHasChanged();
357 if (isVisible() && autoRefresh)
358 updateGL();
359 }
360 void BasicGlCamera::UpdateText()
361 {
362 ostringstream str;
363 float min, max, mean, rms, median;
364 min = max = fData[0];
365 mean = 0;
366 rms = 0;
367 median = 0;
368 multiset<double> medianSet;
369 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
370 {
371 if (fData[i] < min)
372 min = fData[i];
373 if (fData[i] > max)
374 max = fData[i];
375 mean += fData[i];
376 rms += fData[i]*fData[i];
377 medianSet.insert(fData[i]);
378 }
379 mean /= ACTUAL_NUM_PIXELS;
380 rms /= ACTUAL_NUM_PIXELS;
381 rms = sqrt(rms);
382// cout << "Size of the set: " << medianSet.size() << endl;
383 multiset<double>::iterator it = medianSet.begin();
384 for (int i=0;i<(ACTUAL_NUM_PIXELS/2)-1;i++)
385 it++;
386 median = *it;
387 it++;
388 median += *it;
389 median /= 2;
390
391 str << "Min: " << min << endl << " Max: " << max << " Mean: " << mean << " RMS: " << rms << " Median: " << median;
392 str << " Units: " << unitsText;
393 dataText = str.str();
394
395 fmin = min;
396 fmax = max;
397 fmean = mean;
398 frms = rms;
399 fmedian = median;
400 }
401 void BasicGlCamera::DrawCameraText()
402 {
403 glPushMatrix();
404 glLoadIdentity();
405// cout << width() << " " << height() << endl;
406 int textSize = (int)(height()*14/600);
407 setFont(QFont("Times", textSize));
408 qglColor(QColor(255,223,127));
409 float shiftx = 0.01f;//0.55f;
410 float shifty = 0.01f;//0.65f;
411 renderText(-shownSizex/2.f + shiftx, 0.f, 0.f, QString(dataText.c_str()));//-shownSizey/2.f + shifty, 0.f, QString(dataText.c_str()));
412
413
414// int textLength = titleText.size();
415 renderText(-shownSizex/2.f + shiftx, shownSizey/2.f - textSize*pixelSize - shifty, 0.f, QString(titleText.c_str()));
416
417 glPopMatrix();
418
419 textSize = (int)(600*14/600);
420 setFont(QFont("Times", textSize));
421 }
422 void BasicGlCamera::DrawScale()
423 {
424 glPushMatrix();
425 glLoadIdentity();
426 glPushAttrib(GL_POLYGON_BIT);
427 glShadeModel(GL_SMOOTH);
428 glBegin(GL_QUADS);
429 float oneX = shownSizex/2.f - shownSizex/50.f;
430 float twoX = shownSizex/2.f;
431 float oneY = -shownSizey/2.f;
432 float twoY = -shownSizey/4.f;
433 float threeY = 0;
434 float fourY = shownSizey/4.f;
435 float fiveY = shownSizey/2.f;
436 glColor3f(rr[0], gg[0], bb[0]);
437 glVertex2f(oneX, oneY);
438 glVertex2f(twoX, oneY);
439 glColor3f(rr[1], gg[1], bb[1]);
440 glVertex2f(twoX, twoY);
441 glVertex2f(oneX, twoY);
442
443 glVertex2f(oneX, twoY);
444 glVertex2f(twoX, twoY);
445 glColor3f(rr[2], gg[2], bb[2]);
446 glVertex2f(twoX, threeY);
447 glVertex2f(oneX, threeY);
448
449 glVertex2f(oneX, threeY);
450 glVertex2f(twoX, threeY);
451 glColor3f(rr[3], gg[3], bb[3]);
452 glVertex2f(twoX, fourY);
453 glVertex2f(oneX, fourY);
454
455 glVertex2f(oneX, fourY);
456 glVertex2f(twoX, fourY);
457 glColor3f(rr[4], gg[4], bb[4]);
458 glVertex2f(twoX, fiveY);
459 glVertex2f(oneX, fiveY);
460 float zeroX = oneX - shownSizex/50.f;
461 float zeroY = fiveY - shownSizey/50.f;
462 glColor3fv(tooHighValueCoulour);
463 glVertex2f(zeroX, fiveY);
464 glVertex2f(oneX, fiveY);
465 glVertex2f(oneX, zeroY);
466 glVertex2f(zeroX, zeroY);
467 glColor3fv(tooLowValueCoulour);
468 glVertex2f(zeroX, -fiveY);
469 glVertex2f(oneX, -fiveY);
470 glVertex2f(oneX, -zeroY);
471 glVertex2f(zeroX, -zeroY);
472 glEnd();
473 //draw linear/log tick marks
474 glColor3f(0.f,0.f,0.f);
475 glBegin(GL_LINES);
476 float value;
477 for (int i=1;i<10;i++)
478 {
479 if (logScale)
480 value = log10(i);
481 else
482 value = (float)(i)/10.f;
483 float yy = -shownSizey/2.f + value*shownSizey;
484 glVertex2f(oneX, yy);
485 glVertex2f(twoX, yy);
486 }
487 glEnd();
488 glPopAttrib();
489 glPopMatrix();
490 }
491 void BasicGlCamera::logScalePlease(bool checked)
492 {
493 if (!checked) return;
494 logScale = true;
495 pixelColorUpToDate = false;
496 emit colorPaletteHasChanged();
497 if (isVisible() && autoRefresh)
498 updateGL();
499 }
500 void BasicGlCamera::regularPalettePlease(bool checked)
501 {
502 if (!checked) return;
503 ss[0] = 0; ss[1] = 0.25f; ss[2] = 0.5f; ss[3] = 0.75f; ss[4] = 1.0f;
504 rr[0] = 0.15; rr[1] = 0; rr[2] = 0; rr[3] = 1.0f; rr[4] = 0.85f;
505 gg[0] = 0.15; gg[1] = 0; gg[2] = 1; gg[3] = 0; gg[4] = 0.85f;
506 bb[0] = 0.15; bb[1] = 1; bb[2] = 0; bb[3] = 0; bb[4] = 0.85f;
507 tooHighValueCoulour[0] = 1.f;
508 tooHighValueCoulour[1] = 1.f;
509 tooHighValueCoulour[2] = 0.f;
510 tooLowValueCoulour[0] = 0.f;
511 tooLowValueCoulour[1] = 1.f;
512 tooLowValueCoulour[2] = 1.f;
513 pixelColorUpToDate = false;
514
515 emit colorPaletteHasChanged();
516
517 if (isVisible() && autoRefresh)
518 updateGL();
519 }
520 void BasicGlCamera::prettyPalettePlease(bool checked)
521 {
522 if (!checked) return;
523 ss[0] = 0.f; ss[1] = 0.25f; ss[2] = 0.5f; ss[3] = 0.75f; ss[4] = 1.0f;
524 rr[0] = 0.f; rr[1] = 0.35f; rr[2] = 0.85f; rr[3] = 1.0f; rr[4] = 1.f;
525 gg[0] = 0.f; gg[1] = 0.10f; gg[2] = 0.20f; gg[3] = 0.73f; gg[4] = 1.f;
526 bb[0] = 0.f; bb[1] = 0.03f; bb[2] = 0.06f; bb[3] = 0.00f; bb[4] = 1.f;
527 tooHighValueCoulour[0] = 0.f;
528 tooHighValueCoulour[1] = 1.f;
529 tooHighValueCoulour[2] = 0.f;
530 tooLowValueCoulour[0] = 0.f;
531 tooLowValueCoulour[1] = 0.f;
532 tooLowValueCoulour[2] = 1.f;
533 pixelColorUpToDate = false;
534
535 emit colorPaletteHasChanged();
536
537 if (isVisible() && autoRefresh)
538 updateGL();
539 }
540 void BasicGlCamera::greyScalePalettePlease(bool checked)
541 {
542 if (!checked) return;
543 ss[0] = 0; ss[1] = 0.25f; ss[2] = 0.5f; ss[3] = 0.75f; ss[4] = 1.0f;
544 rr[0] = 0; rr[1] = 0.25f; rr[2] = 0.5f; rr[3] = 0.75f; rr[4] = 1.0f;
545 gg[0] = 0; gg[1] = 0.25f; gg[2] = 0.5f; gg[3] = 0.75f; gg[4] = 1.0f;
546 bb[0] = 0; bb[1] = 0.25f; bb[2] = 0.5f; bb[3] = 0.75f; bb[4] = 1.0f;
547 tooHighValueCoulour[0] = 0.f;
548 tooHighValueCoulour[1] = 1.f;
549 tooHighValueCoulour[2] = 0.f;
550 tooLowValueCoulour[0] = 0.f;
551 tooLowValueCoulour[1] = 0.f;
552 tooLowValueCoulour[2] = 1.f;
553 pixelColorUpToDate = false;
554
555 emit colorPaletteHasChanged();
556
557 if (isVisible() && autoRefresh)
558 updateGL();
559 }
560 void BasicGlCamera::glowingPalettePlease(bool checked)
561 {
562 if (!checked) return;
563 ss[0] = 0; ss[1] = 0.25f; ss[2] = 0.5f; ss[3] = 0.75f; ss[4] = 1.0f;
564 rr[0] = 0.15; rr[1] = 0.5; rr[2] = 1.f; rr[3] = 0.0f; rr[4] = 1.f;
565 gg[0] = 0.15; gg[1] = 0.5; gg[2] = 1.f; gg[3] = 0.5f; gg[4] = 0.5f;
566 bb[0] = 0.15; bb[1] = 0.5; bb[2] = 1; bb[3] = 1.f; bb[4] = 0.f;
567 tooHighValueCoulour[0] = 1.f;
568 tooHighValueCoulour[1] = 0.f;
569 tooHighValueCoulour[2] = 0.f;
570 tooLowValueCoulour[0] = 0.f;
571 tooLowValueCoulour[1] = 1.f;
572 tooLowValueCoulour[2] = 0.f;
573 pixelColorUpToDate = false;
574
575 emit colorPaletteHasChanged();
576
577 if (isVisible() && autoRefresh)
578 updateGL();
579 }
580 void BasicGlCamera::SetAutoRefresh(bool on)
581 {
582 autoRefresh = on;
583 if (isVisible() && autoRefresh)
584 updateGL();
585 }
586 void BasicGlCamera::zeroRotationPlease(bool checked)
587 {
588 if (!checked) return;
589 cameraRotation = 0;
590 pixelColorUpToDate = false;
591 if (isVisible() && autoRefresh)
592 updateGL();
593 }
594 void BasicGlCamera::plus90RotationPlease(bool checked)
595 {
596 if (!checked) return;
597 cameraRotation = 90;
598 pixelColorUpToDate = false;
599 if (isVisible() && autoRefresh)
600 updateGL();
601 }
602 void BasicGlCamera::minus90RotationPlease(bool checked)
603 {
604 if (!checked) return;
605 cameraRotation = -90;
606 pixelColorUpToDate = false;
607 if (isVisible() && autoRefresh)
608 updateGL();
609 }
610
611 void BasicGlCamera::initializeGL()
612 {
613 qglClearColor(QColor(25,25,38));
614 glShadeModel(GL_FLAT);
615 glDisable(GL_DEPTH_TEST);
616 glDisable(GL_CULL_FACE);
617
618 glEnable (GL_LINE_SMOOTH);
619 glEnable (GL_BLEND);
620 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
621 glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
622
623 }
624 void BasicGlCamera::resizeGL(int cWidth, int cHeight)
625 {
626 glViewport(0, 0, cWidth, cHeight);
627 glMatrixMode(GL_PROJECTION);
628 glLoadIdentity();
629 GLfloat windowRatio = (float)cWidth/(float)cHeight;
630 if (windowRatio < 1)
631 {
632 windowRatio = 1.0f/windowRatio;
633 gluOrtho2D(-viewSize, viewSize, -viewSize*windowRatio, viewSize*windowRatio);
634 pixelSize = 2*viewSize/(float)cWidth;
635 shownSizex = 2*viewSize;
636 shownSizey = 2*viewSize*windowRatio;
637 }
638 else
639 {
640 gluOrtho2D(-viewSize*windowRatio, viewSize*windowRatio, -viewSize, viewSize);
641 pixelSize = 2*viewSize/(float)cHeight;
642 shownSizex = 2*viewSize*windowRatio;
643 shownSizey = 2*viewSize;
644 }
645 glMatrixMode(GL_MODELVIEW);
646 }
647 void BasicGlCamera::paintGL()
648 {
649 glClear(GL_COLOR_BUFFER_BIT);
650 glLoadIdentity();
651
652 glTranslatef(0,-0.44,0);
653 glScalef(1.5, 1.5, 1.5);
654
655 drawCamera(true);
656
657 drawPatches();
658 }
659 void BasicGlCamera::toggleInterfaceDisplay()
660 {
661 if (linearButton->isVisible())
662 {
663 linearButton->hide();
664 logButton->hide();
665 regularPaletteButton->hide();
666 prettyPaletteButton->hide();
667 greyScalePaletteButton->hide();
668 glowingPaletteButton->hide();
669 zeroRotationButton->hide();
670 minus90RotationButton->hide();
671 plus90Rotationbutton->hide();
672 scaleLabel->hide();
673 colorPaletteLabel->hide();
674 rotationLabel->hide();
675 }
676 else
677 {
678 linearButton->show();
679 logButton->show();
680 regularPaletteButton->show();
681 prettyPaletteButton->show();
682 greyScalePaletteButton->show();
683 glowingPaletteButton->show();
684 zeroRotationButton->show();
685 minus90RotationButton->show();
686 plus90Rotationbutton->show();
687 scaleLabel->show();
688 colorPaletteLabel->show();
689 rotationLabel->show();
690 }
691 }
692
693 void BasicGlCamera::mousePressEvent(QMouseEvent *)
694 {
695
696 }
697 void BasicGlCamera::mouseMoveEvent(QMouseEvent *)
698 {
699
700 }
701 void BasicGlCamera::mouseDoubleClickEvent(QMouseEvent *)
702 {
703
704 }
705 void BasicGlCamera::timedUpdate()
706 {
707 if (isVisible())
708 updateGL();
709 }
710 void BasicGlCamera::updateCamera()
711 {
712 emit signalUpdateCamera();
713 }
714 void BasicGlCamera::drawCamera(bool alsoWire)
715 {
716// cout << "Super PaintGL" << endl;
717 glColor3f(0.5,0.5,0.5);
718 glLineWidth(1.0);
719 float color;
720
721 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
722 {
723 color = float(fData[i*fPixelStride+fcSlice]);// + ]eventData[nRoi*i + whichSlice]+(VALUES_SPAN/2))/(float)(VALUES_SPAN-1);
724 int index = 0;
725 while (ss[index] < color)
726 index++;
727 index--;
728 float weight0 = (color-ss[index]) / (ss[index+1]-ss[index]);
729 float weight1 = 1.0f-weight0;
730 pixelsColor[i][0] = weight1*rr[index] + weight0*rr[index+1];
731 pixelsColor[i][1] = weight1*gg[index] + weight0*gg[index+1];
732 pixelsColor[i][2] = weight1*bb[index] + weight0*bb[index+1];
733 }
734
735 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
736 {
737 // if (i == 690 ||
738 // i == 70)
739 // continue;
740 glColor3fv(pixelsColor[i]);
741 glLoadName(i);
742
743 drawHexagon(i,true);
744
745 }
746 if (!alsoWire)
747 return;
748 glColor3f(0.0f,0.0f,0.0f);
749 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
750 {
751// if (i == 690 ||
752// i == 70)
753// continue;
754 drawHexagon(i, false);
755 }
756 }
757 void BasicGlCamera::drawPatches()
758 {
759 glLineWidth(2.0f);
760 float backupRadius = hexRadius;
761 hexRadius *= 0.95;
762 glColor3f(0.5f, 0.5f, 0.3f);
763 glBegin(GL_LINES);
764 for (int i=0;i<NTMARK;i++)
765 {
766 for (unsigned int j=0;j<patchesIndices[i].size();j++)
767 {
768 glVertex2fv(verticesList[patchesIndices[i][j].first]);
769 glVertex2fv(verticesList[patchesIndices[i][j].second]);
770 }
771 }
772 glEnd();
773 hexRadius = backupRadius;
774 }
775 int BasicGlCamera::PixelAtPosition(const QPoint &cPos)
776 {
777 const int MaxSize = 512;
778 GLuint buffer[MaxSize];
779 GLint viewport[4];
780
781 makeCurrent();
782
783 glGetIntegerv(GL_VIEWPORT, viewport);
784 glSelectBuffer(MaxSize, buffer);
785 glRenderMode(GL_SELECT);
786
787 glInitNames();
788 glPushName(0);
789
790 glMatrixMode(GL_PROJECTION);
791 glPushMatrix();
792 glLoadIdentity();
793 GLfloat windowRatio = GLfloat(width()) / GLfloat(height());
794 gluPickMatrix(GLdouble(cPos.x()), GLdouble(viewport[3] - cPos.y()),
795 1.0, 1.0, viewport);
796
797 if (windowRatio < 1)
798 {
799 windowRatio = 1.0f/windowRatio;
800 gluOrtho2D(-viewSize, viewSize, -viewSize*windowRatio, viewSize*windowRatio);
801 }
802 else
803 {
804 gluOrtho2D(-viewSize*windowRatio, viewSize*windowRatio, -viewSize, viewSize);
805 }
806
807 glMatrixMode(GL_MODELVIEW);
808 drawCamera(false);
809 glMatrixMode(GL_PROJECTION);
810 glPopMatrix();
811
812 //for some reason that I cannot understand, the push/pop matrix doesn't do the trick here... bizarre
813 //ok, so re-do the resizeGL thing.
814 resizeGL(width(), height());
815
816 if (!glRenderMode(GL_RENDER))
817 return -1;
818
819 return buffer[3];
820 }
821 void BasicGlCamera::drawHexagon(int index, bool solid)
822 {
823/* float minX, maxX, minY, maxY;
824 minX = minY = 1e10;
825 maxX = maxY = -1e10;
826 for (int i=0;i<1438;i++)
827 {
828 for (int j=0;j<6;j++)
829 {
830 if (verticesList[verticesIndices[i][j]][0] > maxX)
831 maxX = verticesList[verticesIndices[i][j]][0];
832 if (verticesList[verticesIndices[i][j]][0] < minX)
833 minX = verticesList[verticesIndices[i][j]][0];
834 if (verticesList[verticesIndices[i][j]][1] > maxY)
835 maxY = verticesList[verticesIndices[i][j]][1];
836 if (verticesList[verticesIndices[i][j]][1] < minY)
837 minY = verticesList[verticesIndices[i][j]][1];
838 }
839 }
840 cout << "Min, Max X: " << minX << " " << maxX << endl;
841 cout << "Min, Max Y: " << minY << " " << maxY << endl;
842 exit(0);*/
843 if (solid)
844 glBegin(GL_POLYGON);
845 else
846 glBegin(GL_LINE_LOOP);
847
848 glVertex2fv(verticesList[verticesIndices[index][0]]);
849 glVertex2fv(verticesList[verticesIndices[index][1]]);
850 glVertex2fv(verticesList[verticesIndices[index][2]]);
851 glVertex2fv(verticesList[verticesIndices[index][3]]);
852 glVertex2fv(verticesList[verticesIndices[index][4]]);
853 glVertex2fv(verticesList[verticesIndices[index][5]]);
854 if (solid)
855 glVertex2fv(verticesList[verticesIndices[index][0]]);
856
857 glEnd();
858 }
859
860 void BasicGlCamera::updateNeighbors(int currentPixel)
861 {
862 float squaredDistance = 0;
863 for (int i=0;i<currentPixel;i++)
864 {
865 squaredDistance = (pixelsCoords[i][0] - pixelsCoords[currentPixel][0])*
866 (pixelsCoords[i][0] - pixelsCoords[currentPixel][0]) +
867 (pixelsCoords[i][1] - pixelsCoords[currentPixel][1])*
868 (pixelsCoords[i][1] - pixelsCoords[currentPixel][1]);
869 if (squaredDistance < 4*hexRadius*hexRadius*(1.0f+hexTolerance))//neighbor !
870 {//ok, but which one ?
871 if (fabs(pixelsCoords[i][0] - pixelsCoords[currentPixel][0]) < hexTolerance &&
872 pixelsCoords[i][1] < pixelsCoords[currentPixel][1]){//top
873 neighbors[i][0] = currentPixel;
874 neighbors[currentPixel][3] = i;
875 continue;}
876 if (fabs(pixelsCoords[i][0] - pixelsCoords[currentPixel][0]) < hexTolerance &&
877 pixelsCoords[i][1] > pixelsCoords[currentPixel][1]){//bottom
878 neighbors[i][3] = currentPixel;
879 neighbors[currentPixel][0] = i;
880 continue;}
881 if (pixelsCoords[i][0] > pixelsCoords[currentPixel][0] &&
882 pixelsCoords[i][1] > pixelsCoords[currentPixel][1]){//top right
883 neighbors[i][4] = currentPixel;
884 neighbors[currentPixel][1] = i;
885 continue;}
886 if (pixelsCoords[i][0] > pixelsCoords[currentPixel][0] &&
887 pixelsCoords[i][1] < pixelsCoords[currentPixel][1]){//bottom right
888 neighbors[i][5] = currentPixel;
889 neighbors[currentPixel][2] = i;
890 continue;}
891 if (pixelsCoords[i][0] < pixelsCoords[currentPixel][0] &&
892 pixelsCoords[i][1] > pixelsCoords[currentPixel][1]){//top left
893 neighbors[i][2] = currentPixel;
894 neighbors[currentPixel][5] = i;
895 continue;}
896 if (pixelsCoords[i][0] < pixelsCoords[currentPixel][0] &&
897 pixelsCoords[i][1] < pixelsCoords[currentPixel][1]){//bottom left
898 neighbors[i][1] = currentPixel;
899 neighbors[currentPixel][4] = i;
900 continue;}
901 }
902 }
903 }
904 void BasicGlCamera::skipPixels(int start, int howMany)
905 {
906 for (int i=start;i<MAX_NUM_PIXELS-howMany;i++)
907 {
908 pixelsCoords[i][0] = pixelsCoords[i+howMany][0];
909 pixelsCoords[i][1] = pixelsCoords[i+howMany][1];
910 }
911 }
912 void BasicGlCamera::calculatePixelsCoords()
913 {
914 if (pixelsCoords[0][1] >= 0.299999 && pixelsCoords[0][1] <= 0.300001)
915 return;
916 pixelsCoords[0][0] = 0;
917 pixelsCoords[0][1] = 0.3;
918 pixelsCoords[0][2] = 0;
919 pixelsCoords[1][0] = 0;
920 pixelsCoords[1][1] = 0.3+2*hexRadius;
921 pixelsCoords[1][2] = 0;
922 neighbors[0][0] = 1;
923 neighbors[1][3] = 0;
924 //from which side of the previous hexagon are we coming from ?
925 int fromSide = 3;
926 //to which side are we heading to ?
927 int toSide = 0;
928 for (int i=2;i<MAX_NUM_PIXELS;i++)
929 {
930 toSide = fromSide-1;
931 if (toSide < 0)
932 toSide =5;
933 while (neighbors[i-1][toSide] >= 0)
934 {
935 toSide--;
936 if (toSide < 0)
937 toSide = 5;
938 }
939 fromSide = toSide + 3;
940 if (fromSide > 5)
941 fromSide -= 6;
942 //ok. now we now in which direction we're heading
943 pixelsCoords[i][0] = pixelsCoords[i-1][0];
944 pixelsCoords[i][1] = pixelsCoords[i-1][1];
945 pixelsCoords[i][2] = pixelsCoords[i-1][2];
946 switch (toSide)
947 {
948 case 0:
949 pixelsCoords[i][1] += 2*hexRadius;
950 break;
951 case 1:
952 pixelsCoords[i][0] += (2*hexRadius)*sin(M_PI/3.0);
953 pixelsCoords[i][1] += (2*hexRadius)*cos(M_PI/3.0);
954 break;
955 case 2:
956 pixelsCoords[i][0] += (2*hexRadius)*sin(M_PI/3.0);
957 pixelsCoords[i][1] -= (2*hexRadius)*cos(M_PI/3.0);
958 break;
959 case 3:
960 pixelsCoords[i][1] -= 2*hexRadius;
961 break;
962 case 4:
963 pixelsCoords[i][0] -= (2*hexRadius)*sin(M_PI/3.0);
964 pixelsCoords[i][1] -= (2*hexRadius)*cos(M_PI/3.0);
965 break;
966 case 5:
967 pixelsCoords[i][0] -= (2*hexRadius)*sin(M_PI/3.0);
968 pixelsCoords[i][1] += (2*hexRadius)*cos(M_PI/3.0);
969 break;
970 };
971
972 updateNeighbors(i);
973 }
974 //Ok. So now we've circled around all the way to MAX_NUM_PIXELS
975 //do the required shifts so that it matches the fact camera up to ACTUAL_NUM_PIXELS pixels
976 //remember the location pixels 1438 and 1439, and re-assign them later on
977 GLfloat backupCoords[4];
978 skipPixels(1200, 1);
979 skipPixels(1218, 3);
980 skipPixels(1236, 1);
981 skipPixels(1256, 1);
982 skipPixels(1274, 3);
983 skipPixels(1292, 3);
984 skipPixels(1309, 6);
985 skipPixels(1323, 7);
986 skipPixels(1337, 6);
987 skipPixels(1354, 6);
988 skipPixels(1368, 7);
989 //la c'est dans 1390 qu'il y a 1439
990 backupCoords[0] = pixelsCoords[1390][0];
991 backupCoords[1] = pixelsCoords[1390][1];
992 skipPixels(1382, 9);
993 skipPixels(1394, 12);
994 skipPixels(1402, 15);
995 skipPixels(1410, 12);
996 //la c'est dans 1422 qu'il y a 1438
997 backupCoords[2] = pixelsCoords[1422][0];
998 backupCoords[3] = pixelsCoords[1422][1];
999 skipPixels(1422, 12);
1000 skipPixels(1430, 15);
1001
1002 pixelsCoords[1438][0] = backupCoords[2];
1003 pixelsCoords[1438][1] = backupCoords[3];
1004 pixelsCoords[1439][0] = backupCoords[0];
1005 pixelsCoords[1439][1] = backupCoords[1];
1006 }
1007 void BasicGlCamera::buildVerticesList()
1008 {
1009 numVertices = 0;
1010 GLfloat cVertex[2];
1011 for (int i=0;i<NPIX;i++)
1012 {
1013 for (int j=0;j<6;j++)
1014 {
1015 for (int k=0;k<2;k++)
1016 cVertex[k] = hexcoords[j][k]*hexRadius + pixelsCoords[i][k];
1017 bool found = false;
1018 for (int k=0;k<numVertices;k++)
1019 {
1020 if ((cVertex[0] - verticesList[k][0])*
1021 (cVertex[0] - verticesList[k][0]) +
1022 (cVertex[1] - verticesList[k][1])*
1023 (cVertex[1] - verticesList[k][1]) < hexTolerance*hexTolerance)
1024 {
1025 found = true;
1026 break;
1027 }
1028 }
1029 if (!found)
1030 {
1031 for (int k=0;k<2;k++)
1032 verticesList[numVertices][k] = cVertex[k];
1033 numVertices++;
1034 }
1035 }
1036 }
1037//cout << "numVertices: " << numVertices << endl;
1038 for (int i=0;i<NPIX;i++)
1039 {
1040 for (int j=0;j<6;j++)
1041 {
1042 for (int k=0;k<2;k++)
1043 cVertex[k] = hexcoords[j][k]*hexRadius + pixelsCoords[i][k];
1044 for (int k=0;k<numVertices;k++)
1045 {
1046 if ((cVertex[0] - verticesList[k][0])*
1047 (cVertex[0] - verticesList[k][0]) +
1048 (cVertex[1] - verticesList[k][1])*
1049 (cVertex[1] - verticesList[k][1]) < hexTolerance*hexTolerance)
1050 {
1051 verticesIndices[i][j] = k;
1052 break;
1053 }
1054 }
1055 }
1056 }
1057 }
1058 void BasicGlCamera::buildPatchesIndices()
1059 {
1060 vector<edge>::iterator it;
1061 bool erased = false;
1062// patchesIndices.resize(NTMARK);
1063 for (int i=0;i<NTMARK;i++)//for all patches
1064 {
1065 patchesIndices[i].clear();
1066 for (int j=0;j<9;j++)//for all cells of the current patch
1067 {
1068 if (softwareMapping[patches[i][j]] >= ACTUAL_NUM_PIXELS)
1069 continue;
1070 for (int k=0;k<6;k++)//for all sides of the current cell
1071 {
1072 int first = k-1;
1073 int second = k;
1074 if (first < 0)
1075 first = 5;
1076 erased = false;
1077 for (it=(patchesIndices[i]).begin(); it != (patchesIndices[i]).end(); it++)//check if this side is here already or not
1078 {
1079 if (((*it).first == verticesIndices[softwareMapping[patches[i][j]]][first] &&
1080 (*it).second == verticesIndices[softwareMapping[patches[i][j]]][second]) ||
1081 ((*it).first == verticesIndices[softwareMapping[patches[i][j]]][second] &&
1082 (*it).second == verticesIndices[softwareMapping[patches[i][j]]][first]))
1083 {
1084 patchesIndices[i].erase(it);
1085 erased = true;
1086 break;
1087 }
1088 }
1089 if (!erased)
1090 {
1091 edge temp;
1092 temp.first = verticesIndices[softwareMapping[patches[i][j]]][first];
1093 temp.second = verticesIndices[softwareMapping[patches[i][j]]][second];
1094 patchesIndices[i].push_back(temp);
1095 }
1096 }
1097 }
1098 }
1099// for (int i=0;i<NTMARK;i++)
1100// {
1101// cout << ".....................patch " << i << " size: " << patchesIndices[i].size() << endl;
1102// for (unsigned int j=0;j<patchesIndices[i].size();j++)
1103// {
1104// if (patchesIndices[i][j].first < 0 || patchesIndices[i][j].first > 3013)
1105// cout << patchesIndices[i][j].first << " and " << patchesIndices[i][j].second << " and " << j << endl;
1106// }
1107// }
1108 }
1109
Note: See TracBrowser for help on using the repository browser.