Open3D (C++ API)  0.15.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 
120  virtual Eigen::Vector3f Unproject(float x,
121  float y,
122  float z,
123  float view_width,
124  float view_height) const = 0;
125 
126  // Returns the normalized device coordinates (NDC) of the specified point
127  // given the view and projection matrices of the camera. The returned point
128  // is in the range [-1, 1] if the point is in view, or outside the range if
129  // the point is out of view.
130  virtual Eigen::Vector2f GetNDC(const Eigen::Vector3f& pt) const = 0;
131 
134  virtual double GetViewZ(float z_buffer) const = 0;
135 
136  struct ProjectionInfo {
137  bool is_ortho;
139  union {
140  struct {
142  double left;
143  double right;
144  double bottom;
145  double top;
146  double near_plane; // Windows #defines "near"
147  double far_plane; // Windows #defines "far"
148  } ortho;
149  struct {
151  double fov;
152  double aspect;
153  double near_plane;
154  double far_plane;
156  struct {
157  double fx;
158  double fy;
159  double cx;
160  double cy;
161  double near_plane;
162  double far_plane;
163  double width;
164  double height;
166  } proj;
167  };
168  virtual const ProjectionInfo& GetProjection() const = 0;
169 
170  virtual void CopyFrom(const Camera* camera) = 0;
171 
176  static void SetupCameraAsPinholeCamera(
177  rendering::Camera& camera,
178  const Eigen::Matrix3d& intrinsic,
179  const Eigen::Matrix4d& extrinsic,
180  int intrinsic_width_px,
181  int intrinsic_height_px,
182  const geometry::AxisAlignedBoundingBox& scene_bounds);
183 
185  static float CalcNearPlane();
186 
189  static float CalcFarPlane(
190  const rendering::Camera& camera,
191  const geometry::AxisAlignedBoundingBox& scene_bounds);
192 };
193 
194 } // namespace rendering
195 } // namespace visualization
196 } // namespace open3d
A bounding box that is aligned along the coordinate axes.
Definition: BoundingVolume.h:155
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