GteDistTriangle3Triangle3.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 
11 
12 namespace gte
13 {
14 
15 template <typename Real>
16 class DCPQuery<Real, Triangle3<Real>, Triangle3<Real>>
17 {
18 public:
19  struct Result
20  {
22  Real triangle0Parameter[3], triangle1Parameter[3];
23  Vector3<Real> closestPoint[2];
24  };
25 
26  Result operator()(Triangle3<Real> const& triangle0,
27  Triangle3<Real> const& triangle1);
28 };
29 
30 
31 template <typename Real>
34  Triangle3<Real> const& triangle0, Triangle3<Real> const& triangle1)
35 {
36  Result result;
37 
40  stResult;
41  result.sqrDistance = std::numeric_limits<Real>::max();
42 
43  // Compare edges of triangle0 to the interior of triangle1.
44  for (int i0 = 2, i1 = 0; i1 < 3; i0 = i1++)
45  {
46  Vector3<Real> segCenter = ((Real)0.5)*(triangle0.v[i0] +
47  triangle0.v[i1]);
48  Vector3<Real> segDirection = triangle0.v[i1] - triangle0.v[i0];
49  Real segExtent = ((Real)0.5)*Normalize(segDirection);
50  Segment3<Real> edge(segCenter, segDirection, segExtent);
51 
52  stResult = stQuery(edge, triangle1);
53  if (stResult.sqrDistance < result.sqrDistance)
54  {
55  result.distance = stResult.distance;
56  result.sqrDistance = stResult.sqrDistance;
57  Real ratio = stResult.segmentParameter / segExtent; // in [-1,1]
58  result.triangle0Parameter[i0] = ((Real)0.5)*((Real)1 - ratio);
59  result.triangle0Parameter[i1] =
60  (Real)1 - result.triangle0Parameter[i0];
61  result.triangle0Parameter[3 - i0 - i1] = (Real)0;
62  result.triangle1Parameter[0] = stResult.triangleParameter[0];
63  result.triangle1Parameter[1] = stResult.triangleParameter[1];
64  result.triangle1Parameter[2] = stResult.triangleParameter[2];
65  result.closestPoint[0] = stResult.closestPoint[0];
66  result.closestPoint[1] = stResult.closestPoint[1];
67  }
68  }
69 
70  // Compare edges of triangle1 to the interior of triangle0.
71  for (int i0 = 2, i1 = 0; i1 < 3; i0 = i1++)
72  {
73  Vector3<Real> segCenter = ((Real)0.5)*(triangle1.v[i0] +
74  triangle1.v[i1]);
75  Vector3<Real> segDirection = triangle1.v[i1] - triangle1.v[i0];
76  Real segExtent = ((Real)0.5)*Normalize(segDirection);
77  Segment3<Real> edge(segCenter, segDirection, segExtent);
78 
79  stResult = stQuery(edge, triangle0);
80  if (stResult.sqrDistance < result.sqrDistance)
81  {
82  result.distance = stResult.distance;
83  result.sqrDistance = stResult.sqrDistance;
84  Real ratio = stResult.segmentParameter / segExtent; // in [-1,1]
85  result.triangle0Parameter[0] = stResult.triangleParameter[0];
86  result.triangle0Parameter[1] = stResult.triangleParameter[1];
87  result.triangle0Parameter[2] = stResult.triangleParameter[2];
88  result.triangle1Parameter[i0] = ((Real)0.5)*((Real)1 - ratio);
89  result.triangle1Parameter[i1] =
90  (Real)1 - result.triangle0Parameter[i0];
91  result.triangle1Parameter[3 - i0 - i1] = (Real)0;
92  result.closestPoint[0] = stResult.closestPoint[0];
93  result.closestPoint[1] = stResult.closestPoint[1];
94  }
95  }
96  return result;
97 }
98 
99 
100 }
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
GLuint64EXT * result
Definition: glext.h:10003


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