base_binary_edge.hpp
Go to the documentation of this file.
1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, H. Strasdat, W. Burgard
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 template <int D, typename E, typename VertexXiType, typename VertexXjType>
28 OptimizableGraph::Vertex* BaseBinaryEdge<D, E, VertexXiType, VertexXjType>::createFrom(){
29  return new VertexXiType();
30 }
31 
32 template <int D, typename E, typename VertexXiType, typename VertexXjType>
33 OptimizableGraph::Vertex* BaseBinaryEdge<D, E, VertexXiType, VertexXjType>::createTo(){
34  return new VertexXjType();
35 }
36 
37 
38 template <int D, typename E, typename VertexXiType, typename VertexXjType>
39 void BaseBinaryEdge<D, E, VertexXiType, VertexXjType>::resize(size_t size)
40 {
41  if (size != 2) {
42  std::cerr << "WARNING, attempting to resize binary edge " << BaseEdge<D, E>::id() << " to " << size << std::endl;
43  }
44  BaseEdge<D, E>::resize(size);
45 }
46 
47 template <int D, typename E, typename VertexXiType, typename VertexXjType>
48 bool BaseBinaryEdge<D, E, VertexXiType, VertexXjType>::allVerticesFixed() const
49 {
50  return (static_cast<const VertexXiType*> (_vertices[0])->fixed() &&
51  static_cast<const VertexXjType*> (_vertices[1])->fixed());
52 }
53 
54 template <int D, typename E, typename VertexXiType, typename VertexXjType>
55 void BaseBinaryEdge<D, E, VertexXiType, VertexXjType>::constructQuadraticForm()
56 {
57  VertexXiType* from = static_cast<VertexXiType*>(_vertices[0]);
58  VertexXjType* to = static_cast<VertexXjType*>(_vertices[1]);
59 
60  // get the Jacobian of the nodes in the manifold domain
61  const JacobianXiOplusType& A = jacobianOplusXi();
62  const JacobianXjOplusType& B = jacobianOplusXj();
63 
64 
65  bool fromNotFixed = !(from->fixed());
66  bool toNotFixed = !(to->fixed());
67 
68  if (fromNotFixed || toNotFixed) {
69 #ifdef G2O_OPENMP
70  from->lockQuadraticForm();
71  to->lockQuadraticForm();
72 #endif
73  const InformationType& omega = _information;
74  Matrix<double, D, 1> omega_r = - omega * _error;
75  if (this->robustKernel() == 0) {
76  if (fromNotFixed) {
77  Matrix<double, VertexXiType::Dimension, D> AtO = A.transpose() * omega;
78  from->b().noalias() += A.transpose() * omega_r;
79  from->A().noalias() += AtO*A;
80  if (toNotFixed ) {
81  if (_hessianRowMajor) // we have to write to the block as transposed
82  _hessianTransposed.noalias() += B.transpose() * AtO.transpose();
83  else
84  _hessian.noalias() += AtO * B;
85  }
86  }
87  if (toNotFixed) {
88  to->b().noalias() += B.transpose() * omega_r;
89  to->A().noalias() += B.transpose() * omega * B;
90  }
91  } else { // robust (weighted) error according to some kernel
92  double error = this->chi2();
93  Eigen::Vector3d rho;
94  this->robustKernel()->robustify(error, rho);
95  InformationType weightedOmega = this->robustInformation(rho);
96  //std::cout << PVAR(rho.transpose()) << std::endl;
97  //std::cout << PVAR(weightedOmega) << std::endl;
98 
99  omega_r *= rho[1];
100  if (fromNotFixed) {
101  from->b().noalias() += A.transpose() * omega_r;
102  from->A().noalias() += A.transpose() * weightedOmega * A;
103  if (toNotFixed ) {
104  if (_hessianRowMajor) // we have to write to the block as transposed
105  _hessianTransposed.noalias() += B.transpose() * weightedOmega * A;
106  else
107  _hessian.noalias() += A.transpose() * weightedOmega * B;
108  }
109  }
110  if (toNotFixed) {
111  to->b().noalias() += B.transpose() * omega_r;
112  to->A().noalias() += B.transpose() * weightedOmega * B;
113  }
114  }
115 #ifdef G2O_OPENMP
116  to->unlockQuadraticForm();
117  from->unlockQuadraticForm();
118 #endif
119  }
120 }
121 
122 template <int D, typename E, typename VertexXiType, typename VertexXjType>
123 void BaseBinaryEdge<D, E, VertexXiType, VertexXjType>::linearizeOplus(JacobianWorkspace& jacobianWorkspace)
124 {
125  new (&_jacobianOplusXi) JacobianXiOplusType(jacobianWorkspace.workspaceForVertex(0), D, Di);
126  new (&_jacobianOplusXj) JacobianXjOplusType(jacobianWorkspace.workspaceForVertex(1), D, Dj);
127  linearizeOplus();
128 }
129 
130 template <int D, typename E, typename VertexXiType, typename VertexXjType>
131 void BaseBinaryEdge<D, E, VertexXiType, VertexXjType>::linearizeOplus()
132 {
133  VertexXiType* vi = static_cast<VertexXiType*>(_vertices[0]);
134  VertexXjType* vj = static_cast<VertexXjType*>(_vertices[1]);
135 
136  bool iNotFixed = !(vi->fixed());
137  bool jNotFixed = !(vj->fixed());
138 
139  if (!iNotFixed && !jNotFixed)
140  return;
141 
142 #ifdef G2O_OPENMP
143  vi->lockQuadraticForm();
144  vj->lockQuadraticForm();
145 #endif
146 
147  const double delta = 1e-9;
148  const double scalar = 1.0 / (2*delta);
149  ErrorVector errorBak;
150  ErrorVector errorBeforeNumeric = _error;
151 
152  if (iNotFixed) {
153  //Xi - estimate the jacobian numerically
154  double add_vi[VertexXiType::Dimension];
155  std::fill(add_vi, add_vi + VertexXiType::Dimension, 0.0);
156  // add small step along the unit vector in each dimension
157  for (int d = 0; d < VertexXiType::Dimension; ++d) {
158  vi->push();
159  add_vi[d] = delta;
160  vi->oplus(add_vi);
161  computeError();
162  errorBak = _error;
163  vi->pop();
164  vi->push();
165  add_vi[d] = -delta;
166  vi->oplus(add_vi);
167  computeError();
168  errorBak -= _error;
169  vi->pop();
170  add_vi[d] = 0.0;
171 
172  _jacobianOplusXi.col(d) = scalar * errorBak;
173  } // end dimension
174  }
175 
176  if (jNotFixed) {
177  //Xj - estimate the jacobian numerically
178  double add_vj[VertexXjType::Dimension];
179  std::fill(add_vj, add_vj + VertexXjType::Dimension, 0.0);
180  // add small step along the unit vector in each dimension
181  for (int d = 0; d < VertexXjType::Dimension; ++d) {
182  vj->push();
183  add_vj[d] = delta;
184  vj->oplus(add_vj);
185  computeError();
186  errorBak = _error;
187  vj->pop();
188  vj->push();
189  add_vj[d] = -delta;
190  vj->oplus(add_vj);
191  computeError();
192  errorBak -= _error;
193  vj->pop();
194  add_vj[d] = 0.0;
195 
196  _jacobianOplusXj.col(d) = scalar * errorBak;
197  }
198  } // end dimension
199 
200  _error = errorBeforeNumeric;
201 #ifdef G2O_OPENMP
202  vj->unlockQuadraticForm();
203  vi->unlockQuadraticForm();
204 #endif
205 }
206 
207 template <int D, typename E, typename VertexXiType, typename VertexXjType>
208 void BaseBinaryEdge<D, E, VertexXiType, VertexXjType>::mapHessianMemory(double* d, int i, int j, bool rowMajor)
209 {
210  (void) i; (void) j;
211  //assert(i == 0 && j == 1);
212  if (rowMajor) {
213  new (&_hessianTransposed) HessianBlockTransposedType(d, VertexXjType::Dimension, VertexXiType::Dimension);
214  } else {
215  new (&_hessian) HessianBlockType(d, VertexXiType::Dimension, VertexXjType::Dimension);
216  }
217  _hessianRowMajor = rowMajor;
218 }
d


orb_slam2_ros
Author(s):
autogenerated on Wed Apr 21 2021 02:53:05