GteIntrLine2Segment2.h
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.0 (2016/06/19)
7 
8 #pragma once
9 
10 #include <Mathematics/GteSegment.h>
12 
13 namespace gte
14 {
15 
16 template <typename Real>
17 class TIQuery<Real, Line2<Real>, Segment2<Real>>
18 {
19 public:
20  struct Result
21  {
22  bool intersect;
23 
24  // The number is 0 (no intersection), 1 (line and segment intersect in
25  // a single point) or std::numeric_limits<int>::max() (line and
26  // segment are collinear).
28  };
29 
30  Result operator()(Line2<Real> const& line, Segment2<Real> const& segment);
31 };
32 
33 template <typename Real>
34 class FIQuery<Real, Line2<Real>, Segment2<Real>>
35 {
36 public:
37  struct Result
38  {
39  bool intersect;
40 
41  // The number is 0 (no intersection), 1 (line and segment intersect in
42  // a single point) or std::numeric_limits<int>::max() (line and
43  // segment are collinear).
45 
46  // If numIntersections is 1, the intersection is
47  // point = line.origin + lineParameter[0] * line.direction
48  // = segment.origin + segmentParameter[0] * segment.direction
49  // If numIntersections is maxInt, point is not valid but the
50  // intervals are
51  // lineParameter[] = { -maxReal, +maxReal }
52  // segmentParameter[] = { -segmentExtent, segmentExtent }
53  Real lineParameter[2], segmentParameter[2];
55  };
56 
57  Result operator()(Line2<Real> const& line, Segment2<Real> const& segment);
58 };
59 
60 
61 template <typename Real>
64  Line2<Real> const& line, Segment2<Real> const& segment)
65 {
66  Result result;
67  Vector2<Real> segOrigin, segDirection;
68  Real segExtent;
69  segment.GetCenteredForm(segOrigin, segDirection, segExtent);
70 
72  auto llResult = llQuery(line, Line2<Real>(segOrigin, segDirection));
73  if (llResult.numIntersections == 1)
74  {
75  // Test whether the line-line intersection is on the segment.
76  if (std::abs(llResult.line1Parameter[0]) <= segExtent)
77  {
78  result.intersect = true;
79  result.numIntersections = 1;
80  }
81  else
82  {
83  result.intersect = false;
84  result.numIntersections = 0;
85  }
86  }
87  else
88  {
89  result.intersect = llResult.intersect;
90  result.numIntersections = llResult.numIntersections;
91  }
92  return result;
93 }
94 
95 template <typename Real>
98  Line2<Real> const& line, Segment2<Real> const& segment)
99 {
100  Result result;
101  Vector2<Real> segOrigin, segDirection;
102  Real segExtent;
103  segment.GetCenteredForm(segOrigin, segDirection, segExtent);
104 
106  auto llResult = llQuery(line, Line2<Real>(segOrigin, segDirection));
107  if (llResult.numIntersections == 1)
108  {
109  // Test whether the line-line intersection is on the ray.
110  if (std::abs(llResult.line1Parameter[0]) <= segExtent)
111  {
112  result.intersect = true;
113  result.numIntersections = 1;
114  result.lineParameter[0] = llResult.line0Parameter[0];
115  result.segmentParameter[0] = llResult.line1Parameter[0];
116  result.point = llResult.point;
117  }
118  else
119  {
120  result.intersect = false;
121  result.numIntersections = 0;
122  }
123  }
124  else if (llResult.numIntersections == std::numeric_limits<int>::max())
125  {
126  result.intersect = true;
127  result.numIntersections = std::numeric_limits<int>::max();
128  Real maxReal = std::numeric_limits<Real>::max();
129  result.lineParameter[0] = -maxReal;
130  result.lineParameter[1] = +maxReal;
131  result.segmentParameter[0] = -segExtent;
132  result.segmentParameter[1] = +segExtent;
133  }
134  else
135  {
136  result.intersect = false;
137  result.numIntersections = 0;
138  }
139  return result;
140 }
141 
142 
143 }
gte::BSNumber< UIntegerType > abs(gte::BSNumber< UIntegerType > const &number)
Definition: GteBSNumber.h:966
Result operator()(Type0 const &primitive0, Type1 const &primitive1)
GLuint64EXT * result
Definition: glext.h:10003


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 04:00:00