| 1 | #include "QCameraWidget.h"
|
|---|
| 2 | #include <sstream>
|
|---|
| 3 | #include <iostream>
|
|---|
| 4 |
|
|---|
| 5 | QCameraWidget::QCameraWidget(QWidget *pparent) : BasicGlCamera(pparent)
|
|---|
| 6 | {
|
|---|
| 7 | fWhite = 0;
|
|---|
| 8 | fWhitePatch = pixelsPatch[0];
|
|---|
| 9 | fBold.resize(1440);
|
|---|
| 10 | fEnable.resize(1440);
|
|---|
| 11 | fBold.assign(1440, false);
|
|---|
| 12 | fEnable.assign(1440, true);
|
|---|
| 13 | fMin = -1;
|
|---|
| 14 | fMax = -1;
|
|---|
| 15 | lastFace = -1;
|
|---|
| 16 | CalculatePixelsColor();
|
|---|
| 17 |
|
|---|
| 18 | }
|
|---|
| 19 | void QCameraWidget::paintGL()
|
|---|
| 20 | {
|
|---|
| 21 | glClear(GL_COLOR_BUFFER_BIT);
|
|---|
| 22 | glLoadIdentity();
|
|---|
| 23 |
|
|---|
| 24 | glTranslatef(0,-0.44,0);
|
|---|
| 25 | glScalef(1.5, 1.5, 1.5);
|
|---|
| 26 | drawCamera(true);
|
|---|
| 27 | drawPatches();
|
|---|
| 28 |
|
|---|
| 29 | glLineWidth(1.0f);
|
|---|
| 30 | glColor3f(1,1,1);
|
|---|
| 31 | drawHexagon(fWhite, false);
|
|---|
| 32 |
|
|---|
| 33 | DrawCameraText();
|
|---|
| 34 | }
|
|---|
| 35 | void QCameraWidget::drawCamera(bool alsoWire)
|
|---|
| 36 | {
|
|---|
| 37 | if (!pixelColorUpToDate)
|
|---|
| 38 | CalculatePixelsColor();
|
|---|
| 39 | glLineWidth(1.0);
|
|---|
| 40 | for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
|
|---|
| 41 | {
|
|---|
| 42 | glColor3fv(pixelsColor[i]);
|
|---|
| 43 | glLoadName(i);
|
|---|
| 44 | drawHexagon(i,true);
|
|---|
| 45 | }
|
|---|
| 46 | if (!alsoWire)
|
|---|
| 47 | return;
|
|---|
| 48 | glColor3f(0.0f,0.0f,0.0f);
|
|---|
| 49 | for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
|
|---|
| 50 | {
|
|---|
| 51 | drawHexagon(i, false);
|
|---|
| 52 | }
|
|---|
| 53 | }
|
|---|
| 54 | void QCameraWidget::drawPatches()
|
|---|
| 55 | {
|
|---|
| 56 | glLineWidth(2.0f);
|
|---|
| 57 | glColor3f(patchColour[0],patchColour[1],patchColour[2]);//0.5f, 0.5f, 0.3f);
|
|---|
| 58 | glBegin(GL_LINES);
|
|---|
| 59 | // for (int i=0;i<NTMARK;i++)
|
|---|
| 60 | // {
|
|---|
| 61 | for (unsigned int j=0;j<patchesIndices[fWhitePatch].size();j++)
|
|---|
| 62 | {
|
|---|
| 63 | glVertex2fv(verticesList[patchesIndices[fWhitePatch][j].first]);
|
|---|
| 64 | glVertex2fv(verticesList[patchesIndices[fWhitePatch][j].second]);
|
|---|
| 65 | }
|
|---|
| 66 | // }
|
|---|
| 67 | glEnd();
|
|---|
| 68 | }
|
|---|
| 69 | void QCameraWidget::Reset()
|
|---|
| 70 | {
|
|---|
| 71 | fBold.assign(1440, false);
|
|---|
| 72 | }
|
|---|
| 73 | void QCameraWidget::mousePressEvent(QMouseEvent *cEvent)
|
|---|
| 74 | {
|
|---|
| 75 | int face = PixelAtPosition(cEvent->pos());
|
|---|
| 76 | if (face != -1) {
|
|---|
| 77 | fWhite = face;
|
|---|
| 78 | fWhitePatch = pixelsPatch[fWhite];
|
|---|
| 79 | CalculatePatchColor();
|
|---|
| 80 | emit signalCurrentPixel(face);
|
|---|
| 81 | updateGL();
|
|---|
| 82 | }
|
|---|
| 83 | }
|
|---|
| 84 | void QCameraWidget::mouseMoveEvent(QMouseEvent* cEvent)
|
|---|
| 85 | {
|
|---|
| 86 | int face = PixelAtPosition(cEvent->pos());
|
|---|
| 87 | if (face != -1 && lastFace != face) {
|
|---|
| 88 | emit signalPixelMoveOver(face);
|
|---|
| 89 | }
|
|---|
| 90 | lastFace = face;
|
|---|
| 91 | }
|
|---|
| 92 | void QCameraWidget::mouseDoubleClickEvent(QMouseEvent* cEvent)
|
|---|
| 93 | {
|
|---|
| 94 | int face = PixelAtPosition(cEvent->pos());
|
|---|
| 95 | if (face != -1) {
|
|---|
| 96 | fWhite = face;
|
|---|
| 97 | fWhitePatch = pixelsPatch[fWhite];
|
|---|
| 98 | CalculatePatchColor();
|
|---|
| 99 | emit signalPixelDoubleClick(face);
|
|---|
| 100 | updateGL();
|
|---|
| 101 | }
|
|---|
| 102 | }
|
|---|
| 103 | void QCameraWidget::SetBold(int idx)
|
|---|
| 104 | {
|
|---|
| 105 | //cout << "Boldness not taken into account yet" << endl;
|
|---|
| 106 | }
|
|---|
| 107 | void QCameraWidget::SetWhite(int idx)
|
|---|
| 108 | {
|
|---|
| 109 | fWhite = idx;
|
|---|
| 110 | fWhitePatch = pixelsPatch[fWhite];
|
|---|
| 111 | CalculatePatchColor();
|
|---|
| 112 | }
|
|---|
| 113 | void QCameraWidget::SetEnable(int idx, bool b)
|
|---|
| 114 | {
|
|---|
| 115 | fEnable[idx] = b;
|
|---|
| 116 | }
|
|---|
| 117 | void QCameraWidget::Toggle(int idx)
|
|---|
| 118 | {
|
|---|
| 119 | }
|
|---|
| 120 | double QCameraWidget::GetData(int idx)
|
|---|
| 121 | {
|
|---|
| 122 | return fData[idx];
|
|---|
| 123 | }
|
|---|
| 124 | void QCameraWidget::SetMin(int64_t min)
|
|---|
| 125 | {
|
|---|
| 126 | fMin = min;
|
|---|
| 127 | pixelColorUpToDate = false;
|
|---|
| 128 | if (isVisible())
|
|---|
| 129 | updateGL();
|
|---|
| 130 | }
|
|---|
| 131 | void QCameraWidget::SetMax(int64_t max)
|
|---|
| 132 | {
|
|---|
| 133 | fMax = max;
|
|---|
| 134 | pixelColorUpToDate = false;
|
|---|
| 135 | if (isVisible())
|
|---|
| 136 | updateGL();
|
|---|
| 137 | }
|
|---|
| 138 | const char* QCameraWidget::GetName()
|
|---|
| 139 | {
|
|---|
| 140 | return "QCameraWidget";
|
|---|
| 141 | }
|
|---|
| 142 | char *QCameraWidget::GetObjectInfo(int px, int py)
|
|---|
| 143 | {
|
|---|
| 144 |
|
|---|
| 145 | static stringstream stream;
|
|---|
| 146 | static string str;
|
|---|
| 147 | const int pixel = this->PixelAtPosition(QPoint(px, py));
|
|---|
| 148 | if (pixel >= 0)
|
|---|
| 149 | {
|
|---|
| 150 | stream << "Pixel=" << pixel << " Data=" << fData[pixel] << '\0';
|
|---|
| 151 | }
|
|---|
| 152 | str = stream.str();
|
|---|
| 153 | return const_cast<char*>(str.c_str());
|
|---|
| 154 | }
|
|---|
| 155 | void QCameraWidget::CalculatePixelsColor()
|
|---|
| 156 | {
|
|---|
| 157 | double dmin = fData[0];
|
|---|
| 158 | double dmax = fData[0];
|
|---|
| 159 | if (fMin < 0 && fMax < 0)
|
|---|
| 160 | for (int i=0;i<1440;i++)
|
|---|
| 161 | {
|
|---|
| 162 | if (!fEnable[i]) continue;
|
|---|
| 163 | if (fData[i] > dmax) dmax = fData[i];
|
|---|
| 164 | if (fData[i] < dmin) dmin = fData[i];
|
|---|
| 165 | }
|
|---|
| 166 | if (fMin >= 0) dmin = fMin;
|
|---|
| 167 | if (fMax >= 0) dmax = fMax;
|
|---|
| 168 | float color;
|
|---|
| 169 | for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
|
|---|
| 170 | {
|
|---|
| 171 | if (fData[i] < dmin)
|
|---|
| 172 | {
|
|---|
| 173 | pixelsColor[i][0] = pixelsColor[i][1] = pixelsColor[i][2] = 0;
|
|---|
| 174 | continue;
|
|---|
| 175 | }
|
|---|
| 176 | if (fData[i] > dmax)
|
|---|
| 177 | {
|
|---|
| 178 | pixelsColor[i][0] = pixelsColor[i][1] = pixelsColor[i][2] = 1;
|
|---|
| 179 | continue;
|
|---|
| 180 | }
|
|---|
| 181 | color = float((fData[i]-dmin)/(dmax-dmin));
|
|---|
| 182 | if (!fEnable[i])
|
|---|
| 183 | color = 0;
|
|---|
| 184 | int index = 0;
|
|---|
| 185 | while (ss[index] < color && index < 4)
|
|---|
| 186 | index++;
|
|---|
| 187 | index--;
|
|---|
| 188 | if (index < 0) index = 0;
|
|---|
| 189 | float weight0 = (color-ss[index]) / (ss[index+1]-ss[index]);
|
|---|
| 190 | if (weight0 > 1.0f) weight0 = 1.0f;
|
|---|
| 191 | if (weight0 < 0.0f) weight0 = 0.0f;
|
|---|
| 192 | float weight1 = 1.0f-weight0;
|
|---|
| 193 | pixelsColor[i][0] = weight1*rr[index] + weight0*rr[index+1];
|
|---|
| 194 | pixelsColor[i][1] = weight1*gg[index] + weight0*gg[index+1];
|
|---|
| 195 | pixelsColor[i][2] = weight1*bb[index] + weight0*bb[index+1];
|
|---|
| 196 | }
|
|---|
| 197 | CalculatePatchColor();
|
|---|
| 198 | pixelColorUpToDate = true;
|
|---|
| 199 | }
|
|---|
| 200 | void QCameraWidget::CalculatePatchColor()
|
|---|
| 201 | {
|
|---|
| 202 | //calculate the patch contour color. let's use the anti-colour of the pixels
|
|---|
| 203 | GLfloat averagePatchColour[3] = {0.0f,0.0f,0.0f};
|
|---|
| 204 | for (int i=0;i<9;i++)
|
|---|
| 205 | for (int j=0;j<3;j++)
|
|---|
| 206 | averagePatchColour[j] += pixelsColor[softwareMapping[patches[fWhitePatch][i]]][j];
|
|---|
| 207 | for (int j=0;j<3;j++)
|
|---|
| 208 | averagePatchColour[j] /= 9;
|
|---|
| 209 | for (int j=0;j<3;j++)
|
|---|
| 210 | patchColour[j] = 1.0f - averagePatchColour[j];
|
|---|
| 211 | }
|
|---|
| 212 | void QCameraWidget::SetData(const valarray<double> &ddata)
|
|---|
| 213 | {
|
|---|
| 214 | // fData = ddata;
|
|---|
| 215 | for (int i=0;i<ACTUAL_NUM_PIXELS;i++)
|
|---|
| 216 | fData[i] = ddata[i];
|
|---|
| 217 | pixelColorUpToDate = false;
|
|---|
| 218 | if (isVisible())
|
|---|
| 219 | updateGL();
|
|---|
| 220 | }
|
|---|
| 221 | void QCameraWidget::DrawCameraText()
|
|---|
| 222 | {
|
|---|
| 223 | glPushMatrix();
|
|---|
| 224 | glLoadIdentity();
|
|---|
| 225 | cout << width() << " " << height() << endl;
|
|---|
| 226 | int textSize = (int)(height()*14/600);
|
|---|
| 227 | setFont(QFont("Times", textSize));
|
|---|
| 228 | qglColor(QColor(255,223,127));
|
|---|
| 229 | float shiftx = 0.01f;//0.55f;
|
|---|
| 230 | float shifty = 0.01f;//0.65f;
|
|---|
| 231 | renderText(-shownSizex/2.f + shiftx, -shownSizey/2.f + shifty, 0.f, QString("This is a first text that is scaled according to the drawing size"));
|
|---|
| 232 | glPopMatrix();
|
|---|
| 233 | }
|
|---|