ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Camera.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <Eigen/Geometry>
11 
12 class ccBBox;
13 namespace cloudViewer {
14 
15 namespace visualization {
16 namespace rendering {
17 
18 class Camera {
19 public:
20  enum class FovType { Vertical, Horizontal };
21  enum class Projection { Perspective, Ortho };
22 
23 #ifdef SIMD_ENABLED
24  using Transform =
25  Eigen::Transform<float, 3, Eigen::Affine, Eigen::DontAlign>;
26  using ProjectionMatrix =
27  Eigen::Transform<float, 3, Eigen::Projective, Eigen::DontAlign>;
28 #else
29  using Transform = Eigen::Transform<float, 3, Eigen::Affine>;
30  using ProjectionMatrix = Eigen::Transform<float, 3, Eigen::Projective>;
31 #endif
32 
33  virtual ~Camera() = default;
34 
35  virtual void SetProjection(double fov,
36  double aspect,
37  double near,
38  double far,
39  FovType fov_type) = 0;
40 
63  virtual void SetProjection(Projection projection,
64  double left,
65  double right,
66  double bottom,
67  double top,
68  double near,
69  double far) = 0;
70 
71  virtual void SetProjection(const Eigen::Matrix3d& intrinsics,
72  double near,
73  double far,
74  double width,
75  double height) = 0;
76 
77  virtual void LookAt(const Eigen::Vector3f& center,
78  const Eigen::Vector3f& eye,
79  const Eigen::Vector3f& up) = 0;
80  virtual void FromExtrinsics(const Eigen::Matrix4d& extrinsics);
81 
82  virtual void SetModelMatrix(const Transform& view) = 0;
83  virtual void SetModelMatrix(const Eigen::Vector3f& forward,
84  const Eigen::Vector3f& left,
85  const Eigen::Vector3f& up) = 0;
86 
87  virtual double GetNear() const = 0;
88  virtual double GetFar() const = 0;
90  virtual double GetFieldOfView() const = 0;
92  virtual FovType GetFieldOfViewType() const = 0;
93 
94  virtual Eigen::Vector3f GetPosition() const = 0;
95  virtual Eigen::Vector3f GetForwardVector() const = 0;
96  virtual Eigen::Vector3f GetLeftVector() const = 0;
97  virtual Eigen::Vector3f GetUpVector() const = 0;
98  virtual Transform GetModelMatrix() const = 0;
99  virtual Transform GetViewMatrix() const = 0;
102 
103  virtual Eigen::Vector3f Unproject(float x,
104  float y,
105  float z,
106  float view_width,
107  float view_height) const = 0;
108 
109  // Returns the normalized device coordinates (NDC) of the specified point
110  // given the view and projection matrices of the camera. The returned point
111  // is in the range [-1, 1] if the point is in view, or outside the range if
112  // the point is out of view.
113  virtual Eigen::Vector2f GetNDC(const Eigen::Vector3f& pt) const = 0;
114 
117  virtual double GetViewZ(float z_buffer) const = 0;
118 
119  struct ProjectionInfo {
120  bool is_ortho;
122  union {
123  struct {
125  double left;
126  double right;
127  double bottom;
128  double top;
129  double near_plane; // Windows #defines "near"
130  double far_plane; // Windows #defines "far"
131  } ortho;
132  struct {
134  double fov;
135  double aspect;
136  double near_plane;
137  double far_plane;
139  struct {
140  double fx;
141  double fy;
142  double cx;
143  double cy;
144  double near_plane;
145  double far_plane;
146  double width;
147  double height;
149  } proj;
150  };
151  virtual const ProjectionInfo& GetProjection() const = 0;
152 
153  virtual void CopyFrom(const Camera* camera) = 0;
154 
159  static void SetupCameraAsPinholeCamera(rendering::Camera& camera,
160  const Eigen::Matrix3d& intrinsic,
161  const Eigen::Matrix4d& extrinsic,
162  int intrinsic_width_px,
163  int intrinsic_height_px,
164  const ccBBox& scene_bounds);
165 
167  static float CalcNearPlane();
168 
171  static float CalcFarPlane(const rendering::Camera& camera,
172  const ccBBox& scene_bounds);
173 };
174 
175 } // namespace rendering
176 } // namespace visualization
177 } // namespace cloudViewer
int width
int height
Bounding box structure.
Definition: ecvBBox.h:25
virtual Eigen::Vector3f GetUpVector() const =0
virtual Eigen::Vector2f GetNDC(const Eigen::Vector3f &pt) const =0
virtual Eigen::Vector3f GetLeftVector() const =0
virtual Transform GetViewMatrix() const =0
virtual Eigen::Vector3f Unproject(float x, float y, float z, float view_width, float view_height) const =0
virtual const ProjectionInfo & GetProjection() const =0
virtual void SetModelMatrix(const Eigen::Vector3f &forward, const Eigen::Vector3f &left, const Eigen::Vector3f &up)=0
Eigen::Transform< float, 3, Eigen::Affine > Transform
Definition: Camera.h:29
virtual double GetViewZ(float z_buffer) const =0
virtual Eigen::Vector3f GetForwardVector() const =0
virtual void CopyFrom(const Camera *camera)=0
virtual void SetModelMatrix(const Transform &view)=0
virtual ProjectionMatrix GetProjectionMatrix() const =0
virtual void FromExtrinsics(const Eigen::Matrix4d &extrinsics)
Definition: Camera.cpp:19
virtual void SetProjection(const Eigen::Matrix3d &intrinsics, double near, double far, double width, double height)=0
virtual Eigen::Vector3f GetPosition() const =0
virtual Transform GetModelMatrix() const =0
virtual void SetProjection(Projection projection, double left, double right, double bottom, double top, double near, double far)=0
Eigen::Transform< float, 3, Eigen::Projective > ProjectionMatrix
Definition: Camera.h:30
virtual double GetFieldOfView() const =0
only valid if fov was passed to SetProjection()
virtual FovType GetFieldOfViewType() const =0
only valid if fov was passed to SetProjection()
static void SetupCameraAsPinholeCamera(rendering::Camera &camera, const Eigen::Matrix3d &intrinsic, const Eigen::Matrix4d &extrinsic, int intrinsic_width_px, int intrinsic_height_px, const ccBBox &scene_bounds)
Definition: Camera.cpp:35
static float CalcNearPlane()
Returns a good value for the near plane.
Definition: Camera.cpp:47
virtual void SetProjection(double fov, double aspect, double near, double far, FovType fov_type)=0
static float CalcFarPlane(const rendering::Camera &camera, const ccBBox &scene_bounds)
Definition: Camera.cpp:49
virtual Transform GetCullingProjectionMatrix() const =0
virtual void LookAt(const Eigen::Vector3f &center, const Eigen::Vector3f &eye, const Eigen::Vector3f &up)=0
Generic file read and write utility for python interface.
struct cloudViewer::visualization::rendering::Camera::ProjectionInfo::@23::@26 intrinsics
struct cloudViewer::visualization::rendering::Camera::ProjectionInfo::@23::@24 ortho
union cloudViewer::visualization::rendering::Camera::ProjectionInfo::@23 proj
struct cloudViewer::visualization::rendering::Camera::ProjectionInfo::@23::@25 perspective