GteIntrOrientedBox2Cone2.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/GteCone.h>
12 
13 // The queries consider the box and cone to be solids.
14 //
15 // Define V = cone.ray.origin, D = cone.ray.direction, and cs = cone.cosAngle.
16 // Define C = box.center, U0 = box.axis[0], U1 = box.axis[1],
17 // e0 = box.extent[0], and e1 = box.extent[1]. A box point is
18 // P = C + x*U0 + y*U1 where |x| <= e0 and |y| <= e1. Define the function
19 // F(P) = Dot(D, (P-V)/Length(P-V)) = F(x,y)
20 // = Dot(D, (x*U0 + y*U1 + (C-V))/|x*U0 + y*U1 + (C-V)|
21 // = (a0*x + a1*y + a2)/(x^2 + y^2 + 2*b0*x + 2*b1*y + b2)^{1/2}
22 // The function has an essential singularity when P = V. The box intersects
23 // the cone (with positive-area overlap) when at least one of the four box
24 // corners is strictly inside the cone. It is necessary that the numerator
25 // of F(P) be positive at such a corner. The (interior of the) solid cone
26 // is defined by the quadratic inequality
27 // (Dot(D,P-V))^2 > |P-V|^2*(cone.cosAngle)^2
28 // This inequality is inexpensive to compute. In summary, overlap occurs
29 // when there is a box corner P for which
30 // F(P) > 0 and (Dot(D,P-V))^2 > |P-V|^2*(cone.cosAngle)^2
31 
32 namespace gte
33 {
34 
35 template <typename Real>
36 class TIQuery<Real, OrientedBox<2, Real>, Cone<2, Real>>
37 {
38 public:
39  struct Result
40  {
41  // The value of 'intersect' is true when there is a box point that
42  // is strictly inside the cone. If the box just touches the cone
43  // from the outside, an intersection is not reported, which supports
44  // the common operation of culling objects outside a cone.
45  bool intersect;
46  };
47 
48  Result operator()(OrientedBox<2, Real> const& box, Cone<2, Real>& cone);
49 };
50 
51 
52 template <typename Real>
55  OrientedBox<2, Real> const& box, Cone<2, Real>& cone)
56 {
57  Result result;
58 
60  auto rbResult = rbQuery(cone.ray, box);
61  if (rbResult.intersect)
62  {
63  // The cone intersects the box.
64  result.intersect = true;
65  return result;
66  }
67 
68  // Define V = cone.ray.origin, D = cone.ray.direction, and
69  // cs = cone.cosAngle. Define C = box.center, U0 = box.axis[0],
70  // U1 = box.axis[1], e0 = box.extent[0], and e1 = box.extent[1].
71  // A box point is P = C + x*U0 + y*U1 where |x| <= e0 and |y| <= e1.
72  // Define the function
73  // F(x,y) = Dot(D, (P-V)/Length(P-V))
74  // = Dot(D, (x*U0 + y*U1 + (C-V))/|x*U0 + y*U1 + (C-V)|
75  // = (a0*x + a1*y + a2)/(x^2 + y^2 + 2*b0*x + 2*b1*y + b2)^{1/2}
76  // The function has an essential singularity when P = V.
77  Vector<2, Real> diff = box.center - cone.ray.origin;
78  Real a0 = Dot(cone.ray.direction, box.axis[0]);
79  Real a1 = Dot(cone.ray.direction, box.axis[1]);
80  Real a2 = Dot(cone.ray.direction, diff);
81  Real b0 = Dot(box.axis[0], diff);
82  Real b1 = Dot(box.axis[1], diff);
83  Real b2 = Dot(diff, diff);
84  Real csSqr = cone.cosAngle * cone.cosAngle;
85 
86  for (int i1 = 0; i1 < 2; ++i1)
87  {
88  Real sign1 = i1 * (Real)2 - (Real)1;
89  Real y = sign1 * box.extent[1];
90  for (int i0 = 0; i0 < 2; ++i0)
91  {
92  Real sign0 = i0 * (Real)2 - (Real)1;
93  Real x = sign0 * box.extent[0];
94  Real fNumerator = a0 * x + a1 * y + a2;
95  if (fNumerator >(Real)0)
96  {
97  Real dSqr = x*x + y*y + (b0*x + b1*y)*(Real)2 + b2;
98  Real nSqr = fNumerator*fNumerator;
99  if (nSqr > dSqr * csSqr)
100  {
101  result.intersect = true;
102  return result;
103  }
104  }
105  }
106  }
107 
108  result.intersect = false;
109  return result;
110 }
111 
112 
113 }
GLint GLenum GLint x
Definition: glcorearb.h:404
DualQuaternion< Real > Dot(DualQuaternion< Real > const &d0, DualQuaternion< Real > const &d1)
Result operator()(Type0 const &primitive0, Type1 const &primitive1)
GLuint64EXT * result
Definition: glext.h:10003
GLint y
Definition: glcorearb.h:98


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