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

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