types_six_dof_expmap.cpp
Go to the documentation of this file.
00001 // g2o - General Graph Optimization
00002 // Copyright (C) 2011 H. Strasdat
00003 //
00004 // g2o is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU Lesser General Public License as published
00006 // by the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // g2o is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU Lesser General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU Lesser General Public License
00015 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #include "types_six_dof_expmap.h"
00018 
00019 #include "g2o/core/factory.h"
00020 
00021 namespace g2o {
00022 
00023   void __attribute__ ((constructor)) init_six_dof_types(void)
00024   {
00025     //cerr << "Calling " << __FILE__ << " " << __PRETTY_FUNCTION__ << endl;
00026     Factory* factory = Factory::instance();
00027     factory->registerType("VERTEX_SE3:EXPMAP", new HyperGraphElementCreator<VertexSE3Expmap>);
00028 
00029     factory->registerType("EDGE_SE3:EXPMAP", new HyperGraphElementCreator<EdgeSE3Expmap>);
00030     factory->registerType("EDGE_PROJECT_XYZ2UV:EXPMAP", new HyperGraphElementCreator<EdgeProjectXYZ2UV>);
00031     factory->registerType("EDGE_PROJECT_XYZ2UVQ:EXPMAP", new HyperGraphElementCreator<EdgeProjectXYZ2UVQ>);
00032     factory->registerType("EDGE_PROJECT_XYZ2UVU:EXPMAP", new HyperGraphElementCreator<EdgeProjectXYZ2UVU>);
00033   }
00034 
00035   VertexSE3Expmap::VertexSE3Expmap() : BaseVertex<6, SE3Quat>()
00036   {
00037     _principle_point[0] = 0;
00038     _principle_point[1] = 0;
00039     _focal_length[0] = 1;
00040     _focal_length[1] = 1;
00041     _baseline = 0.5;
00042   }
00043 
00044   bool VertexSE3Expmap::read(std::istream& is)
00045   {
00046     SE3Quat cam2world;
00047     for (int i=0; i<7; i++)
00048       is  >> cam2world[i];
00049     estimate() = cam2world.inverse();
00050     for (int i=0; i<2; i++)
00051       is >> _focal_length[i];
00052     for (int i=0; i<2; i++)
00053       is >> _principle_point[i];
00054     is >> _baseline;
00055     return true;
00056   }
00057 
00058   bool VertexSE3Expmap::write(std::ostream& os) const
00059   {
00060     SE3Quat cam2world(estimate().inverse());
00061     for (int i=0; i<7; i++)
00062       os << cam2world[i] << " ";
00063     for (int i=0; i<2; i++)
00064       os << _focal_length[i] << " ";
00065     for (int i=0; i<2; i++)
00066       os << _principle_point[i] << " ";
00067     os << _baseline << " ";
00068     return os.good();
00069   }
00070 
00071   EdgeSE3Expmap::EdgeSE3Expmap() :
00072     BaseBinaryEdge<6, SE3Quat, VertexSE3Expmap, VertexSE3Expmap>()
00073   {
00074   }
00075 
00076   bool EdgeSE3Expmap::read(std::istream& is)
00077   {
00078     SE3Quat cam2world;
00079     for (int i=0; i<7; i++)
00080       is >> cam2world[i];
00081     measurement() = cam2world.inverse();
00082     inverseMeasurement() = cam2world;
00083     //TODO: Convert information matrix!!
00084     for (int i=0; i<6; i++)
00085       for (int j=i; j<6; j++) {
00086         is >> information()(i,j);
00087         if (i!=j)
00088           information()(j,i)=information()(i,j);
00089       }
00090     return true;
00091   }
00092 
00093   bool EdgeSE3Expmap::write(std::ostream& os) const
00094   {
00095     SE3Quat cam2world(measurement().inverse());
00096     for (int i=0; i<7; i++)
00097       os << cam2world[i] << " ";
00098     for (int i=0; i<6; i++)
00099       for (int j=i; j<6; j++){
00100         os << " " <<  information()(i,j);
00101       }
00102     return os.good();
00103   }
00104 
00105   EdgeProjectXYZ2UV::EdgeProjectXYZ2UV() :
00106   BaseBinaryEdge<2, Vector2d, VertexPointXYZ, VertexSE3Expmap>()
00107   {
00108   }
00109 
00110   EdgeProjectXYZ2UVQ::EdgeProjectXYZ2UVQ() :
00111   BaseBinaryEdge<3, Vector3d, VertexPointXYZ, VertexSE3Expmap>()
00112   {
00113   }
00114 
00115   EdgeProjectXYZ2UVU::EdgeProjectXYZ2UVU() :
00116   BaseBinaryEdge<3, Vector3d, VertexPointXYZ, VertexSE3Expmap>()
00117   {
00118   }
00119 
00120   bool EdgeProjectXYZ2UV::read(std::istream& is)
00121   {
00122     for (int i=0; i<2; i++){
00123       is >> measurement()[i];
00124     }
00125 
00126     inverseMeasurement()[0] = -measurement()[0];
00127     inverseMeasurement()[1] = -measurement()[1];
00128     for (int i=0; i<2; i++)
00129       for (int j=i; j<2; j++) {
00130         is >> information()(i,j);
00131       if (i!=j)
00132         information()(j,i)=information()(i,j);
00133     }
00134     return true;
00135   }
00136 
00137   bool EdgeProjectXYZ2UV::write(std::ostream& os) const
00138   {
00139     for (int i=0; i<2; i++){
00140       os << measurement()[i] << " ";
00141     }
00142 
00143     for (int i=0; i<2; i++)
00144       for (int j=i; j<2; j++){
00145         os << " " <<  information()(i,j);
00146       }
00147     return os.good();
00148   }
00149 
00150   void EdgeSE3Expmap::linearizeOplus()
00151   {
00152     VertexSE3Expmap * vi = static_cast<VertexSE3Expmap *>(_vertices[0]);
00153     SE3Quat Ti(vi->estimate());
00154 
00155     VertexSE3Expmap * vj = static_cast<VertexSE3Expmap *>(_vertices[1]);
00156     SE3Quat Tj(vj->estimate());
00157 
00158     const SE3Quat & Tij = _measurement;
00159     SE3Quat invTij = Tij.inverse();
00160 
00161     SE3Quat invTj_Tij = Tj.inverse()*Tij;
00162     SE3Quat infTi_invTij = Ti.inverse()*invTij;
00163 
00164     _jacobianOplusXi = invTj_Tij.adj();
00165     _jacobianOplusXj = -infTi_invTij.adj();
00166   }
00167 
00168   void EdgeProjectXYZ2UV::linearizeOplus()
00169   {
00170 
00171     VertexSE3Expmap * vj = static_cast<VertexSE3Expmap *>(_vertices[1]);
00172     SE3Quat T(vj->estimate());
00173 
00174     VertexPointXYZ* vi = static_cast<VertexPointXYZ*>(_vertices[0]);
00175     Vector3d xyz = vi->estimate();
00176     Vector3d xyz_trans = T.map(xyz);
00177 
00178     double x = xyz_trans[0];
00179     double y = xyz_trans[1];
00180     double z = xyz_trans[2];
00181     double z_2 = z*z;
00182 
00183     Matrix<double,2,3> tmp;
00184     tmp(0,0) = vj->_focal_length(0);
00185     tmp(0,1) = 0;
00186     tmp(0,2) = -x/z*vj->_focal_length(0);
00187 
00188     tmp(1,0) = 0;
00189     tmp(1,1) = vj->_focal_length(1);
00190     tmp(1,2) = -y/z*vj->_focal_length(1);
00191 
00192     _jacobianOplusXi =  -1./z * tmp * T.rotation().toRotationMatrix();
00193 
00194     _jacobianOplusXj(0,0) =  x*y/z_2 *vj->_focal_length(0);
00195     _jacobianOplusXj(0,1) = -(1+(x*x/z_2)) *vj->_focal_length(0);
00196     _jacobianOplusXj(0,2) = y/z *vj->_focal_length(0);
00197     _jacobianOplusXj(0,3) = -1./z *vj->_focal_length(0);
00198     _jacobianOplusXj(0,4) = 0;
00199     _jacobianOplusXj(0,5) = x/z_2 *vj->_focal_length(0);
00200 
00201 
00202     _jacobianOplusXj(1,0) = (1+y*y/z_2) *vj->_focal_length(1);
00203     _jacobianOplusXj(1,1) = -x*y/z_2 *vj->_focal_length(1);
00204     _jacobianOplusXj(1,2) = -x/z *vj->_focal_length(1);
00205     _jacobianOplusXj(1,3) = 0;
00206     _jacobianOplusXj(1,4) = -1./z *vj->_focal_length(1);
00207     _jacobianOplusXj(1,5) = y/z_2 *vj->_focal_length(1);
00208   }
00209 
00210 
00211   void EdgeProjectXYZ2UVQ::linearizeOplus()
00212   {
00213 
00214     VertexSE3Expmap * vj = static_cast<VertexSE3Expmap *>(_vertices[1]);
00215     SE3Quat T(vj->estimate());
00216 
00217     VertexPointXYZ* vi = static_cast<VertexPointXYZ*>(_vertices[0]);
00218     Vector3d xyz = vi->estimate();
00219     Vector3d xyz_trans = T.map(xyz);
00220 
00221     const double & x = xyz_trans[0];
00222     const double & y = xyz_trans[1];
00223     const double & z = xyz_trans[2];
00224     double z_sq = z*z;
00225     const double & Fx = vj->_focal_length(0);
00226     const double & Fy = vj->_focal_length(1);
00227     double dq_dz = -Fx/z_sq;
00228     double x_Fx_by_zsq = x*Fx/z_sq;
00229     double y_Fy_by_zsq = y*Fy/z_sq;
00230 
00231 
00232      Matrix3d R = T.rotation().toRotationMatrix();
00233     _jacobianOplusXi.row(0) = -Fx/z*R.row(0) + x_Fx_by_zsq*R.row(2);
00234     _jacobianOplusXi.row(1) = -Fy/z*R.row(1) + y_Fy_by_zsq*R.row(2);
00235     _jacobianOplusXi.row(2) =  -dq_dz*R.row(2);
00236 
00237 
00238     _jacobianOplusXj(0,0) =  x*y/z_sq *Fx;
00239     _jacobianOplusXj(0,1) = -(1+(x*x/z_sq)) *Fx;
00240     _jacobianOplusXj(0,2) = y/z *Fx;
00241     _jacobianOplusXj(0,3) = -1./z *Fx;
00242     _jacobianOplusXj(0,4) = 0;
00243     _jacobianOplusXj(0,5) = x/z_sq *Fx;
00244 
00245 
00246     _jacobianOplusXj(1,0) = (1+y*y/z_sq) *Fy;
00247     _jacobianOplusXj(1,1) = -x*y/z_sq *Fy;
00248     _jacobianOplusXj(1,2) = -x/z *Fy;
00249     _jacobianOplusXj(1,3) = 0;
00250     _jacobianOplusXj(1,4) = -1./z *Fy;
00251     _jacobianOplusXj(1,5) = y/z_sq *Fy;
00252 
00253     _jacobianOplusXj(2,0) = -y*dq_dz ;
00254     _jacobianOplusXj(2,1) = x*dq_dz;
00255     _jacobianOplusXj(2,2) = 0;
00256     _jacobianOplusXj(2,3) = 0;
00257     _jacobianOplusXj(2,4) = 0;
00258     _jacobianOplusXj(2,5) = -dq_dz ;
00259 
00260 //    std::cerr << _jacobianOplusXi << std::endl;
00261 //    std::cerr << _jacobianOplusXj << std::endl;
00262 
00263 //    BaseBinaryEdge<3, Vector3d, VertexPointXYZ, VertexSE3Expmap, false>::linearizeOplus();
00264 //    std::cerr << _jacobianOplusXi << std::endl;
00265 //    std::cerr << _jacobianOplusXj << std::endl;
00266   }
00267 
00268   bool EdgeProjectXYZ2UVQ::read(std::istream& is)
00269   {
00270     for (int i=0; i<3; i++){
00271       is  >> measurement()[i];
00272     }
00273 
00274     inverseMeasurement() = -measurement();
00275     for (int i=0; i<3; i++)
00276       for (int j=i; j<3; j++) {
00277         is >> information()(i,j);
00278       if (i!=j)
00279         information()(j,i)=information()(i,j);
00280     }
00281     return true;
00282   }
00283 
00284   bool EdgeProjectXYZ2UVQ::write(std::ostream& os) const
00285   {
00286     for (int i=0; i<3; i++){
00287       os  << measurement()[i] << " ";
00288     }
00289 
00290     for (int i=0; i<3; i++)
00291       for (int j=i; j<3; j++){
00292         os << " " <<  information()(i,j);
00293     }
00294     return os.good();
00295   }
00296 
00297 
00298   bool EdgeProjectXYZ2UVU::read(std::istream& is)
00299   {
00300     for (int i=0; i<3; i++){
00301       is  >> measurement()[i];
00302     }
00303 
00304     inverseMeasurement() = -measurement();
00305     for (int i=0; i<3; i++)
00306       for (int j=i; j<3; j++) {
00307         is >> information()(i,j);
00308       if (i!=j)
00309         information()(j,i)=information()(i,j);
00310     }
00311     return true;
00312   }
00313 
00314   bool EdgeProjectXYZ2UVU::write(std::ostream& os) const
00315   {
00316     for (int i=0; i<3; i++){
00317       os  << measurement()[i] << " ";
00318     }
00319 
00320     for (int i=0; i<3; i++)
00321       for (int j=i; j<3; j++){
00322         os << " " << information()(i,j);
00323       }
00324     return os.good();
00325   }
00326 
00327 } // end namespace


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:33:29