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

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