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