source: branches/fscctrl_safety_limits/gui/QCameraWidget.cc@ 18811

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