GteDistPointAlignedBox.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 
12 
13 namespace gte
14 {
15 
16 template <int N, typename Real>
17 class DCPQuery<Real, Vector<N, Real>, AlignedBox<N, Real>>
18 {
19 public:
20  struct Result
21  {
24  };
25 
26  Result operator()(Vector<N, Real> const& point,
27  AlignedBox<N, Real> const& box);
28 
29 protected:
30  // On input, 'point' is the difference of the query point and the box
31  // center. On output, 'point' is the point on the box closest to the
32  // query point.
33  void DoQuery(Vector<N, Real>& point, Vector<N, Real> const& boxExtent,
34  Result& result);
35 };
36 
37 // Template aliases for convenience.
38 template <int N, typename Real>
39 using DCPPointAlignedBox =
41 
42 template <typename Real>
44 
45 template <typename Real>
47 
48 
49 template <int N, typename Real>
50 typename DCPQuery<Real, Vector<N, Real>, AlignedBox<N, Real>>::Result
51 DCPQuery<Real, Vector<N, Real>, AlignedBox<N, Real>>::operator()(
52  Vector<N, Real> const& point, AlignedBox<N, Real> const& box)
53 {
54  // Translate the point and box so that the box has center at the origin.
55  Vector<N, Real> boxCenter, boxExtent;
56  box.GetCenteredForm(boxCenter, boxExtent);
57  Vector<N, Real> closest = point - boxCenter;
58 
59  Result result;
60  DoQuery(closest, boxExtent, result);
61 
62  // Compute the closest point on the box.
63  result.boxClosest = boxCenter + closest;
64  return result;
65 }
66 
67 template <int N, typename Real>
68 void DCPQuery<Real, Vector<N, Real>, AlignedBox<N, Real>>::DoQuery(
69  Vector<N, Real>& point, Vector<N, Real> const& boxExtent, Result& result)
70 {
71  result.sqrDistance = (Real)0;
72  for (int i = 0; i < N; ++i)
73  {
74  if (point[i] < -boxExtent[i])
75  {
76  Real delta = point[i] + boxExtent[i];
77  result.sqrDistance += delta * delta;
78  point[i] = -boxExtent[i];
79  }
80  else if (point[i] > boxExtent[i])
81  {
82  Real delta = point[i] - boxExtent[i];
83  result.sqrDistance += delta * delta;
84  point[i] = boxExtent[i];
85  }
86  }
87  result.distance = sqrt(result.sqrDistance);
88 }
89 
90 
91 }
GLsizei GLsizei GLfloat distance
Definition: glext.h:9704
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 03:59:59