g2o
types_sba.h
Go to the documentation of this file.
1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 Kurt Konolige
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_SBA_TYPES
28 #define G2O_SBA_TYPES
29 
30 #include "g2o/core/base_vertex.h"
33 #include "sbacam.h"
34 #include <Eigen/Geometry>
35 #include <iostream>
36 
37 #include "g2o_types_sba_api.h"
38 
39 namespace g2o {
40 
45 class G2O_TYPES_SBA_API VertexIntrinsics : public BaseVertex<4, Eigen::Matrix<double, 5, 1, Eigen::ColMajor> >
46 {
47  public:
48  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
50  virtual bool read(std::istream& is);
51  virtual bool write(std::ostream& os) const;
52 
53  virtual void setToOriginImpl() {
54  _estimate << 1., 1., 0.5, 0.5, 0.1;
55  }
56 
57  virtual void oplusImpl(const double* update)
58  {
59  _estimate.head<4>() += Vector4D(update);
60  }
61  };
62 
71  class G2O_TYPES_SBA_API VertexCam : public BaseVertex<6, SBACam>
72 {
73  public:
75  VertexCam();
76 
77  virtual bool read(std::istream& is);
78  virtual bool write(std::ostream& os) const;
79 
80  virtual void setToOriginImpl() {
81  _estimate = SBACam();
82  }
83 
84  virtual void setEstimate(const SBACam& cam){
86  _estimate.setTransform();
87  _estimate.setProjection();
88  _estimate.setDr();
89  }
90 
91  virtual void oplusImpl(const double* update)
92  {
93  Eigen::Map<const Vector6d> v(update);
94  _estimate.update(v);
95  _estimate.setTransform();
96  _estimate.setProjection();
97  _estimate.setDr();
98  }
99 
100 
101  virtual bool setEstimateDataImpl(const double* est){
102  Eigen::Map <const Vector7d> v(est);
103  _estimate.fromVector(v);
104  return true;
105  }
106 
107  virtual bool getEstimateData(double* est) const{
108  Eigen::Map <Vector7d> v(est);
109  v = estimate().toVector();
110  return true;
111  }
112 
113  virtual int estimateDimension() const {
114  return 7;
115  }
116 
117  virtual bool setMinimalEstimateDataImpl(const double* est){
118  Eigen::Map<const Vector6d> v(est);
119  _estimate.fromMinimalVector(v);
120  return true;
121  }
122 
123  virtual bool getMinimalEstimateData(double* est) const{
124  Eigen::Map<Vector6d> v(est);
125  v = _estimate.toMinimalVector();
126  return true;
127  }
128 
129  virtual int minimalEstimateDimension() const {
130  return 6;
131  }
132  };
133 
137  class G2O_TYPES_SBA_API VertexSBAPointXYZ : public BaseVertex<3, Vector3D>
138 {
139  public:
140  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
142  virtual bool read(std::istream& is);
143  virtual bool write(std::ostream& os) const;
144 
145  virtual void setToOriginImpl() {
146  _estimate.fill(0.);
147  }
148 
149  virtual void oplusImpl(const double* update)
150  {
151  Eigen::Map<const Vector3D> v(update);
152  _estimate += v;
153  }
154 };
155 
156 
157 // monocular projection
158 // first two args are the measurement type, second two the connection classes
159  class G2O_TYPES_SBA_API EdgeProjectP2MC : public BaseBinaryEdge<2, Vector2D, VertexSBAPointXYZ, VertexCam>
160 {
161  public:
162  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
163  EdgeProjectP2MC();
164  virtual bool read(std::istream& is);
165  virtual bool write(std::ostream& os) const;
166 
167  // return the error estimate as a 2-vector
169  {
170  // from <Point> to <Cam>
171  const VertexSBAPointXYZ *point = static_cast<const VertexSBAPointXYZ*>(_vertices[0]);
172  const VertexCam *cam = static_cast<const VertexCam*>(_vertices[1]);
173 
174  // calculate the projection
175  const Vector3D &pt = point->estimate();
176  Vector4D ppt(pt(0),pt(1),pt(2),1.0);
177  Vector3D p = cam->estimate().w2i * ppt;
178  Vector2D perr;
179  perr = p.head<2>()/p(2);
180  // std::cout << std::endl << "CAM " << cam->estimate() << std::endl;
181  // std::cout << "POINT " << pt.transpose() << std::endl;
182  // std::cout << "PROJ " << p.transpose() << std::endl;
183  // std::cout << "CPROJ " << perr.transpose() << std::endl;
184  // std::cout << "MEAS " << _measurement.transpose() << std::endl;
185 
186  // error, which is backwards from the normal observed - calculated
187  // _measurement is the measured projection
188  _error = perr - _measurement;
189  // std::cerr << _error.x() << " " << _error.y() << " " << chi2() << std::endl;
190  }
191 
192  // jacobian
193  virtual void linearizeOplus();
194 };
195 
196 // stereo projection
197 // first two args are the measurement type, second two the connection classes
198  class G2O_TYPES_SBA_API EdgeProjectP2SC : public BaseBinaryEdge<3, Vector3D, VertexSBAPointXYZ, VertexCam>
199 {
200  public:
201  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
202  EdgeProjectP2SC();
203  virtual bool read(std::istream& is);
204  virtual bool write(std::ostream& os) const;
205 
206  // return the error estimate as a 2-vector
208  {
209  // from <Point> to <Cam>
210  const VertexSBAPointXYZ *point = static_cast<const VertexSBAPointXYZ*>(_vertices[0]);
211  VertexCam *cam = static_cast<VertexCam*>(_vertices[1]);
212 
213  // calculate the projection
214  Vector3D kp;
215  Vector4D pt;
216  pt.head<3>() = point->estimate();
217  pt(3) = 1.0;
218  const SBACam& nd = cam->estimate();
219  // these should be already ok
220  /* nd.setTransform(); */
221  /* nd.setProjection(); */
222  /* nd.setDr(); */
223 
224  Vector3D p1 = nd.w2i * pt;
225  Vector3D p2 = nd.w2n * pt;
226  Vector3D pb(nd.baseline,0,0);
227 
228  double invp1 = 1.0/p1(2);
229  kp.head<2>() = p1.head<2>()*invp1;
230 
231  // right camera px
232  p2 = nd.Kcam*(p2-pb);
233  kp(2) = p2(0)/p2(2);
234 
235  // std::cout << std::endl << "CAM " << cam->estimate() << std::endl;
236  // std::cout << "POINT " << pt.transpose() << std::endl;
237  // std::cout << "PROJ " << p1.transpose() << std::endl;
238  // std::cout << "PROJ " << p2.transpose() << std::endl;
239  // std::cout << "CPROJ " << kp.transpose() << std::endl;
240  // std::cout << "MEAS " << _measurement.transpose() << std::endl;
241 
242  // error, which is backwards from the normal observed - calculated
243  // _measurement is the measured projection
244  _error = kp - _measurement;
245  }
246 
247  // jacobian
248  virtual void linearizeOplus();
249 
250 };
251 
252 // monocular projection with parameter calibration
253 // first two args are the measurement type, second two the connection classes
255 {
256  public:
257  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
259  virtual bool read(std::istream& is);
260  virtual bool write(std::ostream& os) const;
261 
262  // return the error estimate as a 2-vector
264  {
265  // from <Point> to <Cam>, the intrinsics in KCam should be already set!
266  const VertexSBAPointXYZ *point = static_cast<const VertexSBAPointXYZ*>(_vertices[0]);
267  VertexCam *cam = static_cast<VertexCam*>(_vertices[1]);
268  // calculate the projection
269  const Vector3D &pt = point->estimate();
270  Vector4D ppt(pt(0),pt(1),pt(2),1.0);
271  Vector3D p = cam->estimate().w2i * ppt;
272  Vector2D perr = p.head<2>()/p(2);
273  _error = perr - _measurement;
274  }
275 
276  // jacobian
277  virtual void linearizeOplus();
278 
279 };
280 
281 
285  class G2O_TYPES_SBA_API EdgeSBACam : public BaseBinaryEdge<6, SE3Quat, VertexCam, VertexCam>
286 {
287  public:
289  EdgeSBACam();
290  virtual bool read(std::istream& is);
291  virtual bool write(std::ostream& os) const;
293  {
294  const VertexCam* v1 = dynamic_cast<const VertexCam*>(_vertices[0]);
295  const VertexCam* v2 = dynamic_cast<const VertexCam*>(_vertices[1]);
296  SE3Quat delta = _inverseMeasurement * (v1->estimate().inverse()*v2->estimate());
297  _error[0]=delta.translation().x();
298  _error[1]=delta.translation().y();
299  _error[2]=delta.translation().z();
300  _error[3]=delta.rotation().x();
301  _error[4]=delta.rotation().y();
302  _error[5]=delta.rotation().z();
303  }
304 
305  virtual void setMeasurement(const SE3Quat& meas){
306  _measurement=meas;
307  _inverseMeasurement=meas.inverse();
308  }
309 
311  virtual void initialEstimate(const OptimizableGraph::VertexSet& from, OptimizableGraph::Vertex* to);
312 
313  virtual bool setMeasurementData(const double* d){
314  Eigen::Map<const Vector7d> v(d);
315  _measurement.fromVector(v);
316  _inverseMeasurement = _measurement.inverse();
317  return true;
318  }
319 
320  virtual bool getMeasurementData(double* d) const{
321  Eigen::Map<Vector7d> v(d);
322  v = _measurement.toVector();
323  return true;
324  }
325 
326  virtual int measurementDimension() const {return 7;}
327 
328  virtual bool setMeasurementFromState();
329 
330  protected:
332 };
333 
334 
338  class G2O_TYPES_SBA_API EdgeSBAScale : public BaseBinaryEdge<1, double, VertexCam, VertexCam>
339 {
340  public:
341  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
342  EdgeSBAScale();
343  virtual bool read(std::istream& is);
344  virtual bool write(std::ostream& os) const;
346  {
347  const VertexCam* v1 = dynamic_cast<const VertexCam*>(_vertices[0]);
348  const VertexCam* v2 = dynamic_cast<const VertexCam*>(_vertices[1]);
349  Vector3D dt=v2->estimate().translation()-v1->estimate().translation();
350  _error[0] = _measurement - dt.norm();
351  }
352  virtual void setMeasurement(const double& m){
353  _measurement = m;
354  }
356  virtual void initialEstimate(const OptimizableGraph::VertexSet& from_, OptimizableGraph::Vertex* to_);
357 };
358 
359 
360 
361 } // end namespace
362 
363 #endif // SBA_TYPES
virtual int minimalEstimateDimension() const
Definition: types_sba.h:129
Eigen::Matrix< double, 2, 1, Eigen::ColMajor > Vector2D
Definition: eigen_types.h:45
#define G2O_TYPES_SBA_API
double baseline
Definition: sbacam.h:64
edge between two SBAcam that specifies the distance between them
Definition: types_sba.h:338
virtual bool setMeasurementData(const double *d)
Definition: types_sba.h:313
Eigen::Matrix< double, 3, 1, Eigen::ColMajor > Vector3D
Definition: eigen_types.h:46
virtual bool setMinimalEstimateDataImpl(const double *est)
Definition: types_sba.h:117
Eigen::Matrix< double, 4, 1, Eigen::ColMajor > Vector4D
Definition: eigen_types.h:47
virtual bool getMeasurementData(double *d) const
Definition: types_sba.h:320
std::set< Vertex * > VertexSet
Definition: hyper_graph.h:136
Templatized BaseVertex.
Definition: base_vertex.h:50
virtual int measurementDimension() const
Definition: types_sba.h:326
virtual void oplusImpl(const double *update)
Definition: types_sba.h:149
Eigen::Matrix3d Kcam
Definition: sbacam.h:63
SE3Quat inverse() const
Definition: se3quat.h:120
virtual void setEstimate(const SBACam &cam)
Definition: types_sba.h:84
Point vertex, XYZ.
Definition: types_sba.h:137
virtual void setToOriginImpl()
sets the node to the origin (used in the multilevel stuff)
Definition: types_sba.h:80
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
Definition: types_sba.h:288
SBACam Vertex, (x,y,z,qw,qx,qy,qz) the parameterization for the increments constructed is a 6d vector...
Definition: types_sba.h:71
3D edge between two SBAcam
Definition: types_sba.h:285
virtual bool getMinimalEstimateData(double *est) const
Definition: types_sba.h:123
Eigen::Matrix< double, 3, 4 > w2i
Definition: sbacam.h:68
void computeError()
Definition: types_sba.h:345
SE3Quat _inverseMeasurement
Definition: types_sba.h:331
base class to represent an edge connecting an arbitrary number of nodes
virtual double initialEstimatePossible(const OptimizableGraph::VertexSet &, OptimizableGraph::Vertex *)
Definition: types_sba.h:355
const Eigen::Quaterniond & rotation() const
Definition: se3quat.h:97
virtual bool setEstimateDataImpl(const double *est)
Definition: types_sba.h:101
void computeError()
Definition: types_sba.h:292
void setEstimate(const EstimateType &et)
set the estimate for the vertex also calls updateCache()
Definition: base_vertex.h:101
A general case Vertex for optimization.
virtual bool getEstimateData(double *est) const
Definition: types_sba.h:107
virtual void setToOriginImpl()
sets the node to the origin (used in the multilevel stuff)
Definition: types_sba.h:145
const EstimateType & estimate() const
return the current estimate of the vertex
Definition: base_vertex.h:99
virtual void setMeasurement(const double &m)
Definition: types_sba.h:352
virtual void setToOriginImpl()
sets the node to the origin (used in the multilevel stuff)
Definition: types_sba.h:53
virtual double initialEstimatePossible(const OptimizableGraph::VertexSet &, OptimizableGraph::Vertex *)
Definition: types_sba.h:310
virtual void oplusImpl(const double *update)
Definition: types_sba.h:57
Eigen::Matrix< double, 3, 4 > w2n
Definition: sbacam.h:67
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
Definition: types_sba.h:74
virtual int estimateDimension() const
Definition: types_sba.h:113
virtual void oplusImpl(const double *update)
Definition: types_sba.h:91
Vertex encoding the intrinsics of the camera fx, fy, cx, xy, baseline;.
Definition: types_sba.h:45
virtual void setMeasurement(const SE3Quat &meas)
Definition: types_sba.h:305
const Vector3D & translation() const
Definition: se3quat.h:93