g2o
Loading...
Searching...
No Matches
edge_se3_lotsofxyz.cpp
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#include "edge_se3_lotsofxyz.h"
28
29#include <cassert>
30
31namespace g2o {
32
34 : BaseVariableSizedEdge<-1, VectorX>(), _observedPoints(0) {
35 resize(0);
36}
37
39 VertexSE3* pose = static_cast<VertexSE3*>(_vertices[0]);
40
41 Eigen::Transform<double, 3, 1> poseinv = pose->estimate().inverse();
42
43 for (unsigned int i = 0; i < _observedPoints; i++) {
44 VertexPointXYZ* xyz = static_cast<VertexPointXYZ*>(_vertices[1 + i]);
45 // const Vector3 &pt = xyz->estimate();
46 Vector3 m = poseinv * xyz->estimate();
47
48 unsigned int index = 3 * i;
49 _measurement[index] = m[0];
50 _measurement[index + 1] = m[1];
51 _measurement[index + 2] = m[2];
52 }
53 return true;
54}
55
57 VertexSE3* pose = static_cast<VertexSE3*>(_vertices[0]);
58
59 for (unsigned int i = 0; i < _observedPoints; i++) {
60 VertexPointXYZ* xyz = static_cast<VertexPointXYZ*>(_vertices[1 + i]);
61 Vector3 m = pose->estimate().inverse() * xyz->estimate();
62
63 unsigned int index = 3 * i;
64 _error[index] = m[0] - _measurement[index];
65 _error[index + 1] = m[1] - _measurement[index + 1];
66 _error[index + 2] = m[2] - _measurement[index + 2];
67 }
68}
69
72
73 // initialize Ji matrix
74 MatrixX Ji;
75 unsigned int rows = 3 * (_vertices.size() - 1);
76 Ji.resize(rows, 6);
77 Ji.fill(0);
78
79 Matrix3 poseRot = pose->estimate().inverse().rotation();
80
81 for (unsigned int i = 1; i < _vertices.size(); i++) {
83 Vector3 Zcam = pose->estimate().inverse() * point->estimate();
84
85 unsigned int index = 3 * (i - 1);
86
87 // Ji.block<3,3>(index,0) = -poseRot;
88 Ji.block<3, 3>(index, 0) = -Matrix3::Identity();
89
90 Ji(index, 3) = -0.0;
91 Ji(index, 4) = -2 * Zcam(2);
92 Ji(index, 5) = 2 * Zcam(1);
93
94 Ji(index + 1, 3) = 2 * Zcam(2);
95 Ji(index + 1, 4) = -0.0;
96 Ji(index + 1, 5) = -2 * Zcam(0);
97
98 Ji(index + 2, 3) = -2 * Zcam(1);
99 Ji(index + 2, 4) = 2 * Zcam(0);
100 Ji(index + 2, 5) = -0.0;
101
102 MatrixX Jj;
103 Jj.resize(rows, 3);
104 Jj.fill(0);
105 Jj.block<3, 3>(index, 0) = poseRot;
106
107 _jacobianOplus[i] = Jj;
108 }
109
110 _jacobianOplus[0] = Ji;
111}
112
113bool EdgeSE3LotsOfXYZ::read(std::istream& is) {
114 is >> _observedPoints;
115
117
118 // read the measurements
119 for (unsigned int i = 0; i < _observedPoints; i++) {
120 unsigned int index = 3 * i;
121 is >> _measurement[index] >> _measurement[index + 1] >>
122 _measurement[index + 2];
123 }
124
125 // read the information matrix
126 for (unsigned int i = 0; i < _observedPoints * 3; i++) {
127 // fill the "upper triangle" part of the matrix
128 for (unsigned int j = i; j < _observedPoints * 3; j++) {
129 is >> information()(i, j);
130 }
131
132 // fill the lower triangle part
133 for (unsigned int j = 0; j < i; j++) {
134 information()(i, j) = information()(j, i);
135 }
136 }
137 return true;
138}
139
140bool EdgeSE3LotsOfXYZ::write(std::ostream& os) const {
141 // write number of observed points
142 os << "|| " << _observedPoints;
143
144 // write measurements
145 for (unsigned int i = 0; i < _observedPoints; i++) {
146 unsigned int index = 3 * i;
147 os << " " << _measurement[index] << " " << _measurement[index + 1] << " "
148 << _measurement[index + 2];
149 }
150
151 // write information matrix
152 for (unsigned int i = 0; i < _observedPoints * 3; i++) {
153 for (unsigned int j = i; j < _observedPoints * 3; j++) {
154 os << " " << information()(i, j);
155 }
156 }
157 return os.good();
158}
159
161 OptimizableGraph::Vertex* toEstimate) {
162 (void)toEstimate;
163
164 assert(initialEstimatePossible(fixed, toEstimate) &&
165 "Bad vertices specified");
166
167 VertexSE3* pose = static_cast<VertexSE3*>(_vertices[0]);
168
169#ifdef _MSC_VER
170 std::vector<bool> estimate_this(_observedPoints, true);
171#else
172 bool estimate_this[_observedPoints];
173 for (unsigned int i = 0; i < _observedPoints; i++) {
174 estimate_this[i] = true;
175 }
176#endif
177
178 for (std::set<HyperGraph::Vertex*>::iterator it = fixed.begin();
179 it != fixed.end(); ++it) {
180 for (unsigned int i = 1; i < _vertices.size(); i++) {
181 VertexPointXYZ* vert = static_cast<VertexPointXYZ*>(_vertices[i]);
182 if (vert->id() == (*it)->id()) estimate_this[i - 1] = false;
183 }
184 }
185
186 for (unsigned int i = 1; i < _vertices.size(); i++) {
187 if (estimate_this[i - 1]) {
188 unsigned int index = 3 * (i - 1);
189 Vector3 submeas(_measurement[index], _measurement[index + 1],
190 _measurement[index + 2]);
191 VertexPointXYZ* vert = static_cast<VertexPointXYZ*>(_vertices[i]);
192 vert->setEstimate(pose->estimate() * submeas);
193 }
194 }
195}
196
198 const OptimizableGraph::VertexSet& fixed,
199 OptimizableGraph::Vertex* toEstimate) {
200 (void)toEstimate;
201
202 for (std::set<HyperGraph::Vertex*>::iterator it = fixed.begin();
203 it != fixed.end(); ++it) {
204 if (_vertices[0]->id() == (*it)->id()) {
205 return 1.0;
206 }
207 }
208
209 return -1.0;
210}
211
212} // namespace g2o
EIGEN_STRONG_INLINE const InformationType & information() const
information matrix of the constraint
Definition base_edge.h:107
Measurement _measurement
the measurement of the edge
Definition base_edge.h:146
ErrorVector _error
Definition base_edge.h:149
base class to represent an edge connecting an arbitrary number of nodes
std::vector< JacobianType > _jacobianOplus
jacobians of the edge (w.r.t. oplus)
const EstimateType & estimate() const
return the current estimate of the vertex
void setEstimate(const EstimateType &et)
set the estimate for the vertex also calls updateCache()
virtual void initialEstimate(const OptimizableGraph::VertexSet &, OptimizableGraph::Vertex *)
virtual bool write(std::ostream &os) const
write the vertex to a stream
virtual double initialEstimatePossible(const OptimizableGraph::VertexSet &, OptimizableGraph::Vertex *)
void setSize(int vertices)
virtual bool read(std::istream &is)
read the vertex from a stream, i.e., the internal state of the vertex
virtual bool setMeasurementFromState()
VertexContainer _vertices
int id() const
returns the id
std::set< Vertex * > VertexSet
A general case Vertex for optimization.
Vertex for a tracked point in space.
3D pose Vertex, represented as an Isometry3
Definition vertex_se3.h:50
VectorN< 3 > Vector3
Definition eigen_types.h:51
MatrixN< Eigen::Dynamic > MatrixX
Definition eigen_types.h:74
MatrixN< 3 > Matrix3
Definition eigen_types.h:72
VectorN< Eigen::Dynamic > VectorX
Definition eigen_types.h:55