GteIntrOrientedBox3Cone3.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>
13 
14 // Test for intersection of a box and a cone. The cone can be finite or
15 // infinite. The algorithm is described in
16 // http://www.geometrictools.com/Documentation/IntersectionBoxCone.pdf
17 // and assumes that the intersection set must have positive volume. For
18 // example, let the box be outside the cone. If the box is below the support
19 // plane at the cone vertex and just touches the cone vertex, nointersection
20 // is reported. If the box is above the plane of the disk capping a finite
21 // cone, no intersection is reported. However, if the box straddles the
22 // support plane and just touches the cone vertex, an intersection is
23 // reported. This is a consequence of wanting a fast test for culling boxes
24 // against a cone. It is possible to add more logic to change the behavior.
25 
26 namespace gte
27 {
28 
29 template <typename Real>
30 class TIQuery<Real, OrientedBox<3, Real>, Cone<3, Real>>
31  :
32  public TIQuery<Real, AlignedBox<3, Real>, Cone<3, Real>>
33 {
34 public:
35  struct Result
36  :
37  public TIQuery<Real, AlignedBox<3, Real>, Cone<3, Real>>::Result
38  {
39  // No additional information to compute.
40  };
41 
42  Result operator()(OrientedBox<3, Real> const& box, Cone<3, Real>& cone);
43 };
44 
45 // Template alias for convenience.
46 template <typename Real>
47 using TIOrientedBox3Cone3 =
49 
50 
51 template <typename Real>
52 typename TIQuery<Real, OrientedBox<3, Real>, Cone<3, Real>>::Result
53  TIQuery<Real, OrientedBox<3, Real>, Cone<3, Real>>::operator()(
54  OrientedBox<3, Real> const& box, Cone<3, Real>& cone)
55 {
56  Result result;
57 
58  // Quick-rejection test for boxes below the supporting plane of the cone.
59  Vector<3, Real> CmV = box.center - cone.ray.origin;
60  Vector<3, Real> DdU{
61  Dot(cone.ray.direction, box.axis[0]),
62  Dot(cone.ray.direction, box.axis[1]),
63  Dot(cone.ray.direction, box.axis[2]) };
64  Real DdCmV = Dot(cone.ray.direction, CmV); // interval center
65  Real radius = // interval half-length
66  box.extent[0] * std::abs(DdU[0]) +
67  box.extent[1] * std::abs(DdU[1]) +
68  box.extent[2] * std::abs(DdU[2]);
69  if (DdCmV + radius <= (Real)0)
70  {
71  // The box is in the halfspace below the supporting plane of the cone.
72  result.intersect = false;
73  return result;
74  }
75 
76  // Quick-rejection test for boxes outside the plane determined by the
77  // height of the cone.
78  if (cone.height < std::numeric_limits<Real>::max())
79  {
80  if (DdCmV - radius >= cone.height)
81  {
82  // The box is outside the plane determined by the height of the
83  // cone.
84  result.intersect = false;
85  return result;
86  }
87  }
88 
89  // Determine the box faces that are visible to the cone vertex. The
90  // box center has been translated (C-V) so that the cone vertex is at
91  // the origin. Compute the coordinates of the origin relative to the
92  // translated box.
93  Vector<3, Real> UdCmV{
94  Dot(box.axis[0], CmV),
95  Dot(box.axis[1], CmV),
96  Dot(box.axis[2], CmV) };
97  int index[3] = {
98  (UdCmV[0] < -box.extent[0] ? 2 : (UdCmV[0] > box.extent[0] ? 0 : 1)),
99  (UdCmV[1] < -box.extent[1] ? 2 : (UdCmV[1] > box.extent[1] ? 0 : 1)),
100  (UdCmV[2] < -box.extent[2] ? 2 : (UdCmV[2] > box.extent[2] ? 0 : 1))
101  };
102  int lookup = index[0] + 3 * index[1] + 9 * index[2];
103  if (lookup == 13)
104  {
105  // The cone vertex is in the box.
106  result.intersect = true;
107  return result;
108  }
109 
110  auto const& polygon = this->mPolygon[lookup];
111 
112  this->DoQuery(box.extent, cone.cosAngleSqr, DdU, UdCmV, DdCmV, polygon,
113  result);
114  return result;
115 }
116 
117 
118 }
gte::BSNumber< UIntegerType > abs(gte::BSNumber< UIntegerType > const &number)
Definition: GteBSNumber.h:966
DualQuaternion< Real > Dot(DualQuaternion< Real > const &d0, DualQuaternion< Real > const &d1)
GLuint index
Definition: glcorearb.h:781
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