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

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