GteDistTriangle3OrientedBox3.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, Triangle3<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> triangleParameter, boxParameter;
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 81 (n = 9, 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()(Triangle3<Real> const& triangle, OrientedBox3<Real> const& box);
45 
46 private:
48 };
49 
50 
51 template <typename Real>
52 void DCPQuery<Real, Triangle3<Real>, OrientedBox3<Real>>::SetMaxLCPIterations(int maxLCPIterations)
53 {
54  mLCP.SetMaxIterations(maxLCPIterations);
55 }
56 
57 template <typename Real>
58 typename DCPQuery<Real, Triangle3<Real>, OrientedBox3<Real>>::Result
59 DCPQuery<Real, Triangle3<Real>, OrientedBox3<Real>>::operator()(
60  Triangle3<Real> const& triangle, OrientedBox3<Real> const& box)
61 {
62  Result result;
63 
64  // Rigidly transform the triangle and oriented box so that the oriented
65  // box becomes a canonical box.
66  Vector3<Real> K = box.extent * (Real)2;
67  Vector3<Real> tempV = triangle.v[0] - box.center;
68  Vector3<Real> tempE0 = triangle.v[1] - triangle.v[0];
69  Vector3<Real> tempE1 = triangle.v[2] - triangle.v[0];
70  Vector3<Real> V, E0, E1;
71  for (int i = 0; i < 3; ++i)
72  {
73  V[i] = Dot(box.axis[i], tempV) + box.extent[i];
74  E0[i] = Dot(box.axis[i], tempE0);
75  E1[i] = Dot(box.axis[i], tempE1);
76  }
77 
78  // Compute quantities to initialize q and M in the LCP.
79  Real dotVE0 = Dot(V, E0);
80  Real dotVE1 = Dot(V, E1);
81  Real dotE0E0 = Dot(E0, E0);
82  Real dotE0E1 = Dot(E0, E1);
83  Real dotE1E1 = Dot(E1, E1);
84 
85  // The LCP has 5 variables and 4 (nontrivial) inequality constraints.
86  std::array<Real, 9> q =
87  {
88  -V[0], -V[1], -V[2], dotVE0, dotVE1, K[0], K[1], K[2], (Real)1
89  };
90 
91  std::array<std::array<Real, 9>, 9> M;
92  M[0] = { (Real)1, (Real)0, (Real)0, -E0[0], -E1[0], (Real)1, (Real)0, (Real)0, (Real)0 };
93  M[1] = { (Real)0, (Real)1, (Real)0, -E0[1], -E1[1], (Real)0, (Real)1, (Real)0, (Real)0 };
94  M[2] = { (Real)0, (Real)0, (Real)1, -E0[2], -E1[2], (Real)0, (Real)0, (Real)1, (Real)0 };
95  M[3] = { -E0[0], -E0[1], -E0[2], dotE0E0, dotE0E1, (Real)0, (Real)0, (Real)0, (Real)1 };
96  M[4] = { -E1[0], -E1[1], -E1[2], dotE0E1, dotE1E1, (Real)0, (Real)0, (Real)0, (Real)1 };
97  M[5] = { (Real)-1, (Real)0, (Real)0, (Real)0, (Real)0, (Real)0, (Real)0, (Real)0, (Real)0 };
98  M[6] = { (Real)0, (Real)-1, (Real)0, (Real)0, (Real)0, (Real)0, (Real)0, (Real)0, (Real)0 };
99  M[7] = { (Real)0, (Real)0, (Real)-1, (Real)0, (Real)0, (Real)0, (Real)0, (Real)0, (Real)0 };
100  M[8] = { (Real)0, (Real)0, (Real)0, (Real)-1, (Real)-1, (Real)0, (Real)0, (Real)0, (Real)0 };
101 
102  std::array<Real, 9> w, z;
103  if (mLCP.Solve(q, M, w, z))
104  {
105  result.queryIsSuccessful = true;
106 
107  result.triangleParameter[0] = (Real)1 - z[3] - z[4];
108  result.triangleParameter[1] = z[3];
109  result.triangleParameter[2] = z[4];
110  result.closestPoint[0] = triangle.v[0] + z[3] * tempE0 + z[4] * tempE1;
111  result.closestPoint[1] = box.center;
112  for (int i = 0; i < 3; ++i)
113  {
114  result.boxParameter[i] = z[i] - box.extent[i];
115  result.closestPoint[1] += result.boxParameter[i] * box.axis[i];
116  }
117 
118  Vector3<Real> diff = result.closestPoint[1] - result.closestPoint[0];
119  result.sqrDistance = Dot(diff, diff);
120  result.distance = Function<Real>::Sqrt(result.sqrDistance);
121  }
122  else
123  {
124  // If you reach this case, the maximum number of iterations was not
125  // specified to be large enough or there is a problem due to
126  // floating-point rounding errors. If you believe the latter is
127  // true, file a bug report.
128  result.queryIsSuccessful = false;
129 
130  for (int i = 0; i < 3; ++i)
131  {
132  result.triangleParameter[i] = (Real)0;
133  result.boxParameter[i] = (Real)0;
134  result.closestPoint[0][i] = (Real)0;
135  result.closestPoint[1][i] = (Real)0;
136  }
137  result.distance = (Real)0;
138  result.sqrDistance = (Real)0;
139  }
140 
141  result.numLCPIterations = mLCP.GetNumIterations();
142  return result;
143 }
144 
145 }
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