gtsam
Loading...
Searching...
No Matches
InvDepthCamera3.h
Go to the documentation of this file.
1
11
12#pragma once
13
14#include <gtsam/base/Matrix.h>
16#include <gtsam/base/Vector.h>
21
22#include <cmath>
23
24#if GTSAM_ENABLE_BOOST_SERIALIZATION
25#include <boost/serialization/nvp.hpp>
26#endif
27
28namespace gtsam {
29
35template <class CALIBRATION>
37private:
38 Pose3 pose_;
39 std::shared_ptr<CALIBRATION> k_;
40
41public:
42
45
48
50 InvDepthCamera3(const Pose3& pose, const std::shared_ptr<CALIBRATION>& k) :
51 pose_(pose),k_(k) {}
52
56
57 virtual ~InvDepthCamera3() {}
58
60 inline Pose3& pose() { return pose_; }
61
63 inline const std::shared_ptr<CALIBRATION>& calibration() const { return k_; }
64
66 void print(const std::string& s = "") const {
67 pose_.print("pose3");
68 k_.print("calibration");
69 }
70
77 static gtsam::Point3 invDepthTo3D(const Vector5& pw, double rho) {
78 gtsam::Point3 ray_base(pw.segment(0,3));
79 double theta = pw(3), phi = pw(4);
80 gtsam::Point3 m(cos(theta)*cos(phi),sin(theta)*cos(phi),sin(phi));
81 return ray_base + m/rho;
82 }
83
89 inline gtsam::Point2 project(const Vector5& pw,
90 double rho,
93 OptionalJacobian<2,1> H3 = {}) const {
94
95 gtsam::Point3 ray_base(pw.segment(0,3));
96 double theta = pw(3), phi = pw(4);
97 gtsam::Point3 m(cos(theta)*cos(phi),sin(theta)*cos(phi),sin(phi));
98 const gtsam::Point3 landmark = ray_base + m/rho;
99
100 gtsam::PinholeCamera<CALIBRATION> camera(pose_, *k_);
101
102 if (!H1 && !H2 && !H3) {
103 gtsam::Point2 uv= camera.project(landmark);
104 return uv;
105 }
106 else {
107 gtsam::Matrix J2;
108 gtsam::Point2 uv= camera.project(landmark,H1, J2, {});
109 if (H1) {
110 *H1 = (*H1) * I_6x6;
111 }
112
113 double cos_theta = cos(theta);
114 double sin_theta = sin(theta);
115 double cos_phi = cos(phi);
116 double sin_phi = sin(phi);
117 double rho2 = rho * rho;
118
119 if (H2) {
120 double H11 = 1;
121 double H12 = 0;
122 double H13 = 0;
123 double H14 = -cos_phi*sin_theta/rho;
124 double H15 = -cos_theta*sin_phi/rho;
125
126 double H21 = 0;
127 double H22 = 1;
128 double H23 = 0;
129 double H24 = cos_phi*cos_theta/rho;
130 double H25 = -sin_phi*sin_theta/rho;
131
132 double H31 = 0;
133 double H32 = 0;
134 double H33 = 1;
135 double H34 = 0;
136 double H35 = cos_phi/rho;
137
138 *H2 = J2 * Matrix{{H11, H12, H13, H14, H15},
139 {H21, H22, H23, H24, H25},
140 {H31, H32, H33, H34, H35}};
141 }
142 if(H3) {
143 double H16 = -cos_phi*cos_theta/rho2;
144 double H26 = -cos_phi*sin_theta/rho2;
145 double H36 = -sin_phi/rho2;
146 *H3 = J2 * Matrix{{H16}, {H26}, {H36}};
147 }
148 return uv;
149 }
150 }
151
156
157 inline std::pair<Vector5, double> backproject(const gtsam::Point2& pi, const double depth) const {
158 const gtsam::Point2 pn = k_->calibrate(pi);
159 gtsam::Point3 pc(pn.x(), pn.y(), 1.0);
160 pc = pc/pc.norm();
161 gtsam::Point3 pw = pose_.transformFrom(pc);
162 const gtsam::Point3& pt = pose_.translation();
163 gtsam::Point3 ray = pw - pt;
164 double theta = atan2(ray.y(), ray.x()); // longitude
165 double phi = atan2(ray.z(), sqrt(ray.x()*ray.x()+ray.y()*ray.y()));
166 return std::make_pair(Vector5{pt.x(), pt.y(), pt.z(), theta, phi},
167 double(1. / depth));
168 }
169
170private:
171
175
176#if GTSAM_ENABLE_BOOST_SERIALIZATION
178 friend class boost::serialization::access;
179 template<class Archive>
180 void serialize(Archive & ar, const unsigned int /*version*/) {
181 ar & BOOST_SERIALIZATION_NVP(pose_);
182 ar & BOOST_SERIALIZATION_NVP(k_);
183 }
184#endif
186}; // \class InvDepthCamera
187} // \namespace gtsam
typedef and functions to augment Eigen's MatrixXd
Macros for Matrix constants to avoid excessive template instantiation.
typedef and functions to augment Eigen's VectorXd
Base class for all pinhole cameras.
3D Pose manifold SO(3) x R^3 and group SE(3)
2D Point
Non-linear factor base classes.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
Vector3 Point3
As of GTSAM 4, in order to make GTSAM more lean, it is now possible to just typedef Point3 to Vector3...
Definition Point3.h:38
Vector2 Point2
As of GTSAM 4, in order to make GTSAM more lean, it is now possible to just typedef Point2 to Vector2...
Definition Point2.h:32
OptionalJacobian is an Eigen::Ref like class that can take be constructed using either a fixed size o...
Definition OptionalJacobian.h:40
A 3D pose (R,t) : (Rot3,Point3).
Definition Pose3.h:42
A pinhole camera class that has a Pose3 and a Calibration.
Definition InvDepthCamera3.h:36
std::pair< Vector5, double > backproject(const gtsam::Point2 &pi, const double depth) const
backproject a 2-dimensional point to an Inverse Depth landmark useful for point initialization
Definition InvDepthCamera3.h:157
Pose3 & pose()
return pose
Definition InvDepthCamera3.h:60
static gtsam::Point3 invDepthTo3D(const Vector5 &pw, double rho)
Convert an inverse depth landmark to cartesian Point3.
Definition InvDepthCamera3.h:77
gtsam::Point2 project(const Vector5 &pw, double rho, OptionalJacobian< 2, 6 > H1={}, OptionalJacobian< 2, 5 > H2={}, OptionalJacobian< 2, 1 > H3={}) const
project a point from world InvDepth parameterization to the image
Definition InvDepthCamera3.h:89
InvDepthCamera3(const Pose3 &pose, const std::shared_ptr< CALIBRATION > &k)
constructor with pose and calibration
Definition InvDepthCamera3.h:50
InvDepthCamera3()
default constructor
Definition InvDepthCamera3.h:47
const std::shared_ptr< CALIBRATION > & calibration() const
return calibration
Definition InvDepthCamera3.h:63
void print(const std::string &s="") const
print
Definition InvDepthCamera3.h:66