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

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