GteDistRectangle3OrientedBox3.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/23)
7 
8 #pragma once
9 
14 #include <Mathematics/GteVector3.h>
16 
17 namespace gte
18 {
19 
20 template <typename Real>
21 class DCPQuery<Real, Rectangle3<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, 2> rectangleParameter;
32  std::array<Real, 3> boxParameter;
33  Vector3<Real> closestPoint[2];
34 
35  // The number of iterations used by LCPSolver regardless of whether
36  // the query is successful.
38  };
39 
40  // The default maximum iterations is 81 (n = 9, maxIterations = n*n). If
41  // the solver fails to converge, try increasing the maximum number of
42  // iterations.
43  void SetMaxLCPIterations(int maxLCPIterations);
44 
45  Result operator()(Rectangle3<Real> const& rectangle, OrientedBox3<Real> const& box);
46 
47 private:
49 };
50 
51 
52 template <typename Real>
53 void DCPQuery<Real, Rectangle3<Real>, OrientedBox3<Real>>::SetMaxLCPIterations(int maxLCPIterations)
54 {
55  mLCP.SetMaxIterations(maxLCPIterations);
56 }
57 
58 template <typename Real>
59 typename DCPQuery<Real, Rectangle3<Real>, OrientedBox3<Real>>::Result
60 DCPQuery<Real, Rectangle3<Real>, OrientedBox3<Real>>::operator()(
61  Rectangle3<Real> const& rectangle, OrientedBox3<Real> const& box)
62 {
63  Result result;
64 
65  // Rigidly transform the rectangle and oriented box so that the oriented
66  // box becomes a canonical box.
67  Vector3<Real> K = box.extent * (Real)2;
68  Vector3<Real> tempV = rectangle.center - box.center;
69  Vector3<Real> V, E0, E1;
70  for (int i = 0; i < 3; ++i)
71  {
72  V[i] = Dot(box.axis[i], tempV) + box.extent[i];
73  E0[i] = Dot(box.axis[i], rectangle.axis[0]);
74  E1[i] = Dot(box.axis[i], rectangle.axis[1]);
75  }
76 
77  // Convert the oriented rectangle to a regular one (origin at a corner).
78  Vector3<Real> scaledE0 = E0 * rectangle.extent[0];
79  Vector3<Real> scaledE1 = E1 * rectangle.extent[1];
80  V -= scaledE0 + scaledE1;
81  E0 = scaledE0 * (Real)2;
82  E1 = scaledE1 * (Real)2;
83 
84  // Compute quantities to initialize q and M in the LCP.
85  Real dotVE0 = Dot(V, E0);
86  Real dotVE1 = Dot(V, E1);
87  Real dotE0E0 = Dot(E0, E0);
88  Real dotE1E1 = Dot(E1, E1);
89 
90  // The LCP has 5 variables and 5 (nontrivial) inequality constraints.
91  std::array<Real, 10> q =
92  {
93  -V[0], -V[1], -V[2], dotVE0, dotVE1, K[0], K[1], K[2], (Real)1, (Real)1
94  };
95 
96  std::array<std::array<Real, 10>, 10> M;
97  M[0] = { (Real)1, (Real)0, (Real)0, -E0[0], -E1[0], (Real)1, (Real)0, (Real)0, (Real)0, (Real)0 };
98  M[1] = { (Real)0, (Real)1, (Real)0, -E0[1], -E1[1], (Real)0, (Real)1, (Real)0, (Real)0, (Real)0 };
99  M[2] = { (Real)0, (Real)0, (Real)1, -E0[2], -E1[2], (Real)0, (Real)0, (Real)1, (Real)0 , (Real)0 };
100  M[3] = { -E0[0], -E0[1], -E0[2], dotE0E0, (Real)0, (Real)0, (Real)0, (Real)0, (Real)1, (Real)0 };
101  M[4] = { -E1[0], -E1[1], -E1[2], (Real)0, dotE1E1, (Real)0, (Real)0, (Real)0, (Real)0, (Real)1 };
102  M[5] = { (Real)-1, (Real)0, (Real)0, (Real)0, (Real)0, (Real)0, (Real)0, (Real)0, (Real)0, (Real)0 };
103  M[6] = { (Real)0, (Real)-1, (Real)0, (Real)0, (Real)0, (Real)0, (Real)0, (Real)0, (Real)0, (Real)0 };
104  M[7] = { (Real)0, (Real)0, (Real)-1, (Real)0, (Real)0, (Real)0, (Real)0, (Real)0, (Real)0, (Real)0 };
105  M[8] = { (Real)0, (Real)0, (Real)0, (Real)-1, (Real)0, (Real)0, (Real)0, (Real)0, (Real)0, (Real)0 };
106  M[9] = { (Real)0, (Real)0, (Real)0, (Real)0, (Real)-1, (Real)0, (Real)0, (Real)0, (Real)0, (Real)0 };
107 
108  std::array<Real, 10> w, z;
109  if (mLCP.Solve(q, M, w, z))
110  {
111  result.queryIsSuccessful = true;
112 
113  Real t0 = (z[3] * (Real)2 - (Real)1) * rectangle.extent[0];
114  Real t1 = (z[4] * (Real)2 - (Real)1) * rectangle.extent[1];
115  result.rectangleParameter[0] = t0;
116  result.rectangleParameter[1] = t1;
117  result.closestPoint[0] = rectangle.center + t0 * rectangle.axis[0] + t1 * rectangle.axis[1];
118  result.closestPoint[1] = box.center;
119  for (int i = 0; i < 3; ++i)
120  {
121  result.boxParameter[i] = z[i] - box.extent[i];
122  result.closestPoint[1] += result.boxParameter[i] * box.axis[i];
123  }
124 
125  Vector3<Real> diff = result.closestPoint[1] - result.closestPoint[0];
126  result.sqrDistance = Dot(diff, diff);
127  result.distance = Function<Real>::Sqrt(result.sqrDistance);
128  }
129  else
130  {
131  // If you reach this case, the maximum number of iterations was not
132  // specified to be large enough or there is a problem due to
133  // floating-point rounding errors. If you believe the latter is
134  // true, file a bug report.
135  result.queryIsSuccessful = false;
136 
137  for (int i = 0; i < 2; ++i)
138  {
139  result.rectangleParameter[i] = (Real)0;
140  }
141  for (int i = 0; i < 3; ++i)
142  {
143  result.boxParameter[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 }
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t0
Definition: glext.h:9013
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t1
Definition: glext.h:9013
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
GLuint64EXT * result
Definition: glext.h:10003


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