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

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