#include "VTracking3DView.h" #include "VTrackingData.h" #include "VTrackingScene.h" #include #include #include #include #include #include using namespace std; #ifndef GL_MULTISAMPLE #define GL_MULTISAMPLE 0x809D #endif VTracking3DView::VTracking3DView( VTrackingScene* scene, QMainWindow* mwind, QWidget *parent) : QGLWidget(QGLFormat(QGL::SampleBuffers), parent) { fMWind=mwind; fScene=scene; fXRot = 0; fYRot = 0; fZRot = 0; fOrthoLeft = -200.; fOrthoRight = 200.; fOrthoBottom = -200.; fOrthoTop = 200.; fOrthoNearVal = -2000.; fOrthoFarVal = 2000.; fDX=0; fDY=0; fDZ=0; fWidthDivHeight=1.0; setMouseTracking(true); } VTracking3DView::~VTracking3DView() { } QSize VTracking3DView::minimumSizeHint() const { return QSize(200, 200); } QSize VTracking3DView::sizeHint() const { return QSize(400, 400); } static void qNormalizeAngle(int &angle) { while (angle < 0) angle += 360 * 16; while (angle > 360 * 16) angle -= 360 * 16; } void VTracking3DView::setXRotation(int angle) { qNormalizeAngle(angle); if (angle != fXRot) { fXRot = angle; emit xRotationChanged(angle); updateGL(); } } void VTracking3DView::setYRotation(int angle) { qNormalizeAngle(angle); if (angle != fYRot) { fYRot = angle; emit yRotationChanged(angle); updateGL(); } } void VTracking3DView::setZRotation(int angle) { qNormalizeAngle(angle); if (angle != fZRot) { fZRot = angle; emit zRotationChanged(angle); updateGL(); } } void VTracking3DView::initializeGL() { qglClearColor(Qt::black); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glShadeModel(GL_SMOOTH); // glEnable(GL_LIGHTING); // glEnable(GL_LIGHT0); glEnable(GL_MULTISAMPLE); // static GLfloat lightPosition[4] = { 0.5, 5.0, 7.0, 0.0 }; // glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); } void VTracking3DView::paintGL() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(fDX, fDY, fDZ); glRotatef(fXRot / 16.0, 1.0, 0.0, 0.0); glRotatef(fYRot / 16.0, 0.0, 1.0, 0.0); glRotatef(fZRot / 16.0, 0.0, 0.0, 1.0); glEnable(GL_POINT_SMOOTH); glEnable(GL_LINE_SMOOTH); glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE); PaintAxis(); DrawHits(); DrawTracks(); } void VTracking3DView::PaintAxis() { int i; char txt[20]; //Axises //Z const int cStepZ=100; const int cStepsZ=12; glColor4f(1.0, 1.0, 1.0, 1.0); glBegin(GL_LINES); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 0.0, cStepZ*cStepsZ); glEnd(); glColor4f(1.0, 1.0, 1.0, 1.0); glPointSize(3); glBegin(GL_POINTS); for(i=1;i<=cStepsZ;i++) glVertex3f(0.0, 0.0, i*cStepZ); glEnd(); //X const int cStepX=20; const int cStepsX=10; glColor4f(1.0, 1.0, 1.0, 1.0); glBegin(GL_LINES); glVertex3f(-cStepX*cStepsX, 0.0, 0.0); glVertex3f( cStepX*cStepsX, 0.0, 0.0); glEnd(); glColor4f(1.0, 1.0, 1.0, 1.0); glPointSize(3); glBegin(GL_POINTS); for(i=-cStepsX;i<=cStepsX;i++) glVertex3f(i*cStepX, 0.0, 0.0); glEnd(); //Y const int cStepY=20; const int cStepsY=10; glColor4f(1.0, 1.0, 1.0, 1.0); glBegin(GL_LINES); glVertex3f(0.0,-cStepY*cStepsY, 0.0); glVertex3f(0.0, cStepY*cStepsY, 0.0); glEnd(); glColor4f(1.0, 1.0, 1.0, 1.0); glPointSize(3); glBegin(GL_POINTS); for(i=-cStepsY;i<=cStepsY;i++) glVertex3f(0.0, i*cStepY, 0.0); glEnd(); qglColor(Qt::white); renderText(0.0, 0.0, 0.0, " 0"); for(i=1;i<=cStepsZ;i++) { sprintf(txt," %d", i*cStepZ); renderText(0.0, 0.0, i*cStepZ, txt); } for(i=-cStepsX;i<=cStepsX;i++) { sprintf(txt," %d", i*cStepX); renderText(i*cStepX, 0.0, 0.0, txt); } renderText((i-0.5)*cStepX, 0.0, 0.0, "X"); for(i=-cStepsY;i<=cStepsY;i++) { sprintf(txt," %d", i*cStepY); renderText(0.0, i*cStepY, 0.0, txt); } renderText(0.0, (i-0.5)*cStepY, 0.0, "Y"); } void VTracking3DView::DrawHits() { unsigned int i; const QColor& c1 = fScene->GetColor(kVHIT); glColor4f(c1.redF(), c1.greenF(), c1.blueF(), 1.0); glPointSize(fScene->GetSize(kVHIT)); glBegin(GL_POINTS); for (i = 0; i < fScene->GetHitsSize(); i++) { const VMyHit& hit = fScene->GetHit(i); glVertex3f(hit.GetX(), hit.GetY(), hit.GetZ()); } glEnd(); const QColor& c2 = fScene->GetColor(kVPOINT); glColor4f(c2.redF(), c2.greenF(), c2.blueF(), 1.0); glPointSize(fScene->GetSize(kVPOINT)); glBegin(GL_POINTS); for (i = 0; i < fScene->GetPointsSize(); i++) { const VMyHit& point = fScene->GetPoint(i); glVertex3f(point.GetX(), point.GetY(), point.GetZ()); } glEnd(); } void VTracking3DView::DrawTracks() { unsigned int i; const QColor& c3 = fScene->GetColor(kVTRACK); glColor4f(c3.redF(), c3.greenF(), c3.blueF(), 1.0); glLineWidth(fScene->GetSize(kVTRACK)); for (i = 0; i < fScene->GetTracksSize(); i++) { const VMyTrack& track = fScene->GetTrack(i); const VMyHit& lasthit=track.GetHit(track.GetNofHits()-1); glBegin(GL_LINES); for (unsigned int j = 0; j < track.GetNofHits() - 1; j++){ const VMyHit& hit1 = track.GetHit(j); const VMyHit& hit2 = track.GetHit(j+1); glVertex3f(hit1.GetX(), hit1.GetY(), hit1.GetZ()); glVertex3f(hit2.GetX(), hit2.GetY(), hit2.GetZ()); } glEnd(); //TODO: Need track text color qglColor(c3); renderText(lasthit.GetX(), lasthit.GetY(), lasthit.GetZ(), track.Parse(fScene->GetTrackTitle())); } } void VTracking3DView::resizeGL( int width, int height) { // int side = qMin(width, height); // glViewport((width - side) / 2, (height - side) / 2, side, side); double x1; double x2; double y1; double y2; fWidthDivHeight=width; fWidthDivHeight/=height; if (fWidthDivHeight<1.0) { x1=fOrthoLeft; x2=fOrthoRight; y1=fOrthoBottom/fWidthDivHeight; y2=fOrthoTop/fWidthDivHeight; } else { y1=fOrthoBottom; y2=fOrthoTop; x1=fOrthoLeft*fWidthDivHeight; x2=fOrthoRight*fWidthDivHeight; } glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); #ifdef QT_OPENGL_ES_1 glOrthof(x1, x2, y1, y2, fOrthoNearVal, fOrthoFarVal); #else glOrtho(x1, x2, y1, y2, fOrthoNearVal, fOrthoFarVal); #endif glMatrixMode(GL_MODELVIEW); } void VTracking3DView::mousePressEvent(QMouseEvent *event) { fLastPos = event->pos(); } void VTracking3DView::mouseMoveEvent(QMouseEvent *event) { int dx = event->x() - fLastPos.x(); int dy = event->y() - fLastPos.y(); GLint viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport); GLdouble modelview[16]; glGetDoublev(GL_MODELVIEW_MATRIX, modelview); GLdouble projection[16]; glGetDoublev(GL_PROJECTION_MATRIX, projection); GLdouble winx; GLdouble winy; GLdouble winz; unsigned int i; double x=0.0; double mx=0.0; double y=0.0; double my=0.0; double z=0.0; double mz=0.0; double ex=1111; double me=1e111; double ey; int trackNum=-1111; //TODO A dirty hack! QStatusBar* bar=fMWind->statusBar(); QString tmp; //Calculate tooltip text for (i=0;iGetHitsSize();i++) { const VMyHit& hit = fScene->GetHit(i); ex=event->x(); ey=event->y(); x=hit.GetX(); y=hit.GetY(); z=hit.GetZ(); gluProject(x, y, z, modelview, projection, viewport, &winx, &winy, &winz); winy=height()-winy; ex-=winx; ey-=winy; ex=sqrt(ex*ex+ey*ey); if (me>ex) { me=ex; mx=x; my=y; mz=z; if (me<5) trackNum=hit.GetTrack(); } } for (i=0;iGetPointsSize();i++) { const VMyHit& hit = fScene->GetPoint(i); ex=event->x(); ey=event->y(); x=hit.GetX(); y=hit.GetY(); z=hit.GetZ(); gluProject(x, y, z, modelview, projection, viewport, &winx, &winy, &winz); winy=height()-winy; ex-=winx; ey-=winy; ex=sqrt(ex*ex+ey*ey); if (me>ex) {me=ex; mx=x; my=y; mz=z;} } if (me<5) { tmp=QString("(%1, %2, %3)").arg(mx).arg(my).arg(mz); setToolTip(tmp); if (trackNum!=-1111) { tmp=fScene->GetTrack(trackNum).Parse(fScene->GetTrackCaption()); bar->showMessage(tmp); setStatusTip(tmp); } else { bar->showMessage(""); setStatusTip(""); } } else { setToolTip(""); bar->showMessage(""); setStatusTip(""); } if (event->buttons() & Qt::LeftButton) { setXRotation(fXRot + 8 * dy); setYRotation(fYRot + 8 * dx); } else if (event->buttons() & Qt::RightButton) { setXRotation(fXRot + 8 * dy); setZRotation(fZRot + 8 * dx); } else if (event->buttons()&Qt::MidButton) { fDX+=dx/1.5; fDY-=dy/1.5; updateGL(); } fLastPos = event->pos(); } void VTracking3DView::wheelEvent(QWheelEvent *event) { double zoom = (event->delta() > 0)? 10. : -10.; fOrthoLeft += zoom; fOrthoRight -= zoom; fOrthoBottom += zoom; fOrthoTop -= zoom; double x1; double x2; double y1; double y2; if (fWidthDivHeight<1.0) { x1=fOrthoLeft; x2=fOrthoRight; y1=fOrthoBottom/fWidthDivHeight; y2=fOrthoTop/fWidthDivHeight; } else { y1=fOrthoBottom; y2=fOrthoTop; x1=fOrthoLeft*fWidthDivHeight; x2=fOrthoRight*fWidthDivHeight; } glMatrixMode(GL_PROJECTION); glLoadIdentity(); #ifdef QT_OPENGL_ES_1 glOrthof(x1, x2, y1, y2, fOrthoNearVal, fOrthoFarVal); #else glOrtho(x1, x2, y1, y2, fOrthoNearVal, fOrthoFarVal); #endif glMatrixMode(GL_MODELVIEW); updateGL(); }