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

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