GteIntrHalfspace3Triangle3.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 
10 #include <Mathematics/GteVector3.h>
13 #include <Mathematics/GteFIQuery.h>
14 #include <Mathematics/GteTIQuery.h>
15 
16 // Queries for intersection of objects with halfspaces. These are useful for
17 // containment testing, object culling, and clipping.
18 
19 namespace gte
20 {
21 
22 template <typename Real>
23 class TIQuery<Real, Halfspace3<Real>, Triangle3<Real>>
24 {
25 public:
26  struct Result
27  {
28  bool intersect;
29  };
30 
31  Result operator()(Halfspace3<Real> const& halfspace,
32  Triangle3<Real> const& triangle);
33 };
34 
35 template <typename Real>
36 class FIQuery<Real, Halfspace3<Real>, Triangle3<Real>>
37 {
38 public:
39  struct Result
40  {
41  bool intersect;
42 
43  // The triangle is clipped against the plane defining the halfspace.
44  // The 'numPoints' is either 0 (no intersection), 1 (point), 2
45  // (segment), 3 (triangle), or 4 (quadrilateral).
46  int numPoints;
47  Vector3<Real> point[4];
48  };
49 
50  Result operator()(Halfspace3<Real> const& halfspace,
51  Triangle3<Real> const& triangle);
52 };
53 
54 
55 template <typename Real>
58  Halfspace3<Real> const& halfspace, Triangle3<Real> const& triangle)
59 {
60  Result result;
61 
62  // Project the triangle vertices onto the normal line. The plane of the
63  // halfspace occurs at the origin (zero) of the normal line.
64  Real s[3];
65  for (int i = 0; i < 3; ++i)
66  {
67  s[i] = Dot(halfspace.normal, triangle.v[i]) - halfspace.constant;
68  }
69 
70  // The triangle and halfspace intersect when the projection interval
71  // maximum is nonnegative.
72  result.intersect = (std::max(std::max(s[0], s[1]), s[2]) >= (Real)0);
73  return result;
74 }
75 
76 template <typename Real>
79  Halfspace3<Real> const& halfspace, Triangle3<Real> const& triangle)
80 {
81  Result result;
82 
83  // Determine on which side of the plane the vertices lie. The table of
84  // possibilities is listed next with n = numNegative, p = numPositive, and
85  // z = numZero.
86  //
87  // n p z intersection
88  // ---------------------------------
89  // 0 3 0 triangle (original)
90  // 0 2 1 triangle (original)
91  // 0 1 2 triangle (original)
92  // 0 0 3 triangle (original)
93  // 1 2 0 quad (2 edges clipped)
94  // 1 1 1 triangle (1 edge clipped)
95  // 1 0 2 edge
96  // 2 1 0 triangle (2 edges clipped)
97  // 2 0 1 vertex
98  // 3 0 0 none
99 
100  Real s[3];
101  int numPositive = 0, numNegative = 0, numZero = 0;
102  for (int i = 0; i < 3; ++i)
103  {
104  s[i] = Dot(halfspace.normal, triangle.v[i]) - halfspace.constant;
105  if (s[i] >(Real)0)
106  {
107  ++numPositive;
108  }
109  else if (s[i] < (Real)0)
110  {
111  ++numNegative;
112  }
113  else
114  {
115  ++numZero;
116  }
117  }
118 
119  if (numNegative == 0)
120  {
121  // The triangle is in the halfspace.
122  result.intersect = true;
123  result.numPoints = 3;
124  result.point[0] = triangle.v[0];
125  result.point[1] = triangle.v[1];
126  result.point[2] = triangle.v[2];
127  }
128  else if (numNegative == 1)
129  {
130  result.intersect = true;
131  if (numPositive == 2)
132  {
133  // The portion of the triangle in the halfspace is a
134  // quadrilateral.
135  result.numPoints = 4;
136  for (int i0 = 0; i0 < 3; ++i0)
137  {
138  if (s[i0] < (Real)0)
139  {
140  int i1 = (i0 + 1) % 3, i2 = (i0 + 2) % 3;
141  result.point[0] = triangle.v[i1];
142  result.point[1] = triangle.v[i2];
143  Real t2 = s[i2] / (s[i2] - s[i0]);
144  Real t0 = s[i0] / (s[i0] - s[i1]);
145  result.point[2] = triangle.v[i2] + t2 *
146  (triangle.v[i0] - triangle.v[i2]);
147  result.point[3] = triangle.v[i0] + t0 *
148  (triangle.v[i1] - triangle.v[i0]);
149  break;
150  }
151  }
152  }
153  else if (numPositive == 1)
154  {
155  // The portion of the triangle in the halfspace is a triangle
156  // with one vertex on the plane.
157  result.numPoints = 3;
158  for (int i0 = 0; i0 < 3; ++i0)
159  {
160  if (s[i0] == (Real)0)
161  {
162  int i1 = (i0 + 1) % 3, i2 = (i0 + 2) % 3;
163  result.point[0] = triangle.v[i0];
164  Real t1 = s[i1] / (s[i1] - s[i2]);
165  Vector3<Real> p = triangle.v[i1] + t1 *
166  (triangle.v[i2] - triangle.v[i1]);
167  if (s[i1] >(Real)0)
168  {
169  result.point[1] = triangle.v[i1];
170  result.point[2] = p;
171  }
172  else
173  {
174  result.point[1] = p;
175  result.point[2] = triangle.v[i2];
176  }
177  break;
178  }
179  }
180  }
181  else
182  {
183  // Only an edge of the triangle is in the halfspace.
184  result.numPoints = 0;
185  for (int i = 0; i < 3; ++i)
186  {
187  if (s[i] == (Real)0)
188  {
189  result.point[result.numPoints++] = triangle.v[i];
190  }
191  }
192  }
193  }
194  else if (numNegative == 2)
195  {
196  result.intersect = true;
197  if (numPositive == 1)
198  {
199  // The portion of the triangle in the halfspace is a triangle.
200  result.numPoints = 3;
201  for (int i0 = 0; i0 < 3; ++i0)
202  {
203  if (s[i0] >(Real)0)
204  {
205  int i1 = (i0 + 1) % 3, i2 = (i0 + 2) % 3;
206  result.point[0] = triangle.v[i0];
207  Real t0 = s[i0] / (s[i0] - s[i1]);
208  Real t2 = s[i2] / (s[i2] - s[i0]);
209  result.point[1] = triangle.v[i0] + t0 *
210  (triangle.v[i1] - triangle.v[i0]);
211  result.point[2] = triangle.v[i2] + t2 *
212  (triangle.v[i0] - triangle.v[i2]);
213  break;
214  }
215  }
216  }
217  else
218  {
219  // Only a vertex of the triangle is in the halfspace.
220  result.numPoints = 1;
221  for (int i = 0; i < 3; ++i)
222  {
223  if (s[i] == (Real)0)
224  {
225  result.point[0] = triangle.v[i];
226  break;
227  }
228  }
229  }
230  }
231  else // numNegative == 3
232  {
233  // The triangle is outside the halfspace. (numNegative == 3)
234  result.intersect = false;
235  result.numPoints = 0;
236  }
237 
238  return result;
239 }
240 
241 
242 }
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
DualQuaternion< Real > Dot(DualQuaternion< Real > const &d0, DualQuaternion< Real > const &d1)
GLdouble s
Definition: glext.h:231
Result operator()(Type0 const &primitive0, Type1 const &primitive1)
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 04:00:00