Open3D (C++ API)  0.18.0
Camera.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2023 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <Eigen/Geometry>
11 
12 namespace open3d {
13 
14 namespace geometry {
15 class AxisAlignedBoundingBox;
16 } // namespace geometry
17 
18 namespace visualization {
19 namespace rendering {
20 
21 class Camera {
22 public:
23  enum class FovType { Vertical, Horizontal };
24  enum class Projection { Perspective, Ortho };
25  using Transform = Eigen::Transform<float, 3, Eigen::Affine>;
26  using ProjectionMatrix = Eigen::Transform<float, 3, Eigen::Projective>;
27 
28  virtual ~Camera() = default;
29 
30  virtual void SetProjection(double fov,
31  double aspect,
32  double near,
33  double far,
34  FovType fov_type) = 0;
35 
58  virtual void SetProjection(Projection projection,
59  double left,
60  double right,
61  double bottom,
62  double top,
63  double near,
64  double far) = 0;
65 
66  virtual void SetProjection(const Eigen::Matrix3d& intrinsics,
67  double near,
68  double far,
69  double width,
70  double height) = 0;
71 
72  virtual void LookAt(const Eigen::Vector3f& center,
73  const Eigen::Vector3f& eye,
74  const Eigen::Vector3f& up) = 0;
75  virtual void FromExtrinsics(const Eigen::Matrix4d& extrinsics);
76 
77  virtual void SetModelMatrix(const Transform& view) = 0;
78  virtual void SetModelMatrix(const Eigen::Vector3f& forward,
79  const Eigen::Vector3f& left,
80  const Eigen::Vector3f& up) = 0;
81 
82  virtual double GetNear() const = 0;
83  virtual double GetFar() const = 0;
85  virtual double GetFieldOfView() const = 0;
87  virtual FovType GetFieldOfViewType() const = 0;
88 
89  virtual Eigen::Vector3f GetPosition() const = 0;
90  virtual Eigen::Vector3f GetForwardVector() const = 0;
91  virtual Eigen::Vector3f GetLeftVector() const = 0;
92  virtual Eigen::Vector3f GetUpVector() const = 0;
93  virtual Transform GetModelMatrix() const = 0;
94  virtual Transform GetViewMatrix() const = 0;
97 
101  virtual Eigen::Vector3f Unproject(float x,
102  float y,
103  float z,
104  float view_width,
105  float view_height) const = 0;
106 
107  // Returns the normalized device coordinates (NDC) of the specified point
108  // given the view and projection matrices of the camera. The returned point
109  // is in the range [-1, 1] if the point is in view, or outside the range if
110  // the point is out of view.
111  virtual Eigen::Vector2f GetNDC(const Eigen::Vector3f& pt) const = 0;
112 
115  virtual double GetViewZ(float z_buffer) const = 0;
116 
117  struct ProjectionInfo {
118  bool is_ortho;
120  union {
121  struct {
123  double left;
124  double right;
125  double bottom;
126  double top;
127  double near_plane; // Windows #defines "near"
128  double far_plane; // Windows #defines "far"
129  } ortho;
130  struct {
132  double fov;
133  double aspect;
134  double near_plane;
135  double far_plane;
137  struct {
138  double fx;
139  double fy;
140  double cx;
141  double cy;
142  double near_plane;
143  double far_plane;
144  double width;
145  double height;
147  } proj;
148  };
149  virtual const ProjectionInfo& GetProjection() const = 0;
150 
151  virtual void CopyFrom(const Camera* camera) = 0;
152 
157  static void SetupCameraAsPinholeCamera(
158  rendering::Camera& camera,
159  const Eigen::Matrix3d& intrinsic,
160  const Eigen::Matrix4d& extrinsic,
161  int intrinsic_width_px,
162  int intrinsic_height_px,
163  const geometry::AxisAlignedBoundingBox& scene_bounds);
164 
166  static float CalcNearPlane();
167 
170  static float CalcFarPlane(
171  const rendering::Camera& camera,
172  const geometry::AxisAlignedBoundingBox& scene_bounds);
173 };
174 
175 } // namespace rendering
176 } // namespace visualization
177 } // namespace open3d
A bounding box that is aligned along the coordinate axes and defined by the min_bound and max_bound.
Definition: BoundingVolume.h:160
Eigen::Transform< float, 3, Eigen::Projective > ProjectionMatrix
Definition: Camera.h:26
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:50
virtual Transform GetModelMatrix() const =0
Eigen::Transform< float, 3, Eigen::Affine > Transform
Definition: Camera.h:25
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:35
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:48
virtual ProjectionMatrix GetProjectionMatrix() const =0
virtual void FromExtrinsics(const Eigen::Matrix4d &extrinsics)
Definition: Camera.cpp:19
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:52
int height
Definition: FilePCD.cpp:53
Definition: PinholeCameraIntrinsic.cpp:16
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