GteDistAlignedBox3OrientedBox3.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.5.0 (2016/11/26)
7 
8 #pragma once
9 
14 #include <Mathematics/GteVector3.h>
16 
17 namespace gte
18 {
19 
20 template <typename Real>
21 class DCPQuery<Real, AlignedBox3<Real>, OrientedBox3<Real>>
22 {
23 public:
24  struct Result
25  {
27 
28  // These members are valid only when queryIsSuccessful is true;
29  // otherwise, they are all set to zero.
31  std::array<Real, 3> box0Parameter, box1Parameter;
32  Vector3<Real> closestPoint[2];
33 
34  // The number of iterations used by LCPSolver regardless of whether
35  // the query is successful.
37  };
38 
39  // The default maximum iterations is 144 (n = 12, maxIterations = n*n). If
40  // the solver fails to converge, try increasing the maximum number of
41  // iterations.
42  void SetMaxLCPIterations(int maxLCPIterations);
43 
44  Result operator()(AlignedBox3<Real> const& box0, OrientedBox3<Real> const& box1);
45 
46 private:
48 };
49 
50 
51 template <typename Real>
52 void DCPQuery<Real, AlignedBox3<Real>, OrientedBox3<Real>>::SetMaxLCPIterations(int maxLCPIterations)
53 {
54  mLCP.SetMaxIterations(maxLCPIterations);
55 }
56 
57 template <typename Real>
58 typename DCPQuery<Real, AlignedBox3<Real>, OrientedBox3<Real>>::Result
59  DCPQuery<Real, AlignedBox3<Real>, OrientedBox3<Real>>::operator()(
60  AlignedBox3<Real> const& box0, OrientedBox3<Real> const& box1)
61 {
62  Result result;
63 
64  // Translate the boxes so that the aligned box becomes a canonical box.
65  // Modify the oriented box coefficients to be nonnegative.
66  Vector3<Real> K = box0.max - box0.min;
67  Vector3<Real> delta = box1.center - box0.min;
68  for (int i = 0; i < 3; ++i)
69  {
70  delta -= box1.extent[i] * box1.axis[i];
71  }
72 
73  Vector3<Real> rotDelta;
74  for (int i = 0; i < 3; ++i)
75  {
76  rotDelta[i] = Dot(box1.axis[i], delta);
77  }
78 
79  Vector3<Real> twoExtent = box1.extent * (Real)2;
80 
81  // The LCP has 6 variables and 6 (nontrivial) inequality constraints.
82  std::array<Real, 12> q =
83  {
84  -delta[0], -delta[1], -delta[2], rotDelta[0], rotDelta[1], rotDelta[2],
85  K[0], K[1], K[2], twoExtent[0], twoExtent[1], twoExtent[2]
86  };
87 
88  std::array<std::array<Real, 12>, 12> M;
89  {
90  Real const z = (Real)0;
91  Real const p = (Real)1;
92  Real const m = (Real)-1;
93  Vector3<Real> const& U0 = box1.axis[0];
94  Vector3<Real> const& U1 = box1.axis[1];
95  Vector3<Real> const& U2 = box1.axis[2];
96  M[ 0] = { p, z, z, -U0[0], -U1[0], -U1[2], p, z, z, z, z, z };
97  M[ 1] = { z, p, z, -U0[1], -U1[1], -U1[1], z, p, z, z, z, z };
98  M[ 2] = { z, z, p, -U0[2], -U1[2], -U1[2], z, z, p, z, z, z };
99  M[ 3] = { -U0[0], -U0[1], -U0[2], p, z, z, z, z, z, p, z, z };
100  M[ 4] = { -U1[0], -U1[1], -U1[2], z, p, z, z, z, z, z, p, z };
101  M[ 5] = { -U2[0], -U2[1], -U2[2], z, z, p, z, z, z, z, z, p };
102  M[ 6] = { m, z, z, z, z, z, z, z, z, z, z, z };
103  M[ 7] = { z, m, z, z, z, z, z, z, z, z, z, z };
104  M[ 8] = { z, z, m, z, z, z, z, z, z, z, z, z };
105  M[ 9] = { z, z, z, m, z, z, z, z, z, z, z, z };
106  M[10] = { z, z, z, z, m, z, z, z, z, z, z, z };
107  M[11] = { z, z, z, z, z, m, z, z, z, z, z, z };
108  }
109 
110  std::array<Real, 12> w, z;
111  if (mLCP.Solve(q, M, w, z))
112  {
113  result.queryIsSuccessful = true;
114 
115  for (int i = 0; i < 3; ++i)
116  {
117  result.box0Parameter[i] = z[i] + box0.min[i];
118  result.closestPoint[0][i] = result.box0Parameter[i];
119  }
120 
121  result.closestPoint[1] = box1.center;
122  for (int i = 0, j = 3; i < 3; ++i, ++j)
123  {
124  result.box1Parameter[i] = z[j] - box1.extent[i];
125  result.closestPoint[1] += result.box1Parameter[i] * box1.axis[i];
126  }
127 
128  Vector3<Real> diff = result.closestPoint[1] - result.closestPoint[0];
129  result.sqrDistance = Dot(diff, diff);
130  result.distance = Function<Real>::Sqrt(result.sqrDistance);
131  }
132  else
133  {
134  // If you reach this case, the maximum number of iterations was not
135  // specified to be large enough or there is a problem due to
136  // floating-point rounding errors. If you believe the latter is
137  // true, file a bug report.
138  result.queryIsSuccessful = false;
139 
140  for (int i = 0; i < 3; ++i)
141  {
142  result.box0Parameter[i] = (Real)0;
143  result.box1Parameter[i] = (Real)0;
144  result.closestPoint[0][i] = (Real)0;
145  result.closestPoint[1][i] = (Real)0;
146  }
147  result.distance = (Real)0;
148  result.sqrDistance = (Real)0;
149  }
150 
151  result.numLCPIterations = mLCP.GetNumIterations();
152  return result;
153 }
154 
155 }
const GLfloat * m
Definition: glext.h:6461
GLsizei GLsizei GLfloat distance
Definition: glext.h:9704
static Real Sqrt(Real const &x)
Definition: GteFunctions.h:937
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:852
Result operator()(Type0 const &primitive0, Type1 const &primitive1)
DualQuaternion< Real > Dot(DualQuaternion< Real > const &d0, DualQuaternion< Real > const &d1)
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:843
GLdouble GLdouble GLdouble GLdouble q
Definition: glext.h:255
GLfloat GLfloat p
Definition: glext.h:11668
GLuint64EXT * result
Definition: glext.h:10003


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 03:59:59