g2o
Loading...
Searching...
No Matches
edge_project_p2mc.cpp
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#include "edge_project_p2mc.h"
28
29namespace g2o {
30
31// point to camera projection, monocular
36
37bool EdgeProjectP2MC::read(std::istream& is) {
38 // measured keypoint
40 return readInformationMatrix(is);
41}
42
43bool EdgeProjectP2MC::write(std::ostream& os) const {
46 return os.good();
47}
48
50 // from <Point> to <Cam>
51 const VertexPointXYZ* point =
52 static_cast<const VertexPointXYZ*>(_vertices[0]);
53 const VertexCam* cam = static_cast<const VertexCam*>(_vertices[1]);
54
55 // calculate the projection
56 const Vector3& pt = point->estimate();
57 Vector4 ppt(pt(0), pt(1), pt(2), 1);
58 Vector3 p = cam->estimate().w2i * ppt;
59 Vector2 perr;
60 perr = p.head<2>() / p(2);
61
62 // error, which is backwards from the normal observed - calculated
63 // _measurement is the measured projection
64 _error = perr - _measurement;
65}
66
68 VertexCam* vc = static_cast<VertexCam*>(_vertices[1]);
69 const SBACam& cam = vc->estimate();
70
71 VertexPointXYZ* vp = static_cast<VertexPointXYZ*>(_vertices[0]);
72 Vector4 pt, trans;
73 pt.head<3>() = vp->estimate();
74 pt(3) = 1.0;
75 trans.head<3>() = cam.translation();
76 trans(3) = 1.0;
77
78 // first get the world point in camera coords
79 Vector3 pc = cam.w2n * pt;
80
81 // Jacobians wrt camera parameters
82 // set d(quat-x) values [ pz*dpx/dx - px*dpz/dx ] / pz^2
83 double px = pc(0);
84 double py = pc(1);
85 double pz = pc(2);
86 double ipz2 = 1.0 / (pz * pz);
87 if (g2o_isnan(ipz2)) {
88 std::cout << "[SetJac] infinite jac" << std::endl;
89 abort();
90 }
91
92 double ipz2fx = ipz2 * cam.Kcam(0, 0); // Fx
93 double ipz2fy = ipz2 * cam.Kcam(1, 1); // Fy
94
95 // check for local vars
96 Vector3 pwt =
97 (pt - trans)
98 .head<3>(); // transform translations, use differential rotation
99
100 // dx
101 Vector3 dp = cam.dRdx * pwt; // dR'/dq * [pw - t]
102 _jacobianOplusXj(0, 3) = (pz * dp(0) - px * dp(2)) * ipz2fx;
103 _jacobianOplusXj(1, 3) = (pz * dp(1) - py * dp(2)) * ipz2fy;
104 // dy
105 dp = cam.dRdy * pwt; // dR'/dq * [pw - t]
106 _jacobianOplusXj(0, 4) = (pz * dp(0) - px * dp(2)) * ipz2fx;
107 _jacobianOplusXj(1, 4) = (pz * dp(1) - py * dp(2)) * ipz2fy;
108 // dz
109 dp = cam.dRdz * pwt; // dR'/dq * [pw - t]
110 _jacobianOplusXj(0, 5) = (pz * dp(0) - px * dp(2)) * ipz2fx;
111 _jacobianOplusXj(1, 5) = (pz * dp(1) - py * dp(2)) * ipz2fy;
112
113 // set d(t) values [ pz*dpx/dx - px*dpz/dx ] / pz^2
114 dp = -cam.w2n.col(0); // dpc / dx
115 _jacobianOplusXj(0, 0) = (pz * dp(0) - px * dp(2)) * ipz2fx;
116 _jacobianOplusXj(1, 0) = (pz * dp(1) - py * dp(2)) * ipz2fy;
117 dp = -cam.w2n.col(1); // dpc / dy
118 _jacobianOplusXj(0, 1) = (pz * dp(0) - px * dp(2)) * ipz2fx;
119 _jacobianOplusXj(1, 1) = (pz * dp(1) - py * dp(2)) * ipz2fy;
120 dp = -cam.w2n.col(2); // dpc / dz
121 _jacobianOplusXj(0, 2) = (pz * dp(0) - px * dp(2)) * ipz2fx;
122 _jacobianOplusXj(1, 2) = (pz * dp(1) - py * dp(2)) * ipz2fy;
123
124 // Jacobians wrt point parameters
125 // set d(t) values [ pz*dpx/dx - px*dpz/dx ] / pz^2
126 dp = cam.w2n.col(0); // dpc / dx
127 _jacobianOplusXi(0, 0) = (pz * dp(0) - px * dp(2)) * ipz2fx;
128 _jacobianOplusXi(1, 0) = (pz * dp(1) - py * dp(2)) * ipz2fy;
129 dp = cam.w2n.col(1); // dpc / dy
130 _jacobianOplusXi(0, 1) = (pz * dp(0) - px * dp(2)) * ipz2fx;
131 _jacobianOplusXi(1, 1) = (pz * dp(1) - py * dp(2)) * ipz2fy;
132 dp = cam.w2n.col(2); // dpc / dz
133 _jacobianOplusXi(0, 2) = (pz * dp(0) - px * dp(2)) * ipz2fx;
134 _jacobianOplusXi(1, 2) = (pz * dp(1) - py * dp(2)) * ipz2fy;
135}
136
137} // namespace g2o
BaseFixedSizedEdge< D, Vector2, VertexPointXYZ, VertexCam >::template JacobianType< D, VertexXj::Dimension > & _jacobianOplusXj
BaseFixedSizedEdge< D, Vector2, VertexPointXYZ, VertexCam >::template JacobianType< D, VertexXi::Dimension > & _jacobianOplusXi
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
EIGEN_STRONG_INLINE const Measurement & measurement() const
accessor functions for the measurement represented by the edge
Definition base_edge.h:119
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
const EstimateType & estimate() const
return the current estimate of the vertex
EIGEN_MAKE_ALIGNED_OPERATOR_NEW EdgeProjectP2MC()
virtual void computeError()
return the error estimate as a 2-vector
virtual void linearizeOplus()
Jacobian for monocular projection.
virtual bool write(std::ostream &os) const
write the vertex to a stream
virtual bool read(std::istream &is)
read the vertex from a stream, i.e., the internal state of the vertex
VertexContainer _vertices
Matrix3 Kcam
Definition sbacam.h:51
Eigen::Matrix< double, 3, 4 > w2i
Definition sbacam.h:56
Matrix3 dRdx
Definition sbacam.h:60
Eigen::Matrix< double, 3, 4 > w2n
Definition sbacam.h:55
Matrix3 dRdy
Definition sbacam.h:60
Matrix3 dRdz
Definition sbacam.h:60
const Vector3 & translation() const
Definition se3quat.h:89
SBACam Vertex, (x,y,z,qw,qx,qy,qz) the parameterization for the increments constructed is a 6d vector...
Definition vertex_cam.h:43
Vertex for a tracked point in space.
#define g2o_isnan(x)
Definition macros.h:100
bool writeVector(std::ostream &os, const Eigen::DenseBase< Derived > &b)
Definition io_helper.h:36
bool readVector(std::istream &is, Eigen::DenseBase< Derived > &b)
Definition io_helper.h:42
VectorN< 3 > Vector3
Definition eigen_types.h:51
VectorN< 4 > Vector4
Definition eigen_types.h:52
VectorN< 2 > Vector2
Definition eigen_types.h:50