Open3D (C++ API)  0.13.0
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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) 2019 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;
114  virtual ProjectionMatrix GetProjectionMatrix() const = 0;
115  virtual Transform GetCullingProjectionMatrix() 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 
129  struct ProjectionInfo {
130  bool is_ortho;
132  union {
133  struct {
135  double left;
136  double right;
137  double bottom;
138  double top;
139  double near_plane; // Windows #defines "near"
140  double far_plane; // Windows #defines "far"
141  } ortho;
142  struct {
144  double fov;
145  double aspect;
146  double near_plane;
147  double far_plane;
148  } perspective;
149  struct {
150  double fx;
151  double fy;
152  double cx;
153  double cy;
154  double near_plane;
155  double far_plane;
156  double width;
157  double height;
158  } intrinsics;
159  } proj;
160  };
161  virtual const ProjectionInfo& GetProjection() const = 0;
162 
163  virtual void CopyFrom(const Camera* camera) = 0;
164 
169  static void SetupCameraAsPinholeCamera(
170  rendering::Camera& camera,
171  const Eigen::Matrix3d& intrinsic,
172  const Eigen::Matrix4d& extrinsic,
173  int intrinsic_width_px,
174  int intrinsic_height_px,
175  const geometry::AxisAlignedBoundingBox& scene_bounds);
176 
178  static float CalcNearPlane();
179 
182  static float CalcFarPlane(
183  const rendering::Camera& camera,
184  const geometry::AxisAlignedBoundingBox& scene_bounds);
185 };
186 
187 } // namespace rendering
188 } // namespace visualization
189 } // namespace open3d
Eigen::Transform< float, 3, Eigen::Projective > ProjectionMatrix
Definition: Camera.h:45
A bounding box that is aligned along the coordinate axes.
Definition: BoundingVolume.h:150
GLMatrix4f LookAt(const Eigen::Vector3d &eye, const Eigen::Vector3d &lookat, const Eigen::Vector3d &up)
Definition: GLHelper.cpp:36
Eigen::Transform< float, 3, Eigen::Affine > Transform
Definition: Camera.h:44
GLMatrix4f Perspective(double field_of_view_, double aspect, double z_near, double z_far)
Definition: GLHelper.cpp:55
Definition: PinholeCameraIntrinsic.cpp:35
int height
Definition: FilePCD.cpp:72
void Unproject(const core::Tensor &depth, utility::optional< std::reference_wrapper< const core::Tensor >> image_colors, core::Tensor &points, utility::optional< std::reference_wrapper< core::Tensor >> colors, const core::Tensor &intrinsics, const core::Tensor &extrinsics, float depth_scale, float depth_max, int64_t stride)
Definition: PointCloud.cpp:42
GLMatrix4f Ortho(double left, double right, double bottom, double top, double z_near, double z_far)
Definition: GLHelper.cpp:70
int width
Definition: FilePCD.cpp:71