gtsam  4.0.0
gtsam
PinholeCamera.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 #pragma once
20 
23 
24 namespace gtsam {
25 
32 template<typename Calibration>
33 class GTSAM_EXPORT PinholeCamera: public PinholeBaseK<Calibration> {
34 
35 public:
36 
42 
43 private:
44 
46  Calibration K_;
47 
48  // Get dimensions of calibration type at compile time
49  static const int DimK = FixedDimension<Calibration>::value;
50 
51 public:
52 
53  enum {
54  dimension = 6 + DimK
55  };
56 
59 
62  }
63 
65  explicit PinholeCamera(const Pose3& pose) :
66  Base(pose) {
67  }
68 
70  PinholeCamera(const Pose3& pose, const Calibration& K) :
71  Base(pose), K_(K) {
72  }
73 
77 
85  static PinholeCamera Level(const Calibration &K, const Pose2& pose2,
86  double height) {
87  return PinholeCamera(Base::LevelPose(pose2, height), K);
88  }
89 
91  static PinholeCamera Level(const Pose2& pose2, double height) {
92  return PinholeCamera::Level(Calibration(), pose2, height);
93  }
94 
104  static PinholeCamera Lookat(const Point3& eye, const Point3& target,
105  const Point3& upVector, const Calibration& K = Calibration()) {
106  return PinholeCamera(Base::LookatPose(eye, target, upVector), K);
107  }
108 
109  // Create PinholeCamera, with derivatives
110  static PinholeCamera Create(const Pose3& pose, const Calibration &K,
111  OptionalJacobian<dimension, 6> H1 = boost::none, //
112  OptionalJacobian<dimension, DimK> H2 = boost::none) {
113  typedef Eigen::Matrix<double, DimK, 6> MatrixK6;
114  if (H1)
115  *H1 << I_6x6, MatrixK6::Zero();
116  typedef Eigen::Matrix<double, 6, DimK> Matrix6K;
117  typedef Eigen::Matrix<double, DimK, DimK> MatrixK;
118  if (H2)
119  *H2 << Matrix6K::Zero(), MatrixK::Identity();
120  return PinholeCamera(pose,K);
121  }
122 
126 
128  explicit PinholeCamera(const Vector &v) :
129  Base(v.head<6>()) {
130  if (v.size() > 6)
131  K_ = Calibration(v.tail<DimK>());
132  }
133 
135  PinholeCamera(const Vector &v, const Vector &K) :
136  Base(v), K_(K) {
137  }
138 
142 
144  bool equals(const Base &camera, double tol = 1e-9) const {
145  const PinholeCamera* e = dynamic_cast<const PinholeCamera*>(&camera);
146  return Base::equals(camera, tol) && K_.equals(e->calibration(), tol);
147  }
148 
150  void print(const std::string& s = "PinholeCamera") const {
151  Base::print(s);
152  K_.print(s + ".calibration");
153  }
154 
158 
159  virtual ~PinholeCamera() {
160  }
161 
163  const Pose3& pose() const {
164  return Base::pose();
165  }
166 
169  if (H) {
170  H->setZero();
171  H->template block<6, 6>(0, 0) = I_6x6;
172  }
173  return Base::pose();
174  }
175 
177  const Calibration& calibration() const {
178  return K_;
179  }
180 
184 
186  size_t dim() const {
187  return dimension;
188  }
189 
191  static size_t Dim() {
192  return dimension;
193  }
194 
195  typedef Eigen::Matrix<double, dimension, 1> VectorK6;
196 
198  PinholeCamera retract(const Vector& d) const {
199  if ((size_t) d.size() == 6)
200  return PinholeCamera(this->pose().retract(d), calibration());
201  else
202  return PinholeCamera(this->pose().retract(d.head<6>()),
203  calibration().retract(d.tail(calibration().dim())));
204  }
205 
207  VectorK6 localCoordinates(const PinholeCamera& T2) const {
208  VectorK6 d;
209  d.template head<6>() = this->pose().localCoordinates(T2.pose());
210  d.template tail<DimK>() = calibration().localCoordinates(T2.calibration());
211  return d;
212  }
213 
216  return PinholeCamera(); // assumes that the default constructor is valid
217  }
218 
222 
223  typedef Eigen::Matrix<double, 2, DimK> Matrix2K;
224 
228  template<class POINT>
231  // We just call 3-derivative version in Base
232  Matrix26 Dpose;
233  Eigen::Matrix<double, 2, DimK> Dcal;
234  Point2 pi = Base::project(pw, Dcamera ? &Dpose : 0, Dpoint,
235  Dcamera ? &Dcal : 0);
236  if (Dcamera)
237  *Dcamera << Dpose, Dcal;
238  return pi;
239  }
240 
243  boost::none, OptionalJacobian<2, 3> Dpoint = boost::none) const {
244  return _project2(pw, Dcamera, Dpoint);
245  }
246 
249  boost::none, OptionalJacobian<2, 2> Dpoint = boost::none) const {
250  return _project2(pw, Dcamera, Dpoint);
251  }
252 
258  double range(const Point3& point, OptionalJacobian<1, dimension> Dcamera =
259  boost::none, OptionalJacobian<1, 3> Dpoint = boost::none) const {
260  Matrix16 Dpose_;
261  double result = this->pose().range(point, Dcamera ? &Dpose_ : 0, Dpoint);
262  if (Dcamera)
263  *Dcamera << Dpose_, Eigen::Matrix<double, 1, DimK>::Zero();
264  return result;
265  }
266 
272  double range(const Pose3& pose, OptionalJacobian<1, dimension> Dcamera =
273  boost::none, OptionalJacobian<1, 6> Dpose = boost::none) const {
274  Matrix16 Dpose_;
275  double result = this->pose().range(pose, Dcamera ? &Dpose_ : 0, Dpose);
276  if (Dcamera)
277  *Dcamera << Dpose_, Eigen::Matrix<double, 1, DimK>::Zero();
278  return result;
279  }
280 
286  template<class CalibrationB>
287  double range(const PinholeCamera<CalibrationB>& camera,
288  OptionalJacobian<1, dimension> Dcamera = boost::none,
289  OptionalJacobian<1, 6 + CalibrationB::dimension> Dother = boost::none) const {
290  Matrix16 Dcamera_, Dother_;
291  double result = this->pose().range(camera.pose(), Dcamera ? &Dcamera_ : 0,
292  Dother ? &Dother_ : 0);
293  if (Dcamera) {
294  *Dcamera << Dcamera_, Eigen::Matrix<double, 1, DimK>::Zero();
295  }
296  if (Dother) {
297  Dother->setZero();
298  Dother->template block<1, 6>(0, 0) = Dother_;
299  }
300  return result;
301  }
302 
308  double range(const CalibratedCamera& camera,
309  OptionalJacobian<1, dimension> Dcamera = boost::none,
310  OptionalJacobian<1, 6> Dother = boost::none) const {
311  return range(camera.pose(), Dcamera, Dother);
312  }
313 
314 private:
315 
317  friend class boost::serialization::access;
318  template<class Archive>
319  void serialize(Archive & ar, const unsigned int /*version*/) {
320  ar
321  & boost::serialization::make_nvp("PinholeBaseK",
322  boost::serialization::base_object<Base>(*this));
323  ar & BOOST_SERIALIZATION_NVP(K_);
324  }
325 
326 };
327 
328 // manifold traits
329 
330 template <typename Calibration>
331 struct traits<PinholeCamera<Calibration> >
332  : public internal::Manifold<PinholeCamera<Calibration> > {};
333 
334 template <typename Calibration>
335 struct traits<const PinholeCamera<Calibration> >
336  : public internal::Manifold<PinholeCamera<Calibration> > {};
337 
338 // range traits, used in RangeFactor
339 template <typename Calibration, typename T>
340 struct Range<PinholeCamera<Calibration>, T> : HasRange<PinholeCamera<Calibration>, T, double> {};
341 
342 } // \ gtsam
void print(const std::string &s="PinholeCamera") const
print
Definition: PinholeCamera.h:150
Point2 project2(const Point3 &pw, OptionalJacobian< 2, dimension > Dcamera=boost::none, OptionalJacobian< 2, 3 > Dpoint=boost::none) const
project a 3D point from world coordinates into the image
Definition: PinholeCamera.h:242
Definition: CalibratedCamera.h:240
Represents a 3D point on a unit sphere.
Definition: Unit3.h:42
Definition: BearingRange.h:38
double range(const CalibratedCamera &camera, OptionalJacobian< 1, dimension > Dcamera=boost::none, OptionalJacobian< 1, 6 > Dother=boost::none) const
Calculate range to a calibrated camera.
Definition: PinholeCamera.h:308
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition: Matrix.cpp:140
static size_t Dim()
Definition: PinholeCamera.h:191
OptionalJacobian is an Eigen::Ref like class that can take be constructed using either a fixed size o...
Definition: OptionalJacobian.h:39
Pinhole camera with known calibration.
double range(const Pose3 &pose, OptionalJacobian< 1, dimension > Dcamera=boost::none, OptionalJacobian< 1, 6 > Dpose=boost::none) const
Calculate range to another pose.
Definition: PinholeCamera.h:272
static PinholeCamera Level(const Calibration &K, const Pose2 &pose2, double height)
Create a level camera at the given 2D pose and height.
Definition: PinholeCamera.h:85
Definition: Pose3.h:37
PinholeCamera(const Pose3 &pose, const Calibration &K)
constructor with pose and calibration
Definition: PinholeCamera.h:70
Definition: Point3.h:45
size_t dim() const
Definition: PinholeCamera.h:186
PinholeCamera(const Vector &v)
Init from vector, can be 6D (default calibration) or dim.
Definition: PinholeCamera.h:128
Definition: Pose2.h:36
double range(const Point3 &point, OptionalJacobian< 1, dimension > Dcamera=boost::none, OptionalJacobian< 1, 3 > Dpoint=boost::none) const
Calculate range to a landmark.
Definition: PinholeCamera.h:258
Both ManifoldTraits and Testable.
Definition: Manifold.h:120
Definition: Point2.h:40
const Calibration & calibration() const
return calibration
Definition: PinholeCamera.h:177
Point2 project2(const Unit3 &pw, OptionalJacobian< 2, dimension > Dcamera=boost::none, OptionalJacobian< 2, 2 > Dpoint=boost::none) const
project a point at infinity from world coordinates into the image
Definition: PinholeCamera.h:248
PinholeCamera()
default constructor
Definition: PinholeCamera.h:61
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
Definition: PinholeCamera.h:33
PinholeCamera retract(const Vector &d) const
move a cameras according to d
Definition: PinholeCamera.h:198
VectorK6 localCoordinates(const PinholeCamera &T2) const
return canonical coordinate
Definition: PinholeCamera.h:207
Bearing-Range product.
PinholeCamera(const Vector &v, const Vector &K)
Init from Vector and calibration.
Definition: PinholeCamera.h:135
Give fixed size dimension of a type, fails at compile time if dynamic.
Definition: Manifold.h:164
static PinholeCamera Lookat(const Point3 &eye, const Point3 &target, const Point3 &upVector, const Calibration &K=Calibration())
Create a camera at the given eye position looking at a target point in the scene with the specified u...
Definition: PinholeCamera.h:104
double range(const PinholeCamera< CalibrationB > &camera, OptionalJacobian< 1, dimension > Dcamera=boost::none, OptionalJacobian< 1, 6+CalibrationB::dimension > Dother=boost::none) const
Calculate range to another camera.
Definition: PinholeCamera.h:287
Definition: BearingRange.h:123
Point2 _project2(const POINT &pw, OptionalJacobian< 2, dimension > Dcamera, OptionalJacobian< 2, FixedDimension< POINT >::value > Dpoint) const
Templated projection of a 3D point or a point at infinity into the image.
Definition: PinholeCamera.h:229
Point2 Measurement
Some classes template on either PinholeCamera or StereoCamera, and this typedef informs those classes...
Definition: PinholeCamera.h:41
const Pose3 & getPose(OptionalJacobian< 6, dimension > H) const
return pose, with derivative
Definition: PinholeCamera.h:168
const Pose3 & pose() const
return pose
Definition: PinholeCamera.h:163
static PinholeCamera identity()
for Canonical
Definition: PinholeCamera.h:215
Definition: PinholePose.h:34
double range(const Point3 &point, OptionalJacobian< 1, 6 > H1=boost::none, OptionalJacobian< 1, 3 > H2=boost::none) const
Calculate range to a landmark.
Definition: Pose3.cpp:339
PinholeCamera(const Pose3 &pose)
constructor with pose
Definition: PinholeCamera.h:65
TangentVector localCoordinates(const Class &g) const
localCoordinates as required by manifold concept: finds tangent vector between *this and g ...
Definition: Lie.h:138
const Pose3 & pose() const
return pose, constant version
Definition: CalibratedCamera.h:146
Class retract(const TangentVector &v) const
retract as required by manifold concept: applies v at *this
Definition: Lie.h:133
bool equals(const Base &camera, double tol=1e-9) const
assert equality up to a tolerance
Definition: PinholeCamera.h:144
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
static PinholeCamera Level(const Pose2 &pose2, double height)
PinholeCamera::level with default calibration.
Definition: PinholeCamera.h:91