HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
camera.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_BASE_GF_CAMERA_H
8 #define PXR_BASE_GF_CAMERA_H
9 
10 /// \file gf/camera.h
11 /// \ingroup group_gf_BasicGeometry
12 
13 #include "pxr/pxr.h"
14 #include "pxr/base/gf/matrix4d.h"
15 #include "pxr/base/gf/range1f.h"
16 #include "pxr/base/gf/vec4f.h"
17 #include "pxr/base/gf/api.h"
18 
19 #include <vector>
20 
22 
23 class GfFrustum;
24 
25 /// \class GfCamera
26 /// \ingroup group_gf_BasicGeometry
27 /// \brief Object-based representation of a camera.
28 ///
29 /// This class provides a thin wrapper on the camera data model,
30 /// with a small number of computations.
31 ///
32 class GfCamera
33 {
34 public:
35  /// Projection type.
36  enum Projection {
39  };
40 
41  /// Direction used for Field of View or orthographic size
42  enum FOVDirection {
45  };
46 
47  /// The unit for horizontal and vertical aperture is one tenth of the
48  /// world unit. Thus, if the world unit is assumed to be cm, the horizontal
49  /// and vertical aperture unit is mm.
50  GF_API static const double APERTURE_UNIT;
51  /// The unit for focal length. Similar to APERTURE_UNIT.
52  GF_API static const double FOCAL_LENGTH_UNIT;
53 
54  /// Default horizontal and vertical aperture, based on a 35mm
55  /// (non-anamorphic) projector aperture (0.825 x 0602 inches, converted to
56  /// mm).
57  GF_API static const double DEFAULT_HORIZONTAL_APERTURE;
58  GF_API static const double DEFAULT_VERTICAL_APERTURE;
59 
60 public:
62  const GfMatrix4d &transform = GfMatrix4d(1.0),
63  Projection projection = Perspective,
64  float horizontalAperture = DEFAULT_HORIZONTAL_APERTURE,
65  float verticalAperture = DEFAULT_VERTICAL_APERTURE,
66  float horizontalApertureOffset = 0.0,
67  float verticalApertureOffset = 0.0,
68  float focalLength = 50.0,
69  const GfRange1f &clippingRange = GfRange1f(1, 1000000),
70  const std::vector<GfVec4f> &clippingPlanes = std::vector<GfVec4f>(),
71  float fStop = 0.0,
72  float focusDistance = 0.0);
73 
74  /// Sets the transform of the filmback in world space to \p val.
75  GF_API void SetTransform(const GfMatrix4d &val);
76 
77  /// Sets the projection type.
78  GF_API void SetProjection(const Projection &val);
79 
80  /// \name Physics based camera setup
81 
82  /// These are the values actually stored in the class and they correspond
83  /// to measurements of an actual physical camera (in mm).
84  /// Together with the clipping range, they determine the camera frustum.
85 
86  /// @{
87 
88  /// Sets the focal length in tenths of a world unit (e.g., mm if the world
89  /// unit is assumed to be cm).
90  GF_API void SetFocalLength(const float val);
91 
92  /// Sets the width of the projector aperture in tenths of a world unit
93  /// (e.g., mm if the world unit is assumed to be cm).
94  GF_API void SetHorizontalAperture(const float val);
95 
96  /// Sets the height of the projector aperture in tenths of a world unit
97  /// (e.g., mm if the world unit is assumed to be cm).
98  GF_API void SetVerticalAperture(const float val);
99 
100  /// Sets the horizontal offset of the projector aperture in tenths of a
101  /// world unit (e.g., mm if the world unit is assumed to be cm).
102  GF_API void SetHorizontalApertureOffset(const float val);
103 
104  /// Sets the vertical offset of the projector aperture in tenths of a
105  /// world unit (e.g., mm if the world unit is assumed to be cm).
106  GF_API void SetVerticalApertureOffset(const float val);
107  /// @}
108 
109  /// \name Frustum geometry setup
110  /// @{
111 
112  /// Sets the frustum to be projective with the given \p aspectRatio
113  /// and horizontal, respectively, vertical field of view \p fieldOfView
114  /// (similar to gluPerspective when direction = FOVVertical).
115  ///
116  /// Do not pass values for \p horionztalAperture unless you care about
117  /// DepthOfField.
118 
120  float aspectRatio,
121  float fieldOfView,
123  float horizontalAperture = DEFAULT_HORIZONTAL_APERTURE);
124 
125  /// Sets the frustum to be orthographic such that it has the given
126  /// \p aspectRatio and such that the orthographic width, respectively,
127  /// orthographic height (in cm) is equal to \p orthographicSize
128  /// (depending on direction).
129 
131  float aspectRatio, float orthographicSize, FOVDirection direction);
132 
133  /// Sets the camera from a view and projection matrix.
134  ///
135  /// Note that the projection matrix does only determine the ratio
136  /// of aperture to focal length, so there is a choice which defaults
137  /// to 50mm (or more accurately, 50 tenths of a world unit).
138 
140  const GfMatrix4d &viewMatrix, const GfMatrix4d &projMatix,
141  const float focalLength = 50.0);
142 
143  /// @}
144 
145  /// Sets the clipping range in world units.
146  GF_API void SetClippingRange(const GfRange1f &val);
147 
148  /// Sets additional arbitrarily oriented clipping planes.
149  /// A vector (a,b,c,d) encodes a clipping plane that clips off points
150  /// (x,y,z) with
151  ///
152  /// a * x + b * y + c * z + d * 1 < 0
153  ///
154  /// where (x,y,z) are the coordinates in the camera's space.
155  GF_API void SetClippingPlanes(const std::vector<GfVec4f> &val);
156 
157  /// Sets the lens aperture, unitless.
158  GF_API void SetFStop(const float val);
159 
160  /// Sets the focus distance in world units.
161  GF_API void SetFocusDistance(const float val);
162 
163  /// Returns the transform of the filmback in world space. This is
164  /// exactly the transform specified via SetTransform().
166 
167  /// Returns the projection type.
169 
170  /// Returns the width of the projector aperture in tenths of a world unit
171  /// (e.g., mm if the world unit is assumed to be cm).
172  GF_API float GetHorizontalAperture() const;
173 
174  /// Returns the height of the projector aperture in tenths of a world unit
175  /// (e.g., mm if the world unit is assumed to be cm).
176  GF_API float GetVerticalAperture() const;
177 
178  /// Returns the horizontal offset of the projector aperture in tenths of a
179  /// world unit (e.g., mm if the world unit is assumed to be cm).
180  /// In particular, an offset is necessary when writing out a stereo camera
181  /// with finite convergence distance as two cameras.
182  GF_API float GetHorizontalApertureOffset() const;
183 
184  /// Returns the vertical offset of the projector aperture in tenths of a
185  /// world unit (e.g., mm if the world unit is assumed to be cm).
186  GF_API float GetVerticalApertureOffset() const;
187 
188  /// Returns the projector aperture aspect ratio.
189  GF_API float GetAspectRatio() const;
190 
191  /// Returns the focal length in tenths of a world unit (e.g., mm if the
192  /// world unit is assumed to be cm).
193  GF_API float GetFocalLength() const;
194 
195  /// Returns the horizontal or vertical field of view in degrees.
197 
198  /// Returns the clipping range in world units.
200 
201  /// Returns additional clipping planes.
202  GF_API const std::vector<GfVec4f> &GetClippingPlanes() const;
203 
204  /// Returns the computed, world-space camera frustum. The frustum
205  /// will always be that of a Y-up, -Z-looking camera.
206  GF_API GfFrustum GetFrustum() const;
207 
208  /// Returns the lens aperture.
209  GF_API float GetFStop() const;
210 
211  /// Returns the focus distance in world units.
212  GF_API float GetFocusDistance() const;
213 
214  /// Equality operator. true iff all parts match.
215  GF_API bool operator==(const GfCamera& other) const;
216 
217  // Inequality operator. true iff not equality.
218  GF_API bool operator!=(const GfCamera& other) const;
219 
220 private:
221  // frustum
222  GfMatrix4d _transform;
223  Projection _projection;
224  float _horizontalAperture;
225  float _verticalAperture;
226  float _horizontalApertureOffset;
227  float _verticalApertureOffset;
228  float _focalLength;
229  GfRange1f _clippingRange;
230  std::vector<GfVec4f> _clippingPlanes;
231 
232  // focus
233  float _fStop;
234  float _focusDistance;
235 };
236 
238 
239 #endif // PXR_BASE_GF_CAMERA_H
GF_API const std::vector< GfVec4f > & GetClippingPlanes() const
Returns additional clipping planes.
FOVDirection
Direction used for Field of View or orthographic size.
Definition: camera.h:42
GF_API void SetVerticalAperture(const float val)
Object-based representation of a camera.
Definition: camera.h:32
GF_API float GetHorizontalAperture() const
IMF_EXPORT IMATH_NAMESPACE::V3f direction(const IMATH_NAMESPACE::Box2i &dataWindow, const IMATH_NAMESPACE::V2f &pixelPosition)
GF_API void SetProjection(const Projection &val)
Sets the projection type.
GF_API GfFrustum GetFrustum() const
GF_API void SetHorizontalApertureOffset(const float val)
GF_API GfMatrix4d GetTransform() const
static GF_API const double DEFAULT_VERTICAL_APERTURE
Definition: camera.h:58
GF_API void SetFocalLength(const float val)
GF_API void SetClippingRange(const GfRange1f &val)
Sets the clipping range in world units.
GF_API float GetFStop() const
Returns the lens aperture.
GF_API void SetPerspectiveFromAspectRatioAndFieldOfView(float aspectRatio, float fieldOfView, FOVDirection direction, float horizontalAperture=DEFAULT_HORIZONTAL_APERTURE)
GF_API void SetFocusDistance(const float val)
Sets the focus distance in world units.
GF_API float GetVerticalApertureOffset() const
GF_API GfCamera(const GfMatrix4d &transform=GfMatrix4d(1.0), Projection projection=Perspective, float horizontalAperture=DEFAULT_HORIZONTAL_APERTURE, float verticalAperture=DEFAULT_VERTICAL_APERTURE, float horizontalApertureOffset=0.0, float verticalApertureOffset=0.0, float focalLength=50.0, const GfRange1f &clippingRange=GfRange1f(1, 1000000), const std::vector< GfVec4f > &clippingPlanes=std::vector< GfVec4f >(), float fStop=0.0, float focusDistance=0.0)
GF_API void SetTransform(const GfMatrix4d &val)
Sets the transform of the filmback in world space to val.
GF_API void SetVerticalApertureOffset(const float val)
GF_API Projection GetProjection() const
Returns the projection type.
GF_API float GetAspectRatio() const
Returns the projector aperture aspect ratio.
GF_API float GetHorizontalApertureOffset() const
GF_API void SetFStop(const float val)
Sets the lens aperture, unitless.
static GF_API const double FOCAL_LENGTH_UNIT
The unit for focal length. Similar to APERTURE_UNIT.
Definition: camera.h:52
GA_API const UT_StringHolder transform
static GF_API const double DEFAULT_HORIZONTAL_APERTURE
Definition: camera.h:57
GF_API void SetClippingPlanes(const std::vector< GfVec4f > &val)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
GF_API void SetFromViewAndProjectionMatrix(const GfMatrix4d &viewMatrix, const GfMatrix4d &projMatix, const float focalLength=50.0)
static GF_API const double APERTURE_UNIT
Definition: camera.h:50
GF_API float GetFocusDistance() const
Returns the focus distance in world units.
GLuint GLfloat * val
Definition: glcorearb.h:1608
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
GF_API float GetFieldOfView(FOVDirection direction) const
Returns the horizontal or vertical field of view in degrees.
GF_API bool operator!=(const GfCamera &other) const
Projection
Projection type.
Definition: camera.h:36
GF_API void SetOrthographicFromAspectRatioAndSize(float aspectRatio, float orthographicSize, FOVDirection direction)
GF_API float GetVerticalAperture() const
GF_API GfRange1f GetClippingRange() const
Returns the clipping range in world units.
GF_API float GetFocalLength() const
GF_API bool operator==(const GfCamera &other) const
Equality operator. true iff all parts match.
GF_API void SetHorizontalAperture(const float val)
#define GF_API
Definition: api.h:23