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