GteIntrSegment2Segment2.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>
13 
14 namespace gte
15 {
16 
17 template <typename Real>
18 class TIQuery<Real, Segment2<Real>, Segment2<Real>>
19 {
20 public:
21  struct Result
22  {
23  bool intersect;
24 
25  // The number is 0 (no intersection), 1 (segments intersect in a
26  // single point), or 2 (segments are collinear and intersect in a
27  // segment).
29  };
30 
31  Result operator()(Segment2<Real> const& segment0,
32  Segment2<Real> const& segment1);
33 };
34 
35 template <typename Real>
36 class FIQuery<Real, Segment2<Real>, Segment2<Real>>
37 {
38 public:
39  struct Result
40  {
41  bool intersect;
42 
43  // The number is 0 (no intersection), 1 (segments intersect in a
44  // a single point), or 2 (segments are collinear and intersect
45  // in a segment).
47 
48  // If numIntersections is 1, the intersection is
49  // point[0]
50  // = segment0.origin + segment0Parameter[0] * segment0.direction
51  // = segment1.origin + segment1Parameter[0] * segment1.direction
52  // If numIntersections is 2, the endpoints of the segment of
53  // intersection are
54  // point[i]
55  // = segment0.origin + segment0Parameter[i] * segment0.direction
56  // = segment1.origin + segment1Parameter[i] * segment1.direction
57  // with segment0Parameter[0] <= segment0Parameter[1] and
58  // segment1Parameter[0] <= segment1Parameter[1].
59  Real segment0Parameter[2], segment1Parameter[2];
60  Vector2<Real> point[2];
61  };
62 
63  Result operator()(Segment2<Real> const& segment0,
64  Segment2<Real> const& segment1);
65 };
66 
67 
68 template <typename Real>
71  Segment2<Real> const& segment0, Segment2<Real> const& segment1)
72 {
73  Result result;
74  Vector2<Real> seg0Origin, seg0Direction, seg1Origin, seg1Direction;
75  Real seg0Extent, seg1Extent;
76  segment0.GetCenteredForm(seg0Origin, seg0Direction, seg0Extent);
77  segment1.GetCenteredForm(seg1Origin, seg1Direction, seg1Extent);
78 
80  Line2<Real> line0(seg0Origin, seg0Direction);
81  Line2<Real> line1(seg1Origin, seg1Direction);
82  auto llResult = llQuery(line0, line1);
83  if (llResult.numIntersections == 1)
84  {
85  // Test whether the line-line intersection is on the segments.
86  if (std::abs(llResult.line0Parameter[0]) <= seg0Extent
87  && std::abs(llResult.line1Parameter[0]) <= seg1Extent)
88  {
89  result.intersect = true;
90  result.numIntersections = 1;
91  }
92  else
93  {
94  result.intersect = false;
95  result.numIntersections = 0;
96  }
97  }
98  else if (llResult.numIntersections == std::numeric_limits<int>::max())
99  {
100  // Compute the location of segment1 endpoints relative to segment0.
101  Vector2<Real> diff = seg1Origin - seg0Origin;
102  Real t = Dot(seg0Direction, diff);
103 
104  // Get the parameter intervals of the segments relative to segment0.
105  std::array<Real, 2> interval0 = { -seg0Extent, seg0Extent };
106  std::array<Real, 2> interval1 = { t - seg1Extent, t + seg1Extent };
107 
108  // Compute the intersection of the intervals.
109  FIQuery<Real, std::array<Real, 2>, std::array<Real, 2>> iiQuery;
110  auto iiResult = iiQuery(interval0, interval1);
111  result.intersect = true;
112  result.numIntersections = iiResult.numIntersections;
113  }
114  else
115  {
116  result.intersect = false;
117  result.numIntersections = 0;
118  }
119 
120  return result;
121 }
122 
123 template <typename Real>
126  Segment2<Real> const& segment0, Segment2<Real> const& segment1)
127 {
128  Result result;
129  Vector2<Real> seg0Origin, seg0Direction, seg1Origin, seg1Direction;
130  Real seg0Extent, seg1Extent;
131  segment0.GetCenteredForm(seg0Origin, seg0Direction, seg0Extent);
132  segment1.GetCenteredForm(seg1Origin, seg1Direction, seg1Extent);
133 
135  Line2<Real> line0(seg0Origin, seg0Direction);
136  Line2<Real> line1(seg1Origin, seg1Direction);
137  auto llResult = llQuery(line0, line1);
138  if (llResult.numIntersections == 1)
139  {
140  // Test whether the line-line intersection is on the segments.
141  if (std::abs(llResult.line0Parameter[0]) <= seg0Extent
142  && std::abs(llResult.line1Parameter[0]) <= seg1Extent)
143  {
144  result.intersect = true;
145  result.numIntersections = 1;
146  result.segment0Parameter[0] = llResult.line0Parameter[0];
147  result.segment1Parameter[0] = llResult.line1Parameter[0];
148  result.point[0] = llResult.point;
149  }
150  else
151  {
152  result.intersect = false;
153  result.numIntersections = 0;
154  }
155  }
156  else if (llResult.numIntersections == std::numeric_limits<int>::max())
157  {
158  // Compute the location of segment1 endpoints relative to segment0.
159  Vector2<Real> diff = seg1Origin - seg0Origin;
160  Real t = Dot(seg0Direction, diff);
161 
162  // Get the parameter intervals of the segments relative to segment0.
163  std::array<Real, 2> interval0 = { -seg0Extent, seg0Extent };
164  std::array<Real, 2> interval1 = { t - seg1Extent, t + seg1Extent };
165 
166  // Compute the intersection of the intervals.
167  FIQuery<Real, std::array<Real, 2>, std::array<Real, 2>> iiQuery;
168  auto iiResult = iiQuery(interval0, interval1);
169  if (iiResult.intersect)
170  {
171  result.intersect = true;
172  result.numIntersections = iiResult.numIntersections;
173  for (int i = 0; i < iiResult.numIntersections; ++i)
174  {
175  result.segment0Parameter[i] = iiResult.overlap[i];
176  result.segment1Parameter[i] = iiResult.overlap[i] - t;
177  result.point[i] = seg0Origin +
178  result.segment0Parameter[i] * seg0Direction;
179  }
180  }
181  else
182  {
183  result.intersect = false;
184  result.numIntersections = 0;
185  }
186  }
187  else
188  {
189  result.intersect = false;
190  result.numIntersections = 0;
191  }
192 
193  return result;
194 }
195 
196 
197 }
gte::BSNumber< UIntegerType > abs(gte::BSNumber< UIntegerType > const &number)
Definition: GteBSNumber.h:966
DualQuaternion< Real > Dot(DualQuaternion< Real > const &d0, DualQuaternion< Real > const &d1)
GLdouble GLdouble t
Definition: glext.h:239
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