GteIntrSegment3AlignedBox3.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 // The queries consider the box to be a solid.
15 //
16 // The test-intersection queries use the method of separating axes. The
17 // find-intersection queries use parametric clipping against the six faces of
18 // the box.
19 
20 namespace gte
21 {
22 
23 template <typename Real>
24 class TIQuery<Real, Segment3<Real>, AlignedBox3<Real>>
25  :
26  public TIQuery<Real, Line3<Real>, AlignedBox3<Real>>
27 {
28 public:
29  struct Result
30  :
31  public TIQuery<Real, Line3<Real>, AlignedBox3<Real>>::Result
32  {
33  // No additional information to compute.
34  };
35 
36  Result operator()(Segment3<Real> const& segment,
37  AlignedBox3<Real> const& box);
38 
39 protected:
40  void DoQuery(Vector3<Real> const& segOrigin,
41  Vector3<Real> const& segDirection, Real segExtent,
42  Vector3<Real> const& boxExtent, Result& result);
43 };
44 
45 template <typename Real>
46 class FIQuery<Real, Segment3<Real>, AlignedBox3<Real>>
47  :
48  public FIQuery<Real, Line3<Real>, AlignedBox3<Real>>
49 {
50 public:
51  struct Result
52  :
53  public FIQuery<Real, Line3<Real>, AlignedBox3<Real>>::Result
54  {
55  // No additional information to compute.
56  };
57 
58  Result operator()(Segment3<Real> const& segment,
59  AlignedBox3<Real> const& box);
60 
61 protected:
62  void DoQuery(Vector3<Real> const& segOrigin,
63  Vector3<Real> const& segDirection, Real segExtent,
64  Vector3<Real> const& boxExtent, Result& result);
65 };
66 
67 
68 template <typename Real>
71  Segment3<Real> const& segment, AlignedBox3<Real> const& box)
72 {
73  // Get the centered form of the aligned box. The axes are implicitly
74  // Axis[d] = Vector3<Real>::Unit(d).
75  Vector3<Real> boxCenter, boxExtent;
76  box.GetCenteredForm(boxCenter, boxExtent);
77 
78  // Transform the segment to a centered form in the aligned-box coordinate
79  // system.
80  Vector3<Real> transformedP0 = segment.p[0] - boxCenter;
81  Vector3<Real> transformedP1 = segment.p[1] - boxCenter;
82  Segment3<Real> transformedSegment(transformedP0, transformedP1);
83  Vector3<Real> segOrigin, segDirection;
84  Real segExtent;
85  transformedSegment.GetCenteredForm(segOrigin, segDirection, segExtent);
86 
87  Result result;
88  DoQuery(segOrigin, segDirection, segExtent, boxExtent, result);
89  return result;
90 }
91 
92 template <typename Real>
94  Vector3<Real> const& segOrigin, Vector3<Real> const& segDirection,
95  Real segExtent, Vector3<Real> const& boxExtent, Result& result)
96 {
97  for (int i = 0; i < 3; ++i)
98  {
99  if (std::abs(segOrigin[i]) > boxExtent[i] +
100  segExtent*std::abs(segDirection[i]))
101  {
102  result.intersect = false;
103  return;
104  }
105  }
106 
107  TIQuery<Real, Line3<Real>, AlignedBox3<Real>>::DoQuery(segOrigin,
108  segDirection, boxExtent, result);
109 }
110 
111 template <typename Real>
114  Segment3<Real> const& segment, AlignedBox3<Real> const& box)
115 {
116  // Get the centered form of the aligned box. The axes are implicitly
117  // Axis[d] = Vector3<Real>::Unit(d).
118  Vector3<Real> boxCenter, boxExtent;
119  box.GetCenteredForm(boxCenter, boxExtent);
120 
121  // Transform the segment to a centered form in the aligned-box coordinate
122  // system.
123  Vector3<Real> transformedP0 = segment.p[0] - boxCenter;
124  Vector3<Real> transformedP1 = segment.p[1] - boxCenter;
125  Segment3<Real> transformedSegment(transformedP0, transformedP1);
126  Vector3<Real> segOrigin, segDirection;
127  Real segExtent;
128  transformedSegment.GetCenteredForm(segOrigin, segDirection, segExtent);
129 
130  Result result;
131  DoQuery(segOrigin, segDirection, segExtent, boxExtent, result);
132  for (int i = 0; i < result.numPoints; ++i)
133  {
134  result.point[i] = segOrigin + result.lineParameter[i] * segDirection;
135  }
136  return result;
137 }
138 
139 template <typename Real>
141  Vector3<Real> const& segOrigin, Vector3<Real> const& segDirection,
142  Real segExtent, Vector3<Real> const& boxExtent, Result& result)
143 {
144  FIQuery<Real, Line3<Real>, AlignedBox3<Real>>::DoQuery(segOrigin,
145  segDirection, boxExtent, result);
146 
147  if (result.intersect)
148  {
149  // The line containing the segment intersects the box; the t-interval
150  // is [t0,t1]. The segment intersects the box as long as [t0,t1]
151  // loverlaps the segment t-interval [-segExtent,+segExtent].
152  FIQuery<Real, std::array<Real, 2>, std::array<Real, 2>> iiQuery;
153 
154  std::array<Real, 2> interval0 =
155  {
156  result.lineParameter[0], result.lineParameter[1]
157  };
158 
159  std::array<Real, 2> interval1 =
160  {
161  -segExtent, segExtent
162  };
163 
164  auto iiResult = iiQuery(interval0, interval1);
165  if (iiResult.numIntersections > 0)
166  {
167  result.numPoints = iiResult.numIntersections;
168  for (int i = 0; i < result.numPoints; ++i)
169  {
170  result.lineParameter[i] = iiResult.overlap[i];
171  }
172  }
173  else
174  {
175  result.intersect = false;
176  result.numPoints = 0;
177  }
178  }
179 }
180 
181 
182 }
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
void GetCenteredForm(Vector< N, Real > &center, Vector< N, Real > &direction, Real &extent) const
Definition: GteSegment.h:103


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