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

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