g2o
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  Vector3D rho;
41  this->robustKernel()->robustify(error, rho);
42  Eigen::Matrix<double, D, 1, Eigen::ColMajor> 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 JacobianType& 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 JacobianType& 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 }
223 
224 
225 // PARTIAL TEMPLATE SPECIALIZATION
226 
227 template <typename E>
228 void BaseMultiEdge<-1, E>::constructQuadraticForm()
229 {
230  if (this->robustKernel()) {
231  double error = this->chi2();
232  Vector3D rho;
233  this->robustKernel()->robustify(error, rho);
234  Eigen::Matrix<double, Eigen::Dynamic, 1, Eigen::ColMajor> omega_r = - _information * _error;
235  omega_r *= rho[1];
236  computeQuadraticForm(this->robustInformation(rho), omega_r);
237  } else {
238  computeQuadraticForm(_information, - _information * _error);
239  }
240 }
241 
242 
243 template <typename E>
244 void BaseMultiEdge<-1, E>::linearizeOplus(JacobianWorkspace& jacobianWorkspace)
245 {
246  for (size_t i = 0; i < _vertices.size(); ++i) {
247  OptimizableGraph::Vertex* v = static_cast<OptimizableGraph::Vertex*>(_vertices[i]);
248  assert(v->dimension() >= 0);
249  new (&_jacobianOplus[i]) JacobianType(jacobianWorkspace.workspaceForVertex(i), _dimension, v->dimension());
250  }
251  linearizeOplus();
252 }
253 
254 template <typename E>
255 void BaseMultiEdge<-1, E>::linearizeOplus()
256 {
257 #ifdef G2O_OPENMP
258  for (size_t i = 0; i < _vertices.size(); ++i) {
259  OptimizableGraph::Vertex* v = static_cast<OptimizableGraph::Vertex*>(_vertices[i]);
260  v->lockQuadraticForm();
261  }
262 #endif
263 
264  const double delta = 1e-9;
265  const double scalar = 1.0 / (2*delta);
266  ErrorVector errorBak;
267  ErrorVector errorBeforeNumeric = _error;
268 
269  for (size_t i = 0; i < _vertices.size(); ++i) {
270  //Xi - estimate the jacobian numerically
271  OptimizableGraph::Vertex* vi = static_cast<OptimizableGraph::Vertex*>(_vertices[i]);
272 
273  if (vi->fixed())
274  continue;
275 
276  const int vi_dim = vi->dimension();
277  assert(vi_dim >= 0);
278 #ifdef _MSC_VER
279  double* add_vi = new double[vi_dim];
280 #else
281  double add_vi[vi_dim];
282 #endif
283  std::fill(add_vi, add_vi + vi_dim, 0.0);
284  assert(_dimension >= 0);
285  assert(_jacobianOplus[i].rows() == _dimension && _jacobianOplus[i].cols() == vi_dim && "jacobian cache dimension does not match");
286  _jacobianOplus[i].resize(_dimension, vi_dim);
287  // add small step along the unit vector in each dimension
288  for (int d = 0; d < vi_dim; ++d) {
289  vi->push();
290  add_vi[d] = delta;
291  vi->oplus(add_vi);
292  computeError();
293  errorBak = _error;
294  vi->pop();
295  vi->push();
296  add_vi[d] = -delta;
297  vi->oplus(add_vi);
298  computeError();
299  errorBak -= _error;
300  vi->pop();
301  add_vi[d] = 0.0;
302 
303  _jacobianOplus[i].col(d) = scalar * errorBak;
304  } // end dimension
305 #ifdef _MSC_VER
306  delete[] add_vi;
307 #endif
308  }
309  _error = errorBeforeNumeric;
310 
311 #ifdef G2O_OPENMP
312  for (int i = (int)(_vertices.size()) - 1; i >= 0; --i) {
313  OptimizableGraph::Vertex* v = static_cast<OptimizableGraph::Vertex*>(_vertices[i]);
314  v->unlockQuadraticForm();
315  }
316 #endif
317 
318 }
319 
320 template <typename E>
321 void BaseMultiEdge<-1, E>::mapHessianMemory(double* d, int i, int j, bool rowMajor)
322 {
323  int idx = internal::computeUpperTriangleIndex(i, j);
324  assert(idx < (int)_hessian.size());
325  OptimizableGraph::Vertex* vi = static_cast<OptimizableGraph::Vertex*>(HyperGraph::Edge::vertex(i));
326  OptimizableGraph::Vertex* vj = static_cast<OptimizableGraph::Vertex*>(HyperGraph::Edge::vertex(j));
327  assert(vi->dimension() >= 0);
328  assert(vj->dimension() >= 0);
329  HessianHelper& h = _hessian[idx];
330  if (rowMajor) {
331  if (h.matrix.data() != d || h.transposed != rowMajor)
332  new (&h.matrix) HessianBlockType(d, vj->dimension(), vi->dimension());
333  } else {
334  if (h.matrix.data() != d || h.transposed != rowMajor)
335  new (&h.matrix) HessianBlockType(d, vi->dimension(), vj->dimension());
336  }
337  h.transposed = rowMajor;
338 }
339 
340 template <typename E>
341 void BaseMultiEdge<-1, E>::resize(size_t size)
342 {
343  BaseEdge<-1,E>::resize(size);
344  int n = (int)_vertices.size();
345  int maxIdx = (n * (n-1))/2;
346  assert(maxIdx >= 0);
347  _hessian.resize(maxIdx);
348  _jacobianOplus.resize(size, JacobianType(0,0,0));
349 }
350 
351 template <typename E>
352 bool BaseMultiEdge<-1, E>::allVerticesFixed() const
353 {
354  for (size_t i = 0; i < _vertices.size(); ++i) {
355  if (!static_cast<const OptimizableGraph::Vertex*> (_vertices[i])->fixed()) {
356  return false;
357  }
358  }
359  return true;
360 }
361 
362 template <typename E>
363 void BaseMultiEdge<-1, E>::computeQuadraticForm(const InformationType& omega, const ErrorVector& weightedError)
364 {
365  for (size_t i = 0; i < _vertices.size(); ++i) {
366  OptimizableGraph::Vertex* from = static_cast<OptimizableGraph::Vertex*>(_vertices[i]);
367  bool istatus = !(from->fixed());
368 
369  if (istatus) {
370  const JacobianType& A = _jacobianOplus[i];
371 
372  MatrixXD AtO = A.transpose() * omega;
373  int fromDim = from->dimension();
374  assert(fromDim >= 0);
375  Eigen::Map<MatrixXD> fromMap(from->hessianData(), fromDim, fromDim);
376  Eigen::Map<VectorXD> fromB(from->bData(), fromDim);
377 
378  // ii block in the hessian
379 #ifdef G2O_OPENMP
380  from->lockQuadraticForm();
381 #endif
382  fromMap.noalias() += AtO * A;
383  fromB.noalias() += A.transpose() * weightedError;
384 
385  // compute the off-diagonal blocks ij for all j
386  for (size_t j = i+1; j < _vertices.size(); ++j) {
387  OptimizableGraph::Vertex* to = static_cast<OptimizableGraph::Vertex*>(_vertices[j]);
388 #ifdef G2O_OPENMP
389  to->lockQuadraticForm();
390 #endif
391  bool jstatus = !(to->fixed());
392  if (jstatus) {
393  const JacobianType& B = _jacobianOplus[j];
394  int idx = internal::computeUpperTriangleIndex(i, j);
395  assert(idx < (int)_hessian.size());
396  HessianHelper& hhelper = _hessian[idx];
397  if (hhelper.transposed) { // we have to write to the block as transposed
398  hhelper.matrix.noalias() += B.transpose() * AtO.transpose();
399  } else {
400  hhelper.matrix.noalias() += AtO * B;
401  }
402  }
403 #ifdef G2O_OPENMP
404  to->unlockQuadraticForm();
405 #endif
406  }
407 
408 #ifdef G2O_OPENMP
409  from->unlockQuadraticForm();
410 #endif
411  }
412 
413  }
414 }
int computeUpperTriangleIndex(int i, int j)
Eigen::Matrix< double, 3, 1, Eigen::ColMajor > Vector3D
Definition: eigen_types.h:46
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > MatrixXD
Definition: eigen_types.h:63