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

Last change on this file since 11788 was 11779, checked in by lyard, 13 years ago
Replaced root camera by openGL camera in the gui
File size: 17.3 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//Coordinates of an hexagon of radius 1 and center 0
10GLfloat hexcoords[6][2] = {{-0.577367206, 1},
11 { 0.577367206, 1},
12 { 1.154734411, 0},
13 { 0.577367206, -1},
14 {-0.577367206, -1},
15 {-1.154734411, 0}};
16
17
18
19 BasicGlCamera::BasicGlCamera(QWidget* cParent) : QGLWidget(cParent)
20 {
21 setFormat(QGLFormat(QGL::DoubleBuffer));// | QGL::DepthBuffer));
22 hexRadius = 0.015f;
23 hexTolerance = hexRadius/100.0f;
24 viewSize = 1.0f;
25 calculatePixelsCoords();
26
27 ifstream fin2("MasterList-v3.txt");
28 if (!fin2.is_open())
29 {
30 cout << "Error: file \"MasterList-v3\" missing. aborting." << endl;
31 exit(-1);
32 }
33 int l = 0;
34 string buf;
35 while (getline(fin2, buf, '\n'))
36 {
37 buf = Tools::Trim(buf);
38 if (buf[0]=='#')
39 continue;
40
41 unsigned int softid, hardid, dummy;
42
43 stringstream str(buf);
44
45 str >> softid;
46 str >> dummy;
47 str >> hardid;
48
49 if (softid>=1440)
50 continue;
51
52 hardwareMapping[softid] = hardid;
53 softwareMapping[hardid] = softid;
54
55 l++;
56 }
57// GLfloat tempPixelsCoords[MAX_NUM_PIXELS][3];
58// for (int i=0;i<1440;i++)
59// for (int j=0;j<3;j++)
60// tempPixelsCoords[hardwareMapping[i]][j] = pixelsCoords[i][j];
61// for (int i=0;i<1440;i++)
62// for (int j=0;j<3;j++)
63// pixelsCoords[i][j] = tempPixelsCoords[i][j];
64 buildVerticesList();
65 ifstream fin1("Trigger-Patches.txt");
66 if (!fin1.is_open())
67 {
68 cout << "Error: file \"Trigger-Patches.txt\" missing. Aborting." << endl;
69 exit(-1);
70 }
71 l=0;
72 while (getline(fin1, buf, '\n'))
73 {
74 buf = Tools::Trim(buf);
75 if (buf[0]=='#')
76 continue;
77
78 stringstream str(buf);
79 for (int i=0; i<9; i++)
80 {
81 unsigned int n;
82 str >> n;
83
84 if (n>=1440)
85 continue;
86
87 patches[l][i] = hardwareMapping[n];
88 }
89 l++;
90 }
91 buildPatchesIndices();
92
93 for (int i=0;i<1440;i++)
94 updateNeighbors(i);
95
96 ss[0] = 0; ss[1] = 0.25f; ss[2] = 0.5f; ss[3] = 0.75f; ss[4] = 1.0f;
97 rr[0] = 0.15; rr[1] = rr[2] = 0; rr[3] = 1.0f; rr[4] = 0.85f;
98 gg[0] = 0.15; gg[1] = 0; gg[2] = 1; gg[3] = 0; gg[4] = 0.85f;
99 bb[0] = 0.15; bb[1] = 1; bb[2] = bb[3] = 0; bb[4] = 0.85f;
100
101 fPixelStride = 1;
102 fcSlice = 0;
103 fData.resize(1440);
104 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
105 fData[i] = (double)i/1.44;//(double)(i)/(double)(ACTUAL_NUM_PIXELS);
106 }
107 BasicGlCamera::~BasicGlCamera()
108 {
109 }
110
111 void BasicGlCamera::initializeGL()
112 {
113 qglClearColor(QColor(25,25,38));
114 glShadeModel(GL_FLAT);
115 glDisable(GL_DEPTH_TEST);
116 glDisable(GL_CULL_FACE);
117 }
118 void BasicGlCamera::resizeGL(int cWidth, int cHeight)
119 {
120 glViewport(0, 0, cWidth, cHeight);
121 glMatrixMode(GL_PROJECTION);
122 glLoadIdentity();
123 GLfloat windowRatio = (float)cWidth/(float)cHeight;
124 if (windowRatio < 1)
125 {
126 windowRatio = 1.0f/windowRatio;
127 gluOrtho2D(-viewSize, viewSize, -viewSize*windowRatio, viewSize*windowRatio);
128 pixelSize = 2*viewSize/(float)cWidth;
129 shownSizex = 2*viewSize;
130 shownSizey = 2*viewSize*windowRatio;
131 }
132 else
133 {
134 gluOrtho2D(-viewSize*windowRatio, viewSize*windowRatio, -viewSize, viewSize);
135 pixelSize = 2*viewSize/(float)cHeight;
136 shownSizex = 2*viewSize*windowRatio;
137 shownSizey = 2*viewSize;
138 }
139 glMatrixMode(GL_MODELVIEW);
140 }
141 void BasicGlCamera::paintGL()
142 {
143 glClear(GL_COLOR_BUFFER_BIT);
144 glLoadIdentity();
145
146 glTranslatef(0,-0.44,0);
147 glScalef(1.5, 1.5, 1.5);
148
149 drawCamera(true);
150
151 drawPatches();
152 }
153 void BasicGlCamera::mousePressEvent(QMouseEvent *cEvent)
154 {
155
156 }
157 void BasicGlCamera::mouseMoveEvent(QMouseEvent *cEvent)
158 {
159
160 }
161 void BasicGlCamera::mouseDoubleClickEvent(QMouseEvent *cEvent)
162 {
163
164 }
165 void BasicGlCamera::drawCamera(bool alsoWire)
166 {
167 cout << "Super PaintGL" << endl;
168 glColor3f(0.5,0.5,0.5);
169 glLineWidth(1.0);
170 float color;
171
172 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
173 {
174
175 color = float(fData[i*fPixelStride+fcSlice]);// + ]eventData[nRoi*i + whichSlice]+(VALUES_SPAN/2))/(float)(VALUES_SPAN-1);
176 int index = 0;
177 while (ss[index] < color)
178 index++;
179 index--;
180 float weight0 = (color-ss[index]) / (ss[index+1]-ss[index]);
181 float weight1 = 1.0f-weight0;
182 pixelsColor[i][0] = weight1*rr[index] + weight0*rr[index+1];
183 pixelsColor[i][1] = weight1*gg[index] + weight0*gg[index+1];
184 pixelsColor[i][2] = weight1*bb[index] + weight0*bb[index+1];
185 }
186
187 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
188 {
189 // if (i == 690 ||
190 // i == 70)
191 // continue;
192 glColor3fv(pixelsColor[i]);
193 glLoadName(i);
194
195 drawHexagon(i,true);
196
197 }
198 if (!alsoWire)
199 return;
200 glColor3f(0.0f,0.0f,0.0f);
201 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
202 {
203// if (i == 690 ||
204// i == 70)
205// continue;
206 drawHexagon(i, false);
207 }
208 }
209 void BasicGlCamera::drawPatches()
210 {
211 glLineWidth(2.0f);
212 float backupRadius = hexRadius;
213 hexRadius *= 0.95;
214 glColor3f(0.5f, 0.5f, 0.3f);
215 glBegin(GL_LINES);
216 for (int i=0;i<NTMARK;i++)
217 {
218 for (unsigned int j=0;j<patchesIndices[i].size();j++)
219 {
220 glVertex2fv(verticesList[patchesIndices[i][j].first]);
221 glVertex2fv(verticesList[patchesIndices[i][j].second]);
222 }
223 }
224 glEnd();
225 hexRadius = backupRadius;
226 }
227 int BasicGlCamera::PixelAtPosition(const QPoint &cPos)
228 {
229 const int MaxSize = 512;
230 GLuint buffer[MaxSize];
231 GLint viewport[4];
232
233 makeCurrent();
234
235 glGetIntegerv(GL_VIEWPORT, viewport);
236 glSelectBuffer(MaxSize, buffer);
237 glRenderMode(GL_SELECT);
238
239 glInitNames();
240 glPushName(0);
241
242 glMatrixMode(GL_PROJECTION);
243 glPushMatrix();
244 glLoadIdentity();
245 GLfloat windowRatio = GLfloat(width()) / GLfloat(height());
246 gluPickMatrix(GLdouble(cPos.x()), GLdouble(viewport[3] - cPos.y()),
247 1.0, 1.0, viewport);
248
249 if (windowRatio < 1)
250 {
251 windowRatio = 1.0f/windowRatio;
252 gluOrtho2D(-viewSize, viewSize, -viewSize*windowRatio, viewSize*windowRatio);
253 }
254 else
255 {
256 gluOrtho2D(-viewSize*windowRatio, viewSize*windowRatio, -viewSize, viewSize);
257 }
258
259 glMatrixMode(GL_MODELVIEW);
260 drawCamera(false);
261 glMatrixMode(GL_PROJECTION);
262 glPopMatrix();
263
264 //for some reason that I cannot understand, the push/pop matrix doesn't do the trick here... bizarre
265 //ok, so re-do the resizeGL thing.
266 resizeGL(width(), height());
267
268 if (!glRenderMode(GL_RENDER))
269 return -1;
270
271 return buffer[3];
272 }
273 void BasicGlCamera::drawHexagon(int index, bool solid)
274 {
275 if (solid)
276 glBegin(GL_POLYGON);
277 else
278 glBegin(GL_LINE_LOOP);
279
280 glVertex2fv(verticesList[verticesIndices[index][0]]);
281 glVertex2fv(verticesList[verticesIndices[index][1]]);
282 glVertex2fv(verticesList[verticesIndices[index][2]]);
283 glVertex2fv(verticesList[verticesIndices[index][3]]);
284 glVertex2fv(verticesList[verticesIndices[index][4]]);
285 glVertex2fv(verticesList[verticesIndices[index][5]]);
286 if (solid)
287 glVertex2fv(verticesList[verticesIndices[index][0]]);
288
289 glEnd();
290 }
291
292 void BasicGlCamera::updateNeighbors(int currentPixel)
293 {
294 float squaredDistance = 0;
295 for (int i=0;i<currentPixel;i++)
296 {
297 squaredDistance = (pixelsCoords[i][0] - pixelsCoords[currentPixel][0])*
298 (pixelsCoords[i][0] - pixelsCoords[currentPixel][0]) +
299 (pixelsCoords[i][1] - pixelsCoords[currentPixel][1])*
300 (pixelsCoords[i][1] - pixelsCoords[currentPixel][1]);
301 if (squaredDistance < 4*hexRadius*hexRadius*(1.0f+hexTolerance))//neighbor !
302 {//ok, but which one ?
303 if (fabs(pixelsCoords[i][0] - pixelsCoords[currentPixel][0]) < hexTolerance &&
304 pixelsCoords[i][1] < pixelsCoords[currentPixel][1]){//top
305 neighbors[i][0] = currentPixel;
306 neighbors[currentPixel][3] = i;
307 continue;}
308 if (fabs(pixelsCoords[i][0] - pixelsCoords[currentPixel][0]) < hexTolerance &&
309 pixelsCoords[i][1] > pixelsCoords[currentPixel][1]){//bottom
310 neighbors[i][3] = currentPixel;
311 neighbors[currentPixel][0] = i;
312 continue;}
313 if (pixelsCoords[i][0] > pixelsCoords[currentPixel][0] &&
314 pixelsCoords[i][1] > pixelsCoords[currentPixel][1]){//top right
315 neighbors[i][4] = currentPixel;
316 neighbors[currentPixel][1] = i;
317 continue;}
318 if (pixelsCoords[i][0] > pixelsCoords[currentPixel][0] &&
319 pixelsCoords[i][1] < pixelsCoords[currentPixel][1]){//bottom right
320 neighbors[i][5] = currentPixel;
321 neighbors[currentPixel][2] = i;
322 continue;}
323 if (pixelsCoords[i][0] < pixelsCoords[currentPixel][0] &&
324 pixelsCoords[i][1] > pixelsCoords[currentPixel][1]){//top left
325 neighbors[i][2] = currentPixel;
326 neighbors[currentPixel][5] = i;
327 continue;}
328 if (pixelsCoords[i][0] < pixelsCoords[currentPixel][0] &&
329 pixelsCoords[i][1] < pixelsCoords[currentPixel][1]){//bottom left
330 neighbors[i][1] = currentPixel;
331 neighbors[currentPixel][4] = i;
332 continue;}
333 }
334 }
335 }
336 void BasicGlCamera::skipPixels(int start, int howMany)
337 {
338 for (int i=start;i<MAX_NUM_PIXELS-howMany;i++)
339 {
340 pixelsCoords[i][0] = pixelsCoords[i+howMany][0];
341 pixelsCoords[i][1] = pixelsCoords[i+howMany][1];
342 }
343 }
344 void BasicGlCamera::calculatePixelsCoords()
345 {
346 pixelsCoords[0][0] = 0;
347 pixelsCoords[0][1] = 0.3;
348 pixelsCoords[0][2] = 0;
349 pixelsCoords[1][0] = 0;
350 pixelsCoords[1][1] = 0.3+2*hexRadius;
351 pixelsCoords[1][2] = 0;
352 neighbors[0][0] = 1;
353 neighbors[1][3] = 0;
354 //from which side of the previous hexagon are we coming from ?
355 int fromSide = 3;
356 //to which side are we heading to ?
357 int toSide = 0;
358 for (int i=2;i<MAX_NUM_PIXELS;i++)
359 {
360 toSide = fromSide-1;
361 if (toSide < 0)
362 toSide =5;
363 while (neighbors[i-1][toSide] >= 0)
364 {
365 toSide--;
366 if (toSide < 0)
367 toSide = 5;
368 }
369 fromSide = toSide + 3;
370 if (fromSide > 5)
371 fromSide -= 6;
372 //ok. now we now in which direction we're heading
373 pixelsCoords[i][0] = pixelsCoords[i-1][0];
374 pixelsCoords[i][1] = pixelsCoords[i-1][1];
375 pixelsCoords[i][2] = pixelsCoords[i-1][2];
376 switch (toSide)
377 {
378 case 0:
379 pixelsCoords[i][1] += 2*hexRadius;
380 break;
381 case 1:
382 pixelsCoords[i][0] += (2*hexRadius)*sin(M_PI/3.0);
383 pixelsCoords[i][1] += (2*hexRadius)*cos(M_PI/3.0);
384 break;
385 case 2:
386 pixelsCoords[i][0] += (2*hexRadius)*sin(M_PI/3.0);
387 pixelsCoords[i][1] -= (2*hexRadius)*cos(M_PI/3.0);
388 break;
389 case 3:
390 pixelsCoords[i][1] -= 2*hexRadius;
391 break;
392 case 4:
393 pixelsCoords[i][0] -= (2*hexRadius)*sin(M_PI/3.0);
394 pixelsCoords[i][1] -= (2*hexRadius)*cos(M_PI/3.0);
395 break;
396 case 5:
397 pixelsCoords[i][0] -= (2*hexRadius)*sin(M_PI/3.0);
398 pixelsCoords[i][1] += (2*hexRadius)*cos(M_PI/3.0);
399 break;
400 };
401
402 updateNeighbors(i);
403 }
404 //Ok. So now we've circled around all the way to MAX_NUM_PIXELS
405 //do the required shifts so that it matches the fact camera up to ACTUAL_NUM_PIXELS pixels
406 skipPixels(1200, 1);
407 skipPixels(1218, 3);
408 skipPixels(1236, 1);
409 skipPixels(1256, 1);
410 skipPixels(1274, 3);
411 skipPixels(1292, 3);
412 skipPixels(1309, 6);
413 skipPixels(1323, 7);
414 skipPixels(1337, 6);
415 skipPixels(1354, 6);
416 skipPixels(1368, 7);
417 skipPixels(1382, 9);
418 skipPixels(1394, 12);
419 skipPixels(1402, 15);
420 skipPixels(1410, 12);
421 skipPixels(1422, 12);
422 skipPixels(1430, 15);
423 }
424 void BasicGlCamera::buildVerticesList()
425 {
426 numVertices = 0;
427 GLfloat cVertex[2];
428 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
429 {
430 for (int j=0;j<6;j++)
431 {
432 for (int k=0;k<2;k++)
433 cVertex[k] = hexcoords[j][k]*hexRadius + pixelsCoords[i][k];
434 bool found = false;
435 for (int k=0;k<numVertices;k++)
436 {
437 if ((cVertex[0] - verticesList[k][0])*
438 (cVertex[0] - verticesList[k][0]) +
439 (cVertex[1] - verticesList[k][1])*
440 (cVertex[1] - verticesList[k][1]) < hexTolerance*hexTolerance)
441 {
442 found = true;
443 break;
444 }
445 }
446 if (!found)
447 {
448 for (int k=0;k<2;k++)
449 verticesList[numVertices][k] = cVertex[k];
450 numVertices++;
451 }
452 }
453 }
454 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
455 {
456 for (int j=0;j<6;j++)
457 {
458 for (int k=0;k<2;k++)
459 cVertex[k] = hexcoords[j][k]*hexRadius + pixelsCoords[i][k];
460 for (int k=0;k<numVertices;k++)
461 {
462 if ((cVertex[0] - verticesList[k][0])*
463 (cVertex[0] - verticesList[k][0]) +
464 (cVertex[1] - verticesList[k][1])*
465 (cVertex[1] - verticesList[k][1]) < hexTolerance*hexTolerance)
466 {
467 verticesIndices[i][j] = k;
468 break;
469 }
470 }
471 }
472 }
473 }
474 void BasicGlCamera::buildPatchesIndices()
475 {
476 vector<edge>::iterator it;
477 bool erased = false;
478 for (int i=0;i<NTMARK;i++)//for all patches
479 {
480 patchesIndices[i].clear();
481 for (int j=0;j<9;j++)//for all cells of the current patch
482 {
483 for (int k=0;k<6;k++)//for all sides of the current cell
484 {
485 int first = k-1;
486 int second = k;
487 if (first < 0)
488 first = 5;
489 erased = false;
490 for (it=(patchesIndices[i]).begin(); it != (patchesIndices[i]).end(); it++)//check if this side is here already or not
491 {
492 if (((*it).first == verticesIndices[softwareMapping[patches[i][j]]][first] &&
493 (*it).second == verticesIndices[softwareMapping[patches[i][j]]][second]) ||
494 ((*it).first == verticesIndices[softwareMapping[patches[i][j]]][second] &&
495 (*it).second == verticesIndices[softwareMapping[patches[i][j]]][first]))
496 {
497 patchesIndices[i].erase(it);
498 erased = true;
499 break;
500 }
501 }
502 if (!erased)
503 {
504 edge temp;
505 temp.first = verticesIndices[softwareMapping[patches[i][j]]][first];
506 temp.second = verticesIndices[softwareMapping[patches[i][j]]][second];
507 patchesIndices[i].push_back(temp);
508 }
509 }
510 }
511 }
512 }
513
Note: See TracBrowser for help on using the repository browser.