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

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