keyframes.cpp
Go to the documentation of this file.
00001 
00004 /*****************************************************************************
00005 ** Includes
00006 *****************************************************************************/
00007 
00008 #include <qglv/opengl.hpp>
00009 #include "../../include/qglv/gl/colours.hpp"
00010 #include "../../include/qglv/gl/helpers.hpp"
00011 #include "../../include/qglv/objects/keyframes.hpp"
00012 
00013 /*****************************************************************************
00014 ** Namespaces
00015 *****************************************************************************/
00016 
00017 namespace qglv {
00018 
00019 /*****************************************************************************
00020  ** Static Variables
00021  *****************************************************************************/
00022 
00023 const float KeyFrame::default_keyframe_size = 0.3;
00024 
00025 /*****************************************************************************
00026  ** Construct without cloud
00027  *****************************************************************************/
00028 
00029 KeyFrame::KeyFrame(const int id,
00030                    const Sophus::SE3f& T_frame_rel_map,
00031                    const AxisColourScheme& axis_colour_scheme,
00032                    const float& axis_scale_factor
00033                    )
00034 : id(id)
00035 , T_frame_rel_map(T_frame_rel_map)
00036 , updated(true)
00037 , gl_id_start(-1)
00038 , pinned(false)
00039 , axis_colour_scheme(axis_colour_scheme)
00040 , axis_scale_factor(axis_scale_factor)
00041 {
00042 }
00043 
00044 /*****************************************************************************
00045 ** Construct from a Tracking Frame
00046 *****************************************************************************/
00047 
00048 KeyFrame::KeyFrame( const Sophus::SE3f& T,
00049                     const std::vector<float> & nics,
00050                     const std::vector<float> & idepths, 
00051                     const std::vector<float>& seed_variances,
00052                     const std::vector<unsigned char>& seed_intensities_and_grad )
00053 : id(-1)
00054 , T_frame_rel_map(T)
00055 , updated(true)
00056 , gl_id_start(-1)
00057 , pinned(false)
00058 , axis_colour_scheme(AxisColourGolden)
00059 , axis_scale_factor(1.25)
00060 {
00061   // not very optimal...better to reserve and then push back
00062   vertices.resize( idepths.size() * 3 );
00063   variance_lines.resize( idepths.size() * 3 * 2 );
00064 
00065   // precompute some data for the point clouds
00066   float * ptr_vertices = vertices.data();
00067   float * ptr_var = variance_lines.data();
00068   Eigen::Vector3f lm0, lmp, lmn;
00069 
00070   for( int j(0); j < idepths.size(); j++ )
00071   {
00072     float r, g, b;
00073     normalisedValueToRGB(static_cast<float>(seed_intensities_and_grad[j*3])/255.0f, b, g, r); // make sure to convert away from char
00074 
00075     intensities.push_back(255*r);
00076     intensities.push_back(255*g);
00077     intensities.push_back(255*b);
00078     
00079     lm0 = Eigen::Vector3f( nics[j*2], nics[j*2+1], 1.0f ) / idepths[j];
00080 
00081     *ptr_vertices++ = lm0.x();
00082     *ptr_vertices++ = lm0.y();
00083     *ptr_vertices++ = lm0.z();
00084 
00085     float depth = lm0.z();
00086     lm0 /= depth;
00087 
00088     // draw the variance as line
00089     float depth_n = depth + seed_variances[j];
00090     float depth_p = depth - seed_variances[j];
00091     if( depth_p < 1e-3f ) depth_p = 1e-3f;
00092     lmp = lm0 * depth_p;
00093     lmn = lm0 * depth_n;
00094 
00095     *ptr_var++ = lmp.x();
00096     *ptr_var++ = lmp.y();
00097     *ptr_var++ = lmp.z();
00098 
00099     *ptr_var++ = lmn.x();
00100     *ptr_var++ = lmn.y();
00101     *ptr_var++ = lmn.z();
00102   }
00103 }
00104 
00105 /*****************************************************************************
00106  ** Construct from a Mapped Keyframe
00107  *****************************************************************************/
00108 
00109 KeyFrame::KeyFrame( const int id,
00110                     const Sophus::SE3f& T,
00111                     const bool& pinned,
00112                     const std::vector<float> & nics,
00113                     const std::vector<float> & idepths, 
00114                     const std::vector<float>& seed_variances,
00115                     const std::vector<unsigned char>& seed_intensities_and_grad,
00116                     const float& focal_length_times_baseline )
00117 : id(id)
00118 , T_frame_rel_map(T)
00119 , pinned(pinned)
00120 , updated(true)
00121 , gl_id_start(-1)
00122 {
00123   axis_colour_scheme = pinned ? AxisColourGolden : AxisColourRGB;
00124   axis_scale_factor = pinned ? 1.25 : 1.0;
00125 
00126   // not very optimal...better to reserve and then push back
00127   vertices.reserve( idepths.size() * 3 );
00128   variance_lines.reserve( idepths.size() * 3 * 2 );
00129 
00130   // precompute some data for the point clouds
00131   float * ptr_var = variance_lines.data();
00132   Eigen::Vector3f lm0, lmp, lmn;
00133   const float depth_variance_threshold = 0.04f;
00134 //  const float depth_variance_threshold = 0.02f;
00135   const float sq_depth_variance_threshold = depth_variance_threshold *depth_variance_threshold;
00136 
00137   for( int j(0); j < idepths.size(); j++ )
00138   {
00139     float inverse_depth_var = std::sqrt( seed_variances[j] );
00140     float inverse_depth = idepths[j];
00141     float inverse_depth_f = (inverse_depth - inverse_depth_var); 
00142     if( inverse_depth_f < 1e-3f ) inverse_depth_f = 1e-3f;
00143     float inverse_depth_n = (inverse_depth + inverse_depth_var);
00144     float depth_var = 1.0f / inverse_depth_f - 1.0f / inverse_depth_n;
00145     
00146     depth_var *= depth_var;
00147 
00148     if( depth_var < sq_depth_variance_threshold) 
00149     {
00150       intensities.push_back( seed_intensities_and_grad[j*3] );
00151       intensities.push_back( seed_intensities_and_grad[j*3] );
00152       intensities.push_back( seed_intensities_and_grad[j*3] );
00153       
00154       lm0 = Eigen::Vector3f( nics[j*2], nics[j*2+1], 1.0f ) / inverse_depth;
00155 
00156       vertices.push_back(lm0.x());
00157       vertices.push_back(lm0.y());
00158       vertices.push_back(lm0.z());
00159 
00160       float depth = lm0.z();
00161       lm0 /= depth;
00162 
00163       // draw the variance as line
00164       float depth_n = depth + 0.0;
00165       float depth_p = depth - 0.0;
00166       if( depth_p < 1e-3f ) depth_p = 1e-3f;
00167       lmp = lm0 * depth_p;
00168       lmn = lm0 * depth_n;
00169 
00170       variance_lines.push_back(lmp.x());
00171       variance_lines.push_back(lmp.y());
00172       variance_lines.push_back(lmp.z());
00173 
00174       variance_lines.push_back(lmn.x());
00175       variance_lines.push_back(lmn.y());
00176       variance_lines.push_back(lmn.z());
00177     }
00178   }
00179 }
00180 
00181 
00182 KeyFrame::KeyFrame( const int id,
00183                     const Sophus::SE3f& T,
00184                     const bool& pinned,
00185                     const std::vector< Eigen::Vector3f > & seeds, 
00186                     const std::vector<float>& seed_variances,
00187                     const std::vector<unsigned char>& seed_intensities,
00188                     const float& focal_length_times_baseline )
00189 : id(id)
00190 , T_frame_rel_map(T)
00191 , pinned(pinned)
00192 , updated(true)
00193 , gl_id_start(-1)
00194 {
00195   axis_colour_scheme = pinned ? AxisColourGolden : AxisColourRGB;
00196   axis_scale_factor = pinned ? 1.25 : 1.0;
00197 
00198   // not very optimal...better to reserve and then push back
00199   vertices.reserve( seeds.size() * 3 );
00200   variance_lines.reserve( seeds.size() * 3 * 2 );
00201 
00202   // precompute some data for the point clouds
00203   float * ptr_var = variance_lines.data();
00204   Eigen::Vector3f lm0, lmp, lmn;
00205 //  const float depth_variance_threshold = 0.04f;
00206   const float depth_variance_threshold = 0.02f;
00207   const float sq_depth_variance_threshold = depth_variance_threshold *depth_variance_threshold;
00208 
00209   for( int j(0); j < seeds.size(); j++ )
00210   {
00211     float inverse_depth_var = std::sqrt( seed_variances[j] );
00212     float inverse_depth = 1.0f / seeds[j].z();
00213     float inverse_depth_f = (inverse_depth - inverse_depth_var); 
00214     if( inverse_depth_f < 1e-3f ) inverse_depth_f = 1e-3f;
00215     float inverse_depth_n = (inverse_depth + inverse_depth_var);
00216     float depth_var = 1.0f / inverse_depth_f - 1.0f / inverse_depth_n;
00217     
00218     depth_var *= depth_var;
00219 
00220     if( depth_var < sq_depth_variance_threshold) 
00221     {
00222       intensities.push_back( seed_intensities[j*3] );
00223       intensities.push_back( seed_intensities[j*3] );
00224       intensities.push_back( seed_intensities[j*3] );
00225       
00226       lm0 = seeds[j];
00227 
00228       vertices.push_back(lm0.x());
00229       vertices.push_back(lm0.y());
00230       vertices.push_back(lm0.z());
00231 
00232       float depth = lm0.z();
00233       lm0 /= depth;
00234 
00235       // draw the variance as line
00236       float depth_n = depth + 0.0;
00237       float depth_p = depth - 0.0;
00238       if( depth_p < 1e-3f ) depth_p = 1e-3f;
00239       lmp = lm0 * depth_p;
00240       lmn = lm0 * depth_n;
00241 
00242       variance_lines.push_back(lmp.x());
00243       variance_lines.push_back(lmp.y());
00244       variance_lines.push_back(lmp.z());
00245 
00246       variance_lines.push_back(lmn.x());
00247       variance_lines.push_back(lmn.y());
00248       variance_lines.push_back(lmn.z());
00249     }
00250   }
00251 }
00252 KeyFrame::~KeyFrame() {
00253   if ( gl_id_start > 0 ) {
00254     glDeleteLists(gl_id_start, 3);
00255   }
00256 }
00257 
00258 void KeyFrame::draw(
00259     bool draw_frames,
00260     bool draw_seeds,
00261     bool draw_covariances
00262     )
00263 {
00264   if (gl_id_start == -1) {
00265     _glGenLists();
00266   }
00267   GLfloat model[16];
00268   glPushMatrix();
00269   qglv::moveTo(T_frame_rel_map);
00270   if ( draw_frames ) { glCallList(gl_id_start); }
00271   if ( draw_seeds )  { glCallList(gl_id_start + 1); }
00272   if ( draw_covariances ) { glCallList(gl_id_start + 2); }
00273   glPopMatrix();
00274 }
00275 
00276 void KeyFrame::_glGenLists() {
00277   gl_id_start = ::glGenLists(3);
00278 
00279   glNewList( gl_id_start, GL_COMPILE );
00280   // pose
00281   // note the identity SE3f() here, we're just drawing in place
00282   pose(Sophus::SE3f(),
00283                  axis_scale_factor*default_keyframe_size,
00284                  axis_scale_factor*2.0,
00285                  axis_colour_scheme
00286                  );
00287   if ( !name.empty() ) {
00288     // TODO : figure out how to render the text locally relative to the frame
00289     // refer to renderText and QGLViewer.drawText
00290   }
00291   glEndList();
00292 
00293   glNewList( gl_id_start + 1, GL_COMPILE );
00294   if ( vertices.size() > 0 ) {
00295     // point cloud
00296     glPointSize(1);
00297     glEnableClientState(GL_VERTEX_ARRAY);
00298     glEnableClientState(GL_COLOR_ARRAY);
00299     glVertexPointer(3, GL_FLOAT, 0, vertices.data() );
00300     glColorPointer(3, GL_UNSIGNED_BYTE, 0, intensities.data() );
00301     glDrawArrays(GL_POINTS, 0, vertices.size() / 3 );
00302     glDisableClientState(GL_VERTEX_ARRAY);
00303     glDisableClientState(GL_COLOR_ARRAY);
00304   }
00305   glEndList();
00306   glNewList( gl_id_start + 2, GL_COMPILE );
00307   if ( variance_lines.size() > 0 ) {
00308     glColor4f(0.0f, 0.5f, 0.0f, 0.5f);
00309     glEnableClientState(GL_VERTEX_ARRAY);
00310     glVertexPointer(3, GL_FLOAT, 0, variance_lines.data() );
00311     glDrawArrays(GL_LINES, 0, variance_lines.size() / 3 );
00312     glDisableClientState(GL_VERTEX_ARRAY);
00313   }
00314   glEndList();
00315 }
00316 
00317 } // namespace qglv


qglv_opengl
Author(s): Daniel Stonier
autogenerated on Sat Jun 18 2016 08:19:28