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

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