OptimizableTypes.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 "OptimizableTypes.h"
  19. namespace ORB_SLAM3 {
  20. bool EdgeSE3ProjectXYZOnlyPose::read(std::istream& is){
  21. for (int i=0; i<2; i++){
  22. is >> _measurement[i];
  23. }
  24. for (int i=0; i<2; i++)
  25. for (int j=i; j<2; j++) {
  26. is >> information()(i,j);
  27. if (i!=j)
  28. information()(j,i)=information()(i,j);
  29. }
  30. return true;
  31. }
  32. bool EdgeSE3ProjectXYZOnlyPose::write(std::ostream& os) const {
  33. for (int i=0; i<2; i++){
  34. os << measurement()[i] << " ";
  35. }
  36. for (int i=0; i<2; i++)
  37. for (int j=i; j<2; j++){
  38. os << " " << information()(i,j);
  39. }
  40. return os.good();
  41. }
  42. void EdgeSE3ProjectXYZOnlyPose::linearizeOplus() {
  43. g2o::VertexSE3Expmap * vi = static_cast<g2o::VertexSE3Expmap *>(_vertices[0]);
  44. Eigen::Vector3d xyz_trans = vi->estimate().map(Xw);
  45. double x = xyz_trans[0];
  46. double y = xyz_trans[1];
  47. double z = xyz_trans[2];
  48. Eigen::Matrix<double,3,6> SE3deriv;
  49. SE3deriv << 0.f, z, -y, 1.f, 0.f, 0.f,
  50. -z , 0.f, x, 0.f, 1.f, 0.f,
  51. y , -x , 0.f, 0.f, 0.f, 1.f;
  52. _jacobianOplusXi = -pCamera->projectJac(xyz_trans) * SE3deriv;
  53. }
  54. bool EdgeSE3ProjectXYZOnlyPoseToBody::read(std::istream& is){
  55. for (int i=0; i<2; i++){
  56. is >> _measurement[i];
  57. }
  58. for (int i=0; i<2; i++)
  59. for (int j=i; j<2; j++) {
  60. is >> information()(i,j);
  61. if (i!=j)
  62. information()(j,i)=information()(i,j);
  63. }
  64. return true;
  65. }
  66. bool EdgeSE3ProjectXYZOnlyPoseToBody::write(std::ostream& os) const {
  67. for (int i=0; i<2; i++){
  68. os << measurement()[i] << " ";
  69. }
  70. for (int i=0; i<2; i++)
  71. for (int j=i; j<2; j++){
  72. os << " " << information()(i,j);
  73. }
  74. return os.good();
  75. }
  76. void EdgeSE3ProjectXYZOnlyPoseToBody::linearizeOplus() {
  77. g2o::VertexSE3Expmap * vi = static_cast<g2o::VertexSE3Expmap *>(_vertices[0]);
  78. g2o::SE3Quat T_lw(vi->estimate());
  79. Eigen::Vector3d X_l = T_lw.map(Xw);
  80. Eigen::Vector3d X_r = mTrl.map(T_lw.map(Xw));
  81. double x_w = X_l[0];
  82. double y_w = X_l[1];
  83. double z_w = X_l[2];
  84. Eigen::Matrix<double,3,6> SE3deriv;
  85. SE3deriv << 0.f, z_w, -y_w, 1.f, 0.f, 0.f,
  86. -z_w , 0.f, x_w, 0.f, 1.f, 0.f,
  87. y_w , -x_w , 0.f, 0.f, 0.f, 1.f;
  88. _jacobianOplusXi = -pCamera->projectJac(X_r) * mTrl.rotation().toRotationMatrix() * SE3deriv;
  89. }
  90. EdgeSE3ProjectXYZ::EdgeSE3ProjectXYZ() : BaseBinaryEdge<2, Eigen::Vector2d, g2o::VertexSBAPointXYZ, g2o::VertexSE3Expmap>() {
  91. }
  92. bool EdgeSE3ProjectXYZ::read(std::istream& is){
  93. for (int i=0; i<2; i++){
  94. is >> _measurement[i];
  95. }
  96. for (int i=0; i<2; i++)
  97. for (int j=i; j<2; j++) {
  98. is >> information()(i,j);
  99. if (i!=j)
  100. information()(j,i)=information()(i,j);
  101. }
  102. return true;
  103. }
  104. bool EdgeSE3ProjectXYZ::write(std::ostream& os) const {
  105. for (int i=0; i<2; i++){
  106. os << measurement()[i] << " ";
  107. }
  108. for (int i=0; i<2; i++)
  109. for (int j=i; j<2; j++){
  110. os << " " << information()(i,j);
  111. }
  112. return os.good();
  113. }
  114. void EdgeSE3ProjectXYZ::linearizeOplus() {
  115. g2o::VertexSE3Expmap * vj = static_cast<g2o::VertexSE3Expmap *>(_vertices[1]);
  116. g2o::SE3Quat T(vj->estimate());
  117. g2o::VertexSBAPointXYZ* vi = static_cast<g2o::VertexSBAPointXYZ*>(_vertices[0]);
  118. Eigen::Vector3d xyz = vi->estimate();
  119. Eigen::Vector3d xyz_trans = T.map(xyz);
  120. double x = xyz_trans[0];
  121. double y = xyz_trans[1];
  122. double z = xyz_trans[2];
  123. auto projectJac = -pCamera->projectJac(xyz_trans);
  124. _jacobianOplusXi = projectJac * T.rotation().toRotationMatrix();
  125. Eigen::Matrix<double,3,6> SE3deriv;
  126. SE3deriv << 0.f, z, -y, 1.f, 0.f, 0.f,
  127. -z , 0.f, x, 0.f, 1.f, 0.f,
  128. y , -x , 0.f, 0.f, 0.f, 1.f;
  129. _jacobianOplusXj = projectJac * SE3deriv;
  130. }
  131. EdgeSE3ProjectXYZToBody::EdgeSE3ProjectXYZToBody() : BaseBinaryEdge<2, Eigen::Vector2d, g2o::VertexSBAPointXYZ, g2o::VertexSE3Expmap>() {
  132. }
  133. bool EdgeSE3ProjectXYZToBody::read(std::istream& is){
  134. for (int i=0; i<2; i++){
  135. is >> _measurement[i];
  136. }
  137. for (int i=0; i<2; i++)
  138. for (int j=i; j<2; j++) {
  139. is >> information()(i,j);
  140. if (i!=j)
  141. information()(j,i)=information()(i,j);
  142. }
  143. return true;
  144. }
  145. bool EdgeSE3ProjectXYZToBody::write(std::ostream& os) const {
  146. for (int i=0; i<2; i++){
  147. os << measurement()[i] << " ";
  148. }
  149. for (int i=0; i<2; i++)
  150. for (int j=i; j<2; j++){
  151. os << " " << information()(i,j);
  152. }
  153. return os.good();
  154. }
  155. void EdgeSE3ProjectXYZToBody::linearizeOplus() {
  156. g2o::VertexSE3Expmap * vj = static_cast<g2o::VertexSE3Expmap *>(_vertices[1]);
  157. g2o::SE3Quat T_lw(vj->estimate());
  158. g2o::SE3Quat T_rw = mTrl * T_lw;
  159. g2o::VertexSBAPointXYZ* vi = static_cast<g2o::VertexSBAPointXYZ*>(_vertices[0]);
  160. Eigen::Vector3d X_w = vi->estimate();
  161. Eigen::Vector3d X_l = T_lw.map(X_w);
  162. Eigen::Vector3d X_r = mTrl.map(T_lw.map(X_w));
  163. _jacobianOplusXi = -pCamera->projectJac(X_r) * T_rw.rotation().toRotationMatrix();
  164. double x = X_l[0];
  165. double y = X_l[1];
  166. double z = X_l[2];
  167. Eigen::Matrix<double,3,6> SE3deriv;
  168. SE3deriv << 0.f, z, -y, 1.f, 0.f, 0.f,
  169. -z , 0.f, x, 0.f, 1.f, 0.f,
  170. y , -x , 0.f, 0.f, 0.f, 1.f;
  171. _jacobianOplusXj = -pCamera->projectJac(X_r) * mTrl.rotation().toRotationMatrix() * SE3deriv;
  172. }
  173. VertexSim3Expmap::VertexSim3Expmap() : BaseVertex<7, g2o::Sim3>()
  174. {
  175. _marginalized=false;
  176. _fix_scale = false;
  177. }
  178. bool VertexSim3Expmap::read(std::istream& is)
  179. {
  180. g2o::Vector7d cam2world;
  181. for (int i=0; i<6; i++){
  182. is >> cam2world[i];
  183. }
  184. is >> cam2world[6];
  185. float nextParam;
  186. for(size_t i = 0; i < pCamera1->size(); i++){
  187. is >> nextParam;
  188. pCamera1->setParameter(nextParam,i);
  189. }
  190. for(size_t i = 0; i < pCamera2->size(); i++){
  191. is >> nextParam;
  192. pCamera2->setParameter(nextParam,i);
  193. }
  194. setEstimate(g2o::Sim3(cam2world).inverse());
  195. return true;
  196. }
  197. bool VertexSim3Expmap::write(std::ostream& os) const
  198. {
  199. g2o::Sim3 cam2world(estimate().inverse());
  200. g2o::Vector7d lv=cam2world.log();
  201. for (int i=0; i<7; i++){
  202. os << lv[i] << " ";
  203. }
  204. for(size_t i = 0; i < pCamera1->size(); i++){
  205. os << pCamera1->getParameter(i) << " ";
  206. }
  207. for(size_t i = 0; i < pCamera2->size(); i++){
  208. os << pCamera2->getParameter(i) << " ";
  209. }
  210. return os.good();
  211. }
  212. EdgeSim3ProjectXYZ::EdgeSim3ProjectXYZ() :
  213. g2o::BaseBinaryEdge<2, Eigen::Vector2d, g2o::VertexSBAPointXYZ, VertexSim3Expmap>()
  214. {
  215. }
  216. bool EdgeSim3ProjectXYZ::read(std::istream& is)
  217. {
  218. for (int i=0; i<2; i++)
  219. {
  220. is >> _measurement[i];
  221. }
  222. for (int i=0; i<2; i++)
  223. for (int j=i; j<2; j++) {
  224. is >> information()(i,j);
  225. if (i!=j)
  226. information()(j,i)=information()(i,j);
  227. }
  228. return true;
  229. }
  230. bool EdgeSim3ProjectXYZ::write(std::ostream& os) const
  231. {
  232. for (int i=0; i<2; i++){
  233. os << _measurement[i] << " ";
  234. }
  235. for (int i=0; i<2; i++)
  236. for (int j=i; j<2; j++){
  237. os << " " << information()(i,j);
  238. }
  239. return os.good();
  240. }
  241. EdgeInverseSim3ProjectXYZ::EdgeInverseSim3ProjectXYZ() :
  242. g2o::BaseBinaryEdge<2, Eigen::Vector2d, g2o::VertexSBAPointXYZ, VertexSim3Expmap>()
  243. {
  244. }
  245. bool EdgeInverseSim3ProjectXYZ::read(std::istream& is)
  246. {
  247. for (int i=0; i<2; i++)
  248. {
  249. is >> _measurement[i];
  250. }
  251. for (int i=0; i<2; i++)
  252. for (int j=i; j<2; j++) {
  253. is >> information()(i,j);
  254. if (i!=j)
  255. information()(j,i)=information()(i,j);
  256. }
  257. return true;
  258. }
  259. bool EdgeInverseSim3ProjectXYZ::write(std::ostream& os) const
  260. {
  261. for (int i=0; i<2; i++){
  262. os << _measurement[i] << " ";
  263. }
  264. for (int i=0; i<2; i++)
  265. for (int j=i; j<2; j++){
  266. os << " " << information()(i,j);
  267. }
  268. return os.good();
  269. }
  270. }