GteIntrRay2AlignedBox2.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>
11 #include <Mathematics/GteRay.h>
14 
15 // The queries consider the box to be a solid.
16 //
17 // The test-intersection queries use the method of separating axes. The
18 // find-intersection queries use parametric clipping against the four edges of
19 // the box.
20 
21 namespace gte
22 {
23 
24 template <typename Real>
25 class TIQuery<Real, Ray2<Real>, AlignedBox2<Real>>
26  :
27  public TIQuery<Real, Line2<Real>, AlignedBox2<Real>>
28 {
29 public:
30  struct Result
31  :
32  public TIQuery<Real, Line2<Real>, AlignedBox2<Real>>::Result
33  {
34  // No additional information to compute.
35  };
36 
37  Result operator()(Ray2<Real> const& ray, AlignedBox2<Real> const& box);
38 
39 protected:
40  void DoQuery(Vector2<Real> const& rayOrigin,
41  Vector2<Real> const& rayDirection, Vector2<Real> const& boxExtent,
42  Result& result);
43 };
44 
45 template <typename Real>
46 class FIQuery<Real, Ray2<Real>, AlignedBox2<Real>>
47  :
48  public FIQuery<Real, Line2<Real>, AlignedBox2<Real>>
49 {
50 public:
51  struct Result
52  :
53  public FIQuery<Real, Line2<Real>, AlignedBox2<Real>>::Result
54  {
55  // No additional information to compute.
56  };
57 
58  Result operator()(Ray2<Real> const& ray, AlignedBox2<Real> const& box);
59 
60 protected:
61  void DoQuery(Vector2<Real> const& rayOrigin,
62  Vector2<Real> const& rayDirection, Vector2<Real> const& boxExtent,
63  Result& result);
64 };
65 
66 
67 template <typename Real>
70  Ray2<Real> const& ray, AlignedBox2<Real> const& box)
71 {
72  // Get the centered form of the aligned box. The axes are implicitly
73  // Axis[d] = Vector2<Real>::Unit(d).
74  Vector2<Real> boxCenter, boxExtent;
75  box.GetCenteredForm(boxCenter, boxExtent);
76 
77  // Transform the ray to the aligned-box coordinate system.
78  Vector2<Real> rayOrigin = ray.origin - boxCenter;
79 
80  Result result;
81  DoQuery(rayOrigin, ray.direction, boxExtent, result);
82  return result;
83 }
84 
85 template <typename Real>
87  Vector2<Real> const& rayOrigin, Vector2<Real> const& rayDirection,
88  Vector2<Real> const& boxExtent, Result& result)
89 {
90  for (int i = 0; i < 2; ++i)
91  {
92  if (std::abs(rayOrigin[i]) > boxExtent[i]
93  && rayOrigin[i] * rayDirection[i] >= (Real)0)
94  {
95  result.intersect = false;
96  return;
97  }
98  }
99 
100  TIQuery<Real, Line2<Real>, AlignedBox2<Real>>::DoQuery(rayOrigin,
101  rayDirection, boxExtent, result);
102 }
103 
104 template <typename Real>
107  Ray2<Real> const& ray, AlignedBox2<Real> const& box)
108 {
109  // Get the centered form of the aligned box. The axes are implicitly
110  // Axis[d] = Vector2<Real>::Unit(d).
111  Vector2<Real> boxCenter, boxExtent;
112  box.GetCenteredForm(boxCenter, boxExtent);
113 
114  // Transform the ray to the aligned-box coordinate system.
115  Vector2<Real> rayOrigin = ray.origin - boxCenter;
116 
117  Result result;
118  DoQuery(rayOrigin, ray.direction, boxExtent, result);
119  for (int i = 0; i < result.numIntersections; ++i)
120  {
121  result.point[i] = ray.origin + result.parameter[i] * ray.direction;
122  }
123  return result;
124 }
125 
126 template <typename Real>
128  Vector2<Real> const& rayOrigin, Vector2<Real> const& rayDirection,
129  Vector2<Real> const& boxExtent, Result& result)
130 {
131  FIQuery<Real, Line2<Real>, AlignedBox2<Real>>::DoQuery(rayOrigin,
132  rayDirection, boxExtent, result);
133 
134  if (result.intersect)
135  {
136  // The line containing the ray intersects the box; the t-interval is
137  // [t0,t1]. The ray intersects the box as long as [t0,t1] overlaps
138  // the ray t-interval [0,+infinity).
139  std::array<Real, 2> rayInterval =
140  { (Real)0, std::numeric_limits<Real>::max() };
141  FIQuery<Real, std::array<Real, 2>, std::array<Real, 2>> iiQuery;
142  auto iiResult = iiQuery(result.parameter, rayInterval);
143  result.intersect = iiResult.intersect;
144  result.numIntersections = iiResult.numIntersections;
145  result.parameter = iiResult.overlap;
146  }
147 }
148 
149 
150 }
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