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