HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
frustum.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_BASE_GF_FRUSTUM_H
25 #define PXR_BASE_GF_FRUSTUM_H
26 
27 /// \file gf/frustum.h
28 /// \ingroup group_gf_BasicGeometry
29 
30 #include "pxr/pxr.h"
31 #include "pxr/base/gf/bbox3d.h"
32 #include "pxr/base/gf/matrix4d.h"
33 #include "pxr/base/gf/plane.h"
34 #include "pxr/base/gf/ray.h"
35 #include "pxr/base/gf/range1d.h"
36 #include "pxr/base/gf/range2d.h"
37 #include "pxr/base/gf/rotation.h"
38 #include "pxr/base/gf/vec2d.h"
39 #include "pxr/base/gf/vec3d.h"
40 #include "pxr/base/gf/api.h"
41 #include "pxr/base/tf/hash.h"
42 
43 #include <array>
44 #include <atomic>
45 #include <iosfwd>
46 #include <vector>
47 
49 
50 /// \class GfFrustum
51 /// \ingroup group_gf_BasicGeometry
52 ///
53 /// Basic type: View frustum.
54 ///
55 /// This class represents a viewing frustum in three dimensional eye space. It
56 /// may represent either a parallel (orthographic) or perspective projection.
57 /// One can think of the frustum as being defined by 6 boundary planes.
58 ///
59 /// The frustum is specified using these parameters:
60 /// \li The \em position of the viewpoint.
61 /// \li The \em rotation applied to the default view frame, which is
62 /// looking along the -z axis with the +y axis as the "up"
63 /// direction.
64 /// \li The 2D \em window on the reference plane that defines the left,
65 /// right, top, and bottom planes of the viewing frustum, as
66 /// described below.
67 /// \li The distances to the \em near and \em far planes.
68 /// \li The \em projection \em type
69 /// \li The view distance.
70 ///
71 /// The window and near/far parameters combine to define the view frustum as
72 /// follows. Transform the -z axis and the +y axis by the frustum rotation to
73 /// get the world-space \em view \em direction and \em up \em direction. Now
74 /// consider the \em reference \em plane that is perpendicular to the view
75 /// direction, a distance of referencePlaneDepth from the viewpoint, and whose
76 /// y axis corresponds to the up direction. The window rectangle is specified
77 /// in a 2D coordinate system embedded in this plane. The origin of the
78 /// coordinate system is the point at which the view direction vector
79 /// intersects the plane. Therefore, the point (0,1) in this plane is found by
80 /// moving 1 unit along the up direction vector in this plane. The vector from
81 /// the viewpoint to the resulting point will form a 45-degree angle with the
82 /// view direction.
83 ///
84 /// The view distance is only useful for interactive applications. It can be
85 /// used to compute a look at point which is useful when rotating around an
86 /// object of interest.
87 ///
88 class GfFrustum {
89  public:
90  /// This enum is used to determine the type of projection represented by a
91  /// frustum.
93  Orthographic, ///< Orthographic projection
94  Perspective, ///< Perspective projection
95  };
96 
97  /// This constructor creates an instance with default viewing parameters:
98  /// \li The position is the origin.
99  /// \li The rotation is the identity rotation. (The view is along
100  /// the -z axis, with the +y axis as "up").
101  /// \li The window is -1 to +1 in both dimensions.
102  /// \li The near/far interval is (1, 10).
103  /// \li The view distance is 5.0.
104  /// \li The projection type is \c GfFrustum::Perspective.
105  GF_API GfFrustum();
106 
107  /// Copy constructor.
109  : _position(o._position)
110  , _rotation(o._rotation)
111  , _window(o._window)
112  , _nearFar(o._nearFar)
113  , _viewDistance(o._viewDistance)
114  , _projectionType(o._projectionType)
115  , _planes(nullptr) {
116  if (auto *planes = o._planes.load()) {
117  _planes = new std::array<GfPlane, 6>(*planes);
118  }
119  }
120 
121  /// Move constructor.
122  GfFrustum(GfFrustum &&o) noexcept
123  : _position(o._position)
124  , _rotation(o._rotation)
125  , _window(o._window)
126  , _nearFar(o._nearFar)
127  , _viewDistance(o._viewDistance)
128  , _projectionType(o._projectionType)
129  , _planes(nullptr) {
130  if (auto *planes =
131  o._planes.exchange(nullptr, std::memory_order_relaxed)) {
132  _planes = planes;
133  }
134  }
135 
136  /// This constructor creates an instance with the given viewing
137  /// parameters.
139  const GfRange2d &window, const GfRange1d &nearFar,
140  GfFrustum::ProjectionType projectionType,
141  double viewDistance = 5.0);
142 
143  /// This constructor creates an instance from a camera matrix (always of a
144  /// y-Up camera, also see SetPositionAndRotationFromMatrix) and the given
145  /// viewing parameters.
146  GF_API GfFrustum(const GfMatrix4d &camToWorldXf,
147  const GfRange2d &window, const GfRange1d &nearFar,
148  GfFrustum::ProjectionType projectionType,
149  double viewDistance = 5.0);
150 
151  /// Copy assignment.
152  GfFrustum &operator=(GfFrustum const &o) noexcept {
153  if (this == &o) {
154  return *this;
155  }
156  _position = o._position;
157  _rotation = o._rotation;
158  _window = o._window;
159  _nearFar = o._nearFar;
160  _viewDistance = o._viewDistance;
161  _projectionType = o._projectionType;
162  delete _planes.load(std::memory_order_relaxed);
163  if (auto *planes = o._planes.load(std::memory_order_relaxed)) {
164  _planes.store(new std::array<GfPlane, 6>(*planes),
165  std::memory_order_relaxed);
166  }
167  else {
168  _planes.store(nullptr, std::memory_order_relaxed);
169  }
170  return *this;
171  }
172 
173  /// Move assignment.
174  GfFrustum &operator=(GfFrustum &&o) noexcept {
175  if (this == &o) {
176  return *this;
177  }
178  _position = o._position;
179  _rotation = o._rotation;
180  _window = o._window;
181  _nearFar = o._nearFar;
182  _viewDistance = o._viewDistance;
183  _projectionType = o._projectionType;
184  delete _planes.load(std::memory_order_relaxed);
185  _planes.store(o._planes.load(std::memory_order_relaxed),
186  std::memory_order_relaxed);
187  o._planes.store(nullptr, std::memory_order_relaxed);
188  return *this;
189  }
190 
191  friend inline size_t hash_value(const GfFrustum &f) {
192  return TfHash::Combine(
193  f._position,
194  f._rotation,
195  f._window,
196  f._nearFar,
197  f._viewDistance,
198  f._projectionType
199  );
200  }
201 
202  // Equality operator. true iff all parts match.
203  bool operator ==(const GfFrustum& f) const {
204  if (_position != f._position) return false;
205  if (_rotation != f._rotation) return false;
206  if (_window != f._window) return false;
207  if (_nearFar != f._nearFar) return false;
208  if (_viewDistance != f._viewDistance) return false;
209  if (_projectionType != f._projectionType) return false;
210 
211  return true;
212  }
213 
214  // Inequality operator. true iff not equality.
215  bool operator !=(const GfFrustum& f) const {
216  return !(*this == f);
217  }
218 
219  /// Destructor.
220  GF_API ~GfFrustum();
221 
222  /// \name Value setting and access
223  /// The methods in this group set and access the values that are used to
224  /// define a frustum.
225  ///@{
226 
227  /// Sets the position of the frustum in world space.
228  void SetPosition(const GfVec3d &position) {
229  _position = position;
230  _DirtyFrustumPlanes();
231  }
232 
233  /// Returns the position of the frustum in world space.
234  const GfVec3d & GetPosition() const {
235  return _position;
236  }
237 
238  /// Sets the orientation of the frustum in world space as a rotation to
239  /// apply to the default frame: looking along the -z axis with the +y axis
240  /// as "up".
242  _rotation = rotation;
243  _DirtyFrustumPlanes();
244  }
245 
246  /// Returns the orientation of the frustum in world space as a rotation to
247  /// apply to the -z axis.
248  const GfRotation & GetRotation() const {
249  return _rotation;
250  }
251 
252  /// Sets the position and rotation of the frustum from a camera matrix
253  /// (always from a y-Up camera). The resulting frustum's transform will
254  /// always represent a right-handed and orthonormal coordinate sytem
255  /// (scale, shear, and projection are removed from the given \p
256  /// camToWorldXf).
257  GF_API void SetPositionAndRotationFromMatrix(const GfMatrix4d &camToWorldXf);
258 
259  /// Sets the window rectangle in the reference plane that defines the
260  /// left, right, top, and bottom planes of the frustum.
261  void SetWindow(const GfRange2d &window) {
262  _window = window;
263  _DirtyFrustumPlanes();
264  }
265 
266  /// Returns the window rectangle in the reference plane.
267  const GfRange2d & GetWindow() const {
268  return _window;
269  }
270 
271  /// Returns the depth of the reference plane.
272  static double GetReferencePlaneDepth() {
273  return 1.0;
274  }
275 
276  /// Sets the near/far interval.
277  void SetNearFar(const GfRange1d &nearFar) {
278  _nearFar = nearFar;
279  _DirtyFrustumPlanes();
280  }
281 
282  /// Returns the near/far interval.
283  const GfRange1d & GetNearFar() const {
284  return _nearFar;
285  }
286 
287  /// Sets the view distance.
288  void SetViewDistance(double viewDistance) {
289  _viewDistance = viewDistance;
290  }
291 
292  /// Returns the view distance.
293  double GetViewDistance() const {
294  return _viewDistance;
295  }
296 
297  /// Sets the projection type.
299  _projectionType = projectionType;
300  _DirtyFrustumPlanes();
301  }
302 
303  /// Returns the projection type.
305  return _projectionType;
306  }
307 
308  ///@}
309 
310  /// \name Convenience methods
311  ///
312  /// The methods in this group allow the frustum's data to be accessed and
313  /// modified in terms of different representations that may be more
314  /// convenient for certain applications.
315  ///
316  ///@{
317 
318  /// Sets up the frustum in a manner similar to \c gluPerspective().
319  ///
320  /// It sets the projection type to \c GfFrustum::Perspective and sets the
321  /// window specification so that the resulting symmetric frustum encloses
322  /// an angle of \p fieldOfViewHeight degrees in the vertical direction,
323  /// with \p aspectRatio used to figure the angle in the horizontal
324  /// direction. The near and far distances are specified as well. The
325  /// window coordinates are computed as:
326  /// \code
327  /// top = tan(fieldOfViewHeight / 2)
328  /// bottom = -top
329  /// right = top * aspectRatio
330  /// left = -right
331  /// near = nearDistance
332  /// far = farDistance
333  /// \endcode
334  ///
335  GF_API void SetPerspective(double fieldOfViewHeight,
336  double aspectRatio,
337  double nearDistance, double farDistance);
338 
339  /// Sets up the frustum in a manner similar to gluPerspective().
340  ///
341  /// It sets the projection type to \c GfFrustum::Perspective and
342  /// sets the window specification so that:
343  ///
344  /// If \a isFovVertical is true, the resulting symmetric frustum encloses
345  /// an angle of \p fieldOfView degrees in the vertical direction, with \p
346  /// aspectRatio used to figure the angle in the horizontal direction.
347  ///
348  /// If \a isFovVertical is false, the resulting symmetric frustum encloses
349  /// an angle of \p fieldOfView degrees in the horizontal direction, with
350  /// \p aspectRatio used to figure the angle in the vertical direction.
351  ///
352  /// The near and far distances are specified as well. The window
353  /// coordinates are computed as follows:
354  ///
355  /// \li if isFovVertical:
356  /// \li top = tan(fieldOfView / 2)
357  /// \li right = top * aspectRatio
358  /// \li if NOT isFovVertical:
359  /// \li right = tan(fieldOfView / 2)
360  /// \li top = right / aspectRation
361  /// \li bottom = -top
362  /// \li left = -right
363  /// \li near = nearDistance
364  /// \li far = farDistance
365  ///
366  GF_API void SetPerspective(double fieldOfView,
367  bool isFovVertical,
368  double aspectRatio,
369  double nearDistance, double farDistance);
370 
371  /// Returns the current frustum in the format used by \c SetPerspective().
372  /// If the current frustum is not a perspective projection, this returns
373  /// \c false and leaves the parameters untouched.
374  GF_API bool GetPerspective(double *fieldOfViewHeight,
375  double *aspectRatio,
376  double *nearDistance,
377  double *farDistance) const;
378 
379  /// Returns the current frustum in the format used by \c SetPerspective().
380  /// If the current frustum is not a perspective projection, this returns
381  /// \c false and leaves the parameters untouched.
382  GF_API bool GetPerspective(bool isFovVertical,
383  double *fieldOfView,
384  double *aspectRatio,
385  double *nearDistance,
386  double *farDistance) const;
387 
388  /// Returns the horizontal or vertical fov of the frustum. The fov of the
389  /// frustum is not necessarily the same value as displayed in the viewer.
390  /// The displayed fov is a function of the focal length or FOV avar. The
391  /// frustum's fov may be different due to things like lens breathing.
392  ///
393  /// If the frustum is not of type \c GfFrustum::Perspective, the returned
394  /// FOV will be 0.0.
395  ///
396  /// \note The default value for \c isFovVertical is false so calling \c
397  /// GetFOV without an argument will return the horizontal field of view
398  /// which is compatible with menv2x's old GfFrustum::GetFOV routine.
399  GF_API double GetFOV(bool isFovVertical = false);
400 
401  /// Sets up the frustum in a manner similar to \c glOrtho().
402  ///
403  /// Sets the projection to \c GfFrustum::Orthographic and sets the window
404  /// and near/far specifications based on the given values.
405  GF_API
406  void SetOrthographic(double left, double right,
407  double bottom, double top,
408  double nearPlane, double farPlane);
409 
410  /// Returns the current frustum in the format used by \c
411  /// SetOrthographic(). If the current frustum is not an orthographic
412  /// projection, this returns \c false and leaves the parameters untouched.
413  GF_API bool GetOrthographic(double *left, double *right,
414  double *bottom, double *top,
415  double *nearPlane, double *farPlane)
416  const;
417 
418  /// Modifies the frustum to tightly enclose a sphere with the given center
419  /// and radius, using the current view direction. The planes of the
420  /// frustum are adjusted as necessary. The given amount of slack is added
421  /// to the sphere's radius is used around the sphere to avoid boundary
422  /// problems.
423  GF_API void FitToSphere(const GfVec3d &center,
424  double radius,
425  double slack = 0.0);
426 
427  /// Transforms the frustum by the given matrix.
428  ///
429  /// The transformation matrix is applied as follows: the position and the
430  /// direction vector are transformed with the given matrix. Then the
431  /// length of the new direction vector is used to rescale the near and far
432  /// plane and the view distance. Finally, the points that define the
433  /// reference plane are transformed by the matrix. This method assures
434  /// that the frustum will not be sheared or perspective-projected.
435  ///
436  /// \note Note that this definition means that the transformed frustum
437  /// does not preserve scales very well. Do \em not use this function to
438  /// transform a frustum that is to be used for precise operations such as
439  /// intersection testing.
440  GF_API GfFrustum& Transform(const GfMatrix4d &matrix);
441 
442  /// Returns the normalized world-space view direction vector, which is
443  /// computed by rotating the -z axis by the frustum's rotation.
445 
446  /// Returns the normalized world-space up vector, which is computed by
447  /// rotating the y axis by the frustum's rotation.
449 
450  /// Computes the view frame defined by this frustum. The frame consists of
451  /// the view direction, up vector and side vector, as shown in this
452  /// diagram.
453  ///
454  /// \code
455  /// up
456  /// ^ ^
457  /// | /
458  /// | / view
459  /// |/
460  /// +- - - - > side
461  /// \endcode
462  ///
463  GF_API void ComputeViewFrame(GfVec3d *side,
464  GfVec3d *up,
465  GfVec3d *view) const;
466 
467  /// Computes and returns the world-space look-at point from the eye point
468  /// (position), view direction (rotation), and view distance.
470 
471  /// Returns a matrix that represents the viewing transformation for this
472  /// frustum. That is, it returns the matrix that converts points from
473  /// world space to eye (frustum) space.
475 
476  /// Returns a matrix that represents the inverse viewing transformation
477  /// for this frustum. That is, it returns the matrix that converts points
478  /// from eye (frustum) space to world space.
480 
481  /// Returns a GL-style projection matrix corresponding to the frustum's
482  /// projection.
484 
485  /// Returns the aspect ratio of the frustum, defined as the width of the
486  /// window divided by the height. If the height is zero or negative, this
487  /// returns 0.
488  GF_API double ComputeAspectRatio() const;
489 
490  /// Returns the world-space corners of the frustum as a vector of 8
491  /// points, ordered as:
492  /// \li Left bottom near
493  /// \li Right bottom near
494  /// \li Left top near
495  /// \li Right top near
496  /// \li Left bottom far
497  /// \li Right bottom far
498  /// \li Left top far
499  /// \li Right top far
500  GF_API
501  std::vector<GfVec3d> ComputeCorners() const;
502 
503  /// Returns the world-space corners of the intersection of the frustum
504  /// with a plane parallel to the near/far plane at distance d from the
505  /// apex, ordered as:
506  /// \li Left bottom
507  /// \li Right bottom
508  /// \li Left top
509  /// \li Right top
510  /// In particular, it gives the partial result of ComputeCorners when given
511  /// near or far distance.
512  GF_API
513  std::vector<GfVec3d> ComputeCornersAtDistance(double d) const;
514 
515  /// Returns a frustum that is a narrowed-down version of this frustum. The
516  /// new frustum has the same near and far planes, but the other planes are
517  /// adjusted to be centered on \p windowPos with the new width and height
518  /// obtained from the existing width and height by multiplying by \p size[0]
519  /// and \p size[1], respectively. Finally, the new frustum is clipped
520  /// against this frustum so that it is completely contained in the existing
521  /// frustum.
522  ///
523  /// \p windowPos is given in normalized coords (-1 to +1 in both dimensions).
524  /// \p size is given as a scalar (0 to 1 in both dimensions).
525  ///
526  /// If the \p windowPos or \p size given is outside these ranges, it may
527  /// result in returning a collapsed frustum.
528  ///
529  /// This method is useful for computing a volume to use for interactive
530  /// picking.
532  const GfVec2d &size) const;
533 
534  /// Returns a frustum that is a narrowed-down version of this frustum. The
535  /// new frustum has the same near and far planes, but the other planes are
536  /// adjusted to be centered on \p worldPoint with the new width and height
537  /// obtained from the existing width and height by multiplying by \p size[0]
538  /// and \p size[1], respectively. Finally, the new frustum is clipped
539  /// against this frustum so that it is completely contained in the existing
540  /// frustum.
541  ///
542  /// \p worldPoint is given in world space coordinates.
543  /// \p size is given as a scalar (0 to 1 in both dimensions).
544  ///
545  /// If the \p size given is outside this range, it may result in returning
546  /// a collapsed frustum.
547  ///
548  /// If the \p worldPoint is at or behind the eye of the frustum, it will
549  /// return a frustum equal to this frustum.
550  ///
551  /// This method is useful for computing a volume to use for interactive
552  /// picking.
554  const GfVec2d &size) const;
555 
556  /// Builds and returns a \c GfRay that starts at the viewpoint and extends
557  /// through the given \a windowPos given in normalized coords (-1 to +1 in
558  /// both dimensions) window position.
559  ///
560  /// Contrasted with ComputePickRay(), this method returns a ray whose
561  /// origin is the eyepoint, while that method returns a ray whose origin
562  /// is on the near plane.
563  GF_API GfRay ComputeRay(const GfVec2d &windowPos) const;
564 
565  /// Builds and returns a \c GfRay that connects the viewpoint to the given
566  /// 3d point in worldspace.
567  ///
568  /// Contrasted with ComputePickRay(), this method returns a ray whose
569  /// origin is the eyepoint, while that method returns a ray whose origin
570  /// is on the near plane.
571  GF_API GfRay ComputeRay(const GfVec3d &worldSpacePos) const;
572 
573  /// Builds and returns a \c GfRay that can be used for picking at the
574  /// given normalized (-1 to +1 in both dimensions) window position.
575  ///
576  /// Contrasted with ComputeRay(), that method returns a ray whose origin
577  /// is the eyepoint, while this method returns a ray whose origin is on
578  /// the near plane.
579  GF_API GfRay ComputePickRay(const GfVec2d &windowPos) const;
580 
581  /// Builds and returns a \c GfRay that can be used for picking that
582  /// connects the viewpoint to the given 3d point in worldspace.
583  GF_API GfRay ComputePickRay(const GfVec3d &worldSpacePos) const;
584 
585  ///@}
586 
587  /// \name Intersection methods
588  ///
589  /// The methods in this group implement intersection operations
590  /// between this frustum and a given primitive.
591  ///
592  ///@{
593 
594  /// Returns true if the given axis-aligned bbox is inside or intersecting
595  /// the frustum. Otherwise, it returns false. Useful when doing picking or
596  /// frustum culling.
597  GF_API bool Intersects(const GfBBox3d &bbox) const;
598 
599  /// Returns true if the given point is inside or intersecting the frustum.
600  /// Otherwise, it returns false.
601  GF_API bool Intersects(const GfVec3d &point) const;
602 
603  /// Returns \c true if the line segment formed by the given points is
604  /// inside or intersecting the frustum. Otherwise, it returns false.
605  GF_API bool Intersects(const GfVec3d &p0,
606  const GfVec3d &p1) const;
607 
608  /// Returns \c true if the triangle formed by the given points is inside
609  /// or intersecting the frustum. Otherwise, it returns false.
610  GF_API bool Intersects(const GfVec3d &p0,
611  const GfVec3d &p1,
612  const GfVec3d &p2) const;
613 
614  /// Returns \c true if the bbox volume intersects the view volume given by
615  /// the view-projection matrix, erring on the side of false positives for
616  /// efficiency.
617  ///
618  /// This method is intended for cases where a GfFrustum is not available
619  /// or when the view-projection matrix yields a view volume that is not
620  /// expressable as a GfFrustum.
621  ///
622  /// Because it errs on the side of false positives, it is suitable for
623  /// early-out tests such as draw or intersection culling.
624  ///
625  GF_API static bool IntersectsViewVolume(const GfBBox3d &bbox,
626  const GfMatrix4d &vpMat);
627 
628  ///@}
629 
630  private:
631  // Dirty the result of _CalculateFrustumPlanes.
632  GF_API void _DirtyFrustumPlanes();
633 
634  // Calculates cached frustum planes used for intersection tests.
635  GF_API void _CalculateFrustumPlanes() const;
636 
637  // Builds and returns a \c GfRay that can be used for picking. Given an
638  // eye position and direction in camera space, offsets the ray to emanate
639  // from the near plane, then transforms into worldspace
640  GF_API GfRay _ComputePickRayOffsetToNearPlane(
641  const GfVec3d &camSpaceFrom,
642  const GfVec3d &camSpaceDir) const;
643 
644  // Returns a frustum that is a narrowed-down version of this frustum. The
645  // new frustum has the same near and far planes, but the other planes are
646  // adjusted to be centered on \p windowPoint with the new width and height
647  // obtained from the existing width and height by multiplying by \p size[0]
648  // and \p size[1], respectively. Finally, the new frustum is clipped
649  // against this frustum so that it is completely contained in the existing
650  // frustum.
651  //
652  // \p windowPoint is given in window coordinates.
653  // \p size is given as a scalar (0 to 1 in both dimensions).
654  //
655  // If the \p size given is outside this range, it may result in returning
656  // a collapsed frustum.
657  //
658  // This method is useful for computing a volume to use for interactive
659  // picking.
660  GfFrustum _ComputeNarrowedFrustumSub(const GfVec2d windowPoint,
661  const GfVec2d &size) const;
662 
663  bool _SegmentIntersects(GfVec3d const &p0, uint32_t p0Mask,
664  GfVec3d const &p1, uint32_t p1Mask) const;
665 
666  // Position of the frustum in world space.
667  GfVec3d _position;
668 
669  // Orientation of the frustum in world space as a rotation to apply to the
670  // -z axis.
671  GfRotation _rotation;
672 
673  // Window rectangle in the image plane.
674  GfRange2d _window;
675 
676  // Near/far interval.
677  GfRange1d _nearFar;
678 
679  // View distance.
680  double _viewDistance;
681 
682  // Projection type.
683  ProjectionType _projectionType;
684 
685  // Cached planes.
686  // If null, the planes have not been calculated.
687  mutable std::atomic<std::array<GfPlane, 6> *> _planes;
688 };
689 
690 /// Output a GfFrustum using the format [(position) (rotation) [window]
691 /// [nearFar] viewDistance type]
692 ///
693 /// The "type" is "perspective", or "orthographic, depending on the
694 /// projection type of the frustum.
695 ///
696 /// \ingroup group_gf_DebuggingOutput
697 GF_API std::ostream& operator<<(std::ostream& out, const GfFrustum& f);
698 
700 
701 #endif // PXR_BASE_GF_FRUSTUM_H
bool operator==(const GfFrustum &f) const
Definition: frustum.h:203
GF_API GfFrustum()
void SetRotation(const GfRotation &rotation)
Definition: frustum.h:241
GfFrustum(GfFrustum const &o)
Copy constructor.
Definition: frustum.h:108
void SetViewDistance(double viewDistance)
Sets the view distance.
Definition: frustum.h:288
friend size_t hash_value(const GfFrustum &f)
Definition: frustum.h:191
GfFrustum & operator=(GfFrustum const &o) noexcept
Copy assignment.
Definition: frustum.h:152
static double GetReferencePlaneDepth()
Returns the depth of the reference plane.
Definition: frustum.h:272
GF_API std::vector< GfVec3d > ComputeCorners() const
GF_API std::ostream & operator<<(std::ostream &out, const GfFrustum &f)
GLint left
Definition: glcorearb.h:2005
GF_API bool Intersects(const GfBBox3d &bbox) const
GF_API GfVec3d ComputeViewDirection() const
GF_API bool GetOrthographic(double *left, double *right, double *bottom, double *top, double *nearPlane, double *farPlane) const
Perspective projection.
Definition: frustum.h:94
GF_API double ComputeAspectRatio() const
GF_API GfFrustum & Transform(const GfMatrix4d &matrix)
GLdouble right
Definition: glad.h:2817
GF_API GfRay ComputePickRay(const GfVec2d &windowPos) const
GF_API void FitToSphere(const GfVec3d &center, double radius, double slack=0.0)
const GfVec3d & GetPosition() const
Returns the position of the frustum in world space.
Definition: frustum.h:234
GF_API void SetPositionAndRotationFromMatrix(const GfMatrix4d &camToWorldXf)
Definition: vec2d.h:62
GF_API GfVec3d ComputeLookAtPoint() const
GfFrustum(GfFrustum &&o) noexcept
Move constructor.
Definition: frustum.h:122
const GfRange1d & GetNearFar() const
Returns the near/far interval.
Definition: frustum.h:283
GF_API GfMatrix4d ComputeViewMatrix() const
GF_API GfMatrix4d ComputeViewInverse() const
GLfloat f
Definition: glcorearb.h:1926
GF_API void SetOrthographic(double left, double right, double bottom, double top, double nearPlane, double farPlane)
GF_API GfMatrix4d ComputeProjectionMatrix() const
void SetWindow(const GfRange2d &window)
Definition: frustum.h:261
GfFrustum::ProjectionType GetProjectionType() const
Returns the projection type.
Definition: frustum.h:304
static GF_API bool IntersectsViewVolume(const GfBBox3d &bbox, const GfMatrix4d &vpMat)
SIM_API const UT_StringHolder rotation
double GetViewDistance() const
Returns the view distance.
Definition: frustum.h:293
GF_API bool GetPerspective(double *fieldOfViewHeight, double *aspectRatio, double *nearDistance, double *farDistance) const
bool operator!=(const GfFrustum &f) const
Definition: frustum.h:215
GF_API std::vector< GfVec3d > ComputeCornersAtDistance(double d) const
const GfRange2d & GetWindow() const
Returns the window rectangle in the reference plane.
Definition: frustum.h:267
const GfRotation & GetRotation() const
Definition: frustum.h:248
void SetProjectionType(GfFrustum::ProjectionType projectionType)
Sets the projection type.
Definition: frustum.h:298
GLint GLint bottom
Definition: glcorearb.h:2005
ProjectionType
Definition: frustum.h:92
GLsizeiptr size
Definition: glcorearb.h:664
GF_API GfRay ComputeRay(const GfVec2d &windowPos) const
static size_t Combine(Args &&...args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:519
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
GA_API const UT_StringHolder up
GF_API GfFrustum ComputeNarrowedFrustum(const GfVec2d &windowPos, const GfVec2d &size) const
SIM_API const UT_StringHolder position
Definition: vec3d.h:62
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
GF_API ~GfFrustum()
Destructor.
Definition: ray.h:61
GLdouble GLdouble GLdouble top
Definition: glad.h:2817
Definition: core.h:982
Orthographic projection.
Definition: frustum.h:93
GF_API void SetPerspective(double fieldOfViewHeight, double aspectRatio, double nearDistance, double farDistance)
GF_API double GetFOV(bool isFovVertical=false)
void SetNearFar(const GfRange1d &nearFar)
Sets the near/far interval.
Definition: frustum.h:277
GF_API GfVec3d ComputeUpVector() const
GfFrustum & operator=(GfFrustum &&o) noexcept
Move assignment.
Definition: frustum.h:174
#define GF_API
Definition: api.h:40
void SetPosition(const GfVec3d &position)
Sets the position of the frustum in world space.
Definition: frustum.h:228
GF_API void ComputeViewFrame(GfVec3d *side, GfVec3d *up, GfVec3d *view) const