fat_man.cpp
Go to the documentation of this file.
00001 
00004 /*****************************************************************************
00005 ** Includes
00006 *****************************************************************************/
00007 
00008 #include <qglv/opengl.hpp>
00009 #include "../../include/qglv/objects/fat_man.hpp"
00010 
00011 /*****************************************************************************
00012 ** Namespaces
00013 *****************************************************************************/
00014 
00015 namespace qglv {
00016 
00017 /*****************************************************************************
00018 ** C&D
00019 *****************************************************************************/
00020 
00021 FatMan::FatMan(const Sophus::SE3f& pose)
00022 : updated(false)
00023 , moving(false)
00024 , gl_id_start(-1)
00025 , pose(pose)
00026 {
00027 }
00028 
00029 FatMan::~FatMan() {
00030   if ( gl_id_start > 0 ) {
00031     glDeleteLists(gl_id_start, 3);
00032   }
00033 }
00034 
00035 /*****************************************************************************
00036 ** Mouse
00037 *****************************************************************************/
00038 
00039 void FatMan::checkIfGrabsMouse(int x, int y, const qglviewer::Camera* const camera)
00040 {
00041   // might be faster doing this comparison in screen coordinates, but less accurate
00042   Eigen::Vector3f z_plane_position = _pointOnZPlane(QPoint(x,y), camera);
00043   float distance = (z_plane_position - pose.translation()).norm();
00044   setGrabsMouse( moving || distance < 0.5 );
00045 }
00046 
00047 void FatMan::mousePressEvent( QMouseEvent* const event, qglviewer::Camera* const camera) {
00048   moving = true;
00049 }
00050 
00051 void FatMan::mouseMoveEvent(QMouseEvent* const event, qglviewer::Camera* const camera)
00052 {
00053   if (moving)
00054   {
00055     // qglviewer should be doing draw() and mouse event handlers in the one thread
00056     // so no chance of conflict -> don't worry about guarding with mutex
00057     pose = Sophus::SE3f(Eigen::Matrix3f::Identity(), _pointOnZPlane(event->pos(), camera));
00058   }
00059 }
00060 void FatMan::mouseReleaseEvent(QMouseEvent* const event, qglviewer::Camera* const camera) {
00061   moving = false;
00062   // qglviewer should be doing draw() and mouse event handlers in the one thread
00063   // so no chance of conflict -> don't worry about guarding with mutex
00064   pose = Sophus::SE3f(Eigen::Matrix3f::Identity(), _pointOnZPlane(event->pos(), camera));
00065   emit moved(pose.translation().x(), pose.translation().y());
00066 }
00067 
00068 void FatMan::wheelEvent(QWheelEvent* const event, qglviewer::Camera* const camera) {
00069   Q_UNUSED(event);
00070   Q_UNUSED(camera);
00071   // don't know of any way to redirect this event back to the main qgl viewer window.
00072 }
00073 
00074 Eigen::Vector3f FatMan::_pointOnZPlane(const QPoint& point, const qglviewer::Camera* camera) const {
00075   qglviewer::Vec origin, direction, point_under_pixel;
00076   camera->convertClickToLine(point, origin, direction);
00077   bool found;
00078   point_under_pixel = camera->pointUnderPixel(point, found);
00079   float alpha = - origin.z / direction.z;
00080   Eigen::Vector3f point_on_z_plane;
00081   point_on_z_plane << origin.x + alpha*direction.x,
00082                       origin.y + alpha*direction.y,
00083                       0.05;
00084   return point_on_z_plane;
00085 }
00086 
00087 /*****************************************************************************
00088 ** Drawing
00089 *****************************************************************************/
00090 
00091 void FatMan::draw()
00092 {
00093   if (gl_id_start == -1) {
00094     _glGenLists();
00095   }
00096   glPushMatrix();
00097   qglv::moveTo(pose.inverse());
00098   if ( moving ) {
00099     qglv::colour(DimGray);
00100   } else {
00101     qglv::colour(GoldenBrown);
00102   }
00103   glCallList(gl_id_start);
00104   glPopMatrix();
00105 }
00106 
00107 void FatMan::_glGenLists() {
00108   gl_id_start = ::glGenLists(1);
00109 
00110   glNewList( gl_id_start, GL_COMPILE );
00111   glBegin(GL_TRIANGLE_FAN);
00112   glVertex2f( 0.5,  0.0);
00113   glVertex2f( 0.353,  0.353);
00114   glVertex2f( 0.0,  0.5);
00115   glVertex2f( -0.353,  0.353);
00116   glVertex2f(-0.5,  0.0);
00117   glVertex2f( -0.353,  -0.353);
00118   glVertex2f( 0.0, -0.5);
00119   glVertex2f( 0.353,  -0.353);
00120   glEnd();
00121   qglv::colour(White);
00122   qglv::text("Fat Man", Eigen::Vector3f(-0.25,0.005,0.2));
00123   glEndList();
00124 }
00125 
00126 
00127 /*****************************************************************************
00128  ** Trailers
00129  *****************************************************************************/
00130 
00131 } // namespace qglv


qglv_extras
Author(s): Daniel Stonier
autogenerated on Sat Jun 18 2016 08:19:30