g2o
Loading...
Searching...
No Matches
edge_project_p2sc.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_p2sc.h"
28
29namespace g2o {
30
31// point to camera projection, stereo
34
35bool EdgeProjectP2SC::read(std::istream& is) {
37 return readInformationMatrix(is);
38}
39
40bool EdgeProjectP2SC::write(std::ostream& os) const {
43 return os.good();
44}
45
46// return the error estimate as a 2-vector
48 // from <Point> to <Cam>
49 const VertexPointXYZ* point =
50 static_cast<const VertexPointXYZ*>(_vertices[0]);
51 VertexCam* cam = static_cast<VertexCam*>(_vertices[1]);
52
53 // calculate the projection
54 Vector3 kp;
55 Vector4 pt;
56 pt.head<3>() = point->estimate();
57 pt(3) = 1;
58 const SBACam& nd = cam->estimate();
59 // these should be already ok
60 /* nd.setTransform(); */
61 /* nd.setProjection(); */
62 /* nd.setDr(); */
63
64 Vector3 p1 = nd.w2i * pt;
65 Vector3 p2 = nd.w2n * pt;
66 Vector3 pb(nd.baseline, 0, 0);
67
68 double invp1 = cst(1.0) / p1(2);
69 kp.head<2>() = p1.head<2>() * invp1;
70
71 // right camera px
72 p2 = nd.Kcam * (p2 - pb);
73 kp(2) = p2(0) / p2(2);
74
75 // std::cout << std::endl << "CAM " << cam->estimate() << std::endl;
76 // std::cout << "POINT " << pt.transpose() << std::endl;
77 // std::cout << "PROJ " << p1.transpose() << std::endl;
78 // std::cout << "PROJ " << p2.transpose() << std::endl;
79 // std::cout << "CPROJ " << kp.transpose() << std::endl;
80 // std::cout << "MEAS " << _measurement.transpose() << std::endl;
81
82 // error, which is backwards from the normal observed - calculated
83 // _measurement is the measured projection
84 _error = kp - _measurement;
85}
86
88 VertexCam* vc = static_cast<VertexCam*>(_vertices[1]);
89 const SBACam& cam = vc->estimate();
90
91 VertexPointXYZ* vp = static_cast<VertexPointXYZ*>(_vertices[0]);
92 Vector4 pt, trans;
93 pt.head<3>() = vp->estimate();
94 pt(3) = 1.0;
95 trans.head<3>() = cam.translation();
96 trans(3) = 1.0;
97
98 // first get the world point in camera coords
99 Vector3 pc = cam.w2n * pt;
100
101 // Jacobians wrt camera parameters
102 // set d(quat-x) values [ pz*dpx/dx - px*dpz/dx ] / pz^2
103 double px = pc(0);
104 double py = pc(1);
105 double pz = pc(2);
106 double ipz2 = 1.0 / (pz * pz);
107 if (g2o_isnan(ipz2)) {
108 std::cout << "[SetJac] infinite jac" << std::endl;
109 abort();
110 }
111
112 double ipz2fx = ipz2 * cam.Kcam(0, 0); // Fx
113 double ipz2fy = ipz2 * cam.Kcam(1, 1); // Fy
114 double b = cam.baseline; // stereo baseline
115
116 // check for local vars
117 Vector3 pwt =
118 (pt - trans)
119 .head<3>(); // transform translations, use differential rotation
120
121 // dx
122 Vector3 dp = cam.dRdx * pwt; // dR'/dq * [pw - t]
123 _jacobianOplusXj(0, 3) = (pz * dp(0) - px * dp(2)) * ipz2fx;
124 _jacobianOplusXj(1, 3) = (pz * dp(1) - py * dp(2)) * ipz2fy;
125 _jacobianOplusXj(2, 3) =
126 (pz * dp(0) - (px - b) * dp(2)) * ipz2fx; // right image px
127 // dy
128 dp = cam.dRdy * pwt; // dR'/dq * [pw - t]
129 _jacobianOplusXj(0, 4) = (pz * dp(0) - px * dp(2)) * ipz2fx;
130 _jacobianOplusXj(1, 4) = (pz * dp(1) - py * dp(2)) * ipz2fy;
131 _jacobianOplusXj(2, 4) =
132 (pz * dp(0) - (px - b) * dp(2)) * ipz2fx; // right image px
133 // dz
134 dp = cam.dRdz * pwt; // dR'/dq * [pw - t]
135 _jacobianOplusXj(0, 5) = (pz * dp(0) - px * dp(2)) * ipz2fx;
136 _jacobianOplusXj(1, 5) = (pz * dp(1) - py * dp(2)) * ipz2fy;
137 _jacobianOplusXj(2, 5) =
138 (pz * dp(0) - (px - b) * dp(2)) * ipz2fx; // right image px
139
140 // set d(t) values [ pz*dpx/dx - px*dpz/dx ] / pz^2
141 dp = -cam.w2n.col(0); // dpc / dx
142 _jacobianOplusXj(0, 0) = (pz * dp(0) - px * dp(2)) * ipz2fx;
143 _jacobianOplusXj(1, 0) = (pz * dp(1) - py * dp(2)) * ipz2fy;
144 _jacobianOplusXj(2, 0) =
145 (pz * dp(0) - (px - b) * dp(2)) * ipz2fx; // right image px
146 dp = -cam.w2n.col(1); // dpc / dy
147 _jacobianOplusXj(0, 1) = (pz * dp(0) - px * dp(2)) * ipz2fx;
148 _jacobianOplusXj(1, 1) = (pz * dp(1) - py * dp(2)) * ipz2fy;
149 _jacobianOplusXj(2, 1) =
150 (pz * dp(0) - (px - b) * dp(2)) * ipz2fx; // right image px
151 dp = -cam.w2n.col(2); // dpc / dz
152 _jacobianOplusXj(0, 2) = (pz * dp(0) - px * dp(2)) * ipz2fx;
153 _jacobianOplusXj(1, 2) = (pz * dp(1) - py * dp(2)) * ipz2fy;
154 _jacobianOplusXj(2, 2) =
155 (pz * dp(0) - (px - b) * dp(2)) * ipz2fx; // right image px
156
157 // Jacobians wrt point parameters
158 // set d(t) values [ pz*dpx/dx - px*dpz/dx ] / pz^2
159 dp = cam.w2n.col(0); // dpc / dx
160 _jacobianOplusXi(0, 0) = (pz * dp(0) - px * dp(2)) * ipz2fx;
161 _jacobianOplusXi(1, 0) = (pz * dp(1) - py * dp(2)) * ipz2fy;
162 _jacobianOplusXi(2, 0) =
163 (pz * dp(0) - (px - b) * dp(2)) * ipz2fx; // right image px
164 dp = cam.w2n.col(1); // dpc / dy
165 _jacobianOplusXi(0, 1) = (pz * dp(0) - px * dp(2)) * ipz2fx;
166 _jacobianOplusXi(1, 1) = (pz * dp(1) - py * dp(2)) * ipz2fy;
167 _jacobianOplusXi(2, 1) =
168 (pz * dp(0) - (px - b) * dp(2)) * ipz2fx; // right image px
169 dp = cam.w2n.col(2); // dpc / dz
170 _jacobianOplusXi(0, 2) = (pz * dp(0) - px * dp(2)) * ipz2fx;
171 _jacobianOplusXi(1, 2) = (pz * dp(1) - py * dp(2)) * ipz2fy;
172 _jacobianOplusXi(2, 2) =
173 (pz * dp(0) - (px - b) * dp(2)) * ipz2fx; // right image px
174}
175
176} // namespace g2o
BaseFixedSizedEdge< D, Vector3, VertexPointXYZ, VertexCam >::template JacobianType< D, VertexXj::Dimension > & _jacobianOplusXj
BaseFixedSizedEdge< D, Vector3, 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
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
virtual bool read(std::istream &is)
read the vertex from a stream, i.e., the internal state of the vertex
virtual bool write(std::ostream &os) const
write the vertex to a stream
void computeError()
return the error estimate as a 2-vector
virtual void linearizeOplus()
Jacobian for stereo projection.
EIGEN_MAKE_ALIGNED_OPERATOR_NEW EdgeProjectP2SC()
VertexContainer _vertices
Matrix3 Kcam
Definition sbacam.h:51
Eigen::Matrix< double, 3, 4 > w2i
Definition sbacam.h:56
double baseline
Definition sbacam.h:52
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
constexpr double cst(long double v)
Definition misc.h:60