HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
lineSeg2d.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_SEG2D_H
8 #define PXR_BASE_GF_LINE_SEG2D_H
9 
10 /// \file gf/lineSeg2d.h
11 /// \ingroup group_gf_BasicGeometry
12 
13 #include "pxr/pxr.h"
14 #include "pxr/base/gf/line2d.h"
15 #include "pxr/base/gf/vec2d.h"
16 #include "pxr/base/gf/api.h"
17 
18 #include <float.h>
19 
21 
22 /// \class GfLineSeg2d
23 /// \ingroup group_gf_BasicGeometry
24 ///
25 /// Basic type: 2D line segment
26 ///
27 /// This class represents a three-dimensional line segment in space.
28 ///
29 class GfLineSeg2d {
30 
31  public:
32 
33  /// The default constructor leaves line parameters undefined.
35  }
36 
37  /// Construct a line segment that spans two points.
38  GfLineSeg2d(const GfVec2d &p0, const GfVec2d &p1 ) {
39  _length = _line.Set( p0, p1 - p0 );
40  }
41 
42  /// Return the point on the segment specified by the parameter t.
43  /// p = p0 + t * (p1 - p0)
44  GfVec2d GetPoint( double t ) const {return _line.GetPoint( t * _length );}
45 
46  /// Return the normalized direction of the line.
47  const GfVec2d &GetDirection() const { return _line.GetDirection(); }
48 
49  /// Return the length of the line
50  double GetLength() const { return _length; }
51 
52  /// Returns the point on the line that is closest to \p point. If
53  /// \p t is not \c NULL, it will be set to the parametric
54  /// distance along the line of the closest point.
55  GF_API
56  GfVec2d FindClosestPoint(const GfVec2d &point, double *t = NULL) const;
57 
58  /// Component-wise equality test. The starting points and directions,
59  /// must match exactly for lines to be considered equal.
60  bool operator ==(const GfLineSeg2d &l) const {
61  return (_line == l._line && _length == l._length);
62  }
63 
64  /// Component-wise inequality test. The starting points,
65  /// and directions must match exactly for lines to be
66  /// considered equal.
67  bool operator !=(const GfLineSeg2d &r) const {
68  return ! (*this == r);
69  }
70 
71  private:
72  GF_API
73  friend bool GfFindClosestPoints( const GfLine2d &, const GfLineSeg2d &,
74  GfVec2d *, GfVec2d *,
75  double *t1, double *t2 );
76  GF_API
77  friend bool GfFindClosestPoints( const GfLineSeg2d &, const GfLineSeg2d &,
78  GfVec2d *, GfVec2d *,
79  double *, double * );
80 
81  GfLine2d _line;
82  double _length; // distance from p0 to p1
83 };
84 
85 /// Computes the closets points on \p line and \p seg.
86 ///
87 /// The two points are returned in \p p1 and \p p2. The parametric distances
88 /// of \p p1 and \p p2 along the line and segment are returned in \p t1 and \p
89 /// t2.
90 ///
91 /// This returns \c false if the lines were close enough to parallel that no
92 /// points could be computed; in this case, the other return values are
93 /// undefined.
94 GF_API
95 bool GfFindClosestPoints( const GfLine2d &line, const GfLineSeg2d &seg,
96  GfVec2d *p1 = nullptr, GfVec2d *p2 = nullptr,
97  double *t1 = nullptr, double *t2 = nullptr );
98 
99 /// Computes the closets points on two line segments, \p seg1 and \p seg2.
100 ///
101 /// The two points are returned in \p p1 and \p p2. The parametric distances
102 /// of \p p1 and \p p2 along the segments are returned in \p t1 and \p t2.
103 ///
104 /// This returns \c false if the lines were close enough to parallel that no
105 /// points could be computed; in this case, the other return values are
106 /// undefined.
107 GF_API
108 bool GfFindClosestPoints( const GfLineSeg2d &seg1, const GfLineSeg2d &seg2,
109  GfVec2d *p1 = nullptr, GfVec2d *p2 = nullptr,
110  double *t1 = nullptr, double *t2 = nullptr );
111 
113 
114 #endif // PXR_BASE_GF_LINE_SEG2D_H
GfVec2d GetPoint(double t) const
Definition: lineSeg2d.h:44
Definition: vec2d.h:45
const GfVec2d & GetDirection() const
Return the normalized direction of the line.
Definition: lineSeg2d.h:47
GF_API GfVec2d FindClosestPoint(const GfVec2d &point, double *t=NULL) const
GF_API friend bool GfFindClosestPoints(const GfLine2d &, const GfLineSeg2d &, GfVec2d *, GfVec2d *, double *t1, double *t2)
GLdouble t
Definition: glad.h:2397
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
GF_API bool GfFindClosestPoints(const GfLine2d &line, const GfLineSeg2d &seg, GfVec2d *p1=nullptr, GfVec2d *p2=nullptr, double *t1=nullptr, double *t2=nullptr)
bool operator!=(const GfLineSeg2d &r) const
Definition: lineSeg2d.h:67
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
double GetLength() const
Return the length of the line.
Definition: lineSeg2d.h:50
GfLineSeg2d(const GfVec2d &p0, const GfVec2d &p1)
Construct a line segment that spans two points.
Definition: lineSeg2d.h:38
GLboolean r
Definition: glcorearb.h:1222
GfLineSeg2d()
The default constructor leaves line parameters undefined.
Definition: lineSeg2d.h:34
#define GF_API
Definition: api.h:23
bool operator==(const GfLineSeg2d &l) const
Definition: lineSeg2d.h:60