Open3D (C++ API)  0.14.1
Camera.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018-2021 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #pragma once
28 
29 #include <Eigen/Geometry>
30 
31 namespace open3d {
32 
33 namespace geometry {
34 class AxisAlignedBoundingBox;
35 } // namespace geometry
36 
37 namespace visualization {
38 namespace rendering {
39 
40 class Camera {
41 public:
42  enum class FovType { Vertical, Horizontal };
43  enum class Projection { Perspective, Ortho };
44  using Transform = Eigen::Transform<float, 3, Eigen::Affine>;
45  using ProjectionMatrix = Eigen::Transform<float, 3, Eigen::Projective>;
46 
47  virtual ~Camera() = default;
48 
49  virtual void SetProjection(double fov,
50  double aspect,
51  double near,
52  double far,
53  FovType fov_type) = 0;
54 
77  virtual void SetProjection(Projection projection,
78  double left,
79  double right,
80  double bottom,
81  double top,
82  double near,
83  double far) = 0;
84 
85  virtual void SetProjection(const Eigen::Matrix3d& intrinsics,
86  double near,
87  double far,
88  double width,
89  double height) = 0;
90 
91  virtual void LookAt(const Eigen::Vector3f& center,
92  const Eigen::Vector3f& eye,
93  const Eigen::Vector3f& up) = 0;
94  virtual void FromExtrinsics(const Eigen::Matrix4d& extrinsics);
95 
96  virtual void SetModelMatrix(const Transform& view) = 0;
97  virtual void SetModelMatrix(const Eigen::Vector3f& forward,
98  const Eigen::Vector3f& left,
99  const Eigen::Vector3f& up) = 0;
100 
101  virtual double GetNear() const = 0;
102  virtual double GetFar() const = 0;
104  virtual double GetFieldOfView() const = 0;
106  virtual FovType GetFieldOfViewType() const = 0;
107 
108  virtual Eigen::Vector3f GetPosition() const = 0;
109  virtual Eigen::Vector3f GetForwardVector() const = 0;
110  virtual Eigen::Vector3f GetLeftVector() const = 0;
111  virtual Eigen::Vector3f GetUpVector() const = 0;
112  virtual Transform GetModelMatrix() const = 0;
113  virtual Transform GetViewMatrix() const = 0;
116 
117  virtual Eigen::Vector3f Unproject(float x,
118  float y,
119  float z,
120  float view_width,
121  float view_height) const = 0;
122 
123  // Returns the normalized device coordinates (NDC) of the specified point
124  // given the view and projection matrices of the camera. The returned point
125  // is in the range [-1, 1] if the point is in view, or outside the range if
126  // the point is out of view.
127  virtual Eigen::Vector2f GetNDC(const Eigen::Vector3f& pt) const = 0;
128 
131  virtual double GetViewZ(float z_buffer) const = 0;
132 
133  struct ProjectionInfo {
134  bool is_ortho;
136  union {
137  struct {
139  double left;
140  double right;
141  double bottom;
142  double top;
143  double near_plane; // Windows #defines "near"
144  double far_plane; // Windows #defines "far"
145  } ortho;
146  struct {
148  double fov;
149  double aspect;
150  double near_plane;
151  double far_plane;
153  struct {
154  double fx;
155  double fy;
156  double cx;
157  double cy;
158  double near_plane;
159  double far_plane;
160  double width;
161  double height;
163  } proj;
164  };
165  virtual const ProjectionInfo& GetProjection() const = 0;
166 
167  virtual void CopyFrom(const Camera* camera) = 0;
168 
173  static void SetupCameraAsPinholeCamera(
174  rendering::Camera& camera,
175  const Eigen::Matrix3d& intrinsic,
176  const Eigen::Matrix4d& extrinsic,
177  int intrinsic_width_px,
178  int intrinsic_height_px,
179  const geometry::AxisAlignedBoundingBox& scene_bounds);
180 
182  static float CalcNearPlane();
183 
186  static float CalcFarPlane(
187  const rendering::Camera& camera,
188  const geometry::AxisAlignedBoundingBox& scene_bounds);
189 };
190 
191 } // namespace rendering
192 } // namespace visualization
193 } // namespace open3d
A bounding box that is aligned along the coordinate axes.
Definition: BoundingVolume.h:150
Eigen::Transform< float, 3, Eigen::Projective > ProjectionMatrix
Definition: Camera.h:45
virtual void CopyFrom(const Camera *camera)=0
virtual const ProjectionInfo & GetProjection() const =0
virtual void LookAt(const Eigen::Vector3f &center, const Eigen::Vector3f &eye, const Eigen::Vector3f &up)=0
virtual Eigen::Vector3f GetForwardVector() const =0
virtual FovType GetFieldOfViewType() const =0
only valid if fov was passed to SetProjection()
static float CalcFarPlane(const rendering::Camera &camera, const geometry::AxisAlignedBoundingBox &scene_bounds)
Definition: Camera.cpp:69
virtual Transform GetModelMatrix() const =0
Eigen::Transform< float, 3, Eigen::Affine > Transform
Definition: Camera.h:44
virtual Eigen::Vector3f Unproject(float x, float y, float z, float view_width, float view_height) const =0
static void SetupCameraAsPinholeCamera(rendering::Camera &camera, const Eigen::Matrix3d &intrinsic, const Eigen::Matrix4d &extrinsic, int intrinsic_width_px, int intrinsic_height_px, const geometry::AxisAlignedBoundingBox &scene_bounds)
Definition: Camera.cpp:54
virtual double GetViewZ(float z_buffer) const =0
virtual void SetProjection(const Eigen::Matrix3d &intrinsics, double near, double far, double width, double height)=0
virtual void SetModelMatrix(const Eigen::Vector3f &forward, const Eigen::Vector3f &left, const Eigen::Vector3f &up)=0
static float CalcNearPlane()
Returns a good value for the near plane.
Definition: Camera.cpp:67
virtual ProjectionMatrix GetProjectionMatrix() const =0
virtual void FromExtrinsics(const Eigen::Matrix4d &extrinsics)
Definition: Camera.cpp:38
virtual Transform GetViewMatrix() const =0
virtual Eigen::Vector3f GetPosition() const =0
virtual double GetNear() const =0
virtual double GetFieldOfView() const =0
only valid if fov was passed to SetProjection()
virtual void SetProjection(double fov, double aspect, double near, double far, FovType fov_type)=0
virtual Eigen::Vector2f GetNDC(const Eigen::Vector3f &pt) const =0
virtual Eigen::Vector3f GetUpVector() const =0
virtual void SetProjection(Projection projection, double left, double right, double bottom, double top, double near, double far)=0
virtual Transform GetCullingProjectionMatrix() const =0
virtual double GetFar() const =0
virtual void SetModelMatrix(const Transform &view)=0
virtual Eigen::Vector3f GetLeftVector() const =0
int width
Definition: FilePCD.cpp:71
int height
Definition: FilePCD.cpp:72
Definition: PinholeCameraIntrinsic.cpp:35
union open3d::visualization::rendering::Camera::ProjectionInfo::@7 proj
struct open3d::visualization::rendering::Camera::ProjectionInfo::@7::@10 intrinsics
struct open3d::visualization::rendering::Camera::ProjectionInfo::@7::@9 perspective
struct open3d::visualization::rendering::Camera::ProjectionInfo::@7::@8 ortho