source: trunk/FACT++/gui/QCameraWidget.cc@ 12219

Last change on this file since 12219 was 12209, checked in by tbretz, 13 years ago
Unifiy instantiation and assignemnt of initial numbers.
File size: 16.0 KB
Line 
1#include "QCameraWidget.h"
2#include <sstream>
3#include <iostream>
4
5using namespace std;
6
7 QCameraWidget::QCameraWidget(QWidget *pparent) : BasicGlCamera(pparent)
8 {
9 fBold.resize(1440, false);
10 fEnable.resize(1440, true);
11 lastFace = -1;
12 fShowPixelMoveOver = false;
13 fShowPatchMoveOver = false;
14
15 CalculatePixelsColor();
16
17 }
18
19 void QCameraWidget::paintGL()
20 {
21 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
22 glLoadIdentity();
23
24 glTranslatef(0,-0.44,0);
25 glTranslatef(-0.1,0,0);
26 glRotatef(cameraRotation, 0,0,-1);
27 if (cameraRotation == 90)
28 {
29 glTranslatef(-0.45,-0.45,0);
30 // cout << "correction" << endl;
31 }
32 if (cameraRotation == -90)
33 {
34 glTranslatef(0.45,-0.45,0);
35 }
36 glScalef(1.5, 1.5, 1.0);
37 glTranslatef(0,0,-0.5);
38 drawCamera(true);
39 glTranslatef(0,0,0.1f);
40
41 drawPatches();
42 glTranslatef(0,0,0.1f);
43
44 glLineWidth(1.0f);
45 glColor3fv(highlightedPixelsCoulour);
46 for (vector<int>::iterator it = highlightedPixels.begin(); it!= highlightedPixels.end(); it++)
47 {
48 drawHexagon(*it, false);
49 }
50
51 glLineWidth(1.0f);
52 glTranslatef(0,0,0.1f);
53
54 //glColor3f(1.f - pixelsColor[fWhite][0],1.f - pixelsColor[fWhite][1],1.f - pixelsColor[fWhite][2]);
55 if (fWhite != -1)
56 {
57 glColor3f(1.f, 0.f, 0.f);
58 drawHexagon(fWhite, false);
59 }
60 DrawCameraText();
61
62 DrawScale();
63
64// if (linearButton->isVisible())
65// repaintInterface();
66 }
67 void QCameraWidget::drawCamera(bool alsoWire)
68 {
69
70 if (!pixelColorUpToDate)
71 CalculatePixelsColor();
72 glLineWidth(1.0);
73 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
74 {
75 glColor3fv(pixelsColor[i]);
76 glLoadName(i);
77 drawHexagon(i,true);
78 }
79 if (!alsoWire)
80 return;
81 glTranslatef(0,0,0.1f);
82 glColor3fv(pixelContourColour);//0.0f,0.0f,0.0f);
83 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
84 {
85 drawHexagon(i, false);
86 }
87 }
88 void QCameraWidget::DrawCameraText()
89 {
90 glPushMatrix();
91 glLoadIdentity();
92
93
94
95// int textSize = (int)(width()*14/600);
96// setFont(QFont("Monospace", textSize));
97 qglColor(QColor(255, 223, 127));
98
99 //first let's draw the usual data
100 //title
101 renderText(-shownSizex/2.f + 0.01f, shownSizey/2.f - fTextSize*pixelSize - 0.01f, 0.f, QString(titleText.c_str()));
102 //stats
103 ostringstream str;
104 str.precision(2);
105 str.setf(ios::fixed,ios::floatfield);
106 str << "Med " << fmedian;// << unitsText;
107 renderText(3, height()-3-4*fTextSize-35, QString(str.str().c_str()));
108 str.str("");
109 str << "Avg " << fmean;// << unitsText;
110 renderText(3, height()-3-3*fTextSize-27, QString(str.str().c_str()));
111 str.str("");
112 str << "RMS " << frms;// << unitsText;
113 renderText(3, height()-3-2*fTextSize-21, QString(str.str().c_str()));
114 str.str("");
115 str << "Min " << fmin;// << unitsText;
116 renderText(3, height()-3-3, QString(str.str().c_str()));
117 str.str("");
118 str << "Max " << fmax;// << unitsText;
119 renderText(3, height()-3-1*fTextSize-8, QString(str.str().c_str()));
120 //then draw the values beside the scale
121 //the difficulty here is to write the correct min/max besides the scale
122 //it depends whether the actual mean of the data is given by the user
123 //or not. the values given by user are fMin and fMax, while the data
124 //real min/max are fmin and fmax (I know, quite confusing... sorry about that)
125 //so. first let's see what is the span of one pixel
126 float min = (fMin < fScaleLimit || fMax < fScaleLimit) ? fmin : fMin;
127 float max = (fMin < fScaleLimit || fMax < fScaleLimit) ? fmax : fMax;
128// textSize = (int)(height()*12/600);
129 // setFont(QFont("Monospace", textSize));
130 float pixelSpan = (height() - fTextSize - 1)/(max - min);
131
132 //draw the scale values
133 float value = min;
134 int fontWidth = fTextSize;
135 if (fTextSize > 12) fontWidth--;
136 if (fTextSize > 10) fontWidth--;
137 if (fTextSize > 7) fontWidth--;//else fontWidth -=1;
138// if (fTextSize < 7) fontWidth++;
139 for (int i=0;i<11;i++)
140 {
141 str.str("");
142 str << value;
143 if (i==0 || i==10)
144 str << ' ' << unitsText;
145 str << ' ';
146 int h = (value - min)*pixelSpan;
147 if (logScale && h != 0)
148 {
149 float fh = h;
150 float mult = (max - min)*pixelSpan;
151 fh = log10(h*10.f/mult);
152 fh *= mult;
153 h = (int)fh;
154 }
155 h = height()-h;
156 int w = width() - (width()/50) - fontWidth*str.str().size();
157 if (i==0 || i==10) w -= width()/50;
158 if (i!=0 && i!=10) h -= fTextSize/2;
159 renderText(w, h, QString(str.str().c_str()));
160 value += (max - min)/10;
161 }
162
163/*
164 str.str("");
165 str << min << unitsText;
166 int fontWidth = textSize;
167 if (textSize > 12) fontWidth-=3; else fontWidth -= 2;
168 //height of min ?
169 int hmin = (min - min)*pixelSpan;
170 hmin = height() - hmin;
171 renderText(width() - (width()/25) - fontWidth*str.str().size(), hmin, QString(str.str().c_str()));
172 str.str("");
173 str << max << unitsText;
174 int hmax = (max - min)*pixelSpan;
175 hmax = height() - hmax;
176 renderText(width() - (width()/25) - fontWidth*str.str().size(), hmax, QString(str.str().c_str()));
177*/
178 glPopMatrix();
179
180// textSize = (int)(600*14/600);
181// setFont(QFont("Times", textSize));
182 }
183 void QCameraWidget::drawPatches()
184 {
185 glLineWidth(3.0f);
186 glColor3fv(patchesCoulour);
187 glBegin(GL_LINES);
188 for (int i=0;i<NTMARK;i++)
189 {
190 for (unsigned int j=0;j<patchesIndices[i].size();j++)
191 {
192 glVertex2fv(verticesList[patchesIndices[i][j].first]);
193 glVertex2fv(verticesList[patchesIndices[i][j].second]);
194 }
195 }
196 glEnd();
197 glTranslatef(0,0,0.1f);
198
199 glColor3fv(highlightedPatchesCoulour);
200 glBegin(GL_LINES);
201 for (vector<int>::iterator it=highlightedPatches.begin(); it!= highlightedPatches.end(); it++)
202 {
203 for (unsigned int j=0;j<patchesIndices[*it].size();j++)
204 {
205 glVertex2fv(verticesList[patchesIndices[*it][j].first]);
206 glVertex2fv(verticesList[patchesIndices[*it][j].second]);
207 }
208 }
209 glEnd();
210 if (fWhitePatch != -1)
211 {
212 glTranslatef(0,0,0.01);
213 glColor3f(1.f, 0.6f, 0.f);//patchColour);//[0],patchColour[1],patchColour[2]);//0.5f, 0.5f, 0.3f);
214 glBegin(GL_LINES);
215 for (unsigned int j=0;j<patchesIndices[fWhitePatch].size();j++)
216 {
217 glVertex2fv(verticesList[patchesIndices[fWhitePatch][j].first]);
218 glVertex2fv(verticesList[patchesIndices[fWhitePatch][j].second]);
219 }
220 glEnd();
221 }
222
223 }
224 void QCameraWidget::Reset()
225 {
226 fBold.assign(1440, false);
227 }
228
229 void QCameraWidget::mousePressEvent(QMouseEvent *cEvent)
230 {
231 if (cEvent->pos().x() > width()-(width()/50.f))
232 {
233 toggleInterfaceDisplay();
234 return;
235 }
236 int face = PixelAtPosition(cEvent->pos());
237// cout << face << endl;
238 if (face != -1) {
239 fWhite = face;
240 fWhitePatch = pixelsPatch[fWhite];
241 // CalculatePatchColor();
242 emit signalCurrentPixel(face);
243 }
244 else
245 {
246 fWhite = -1;
247 fWhitePatch = -1;
248 }
249 updateGL();
250 }
251 void QCameraWidget::mouseMoveEvent(QMouseEvent* cEvent)
252 {
253 int face = PixelAtPosition(cEvent->pos());
254 if (face != -1 && lastFace != face) {
255 emit signalPixelMoveOver(face);
256 }
257 if (lastFace != face)
258 {
259 if (fShowPixelMoveOver)
260 fWhite = face;
261
262 if (fShowPatchMoveOver)
263 fWhitePatch = face != -1 ? pixelsPatch[face] : -1;
264 }
265
266 if (fShowPixelMoveOver || fShowPatchMoveOver)
267 if (lastFace != face && isVisible())
268 updateGL();
269
270 lastFace = face;
271 }
272 void QCameraWidget::mouseDoubleClickEvent(QMouseEvent* cEvent)
273 {
274 int face = PixelAtPosition(cEvent->pos());
275 if (face != -1) {
276 // cout << "Event !" << endl;
277 fWhite = face;
278 fWhitePatch = pixelsPatch[fWhite];
279 // highlightPixel(face);
280 // highlightPatch(fWhitePatch);
281 // CalculatePatchColor();
282 emit signalPixelDoubleClick(face);
283 }
284 else
285 {
286 fWhite = -1;
287 fWhitePatch = -1;
288 // clearHighlightedPixels();
289 // clearHighlightedPatches();
290 }
291 updateGL();
292
293 }
294 void QCameraWidget::ShowPixelCursor(bool on)
295 {
296 fShowPixelMoveOver = on;
297 if (isVisible() && autoRefresh)
298 updateGL();
299 }
300 void QCameraWidget::ShowPatchCursor(bool on)
301 {
302 fShowPatchMoveOver = on;
303 if (isVisible() && autoRefresh)
304 updateGL();
305 }
306 void QCameraWidget::SetEnable(int idx, bool b)
307 {
308 fEnable[idx] = b;
309 }
310
311 double QCameraWidget::GetData(int idx)
312 {
313 return fData[idx];
314 }
315 const char* QCameraWidget::GetName()
316 {
317 return "QCameraWidget";
318 }
319 char *QCameraWidget::GetObjectInfo(int px, int py)
320 {
321
322 static stringstream stream;
323 static string str;
324 const int pixel = this->PixelAtPosition(QPoint(px, py));
325 if (pixel >= 0)
326 {
327 stream << "Pixel=" << pixel << " Data=" << fData[pixel] << '\0';
328 }
329 str = stream.str();
330 return const_cast<char*>(str.c_str());
331 }
332 void QCameraWidget::CalculatePixelsColor()
333 {
334 double dmin = fData[0];
335 double dmax = fData[0];
336 for (int ii=0;ii<ACTUAL_NUM_PIXELS;ii++)
337 {
338 if (finite(fData[ii]))
339 {
340 dmin = dmax = fData[ii];
341 break;
342 }
343 }
344 if (fMin < fScaleLimit || fMax < fScaleLimit)
345 {
346 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
347 {
348 if (!finite(fData[i])) continue;
349 if (!fEnable[i]) continue;
350 if (fData[i] > dmax) dmax = fData[i];
351 if (fData[i] < dmin) dmin = fData[i];
352 }
353 }
354 if (fMin > fScaleLimit) dmin = fMin;
355 if (fMax > fScaleLimit) dmax = fMax;
356// cout << "min: " << dmin << " max: " << dmax << " fMin: " << fMin << " fMax: " << fMax << endl;
357 float color;
358 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
359 {
360 if (!fEnable[i])
361 {
362// cout << "not enabled !" << i << endl;
363 pixelsColor[i][0] = 0.1f;
364 pixelsColor[i][1] = 0.1f;
365 pixelsColor[i][2] = 0.15f;
366 continue;
367 }
368 if (!finite(fData[i]))
369 {
370// cout << "not enabled !" << i << endl;
371 pixelsColor[i][0] = 0.9f;
372 pixelsColor[i][1] = 0.0f;
373 pixelsColor[i][2] = 0.9f;
374 continue;
375 }
376 if (fData[i] < dmin)
377 {
378 pixelsColor[i][0] = tooLowValueCoulour[0];
379 pixelsColor[i][1] = tooLowValueCoulour[1];
380 pixelsColor[i][2] = tooLowValueCoulour[2];
381 continue;
382 }
383 if (fData[i] > dmax)
384 {
385 pixelsColor[i][0] = tooHighValueCoulour[0];
386 pixelsColor[i][1] = tooHighValueCoulour[1];
387 pixelsColor[i][2] = tooHighValueCoulour[2];
388 continue;
389 }
390 color = float((fData[i]-dmin)/(dmax-dmin));
391 if (logScale)
392 {
393 color *= 9;
394 color += 1;
395 color = log10(color);
396 }
397
398 int index = 0;
399 while (ss[index] < color && index < 4)
400 index++;
401 index--;
402 if (index < 0) index = 0;
403 float weight0 = (color-ss[index]) / (ss[index+1]-ss[index]);
404 if (weight0 > 1.0f) weight0 = 1.0f;
405 if (weight0 < 0.0f) weight0 = 0.0f;
406 float weight1 = 1.0f-weight0;
407 pixelsColor[i][0] = weight1*rr[index] + weight0*rr[index+1];
408 pixelsColor[i][1] = weight1*gg[index] + weight0*gg[index+1];
409 pixelsColor[i][2] = weight1*bb[index] + weight0*bb[index+1];
410 }
411 CalculatePatchColor();
412 UpdateText();
413 pixelColorUpToDate = true;
414 }
415 void QCameraWidget::CalculatePatchColor()
416 {
417 return;
418 //calculate the patch contour color. let's use the anti-colour of the pixels
419 GLfloat averagePatchColour[3] = {0.0f,0.0f,0.0f};
420 for (int i=0;i<9;i++)
421 for (int j=0;j<3;j++)
422 averagePatchColour[j] += pixelsColor[softwareMapping[fWhitePatch*9+i]][j];
423 for (int j=0;j<3;j++)
424 averagePatchColour[j] /= 9;
425 for (int j=0;j<3;j++)
426 patchColour[j] = 1.0f - averagePatchColour[j];
427 }
428 void QCameraWidget::SetData(const valarray<double> &ddata)
429 {
430// fData = ddata;
431 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
432 fData[i] = ddata[i];
433 pixelColorUpToDate = false;
434 if (isVisible() && autoRefresh)
435 updateGL();
436 }
437
438void QCameraWidget::SetData(const valarray<float> &ddata)
439 {
440// fData = ddata;
441 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
442 fData[i] = ddata[i];
443 pixelColorUpToDate = false;
444 if (isVisible() && autoRefresh)
445 updateGL();
446 }
447
448
449 void QCameraWidget::highlightPixel(int idx, bool highlight)
450 {
451 if (idx < 0 || idx >= ACTUAL_NUM_PIXELS)
452 {
453 cout << "Error: requested pixel highlight out of bounds" << endl;
454 return;
455 }
456
457 const vector<int>::iterator v = ::find(highlightedPixels.begin(), highlightedPixels.end(), idx);
458 if (highlight)
459 {
460 if (v==highlightedPixels.end())
461 highlightedPixels.push_back(idx);
462 }
463 else
464 {
465 if (v!=highlightedPixels.end())
466 highlightedPixels.erase(v);
467 }
468
469 if (isVisible() && autoRefresh)
470 updateGL();
471 }
472 void QCameraWidget::highlightPatch(int idx, bool highlight)
473 {
474 if (idx < 0 || idx >= NTMARK)
475 {
476 cout << "Error: requested patch highlight out of bounds" << endl;
477 return;
478 }
479
480 const vector<int>::iterator v = ::find(highlightedPatches.begin(), highlightedPatches.end(), idx);
481 if (highlight)
482 {
483 if (v==highlightedPatches.end())
484 highlightedPatches.push_back(idx);
485 }
486 else
487 {
488 if (v!=highlightedPatches.end())
489 highlightedPatches.erase(v);
490 }
491
492 if (isVisible() && autoRefresh)
493 updateGL();
494
495 }
496 void QCameraWidget::clearHighlightedPatches()
497 {
498 highlightedPatches.clear();
499 if (isVisible() && autoRefresh)
500 updateGL();
501 }
502 void QCameraWidget::clearHighlightedPixels()
503 {
504 highlightedPixels.clear();
505 if (isVisible() && autoRefresh)
506 updateGL();
507 }
508
509
510
511
Note: See TracBrowser for help on using the repository browser.