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

Last change on this file since 12096 was 12093, checked in by lyard, 14 years ago
set nan values color to purple
File size: 15.4 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])
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 (!finite(fData[i]))
345 {
346// cout << "not enabled !" << i << endl;
347 pixelsColor[i][0] = 0.9f;
348 pixelsColor[i][1] = 0.0f;
349 pixelsColor[i][2] = 0.9f;
350 continue;
351 }
352 if (fData[i] < dmin)
353 {
354 pixelsColor[i][0] = tooLowValueCoulour[0];
355 pixelsColor[i][1] = tooLowValueCoulour[1];
356 pixelsColor[i][2] = tooLowValueCoulour[2];
357 continue;
358 }
359 if (fData[i] > dmax)
360 {
361 pixelsColor[i][0] = tooHighValueCoulour[0];
362 pixelsColor[i][1] = tooHighValueCoulour[1];
363 pixelsColor[i][2] = tooHighValueCoulour[2];
364 continue;
365 }
366 color = float((fData[i]-dmin)/(dmax-dmin));
367 if (logScale)
368 {
369 color *= 9;
370 color += 1;
371 color = log10(color);
372 }
373
374 int index = 0;
375 while (ss[index] < color && index < 4)
376 index++;
377 index--;
378 if (index < 0) index = 0;
379 float weight0 = (color-ss[index]) / (ss[index+1]-ss[index]);
380 if (weight0 > 1.0f) weight0 = 1.0f;
381 if (weight0 < 0.0f) weight0 = 0.0f;
382 float weight1 = 1.0f-weight0;
383 pixelsColor[i][0] = weight1*rr[index] + weight0*rr[index+1];
384 pixelsColor[i][1] = weight1*gg[index] + weight0*gg[index+1];
385 pixelsColor[i][2] = weight1*bb[index] + weight0*bb[index+1];
386 }
387 CalculatePatchColor();
388 UpdateText();
389 pixelColorUpToDate = true;
390 }
391 void QCameraWidget::CalculatePatchColor()
392 {
393 return;
394 //calculate the patch contour color. let's use the anti-colour of the pixels
395 GLfloat averagePatchColour[3] = {0.0f,0.0f,0.0f};
396 for (int i=0;i<9;i++)
397 for (int j=0;j<3;j++)
398 averagePatchColour[j] += pixelsColor[softwareMapping[patches[fWhitePatch][i]]][j];
399 for (int j=0;j<3;j++)
400 averagePatchColour[j] /= 9;
401 for (int j=0;j<3;j++)
402 patchColour[j] = 1.0f - averagePatchColour[j];
403 }
404 void QCameraWidget::SetData(const valarray<double> &ddata)
405 {
406// fData = ddata;
407 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
408 fData[i] = ddata[i];
409 pixelColorUpToDate = false;
410 if (isVisible() && autoRefresh)
411 updateGL();
412 }
413
414void QCameraWidget::SetData(const valarray<float> &ddata)
415 {
416// fData = ddata;
417 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
418 fData[i] = ddata[i];
419 pixelColorUpToDate = false;
420 if (isVisible() && autoRefresh)
421 updateGL();
422 }
423
424
425 void QCameraWidget::highlightPixel(int idx, bool highlight)
426 {
427 if (idx < 0 || idx >= ACTUAL_NUM_PIXELS)
428 {
429 cout << "Error: requested pixel highlight out of bounds" << endl;
430 return;
431 }
432
433 const vector<int>::iterator v = ::find(highlightedPixels.begin(), highlightedPixels.end(), idx);
434 if (highlight)
435 {
436 if (v==highlightedPixels.end())
437 highlightedPixels.push_back(idx);
438 }
439 else
440 {
441 if (v!=highlightedPixels.end())
442 highlightedPixels.erase(v);
443 }
444
445 if (isVisible() && autoRefresh)
446 updateGL();
447 }
448 void QCameraWidget::highlightPatch(int idx, bool highlight)
449 {
450 if (idx < 0 || idx >= NTMARK)
451 {
452 cout << "Error: requested patch highlight out of bounds" << endl;
453 return;
454 }
455
456 const vector<int>::iterator v = ::find(highlightedPatches.begin(), highlightedPatches.end(), idx);
457 if (highlight)
458 {
459 if (v==highlightedPatches.end())
460 highlightedPatches.push_back(idx);
461 }
462 else
463 {
464 if (v!=highlightedPatches.end())
465 highlightedPatches.erase(v);
466 }
467
468 if (isVisible() && autoRefresh)
469 updateGL();
470
471 }
472 void QCameraWidget::clearHighlightedPatches()
473 {
474 highlightedPatches.clear();
475 if (isVisible() && autoRefresh)
476 updateGL();
477 }
478 void QCameraWidget::clearHighlightedPixels()
479 {
480 highlightedPixels.clear();
481 if (isVisible() && autoRefresh)
482 updateGL();
483 }
484
485
486
487
Note: See TracBrowser for help on using the repository browser.