HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ray.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_RAY_H
8 #define PXR_BASE_GF_RAY_H
9 
10 /// \file gf/ray.h
11 /// \ingroup group_gf_BasicGeometry
12 
13 #include "pxr/pxr.h"
14 #include "pxr/base/gf/matrix4d.h"
15 #include "pxr/base/gf/api.h"
16 
17 #include <float.h>
18 #include <limits>
19 #include <iosfwd>
20 
22 
23 class GfBBox3d;
24 class GfLine;
25 class GfLineSeg;
26 class GfPlane;
27 class GfRange3d;
28 
29 /// \class GfRay
30 /// \ingroup group_gf_BasicGeometry
31 ///
32 /// Basic type: Ray used for intersection testing
33 ///
34 /// This class represents a three-dimensional ray in space, typically
35 /// used for intersection testing. It consists of an origin and a
36 /// direction.
37 ///
38 /// Note that by default a \c GfRay does not normalize its direction
39 /// vector to unit length.
40 ///
41 /// Note for ray intersections, the start point is included in the computations,
42 /// i.e., a distance of zero is defined to be intersecting.
43 ///
44 class GfRay {
45 
46  public:
47 
48  /// The default constructor leaves the ray parameters undefined.
49  GfRay() {
50  }
51 
52  /// This constructor takes a starting point and a direction.
53  GfRay(const GfVec3d &startPoint, const GfVec3d &direction) {
54  SetPointAndDirection(startPoint, direction);
55  }
56 
57  /// Sets the ray by specifying a starting point and a direction.
58  GF_API
59  void SetPointAndDirection(const GfVec3d &startPoint,
60  const GfVec3d &direction);
61 
62  /// Sets the ray by specifying a starting point and an ending point.
63  GF_API
64  void SetEnds(const GfVec3d &startPoint, const GfVec3d &endPoint);
65 
66  /// Returns the starting point of the segment.
67  const GfVec3d & GetStartPoint() const {
68  return _startPoint;
69  }
70 
71  /// Returns the direction vector of the segment. This is not guaranteed to
72  /// be unit length.
73  const GfVec3d & GetDirection() const {
74  return _direction;
75  }
76 
77  /// Returns the point that is \p distance units from the starting point
78  /// along the direction vector, expressed in parametic distance.
79  GfVec3d GetPoint(double distance) const {
80  return _startPoint + distance * _direction;
81  }
82 
83  /// Transforms the ray by the given matrix.
84  GF_API
85  GfRay & Transform(const GfMatrix4d &matrix);
86 
87  /// Returns the point on the ray that is closest to \p point. If \p
88  /// rayDistance is not \c NULL, it will be set to the parametric distance
89  /// along the ray of the closest point.
90  GF_API
91  GfVec3d FindClosestPoint(const GfVec3d &point,
92  double *rayDistance = NULL) const;
93 
94  /// Component-wise equality test. The starting points, directions, and
95  /// lengths must match exactly for rays to be considered equal.
96  bool operator ==(const GfRay &r) const {
97  return (_startPoint == r._startPoint &&
98  _direction == r._direction);
99  }
100 
101  /// Component-wise inequality test. The starting points, directions, and
102  /// lengths must match exactly for rays to be considered equal.
103  bool operator !=(const GfRay &r) const {
104  return ! (*this == r);
105  }
106 
107  /// \name Intersection methods.
108  ///
109  /// The methods in this group intersect the ray with a geometric entity.
110  ///
111  ///@{
112 
113  /// Intersects the ray with the triangle formed by points \p p0, \p p1,
114  /// and \p p2, returning \c true if it hits. If there is an intersection,
115  /// it also returns the parametric distance to the intersection point in
116  /// \p distance, the barycentric coordinates of the intersection point in
117  /// \p barycentricCoords and the front-facing flag in \p frontFacing. The
118  /// barycentric coordinates are defined with respect to the three vertices
119  /// taken in order. The front-facing flag is \c true if the intersection
120  /// hit the side of the triangle that is formed when the vertices are
121  /// ordered counter-clockwise (right-hand rule). If any of the return
122  /// pointers are \c NULL, the corresponding values are not returned.
123  ///
124  /// If the distance to the intersection is greater than \p maxDist, then
125  /// the method will return false.
126  ///
127  /// Barycentric coordinates are defined to sum to 1 and satisfy this
128  /// relationsip:
129  /// \code
130  /// intersectionPoint = (barycentricCoords[0] * p0 +
131  /// barycentricCoords[1] * p1 +
132  /// barycentricCoords[2] * p2);
133  /// \endcode
134  GF_API
135  bool Intersect(const GfVec3d &p0,
136  const GfVec3d &p1,
137  const GfVec3d &p2,
138  double *distance = NULL,
139  GfVec3d *barycentricCoords = NULL,
140  bool *frontFacing = NULL,
141  double maxDist = std::numeric_limits<double>::infinity())
142  const;
143 
144  /// Intersects the ray with a plane, returning \c true if the ray is not
145  /// parallel to the plane and the intersection is within the ray bounds.
146  /// If there is an intersection, it also returns the parametric distance
147  /// to the intersection point in \p distance and the front-facing flag in
148  /// \p frontFacing, if they are not \c NULL. The front-facing flag is \c
149  /// true if the intersection is on the side of the plane in which its
150  /// normal points.
151  GF_API
152  bool Intersect(const GfPlane &plane, double *distance = NULL,
153  bool *frontFacing = NULL) const;
154 
155  /// Intersects the ray with an axis-aligned box, returning \c true if the
156  /// ray intersects it at all within bounds. If there is an intersection,
157  /// this also returns the parametric distances to the two intersection
158  /// points in \p enterDistance and \p exitDistance.
159  GF_API
160  bool Intersect(const GfRange3d &box,
161  double *enterDistance = NULL,
162  double *exitDistance = NULL) const;
163 
164  /// Intersects the ray with an oriented box, returning \c true if the
165  /// ray intersects it at all within bounds. If there is an intersection,
166  /// this also returns the parametric distances to the two intersection
167  /// points in \p enterDistance and \p exitDistance.
168  GF_API
169  bool Intersect(const GfBBox3d &box,
170  double *enterDistance = NULL,
171  double *exitDistance = NULL) const;
172 
173  /// Intersects the ray with a sphere, returning \c true if the ray
174  /// intersects it at all within bounds. If there is an intersection,
175  /// returns the parametric distance to the two intersection points in \p
176  /// enterDistance and \p exitDistance.
177  GF_API
178  bool Intersect(const GfVec3d &center, double radius,
179  double *enterDistance = NULL,
180  double *exitDistance = NULL ) const;
181 
182  /// Intersects the ray with an infinite cylinder, with axis \p axis,
183  /// centered at the \p origin, with radius \p radius.
184  ///
185  /// Returns \c true if the ray intersects it at all within bounds. If
186  /// there is an intersection, returns the parametric distance to the two
187  /// intersection points in \p enterDistance and \p exitDistance.
188  ///
189  /// Note this method does not validate whether the radius is valid.
190  GF_API
191  bool Intersect(const GfVec3d &origin,
192  const GfVec3d &axis,
193  const double radius,
194  double *enterDistance = NULL,
195  double *exitDistance = NULL) const;
196 
197  /// Intersects the ray with an infinite non-double cone, centered at \p
198  /// origin, with axis \p axis, radius \p radius and apex at \p height.
199  ///
200  /// Returns \c true if the ray intersects it at all within bounds. If
201  /// there is an intersection, returns the parametric distance to the two
202  /// intersection points in \p enterDistance and \p exitDistance.
203  ///
204  /// Note this method does not validate whether the radius are height are
205  /// valid.
206  GF_API
207  bool Intersect(const GfVec3d &origin,
208  const GfVec3d &axis,
209  const double radius,
210  const double height,
211  double *enterDistance = NULL,
212  double *exitDistance = NULL) const;
213  ///@}
214 
215  private:
216  GF_API
217  friend bool GfFindClosestPoints( const GfRay &, const GfLine &,
218  GfVec3d *, GfVec3d *,
219  double *, double * );
220  GF_API
221  friend bool GfFindClosestPoints( const GfRay &, const GfLineSeg &,
222  GfVec3d *, GfVec3d *,
223  double *, double * );
224 
225  /// Solves the quadratic equation returning the solutions, if defined, in
226  /// \p enterDistance and \p exitDistance, where \p enterDistance is less
227  /// than or equal to \p exitDistance.
228  bool _SolveQuadratic(const double a,
229  const double b,
230  const double c,
231  double *enterDistance = NULL,
232  double *exitDistance = NULL) const;
233 
234  /// The starting point of the ray.
235  GfVec3d _startPoint;
236  /// The direction vector.
237  GfVec3d _direction;
238 };
239 
240 /// Computes the closest points between a ray and a line. The two points are
241 /// returned in \p rayPoint and \p linePoint. The parametric distance of each
242 /// point on the lines is returned in \p rayDistance and \p lineDistance.
243 ///
244 /// This returns \c false if the lines were close enough to parallel that no
245 /// points could be computed; in this case, the other return values are
246 /// undefined.
247 GF_API
248 bool GfFindClosestPoints( const GfRay &ray, const GfLine &line,
249  GfVec3d *rayPoint = nullptr,
250  GfVec3d *linePoint = nullptr,
251  double *rayDistance = nullptr,
252  double *lineDistance = nullptr );
253 
254 /// Computes the closest points between a ray and a line segment. The two
255 /// points are returned in \p rayPoint and \p segPoint. The parametric
256 /// distance of each point is returned in \p rayDistance and \p segDistance.
257 ///
258 /// This returns \c false if the lines were close enough to parallel that no
259 /// points could be computed; in this case, the other return values are
260 /// undefined.
261 GF_API
262 bool GfFindClosestPoints( const GfRay &ray, const GfLineSeg &seg,
263  GfVec3d *rayPoint = nullptr,
264  GfVec3d *segPoint = nullptr,
265  double *rayDistance = nullptr,
266  double *segDistance = nullptr );
267 
268 /// Output a GfRay using the format [(x y z) >> (x y z)].
269 /// \ingroup group_gf_DebuggingOutput
270 GF_API std::ostream& operator<<(std::ostream&, const GfRay&);
271 
273 
274 #endif // PXR_BASE_GF_RAY_H
IMF_EXPORT IMATH_NAMESPACE::V3f direction(const IMATH_NAMESPACE::Box2i &dataWindow, const IMATH_NAMESPACE::V2f &pixelPosition)
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
const GfVec3d & GetStartPoint() const
Returns the starting point of the segment.
Definition: ray.h:67
bool operator!=(const GfRay &r) const
Definition: ray.h:103
GfRay()
The default constructor leaves the ray parameters undefined.
Definition: ray.h:49
Definition: plane.h:36
GLint GLsizei GLsizei height
Definition: glcorearb.h:103
const GfVec3d & GetDirection() const
Definition: ray.h:73
GfVec3d GetPoint(double distance) const
Definition: ray.h:79
Definition: line.h:32
GF_API void SetPointAndDirection(const GfVec3d &startPoint, const GfVec3d &direction)
Sets the ray by specifying a starting point and a direction.
GF_API bool Intersect(const GfVec3d &p0, const GfVec3d &p1, const GfVec3d &p2, double *distance=NULL, GfVec3d *barycentricCoords=NULL, bool *frontFacing=NULL, double maxDist=std::numeric_limits< double >::infinity()) const
GF_API void SetEnds(const GfVec3d &startPoint, const GfVec3d &endPoint)
Sets the ray by specifying a starting point and an ending point.
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
bool operator==(const GfRay &r) const
Definition: ray.h:96
Definition: vec3d.h:45
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
GF_API GfVec3d FindClosestPoint(const GfVec3d &point, double *rayDistance=NULL) const
Definition: ray.h:44
GLboolean r
Definition: glcorearb.h:1222
GF_API bool GfFindClosestPoints(const GfRay &ray, const GfLine &line, GfVec3d *rayPoint=nullptr, GfVec3d *linePoint=nullptr, double *rayDistance=nullptr, double *lineDistance=nullptr)
SIM_API const UT_StringHolder distance
GF_API friend bool GfFindClosestPoints(const GfRay &, const GfLine &, GfVec3d *, GfVec3d *, double *, double *)
GF_API std::ostream & operator<<(std::ostream &, const GfRay &)
GF_API GfRay & Transform(const GfMatrix4d &matrix)
Transforms the ray by the given matrix.
GfRay(const GfVec3d &startPoint, const GfVec3d &direction)
This constructor takes a starting point and a direction.
Definition: ray.h:53
#define GF_API
Definition: api.h:23