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

Last change on this file since 12105 was 12103, checked in by lyard, 13 years ago
added auto highlight of pixels
File size: 16.2 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 fShowPixelMoveOver = true;
15 fShowPatchMoveOver = true;
16
17 CalculatePixelsColor();
18
19 }
20
21 void QCameraWidget::paintGL()
22 {
23 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
24 glLoadIdentity();
25
26 glTranslatef(0,-0.44,0);
27 glTranslatef(-0.1,0,0);
28 glRotatef(cameraRotation, 0,0,-1);
29 if (cameraRotation == 90)
30 {
31 glTranslatef(-0.45,-0.45,0);
32 // cout << "correction" << endl;
33 }
34 if (cameraRotation == -90)
35 {
36 glTranslatef(0.45,-0.45,0);
37 }
38 glScalef(1.5, 1.5, 1.0);
39 glTranslatef(0,0,-0.5);
40 drawCamera(true);
41 glTranslatef(0,0,0.1f);
42
43 drawPatches();
44 glTranslatef(0,0,0.1f);
45
46 glLineWidth(1.0f);
47 glColor3fv(highlightedPixelsCoulour);
48 for (vector<int>::iterator it = highlightedPixels.begin(); it!= highlightedPixels.end(); it++)
49 {
50 drawHexagon(*it, false);
51 }
52
53 glLineWidth(1.0f);
54 glTranslatef(0,0,0.1f);
55
56 //glColor3f(1.f - pixelsColor[fWhite][0],1.f - pixelsColor[fWhite][1],1.f - pixelsColor[fWhite][2]);
57 if (fWhite != -1)
58 {
59 glColor3f(1.f, 0.f, 0.f);
60 drawHexagon(fWhite, false);
61 }
62 DrawCameraText();
63
64 DrawScale();
65
66// if (linearButton->isVisible())
67// repaintInterface();
68 }
69 void QCameraWidget::drawCamera(bool alsoWire)
70 {
71
72 if (!pixelColorUpToDate)
73 CalculatePixelsColor();
74 glLineWidth(1.0);
75 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
76 {
77 glColor3fv(pixelsColor[i]);
78 glLoadName(i);
79 drawHexagon(i,true);
80 }
81 if (!alsoWire)
82 return;
83 glTranslatef(0,0,0.1f);
84 glColor3fv(pixelContourColour);//0.0f,0.0f,0.0f);
85 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
86 {
87 drawHexagon(i, false);
88 }
89 }
90 void QCameraWidget::DrawCameraText()
91 {
92 glPushMatrix();
93 glLoadIdentity();
94
95
96
97// int textSize = (int)(width()*14/600);
98// setFont(QFont("Monospace", textSize));
99 qglColor(QColor(255, 223, 127));
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// cout << face << endl;
257 if (face != -1 && lastFace != face) {
258 emit signalPixelMoveOver(face);
259 }
260 if (lastFace != face)
261 {
262 if (fShowPixelMoveOver)
263 fWhite = face;
264
265 if (fShowPatchMoveOver)
266 if (face != -1)
267 fWhitePatch = pixelsPatch[face];
268 else
269 fWhitePatch = -1;
270 }
271
272 if (fShowPixelMoveOver || fShowPatchMoveOver)
273 if (lastFace != face && isVisible())
274 updateGL();
275
276 lastFace = face;
277 }
278 void QCameraWidget::mouseDoubleClickEvent(QMouseEvent* cEvent)
279 {
280 int face = PixelAtPosition(cEvent->pos());
281 if (face != -1) {
282 // cout << "Event !" << endl;
283 fWhite = face;
284 fWhitePatch = pixelsPatch[fWhite];
285 // highlightPixel(face);
286 // highlightPatch(fWhitePatch);
287 // CalculatePatchColor();
288 emit signalPixelDoubleClick(face);
289 }
290 else
291 {
292 fWhite = -1;
293 fWhitePatch = -1;
294 // clearHighlightedPixels();
295 // clearHighlightedPatches();
296 }
297 updateGL();
298
299 }
300 void QCameraWidget::ShowPixelCursor(bool on)
301 {
302 fShowPixelMoveOver = on;
303 if (isVisible() && autoRefresh)
304 updateGL();
305 }
306 void QCameraWidget::ShowPatchCursor(bool on)
307 {
308 fShowPatchMoveOver = on;
309 if (isVisible() && autoRefresh)
310 updateGL();
311 }
312 void QCameraWidget::SetEnable(int idx, bool b)
313 {
314 fEnable[idx] = b;
315 }
316
317 double QCameraWidget::GetData(int idx)
318 {
319 return fData[idx];
320 }
321 const char* QCameraWidget::GetName()
322 {
323 return "QCameraWidget";
324 }
325 char *QCameraWidget::GetObjectInfo(int px, int py)
326 {
327
328 static stringstream stream;
329 static string str;
330 const int pixel = this->PixelAtPosition(QPoint(px, py));
331 if (pixel >= 0)
332 {
333 stream << "Pixel=" << pixel << " Data=" << fData[pixel] << '\0';
334 }
335 str = stream.str();
336 return const_cast<char*>(str.c_str());
337 }
338 void QCameraWidget::CalculatePixelsColor()
339 {
340 double dmin = fData[0];
341 double dmax = fData[0];
342 for (int ii=0;ii<ACTUAL_NUM_PIXELS;ii++)
343 {
344 if (finite(fData[ii]))
345 {
346 dmin = dmax = fData[ii];
347 break;
348 }
349 }
350 if (fMin < fScaleLimit || fMax < fScaleLimit)
351 {
352 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
353 {
354 if (!finite(fData[i])) continue;
355 if (!fEnable[i]) continue;
356 if (fData[i] > dmax) dmax = fData[i];
357 if (fData[i] < dmin) dmin = fData[i];
358 }
359 }
360 if (fMin > fScaleLimit) dmin = fMin;
361 if (fMax > fScaleLimit) dmax = fMax;
362// cout << "min: " << dmin << " max: " << dmax << " fMin: " << fMin << " fMax: " << fMax << endl;
363 float color;
364 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
365 {
366 if (!fEnable[i])
367 {
368// cout << "not enabled !" << i << endl;
369 pixelsColor[i][0] = 0.1f;
370 pixelsColor[i][1] = 0.1f;
371 pixelsColor[i][2] = 0.15f;
372 continue;
373 }
374 if (!finite(fData[i]))
375 {
376// cout << "not enabled !" << i << endl;
377 pixelsColor[i][0] = 0.9f;
378 pixelsColor[i][1] = 0.0f;
379 pixelsColor[i][2] = 0.9f;
380 continue;
381 }
382 if (fData[i] < dmin)
383 {
384 pixelsColor[i][0] = tooLowValueCoulour[0];
385 pixelsColor[i][1] = tooLowValueCoulour[1];
386 pixelsColor[i][2] = tooLowValueCoulour[2];
387 continue;
388 }
389 if (fData[i] > dmax)
390 {
391 pixelsColor[i][0] = tooHighValueCoulour[0];
392 pixelsColor[i][1] = tooHighValueCoulour[1];
393 pixelsColor[i][2] = tooHighValueCoulour[2];
394 continue;
395 }
396 color = float((fData[i]-dmin)/(dmax-dmin));
397 if (logScale)
398 {
399 color *= 9;
400 color += 1;
401 color = log10(color);
402 }
403
404 int index = 0;
405 while (ss[index] < color && index < 4)
406 index++;
407 index--;
408 if (index < 0) index = 0;
409 float weight0 = (color-ss[index]) / (ss[index+1]-ss[index]);
410 if (weight0 > 1.0f) weight0 = 1.0f;
411 if (weight0 < 0.0f) weight0 = 0.0f;
412 float weight1 = 1.0f-weight0;
413 pixelsColor[i][0] = weight1*rr[index] + weight0*rr[index+1];
414 pixelsColor[i][1] = weight1*gg[index] + weight0*gg[index+1];
415 pixelsColor[i][2] = weight1*bb[index] + weight0*bb[index+1];
416 }
417 CalculatePatchColor();
418 UpdateText();
419 pixelColorUpToDate = true;
420 }
421 void QCameraWidget::CalculatePatchColor()
422 {
423 return;
424 //calculate the patch contour color. let's use the anti-colour of the pixels
425 GLfloat averagePatchColour[3] = {0.0f,0.0f,0.0f};
426 for (int i=0;i<9;i++)
427 for (int j=0;j<3;j++)
428 averagePatchColour[j] += pixelsColor[softwareMapping[patches[fWhitePatch][i]]][j];
429 for (int j=0;j<3;j++)
430 averagePatchColour[j] /= 9;
431 for (int j=0;j<3;j++)
432 patchColour[j] = 1.0f - averagePatchColour[j];
433 }
434 void QCameraWidget::SetData(const valarray<double> &ddata)
435 {
436// fData = ddata;
437 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
438 fData[i] = ddata[i];
439 pixelColorUpToDate = false;
440 if (isVisible() && autoRefresh)
441 updateGL();
442 }
443
444void QCameraWidget::SetData(const valarray<float> &ddata)
445 {
446// fData = ddata;
447 for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
448 fData[i] = ddata[i];
449 pixelColorUpToDate = false;
450 if (isVisible() && autoRefresh)
451 updateGL();
452 }
453
454
455 void QCameraWidget::highlightPixel(int idx, bool highlight)
456 {
457 if (idx < 0 || idx >= ACTUAL_NUM_PIXELS)
458 {
459 cout << "Error: requested pixel highlight out of bounds" << endl;
460 return;
461 }
462
463 const vector<int>::iterator v = ::find(highlightedPixels.begin(), highlightedPixels.end(), idx);
464 if (highlight)
465 {
466 if (v==highlightedPixels.end())
467 highlightedPixels.push_back(idx);
468 }
469 else
470 {
471 if (v!=highlightedPixels.end())
472 highlightedPixels.erase(v);
473 }
474
475 if (isVisible() && autoRefresh)
476 updateGL();
477 }
478 void QCameraWidget::highlightPatch(int idx, bool highlight)
479 {
480 if (idx < 0 || idx >= NTMARK)
481 {
482 cout << "Error: requested patch highlight out of bounds" << endl;
483 return;
484 }
485
486 const vector<int>::iterator v = ::find(highlightedPatches.begin(), highlightedPatches.end(), idx);
487 if (highlight)
488 {
489 if (v==highlightedPatches.end())
490 highlightedPatches.push_back(idx);
491 }
492 else
493 {
494 if (v!=highlightedPatches.end())
495 highlightedPatches.erase(v);
496 }
497
498 if (isVisible() && autoRefresh)
499 updateGL();
500
501 }
502 void QCameraWidget::clearHighlightedPatches()
503 {
504 highlightedPatches.clear();
505 if (isVisible() && autoRefresh)
506 updateGL();
507 }
508 void QCameraWidget::clearHighlightedPixels()
509 {
510 highlightedPixels.clear();
511 if (isVisible() && autoRefresh)
512 updateGL();
513 }
514
515
516
517
Note: See TracBrowser for help on using the repository browser.