GTSAM  4.0.2
C++ library for smoothing and mapping (SAM)
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 
20 #pragma once
21 
23 #include <gtsam/geometry/Point2.h>
24 
25 namespace gtsam {
26 
32 template<typename CALIBRATION>
33 class PinholeBaseK: public PinholeBase {
34 
35 private:
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 
42 public:
43 
44  typedef CALIBRATION CalibrationType;
45 
48 
51  }
52 
54  explicit PinholeBaseK(const Pose3& pose) :
55  PinholeBase(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>
92  OptionalJacobian<2, DimK> Dcal) const {
93 
94  // project to normalized coordinates
95  const Point2 pn = PinholeBase::project2(pw, Dpose, Dpoint);
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 
120  OptionalJacobian<2, 3> Dpoint = {},
121  OptionalJacobian<2, DimK> Dcal = {}) const {
122  return Point2(_project(pw, Dpose, Dpoint, Dcal) - measured);
123  }
124 
127  OptionalJacobian<2, 2> Dpoint = {},
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 
213 private:
214 
215 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
216 
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 public:
228 };
229 // end of class PinholeBaseK
230 
238 template<typename CALIBRATION>
239 class PinholePose: public PinholeBaseK<CALIBRATION> {
240 
241 private:
242 
244  std::shared_ptr<CALIBRATION> K_;
245 
246 public:
247 
248  enum {
249  dimension = 6
250  };
251 
254 
257  }
258 
260  explicit PinholePose(const Pose3& pose) :
261  Base(pose), K_(new CALIBRATION()) {
262  }
263 
265  PinholePose(const Pose3& pose, const std::shared_ptr<CALIBRATION>& K) :
266  Base(pose), K_(K) {
267  }
268 
272 
280  static PinholePose Level(const std::shared_ptr<CALIBRATION>& K,
281  const Pose2& pose2, double height) {
282  return PinholePose(Base::LevelPose(pose2, height), K);
283  }
284 
286  static PinholePose Level(const Pose2& pose2, double height) {
287  return PinholePose::Level(std::make_shared<CALIBRATION>(), pose2, height);
288  }
289 
299  static PinholePose Lookat(const Point3& eye, const Point3& target,
300  const Point3& upVector, const std::shared_ptr<CALIBRATION>& K =
301  std::make_shared<CALIBRATION>()) {
302  return PinholePose(Base::LookatPose(eye, target, upVector), K);
303  }
304 
308 
310  explicit PinholePose(const Vector &v) :
311  Base(v), K_(new CALIBRATION()) {
312  }
313 
315  PinholePose(const Vector &v, const Vector &K) :
316  Base(v), K_(new CALIBRATION(K)) {
317  }
318 
319  // Init from Pose3 and calibration
320  PinholePose(const Pose3 &pose, const Vector &K) :
321  Base(pose), K_(new CALIBRATION(K)) {
322  }
323 
327 
329  bool equals(const Base &camera, double tol = 1e-9) const {
330  const PinholePose* e = dynamic_cast<const PinholePose*>(&camera);
331  return Base::equals(camera, tol) && K_->equals(e->calibration(), tol);
332  }
333 
335  friend std::ostream& operator<<(std::ostream &os, const PinholePose& camera) {
336  os << "{R: " << camera.pose().rotation().rpy().transpose();
337  os << ", t: " << camera.pose().translation().transpose();
338  if (!camera.K_) os << ", K: none";
339  else os << ", K: " << *camera.K_;
340  os << "}";
341  return os;
342  }
343 
345  void print(const std::string& s = "PinholePose") const override {
346  Base::print(s);
347  if (!K_)
348  std::cout << "s No calibration given" << std::endl;
349  else
350  K_->print(s + ".calibration");
351  }
352 
356 
357  ~PinholePose() override {
358  }
359 
361  const std::shared_ptr<CALIBRATION>& sharedCalibration() const {
362  return K_;
363  }
364 
366  const CALIBRATION& calibration() const override {
367  return *K_;
368  }
369 
376  OptionalJacobian<2, 3> Dpoint = {}) const {
377  return Base::project(pw, Dpose, Dpoint);
378  }
379 
382  OptionalJacobian<2, 2> Dpoint = {}) const {
383  return Base::project(pw, Dpose, Dpoint);
384  }
385 
389 
391  size_t dim() const {
392  return 6;
393  }
394 
396  static size_t Dim() {
397  return 6;
398  }
399 
401  PinholePose retract(const Vector6& d) const {
402  return PinholePose(Base::pose().retract(d), K_);
403  }
404 
406  Vector6 localCoordinates(const PinholePose& p) const {
407  return Base::pose().localCoordinates(p.Base::pose());
408  }
409 
412  return PinholePose(); // assumes that the default constructor is valid
413  }
414 
416  Matrix34 cameraProjectionMatrix() const {
417  Matrix34 P = Matrix34(PinholeBase::pose().inverse().matrix().block(0, 0, 3, 4));
418  return K_->K() * P;
419  }
420 
423  return Eigen::Matrix<double,traits<Point2>::dimension,1>::Constant(2.0 * K_->fx());
424  }
426 
427 private:
428 
429 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
430 
431  friend class boost::serialization::access;
432  template<class Archive>
433  void serialize(Archive & ar, const unsigned int /*version*/) {
434  ar
435  & boost::serialization::make_nvp("PinholeBaseK",
436  boost::serialization::base_object<Base>(*this));
437  ar & BOOST_SERIALIZATION_NVP(K_);
438  }
439 #endif
440 
441 public:
443 };
444 // end of class PinholePose
445 
446 template<typename CALIBRATION>
447 struct traits<PinholePose<CALIBRATION> > : public internal::Manifold<
448  PinholePose<CALIBRATION> > {
449 };
450 
451 template<typename CALIBRATION>
452 struct traits<const PinholePose<CALIBRATION> > : public internal::Manifold<
453  PinholePose<CALIBRATION> > {
454 };
455 
456 } // \ gtsam
const CALIBRATION & calibration() const override
return calibration
Definition: PinholePose.h:366
static Matrix23 Dpoint(const Point2 &pn, double d, const Matrix3 &Rt)
double range(const Point3 &point, OptionalJacobian< 1, 6 > Hself={}, OptionalJacobian< 1, 3 > Hpoint={}) const
Both ManifoldTraits and Testable.
Definition: Manifold.h:117
Definition: CalibratedCamera.h:251
Point2 _project(const POINT &pw, OptionalJacobian< 2, 6 > Dpose, OptionalJacobian< 2, FixedDimension< POINT >::value > Dpoint, OptionalJacobian< 2, DimK > Dcal) const
Definition: PinholePose.h:90
std::string serialize(const T &input)
serializes to a string
Definition: serialization.h:113
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
Definition: PinholePose.h:33
Matrix34 cameraProjectionMatrix() const
for Linear Triangulation
Definition: PinholePose.h:416
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
double range(const Point3 &point, OptionalJacobian< 1, 6 > Dcamera={}, OptionalJacobian< 1, 3 > Dpoint={}) const
Definition: PinholePose.h:175
PinholePose(const Vector &v, const Vector &K)
Init from Vector and calibration.
Definition: PinholePose.h:315
PinholePose(const Pose3 &pose, const std::shared_ptr< CALIBRATION > &K)
Definition: PinholePose.h:265
Vector2 Point2
Definition: Point2.h:32
static PinholePose Level(const Pose2 &pose2, double height)
PinholePose::level with default calibration.
Definition: PinholePose.h:286
Give fixed size dimension of a type, fails at compile time if dynamic.
Definition: Manifold.h:161
static PinholePose Identity()
for Canonical
Definition: PinholePose.h:411
Definition: Group.h:43
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
PinholePose(const Pose3 &pose)
Definition: PinholePose.h:260
virtual const CALIBRATION & calibration() const =0
return calibration
bool equals(const Base &camera, double tol=1e-9) const
assert equality up to a tolerance
Definition: PinholePose.h:329
PinholePose()
Definition: PinholePose.h:256
PinholeBase()
Default constructor.
Definition: CalibratedCamera.h:125
Unit3 backprojectPointAtInfinity(const Point2 &p) const
backproject a 2-dimensional point to a 3-dimensional point at infinity
Definition: PinholePose.h:164
Definition: CalibratedCamera.h:54
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
std::pair< Point2, bool > projectSafe(const Point3 &pw) const
Project a point into the image and check depth.
Definition: PinholePose.h:76
void print(const std::string &s="PinholePose") const override
print
Definition: PinholePose.h:345
PinholePose retract(const Vector6 &d) const
move a cameras according to d
Definition: PinholePose.h:401
Represents a 3D point on a unit sphere.
Definition: Unit3.h:42
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
Definition: Pose2.h:39
static size_t Dim()
Definition: PinholePose.h:396
Point2 project2(const Unit3 &pw, OptionalJacobian< 2, 6 > Dpose={}, OptionalJacobian< 2, 2 > Dpoint={}) const
project2 version for point at infinity
Definition: PinholePose.h:381
const Pose3 & pose() const
return pose, constant version
Definition: CalibratedCamera.h:154
GTSAM_EXPORT void print(const Matrix &A, const std::string &s, std::ostream &stream)
Vector defaultErrorWhenTriangulatingBehindCamera() const
for Nonlinear Triangulation
Definition: PinholePose.h:422
Vector3 rpy(OptionalJacobian< 3, 3 > H={}) const
std::pair< Point2, bool > projectSafe(const Point3 &pw) const
Project a point into the image and check depth.
double range(const PinholeBaseK< CalibrationB > &camera, OptionalJacobian< 1, 6 > Dcamera={}, OptionalJacobian< 1, 6 > Dother={}) const
Definition: PinholePose.h:207
PinholeBaseK(const Pose3 &pose)
Definition: PinholePose.h:54
static PinholePose Lookat(const Point3 &eye, const Point3 &target, const Point3 &upVector, const std::shared_ptr< CALIBRATION > &K=std::make_shared< CALIBRATION >())
Definition: PinholePose.h:299
PinholeBaseK()
Definition: PinholePose.h:50
PinholePose(const Vector &v)
Init from 6D vector.
Definition: PinholePose.h:310
Definition: PinholePose.h:239
Calibrated camera for which only pose is unknown.
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
Definition: chartTesting.h:28
static PinholePose Level(const std::shared_ptr< CALIBRATION > &K, const Pose2 &pose2, double height)
Definition: PinholePose.h:280
size_t dim() const
Definition: PinholePose.h:391
Definition: OptionalJacobian.h:38
const Point3 & translation(OptionalJacobian< 3, 6 > Hself={}) const
get translation
friend std::ostream & operator<<(std::ostream &os, const PinholePose &camera)
stream operator
Definition: PinholePose.h:335
Point2 project2(const Point3 &point, OptionalJacobian< 2, 6 > Dpose={}, OptionalJacobian< 2, 3 > Dpoint={}) const
Point2 project2(const Point3 &pw, OptionalJacobian< 2, 6 > Dpose={}, OptionalJacobian< 2, 3 > Dpoint={}) const
Definition: PinholePose.h:375
static Matrix26 Dpose(const Point2 &pn, double d)
double range(const CalibratedCamera &camera, OptionalJacobian< 1, 6 > Dcamera={}, OptionalJacobian< 1, 6 > Dother={}) const
Definition: PinholePose.h:196
const Rot3 & rotation(OptionalJacobian< 3, 6 > Hself={}) const
get rotation
#define GTSAM_MAKE_ALIGNED_OPERATOR_NEW
Definition: types.h:284
Vector3 Point3
Definition: Point3.h:38
Point3 rotate(const Point3 &p, OptionalJacobian< 3, 3 > H1={}, OptionalJacobian< 3, 3 > H2={}) const
2D Point
Vector6 localCoordinates(const PinholePose &p) const
return canonical coordinate
Definition: PinholePose.h:406
Definition: Pose3.h:37
const std::shared_ptr< CALIBRATION > & sharedCalibration() const
return shared pointer to calibration
Definition: PinholePose.h:361
double range(const Pose3 &pose, OptionalJacobian< 1, 6 > Dcamera={}, OptionalJacobian< 1, 6 > Dpose={}) const
Definition: PinholePose.h:186