gtsam
Loading...
Searching...
No Matches
PinholePose.h
Go to the documentation of this file.
1/* ----------------------------------------------------------------------------
2
3 * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4 * Atlanta, Georgia 30332-0415
5 * All Rights Reserved
6 * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7
8 * See LICENSE for the license information
9
10 * -------------------------------------------------------------------------- */
11
19
20#pragma once
21
24
25namespace gtsam {
26
32template<typename CALIBRATION>
34
35private:
36
37 GTSAM_CONCEPT_MANIFOLD_TYPE(CALIBRATION)
38
39 // Get dimensions of calibration type at compile time
40 static const int DimK = FixedDimension<CALIBRATION>::value;
41
42public:
43
44 typedef CALIBRATION CalibrationType;
45
48
51 }
52
54 explicit PinholeBaseK(const Pose3& pose) :
56 }
57
61
62 explicit PinholeBaseK(const Vector& v) : PinholeBase(v) {}
63
67
69 virtual const CALIBRATION& calibration() const = 0;
70
74
76 std::pair<Point2, bool> projectSafe(const Point3& pw) const {
77 std::pair<Point2, bool> pn = PinholeBase::projectSafe(pw);
78 pn.first = calibration().uncalibrate(pn.first);
79 return pn;
80 }
81
82
89 template <class POINT>
91 OptionalJacobian<2, FixedDimension<POINT>::value> Dpoint,
92 OptionalJacobian<2, DimK> Dcal) const {
93
94 // project to normalized coordinates
96
97 // uncalibrate to pixel coordinates
98 Matrix2 Dpi_pn;
99 const Point2 pi = calibration().uncalibrate(pn, Dcal,
100 Dpose || Dpoint ? &Dpi_pn : 0);
101
102 // If needed, apply chain rule
103 if (Dpose)
104 *Dpose = Dpi_pn * *Dpose;
105 if (Dpoint)
106 *Dpoint = Dpi_pn * *Dpoint;
107
108 return pi;
109 }
110
114 OptionalJacobian<2, DimK> Dcal = {}) const {
115 return _project(pw, Dpose, Dpoint, Dcal);
116 }
117
121 OptionalJacobian<2, DimK> Dcal = {}) const {
122 return Point2(_project(pw, Dpose, Dpoint, Dcal) - measured);
123 }
124
128 OptionalJacobian<2, DimK> Dcal = {}) const {
129 return _project(pw, Dpose, Dpoint, Dcal);
130 }
131
133 Point3 backproject(const Point2& p, double depth,
134 OptionalJacobian<3, 6> Dresult_dpose = {},
135 OptionalJacobian<3, 2> Dresult_dp = {},
136 OptionalJacobian<3, 1> Dresult_ddepth = {},
137 OptionalJacobian<3, DimK> Dresult_dcal = {}) const {
138 typedef Eigen::Matrix<double, 2, DimK> Matrix2K;
139 Matrix2K Dpn_dcal;
140 Matrix22 Dpn_dp;
141 const Point2 pn = calibration().calibrate(p, Dresult_dcal ? &Dpn_dcal : 0,
142 Dresult_dp ? &Dpn_dp : 0);
143 Matrix32 Dpoint_dpn;
144 Matrix31 Dpoint_ddepth;
145 const Point3 point = BackprojectFromCamera(pn, depth,
146 (Dresult_dp || Dresult_dcal) ? &Dpoint_dpn : 0,
147 Dresult_ddepth ? &Dpoint_ddepth : 0);
148 Matrix33 Dresult_dpoint;
149 const Point3 result = pose().transformFrom(point, Dresult_dpose,
150 (Dresult_ddepth ||
151 Dresult_dp ||
152 Dresult_dcal) ? &Dresult_dpoint : 0);
153 if (Dresult_dcal)
154 *Dresult_dcal = Dresult_dpoint * Dpoint_dpn * Dpn_dcal; // (3x3)*(3x2)*(2xDimK)
155 if (Dresult_dp)
156 *Dresult_dp = Dresult_dpoint * Dpoint_dpn * Dpn_dp; // (3x3)*(3x2)*(2x2)
157 if (Dresult_ddepth)
158 *Dresult_ddepth = Dresult_dpoint * Dpoint_ddepth; // (3x3)*(3x1)
159
160 return result;
161 }
162
165 const Point2 pn = calibration().calibrate(p);
166 const Unit3 pc(pn.x(), pn.y(), 1.0); //by convention the last element is 1
167 return pose().rotation().rotate(pc);
168 }
169
175 double range(const Point3& point,
176 OptionalJacobian<1, 6> Dcamera = {},
177 OptionalJacobian<1, 3> Dpoint = {}) const {
178 return pose().range(point, Dcamera, Dpoint);
179 }
180
186 double range(const Pose3& pose, OptionalJacobian<1, 6> Dcamera = {},
187 OptionalJacobian<1, 6> Dpose = {}) const {
188 return this->pose().range(pose, Dcamera, Dpose);
189 }
190
196 double range(const CalibratedCamera& camera, OptionalJacobian<1, 6> Dcamera =
197 {}, OptionalJacobian<1, 6> Dother = {}) const {
198 return pose().range(camera.pose(), Dcamera, Dother);
199 }
200
206 template<class CalibrationB>
207 double range(const PinholeBaseK<CalibrationB>& camera,
208 OptionalJacobian<1, 6> Dcamera = {},
209 OptionalJacobian<1, 6> Dother = {}) const {
210 return pose().range(camera.pose(), Dcamera, Dother);
211 }
212
213private:
214
215#if GTSAM_ENABLE_BOOST_SERIALIZATION
217 friend class boost::serialization::access;
218 template<class Archive>
219 void serialize(Archive & ar, const unsigned int /*version*/) {
220 ar
221 & boost::serialization::make_nvp("PinholeBase",
222 boost::serialization::base_object<PinholeBase>(*this));
223 }
224#endif
225};
226// end of class PinholeBaseK
227
235template<typename CALIBRATION>
236class PinholePose: public PinholeBaseK<CALIBRATION> {
237
238private:
239
240 typedef PinholeBaseK<CALIBRATION> Base;
241 std::shared_ptr<CALIBRATION> K_;
242
243public:
244
245 inline constexpr static auto calibration_dimension =
246 FixedDimension<CALIBRATION>::value;
247 inline constexpr static auto dimension = 6;
248
251
254 }
255
257 explicit PinholePose(const Pose3& pose) :
258 Base(pose), K_(new CALIBRATION()) {
259 }
260
262 PinholePose(const Pose3& pose, const std::shared_ptr<CALIBRATION>& K) :
263 Base(pose), K_(K) {
264 }
265
269
277 static PinholePose Level(const std::shared_ptr<CALIBRATION>& K,
278 const Pose2& pose2, double height) {
279 return PinholePose(Base::LevelPose(pose2, height), K);
280 }
281
283 static PinholePose Level(const Pose2& pose2, double height) {
284 return PinholePose::Level(std::make_shared<CALIBRATION>(), pose2, height);
285 }
286
296 static PinholePose Lookat(const Point3& eye, const Point3& target,
297 const Point3& upVector, const std::shared_ptr<CALIBRATION>& K =
298 std::make_shared<CALIBRATION>()) {
299 return PinholePose(Base::LookatPose(eye, target, upVector), K);
300 }
301
305
307 explicit PinholePose(const Vector &v) :
308 Base(v), K_(new CALIBRATION()) {
309 }
310
312 PinholePose(const Vector &v, const Vector &K) :
313 Base(v), K_(new CALIBRATION(K)) {
314 }
315
316 // Init from Pose3 and calibration
317 PinholePose(const Pose3 &pose, const Vector &K) :
318 Base(pose), K_(new CALIBRATION(K)) {
319 }
320
324
326 bool equals(const Base &camera, double tol = 1e-9) const {
327 const PinholePose* e = dynamic_cast<const PinholePose*>(&camera);
328 return Base::equals(camera, tol) && K_->equals(e->calibration(), tol);
329 }
330
332 bool equals(const PinholePose& camera, double tol = 1e-9) const {
333 return Base::equals(camera, tol) && K_->equals(camera.calibration(), tol);
334 }
335
337 GTSAM_EXPORT friend std::ostream& operator<<(std::ostream& os,
338 const PinholePose& camera) {
339 os << "{R: " << camera.pose().rotation().rpy().transpose();
340 os << ", t: " << camera.pose().translation().transpose();
341 if (!camera.K_) os << ", K: none";
342 else os << ", K: " << *camera.K_;
343 os << "}";
344 return os;
345 }
346
348 void print(const std::string& s = "PinholePose") const override {
349 Base::print(s);
350 if (!K_)
351 std::cout << "s No calibration given" << std::endl;
352 else
353 K_->print(s + ".calibration");
354 }
355
359
360 ~PinholePose() override {
361 }
362
364 const std::shared_ptr<CALIBRATION>& sharedCalibration() const {
365 return K_;
366 }
367
369 const CALIBRATION& calibration() const override {
370 return *K_;
371 }
372
379 OptionalJacobian<2, 3> Dpoint = {}) const {
380 return Base::project(pw, Dpose, Dpoint);
381 }
382
385 OptionalJacobian<2, 2> Dpoint = {}) const {
386 return Base::project(pw, Dpose, Dpoint);
387 }
388
392
393 size_t dim() const {
394 return 6;
395 }
396
397 static size_t Dim() {
398 return 6;
399 }
400
402 PinholePose retract(const Vector6& d) const {
403 return PinholePose(Base::pose().retract(d), K_);
404 }
405
407 Vector6 localCoordinates(const PinholePose& p) const {
408 return Base::pose().localCoordinates(p.Base::pose());
409 }
410
413 return PinholePose(); // assumes that the default constructor is valid
414 }
415
417 Matrix34 cameraProjectionMatrix() const {
418 Matrix34 P = Matrix34(PinholeBase::pose().inverse().matrix().block(0, 0, 3, 4));
419 return K_->K() * P;
420 }
421
424 return Eigen::Matrix<double,traits<Point2>::dimension,1>::Constant(2.0 * K_->fx());
425 }
426
427
428private:
429
430#if GTSAM_ENABLE_BOOST_SERIALIZATION
432 friend class boost::serialization::access;
433 template<class Archive>
434 void serialize(Archive & ar, const unsigned int /*version*/) {
435 ar
436 & boost::serialization::make_nvp("PinholeBaseK",
437 boost::serialization::base_object<Base>(*this));
438 ar & BOOST_SERIALIZATION_NVP(K_);
439 }
440#endif
441};
442// end of class PinholePose
443
444template<typename CALIBRATION>
445struct traits<PinholePose<CALIBRATION> > : public internal::Manifold<
446 PinholePose<CALIBRATION> > {
447};
448
449template<typename CALIBRATION>
450struct traits<const PinholePose<CALIBRATION> > : public internal::Manifold<
451 PinholePose<CALIBRATION> > {
452};
453
454} // \ gtsam
Calibrated camera for which only pose is unknown.
2D Point
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
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
TangentVector localCoordinates(const Class &g) const
localCoordinates as required by manifold concept: finds tangent vector between *this and g
Definition Lie.h:226
Both ManifoldTraits and Testable.
Definition Manifold.h:156
OptionalJacobian is an Eigen::Ref like class that can take be constructed using either a fixed size o...
Definition OptionalJacobian.h:40
A pinhole camera class that has a Pose3, functions as base class for all pinhole cameras.
Definition CalibratedCamera.h:55
PinholeBase()
Default constructor.
Definition CalibratedCamera.h:126
static Matrix26 Dpose(const Point2 &pn, double d)
Calculate Jacobian with respect to pose.
Definition CalibratedCamera.cpp:28
virtual void print(const std::string &s="PinholeBase") const
print
Definition CalibratedCamera.cpp:75
std::pair< Point2, bool > projectSafe(const Point3 &pw) const
Project a point into the image and check depth.
Definition CalibratedCamera.cpp:109
Point2 project2(const Point3 &point, OptionalJacobian< 2, 6 > Dpose={}, OptionalJacobian< 2, 3 > Dpoint={}) const
Project point into the image Throws a CheiralityException if point behind image plane iff GTSAM_THROW...
Definition CalibratedCamera.cpp:116
const Pose3 & pose() const
return pose, constant version
Definition CalibratedCamera.h:155
static Pose3 LevelPose(const Pose2 &pose2, double height)
Create a level pose at the given 2D pose and height.
Definition CalibratedCamera.cpp:50
bool equals(const PinholeBase &camera, double tol=1e-9) const
assert equality up to a tolerance
Definition CalibratedCamera.cpp:70
static Matrix23 Dpoint(const Point2 &pn, double d, const Matrix3 &Rt)
Calculate Jacobian with respect to point.
Definition CalibratedCamera.cpp:38
static Pose3 LookatPose(const Point3 &eye, const Point3 &target, const Point3 &upVector)
Create a camera pose at the given eye position looking at a target point in the scene with the specif...
Definition CalibratedCamera.cpp:59
static Point3 BackprojectFromCamera(const Point2 &p, const double depth, OptionalJacobian< 3, 2 > Dpoint={}, OptionalJacobian< 3, 1 > Ddepth={})
backproject a 2-dimensional point to a 3-dimensional point at given depth
Definition CalibratedCamera.cpp:167
A Calibrated camera class [R|-R't], calibration K=I.
Definition CalibratedCamera.h:252
const Rot3 & rotation(ComponentJacobian H={}) const
Rotation component.
Definition ExtendedPose3-inl.h:76
std::pair< Point2, bool > projectSafe(const Point3 &pw) const
Project a point into the image and check depth.
Definition PinholePose.h:76
PinholeBaseK()
default constructor
Definition PinholePose.h:50
virtual const CALIBRATION & calibration() const =0
return calibration
PinholeBaseK(const Pose3 &pose)
constructor with pose
Definition PinholePose.h:54
double range(const CalibratedCamera &camera, OptionalJacobian< 1, 6 > Dcamera={}, OptionalJacobian< 1, 6 > Dother={}) const
Calculate range to a CalibratedCamera.
Definition PinholePose.h:196
double range(const Point3 &point, OptionalJacobian< 1, 6 > Dcamera={}, OptionalJacobian< 1, 3 > Dpoint={}) const
Calculate range to a landmark.
Definition PinholePose.h:175
double range(const PinholeBaseK< CalibrationB > &camera, OptionalJacobian< 1, 6 > Dcamera={}, OptionalJacobian< 1, 6 > Dother={}) const
Calculate range to a PinholePoseK derived class.
Definition PinholePose.h:207
Point2 reprojectionError(const Point3 &pw, const Point2 &measured, OptionalJacobian< 2, 6 > Dpose={}, OptionalJacobian< 2, 3 > Dpoint={}, OptionalJacobian< 2, DimK > Dcal={}) const
project a 3D point from world coordinates into the image
Definition PinholePose.h:119
Point2 project(const Unit3 &pw, OptionalJacobian< 2, 6 > Dpose={}, OptionalJacobian< 2, 2 > Dpoint={}, OptionalJacobian< 2, DimK > Dcal={}) const
project a point at infinity from world coordinates into the image
Definition PinholePose.h:126
double range(const Pose3 &pose, OptionalJacobian< 1, 6 > Dcamera={}, OptionalJacobian< 1, 6 > Dpose={}) const
Calculate range to another pose.
Definition PinholePose.h:186
Unit3 backprojectPointAtInfinity(const Point2 &p) const
backproject a 2-dimensional point to a 3-dimensional point at infinity
Definition PinholePose.h:164
Point2 _project(const POINT &pw, OptionalJacobian< 2, 6 > Dpose, OptionalJacobian< 2, FixedDimension< POINT >::value > Dpoint, OptionalJacobian< 2, DimK > Dcal) const
Templated projection of a point (possibly at infinity) from world coordinate to the image.
Definition PinholePose.h:90
Point3 backproject(const Point2 &p, double depth, OptionalJacobian< 3, 6 > Dresult_dpose={}, OptionalJacobian< 3, 2 > Dresult_dp={}, OptionalJacobian< 3, 1 > Dresult_ddepth={}, OptionalJacobian< 3, DimK > Dresult_dcal={}) const
backproject a 2-dimensional point to a 3-dimensional point at given depth
Definition PinholePose.h:133
Point2 project(const Point3 &pw, OptionalJacobian< 2, 6 > Dpose={}, OptionalJacobian< 2, 3 > Dpoint={}, OptionalJacobian< 2, DimK > Dcal={}) const
project a 3D point from world coordinates into the image
Definition PinholePose.h:112
A pinhole camera class that has a Pose3 and a fixed Calibration.
Definition PinholePose.h:236
PinholePose()
default constructor
Definition PinholePose.h:253
Matrix34 cameraProjectionMatrix() const
for Linear Triangulation
Definition PinholePose.h:417
const std::shared_ptr< CALIBRATION > & sharedCalibration() const
return shared pointer to calibration
Definition PinholePose.h:364
const CALIBRATION & calibration() const override
return calibration
Definition PinholePose.h:369
Vector defaultErrorWhenTriangulatingBehindCamera() const
for Nonlinear Triangulation
Definition PinholePose.h:423
bool equals(const PinholePose &camera, double tol=1e-9) const
Compare with another camera of the same concrete type.
Definition PinholePose.h:332
static PinholePose Identity()
for Canonical
Definition PinholePose.h:412
Point2 project2(const Unit3 &pw, OptionalJacobian< 2, 6 > Dpose={}, OptionalJacobian< 2, 2 > Dpoint={}) const
project2 version for point at infinity
Definition PinholePose.h:384
PinholePose(const Vector &v, const Vector &K)
Init from Vector and calibration.
Definition PinholePose.h:312
GTSAM_EXPORT friend std::ostream & operator<<(std::ostream &os, const PinholePose &camera)
stream operator
Definition PinholePose.h:337
Point2 project2(const Point3 &pw, OptionalJacobian< 2, 6 > Dpose={}, OptionalJacobian< 2, 3 > Dpoint={}) const
project a point from world coordinate to the image, 2 derivatives only
Definition PinholePose.h:378
PinholePose(const Pose3 &pose, const std::shared_ptr< CALIBRATION > &K)
constructor with pose and calibration
Definition PinholePose.h:262
PinholePose retract(const Vector6 &d) const
move a cameras according to d
Definition PinholePose.h:402
static PinholePose Lookat(const Point3 &eye, const Point3 &target, const Point3 &upVector, const std::shared_ptr< CALIBRATION > &K=std::make_shared< CALIBRATION >())
Create a camera at the given eye position looking at a target point in the scene with the specified u...
Definition PinholePose.h:296
PinholePose(const Vector &v)
Init from 6D vector.
Definition PinholePose.h:307
static PinholePose Level(const std::shared_ptr< CALIBRATION > &K, const Pose2 &pose2, double height)
Create a level camera at the given 2D pose and height.
Definition PinholePose.h:277
bool equals(const Base &camera, double tol=1e-9) const
assert equality up to a tolerance
Definition PinholePose.h:326
void print(const std::string &s="PinholePose") const override
print
Definition PinholePose.h:348
PinholePose(const Pose3 &pose)
constructor with pose, uses default calibration
Definition PinholePose.h:257
static PinholePose Level(const Pose2 &pose2, double height)
PinholePose::level with default calibration.
Definition PinholePose.h:283
Vector6 localCoordinates(const PinholePose &p) const
return canonical coordinate
Definition PinholePose.h:407
static constexpr auto dimension
Definition PinholePose.h:247
A 2D pose (Point2,Rot2).
Definition Pose2.h:39
A 3D pose (R,t) : (Rot3,Point3).
Definition Pose3.h:42
Point3 transformFrom(const Point3 &point, OptionalJacobian< 3, 6 > Hself={}, OptionalJacobian< 3, 3 > Hpoint={}) const
takes point in Pose coordinates and transforms it to world coordinates
Definition Pose3.cpp:180
double range(const Point3 &point, OptionalJacobian< 1, 6 > Hself={}, OptionalJacobian< 1, 3 > Hpoint={}) const
Calculate range to a landmark.
Definition Pose3.cpp:232
const Point3 & translation(OptionalJacobian< 3, 6 > Hself={}) const
get translation
Definition Pose3.cpp:158
Point3 rotate(const Point3 &p, OptionalJacobian< 3, 3 > H1={}, OptionalJacobian< 3, 3 > H2={}) const
rotate point from rotated coordinate frame to world
Definition Rot3M.cpp:165
Vector3 rpy(OptionalJacobian< 3, 3 > H={}) const
Use RQ to calculate roll-pitch-yaw angle representation.
Definition Rot3.cpp:200
Represents a 3D point on a unit sphere.
Definition Unit3.h:44