HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
line2d.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_LINE2D_H
8 #define PXR_BASE_GF_LINE2D_H
9 
10 /// \file gf/line2d.h
11 /// \ingroup group_gf_BasicGeometry
12 
13 #include "pxr/pxr.h"
14 #include "pxr/base/gf/vec2d.h"
15 #include "pxr/base/gf/api.h"
16 
17 #include <float.h>
18 
20 
21 /// \class GfLine2d
22 /// \ingroup group_gf_BasicGeometry
23 ///
24 /// Basic type: 2D line
25 ///
26 /// This class represents a two-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 GfLine2d {
33 
34  public:
35 
36  /// The default constructor leaves line parameters undefined.
38  }
39 
40  /// Construct a line from a point and a direction.
41  GfLine2d(const GfVec2d &p0, const GfVec2d &dir ) {
42  Set( p0, dir );
43  }
44 
45  double Set(const GfVec2d &p0, const GfVec2d &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  GfVec2d GetPoint( double t ) const { return _p0 + _dir * t; }
54 
55  /// Return the normalized direction of the line.
56  const GfVec2d &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  GfVec2d FindClosestPoint(const GfVec2d &point, double *t = NULL) const;
63 
64  /// Component-wise equality test. The starting points and directions, must
65  /// match exactly for lines to be considered equal.
66  bool operator ==(const GfLine2d &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 GfLine2d &r) const {
73  return ! (*this == r);
74  }
75 
76  private:
77  GF_API
78  friend bool GfFindClosestPoints( const GfLine2d &, const GfLine2d &,
79  GfVec2d *, GfVec2d *,
80  double *, double *);
81 
82  // Parametric description:
83  // l(t) = _p0 + t * _length * _dir;
84  GfVec2d _p0;
85  GfVec2d _dir;
86 };
87 
88 /// Computes the closets points between two lines.
89 ///
90 /// The two points are returned in \p p1 and \p p2. The parametric distance
91 /// of each point on the lines is returned in \p t1 and \p t2.
92 ///
93 /// This returns \c false if the lines were close enough to parallel that no
94 /// points could be computed; in this case, the other return values are
95 /// undefined.
96 GF_API
97 bool GfFindClosestPoints(const GfLine2d &l1, const GfLine2d &l2,
98  GfVec2d *p1 = nullptr, GfVec2d *p2 = nullptr,
99  double *t1 = nullptr, double *t2 = nullptr);
100 
102 
103 #endif // PXR_BASE_GF_LINE2D_H
double Normalize(double eps=GF_MIN_VECTOR_LENGTH)
Definition: vec2d.h:241
bool operator!=(const GfLine2d &r) const
Definition: line2d.h:72
GF_API GfVec2d FindClosestPoint(const GfVec2d &point, double *t=NULL) const
Definition: vec2d.h:45
GF_API bool GfFindClosestPoints(const GfLine2d &l1, const GfLine2d &l2, GfVec2d *p1=nullptr, GfVec2d *p2=nullptr, double *t1=nullptr, double *t2=nullptr)
GLdouble t
Definition: glad.h:2397
GfLine2d(const GfVec2d &p0, const GfVec2d &dir)
Construct a line from a point and a direction.
Definition: line2d.h:41
double Set(const GfVec2d &p0, const GfVec2d &dir)
Definition: line2d.h:45
GfVec2d GetPoint(double t) const
Definition: line2d.h:53
const GfVec2d & GetDirection() const
Return the normalized direction of the line.
Definition: line2d.h:56
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
GfLine2d()
The default constructor leaves line parameters undefined.
Definition: line2d.h:37
GLboolean r
Definition: glcorearb.h:1222
GF_API friend bool GfFindClosestPoints(const GfLine2d &, const GfLine2d &, GfVec2d *, GfVec2d *, double *, double *)
bool operator==(const GfLine2d &l) const
Definition: line2d.h:66
#define GF_API
Definition: api.h:23