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

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