GteDistTriangle3Rectangle3.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 <typename Real>
17 class DCPQuery<Real, Triangle3<Real>, Rectangle3<Real>>
18 {
19 public:
20  struct Result
21  {
23  Real triangleParameter[3], rectangleParameter[2];
24  Vector3<Real> closestPoint[2];
25  };
26 
27  Result operator()(Triangle3<Real> const& triangle,
28  Rectangle3<Real> const& rectangle);
29 };
30 
31 
32 template <typename Real>
35  Triangle3<Real> const& triangle, Rectangle3<Real> const& rectangle)
36 {
37  Result result;
38 
39  result.sqrDistance = std::numeric_limits<Real>::max();
40 
41  // Compare edges of triangle to the interior of rectangle.
42  for (int i0 = 2, i1 = 0; i1 < 3; i0 = i1++)
43  {
44  Vector3<Real> segCenter = ((Real)0.5)*(triangle.v[i0] +
45  triangle.v[i1]);
46  Vector3<Real> segDirection = triangle.v[i1] - triangle.v[i0];
47  Real segExtent = ((Real)0.5)*Normalize(segDirection);
48  Segment3<Real> edge(segCenter, segDirection, segExtent);
49 
51  auto srResult = srQuery(edge, rectangle);
52  if (srResult.sqrDistance < result.sqrDistance)
53  {
54  result.distance = srResult.distance;
55  result.sqrDistance = srResult.sqrDistance;
56  Real ratio = srResult.segmentParameter / segExtent; // in [-1,1]
57  result.triangleParameter[i0] = ((Real)0.5)*((Real)1 - ratio);
58  result.triangleParameter[i1] =
59  (Real)1 - result.triangleParameter[i0];
60  result.triangleParameter[3 - i0 - i1] = (Real)0;
61  result.rectangleParameter[0] = srResult.rectangleParameter[0];
62  result.rectangleParameter[1] = srResult.rectangleParameter[1];
63  result.closestPoint[0] = srResult.closestPoint[0];
64  result.closestPoint[1] = srResult.closestPoint[1];
65  }
66  }
67 
68  // Compare edges of rectangle to the interior of triangle.
69  for (int i1 = 0; i1 < 2; ++i1)
70  {
71  for (int i0 = -1; i0 <= 1; i0 += 2)
72  {
73  Real s = i0 * rectangle.extent[1 - i1];
74  Vector3<Real> segCenter = rectangle.center +
75  s * rectangle.axis[1 - i1];
76  Segment3<Real> edge(segCenter, rectangle.axis[i1],
77  rectangle.extent[i1]);
78 
80  auto stResult = stQuery(edge, triangle);
81  if (stResult.sqrDistance < result.sqrDistance)
82  {
83  result.distance = stResult.distance;
84  result.sqrDistance = stResult.sqrDistance;
85  result.triangleParameter[0] = stResult.triangleParameter[0];
86  result.triangleParameter[1] = stResult.triangleParameter[1];
87  result.triangleParameter[2] = stResult.triangleParameter[2];
88  result.rectangleParameter[i1] = s;
89  result.rectangleParameter[1 - i1] = stResult.segmentParameter;
90  result.closestPoint[0] = stResult.closestPoint[1];
91  result.closestPoint[1] = stResult.closestPoint[0];
92  }
93  }
94  }
95  return result;
96 }
97 
98 
99 }
GLsizei GLsizei GLfloat distance
Definition: glext.h:9704
Result operator()(Type0 const &primitive0, Type1 const &primitive1)
Real Normalize(GVector< Real > &v, bool robust=false)
Definition: GteGVector.h:454
GLdouble s
Definition: glext.h:231
GLuint64EXT * result
Definition: glext.h:10003


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