G2oTypes.cc 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  1. /**
  2. * This file is part of ORB-SLAM3
  3. *
  4. * Copyright (C) 2017-2020 Carlos Campos, Richard Elvira, Juan J. Gómez Rodríguez, José M.M. Montiel and Juan D. Tardós, University of Zaragoza.
  5. * Copyright (C) 2014-2016 Raúl Mur-Artal, José M.M. Montiel and Juan D. Tardós, University of Zaragoza.
  6. *
  7. * ORB-SLAM3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
  8. * License as published by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * ORB-SLAM3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
  12. * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with ORB-SLAM3.
  16. * If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "G2oTypes.h"
  19. #include "ImuTypes.h"
  20. #include "Converter.h"
  21. namespace ORB_SLAM3
  22. {
  23. ImuCamPose::ImuCamPose(KeyFrame *pKF):its(0)
  24. {
  25. // Load IMU pose
  26. twb = Converter::toVector3d(pKF->GetImuPosition());
  27. Rwb = Converter::toMatrix3d(pKF->GetImuRotation());
  28. // Load camera poses
  29. int num_cams;
  30. if(pKF->mpCamera2)
  31. num_cams=2;
  32. else
  33. num_cams=1;
  34. tcw.resize(num_cams);
  35. Rcw.resize(num_cams);
  36. tcb.resize(num_cams);
  37. Rcb.resize(num_cams);
  38. Rbc.resize(num_cams);
  39. tbc.resize(num_cams);
  40. pCamera.resize(num_cams);
  41. // Left camera
  42. tcw[0] = Converter::toVector3d(pKF->GetTranslation());
  43. Rcw[0] = Converter::toMatrix3d(pKF->GetRotation());
  44. tcb[0] = Converter::toVector3d(pKF->mImuCalib.Tcb.rowRange(0,3).col(3));
  45. Rcb[0] = Converter::toMatrix3d(pKF->mImuCalib.Tcb.rowRange(0,3).colRange(0,3));
  46. Rbc[0] = Rcb[0].transpose();
  47. tbc[0] = Converter::toVector3d(pKF->mImuCalib.Tbc.rowRange(0,3).col(3));
  48. pCamera[0] = pKF->mpCamera;
  49. bf = pKF->mbf;
  50. if(num_cams>1)
  51. {
  52. Eigen::Matrix4d Trl = Converter::toMatrix4d(pKF->mTrl);
  53. Rcw[1] = Trl.block<3,3>(0,0)*Rcw[0];
  54. tcw[1] = Trl.block<3,3>(0,0)*tcw[0]+Trl.block<3,1>(0,3);
  55. tcb[1] = Trl.block<3,3>(0,0)*tcb[0]+Trl.block<3,1>(0,3);
  56. Rcb[1] = Trl.block<3,3>(0,0)*Rcb[0];
  57. Rbc[1] = Rcb[1].transpose();
  58. tbc[1] = -Rbc[1]*tcb[1];
  59. pCamera[1] = pKF->mpCamera2;
  60. }
  61. // For posegraph 4DoF
  62. Rwb0 = Rwb;
  63. DR.setIdentity();
  64. }
  65. ImuCamPose::ImuCamPose(Frame *pF):its(0)
  66. {
  67. // Load IMU pose
  68. twb = Converter::toVector3d(pF->GetImuPosition());
  69. Rwb = Converter::toMatrix3d(pF->GetImuRotation());
  70. // Load camera poses
  71. int num_cams;
  72. if(pF->mpCamera2)
  73. num_cams=2;
  74. else
  75. num_cams=1;
  76. tcw.resize(num_cams);
  77. Rcw.resize(num_cams);
  78. tcb.resize(num_cams);
  79. Rcb.resize(num_cams);
  80. Rbc.resize(num_cams);
  81. tbc.resize(num_cams);
  82. pCamera.resize(num_cams);
  83. // Left camera
  84. tcw[0] = Converter::toVector3d(pF->mTcw.rowRange(0,3).col(3));
  85. Rcw[0] = Converter::toMatrix3d(pF->mTcw.rowRange(0,3).colRange(0,3));
  86. tcb[0] = Converter::toVector3d(pF->mImuCalib.Tcb.rowRange(0,3).col(3));
  87. Rcb[0] = Converter::toMatrix3d(pF->mImuCalib.Tcb.rowRange(0,3).colRange(0,3));
  88. Rbc[0] = Rcb[0].transpose();
  89. tbc[0] = Converter::toVector3d(pF->mImuCalib.Tbc.rowRange(0,3).col(3));
  90. pCamera[0] = pF->mpCamera;
  91. bf = pF->mbf;
  92. if(num_cams>1)
  93. {
  94. Eigen::Matrix4d Trl = Converter::toMatrix4d(pF->mTrl);
  95. Rcw[1] = Trl.block<3,3>(0,0)*Rcw[0];
  96. tcw[1] = Trl.block<3,3>(0,0)*tcw[0]+Trl.block<3,1>(0,3);
  97. tcb[1] = Trl.block<3,3>(0,0)*tcb[0]+Trl.block<3,1>(0,3);
  98. Rcb[1] = Trl.block<3,3>(0,0)*Rcb[0];
  99. Rbc[1] = Rcb[1].transpose();
  100. tbc[1] = -Rbc[1]*tcb[1];
  101. pCamera[1] = pF->mpCamera2;
  102. }
  103. // For posegraph 4DoF
  104. Rwb0 = Rwb;
  105. DR.setIdentity();
  106. }
  107. ImuCamPose::ImuCamPose(Eigen::Matrix3d &_Rwc, Eigen::Vector3d &_twc, KeyFrame* pKF): its(0)
  108. {
  109. // This is only for posegrpah, we do not care about multicamera
  110. tcw.resize(1);
  111. Rcw.resize(1);
  112. tcb.resize(1);
  113. Rcb.resize(1);
  114. Rbc.resize(1);
  115. tbc.resize(1);
  116. pCamera.resize(1);
  117. tcb[0] = Converter::toVector3d(pKF->mImuCalib.Tcb.rowRange(0,3).col(3));
  118. Rcb[0] = Converter::toMatrix3d(pKF->mImuCalib.Tcb.rowRange(0,3).colRange(0,3));
  119. Rbc[0] = Rcb[0].transpose();
  120. tbc[0] = Converter::toVector3d(pKF->mImuCalib.Tbc.rowRange(0,3).col(3));
  121. twb = _Rwc*tcb[0]+_twc;
  122. Rwb = _Rwc*Rcb[0];
  123. Rcw[0] = _Rwc.transpose();
  124. tcw[0] = -Rcw[0]*_twc;
  125. pCamera[0] = pKF->mpCamera;
  126. bf = pKF->mbf;
  127. // For posegraph 4DoF
  128. Rwb0 = Rwb;
  129. DR.setIdentity();
  130. }
  131. void ImuCamPose::SetParam(const std::vector<Eigen::Matrix3d> &_Rcw, const std::vector<Eigen::Vector3d> &_tcw, const std::vector<Eigen::Matrix3d> &_Rbc,
  132. const std::vector<Eigen::Vector3d> &_tbc, const double &_bf)
  133. {
  134. Rbc = _Rbc;
  135. tbc = _tbc;
  136. Rcw = _Rcw;
  137. tcw = _tcw;
  138. const int num_cams = Rbc.size();
  139. Rcb.resize(num_cams);
  140. tcb.resize(num_cams);
  141. for(int i=0; i<tcb.size(); i++)
  142. {
  143. Rcb[i] = Rbc[i].transpose();
  144. tcb[i] = -Rcb[i]*tbc[i];
  145. }
  146. Rwb = Rcw[0].transpose()*Rcb[0];
  147. twb = Rcw[0].transpose()*(tcb[0]-tcw[0]);
  148. bf = _bf;
  149. }
  150. Eigen::Vector2d ImuCamPose::Project(const Eigen::Vector3d &Xw, int cam_idx) const
  151. {
  152. Eigen::Vector3d Xc = Rcw[cam_idx]*Xw+tcw[cam_idx];
  153. return pCamera[cam_idx]->project(Xc);
  154. }
  155. Eigen::Vector3d ImuCamPose::ProjectStereo(const Eigen::Vector3d &Xw, int cam_idx) const
  156. {
  157. Eigen::Vector3d Pc = Rcw[cam_idx]*Xw+tcw[cam_idx];
  158. Eigen::Vector3d pc;
  159. double invZ = 1/Pc(2);
  160. pc.head(2) = pCamera[cam_idx]->project(Pc);
  161. pc(2) = pc(0) - bf*invZ;
  162. return pc;
  163. }
  164. bool ImuCamPose::isDepthPositive(const Eigen::Vector3d &Xw, int cam_idx) const
  165. {
  166. return (Rcw[cam_idx].row(2)*Xw+tcw[cam_idx](2))>0.0;
  167. }
  168. void ImuCamPose::Update(const double *pu)
  169. {
  170. Eigen::Vector3d ur, ut;
  171. ur << pu[0], pu[1], pu[2];
  172. ut << pu[3], pu[4], pu[5];
  173. // Update body pose
  174. twb += Rwb*ut;
  175. Rwb = Rwb*ExpSO3(ur);
  176. // Normalize rotation after 5 updates
  177. its++;
  178. if(its>=3)
  179. {
  180. NormalizeRotation(Rwb);
  181. its=0;
  182. }
  183. // Update camera poses
  184. const Eigen::Matrix3d Rbw = Rwb.transpose();
  185. const Eigen::Vector3d tbw = -Rbw*twb;
  186. for(int i=0; i<pCamera.size(); i++)
  187. {
  188. Rcw[i] = Rcb[i]*Rbw;
  189. tcw[i] = Rcb[i]*tbw+tcb[i];
  190. }
  191. }
  192. void ImuCamPose::UpdateW(const double *pu)
  193. {
  194. Eigen::Vector3d ur, ut;
  195. ur << pu[0], pu[1], pu[2];
  196. ut << pu[3], pu[4], pu[5];
  197. const Eigen::Matrix3d dR = ExpSO3(ur);
  198. DR = dR*DR;
  199. Rwb = DR*Rwb0;
  200. // Update body pose
  201. twb += ut;
  202. // Normalize rotation after 5 updates
  203. its++;
  204. if(its>=5)
  205. {
  206. DR(0,2)=0.0;
  207. DR(1,2)=0.0;
  208. DR(2,0)=0.0;
  209. DR(2,1)=0.0;
  210. NormalizeRotation(DR);
  211. its=0;
  212. }
  213. // Update camera pose
  214. const Eigen::Matrix3d Rbw = Rwb.transpose();
  215. const Eigen::Vector3d tbw = -Rbw*twb;
  216. for(int i=0; i<pCamera.size(); i++)
  217. {
  218. Rcw[i] = Rcb[i]*Rbw;
  219. tcw[i] = Rcb[i]*tbw+tcb[i];
  220. }
  221. }
  222. InvDepthPoint::InvDepthPoint(double _rho, double _u, double _v, KeyFrame* pHostKF): u(_u), v(_v), rho(_rho),
  223. fx(pHostKF->fx), fy(pHostKF->fy), cx(pHostKF->cx), cy(pHostKF->cy), bf(pHostKF->mbf)
  224. {
  225. }
  226. void InvDepthPoint::Update(const double *pu)
  227. {
  228. rho += *pu;
  229. }
  230. bool VertexPose::read(std::istream& is)
  231. {
  232. std::vector<Eigen::Matrix<double,3,3> > Rcw;
  233. std::vector<Eigen::Matrix<double,3,1> > tcw;
  234. std::vector<Eigen::Matrix<double,3,3> > Rbc;
  235. std::vector<Eigen::Matrix<double,3,1> > tbc;
  236. const int num_cams = _estimate.Rbc.size();
  237. for(int idx = 0; idx<num_cams; idx++)
  238. {
  239. for (int i=0; i<3; i++){
  240. for (int j=0; j<3; j++)
  241. is >> Rcw[idx](i,j);
  242. }
  243. for (int i=0; i<3; i++){
  244. is >> tcw[idx](i);
  245. }
  246. for (int i=0; i<3; i++){
  247. for (int j=0; j<3; j++)
  248. is >> Rbc[idx](i,j);
  249. }
  250. for (int i=0; i<3; i++){
  251. is >> tbc[idx](i);
  252. }
  253. float nextParam;
  254. for(size_t i = 0; i < _estimate.pCamera[idx]->size(); i++){
  255. is >> nextParam;
  256. _estimate.pCamera[idx]->setParameter(nextParam,i);
  257. }
  258. }
  259. double bf;
  260. is >> bf;
  261. _estimate.SetParam(Rcw,tcw,Rbc,tbc,bf);
  262. updateCache();
  263. return true;
  264. }
  265. bool VertexPose::write(std::ostream& os) const
  266. {
  267. std::vector<Eigen::Matrix<double,3,3> > Rcw = _estimate.Rcw;
  268. std::vector<Eigen::Matrix<double,3,1> > tcw = _estimate.tcw;
  269. std::vector<Eigen::Matrix<double,3,3> > Rbc = _estimate.Rbc;
  270. std::vector<Eigen::Matrix<double,3,1> > tbc = _estimate.tbc;
  271. const int num_cams = tcw.size();
  272. for(int idx = 0; idx<num_cams; idx++)
  273. {
  274. for (int i=0; i<3; i++){
  275. for (int j=0; j<3; j++)
  276. os << Rcw[idx](i,j) << " ";
  277. }
  278. for (int i=0; i<3; i++){
  279. os << tcw[idx](i) << " ";
  280. }
  281. for (int i=0; i<3; i++){
  282. for (int j=0; j<3; j++)
  283. os << Rbc[idx](i,j) << " ";
  284. }
  285. for (int i=0; i<3; i++){
  286. os << tbc[idx](i) << " ";
  287. }
  288. for(size_t i = 0; i < _estimate.pCamera[idx]->size(); i++){
  289. os << _estimate.pCamera[idx]->getParameter(i) << " ";
  290. }
  291. }
  292. os << _estimate.bf << " ";
  293. return os.good();
  294. }
  295. void EdgeMono::linearizeOplus()
  296. {
  297. const VertexPose* VPose = static_cast<const VertexPose*>(_vertices[1]);
  298. const g2o::VertexSBAPointXYZ* VPoint = static_cast<const g2o::VertexSBAPointXYZ*>(_vertices[0]);
  299. const Eigen::Matrix3d &Rcw = VPose->estimate().Rcw[cam_idx];
  300. const Eigen::Vector3d &tcw = VPose->estimate().tcw[cam_idx];
  301. const Eigen::Vector3d Xc = Rcw*VPoint->estimate() + tcw;
  302. const Eigen::Vector3d Xb = VPose->estimate().Rbc[cam_idx]*Xc+VPose->estimate().tbc[cam_idx];
  303. const Eigen::Matrix3d &Rcb = VPose->estimate().Rcb[cam_idx];
  304. const Eigen::Matrix<double,2,3> proj_jac = VPose->estimate().pCamera[cam_idx]->projectJac(Xc);
  305. _jacobianOplusXi = -proj_jac * Rcw;
  306. Eigen::Matrix<double,3,6> SE3deriv;
  307. double x = Xb(0);
  308. double y = Xb(1);
  309. double z = Xb(2);
  310. SE3deriv << 0.0, z, -y, 1.0, 0.0, 0.0,
  311. -z , 0.0, x, 0.0, 1.0, 0.0,
  312. y , -x , 0.0, 0.0, 0.0, 1.0;
  313. _jacobianOplusXj = proj_jac * Rcb * SE3deriv; // TODO optimize this product
  314. }
  315. void EdgeMonoOnlyPose::linearizeOplus()
  316. {
  317. const VertexPose* VPose = static_cast<const VertexPose*>(_vertices[0]);
  318. const Eigen::Matrix3d &Rcw = VPose->estimate().Rcw[cam_idx];
  319. const Eigen::Vector3d &tcw = VPose->estimate().tcw[cam_idx];
  320. const Eigen::Vector3d Xc = Rcw*Xw + tcw;
  321. const Eigen::Vector3d Xb = VPose->estimate().Rbc[cam_idx]*Xc+VPose->estimate().tbc[cam_idx];
  322. const Eigen::Matrix3d &Rcb = VPose->estimate().Rcb[cam_idx];
  323. Eigen::Matrix<double,2,3> proj_jac = VPose->estimate().pCamera[cam_idx]->projectJac(Xc);
  324. Eigen::Matrix<double,3,6> SE3deriv;
  325. double x = Xb(0);
  326. double y = Xb(1);
  327. double z = Xb(2);
  328. SE3deriv << 0.0, z, -y, 1.0, 0.0, 0.0,
  329. -z , 0.0, x, 0.0, 1.0, 0.0,
  330. y , -x , 0.0, 0.0, 0.0, 1.0;
  331. _jacobianOplusXi = proj_jac * Rcb * SE3deriv; // symbol different becasue of update mode
  332. }
  333. void EdgeStereo::linearizeOplus()
  334. {
  335. const VertexPose* VPose = static_cast<const VertexPose*>(_vertices[1]);
  336. const g2o::VertexSBAPointXYZ* VPoint = static_cast<const g2o::VertexSBAPointXYZ*>(_vertices[0]);
  337. const Eigen::Matrix3d &Rcw = VPose->estimate().Rcw[cam_idx];
  338. const Eigen::Vector3d &tcw = VPose->estimate().tcw[cam_idx];
  339. const Eigen::Vector3d Xc = Rcw*VPoint->estimate() + tcw;
  340. const Eigen::Vector3d Xb = VPose->estimate().Rbc[cam_idx]*Xc+VPose->estimate().tbc[cam_idx];
  341. const Eigen::Matrix3d &Rcb = VPose->estimate().Rcb[cam_idx];
  342. const double bf = VPose->estimate().bf;
  343. const double inv_z2 = 1.0/(Xc(2)*Xc(2));
  344. Eigen::Matrix<double,3,3> proj_jac;
  345. proj_jac.block<2,3>(0,0) = VPose->estimate().pCamera[cam_idx]->projectJac(Xc);
  346. proj_jac.block<1,3>(2,0) = proj_jac.block<1,3>(0,0);
  347. proj_jac(2,2) += bf*inv_z2;
  348. _jacobianOplusXi = -proj_jac * Rcw;
  349. Eigen::Matrix<double,3,6> SE3deriv;
  350. double x = Xb(0);
  351. double y = Xb(1);
  352. double z = Xb(2);
  353. SE3deriv << 0.0, z, -y, 1.0, 0.0, 0.0,
  354. -z , 0.0, x, 0.0, 1.0, 0.0,
  355. y , -x , 0.0, 0.0, 0.0, 1.0;
  356. _jacobianOplusXj = proj_jac * Rcb * SE3deriv;
  357. /*const VertexPose* VPose = static_cast<const VertexPose*>(_vertices[1]);
  358. const g2o::VertexSBAPointXYZ* VPoint = static_cast<const g2o::VertexSBAPointXYZ*>(_vertices[0]);
  359. const Eigen::Matrix3d &Rcw = VPose->estimate().Rcw;
  360. const Eigen::Vector3d &tcw = VPose->estimate().tcw;
  361. const Eigen::Vector3d Xc = Rcw*VPoint->estimate() + tcw;
  362. const double &xc = Xc[0];
  363. const double &yc = Xc[1];
  364. const double invzc = 1.0/Xc[2];
  365. const double invzc2 = invzc*invzc;
  366. const double &fx = VPose->estimate().fx;
  367. const double &fy = VPose->estimate().fy;
  368. const double &bf = VPose->estimate().bf;
  369. const Eigen::Matrix3d &Rcb = VPose->estimate().Rcb;
  370. // Jacobian wrt Point
  371. _jacobianOplusXi(0,0) = -fx*invzc*Rcw(0,0)+fx*xc*invzc2*Rcw(2,0);
  372. _jacobianOplusXi(0,1) = -fx*invzc*Rcw(0,1)+fx*xc*invzc2*Rcw(2,1);
  373. _jacobianOplusXi(0,2) = -fx*invzc*Rcw(0,2)+fx*xc*invzc2*Rcw(2,2);
  374. _jacobianOplusXi(1,0) = -fy*invzc*Rcw(1,0)+fy*yc*invzc2*Rcw(2,0);
  375. _jacobianOplusXi(1,1) = -fy*invzc*Rcw(1,1)+fy*yc*invzc2*Rcw(2,1);
  376. _jacobianOplusXi(1,2) = -fy*invzc*Rcw(1,2)+fy*yc*invzc2*Rcw(2,2);
  377. _jacobianOplusXi(2,0) = _jacobianOplusXi(0,0)-bf*invzc2*Rcw(2,0);
  378. _jacobianOplusXi(2,1) = _jacobianOplusXi(0,1)-bf*invzc2*Rcw(2,1);
  379. _jacobianOplusXi(2,2) = _jacobianOplusXi(0,2)-bf*invzc2*Rcw(2,2);
  380. const Eigen::Vector3d Xb = VPose->estimate().Rbc*Xc + VPose->estimate().tbc;
  381. const Eigen::Matrix3d RS = VPose->estimate().Rcb*Skew(Xb);
  382. // Jacobian wrt Imu Pose
  383. _jacobianOplusXj(0,0) = -fx*invzc*RS(0,0)+fx*xc*invzc2*RS(2,0);
  384. _jacobianOplusXj(0,1) = -fx*invzc*RS(0,1)+fx*xc*invzc2*RS(2,1);
  385. _jacobianOplusXj(0,2) = -fx*invzc*RS(0,2)+fx*xc*invzc2*RS(2,2);
  386. _jacobianOplusXj(0,3) = fx*invzc*Rcb(0,0)-fx*xc*invzc2*Rcb(2,0);
  387. _jacobianOplusXj(0,4) = fx*invzc*Rcb(0,1)-fx*xc*invzc2*Rcb(2,1);
  388. _jacobianOplusXj(0,5) = fx*invzc*Rcb(0,2)-fx*xc*invzc2*Rcb(2,2);
  389. _jacobianOplusXj(1,0) = -fy*invzc*RS(1,0)+fy*yc*invzc2*RS(2,0);
  390. _jacobianOplusXj(1,1) = -fy*invzc*RS(1,1)+fy*yc*invzc2*RS(2,1);
  391. _jacobianOplusXj(1,2) = -fy*invzc*RS(1,2)+fy*yc*invzc2*RS(2,2);
  392. _jacobianOplusXj(1,3) = fy*invzc*Rcb(1,0)-fy*yc*invzc2*Rcb(2,0);
  393. _jacobianOplusXj(1,4) = fy*invzc*Rcb(1,1)-fy*yc*invzc2*Rcb(2,1);
  394. _jacobianOplusXj(1,5) = fy*invzc*Rcb(1,2)-fy*yc*invzc2*Rcb(2,2);
  395. _jacobianOplusXj(2,0) = _jacobianOplusXj(0,0) - bf*invzc2*RS(2,0);
  396. _jacobianOplusXj(2,1) = _jacobianOplusXj(0,1) - bf*invzc2*RS(2,1);
  397. _jacobianOplusXj(2,2) = _jacobianOplusXj(0,2) - bf*invzc2*RS(2,2);
  398. _jacobianOplusXj(2,3) = _jacobianOplusXj(0,3) + bf*invzc2*Rcb(2,0);
  399. _jacobianOplusXj(2,4) = _jacobianOplusXj(0,4) + bf*invzc2*Rcb(2,1);
  400. _jacobianOplusXj(2,5) = _jacobianOplusXj(0,5) + bf*invzc2*Rcb(2,2);
  401. */
  402. }
  403. void EdgeStereoOnlyPose::linearizeOplus()
  404. {
  405. const VertexPose* VPose = static_cast<const VertexPose*>(_vertices[0]);
  406. const Eigen::Matrix3d &Rcw = VPose->estimate().Rcw[cam_idx];
  407. const Eigen::Vector3d &tcw = VPose->estimate().tcw[cam_idx];
  408. const Eigen::Vector3d Xc = Rcw*Xw + tcw;
  409. const Eigen::Vector3d Xb = VPose->estimate().Rbc[cam_idx]*Xc+VPose->estimate().tbc[cam_idx];
  410. const Eigen::Matrix3d &Rcb = VPose->estimate().Rcb[cam_idx];
  411. const double bf = VPose->estimate().bf;
  412. const double inv_z2 = 1.0/(Xc(2)*Xc(2));
  413. Eigen::Matrix<double,3,3> proj_jac;
  414. proj_jac.block<2,3>(0,0) = VPose->estimate().pCamera[cam_idx]->projectJac(Xc);
  415. proj_jac.block<1,3>(2,0) = proj_jac.block<1,3>(0,0);
  416. proj_jac(2,2) += bf*inv_z2;
  417. Eigen::Matrix<double,3,6> SE3deriv;
  418. double x = Xb(0);
  419. double y = Xb(1);
  420. double z = Xb(2);
  421. SE3deriv << 0.0, z, -y, 1.0, 0.0, 0.0,
  422. -z , 0.0, x, 0.0, 1.0, 0.0,
  423. y , -x , 0.0, 0.0, 0.0, 1.0;
  424. _jacobianOplusXi = proj_jac * Rcb * SE3deriv;
  425. /*const VertexPose* VPose = static_cast<const VertexPose*>(_vertices[0]);
  426. const Eigen::Matrix3d &Rcw = VPose->estimate().Rcw;
  427. const Eigen::Vector3d &tcw = VPose->estimate().tcw;
  428. const Eigen::Vector3d Xc = Rcw*Xw + tcw;
  429. const double &xc = Xc[0];
  430. const double &yc = Xc[1];
  431. const double invzc = 1.0/Xc[2];
  432. const double invzc2 = invzc*invzc;
  433. const double &fx = VPose->estimate().fx;
  434. const double &fy = VPose->estimate().fy;
  435. const double &bf = VPose->estimate().bf;
  436. const Eigen::Matrix3d &Rcb = VPose->estimate().Rcb;
  437. const Eigen::Vector3d Xb = VPose->estimate().Rbc*Xc + VPose->estimate().tbc;
  438. const Eigen::Matrix3d RS = VPose->estimate().Rcb*Skew(Xb);
  439. // Jacobian wrt Imu Pose
  440. _jacobianOplusXi(0,0) = -fx*invzc*RS(0,0)+fx*xc*invzc2*RS(2,0);
  441. _jacobianOplusXi(0,1) = -fx*invzc*RS(0,1)+fx*xc*invzc2*RS(2,1);
  442. _jacobianOplusXi(0,2) = -fx*invzc*RS(0,2)+fx*xc*invzc2*RS(2,2);
  443. _jacobianOplusXi(0,3) = fx*invzc*Rcb(0,0)-fx*xc*invzc2*Rcb(2,0);
  444. _jacobianOplusXi(0,4) = fx*invzc*Rcb(0,1)-fx*xc*invzc2*Rcb(2,1);
  445. _jacobianOplusXi(0,5) = fx*invzc*Rcb(0,2)-fx*xc*invzc2*Rcb(2,2);
  446. _jacobianOplusXi(1,0) = -fy*invzc*RS(1,0)+fy*yc*invzc2*RS(2,0);
  447. _jacobianOplusXi(1,1) = -fy*invzc*RS(1,1)+fy*yc*invzc2*RS(2,1);
  448. _jacobianOplusXi(1,2) = -fy*invzc*RS(1,2)+fy*yc*invzc2*RS(2,2);
  449. _jacobianOplusXi(1,3) = fy*invzc*Rcb(1,0)-fy*yc*invzc2*Rcb(2,0);
  450. _jacobianOplusXi(1,4) = fy*invzc*Rcb(1,1)-fy*yc*invzc2*Rcb(2,1);
  451. _jacobianOplusXi(1,5) = fy*invzc*Rcb(1,2)-fy*yc*invzc2*Rcb(2,2);
  452. _jacobianOplusXi(2,0) = _jacobianOplusXi(0,0) - bf*invzc2*RS(2,0);
  453. _jacobianOplusXi(2,1) = _jacobianOplusXi(0,1) - bf*invzc2*RS(2,1);
  454. _jacobianOplusXi(2,2) = _jacobianOplusXi(0,2) - bf*invzc2*RS(2,2);
  455. _jacobianOplusXi(2,3) = _jacobianOplusXi(0,3) + bf*invzc2*Rcb(2,0);
  456. _jacobianOplusXi(2,4) = _jacobianOplusXi(0,4) + bf*invzc2*Rcb(2,1);
  457. _jacobianOplusXi(2,5) = _jacobianOplusXi(0,5) + bf*invzc2*Rcb(2,2);
  458. */
  459. }
  460. /*Eigen::Vector2d EdgeMonoInvdepth::cam_project(const Eigen::Vector3d & trans_xyz) const{
  461. double invZ = 1./trans_xyz[2];
  462. Eigen::Vector2d res;
  463. res[0] = invZ*trans_xyz[0]*fx + cx;
  464. res[1] = invZ*trans_xyz[1]*fy + cy;
  465. return res;
  466. }
  467. Eigen::Vector3d EdgeMonoInvdepth::cam_unproject(const double u, const double v, const double invDepth) const{
  468. Eigen::Vector3d res;
  469. res[2] = 1./invDepth;
  470. double z_x=res[2]/fx;
  471. double z_y=res[2]/fy;
  472. res[0] = (u-cx)*z_x;
  473. res[1] = (v-cy)*z_y;
  474. return res;
  475. }
  476. void EdgeMonoInvdepth::linearizeOplus()
  477. {
  478. VertexInvDepth *vPt = static_cast<VertexInvDepth*>(_vertices[0]);
  479. g2o::VertexSE3Expmap *vHost = static_cast<g2o::VertexSE3Expmap*>(_vertices[1]);
  480. g2o::VertexSE3Expmap *vObs = static_cast<g2o::VertexSE3Expmap*>(_vertices[2]);
  481. //
  482. g2o::SE3Quat Tiw(vObs->estimate());
  483. g2o::SE3Quat T0w(vHost->estimate());
  484. g2o::SE3Quat Ti0 = Tiw*T0w.inverse();
  485. double o_rho_j = vPt->estimate().rho;
  486. Eigen::Vector3d o_X_j = cam_unproject(vPt->estimate().u,vPt->estimate().v,o_rho_j);
  487. Eigen::Vector3d i_X_j = Ti0.map(o_X_j);
  488. double i_rho_j = 1./i_X_j[2];
  489. Eigen::Vector2d i_proj_j = cam_project(i_X_j);
  490. // i_rho_j*C_ij matrix
  491. Eigen::Matrix<double,2,3> rhoC_ij;
  492. rhoC_ij(0,0) = i_rho_j*fx;
  493. rhoC_ij(0,1) = 0.0;
  494. rhoC_ij(0,2) = i_rho_j*(cx-i_proj_j[0]);
  495. rhoC_ij(1,0) = 0.0;
  496. rhoC_ij(1,1) = i_rho_j*fy;
  497. rhoC_ij(1,2) = i_rho_j*(cy-i_proj_j[1]);
  498. // o_rho_j^{-2}*K^{-1}*0_proj_j vector
  499. Eigen::Matrix3d Ri0 = Ti0.rotation().toRotationMatrix();
  500. Eigen::Matrix<double, 2, 3> tmp;
  501. tmp = rhoC_ij*Ri0;
  502. // Skew matrices
  503. Eigen::Matrix3d skew_0_X_j;
  504. Eigen::Matrix3d skew_i_X_j;
  505. skew_0_X_j=Eigen::MatrixXd::Zero(3,3);
  506. skew_i_X_j=Eigen::MatrixXd::Zero(3,3);
  507. skew_0_X_j(0,1) = -o_X_j[2];
  508. skew_0_X_j(1,0) = -skew_0_X_j(0,1);
  509. skew_0_X_j(0,2) = o_X_j[1];
  510. skew_0_X_j(2,0) = -skew_0_X_j(0,2);
  511. skew_0_X_j(1,2) = -o_X_j[0];
  512. skew_0_X_j(2,1) = -skew_0_X_j(1,2);
  513. skew_i_X_j(0,1) = -i_X_j[2];
  514. skew_i_X_j(1,0) = -skew_i_X_j(0,1);
  515. skew_i_X_j(0,2) = i_X_j[1];
  516. skew_i_X_j(2,0) = -skew_i_X_j(0,2);
  517. skew_i_X_j(1,2) = -i_X_j[0];
  518. skew_i_X_j(2,1) = -skew_i_X_j(1,2);
  519. // Jacobians w.r.t inverse depth in the host frame
  520. _jacobianOplus[0].setZero();
  521. _jacobianOplus[0] = (1./o_rho_j)*rhoC_ij*Ri0*o_X_j;
  522. // Jacobians w.r.t pose host frame
  523. _jacobianOplus[1].setZero();
  524. // Rotation
  525. _jacobianOplus[1].block<2,3>(0,0) = -tmp*skew_0_X_j;
  526. // translation
  527. _jacobianOplus[1].block<2,3>(0,3) = tmp;
  528. // Jacobians w.r.t pose observer frame
  529. _jacobianOplus[2].setZero();
  530. // Rotation
  531. _jacobianOplus[2].block<2,3>(0,0) = rhoC_ij*skew_i_X_j;
  532. // translation
  533. _jacobianOplus[2].block<2,3>(0,3) = -rhoC_ij;
  534. }
  535. Eigen::Vector2d EdgeMonoInvdepthBody::cam_project(const Eigen::Vector3d & trans_xyz) const{
  536. double invZ = 1./trans_xyz[2];
  537. Eigen::Vector2d res;
  538. res[0] = invZ*trans_xyz[0]*fx + cx;
  539. res[1] = invZ*trans_xyz[1]*fy + cy;
  540. return res;
  541. }
  542. Eigen::Vector3d EdgeMonoInvdepthBody::cam_unproject(const double u, const double v, const double invDepth) const{
  543. Eigen::Vector3d res;
  544. res[2] = 1./invDepth;
  545. double z_x=res[2]/fx;
  546. double z_y=res[2]/fy;
  547. res[0] = (u-cx)*z_x;
  548. res[1] = (v-cy)*z_y;
  549. return res;
  550. }*/
  551. VertexVelocity::VertexVelocity(KeyFrame* pKF)
  552. {
  553. setEstimate(Converter::toVector3d(pKF->GetVelocity()));
  554. }
  555. VertexVelocity::VertexVelocity(Frame* pF)
  556. {
  557. setEstimate(Converter::toVector3d(pF->mVw));
  558. }
  559. VertexGyroBias::VertexGyroBias(KeyFrame *pKF)
  560. {
  561. setEstimate(Converter::toVector3d(pKF->GetGyroBias()));
  562. }
  563. VertexGyroBias::VertexGyroBias(Frame *pF)
  564. {
  565. Eigen::Vector3d bg;
  566. bg << pF->mImuBias.bwx, pF->mImuBias.bwy,pF->mImuBias.bwz;
  567. setEstimate(bg);
  568. }
  569. VertexAccBias::VertexAccBias(KeyFrame *pKF)
  570. {
  571. setEstimate(Converter::toVector3d(pKF->GetAccBias()));
  572. }
  573. VertexAccBias::VertexAccBias(Frame *pF)
  574. {
  575. Eigen::Vector3d ba;
  576. ba << pF->mImuBias.bax, pF->mImuBias.bay,pF->mImuBias.baz;
  577. setEstimate(ba);
  578. }
  579. EdgeInertial::EdgeInertial(IMU::Preintegrated *pInt):JRg(Converter::toMatrix3d(pInt->JRg)),
  580. JVg(Converter::toMatrix3d(pInt->JVg)), JPg(Converter::toMatrix3d(pInt->JPg)), JVa(Converter::toMatrix3d(pInt->JVa)),
  581. JPa(Converter::toMatrix3d(pInt->JPa)), mpInt(pInt), dt(pInt->dT)
  582. {
  583. // This edge links 6 vertices
  584. resize(6);
  585. g << 0, 0, -IMU::GRAVITY_VALUE;
  586. cv::Mat cvInfo = pInt->C.rowRange(0,9).colRange(0,9).inv(cv::DECOMP_SVD);
  587. Matrix9d Info;
  588. for(int r=0;r<9;r++)
  589. for(int c=0;c<9;c++)
  590. Info(r,c)=cvInfo.at<float>(r,c);
  591. Info = (Info+Info.transpose())/2;
  592. Eigen::SelfAdjointEigenSolver<Eigen::Matrix<double,9,9> > es(Info);
  593. Eigen::Matrix<double,9,1> eigs = es.eigenvalues();
  594. for(int i=0;i<9;i++)
  595. if(eigs[i]<1e-12)
  596. eigs[i]=0;
  597. Info = es.eigenvectors()*eigs.asDiagonal()*es.eigenvectors().transpose();
  598. setInformation(Info);
  599. }
  600. void EdgeInertial::computeError()
  601. {
  602. // TODO Maybe Reintegrate inertial measurments when difference between linearization point and current estimate is too big
  603. const VertexPose* VP1 = static_cast<const VertexPose*>(_vertices[0]);
  604. const VertexVelocity* VV1= static_cast<const VertexVelocity*>(_vertices[1]);
  605. const VertexGyroBias* VG1= static_cast<const VertexGyroBias*>(_vertices[2]);
  606. const VertexAccBias* VA1= static_cast<const VertexAccBias*>(_vertices[3]);
  607. const VertexPose* VP2 = static_cast<const VertexPose*>(_vertices[4]);
  608. const VertexVelocity* VV2 = static_cast<const VertexVelocity*>(_vertices[5]);
  609. const IMU::Bias b1(VA1->estimate()[0],VA1->estimate()[1],VA1->estimate()[2],VG1->estimate()[0],VG1->estimate()[1],VG1->estimate()[2]);
  610. const Eigen::Matrix3d dR = Converter::toMatrix3d(mpInt->GetDeltaRotation(b1));
  611. const Eigen::Vector3d dV = Converter::toVector3d(mpInt->GetDeltaVelocity(b1));
  612. const Eigen::Vector3d dP = Converter::toVector3d(mpInt->GetDeltaPosition(b1));
  613. const Eigen::Vector3d er = LogSO3(dR.transpose()*VP1->estimate().Rwb.transpose()*VP2->estimate().Rwb);
  614. const Eigen::Vector3d ev = VP1->estimate().Rwb.transpose()*(VV2->estimate() - VV1->estimate() - g*dt) - dV;
  615. const Eigen::Vector3d ep = VP1->estimate().Rwb.transpose()*(VP2->estimate().twb - VP1->estimate().twb
  616. - VV1->estimate()*dt - g*dt*dt/2) - dP;
  617. _error << er, ev, ep;
  618. }
  619. void EdgeInertial::linearizeOplus()
  620. {
  621. const VertexPose* VP1 = static_cast<const VertexPose*>(_vertices[0]);
  622. const VertexVelocity* VV1= static_cast<const VertexVelocity*>(_vertices[1]);
  623. const VertexGyroBias* VG1= static_cast<const VertexGyroBias*>(_vertices[2]);
  624. const VertexAccBias* VA1= static_cast<const VertexAccBias*>(_vertices[3]);
  625. const VertexPose* VP2 = static_cast<const VertexPose*>(_vertices[4]);
  626. const VertexVelocity* VV2= static_cast<const VertexVelocity*>(_vertices[5]);
  627. const IMU::Bias b1(VA1->estimate()[0],VA1->estimate()[1],VA1->estimate()[2],VG1->estimate()[0],VG1->estimate()[1],VG1->estimate()[2]);
  628. const IMU::Bias db = mpInt->GetDeltaBias(b1);
  629. Eigen::Vector3d dbg;
  630. dbg << db.bwx, db.bwy, db.bwz;
  631. const Eigen::Matrix3d Rwb1 = VP1->estimate().Rwb;
  632. const Eigen::Matrix3d Rbw1 = Rwb1.transpose();
  633. const Eigen::Matrix3d Rwb2 = VP2->estimate().Rwb;
  634. const Eigen::Matrix3d dR = Converter::toMatrix3d(mpInt->GetDeltaRotation(b1));
  635. const Eigen::Matrix3d eR = dR.transpose()*Rbw1*Rwb2;
  636. const Eigen::Vector3d er = LogSO3(eR);
  637. const Eigen::Matrix3d invJr = InverseRightJacobianSO3(er);
  638. // Jacobians wrt Pose 1
  639. _jacobianOplus[0].setZero();
  640. // rotation
  641. _jacobianOplus[0].block<3,3>(0,0) = -invJr*Rwb2.transpose()*Rwb1; // OK
  642. _jacobianOplus[0].block<3,3>(3,0) = Skew(Rbw1*(VV2->estimate() - VV1->estimate() - g*dt)); // OK
  643. _jacobianOplus[0].block<3,3>(6,0) = Skew(Rbw1*(VP2->estimate().twb - VP1->estimate().twb
  644. - VV1->estimate()*dt - 0.5*g*dt*dt)); // OK
  645. // translation
  646. _jacobianOplus[0].block<3,3>(6,3) = -Eigen::Matrix3d::Identity(); // OK
  647. // Jacobians wrt Velocity 1
  648. _jacobianOplus[1].setZero();
  649. _jacobianOplus[1].block<3,3>(3,0) = -Rbw1; // OK
  650. _jacobianOplus[1].block<3,3>(6,0) = -Rbw1*dt; // OK
  651. // Jacobians wrt Gyro 1
  652. _jacobianOplus[2].setZero();
  653. _jacobianOplus[2].block<3,3>(0,0) = -invJr*eR.transpose()*RightJacobianSO3(JRg*dbg)*JRg; // OK
  654. _jacobianOplus[2].block<3,3>(3,0) = -JVg; // OK
  655. _jacobianOplus[2].block<3,3>(6,0) = -JPg; // OK
  656. // Jacobians wrt Accelerometer 1
  657. _jacobianOplus[3].setZero();
  658. _jacobianOplus[3].block<3,3>(3,0) = -JVa; // OK
  659. _jacobianOplus[3].block<3,3>(6,0) = -JPa; // OK
  660. // Jacobians wrt Pose 2
  661. _jacobianOplus[4].setZero();
  662. // rotation
  663. _jacobianOplus[4].block<3,3>(0,0) = invJr; // OK
  664. // translation
  665. _jacobianOplus[4].block<3,3>(6,3) = Rbw1*Rwb2; // OK
  666. // Jacobians wrt Velocity 2
  667. _jacobianOplus[5].setZero();
  668. _jacobianOplus[5].block<3,3>(3,0) = Rbw1; // OK
  669. }
  670. EdgeInertialGS::EdgeInertialGS(IMU::Preintegrated *pInt):JRg(Converter::toMatrix3d(pInt->JRg)),
  671. JVg(Converter::toMatrix3d(pInt->JVg)), JPg(Converter::toMatrix3d(pInt->JPg)), JVa(Converter::toMatrix3d(pInt->JVa)),
  672. JPa(Converter::toMatrix3d(pInt->JPa)), mpInt(pInt), dt(pInt->dT)
  673. {
  674. // This edge links 8 vertices
  675. resize(8);
  676. gI << 0, 0, -IMU::GRAVITY_VALUE;
  677. cv::Mat cvInfo = pInt->C.rowRange(0,9).colRange(0,9).inv(cv::DECOMP_SVD);
  678. Matrix9d Info;
  679. for(int r=0;r<9;r++)
  680. for(int c=0;c<9;c++)
  681. Info(r,c)=cvInfo.at<float>(r,c);
  682. Info = (Info+Info.transpose())/2;
  683. Eigen::SelfAdjointEigenSolver<Eigen::Matrix<double,9,9> > es(Info);
  684. Eigen::Matrix<double,9,1> eigs = es.eigenvalues();
  685. for(int i=0;i<9;i++)
  686. if(eigs[i]<1e-12)
  687. eigs[i]=0;
  688. Info = es.eigenvectors()*eigs.asDiagonal()*es.eigenvectors().transpose();
  689. setInformation(Info);
  690. }
  691. void EdgeInertialGS::computeError()
  692. {
  693. // TODO Maybe Reintegrate inertial measurments when difference between linearization point and current estimate is too big
  694. const VertexPose* VP1 = static_cast<const VertexPose*>(_vertices[0]);
  695. const VertexVelocity* VV1= static_cast<const VertexVelocity*>(_vertices[1]);
  696. const VertexGyroBias* VG= static_cast<const VertexGyroBias*>(_vertices[2]);
  697. const VertexAccBias* VA= static_cast<const VertexAccBias*>(_vertices[3]);
  698. const VertexPose* VP2 = static_cast<const VertexPose*>(_vertices[4]);
  699. const VertexVelocity* VV2 = static_cast<const VertexVelocity*>(_vertices[5]);
  700. const VertexGDir* VGDir = static_cast<const VertexGDir*>(_vertices[6]);
  701. const VertexScale* VS = static_cast<const VertexScale*>(_vertices[7]);
  702. const IMU::Bias b(VA->estimate()[0],VA->estimate()[1],VA->estimate()[2],VG->estimate()[0],VG->estimate()[1],VG->estimate()[2]);
  703. g = VGDir->estimate().Rwg*gI;
  704. const double s = VS->estimate();
  705. const Eigen::Matrix3d dR = Converter::toMatrix3d(mpInt->GetDeltaRotation(b));
  706. const Eigen::Vector3d dV = Converter::toVector3d(mpInt->GetDeltaVelocity(b));
  707. const Eigen::Vector3d dP = Converter::toVector3d(mpInt->GetDeltaPosition(b));
  708. const Eigen::Vector3d er = LogSO3(dR.transpose()*VP1->estimate().Rwb.transpose()*VP2->estimate().Rwb);
  709. const Eigen::Vector3d ev = VP1->estimate().Rwb.transpose()*(s*(VV2->estimate() - VV1->estimate()) - g*dt) - dV;
  710. const Eigen::Vector3d ep = VP1->estimate().Rwb.transpose()*(s*(VP2->estimate().twb - VP1->estimate().twb - VV1->estimate()*dt) - g*dt*dt/2) - dP;
  711. _error << er, ev, ep;
  712. }
  713. void EdgeInertialGS::linearizeOplus()
  714. {
  715. const VertexPose* VP1 = static_cast<const VertexPose*>(_vertices[0]);
  716. const VertexVelocity* VV1= static_cast<const VertexVelocity*>(_vertices[1]);
  717. const VertexGyroBias* VG= static_cast<const VertexGyroBias*>(_vertices[2]);
  718. const VertexAccBias* VA= static_cast<const VertexAccBias*>(_vertices[3]);
  719. const VertexPose* VP2 = static_cast<const VertexPose*>(_vertices[4]);
  720. const VertexVelocity* VV2 = static_cast<const VertexVelocity*>(_vertices[5]);
  721. const VertexGDir* VGDir = static_cast<const VertexGDir*>(_vertices[6]);
  722. const VertexScale* VS = static_cast<const VertexScale*>(_vertices[7]);
  723. const IMU::Bias b(VA->estimate()[0],VA->estimate()[1],VA->estimate()[2],VG->estimate()[0],VG->estimate()[1],VG->estimate()[2]);
  724. const IMU::Bias db = mpInt->GetDeltaBias(b);
  725. Eigen::Vector3d dbg;
  726. dbg << db.bwx, db.bwy, db.bwz;
  727. const Eigen::Matrix3d Rwb1 = VP1->estimate().Rwb;
  728. const Eigen::Matrix3d Rbw1 = Rwb1.transpose();
  729. const Eigen::Matrix3d Rwb2 = VP2->estimate().Rwb;
  730. const Eigen::Matrix3d Rwg = VGDir->estimate().Rwg;
  731. Eigen::MatrixXd Gm = Eigen::MatrixXd::Zero(3,2);
  732. Gm(0,1) = -IMU::GRAVITY_VALUE;
  733. Gm(1,0) = IMU::GRAVITY_VALUE;
  734. const double s = VS->estimate();
  735. const Eigen::MatrixXd dGdTheta = Rwg*Gm;
  736. const Eigen::Matrix3d dR = Converter::toMatrix3d(mpInt->GetDeltaRotation(b));
  737. const Eigen::Matrix3d eR = dR.transpose()*Rbw1*Rwb2;
  738. const Eigen::Vector3d er = LogSO3(eR);
  739. const Eigen::Matrix3d invJr = InverseRightJacobianSO3(er);
  740. // Jacobians wrt Pose 1
  741. _jacobianOplus[0].setZero();
  742. // rotation
  743. _jacobianOplus[0].block<3,3>(0,0) = -invJr*Rwb2.transpose()*Rwb1;
  744. _jacobianOplus[0].block<3,3>(3,0) = Skew(Rbw1*(s*(VV2->estimate() - VV1->estimate()) - g*dt));
  745. _jacobianOplus[0].block<3,3>(6,0) = Skew(Rbw1*(s*(VP2->estimate().twb - VP1->estimate().twb
  746. - VV1->estimate()*dt) - 0.5*g*dt*dt));
  747. // translation
  748. _jacobianOplus[0].block<3,3>(6,3) = -s*Eigen::Matrix3d::Identity();
  749. // Jacobians wrt Velocity 1
  750. _jacobianOplus[1].setZero();
  751. _jacobianOplus[1].block<3,3>(3,0) = -s*Rbw1;
  752. _jacobianOplus[1].block<3,3>(6,0) = -s*Rbw1*dt;
  753. // Jacobians wrt Gyro bias
  754. _jacobianOplus[2].setZero();
  755. _jacobianOplus[2].block<3,3>(0,0) = -invJr*eR.transpose()*RightJacobianSO3(JRg*dbg)*JRg;
  756. _jacobianOplus[2].block<3,3>(3,0) = -JVg;
  757. _jacobianOplus[2].block<3,3>(6,0) = -JPg;
  758. // Jacobians wrt Accelerometer bias
  759. _jacobianOplus[3].setZero();
  760. _jacobianOplus[3].block<3,3>(3,0) = -JVa;
  761. _jacobianOplus[3].block<3,3>(6,0) = -JPa;
  762. // Jacobians wrt Pose 2
  763. _jacobianOplus[4].setZero();
  764. // rotation
  765. _jacobianOplus[4].block<3,3>(0,0) = invJr;
  766. // translation
  767. _jacobianOplus[4].block<3,3>(6,3) = s*Rbw1*Rwb2;
  768. // Jacobians wrt Velocity 2
  769. _jacobianOplus[5].setZero();
  770. _jacobianOplus[5].block<3,3>(3,0) = s*Rbw1;
  771. // Jacobians wrt Gravity direction
  772. _jacobianOplus[6].setZero();
  773. _jacobianOplus[6].block<3,2>(3,0) = -Rbw1*dGdTheta*dt;
  774. _jacobianOplus[6].block<3,2>(6,0) = -0.5*Rbw1*dGdTheta*dt*dt;
  775. // Jacobians wrt scale factor
  776. _jacobianOplus[7].setZero();
  777. _jacobianOplus[7].block<3,1>(3,0) = Rbw1*(VV2->estimate()-VV1->estimate());
  778. _jacobianOplus[7].block<3,1>(6,0) = Rbw1*(VP2->estimate().twb-VP1->estimate().twb-VV1->estimate()*dt);
  779. }
  780. EdgePriorPoseImu::EdgePriorPoseImu(ConstraintPoseImu *c)
  781. {
  782. resize(4);
  783. Rwb = c->Rwb;
  784. twb = c->twb;
  785. vwb = c->vwb;
  786. bg = c->bg;
  787. ba = c->ba;
  788. setInformation(c->H);
  789. }
  790. void EdgePriorPoseImu::computeError()
  791. {
  792. const VertexPose* VP = static_cast<const VertexPose*>(_vertices[0]);
  793. const VertexVelocity* VV = static_cast<const VertexVelocity*>(_vertices[1]);
  794. const VertexGyroBias* VG = static_cast<const VertexGyroBias*>(_vertices[2]);
  795. const VertexAccBias* VA = static_cast<const VertexAccBias*>(_vertices[3]);
  796. const Eigen::Vector3d er = LogSO3(Rwb.transpose()*VP->estimate().Rwb);
  797. const Eigen::Vector3d et = Rwb.transpose()*(VP->estimate().twb-twb);
  798. const Eigen::Vector3d ev = VV->estimate() - vwb;
  799. const Eigen::Vector3d ebg = VG->estimate() - bg;
  800. const Eigen::Vector3d eba = VA->estimate() - ba;
  801. _error << er, et, ev, ebg, eba;
  802. }
  803. void EdgePriorPoseImu::linearizeOplus()
  804. {
  805. const VertexPose* VP = static_cast<const VertexPose*>(_vertices[0]);
  806. const Eigen::Vector3d er = LogSO3(Rwb.transpose()*VP->estimate().Rwb);
  807. _jacobianOplus[0].setZero();
  808. _jacobianOplus[0].block<3,3>(0,0) = InverseRightJacobianSO3(er);
  809. _jacobianOplus[0].block<3,3>(3,3) = Rwb.transpose()*VP->estimate().Rwb;
  810. _jacobianOplus[1].setZero();
  811. _jacobianOplus[1].block<3,3>(6,0) = Eigen::Matrix3d::Identity();
  812. _jacobianOplus[2].setZero();
  813. _jacobianOplus[2].block<3,3>(9,0) = Eigen::Matrix3d::Identity();
  814. _jacobianOplus[3].setZero();
  815. _jacobianOplus[3].block<3,3>(12,0) = Eigen::Matrix3d::Identity();
  816. }
  817. void EdgePriorAcc::linearizeOplus()
  818. {
  819. // Jacobian wrt bias
  820. _jacobianOplusXi.block<3,3>(0,0) = Eigen::Matrix3d::Identity();
  821. }
  822. void EdgePriorGyro::linearizeOplus()
  823. {
  824. // Jacobian wrt bias
  825. _jacobianOplusXi.block<3,3>(0,0) = Eigen::Matrix3d::Identity();
  826. }
  827. // SO3 FUNCTIONS
  828. Eigen::Matrix3d ExpSO3(const Eigen::Vector3d &w)
  829. {
  830. return ExpSO3(w[0],w[1],w[2]);
  831. }
  832. Eigen::Matrix3d ExpSO3(const double x, const double y, const double z)
  833. {
  834. const double d2 = x*x+y*y+z*z;
  835. const double d = sqrt(d2);
  836. Eigen::Matrix3d W;
  837. W << 0.0, -z, y,z, 0.0, -x,-y, x, 0.0;
  838. if(d<1e-5)
  839. {
  840. Eigen::Matrix3d res = Eigen::Matrix3d::Identity() + W +0.5*W*W;
  841. return Converter::toMatrix3d(IMU::NormalizeRotation(Converter::toCvMat(res)));
  842. }
  843. else
  844. {
  845. Eigen::Matrix3d res =Eigen::Matrix3d::Identity() + W*sin(d)/d + W*W*(1.0-cos(d))/d2;
  846. return Converter::toMatrix3d(IMU::NormalizeRotation(Converter::toCvMat(res)));
  847. }
  848. }
  849. Eigen::Vector3d LogSO3(const Eigen::Matrix3d &R)
  850. {
  851. const double tr = R(0,0)+R(1,1)+R(2,2);
  852. Eigen::Vector3d w;
  853. w << (R(2,1)-R(1,2))/2, (R(0,2)-R(2,0))/2, (R(1,0)-R(0,1))/2;
  854. const double costheta = (tr-1.0)*0.5f;
  855. if(costheta>1 || costheta<-1)
  856. return w;
  857. const double theta = acos(costheta);
  858. const double s = sin(theta);
  859. if(fabs(s)<1e-5)
  860. return w;
  861. else
  862. return theta*w/s;
  863. }
  864. Eigen::Matrix3d InverseRightJacobianSO3(const Eigen::Vector3d &v)
  865. {
  866. return InverseRightJacobianSO3(v[0],v[1],v[2]);
  867. }
  868. Eigen::Matrix3d InverseRightJacobianSO3(const double x, const double y, const double z)
  869. {
  870. const double d2 = x*x+y*y+z*z;
  871. const double d = sqrt(d2);
  872. Eigen::Matrix3d W;
  873. W << 0.0, -z, y,z, 0.0, -x,-y, x, 0.0;
  874. if(d<1e-5)
  875. return Eigen::Matrix3d::Identity();
  876. else
  877. return Eigen::Matrix3d::Identity() + W/2 + W*W*(1.0/d2 - (1.0+cos(d))/(2.0*d*sin(d)));
  878. }
  879. Eigen::Matrix3d RightJacobianSO3(const Eigen::Vector3d &v)
  880. {
  881. return RightJacobianSO3(v[0],v[1],v[2]);
  882. }
  883. Eigen::Matrix3d RightJacobianSO3(const double x, const double y, const double z)
  884. {
  885. const double d2 = x*x+y*y+z*z;
  886. const double d = sqrt(d2);
  887. Eigen::Matrix3d W;
  888. W << 0.0, -z, y,z, 0.0, -x,-y, x, 0.0;
  889. if(d<1e-5)
  890. {
  891. return Eigen::Matrix3d::Identity();
  892. }
  893. else
  894. {
  895. return Eigen::Matrix3d::Identity() - W*(1.0-cos(d))/d2 + W*W*(d-sin(d))/(d2*d);
  896. }
  897. }
  898. Eigen::Matrix3d Skew(const Eigen::Vector3d &w)
  899. {
  900. Eigen::Matrix3d W;
  901. W << 0.0, -w[2], w[1],w[2], 0.0, -w[0],-w[1], w[0], 0.0;
  902. return W;
  903. }
  904. Eigen::Matrix3d NormalizeRotation(const Eigen::Matrix3d &R)
  905. {
  906. Eigen::JacobiSVD<Eigen::Matrix3d> svd(R,Eigen::ComputeFullU | Eigen::ComputeFullV);
  907. return svd.matrixU()*svd.matrixV();
  908. }
  909. }