AliceVision
Photogrammetric Computer Vision Framework
Pinhole.hpp
1 // This file is part of the AliceVision project.
2 // Copyright (c) 2016 AliceVision contributors.
3 // Copyright (c) 2012 openMVG contributors.
4 // This Source Code Form is subject to the terms of the Mozilla Public License,
5 // v. 2.0. If a copy of the MPL was not distributed with this file,
6 // You can obtain one at https://mozilla.org/MPL/2.0/.
7 
8 #pragma once
9 
10 #include <aliceVision/numeric/numeric.hpp>
11 #include <aliceVision/numeric/projection.hpp>
12 #include <aliceVision/geometry/Pose3.hpp>
13 #include <aliceVision/camera/cameraCommon.hpp>
14 #include <aliceVision/camera/IntrinsicScaleOffsetDisto.hpp>
15 
16 #include <memory>
17 #include <limits>
18 #include <algorithm>
19 
20 namespace aliceVision {
21 namespace camera {
22 
25 {
26  public:
27  Pinhole()
28  : Pinhole(1, 1, 1.0, 1.0, 0.0, 0.0)
29  {}
30 
31  Pinhole(unsigned int w, unsigned int h, const Mat3& K)
32  : IntrinsicScaleOffsetDisto(w, h, K(0, 0), K(1, 1), K(0, 2), K(1, 2))
33  {}
34 
35  Pinhole(unsigned int w,
36  unsigned int h,
37  double focalLengthPixX,
38  double focalLengthPixY,
39  double offsetX,
40  double offsetY,
41  std::shared_ptr<Distortion> distortion = nullptr,
42  std::shared_ptr<Undistortion> undistortion = nullptr)
43  : IntrinsicScaleOffsetDisto(w, h, focalLengthPixX, focalLengthPixY, offsetX, offsetY, distortion, undistortion)
44  {}
45 
46  ~Pinhole() override = default;
47 
48  Pinhole* clone() const override { return new Pinhole(*this); }
49 
50  void assign(const IntrinsicBase& other) override { *this = dynamic_cast<const Pinhole&>(other); }
51 
52  static std::shared_ptr<Pinhole> cast(std::shared_ptr<IntrinsicBase> sptr);
53 
54  double getFocalLengthPixX() const { return _scale(0); }
55 
56  double getFocalLengthPixY() const { return _scale(1); }
57 
58  bool isValid() const override { return getFocalLengthPixX() > 0 && getFocalLengthPixY() > 0 && IntrinsicBase::isValid(); }
59 
60  EINTRINSIC getType() const override;
61 
62  Mat3 K() const;
63 
64  void setK(double focalLengthPixX, double focalLengthPixY, double ppx, double ppy);
65 
66  void setK(const Mat3& K);
67 
68  Vec2 transformProject(const geometry::Pose3& pose, const Vec4& pt3D, bool applyDistortion = true) const
69  {
70  return transformProject(pose.getHomogeneous(), pt3D, applyDistortion);
71  }
72 
73  Vec2 transformProject(const Eigen::Matrix4d& pose, const Vec4& pt, bool applyDistortion = true) const override;
74 
75  Vec2 project(const Vec4& pt, bool applyDistortion = true) const override;
76 
77  Eigen::Matrix<double, 2, 3> getDerivativeTransformProjectWrtPoint3(const Eigen::Matrix4d& pose, const Vec4& pt) const override;
78 
79  Eigen::Matrix<double, 2, 2> getDerivativeTransformProjectWrtPrincipalPoint(const Eigen::Matrix4d& pose, const Vec4& pt) const;
80 
81  Eigen::Matrix<double, 2, 2> getDerivativeTransformProjectWrtScale(const Eigen::Matrix4d& pose, const Vec4& pt) const;
82 
83  Eigen::Matrix<double, 2, Eigen::Dynamic> getDerivativeTransformProjectWrtParams(const Eigen::Matrix4d& pose, const Vec4& pt3D) const override;
84 
85  Eigen::Matrix<double, 2, Eigen::Dynamic> getDerivativeTransformProjectWrtDistortion(const Eigen::Matrix4d& pose, const Vec4& pt) const override;
86 
87  Eigen::Matrix<double, 3, Eigen::Dynamic> getDerivativeBackProjectUnitWrtDistortion(const Vec2& pt2D) const override;
88 
89  Vec3 toUnitSphere(const Vec2& pt) const override;
90 
91  Eigen::Matrix<double, 3, 2> getDerivativetoUnitSphereWrtPoint(const Vec2& pt) const;
92 
98  Eigen::Matrix<double, 3, Eigen::Dynamic> getDerivativeBackProjectUnitWrtParams(const Vec2& pt2D) const override;
99 
100  double imagePlaneToCameraPlaneError(double value) const override;
101 
102  Mat34 getProjectiveEquivalent(const geometry::Pose3& pose) const;
103 
109  bool isVisibleRay(const Vec3& ray) const override;
110 
115  double getHorizontalFov() const override;
116 
121  double getVerticalFov() const override;
122 
127  double pixelProbability() const override;
128 };
129 
130 } // namespace camera
131 } // namespace aliceVision
aliceVision::camera::Pinhole::getDerivativeTransformProjectWrtPoint3
Eigen::Matrix< double, 2, 3 > getDerivativeTransformProjectWrtPoint3(const Eigen::Matrix4d &pose, const Vec4 &pt) const override
Get the derivative of a projection of a 3D point into the camera plane.
Definition: Pinhole.cpp:61
aliceVision::camera::Pinhole::getType
EINTRINSIC getType() const override
Get embed camera type.
Definition: Pinhole.cpp:221
aliceVision::camera::Pinhole::isVisibleRay
bool isVisibleRay(const Vec3 &ray) const override
Return true if this ray should be visible in the image.
Definition: Pinhole.cpp:193
aliceVision::camera::Pinhole::toUnitSphere
Vec3 toUnitSphere(const Vec2 &pt) const override
Transform a given point (in pixels) to unit sphere in meters.
Definition: Pinhole.cpp:132
aliceVision::camera::IntrinsicBase::h
unsigned int h() const
Get the intrinsic image height.
Definition: IntrinsicBase.hpp:58
aliceVision::camera::Pinhole::assign
void assign(const IntrinsicBase &other) override
Assign object.
Definition: Pinhole.hpp:50
aliceVision::camera::Pinhole::isValid
bool isValid() const override
Return true if the intrinsic is valid.
Definition: Pinhole.hpp:58
aliceVision::camera::Pinhole::clone
Pinhole * clone() const override
Polymorphic clone.
Definition: Pinhole.hpp:48
aliceVision::camera::IntrinsicBase::isValid
virtual bool isValid() const
Return true if the intrinsic is valid.
Definition: IntrinsicBase.hpp:379
aliceVision
Definition: checkerDetector.cpp:32
aliceVision::camera::IntrinsicBase::w
unsigned int w() const
Get the intrinsic image width.
Definition: IntrinsicBase.hpp:52
aliceVision::camera::Pinhole::imagePlaneToCameraPlaneError
double imagePlaneToCameraPlaneError(double value) const override
Normalize a given unit pixel error to the camera plane.
Definition: Pinhole.cpp:183
aliceVision::camera::IntrinsicScaleOffsetDisto
Class with disto.
Definition: IntrinsicScaleOffsetDisto.hpp:23
aliceVision::camera::Pinhole::pixelProbability
double pixelProbability() const override
how a one pixel change relates to an angular change
Definition: Pinhole.cpp:235
aliceVision::camera::Pinhole::getVerticalFov
double getVerticalFov() const override
Get the vertical FOV in radians.
Definition: Pinhole.cpp:229
aliceVision::camera::Pinhole::getDerivativeBackProjectUnitWrtParams
Eigen::Matrix< double, 3, Eigen::Dynamic > getDerivativeBackProjectUnitWrtParams(const Vec2 &pt2D) const override
Get the derivative of the unit sphere backprojection.
Definition: Pinhole.cpp:156
aliceVision::camera::Pinhole::project
Vec2 project(const Vec4 &pt, bool applyDistortion=true) const override
Projection of a 3D point into the camera plane (Apply disto (if any) and Intrinsics)
Definition: Pinhole.cpp:51
aliceVision::camera::IntrinsicBase
Basis class for all intrinsic parameters of a camera.
Definition: IntrinsicBase.hpp:27
aliceVision::camera::Pinhole::getHorizontalFov
double getHorizontalFov() const override
Get the horizontal FOV in radians.
Definition: Pinhole.cpp:223
aliceVision::camera::Pinhole
Define a classic Pinhole camera.
Definition: Pinhole.hpp:24
aliceVision::camera::Pinhole::getDerivativeTransformProjectWrtParams
Eigen::Matrix< double, 2, Eigen::Dynamic > getDerivativeTransformProjectWrtParams(const Eigen::Matrix4d &pose, const Vec4 &pt3D) const override
Get the derivative of a projection of a 3D point into the camera plane.
Definition: Pinhole.cpp:122
aliceVision::geometry::Pose3
Definition: Pose3.hpp:18