HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
line.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_LINE_H
8 #define PXR_BASE_GF_LINE_H
9 
10 /// \file gf/line.h
11 /// \ingroup group_gf_BasicGeometry
12 
13 #include "pxr/pxr.h"
14 #include "pxr/base/gf/vec3d.h"
15 
16 #include <float.h>
17 #include <iosfwd>
18 
20 
21 /// \class GfLine
22 /// \ingroup group_gf_BasicGeometry
23 ///
24 /// Basic type: 3D line
25 ///
26 /// This class represents a three-dimensional line in space. Lines are
27 /// constructed from a point, \p p0, and a direction, dir. The direction is
28 /// normalized in the constructor.
29 ///
30 /// The line is kept in a parametric represention, p = p0 + t * dir.
31 ///
32 class GfLine {
33 
34  public:
35 
36  /// The default constructor leaves line parameters undefined.
37  GfLine() {
38  }
39 
40  /// Construct a line from a point and a direction.
41  GfLine(const GfVec3d &p0, const GfVec3d &dir ) {
42  Set( p0, dir );
43  }
44 
45  double Set(const GfVec3d &p0, const GfVec3d &dir ) {
46  _p0 = p0;
47  _dir = dir;
48  return _dir.Normalize();
49  }
50 
51  /// Return the point on the line at \p ( p0 + t * dir ).
52  /// Remember dir has been normalized so t represents a unit distance.
53  GfVec3d GetPoint( double t ) const { return _p0 + _dir * t; }
54 
55  /// Return the normalized direction of the line.
56  const GfVec3d &GetDirection() const { return _dir; }
57 
58  /// Returns the point on the line that is closest to \p point. If \p t is
59  /// not \c NULL, it will be set to the parametric distance along the line
60  /// of the returned point.
61  GF_API
62  GfVec3d FindClosestPoint(const GfVec3d &point, double *t = NULL) const;
63 
64  /// Component-wise equality test. The starting points and directions,
65  /// must match exactly for lines to be considered equal.
66  bool operator ==(const GfLine &l) const {
67  return _p0 == l._p0 && _dir == l._dir;
68  }
69 
70  /// Component-wise inequality test. The starting points, and directions
71  /// must match exactly for lines to be considered equal.
72  bool operator !=(const GfLine &r) const {
73  return ! (*this == r);
74  }
75 
76  private:
77  GF_API
78  friend bool GfFindClosestPoints( const GfLine &, const GfLine &,
79  GfVec3d *, GfVec3d *,
80  double *, double * );
81  // Parametric description:
82  // l(t) = _p0 + t * _length * _dir;
83  GfVec3d _p0;
84  GfVec3d _dir;
85 };
86 
87 /// Computes the closets points between two lines.
88 ///
89 /// The two points are returned in \p p1 and \p p2. The parametric distance
90 /// of each point on the lines is returned in \p t1 and \p t2.
91 ///
92 /// This returns \c false if the lines were close enough to parallel that no
93 /// points could be computed; in this case, the other return values are
94 /// undefined.
95 GF_API
96 bool GfFindClosestPoints(const GfLine &l1, const GfLine &l2,
97  GfVec3d *p1 = nullptr, GfVec3d *p2 = nullptr,
98  double *t1 = nullptr, double *t2 = nullptr);
99 
100 /// Output a GfLine.
101 /// \ingroup group_gf_DebuggingOutput
102 GF_API std::ostream &operator<<(std::ostream&, const GfLine&);
103 
105 
106 #endif // PXR_BASE_GF_LINE_H
GF_API std::ostream & operator<<(std::ostream &, const GfLine &)
bool operator==(const GfLine &l) const
Definition: line.h:66
GF_API bool GfFindClosestPoints(const GfLine &l1, const GfLine &l2, GfVec3d *p1=nullptr, GfVec3d *p2=nullptr, double *t1=nullptr, double *t2=nullptr)
GF_API friend bool GfFindClosestPoints(const GfLine &, const GfLine &, GfVec3d *, GfVec3d *, double *, double *)
Definition: line.h:32
GfLine()
The default constructor leaves line parameters undefined.
Definition: line.h:37
double Set(const GfVec3d &p0, const GfVec3d &dir)
Definition: line.h:45
GLdouble t
Definition: glad.h:2397
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
const GfVec3d & GetDirection() const
Return the normalized direction of the line.
Definition: line.h:56
Definition: vec3d.h:45
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
bool operator!=(const GfLine &r) const
Definition: line.h:72
GF_API GfVec3d FindClosestPoint(const GfVec3d &point, double *t=NULL) const
GfLine(const GfVec3d &p0, const GfVec3d &dir)
Construct a line from a point and a direction.
Definition: line.h:41
GLboolean r
Definition: glcorearb.h:1222
GfVec3d GetPoint(double t) const
Definition: line.h:53
double Normalize(double eps=GF_MIN_VECTOR_LENGTH)
Definition: vec3d.h:252
#define GF_API
Definition: api.h:23