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

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