GteIntrLine2Circle2.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/GteVector2.h>
13 #include <Mathematics/GteFIQuery.h>
14 #include <Mathematics/GteTIQuery.h>
15 
16 // The queries consider the circle to be a solid (disk).
17 
18 namespace gte
19 {
20 
21 template <typename Real>
22 class TIQuery<Real, Line2<Real>, Circle2<Real>>
23 {
24 public:
25  struct Result
26  {
27  bool intersect;
28  };
29 
30  Result operator()(Line2<Real> const& line, Circle2<Real> const& circle);
31 };
32 
33 template <typename Real>
34 class FIQuery<Real, Line2<Real>, Circle2<Real>>
35 {
36 public:
37  struct Result
38  {
39  bool intersect;
41  std::array<Real,2> parameter;
42  std::array<Vector2<Real>, 2> point;
43  };
44 
45  Result operator()(Line2<Real> const& line, Circle2<Real> const& circle);
46 
47 protected:
48  void DoQuery(Vector2<Real> const& lineOrigin,
49  Vector2<Real> const& lineDirection, Circle2<Real> const& circle,
50  Result& result);
51 };
52 
53 
54 template <typename Real>
57  Line2<Real> const& line, Circle2<Real> const& circle)
58 {
59  Result result;
61  auto plResult = plQuery(circle.center, line);
62  result.intersect = (plResult.distance <= circle.radius);
63  return result;
64 }
65 
66 template <typename Real>
69  Line2<Real> const& line, Circle2<Real> const& circle)
70 {
71  Result result;
72  DoQuery(line.origin, line.direction, circle, result);
73  for (int i = 0; i < result.numIntersections; ++i)
74  {
75  result.point[i] = line.origin + result.parameter[i] * line.direction;
76  }
77  return result;
78 }
79 
80 template <typename Real>
82  Vector2<Real> const& lineOrigin, Vector2<Real> const& lineDirection,
83  Circle2<Real> const& circle, Result& result)
84 {
85  // Intersection of a the line P+t*D and the circle |X-C| = R. The line
86  // direction is unit length. The t-value is a real-valued root to the
87  // quadratic equation
88  // 0 = |t*D+P-C|^2 - R^2
89  // = t^2 + 2*Dot(D,P-C)*t + |P-C|^2-R^2
90  // = t^2 + 2*a1*t + a0
91  // If there are two distinct roots, the order is t0 < t1.
92  Vector2<Real> diff = lineOrigin - circle.center;
93  Real a0 = Dot(diff, diff) - circle.radius * circle.radius;
94  Real a1 = Dot(lineDirection, diff);
95  Real discr = a1 * a1 - a0;
96  if (discr > (Real)0)
97  {
98  Real root = sqrt(discr);
99  result.intersect = true;
100  result.numIntersections = 2;
101  result.parameter[0] = -a1 - root;
102  result.parameter[1] = -a1 + root;
103  }
104  else if (discr < (Real)0)
105  {
106  result.intersect = false;
107  result.numIntersections = 0;
108  }
109  else // discr == 0
110  {
111  result.intersect = true;
112  result.numIntersections = 1;
113  result.parameter[0] = -a1;
114  }
115 }
116 
117 
118 }
DualQuaternion< Real > Dot(DualQuaternion< Real > const &d0, DualQuaternion< Real > const &d1)
Result operator()(Type0 const &primitive0, Type1 const &primitive1)
Vector< N, Real > center
GLuint64EXT * result
Definition: glext.h:10003


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