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

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