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

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