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 PXR_BASE_GF_LINE_SEG_H
25 #define PXR_BASE_GF_LINE_SEG_H
26 
27 /// \file gf/lineSeg.h
28 /// \ingroup group_gf_BasicGeometry
29 
30 #include "pxr/pxr.h"
31 #include "pxr/base/gf/line.h"
32 #include "pxr/base/gf/vec3d.h"
33 #include "pxr/base/gf/api.h"
34 
35 #include <float.h>
36 #include <iosfwd>
37 
38 PXR_NAMESPACE_OPEN_SCOPE
39 
40 /// \class GfLineSeg
41 /// \ingroup group_gf_BasicGeometry
42 ///
43 /// Basic type: 3D line segment
44 ///
45 /// This class represents a three-dimensional line segment in space.
46 ///
47 class GfLineSeg {
48 
49   public:
50 
51     /// The default constructor leaves line parameters undefined.
GfLineSeg()52     GfLineSeg() {
53     }
54 
55     /// Construct a line segment that spans two points.
GfLineSeg(const GfVec3d & p0,const GfVec3d & p1)56     GfLineSeg(const GfVec3d &p0, const GfVec3d &p1 ) {
57         _length = _line.Set( p0, p1 - p0 );
58     }
59 
60     /// Return the point on the segment specified by the parameter t.
61     /// p = p0 + t * (p1 - p0)
GetPoint(double t)62     GfVec3d GetPoint( double t ) const {return _line.GetPoint( t * _length );}
63 
64     /// Return the normalized direction of the line.
GetDirection()65     const GfVec3d &GetDirection() const { return _line.GetDirection(); }
66 
67     /// Return the length of the line
GetLength()68     double GetLength() const { return _length; }
69 
70     /// Returns the point on the line that is closest to \p point. If
71     /// \p t is not \c NULL, it will be set to the parametric
72     /// distance along the line of the closest point.
73     GF_API
74     GfVec3d FindClosestPoint(const GfVec3d &point, double *t = NULL) const;
75 
76     /// Component-wise equality test. The starting points and directions,
77     /// must match exactly for lines to be considered equal.
78     bool		operator ==(const GfLineSeg &l) const {
79 	return (_line == l._line && _length  == l._length);
80     }
81 
82     /// Component-wise inequality test. The starting points,
83     /// and directions must match exactly for lines to be
84     /// considered equal.
85     bool		operator !=(const GfLineSeg &r) const {
86 	return ! (*this == r);
87     }
88 
89   private:
90     GF_API
91     friend bool GfFindClosestPoints( const GfLine &, const GfLineSeg &,
92                                      GfVec3d *, GfVec3d *,
93                                      double *, double * );
94     GF_API
95     friend bool GfFindClosestPoints( const GfLineSeg &, const GfLineSeg &,
96                                      GfVec3d *, GfVec3d *,
97                                      double *, double * );
98 
99     GfLine              _line;
100     double              _length;   // distance from p0 to p1
101 };
102 
103 /// Computes the closets points on \p line and \p seg.
104 ///
105 /// The two points are returned in \p p1 and \p p2. The parametric distances
106 /// of \p p1 and \p p2 along the line and segment are returned in \p t1 and \p
107 /// t2.
108 ///
109 /// This returns \c false if the lines were close enough to parallel that no
110 /// points could be computed; in this case, the other return values are
111 /// undefined.
112 GF_API
113 bool GfFindClosestPoints( const GfLine &line, const GfLineSeg &seg,
114                           GfVec3d *p1 = nullptr, GfVec3d *p2 = nullptr,
115                           double *t1 = nullptr, double *t2 = nullptr );
116 
117 /// Computes the closets points on two line segments, \p seg1 and \p seg2. The
118 /// two points are returned in \p p1 and \p p2. The parametric distances of \p
119 /// p1 and \p p2 along the segments are returned in \p t1 and \p t2.
120 ///
121 /// This returns \c false if the lines were close enough to parallel that no
122 /// points could be computed; in this case, the other return values are
123 /// undefined.
124 GF_API
125 bool GfFindClosestPoints( const GfLineSeg &seg1, const GfLineSeg &seg2,
126                           GfVec3d *p1 = nullptr, GfVec3d *p2 = nullptr,
127                           double *t1 = nullptr, double *t2 = nullptr );
128 
129 /// Output a GfLineSeg.
130 /// \ingroup group_gf_DebuggingOutput
131 GF_API std::ostream &operator<<(std::ostream&, const GfLineSeg&);
132 
133 PXR_NAMESPACE_CLOSE_SCOPE
134 
135 #endif // PXR_BASE_GF_LINE_SEG_H
136