base_multi_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 namespace internal {
28  inline int computeUpperTriangleIndex(int i, int j)
29  {
30  int elemsUpToCol = ((j-1) * j) / 2;
31  return elemsUpToCol + i;
32  }
33 }
34 
35 template <int D, typename E>
36 void BaseMultiEdge<D, E>::constructQuadraticForm()
37 {
38  if (this->robustKernel()) {
39  double error = this->chi2();
40  Eigen::Vector3d rho;
41  this->robustKernel()->robustify(error, rho);
42  Matrix<double, D, 1> omega_r = - _information * _error;
43  omega_r *= rho[1];
44  computeQuadraticForm(this->robustInformation(rho), omega_r);
45  } else {
46  computeQuadraticForm(_information, - _information * _error);
47  }
48 }
49 
50 
51 template <int D, typename E>
52 void BaseMultiEdge<D, E>::linearizeOplus(JacobianWorkspace& jacobianWorkspace)
53 {
54  for (size_t i = 0; i < _vertices.size(); ++i) {
55  OptimizableGraph::Vertex* v = static_cast<OptimizableGraph::Vertex*>(_vertices[i]);
56  assert(v->dimension() >= 0);
57  new (&_jacobianOplus[i]) JacobianType(jacobianWorkspace.workspaceForVertex(i), D, v->dimension());
58  }
59  linearizeOplus();
60 }
61 
62 template <int D, typename E>
63 void BaseMultiEdge<D, E>::linearizeOplus()
64 {
65 #ifdef G2O_OPENMP
66  for (size_t i = 0; i < _vertices.size(); ++i) {
67  OptimizableGraph::Vertex* v = static_cast<OptimizableGraph::Vertex*>(_vertices[i]);
68  v->lockQuadraticForm();
69  }
70 #endif
71 
72  const double delta = 1e-9;
73  const double scalar = 1.0 / (2*delta);
74  ErrorVector errorBak;
75  ErrorVector errorBeforeNumeric = _error;
76 
77  for (size_t i = 0; i < _vertices.size(); ++i) {
78  //Xi - estimate the jacobian numerically
79  OptimizableGraph::Vertex* vi = static_cast<OptimizableGraph::Vertex*>(_vertices[i]);
80 
81  if (vi->fixed())
82  continue;
83 
84  const int vi_dim = vi->dimension();
85  assert(vi_dim >= 0);
86 #ifdef _MSC_VER
87  double* add_vi = new double[vi_dim];
88 #else
89  double add_vi[vi_dim];
90 #endif
91  std::fill(add_vi, add_vi + vi_dim, 0.0);
92  assert(_dimension >= 0);
93  assert(_jacobianOplus[i].rows() == _dimension && _jacobianOplus[i].cols() == vi_dim && "jacobian cache dimension does not match");
94  _jacobianOplus[i].resize(_dimension, vi_dim);
95  // add small step along the unit vector in each dimension
96  for (int d = 0; d < vi_dim; ++d) {
97  vi->push();
98  add_vi[d] = delta;
99  vi->oplus(add_vi);
100  computeError();
101  errorBak = _error;
102  vi->pop();
103  vi->push();
104  add_vi[d] = -delta;
105  vi->oplus(add_vi);
106  computeError();
107  errorBak -= _error;
108  vi->pop();
109  add_vi[d] = 0.0;
110 
111  _jacobianOplus[i].col(d) = scalar * errorBak;
112  } // end dimension
113 #ifdef _MSC_VER
114  delete[] add_vi;
115 #endif
116  }
117  _error = errorBeforeNumeric;
118 
119 #ifdef G2O_OPENMP
120  for (int i = (int)(_vertices.size()) - 1; i >= 0; --i) {
121  OptimizableGraph::Vertex* v = static_cast<OptimizableGraph::Vertex*>(_vertices[i]);
122  v->unlockQuadraticForm();
123  }
124 #endif
125 
126 }
127 
128 template <int D, typename E>
129 void BaseMultiEdge<D, E>::mapHessianMemory(double* d, int i, int j, bool rowMajor)
130 {
131  int idx = internal::computeUpperTriangleIndex(i, j);
132  assert(idx < (int)_hessian.size());
133  OptimizableGraph::Vertex* vi = static_cast<OptimizableGraph::Vertex*>(HyperGraph::Edge::vertex(i));
134  OptimizableGraph::Vertex* vj = static_cast<OptimizableGraph::Vertex*>(HyperGraph::Edge::vertex(j));
135  assert(vi->dimension() >= 0);
136  assert(vj->dimension() >= 0);
137  HessianHelper& h = _hessian[idx];
138  if (rowMajor) {
139  if (h.matrix.data() != d || h.transposed != rowMajor)
140  new (&h.matrix) HessianBlockType(d, vj->dimension(), vi->dimension());
141  } else {
142  if (h.matrix.data() != d || h.transposed != rowMajor)
143  new (&h.matrix) HessianBlockType(d, vi->dimension(), vj->dimension());
144  }
145  h.transposed = rowMajor;
146 }
147 
148 template <int D, typename E>
149 void BaseMultiEdge<D, E>::resize(size_t size)
150 {
151  BaseEdge<D,E>::resize(size);
152  int n = (int)_vertices.size();
153  int maxIdx = (n * (n-1))/2;
154  assert(maxIdx >= 0);
155  _hessian.resize(maxIdx);
156  _jacobianOplus.resize(size, JacobianType(0,0,0));
157 }
158 
159 template <int D, typename E>
160 bool BaseMultiEdge<D, E>::allVerticesFixed() const
161 {
162  for (size_t i = 0; i < _vertices.size(); ++i) {
163  if (!static_cast<const OptimizableGraph::Vertex*> (_vertices[i])->fixed()) {
164  return false;
165  }
166  }
167  return true;
168 }
169 
170 template <int D, typename E>
171 void BaseMultiEdge<D, E>::computeQuadraticForm(const InformationType& omega, const ErrorVector& weightedError)
172 {
173  for (size_t i = 0; i < _vertices.size(); ++i) {
174  OptimizableGraph::Vertex* from = static_cast<OptimizableGraph::Vertex*>(_vertices[i]);
175  bool istatus = !(from->fixed());
176 
177  if (istatus) {
178  const MatrixXd& A = _jacobianOplus[i];
179 
180  MatrixXd AtO = A.transpose() * omega;
181  int fromDim = from->dimension();
182  assert(fromDim >= 0);
183  Eigen::Map<MatrixXd> fromMap(from->hessianData(), fromDim, fromDim);
184  Eigen::Map<VectorXd> fromB(from->bData(), fromDim);
185 
186  // ii block in the hessian
187 #ifdef G2O_OPENMP
188  from->lockQuadraticForm();
189 #endif
190  fromMap.noalias() += AtO * A;
191  fromB.noalias() += A.transpose() * weightedError;
192 
193  // compute the off-diagonal blocks ij for all j
194  for (size_t j = i+1; j < _vertices.size(); ++j) {
195  OptimizableGraph::Vertex* to = static_cast<OptimizableGraph::Vertex*>(_vertices[j]);
196 #ifdef G2O_OPENMP
197  to->lockQuadraticForm();
198 #endif
199  bool jstatus = !(to->fixed());
200  if (jstatus) {
201  const MatrixXd& B = _jacobianOplus[j];
202  int idx = internal::computeUpperTriangleIndex(i, j);
203  assert(idx < (int)_hessian.size());
204  HessianHelper& hhelper = _hessian[idx];
205  if (hhelper.transposed) { // we have to write to the block as transposed
206  hhelper.matrix.noalias() += B.transpose() * AtO.transpose();
207  } else {
208  hhelper.matrix.noalias() += AtO * B;
209  }
210  }
211 #ifdef G2O_OPENMP
212  to->unlockQuadraticForm();
213 #endif
214  }
215 
216 #ifdef G2O_OPENMP
217  from->unlockQuadraticForm();
218 #endif
219  }
220 
221  }
222 }
d
int computeUpperTriangleIndex(int i, int j)


orb_slam2_with_maps_odom
Author(s): teng zhang
autogenerated on Fri Sep 25 2020 03:24:47