00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00036 #include <QtGui>
00037 #include <QtOpenGL>
00038 #include <qgl.h>
00039 #include <qevent.h>
00040 #include <GL/glu.h>
00041 #include <stdlib.h>
00042 #include <stdio.h>
00043 #include <math.h>
00044
00045 #include "refimagewidget.h"
00046
00050 RefImgWidget::RefImgWidget(QWidget *parent)
00051 : QGLWidget(parent)
00052 {
00053 drawAble = false;
00054 setMouseTracking(true);
00055 mainWindow = qobject_cast<QMainWindow *>(parentWidget());
00056 mainWindow->setWindowTitle("Reference Image");
00057 mainWindow->statusBar()->showMessage("Reference Image");
00058 mainWindow->statusBar()->setFixedHeight(20);
00059 setFocusPolicy(Qt::StrongFocus);
00060 }
00061
00065 RefImgWidget::~RefImgWidget()
00066 {
00067 makeCurrent();
00068 }
00069
00075 void RefImgWidget::mouseMoveEvent( QMouseEvent *e )
00076 {
00077 if( drawAble ){
00078 int r = getColor(e->x(), e->y(), 0);
00079 int g = getColor(e->x(), e->y(), 1);
00080 int b = getColor(e->x(), e->y(), 2);
00081
00082 float x = getCoord(e->x(), e->y(), 0);
00083 float y = getCoord(e->x(), e->y(), 1);
00084 float z = getCoord(e->x(), e->y(), 2);
00085
00086 QString xSt, ySt, zSt;
00087 xSt.sprintf("%2.2f", x);
00088 ySt.sprintf("%2.2f", y);
00089 zSt.sprintf("%2.2f", z);
00090
00091 if( r != -1 && g != -1 && b != -1 && e->x() >=0 && e->y() >= 0)
00092 if( x == -10 || y == -10 || z == -10)
00093 mainWindow->statusBar()->showMessage(QString("2D(%1,%2)Color(%3,%4,%5)").arg(e->x()).arg(e->y()).arg(r).arg(g).arg(b));
00094 else
00095 mainWindow->statusBar()->showMessage(QString("2D(%1,%2)Color(%3,%4,%5)3D(%6,%7,%8)").arg(e->x()).arg(e->y()).arg(r).arg(g).arg(b).arg(xSt).arg(ySt).arg(zSt));
00096 }
00097 }
00098
00104 int RefImgWidget::getColor(int x, int y, int z){
00105 if( x >= fileReader->getNCol() || y >= fileReader->getNRow() || x < 0 || y < 0)
00106 return -1;
00107 else
00108 return (int)fileReader->imagePixels[y][x][z];
00109 }
00110
00116 float RefImgWidget::getCoord(int x, int y, int k){
00117 int mousePoint = -1;
00118
00119 for(int i = 0; i < fileReader->getNp(); i++ ){
00120 if( (int) fileReader->pointArray[i][3] == y && (int)fileReader->pointArray[i][4] == x ){
00121 mousePoint = i;
00122 emit setMousePoint(mousePoint);
00123 point3dWidget->updateGL();
00124 return fileReader->pointArray[i][k];
00125 }
00126 }
00127
00128 emit setMousePoint(mousePoint);
00129 point3dWidget->updateGL();
00130
00131
00132 return -10;
00133 }
00134
00140 void RefImgWidget::initializeGL()
00141 {
00142 glClearColor (0.0, 0.0, 0.0, 0.0);
00143 glShadeModel(GL_FLAT);
00144 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00145 }
00146
00152 void RefImgWidget::paintGL()
00153 {
00154 makeCurrent();
00155 glClear(GL_COLOR_BUFFER_BIT);
00156 if(drawAble){
00157 int height = fileReader->getNRow();
00158 int width = fileReader->getNCol();
00159 if( windowHeight - height < 0 ){
00160 glViewport(0, windowHeight - height , (GLsizei) windowHeight, (GLsizei) windowWidth);
00161 glRasterPos2i (0, 0);
00162 }else
00163 glRasterPos2i (0, windowHeight-height );
00164
00165
00166 GLubyte pixels[height][width][3];
00167
00168 for(int i = 0; i < height; i ++)
00169 for(int j = 0; j < width; j ++)
00170 for( int k = 0; k < 3; k ++ )
00171 pixels[i][j][k] = fileReader->imagePixels[height-i][j][k];
00172
00173 glDrawPixels(width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
00174
00175 }
00176 adjustSize();
00177 glFlush ();
00178 }
00179
00185 void RefImgWidget::updateFile( FileReader *fd){
00186 fileReader = fd;
00187 setDrawAble(true);
00188 updateGL();
00189 }
00190
00196 void RefImgWidget::setDrawAble(bool status){
00197 drawAble = status;
00198 }
00199
00205 void RefImgWidget::setPointWidget(Point3dWidget *pw){
00206 point3dWidget = pw;
00207 connect(this, SIGNAL(setMousePoint(int)), point3dWidget, SLOT(setMousePoint(int)));
00208 }
00209
00215 void RefImgWidget::resizeGL(int w, int h)
00216 {
00217 glViewport(0, 0, (GLsizei) w, (GLsizei) h);
00218 windowHeight = h;
00219 windowWidth = w;
00220 myHeight = (GLint) h;
00221 glMatrixMode(GL_PROJECTION);
00222 glLoadIdentity();
00223 gluOrtho2D(0.0, (GLdouble) w, 0.0, (GLdouble) h);
00224 glMatrixMode(GL_MODELVIEW);
00225 glLoadIdentity();
00226 }
00227
00234 void RefImgWidget::keyPressEvent( QKeyEvent *e )
00235 {
00236 switch ( e->key() )
00237 {
00238 default:
00239 break;
00240 }
00241 }