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

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