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 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 GF_RAY_H
25 #define GF_RAY_H
26 
29 
30 #include "pxr/pxr.h"
31 #include "pxr/base/gf/matrix4d.h"
32 #include "pxr/base/gf/api.h"
33 
34 #include <float.h>
35 #include <limits>
36 #include <iosfwd>
37 
38 PXR_NAMESPACE_OPEN_SCOPE
39 
40 class GfLine;
41 class GfLineSeg;
42 class GfPlane;
43 class GfRange3d;
44 
60 class GfRay {
61 
62  public:
63 
65  GfRay() {
66  }
67 
69  GfRay(const GfVec3d &startPoint, const GfVec3d &direction) {
70  SetPointAndDirection(startPoint, direction);
71  }
72 
74  GF_API
75  void SetPointAndDirection(const GfVec3d &startPoint,
76  const GfVec3d &direction);
77 
79  GF_API
80  void SetEnds(const GfVec3d &startPoint, const GfVec3d &endPoint);
81 
83  const GfVec3d & GetStartPoint() const {
84  return _startPoint;
85  }
86 
89  const GfVec3d & GetDirection() const {
90  return _direction;
91  }
92 
95  GfVec3d GetPoint(double distance) const {
96  return _startPoint + distance * _direction;
97  }
98 
100  GF_API
101  GfRay & Transform(const GfMatrix4d &matrix);
102 
106  GF_API
107  GfVec3d FindClosestPoint(const GfVec3d &point,
108  double *rayDistance = NULL) const;
109 
112  bool operator ==(const GfRay &r) const {
113  return (_startPoint == r._startPoint &&
114  _direction == r._direction);
115  }
116 
119  bool operator !=(const GfRay &r) const {
120  return ! (*this == r);
121  }
122 
128 
150  GF_API
151  bool Intersect(const GfVec3d &p0,
152  const GfVec3d &p1,
153  const GfVec3d &p2,
154  double *distance = NULL,
155  GfVec3d *barycentricCoords = NULL,
156  bool *frontFacing = NULL,
157  double maxDist = std::numeric_limits<double>::infinity())
158  const;
159 
167  GF_API
168  bool Intersect(const GfPlane &plane, double *distance = NULL,
169  bool *frontFacing = NULL) const;
170 
175  GF_API
176  bool Intersect(const GfRange3d &box,
177  double *enterDistance = NULL,
178  double *exitDistance = NULL) const;
179 
184  GF_API
185  bool Intersect(const GfVec3d &center, double radius,
186  double *enterDistance = NULL,
187  double *exitDistance = NULL ) const;
188 
197  GF_API
198  bool Intersect(const GfVec3d &origin,
199  const GfVec3d &axis,
200  const double radius,
201  double *enterDistance = NULL,
202  double *exitDistance = NULL) const;
203 
213  GF_API
214  bool Intersect(const GfVec3d &origin,
215  const GfVec3d &axis,
216  const double radius,
217  const double height,
218  double *enterDistance = NULL,
219  double *exitDistance = NULL) const;
221 
222  private:
223  GF_API
224  friend bool GfFindClosestPoints( const GfRay &, const GfLine &,
225  GfVec3d *, GfVec3d *,
226  double *, double * );
227  GF_API
228  friend bool GfFindClosestPoints( const GfRay &, const GfLineSeg &,
229  GfVec3d *, GfVec3d *,
230  double *, double * );
231 
235  bool _SolveQuadratic(const double a,
236  const double b,
237  const double c,
238  double *enterDistance = NULL,
239  double *exitDistance = NULL) const;
240 
242  GfVec3d _startPoint;
244  GfVec3d _direction;
245 };
246 
254 GF_API
255 bool GfFindClosestPoints( const GfRay &ray, const GfLine &line,
256  GfVec3d *rayPoint = nullptr,
257  GfVec3d *linePoint = nullptr,
258  double *rayDistance = nullptr,
259  double *lineDistance = nullptr );
260 
268 GF_API
269 bool GfFindClosestPoints( const GfRay &ray, const GfLineSeg &seg,
270  GfVec3d *rayPoint = nullptr,
271  GfVec3d *segPoint = nullptr,
272  double *rayDistance = nullptr,
273  double *segDistance = nullptr );
274 
277 GF_API std::ostream& operator<<(std::ostream&, const GfRay&);
278 
279 PXR_NAMESPACE_CLOSE_SCOPE
280 
281 #endif // GF_RAY_H
bool operator!=(const GfRay &r) const
Component-wise inequality test.
Definition: ray.h:119
Basic type: 3-dimensional floating point range.
Definition: range3d.h:64
GF_API void SetPointAndDirection(const GfVec3d &startPoint, const GfVec3d &direction)
Sets the ray by specifying a starting point and a direction.
GF_API GfRay & Transform(const GfMatrix4d &matrix)
Transforms the ray by the given matrix.
GfRay()
The default constructor leaves the ray parameters undefined.
Definition: ray.h:65
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
Intersects the ray with the triangle formed by points p0, p1, and p2, returning true if it hits...
GF_API bool GfFindClosestPoints(const GfLine &l1, const GfLine &l2, GfVec3d *p1=nullptr, GfVec3d *p2=nullptr, double *t1=nullptr, double *t2=nullptr)
Computes the closets points between two lines.
Basic type: 3-dimensional plane.
Definition: plane.h:52
const GfVec3d & GetDirection() const
Returns the direction vector of the segment.
Definition: ray.h:89
const GfVec3d & GetStartPoint() const
Returns the starting point of the segment.
Definition: ray.h:83
GfRay(const GfVec3d &startPoint, const GfVec3d &direction)
This constructor takes a starting point and a direction.
Definition: ray.h:69
Stores a 4x4 matrix of double elements.
Definition: matrix4d.h:88
bool operator==(const GfRay &r) const
Component-wise equality test.
Definition: ray.h:112
Basic type: 3D line.
Definition: line.h:49
GfVec3d GetPoint(double distance) const
Returns the point that is distance units from the starting point along the direction vector...
Definition: ray.h:95
GF_API std::ostream & operator<<(std::ostream &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].
GF_API GfVec3d FindClosestPoint(const GfVec3d &point, double *rayDistance=NULL) const
Returns the point on the ray that is closest to point.
Basic type: 3D line segment.
Definition: lineSeg.h:47
Basic type for a vector of 3 double components.
Definition: vec3d.h:63
Basic type: Ray used for intersection testing.
Definition: ray.h:60
GF_API friend bool GfFindClosestPoints(const GfRay &, const GfLine &, GfVec3d *, GfVec3d *, double *, double *)
Computes the closest points between a ray and a line.
GF_API void SetEnds(const GfVec3d &startPoint, const GfVec3d &endPoint)
Sets the ray by specifying a starting point and an ending point.