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

Last change on this file since 12092 was 12092, checked in by lyard, 13 years ago
got rid of crappy nan display
File size: 15.1 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);
10 fEnable.resize(1440);
11 fBold.assign(1440, false);
12 fEnable.assign(1440, true);
13 lastFace = -1;
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 lastFace = face;
258 }
259 void QCameraWidget::mouseDoubleClickEvent(QMouseEvent* cEvent)
260 {
261 int face = PixelAtPosition(cEvent->pos());
262 if (face != -1) {
263 // cout << "Event !" << endl;
264 fWhite = face;
265 fWhitePatch = pixelsPatch[fWhite];
266 // highlightPixel(face);
267 // highlightPatch(fWhitePatch);
268 // CalculatePatchColor();
269 emit signalPixelDoubleClick(face);
270 }
271 else
272 {
273 fWhite = -1;
274 fWhitePatch = -1;
275 // clearHighlightedPixels();
276 // clearHighlightedPatches();
277 }
278 updateGL();
279
280 }
281
282 void QCameraWidget::SetEnable(int idx, bool b)
283 {
284 fEnable[idx] = b;
285 }
286
287 double QCameraWidget::GetData(int idx)
288 {
289 return fData[idx];
290 }
291 const char* QCameraWidget::GetName()
292 {
293 return "QCameraWidget";
294 }
295 char *QCameraWidget::GetObjectInfo(int px, int py)
296 {
297
298 static stringstream stream;
299 static string str;
300 const int pixel = this->PixelAtPosition(QPoint(px, py));
301 if (pixel >= 0)
302 {
303 stream << "Pixel=" << pixel << " Data=" << fData[pixel] << '\0';
304 }
305 str = stream.str();
306 return const_cast<char*>(str.c_str());
307 }
308 void QCameraWidget::CalculatePixelsColor()
309 {
310 double dmin = fData[0];
311 double dmax = fData[0];
312 for (int ii=0;ii<ACTUAL_NUM_PIXELS;ii++)
313 {
314 if (finite(fData[ii]))
315 {
316 dmin = dmax = fData[ii];
317 break;
318 }
319 }
320 if (fMin < fScaleLimit || fMax < fScaleLimit)
321 {
322 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
323 {
324 if (!finite(fData[i])) continue;
325 if (!fEnable[i]) continue;
326 if (fData[i] > dmax) dmax = fData[i];
327 if (fData[i] < dmin) dmin = fData[i];
328 }
329 }
330 if (fMin > fScaleLimit) dmin = fMin;
331 if (fMax > fScaleLimit) dmax = fMax;
332// cout << "min: " << dmin << " max: " << dmax << " fMin: " << fMin << " fMax: " << fMax << endl;
333 float color;
334 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
335 {
336 if (!fEnable[i] || !finite(fData[i]))
337 {
338// cout << "not enabled !" << i << endl;
339 pixelsColor[i][0] = 0.1f;
340 pixelsColor[i][1] = 0.1f;
341 pixelsColor[i][2] = 0.15f;
342 continue;
343 }
344 if (fData[i] < dmin)
345 {
346 pixelsColor[i][0] = tooLowValueCoulour[0];
347 pixelsColor[i][1] = tooLowValueCoulour[1];
348 pixelsColor[i][2] = tooLowValueCoulour[2];
349 continue;
350 }
351 if (fData[i] > dmax)
352 {
353 pixelsColor[i][0] = tooHighValueCoulour[0];
354 pixelsColor[i][1] = tooHighValueCoulour[1];
355 pixelsColor[i][2] = tooHighValueCoulour[2];
356 continue;
357 }
358 color = float((fData[i]-dmin)/(dmax-dmin));
359 if (logScale)
360 {
361 color *= 9;
362 color += 1;
363 color = log10(color);
364 }
365
366 int index = 0;
367 while (ss[index] < color && index < 4)
368 index++;
369 index--;
370 if (index < 0) index = 0;
371 float weight0 = (color-ss[index]) / (ss[index+1]-ss[index]);
372 if (weight0 > 1.0f) weight0 = 1.0f;
373 if (weight0 < 0.0f) weight0 = 0.0f;
374 float weight1 = 1.0f-weight0;
375 pixelsColor[i][0] = weight1*rr[index] + weight0*rr[index+1];
376 pixelsColor[i][1] = weight1*gg[index] + weight0*gg[index+1];
377 pixelsColor[i][2] = weight1*bb[index] + weight0*bb[index+1];
378 }
379 CalculatePatchColor();
380 UpdateText();
381 pixelColorUpToDate = true;
382 }
383 void QCameraWidget::CalculatePatchColor()
384 {
385 return;
386 //calculate the patch contour color. let's use the anti-colour of the pixels
387 GLfloat averagePatchColour[3] = {0.0f,0.0f,0.0f};
388 for (int i=0;i<9;i++)
389 for (int j=0;j<3;j++)
390 averagePatchColour[j] += pixelsColor[softwareMapping[patches[fWhitePatch][i]]][j];
391 for (int j=0;j<3;j++)
392 averagePatchColour[j] /= 9;
393 for (int j=0;j<3;j++)
394 patchColour[j] = 1.0f - averagePatchColour[j];
395 }
396 void QCameraWidget::SetData(const valarray<double> &ddata)
397 {
398// fData = ddata;
399 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
400 fData[i] = ddata[i];
401 pixelColorUpToDate = false;
402 if (isVisible() && autoRefresh)
403 updateGL();
404 }
405
406void QCameraWidget::SetData(const valarray<float> &ddata)
407 {
408// fData = ddata;
409 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
410 fData[i] = ddata[i];
411 pixelColorUpToDate = false;
412 if (isVisible() && autoRefresh)
413 updateGL();
414 }
415
416
417 void QCameraWidget::highlightPixel(int idx, bool highlight)
418 {
419 if (idx < 0 || idx >= ACTUAL_NUM_PIXELS)
420 {
421 cout << "Error: requested pixel highlight out of bounds" << endl;
422 return;
423 }
424
425 const vector<int>::iterator v = ::find(highlightedPixels.begin(), highlightedPixels.end(), idx);
426 if (highlight)
427 {
428 if (v==highlightedPixels.end())
429 highlightedPixels.push_back(idx);
430 }
431 else
432 {
433 if (v!=highlightedPixels.end())
434 highlightedPixels.erase(v);
435 }
436
437 if (isVisible() && autoRefresh)
438 updateGL();
439 }
440 void QCameraWidget::highlightPatch(int idx, bool highlight)
441 {
442 if (idx < 0 || idx >= NTMARK)
443 {
444 cout << "Error: requested patch highlight out of bounds" << endl;
445 return;
446 }
447
448 const vector<int>::iterator v = ::find(highlightedPatches.begin(), highlightedPatches.end(), idx);
449 if (highlight)
450 {
451 if (v==highlightedPatches.end())
452 highlightedPatches.push_back(idx);
453 }
454 else
455 {
456 if (v!=highlightedPatches.end())
457 highlightedPatches.erase(v);
458 }
459
460 if (isVisible() && autoRefresh)
461 updateGL();
462
463 }
464 void QCameraWidget::clearHighlightedPatches()
465 {
466 highlightedPatches.clear();
467 if (isVisible() && autoRefresh)
468 updateGL();
469 }
470 void QCameraWidget::clearHighlightedPixels()
471 {
472 highlightedPixels.clear();
473 if (isVisible() && autoRefresh)
474 updateGL();
475 }
476
477
478
479
Note: See TracBrowser for help on using the repository browser.