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

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