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

Last change on this file since 12075 was 12074, checked in by lyard, 14 years ago
removed anti-aliasing
File size: 39.0 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 min = max = fData[0];
379 double mean = 0;
380 double rms = 0;
381 median = 0;
382 // multiset<double> medianSet;
383 vector<double> medianVec;
384 medianVec.resize(ACTUAL_NUM_PIXELS);
385 auto it = medianVec.begin();
386 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
387 {
388 if (fData[i] < min)
389 min = fData[i];
390 if (fData[i] > max)
391 max = fData[i];
392 mean += fData[i];
393 rms += fData[i]*fData[i];
394 //medianSet.insert(fData[i]);
395 *it = fData[i];
396 it++;
397 }
398
399// vector<double> medianVec;
400// medianVec.resize(ACTUAL_NUM_PIXELS);
401// int iii=0;
402// for (auto it=medianVec.begin(); it != medianVec.end(); it++) {
403// *it = fData[iii];
404// iii++;
405// }
406 sort(medianVec.begin(), medianVec.end());
407
408
409 mean /= ACTUAL_NUM_PIXELS;
410 rms = sqrt(rms/ACTUAL_NUM_PIXELS - mean * mean);
411
412// multiset<double>::iterator it = medianSet.begin();
413 auto jt = medianVec.begin();
414 for (int i=0;i<(ACTUAL_NUM_PIXELS/2)-1;i++)
415 {
416// it++;
417 jt++;
418 }
419 median = *jt;
420 // cout << *it << " " << *jt << endl;
421 jt++;
422 median += *jt;
423 median /= 2;
424
425 str << "Min: " << min << endl << " Max: " << max << " Mean: " << mean << " RMS: " << rms << " Median: " << median;
426 str << " Units: " << unitsText;
427 dataText = str.str();
428
429 fmin = min;
430 fmax = max;
431 fmean = mean;
432 frms = rms;
433 fmedian = median;
434 }
435 void BasicGlCamera::DrawCameraText()
436 {
437 glPushMatrix();
438 glLoadIdentity();
439// cout << width() << " " << height() << endl;
440 int textSize = (int)(height()*14/600);
441 setFont(QFont("Times", textSize));
442 qglColor(QColor(255,223,127));
443 float shiftx = 0.01f;//0.55f;
444 float shifty = 0.01f;//0.65f;
445 renderText(-shownSizex/2.f + shiftx, 0.f, 0.f, QString(dataText.c_str()));//-shownSizey/2.f + shifty, 0.f, QString(dataText.c_str()));
446
447
448// int textLength = titleText.size();
449 renderText(-shownSizex/2.f + shiftx, shownSizey/2.f - textSize*pixelSize - shifty, 0.f, QString(titleText.c_str()));
450
451 glPopMatrix();
452
453 textSize = (int)(600*14/600);
454 setFont(QFont("Times", textSize));
455 }
456 void BasicGlCamera::DrawScale()
457 {
458 glPushMatrix();
459 glLoadIdentity();
460 glPushAttrib(GL_POLYGON_BIT);
461 glShadeModel(GL_SMOOTH);
462 glBegin(GL_QUADS);
463 float oneX = shownSizex/2.f - shownSizex/50.f;
464 float twoX = shownSizex/2.f;
465 float oneY = -shownSizey/2.f;
466 float twoY = -shownSizey/4.f;
467 float threeY = 0;
468 float fourY = shownSizey/4.f;
469 float fiveY = shownSizey/2.f;
470 glColor3f(rr[0], gg[0], bb[0]);
471 glVertex2f(oneX, oneY);
472 glVertex2f(twoX, oneY);
473 glColor3f(rr[1], gg[1], bb[1]);
474 glVertex2f(twoX, twoY);
475 glVertex2f(oneX, twoY);
476
477 glVertex2f(oneX, twoY);
478 glVertex2f(twoX, twoY);
479 glColor3f(rr[2], gg[2], bb[2]);
480 glVertex2f(twoX, threeY);
481 glVertex2f(oneX, threeY);
482
483 glVertex2f(oneX, threeY);
484 glVertex2f(twoX, threeY);
485 glColor3f(rr[3], gg[3], bb[3]);
486 glVertex2f(twoX, fourY);
487 glVertex2f(oneX, fourY);
488
489 glVertex2f(oneX, fourY);
490 glVertex2f(twoX, fourY);
491 glColor3f(rr[4], gg[4], bb[4]);
492 glVertex2f(twoX, fiveY);
493 glVertex2f(oneX, fiveY);
494 float zeroX = oneX - shownSizex/50.f;
495 float zeroY = fiveY - shownSizey/50.f;
496 glColor3fv(tooHighValueCoulour);
497 glVertex2f(zeroX, fiveY);
498 glVertex2f(oneX, fiveY);
499 glVertex2f(oneX, zeroY);
500 glVertex2f(zeroX, zeroY);
501 glColor3fv(tooLowValueCoulour);
502 glVertex2f(zeroX, -fiveY);
503 glVertex2f(oneX, -fiveY);
504 glVertex2f(oneX, -zeroY);
505 glVertex2f(zeroX, -zeroY);
506 glEnd();
507 //draw linear/log tick marks
508 glColor3f(0.f,0.f,0.f);
509 glBegin(GL_LINES);
510 float value;
511 for (int i=1;i<10;i++)
512 {
513 if (logScale)
514 value = log10(i);
515 else
516 value = (float)(i)/10.f;
517 float yy = -shownSizey/2.f + value*shownSizey;
518 glVertex2f(oneX, yy);
519 glVertex2f(twoX, yy);
520 }
521 glEnd();
522 glPopAttrib();
523 glPopMatrix();
524 }
525 void BasicGlCamera::logScalePlease(bool checked)
526 {
527 if (!checked) return;
528 logScale = true;
529 pixelColorUpToDate = false;
530 emit colorPaletteHasChanged();
531 if (isVisible() && autoRefresh)
532 updateGL();
533 }
534 void BasicGlCamera::regularPalettePlease(bool checked)
535 {
536 if (!checked) return;
537 ss[0] = 0; ss[1] = 0.25f; ss[2] = 0.5f; ss[3] = 0.75f; ss[4] = 1.0f;
538 rr[0] = 0.15; rr[1] = 0; rr[2] = 0; rr[3] = 1.0f; rr[4] = 0.85f;
539 gg[0] = 0.15; gg[1] = 0; gg[2] = 1; gg[3] = 0; gg[4] = 0.85f;
540 bb[0] = 0.15; bb[1] = 1; bb[2] = 0; bb[3] = 0; bb[4] = 0.85f;
541 tooHighValueCoulour[0] = 1.f;
542 tooHighValueCoulour[1] = 1.f;
543 tooHighValueCoulour[2] = 0.f;
544 tooLowValueCoulour[0] = 0.f;
545 tooLowValueCoulour[1] = 1.f;
546 tooLowValueCoulour[2] = 1.f;
547 pixelColorUpToDate = false;
548
549 emit colorPaletteHasChanged();
550
551 if (isVisible() && autoRefresh)
552 updateGL();
553 }
554 void BasicGlCamera::prettyPalettePlease(bool checked)
555 {
556 if (!checked) return;
557 ss[0] = 0.f; ss[1] = 0.25f; ss[2] = 0.5f; ss[3] = 0.75f; ss[4] = 1.0f;
558 rr[0] = 0.f; rr[1] = 0.35f; rr[2] = 0.85f; rr[3] = 1.0f; rr[4] = 1.f;
559 gg[0] = 0.f; gg[1] = 0.10f; gg[2] = 0.20f; gg[3] = 0.73f; gg[4] = 1.f;
560 bb[0] = 0.f; bb[1] = 0.03f; bb[2] = 0.06f; bb[3] = 0.00f; bb[4] = 1.f;
561 tooHighValueCoulour[0] = 0.f;
562 tooHighValueCoulour[1] = 1.f;
563 tooHighValueCoulour[2] = 0.f;
564 tooLowValueCoulour[0] = 0.f;
565 tooLowValueCoulour[1] = 0.f;
566 tooLowValueCoulour[2] = 1.f;
567 pixelColorUpToDate = false;
568
569 emit colorPaletteHasChanged();
570
571 if (isVisible() && autoRefresh)
572 updateGL();
573 }
574 void BasicGlCamera::greyScalePalettePlease(bool checked)
575 {
576 if (!checked) return;
577 ss[0] = 0; ss[1] = 0.25f; ss[2] = 0.5f; ss[3] = 0.75f; ss[4] = 1.0f;
578 rr[0] = 0; rr[1] = 0.25f; rr[2] = 0.5f; rr[3] = 0.75f; rr[4] = 1.0f;
579 gg[0] = 0; gg[1] = 0.25f; gg[2] = 0.5f; gg[3] = 0.75f; gg[4] = 1.0f;
580 bb[0] = 0; bb[1] = 0.25f; bb[2] = 0.5f; bb[3] = 0.75f; bb[4] = 1.0f;
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::glowingPalettePlease(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.15; rr[1] = 0.5; rr[2] = 1.f; rr[3] = 0.0f; rr[4] = 1.f;
599 gg[0] = 0.15; gg[1] = 0.5; gg[2] = 1.f; gg[3] = 0.5f; gg[4] = 0.5f;
600 bb[0] = 0.15; bb[1] = 0.5; bb[2] = 1; bb[3] = 1.f; bb[4] = 0.f;
601 tooHighValueCoulour[0] = 1.f;
602 tooHighValueCoulour[1] = 0.f;
603 tooHighValueCoulour[2] = 0.f;
604 tooLowValueCoulour[0] = 0.f;
605 tooLowValueCoulour[1] = 1.f;
606 tooLowValueCoulour[2] = 0.f;
607 pixelColorUpToDate = false;
608
609 emit colorPaletteHasChanged();
610
611 if (isVisible() && autoRefresh)
612 updateGL();
613 }
614 void BasicGlCamera::SetAutoRefresh(bool on)
615 {
616 autoRefresh = on;
617 if (isVisible() && autoRefresh)
618 updateGL();
619 }
620 void BasicGlCamera::zeroRotationPlease(bool checked)
621 {
622 if (!checked) return;
623 cameraRotation = 0;
624 pixelColorUpToDate = false;
625 if (isVisible() && autoRefresh)
626 updateGL();
627 }
628 void BasicGlCamera::plus90RotationPlease(bool checked)
629 {
630 if (!checked) return;
631 cameraRotation = 90;
632 pixelColorUpToDate = false;
633 if (isVisible() && autoRefresh)
634 updateGL();
635 }
636 void BasicGlCamera::minus90RotationPlease(bool checked)
637 {
638 if (!checked) return;
639 cameraRotation = -90;
640 pixelColorUpToDate = false;
641 if (isVisible() && autoRefresh)
642 updateGL();
643 }
644
645 void BasicGlCamera::initializeGL()
646 {
647 qglClearColor(QColor(25,25,38));
648 glShadeModel(GL_FLAT);
649 glDisable(GL_DEPTH_TEST);
650 glDisable(GL_CULL_FACE);
651
652 // glEnable (GL_LINE_SMOOTH);
653 // glEnable (GL_BLEND);
654 // glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
655 // glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
656
657 }
658 void BasicGlCamera::resizeGL(int cWidth, int cHeight)
659 {
660 glViewport(0, 0, cWidth, cHeight);
661 glMatrixMode(GL_PROJECTION);
662 glLoadIdentity();
663 GLfloat windowRatio = (float)cWidth/(float)cHeight;
664 if (windowRatio < 1)
665 {
666 windowRatio = 1.0f/windowRatio;
667 gluOrtho2D(-viewSize, viewSize, -viewSize*windowRatio, viewSize*windowRatio);
668 pixelSize = 2*viewSize/(float)cWidth;
669 shownSizex = 2*viewSize;
670 shownSizey = 2*viewSize*windowRatio;
671 }
672 else
673 {
674 gluOrtho2D(-viewSize*windowRatio, viewSize*windowRatio, -viewSize, viewSize);
675 pixelSize = 2*viewSize/(float)cHeight;
676 shownSizex = 2*viewSize*windowRatio;
677 shownSizey = 2*viewSize;
678 }
679 glMatrixMode(GL_MODELVIEW);
680
681 fTextSize = (int)(cWidth*12/600); //want a sized 12 font for a window of 600 pixels width
682 setFont(QFont("Monospace", fTextSize));
683 }
684 void BasicGlCamera::paintGL()
685 {
686 glClear(GL_COLOR_BUFFER_BIT);
687 glLoadIdentity();
688
689 glTranslatef(0,-0.44,0);
690 glScalef(1.5, 1.5, 1.5);
691
692 drawCamera(true);
693
694 drawPatches();
695 }
696 void BasicGlCamera::toggleInterfaceDisplay()
697 {
698 if (linearButton->isVisible())
699 {
700 linearButton->hide();
701 logButton->hide();
702 regularPaletteButton->hide();
703 prettyPaletteButton->hide();
704 greyScalePaletteButton->hide();
705 glowingPaletteButton->hide();
706 zeroRotationButton->hide();
707 minus90RotationButton->hide();
708 plus90Rotationbutton->hide();
709 scaleLabel->hide();
710 colorPaletteLabel->hide();
711 rotationLabel->hide();
712 }
713 else
714 {
715 linearButton->show();
716 logButton->show();
717 regularPaletteButton->show();
718 prettyPaletteButton->show();
719 greyScalePaletteButton->show();
720 glowingPaletteButton->show();
721 zeroRotationButton->show();
722 minus90RotationButton->show();
723 plus90Rotationbutton->show();
724 scaleLabel->show();
725 colorPaletteLabel->show();
726 rotationLabel->show();
727 }
728 }
729
730 void BasicGlCamera::mousePressEvent(QMouseEvent *)
731 {
732
733 }
734 void BasicGlCamera::mouseMoveEvent(QMouseEvent *)
735 {
736
737 }
738 void BasicGlCamera::mouseDoubleClickEvent(QMouseEvent *)
739 {
740
741 }
742 void BasicGlCamera::timedUpdate()
743 {
744 if (isVisible())
745 updateGL();
746 }
747 void BasicGlCamera::updateCamera()
748 {
749 emit signalUpdateCamera();
750 }
751 void BasicGlCamera::drawCamera(bool alsoWire)
752 {
753// cout << "Super PaintGL" << endl;
754 glColor3f(0.5,0.5,0.5);
755 glLineWidth(1.0);
756 float color;
757
758 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
759 {
760 color = float(fData[i*fPixelStride+fcSlice]);// + ]eventData[nRoi*i + whichSlice]+(VALUES_SPAN/2))/(float)(VALUES_SPAN-1);
761 int index = 0;
762 while (ss[index] < color)
763 index++;
764 index--;
765 float weight0 = (color-ss[index]) / (ss[index+1]-ss[index]);
766 float weight1 = 1.0f-weight0;
767 pixelsColor[i][0] = weight1*rr[index] + weight0*rr[index+1];
768 pixelsColor[i][1] = weight1*gg[index] + weight0*gg[index+1];
769 pixelsColor[i][2] = weight1*bb[index] + weight0*bb[index+1];
770 }
771
772 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
773 {
774 // if (i == 690 ||
775 // i == 70)
776 // continue;
777 glColor3fv(pixelsColor[i]);
778 glLoadName(i);
779
780 drawHexagon(i,true);
781
782 }
783 if (!alsoWire)
784 return;
785 glColor3f(0.0f,0.0f,0.0f);
786 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
787 {
788// if (i == 690 ||
789// i == 70)
790// continue;
791 drawHexagon(i, false);
792 }
793 }
794 void BasicGlCamera::drawPatches()
795 {
796 glLineWidth(2.0f);
797 float backupRadius = hexRadius;
798 hexRadius *= 0.95;
799 glColor3f(0.5f, 0.5f, 0.3f);
800 glBegin(GL_LINES);
801 for (int i=0;i<NTMARK;i++)
802 {
803 for (unsigned int j=0;j<patchesIndices[i].size();j++)
804 {
805 glVertex2fv(verticesList[patchesIndices[i][j].first]);
806 glVertex2fv(verticesList[patchesIndices[i][j].second]);
807 }
808 }
809 glEnd();
810 hexRadius = backupRadius;
811 }
812 int BasicGlCamera::PixelAtPosition(const QPoint &cPos)
813 {
814 const int MaxSize = 512;
815 GLuint buffer[MaxSize];
816 GLint viewport[4];
817
818 makeCurrent();
819
820 glGetIntegerv(GL_VIEWPORT, viewport);
821 glSelectBuffer(MaxSize, buffer);
822 glRenderMode(GL_SELECT);
823
824 glInitNames();
825 glPushName(0);
826
827 glMatrixMode(GL_PROJECTION);
828 glPushMatrix();
829 glLoadIdentity();
830 GLfloat windowRatio = GLfloat(width()) / GLfloat(height());
831 gluPickMatrix(GLdouble(cPos.x()), GLdouble(viewport[3] - cPos.y()),
832 1.0, 1.0, viewport);
833
834 if (windowRatio < 1)
835 {
836 windowRatio = 1.0f/windowRatio;
837 gluOrtho2D(-viewSize, viewSize, -viewSize*windowRatio, viewSize*windowRatio);
838 }
839 else
840 {
841 gluOrtho2D(-viewSize*windowRatio, viewSize*windowRatio, -viewSize, viewSize);
842 }
843
844 glMatrixMode(GL_MODELVIEW);
845 drawCamera(false);
846 glMatrixMode(GL_PROJECTION);
847 glPopMatrix();
848
849 //for some reason that I cannot understand, the push/pop matrix doesn't do the trick here... bizarre
850 //ok, so re-do the resizeGL thing.
851 resizeGL(width(), height());
852
853 if (!glRenderMode(GL_RENDER))
854 return -1;
855
856 return buffer[3];
857 }
858 void BasicGlCamera::drawHexagon(int index, bool solid)
859 {
860/* float minX, maxX, minY, maxY;
861 minX = minY = 1e10;
862 maxX = maxY = -1e10;
863 for (int i=0;i<1438;i++)
864 {
865 for (int j=0;j<6;j++)
866 {
867 if (verticesList[verticesIndices[i][j]][0] > maxX)
868 maxX = verticesList[verticesIndices[i][j]][0];
869 if (verticesList[verticesIndices[i][j]][0] < minX)
870 minX = verticesList[verticesIndices[i][j]][0];
871 if (verticesList[verticesIndices[i][j]][1] > maxY)
872 maxY = verticesList[verticesIndices[i][j]][1];
873 if (verticesList[verticesIndices[i][j]][1] < minY)
874 minY = verticesList[verticesIndices[i][j]][1];
875 }
876 }
877 cout << "Min, Max X: " << minX << " " << maxX << endl;
878 cout << "Min, Max Y: " << minY << " " << maxY << endl;
879 exit(0);*/
880 if (solid)
881 glBegin(GL_POLYGON);
882 else
883 glBegin(GL_LINE_LOOP);
884
885 glVertex2fv(verticesList[verticesIndices[index][0]]);
886 glVertex2fv(verticesList[verticesIndices[index][1]]);
887 glVertex2fv(verticesList[verticesIndices[index][2]]);
888 glVertex2fv(verticesList[verticesIndices[index][3]]);
889 glVertex2fv(verticesList[verticesIndices[index][4]]);
890 glVertex2fv(verticesList[verticesIndices[index][5]]);
891 if (solid)
892 glVertex2fv(verticesList[verticesIndices[index][0]]);
893
894 glEnd();
895 }
896
897 void BasicGlCamera::updateNeighbors(int currentPixel)
898 {
899 float squaredDistance = 0;
900 for (int i=0;i<currentPixel;i++)
901 {
902 squaredDistance = (pixelsCoords[i][0] - pixelsCoords[currentPixel][0])*
903 (pixelsCoords[i][0] - pixelsCoords[currentPixel][0]) +
904 (pixelsCoords[i][1] - pixelsCoords[currentPixel][1])*
905 (pixelsCoords[i][1] - pixelsCoords[currentPixel][1]);
906 if (squaredDistance < 4*hexRadius*hexRadius*(1.0f+hexTolerance))//neighbor !
907 {//ok, but which one ?
908 if (fabs(pixelsCoords[i][0] - pixelsCoords[currentPixel][0]) < hexTolerance &&
909 pixelsCoords[i][1] < pixelsCoords[currentPixel][1]){//top
910 neighbors[i][0] = currentPixel;
911 neighbors[currentPixel][3] = i;
912 continue;}
913 if (fabs(pixelsCoords[i][0] - pixelsCoords[currentPixel][0]) < hexTolerance &&
914 pixelsCoords[i][1] > pixelsCoords[currentPixel][1]){//bottom
915 neighbors[i][3] = currentPixel;
916 neighbors[currentPixel][0] = i;
917 continue;}
918 if (pixelsCoords[i][0] > pixelsCoords[currentPixel][0] &&
919 pixelsCoords[i][1] > pixelsCoords[currentPixel][1]){//top right
920 neighbors[i][4] = currentPixel;
921 neighbors[currentPixel][1] = i;
922 continue;}
923 if (pixelsCoords[i][0] > pixelsCoords[currentPixel][0] &&
924 pixelsCoords[i][1] < pixelsCoords[currentPixel][1]){//bottom right
925 neighbors[i][5] = currentPixel;
926 neighbors[currentPixel][2] = i;
927 continue;}
928 if (pixelsCoords[i][0] < pixelsCoords[currentPixel][0] &&
929 pixelsCoords[i][1] > pixelsCoords[currentPixel][1]){//top left
930 neighbors[i][2] = currentPixel;
931 neighbors[currentPixel][5] = i;
932 continue;}
933 if (pixelsCoords[i][0] < pixelsCoords[currentPixel][0] &&
934 pixelsCoords[i][1] < pixelsCoords[currentPixel][1]){//bottom left
935 neighbors[i][1] = currentPixel;
936 neighbors[currentPixel][4] = i;
937 continue;}
938 }
939 }
940 }
941 void BasicGlCamera::skipPixels(int start, int howMany)
942 {
943 for (int i=start;i<MAX_NUM_PIXELS-howMany;i++)
944 {
945 pixelsCoords[i][0] = pixelsCoords[i+howMany][0];
946 pixelsCoords[i][1] = pixelsCoords[i+howMany][1];
947 }
948 }
949 void BasicGlCamera::calculatePixelsCoords()
950 {
951 if (pixelsCoords[0][1] >= 0.299999 && pixelsCoords[0][1] <= 0.300001)
952 return;
953 pixelsCoords[0][0] = 0;
954 pixelsCoords[0][1] = 0.3;
955 pixelsCoords[0][2] = 0;
956 pixelsCoords[1][0] = 0;
957 pixelsCoords[1][1] = 0.3+2*hexRadius;
958 pixelsCoords[1][2] = 0;
959 neighbors[0][0] = 1;
960 neighbors[1][3] = 0;
961 //from which side of the previous hexagon are we coming from ?
962 int fromSide = 3;
963 //to which side are we heading to ?
964 int toSide = 0;
965 for (int i=2;i<MAX_NUM_PIXELS;i++)
966 {
967 toSide = fromSide-1;
968 if (toSide < 0)
969 toSide =5;
970 while (neighbors[i-1][toSide] >= 0)
971 {
972 toSide--;
973 if (toSide < 0)
974 toSide = 5;
975 }
976 fromSide = toSide + 3;
977 if (fromSide > 5)
978 fromSide -= 6;
979 //ok. now we now in which direction we're heading
980 pixelsCoords[i][0] = pixelsCoords[i-1][0];
981 pixelsCoords[i][1] = pixelsCoords[i-1][1];
982 pixelsCoords[i][2] = pixelsCoords[i-1][2];
983 switch (toSide)
984 {
985 case 0:
986 pixelsCoords[i][1] += 2*hexRadius;
987 break;
988 case 1:
989 pixelsCoords[i][0] += (2*hexRadius)*sin(M_PI/3.0);
990 pixelsCoords[i][1] += (2*hexRadius)*cos(M_PI/3.0);
991 break;
992 case 2:
993 pixelsCoords[i][0] += (2*hexRadius)*sin(M_PI/3.0);
994 pixelsCoords[i][1] -= (2*hexRadius)*cos(M_PI/3.0);
995 break;
996 case 3:
997 pixelsCoords[i][1] -= 2*hexRadius;
998 break;
999 case 4:
1000 pixelsCoords[i][0] -= (2*hexRadius)*sin(M_PI/3.0);
1001 pixelsCoords[i][1] -= (2*hexRadius)*cos(M_PI/3.0);
1002 break;
1003 case 5:
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 };
1008
1009 updateNeighbors(i);
1010 }
1011 //Ok. So now we've circled around all the way to MAX_NUM_PIXELS
1012 //do the required shifts so that it matches the fact camera up to ACTUAL_NUM_PIXELS pixels
1013 //remember the location pixels 1438 and 1439, and re-assign them later on
1014 GLfloat backupCoords[4];
1015 skipPixels(1200, 1);
1016 skipPixels(1218, 3);
1017 skipPixels(1236, 1);
1018 skipPixels(1256, 1);
1019 skipPixels(1274, 3);
1020 skipPixels(1292, 3);
1021 skipPixels(1309, 6);
1022 skipPixels(1323, 7);
1023 skipPixels(1337, 6);
1024 skipPixels(1354, 6);
1025 skipPixels(1368, 7);
1026 //la c'est dans 1390 qu'il y a 1439
1027 backupCoords[0] = pixelsCoords[1390][0];
1028 backupCoords[1] = pixelsCoords[1390][1];
1029 skipPixels(1382, 9);
1030 skipPixels(1394, 12);
1031 skipPixels(1402, 15);
1032 skipPixels(1410, 12);
1033 //la c'est dans 1422 qu'il y a 1438
1034 backupCoords[2] = pixelsCoords[1422][0];
1035 backupCoords[3] = pixelsCoords[1422][1];
1036 skipPixels(1422, 12);
1037 skipPixels(1430, 15);
1038
1039 pixelsCoords[1438][0] = backupCoords[2];
1040 pixelsCoords[1438][1] = backupCoords[3];
1041 pixelsCoords[1439][0] = backupCoords[0];
1042 pixelsCoords[1439][1] = backupCoords[1];
1043 }
1044 void BasicGlCamera::buildVerticesList()
1045 {
1046 numVertices = 0;
1047 GLfloat cVertex[2];
1048 for (int i=0;i<NPIX;i++)
1049 {
1050 for (int j=0;j<6;j++)
1051 {
1052 for (int k=0;k<2;k++)
1053 cVertex[k] = hexcoords[j][k]*hexRadius + pixelsCoords[i][k];
1054 bool found = false;
1055 for (int k=0;k<numVertices;k++)
1056 {
1057 if ((cVertex[0] - verticesList[k][0])*
1058 (cVertex[0] - verticesList[k][0]) +
1059 (cVertex[1] - verticesList[k][1])*
1060 (cVertex[1] - verticesList[k][1]) < hexTolerance*hexTolerance)
1061 {
1062 found = true;
1063 break;
1064 }
1065 }
1066 if (!found)
1067 {
1068 for (int k=0;k<2;k++)
1069 verticesList[numVertices][k] = cVertex[k];
1070 numVertices++;
1071 }
1072 }
1073 }
1074//cout << "numVertices: " << numVertices << endl;
1075 for (int i=0;i<NPIX;i++)
1076 {
1077 for (int j=0;j<6;j++)
1078 {
1079 for (int k=0;k<2;k++)
1080 cVertex[k] = hexcoords[j][k]*hexRadius + pixelsCoords[i][k];
1081 for (int k=0;k<numVertices;k++)
1082 {
1083 if ((cVertex[0] - verticesList[k][0])*
1084 (cVertex[0] - verticesList[k][0]) +
1085 (cVertex[1] - verticesList[k][1])*
1086 (cVertex[1] - verticesList[k][1]) < hexTolerance*hexTolerance)
1087 {
1088 verticesIndices[i][j] = k;
1089 break;
1090 }
1091 }
1092 }
1093 }
1094 }
1095 void BasicGlCamera::buildPatchesIndices()
1096 {
1097 vector<edge>::iterator it;
1098 bool erased = false;
1099// patchesIndices.resize(NTMARK);
1100 for (int i=0;i<NTMARK;i++)//for all patches
1101 {
1102 patchesIndices[i].clear();
1103 for (int j=0;j<9;j++)//for all cells of the current patch
1104 {
1105 if (softwareMapping[patches[i][j]] >= ACTUAL_NUM_PIXELS)
1106 continue;
1107 for (int k=0;k<6;k++)//for all sides of the current cell
1108 {
1109 int first = k-1;
1110 int second = k;
1111 if (first < 0)
1112 first = 5;
1113 erased = false;
1114 for (it=(patchesIndices[i]).begin(); it != (patchesIndices[i]).end(); it++)//check if this side is here already or not
1115 {
1116 if (((*it).first == verticesIndices[softwareMapping[patches[i][j]]][first] &&
1117 (*it).second == verticesIndices[softwareMapping[patches[i][j]]][second]) ||
1118 ((*it).first == verticesIndices[softwareMapping[patches[i][j]]][second] &&
1119 (*it).second == verticesIndices[softwareMapping[patches[i][j]]][first]))
1120 {
1121 patchesIndices[i].erase(it);
1122 erased = true;
1123 break;
1124 }
1125 }
1126 if (!erased)
1127 {
1128 edge temp;
1129 temp.first = verticesIndices[softwareMapping[patches[i][j]]][first];
1130 temp.second = verticesIndices[softwareMapping[patches[i][j]]][second];
1131 patchesIndices[i].push_back(temp);
1132 }
1133 }
1134 }
1135 }
1136// for (int i=0;i<NTMARK;i++)
1137// {
1138// cout << ".....................patch " << i << " size: " << patchesIndices[i].size() << endl;
1139// for (unsigned int j=0;j<patchesIndices[i].size();j++)
1140// {
1141// if (patchesIndices[i][j].first < 0 || patchesIndices[i][j].first > 3013)
1142// cout << patchesIndices[i][j].first << " and " << patchesIndices[i][j].second << " and " << j << endl;
1143// }
1144// }
1145 }
1146
Note: See TracBrowser for help on using the repository browser.