GteIntrHalfspace3Segment3.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>
12 #include <Mathematics/GteSegment.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>, Segment3<Real>>
24 {
25 public:
26  struct Result
27  {
28  bool intersect;
29  };
30 
31  Result operator()(Halfspace3<Real> const& halfspace,
32  Segment3<Real> const& segment);
33 };
34 
35 template <typename Real>
36 class FIQuery<Real, Halfspace3<Real>, Segment3<Real>>
37 {
38 public:
39  struct Result
40  {
41  bool intersect;
42 
43  // The segment is clipped against the plane defining the halfspace.
44  // The 'numPoints' is either 0 (no intersection), 1 (point), or 2
45  // (segment).
46  int numPoints;
47  Vector3<Real> point[2];
48  };
49 
50  Result operator()(Halfspace3<Real> const& halfspace,
51  Segment3<Real> const& segment);
52 };
53 
54 
55 template <typename Real>
58  Halfspace3<Real> const& halfspace, Segment3<Real> const& segment)
59 {
60  Result result;
61 
62  // Project the segment endpoints onto the normal line. The plane of the
63  // halfspace occurs at the origin (zero) of the normal line.
64  Real s[2];
65  for (int i = 0; i < 2; ++i)
66  {
67  s[i] = Dot(halfspace.normal, segment.p[i]) - halfspace.constant;
68  }
69 
70  // The segment and halfspace intersect when the projection interval
71  // maximum is nonnegative.
72  result.intersect = (std::max(s[0], s[1]) >= (Real)0);
73  return result;
74 }
75 
76 template <typename Real>
79  Halfspace3<Real> const& halfspace, Segment3<Real> const& segment)
80 {
81  // Determine on which side of the plane the endpoints lie. The table of
82  // possibilities is listed next with n = numNegative, p = numPositive, and
83  // z = numZero.
84  //
85  // n p z intersection
86  // -------------------------
87  // 0 2 0 segment (original)
88  // 0 1 1 segment (original)
89  // 0 0 2 segment (original)
90  // 1 1 0 segment (clipped)
91  // 1 0 1 point (endpoint)
92  // 2 0 0 none
93 
94  Real s[3];
95  int numPositive = 0, numNegative = 0, numZero = 0;
96  for (int i = 0; i < 2; ++i)
97  {
98  s[i] = Dot(halfspace.normal, segment.p[i]) - halfspace.constant;
99  if (s[i] >(Real)0)
100  {
101  ++numPositive;
102  }
103  else if (s[i] < (Real)0)
104  {
105  ++numNegative;
106  }
107  else
108  {
109  ++numZero;
110  }
111  }
112 
113  Result result;
114 
115  if (numNegative == 0)
116  {
117  // The segment is in the halfspace.
118  result.intersect = true;
119  result.numPoints = 2;
120  result.point[0] = segment.p[0];
121  result.point[1] = segment.p[1];
122  }
123  else if (numNegative == 1)
124  {
125  result.intersect = true;
126  result.numPoints = 1;
127  if (numPositive == 1)
128  {
129  // The segment is intersected at an interior point.
130  result.point[0] = segment.p[0] +
131  (s[0] / (s[0] - s[1]))*(segment.p[1] - segment.p[0]);
132  }
133  else // numZero = 1
134  {
135  // One segment endpoint is on the plane.
136  if (s[0] == (Real)0)
137  {
138  result.point[0] = segment.p[0];
139  }
140  else
141  {
142  result.point[0] = segment.p[1];
143  }
144  }
145  }
146  else // numNegative == 2
147  {
148  // The segment is outside the halfspace. (numNegative == 2)
149  result.intersect = false;
150  result.numPoints = 0;
151  }
152 
153  return result;
154 }
155 
156 
157 }
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)
GLuint64EXT * result
Definition: glext.h:10003


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 04:00:00