g2o
Loading...
Searching...
No Matches
base_edge.h
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#ifndef G2O_BASE_EDGE_H
28#define G2O_BASE_EDGE_H
29
30#include <Eigen/Core>
31#include <iostream>
32#include <limits>
33#include <type_traits>
34
35#include "g2o/stuff/logger.h"
36#include "optimizable_graph.h"
37
38namespace g2o {
39
40namespace internal {
41
42#ifdef G2O_OPENMP
43struct QuadraticFormLock {
45 : _vertex(vertex) {
46 _vertex.lockQuadraticForm();
47 }
48 ~QuadraticFormLock() { _vertex.unlockQuadraticForm(); }
49
50 private:
52};
53#else
58#endif
59
65template <int D>
67 static constexpr int Dimension = D;
68 typedef Eigen::Matrix<double, D, 1, Eigen::ColMajor> ErrorVector;
69 typedef Eigen::Matrix<double, D, D, Eigen::ColMajor> InformationType;
70};
75template <>
76struct BaseEdgeTraits<-1> {
77 static constexpr int Dimension = -1;
78 typedef Eigen::Matrix<double, Eigen::Dynamic, 1, Eigen::ColMajor> ErrorVector;
79 typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>
81};
82
83} // namespace internal
84
85template <int D, typename E>
87 public:
89 typedef E Measurement;
92
94 BaseEdge& operator=(const BaseEdge&) = delete;
95 BaseEdge(const BaseEdge&) = delete;
96
97 virtual ~BaseEdge() {}
98
99 virtual double chi2() const { return _error.dot(information() * _error); }
100
101 virtual const double* errorData() const { return _error.data(); }
102 virtual double* errorData() { return _error.data(); }
103 const ErrorVector& error() const { return _error; }
104 ErrorVector& error() { return _error; }
105
107 EIGEN_STRONG_INLINE const InformationType& information() const {
108 return _information;
109 }
110 EIGEN_STRONG_INLINE InformationType& information() { return _information; }
114
115 virtual const double* informationData() const { return _information.data(); }
116 virtual double* informationData() { return _information.data(); }
117
119 EIGEN_STRONG_INLINE const Measurement& measurement() const {
120 return _measurement;
121 }
122 virtual void setMeasurement(const Measurement& m) { _measurement = m; }
123
124 virtual int rank() const { return _dimension; }
125
128 G2O_WARN(
129 "inititialEstimate() is not implemented, please give implementation in "
130 "your derived class");
131 }
132
138 template <int Dim = D>
139 typename std::enable_if<Dim == -1, void>::type setDimension(int dim) {
140 _dimension = dim;
141 _information.resize(dim, dim);
142 _error.resize(dim, 1);
143 }
144
145 protected:
151
157 InformationType result = rho[1] * _information;
158 // ErrorVector weightedError = _information * _error;
159 // result.noalias() += 2 * rho[2] * (weightedError *
160 // weightedError.transpose());
161 return result;
162 }
163
165 bool writeInformationMatrix(std::ostream& os) const {
166 for (int i = 0; i < information().rows(); ++i)
167 for (int j = i; j < information().cols(); ++j)
168 os << information()(i, j) << " ";
169 return os.good();
170 }
173 bool readInformationMatrix(std::istream& is) {
174 for (int i = 0; i < information().rows() && is.good(); ++i)
175 for (int j = i; j < information().cols() && is.good(); ++j) {
176 is >> information()(i, j);
177 if (i != j) information()(j, i) = information()(i, j);
178 }
179 return is.good() || is.eof();
180 }
182 bool writeParamIds(std::ostream& os) const {
183 for (auto id : _parameterIds) os << id << " ";
184 return os.good();
185 }
187 bool readParamIds(std::istream& is) {
188 for (size_t i = 0; i < numParameters(); ++i) {
189 int paramId;
190 is >> paramId;
191 setParameterId(i, paramId);
192 }
193 return is.good() || is.eof();
194 }
195
196 public:
197 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
198};
199
200} // end namespace g2o
201
202#endif
bool writeInformationMatrix(std::ostream &os) const
write the upper trinagular part of the information matrix into the stream
Definition base_edge.h:165
bool readInformationMatrix(std::istream &is)
Definition base_edge.h:173
virtual void initialEstimate(const OptimizableGraph::VertexSet &, OptimizableGraph::Vertex *)
Definition base_edge.h:126
bool readParamIds(std::istream &is)
reads the param IDs from the stream
Definition base_edge.h:187
ErrorVector & error()
Definition base_edge.h:104
EIGEN_STRONG_INLINE const Measurement & measurement() const
accessor functions for the measurement represented by the edge
Definition base_edge.h:119
EIGEN_STRONG_INLINE InformationType & information()
Definition base_edge.h:110
virtual void setMeasurement(const Measurement &m)
Definition base_edge.h:122
InformationType _information
Definition base_edge.h:147
BaseEdge & operator=(const BaseEdge &)=delete
virtual double * errorData()
Definition base_edge.h:102
EIGEN_STRONG_INLINE const InformationType & information() const
information matrix of the constraint
Definition base_edge.h:107
virtual int rank() const
Definition base_edge.h:124
virtual double chi2() const
Definition base_edge.h:99
internal::BaseEdgeTraits< D >::ErrorVector ErrorVector
Definition base_edge.h:90
virtual const double * errorData() const
returns the error vector cached after calling the computeError;
Definition base_edge.h:101
virtual ~BaseEdge()
Definition base_edge.h:97
void setInformation(const InformationType &information)
Definition base_edge.h:111
virtual double * informationData()
Definition base_edge.h:116
internal::BaseEdgeTraits< D >::InformationType InformationType
Definition base_edge.h:91
virtual const double * informationData() const
Definition base_edge.h:115
const ErrorVector & error() const
Definition base_edge.h:103
bool writeParamIds(std::ostream &os) const
write the param IDs that are potentially used by the edge
Definition base_edge.h:182
BaseEdge(const BaseEdge &)=delete
static constexpr int Dimension
Definition base_edge.h:88
InformationType robustInformation(const Vector3 &rho) const
Definition base_edge.h:156
std::enable_if< Dim==-1, void >::type setDimension(int dim)
Definition base_edge.h:139
Measurement _measurement
the measurement of the edge
Definition base_edge.h:146
ErrorVector _error
Definition base_edge.h:149
std::set< Vertex * > VertexSet
bool setParameterId(int argNum, int paramId)
A general case Vertex for optimization.
#define G2O_WARN(...)
Definition logger.h:88
VectorN< 3 > Vector3
Definition eigen_types.h:51
Eigen::Matrix< double, Eigen::Dynamic, 1, Eigen::ColMajor > ErrorVector
Definition base_edge.h:78
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > InformationType
Definition base_edge.h:80
Eigen::Matrix< double, D, 1, Eigen::ColMajor > ErrorVector
Definition base_edge.h:68
Eigen::Matrix< double, D, D, Eigen::ColMajor > InformationType
Definition base_edge.h:69
static constexpr int Dimension
Definition base_edge.h:67
QuadraticFormLock(OptimizableGraph::Vertex &)
Definition base_edge.h:55