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

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