GtePrimalQuery3.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>
11 
12 // Queries about the relation of a point to various geometric objects. The
13 // choices for N when using UIntegerFP32<N> for either BSNumber of BSRational
14 // are determined in GeometricTools/GTEngine/Tools/PrecisionCalculator. These
15 // N-values are worst case scenarios. Your specific input data might require
16 // much smaller N, in which case you can modify PrecisionCalculator to use the
17 // BSPrecision(int32_t,int32_t,int32_t,bool) constructors.
18 
19 namespace gte
20 {
21 
22 template <typename Real>
24 {
25 public:
26  // The caller is responsible for ensuring that the array is not empty
27  // before calling queries and that the indices passed to the queries are
28  // valid. The class does no range checking.
29  PrimalQuery3();
30  PrimalQuery3(int numVertices, Vector3<Real> const* vertices);
31 
32  // Member access.
33  inline void Set(int numVertices, Vector3<Real> const* vertices);
34  inline int GetNumVertices() const;
35  inline Vector3<Real> const* GetVertices() const;
36 
37  // In the following, point P refers to vertices[i] or 'test' and Vi refers
38  // to vertices[vi].
39 
40  // For a plane with origin V0 and normal N = Cross(V1-V0,V2-V0), ToPlane
41  // returns
42  // +1, P on positive side of plane (side to which N points)
43  // -1, P on negative side of plane (side to which -N points)
44  // 0, P on the plane
45  //
46  // Choice of N for UIntegerFP32<N>.
47  // input type | compute type | N
48  // -----------+--------------+------
49  // float | BSNumber | 27
50  // double | BSNumber | 197
51  // float | BSRational | 2882
52  // double | BSRational | 21688
53  int ToPlane(int i, int v0, int v1, int v2) const;
54  int ToPlane(Vector3<Real> const& test, int v0, int v1, int v2) const;
55 
56  // For a tetrahedron with vertices ordered as described in the file
57  // GteTetrahedronKey.h, the function returns
58  // +1, P outside tetrahedron
59  // -1, P inside tetrahedron
60  // 0, P on tetrahedron
61  //
62  // Choice of N for UIntegerFP32<N>.
63  // input type | compute type | N
64  // -----------+--------------+----
65  // float | BSNumber | 27
66  // double | BSNumber | 197
67  // float | BSRational | 2882
68  // double | BSRational | 21688
69  // The query involves four calls of ToPlane, so the numbers match those
70  // of ToPlane.
71  int ToTetrahedron(int i, int v0, int v1, int v2, int v3) const;
72  int ToTetrahedron(Vector3<Real> const& test, int v0, int v1, int v2, int v3) const;
73 
74  // For a tetrahedron with vertices ordered as described in the file
75  // GteTetrahedronKey.h, the function returns
76  // +1, P outside circumsphere of tetrahedron
77  // -1, P inside circumsphere of tetrahedron
78  // 0, P on circumsphere of tetrahedron
79  //
80  // Choice of N for UIntegerFP32<N>.
81  // input type | compute type | N
82  // -----------+--------------+--------
83  // float | BSNumber | 44
84  // float | BSRational | 329
85  // double | BSNumber | 298037
86  // double | BSRational | 2254442
87  int ToCircumsphere(int i, int v0, int v1, int v2, int v3) const;
88  int ToCircumsphere(Vector3<Real> const& test, int v0, int v1, int v2, int v3) const;
89 
90 private:
93 };
94 
95 
96 template <typename Real>
98  :
99  mNumVertices(0),
100  mVertices(nullptr)
101 {
102 }
103 
104 template <typename Real>
106  Vector3<Real> const* vertices)
107  :
108  mNumVertices(numVertices),
109  mVertices(vertices)
110 {
111 }
112 
113 template <typename Real> inline
114 void PrimalQuery3<Real>::Set(int numVertices, Vector3<Real> const* vertices)
115 {
116  mNumVertices = numVertices;
117  mVertices = vertices;
118 }
119 
120 template <typename Real> inline
122 {
123  return mNumVertices;
124 }
125 
126 template <typename Real> inline
128 {
129  return mVertices;
130 }
131 
132 template <typename Real>
133 int PrimalQuery3<Real>::ToPlane(int i, int v0, int v1, int v2) const
134 {
135  return ToPlane(mVertices[i], v0, v1, v2);
136 }
137 
138 template <typename Real>
139 int PrimalQuery3<Real>::ToPlane(Vector3<Real> const& test, int v0, int v1,
140  int v2) const
141 {
142  Vector3<Real> const& vec0 = mVertices[v0];
143  Vector3<Real> const& vec1 = mVertices[v1];
144  Vector3<Real> const& vec2 = mVertices[v2];
145 
146  Real x0 = test[0] - vec0[0];
147  Real y0 = test[1] - vec0[1];
148  Real z0 = test[2] - vec0[2];
149  Real x1 = vec1[0] - vec0[0];
150  Real y1 = vec1[1] - vec0[1];
151  Real z1 = vec1[2] - vec0[2];
152  Real x2 = vec2[0] - vec0[0];
153  Real y2 = vec2[1] - vec0[1];
154  Real z2 = vec2[2] - vec0[2];
155  Real y1z2 = y1*z2;
156  Real y2z1 = y2*z1;
157  Real y2z0 = y2*z0;
158  Real y0z2 = y0*z2;
159  Real y0z1 = y0*z1;
160  Real y1z0 = y1*z0;
161  Real c0 = y1z2 - y2z1;
162  Real c1 = y2z0 - y0z2;
163  Real c2 = y0z1 - y1z0;
164  Real x0c0 = x0*c0;
165  Real x1c1 = x1*c1;
166  Real x2c2 = x2*c2;
167  Real term = x0c0 + x1c1;
168  Real det = term + x2c2;
169  Real const zero(0);
170 
171  return (det > zero ? +1 : (det < zero ? -1 : 0));
172 }
173 
174 template <typename Real>
175 int PrimalQuery3<Real>::ToTetrahedron(int i, int v0, int v1, int v2, int v3)
176  const
177 {
178  return ToTetrahedron(mVertices[i], v0, v1, v2, v3);
179 }
180 
181 template <typename Real>
183  int v1, int v2, int v3) const
184 {
185  int sign0 = ToPlane(test, v1, v2, v3);
186  if (sign0 > 0)
187  {
188  return +1;
189  }
190 
191  int sign1 = ToPlane(test, v0, v2, v3);
192  if (sign1 < 0)
193  {
194  return +1;
195  }
196 
197  int sign2 = ToPlane(test, v0, v1, v3);
198  if (sign2 > 0)
199  {
200  return +1;
201  }
202 
203  int sign3 = ToPlane(test, v0, v1, v2);
204  if (sign3 < 0)
205  {
206  return +1;
207  }
208 
209  return ((sign0 && sign1 && sign2 && sign3) ? -1 : 0);
210 }
211 
212 template <typename Real>
213 int PrimalQuery3<Real>::ToCircumsphere(int i, int v0, int v1, int v2, int v3)
214 const
215 {
216  return ToCircumsphere(mVertices[i], v0, v1, v2, v3);
217 }
218 
219 template <typename Real>
221  int v1, int v2, int v3) const
222 {
223  Vector3<Real> const& vec0 = mVertices[v0];
224  Vector3<Real> const& vec1 = mVertices[v1];
225  Vector3<Real> const& vec2 = mVertices[v2];
226  Vector3<Real> const& vec3 = mVertices[v3];
227 
228  Real x0 = vec0[0] - test[0];
229  Real y0 = vec0[1] - test[1];
230  Real z0 = vec0[2] - test[2];
231  Real s00 = vec0[0] + test[0];
232  Real s01 = vec0[1] + test[1];
233  Real s02 = vec0[2] + test[2];
234  Real t00 = s00*x0;
235  Real t01 = s01*y0;
236  Real t02 = s02*z0;
237  Real t00pt01 = t00 + t01;
238  Real w0 = t00pt01 + t02;
239 
240  Real x1 = vec1[0] - test[0];
241  Real y1 = vec1[1] - test[1];
242  Real z1 = vec1[2] - test[2];
243  Real s10 = vec1[0] + test[0];
244  Real s11 = vec1[1] + test[1];
245  Real s12 = vec1[2] + test[2];
246  Real t10 = s10*x1;
247  Real t11 = s11*y1;
248  Real t12 = s12*z1;
249  Real t10pt11 = t10 + t11;
250  Real w1 = t10pt11 + t12;
251 
252  Real x2 = vec2[0] - test[0];
253  Real y2 = vec2[1] - test[1];
254  Real z2 = vec2[2] - test[2];
255  Real s20 = vec2[0] + test[0];
256  Real s21 = vec2[1] + test[1];
257  Real s22 = vec2[2] + test[2];
258  Real t20 = s20*x2;
259  Real t21 = s21*y2;
260  Real t22 = s22*z2;
261  Real t20pt21 = t20 + t21;
262  Real w2 = t20pt21 + t22;
263 
264  Real x3 = vec3[0] - test[0];
265  Real y3 = vec3[1] - test[1];
266  Real z3 = vec3[2] - test[2];
267  Real s30 = vec3[0] + test[0];
268  Real s31 = vec3[1] + test[1];
269  Real s32 = vec3[2] + test[2];
270  Real t30 = s30*x3;
271  Real t31 = s31*y3;
272  Real t32 = s32*z3;
273  Real t30pt31 = t30 + t31;
274  Real w3 = t30pt31 + t32;
275 
276  Real x0y1 = x0*y1;
277  Real x0y2 = x0*y2;
278  Real x0y3 = x0*y3;
279  Real x1y0 = x1*y0;
280  Real x1y2 = x1*y2;
281  Real x1y3 = x1*y3;
282  Real x2y0 = x2*y0;
283  Real x2y1 = x2*y1;
284  Real x2y3 = x2*y3;
285  Real x3y0 = x3*y0;
286  Real x3y1 = x3*y1;
287  Real x3y2 = x3*y2;
288  Real a0 = x0y1 - x1y0;
289  Real a1 = x0y2 - x2y0;
290  Real a2 = x0y3 - x3y0;
291  Real a3 = x1y2 - x2y1;
292  Real a4 = x1y3 - x3y1;
293  Real a5 = x2y3 - x3y2;
294 
295  Real z0w1 = z0*w1;
296  Real z0w2 = z0*w2;
297  Real z0w3 = z0*w3;
298  Real z1w0 = z1*w0;
299  Real z1w2 = z1*w2;
300  Real z1w3 = z1*w3;
301  Real z2w0 = z2*w0;
302  Real z2w1 = z2*w1;
303  Real z2w3 = z2*w3;
304  Real z3w0 = z3*w0;
305  Real z3w1 = z3*w1;
306  Real z3w2 = z3*w2;
307  Real b0 = z0w1 - z1w0;
308  Real b1 = z0w2 - z2w0;
309  Real b2 = z0w3 - z3w0;
310  Real b3 = z1w2 - z2w1;
311  Real b4 = z1w3 - z3w1;
312  Real b5 = z2w3 - z3w2;
313  Real a0b5 = a0*b5;
314  Real a1b4 = a1*b4;
315  Real a2b3 = a2*b3;
316  Real a3b2 = a3*b2;
317  Real a4b1 = a4*b1;
318  Real a5b0 = a5*b0;
319  Real term0 = a0b5 - a1b4;
320  Real term1 = term0 + a2b3;
321  Real term2 = term1 + a3b2;
322  Real term3 = term2 - a4b1;
323  Real det = term3 + a5b0;
324  Real const zero(0);
325 
326  return (det > zero ? 1 : (det < zero ? -1 : 0));
327 }
328 
329 
330 }
int ToPlane(int i, int v0, int v1, int v2) const
GLfloat GLfloat v1
Definition: glcorearb.h:812
int ToCircumsphere(int i, int v0, int v1, int v2, int v3) const
GLfixed GLfixed GLfixed y2
Definition: glext.h:4952
int GetNumVertices() const
int ToTetrahedron(int i, int v0, int v1, int v2, int v3) const
Vector3< Real > const * GetVertices() const
Vector3< Real > const * mVertices
GLfixed y1
Definition: glext.h:4952
GLuint GLfloat x0
Definition: glext.h:9013
GLfloat GLfloat GLfloat GLfloat v3
Definition: glcorearb.h:814
GLfloat v0
Definition: glcorearb.h:811
GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint GLdouble GLdouble w2
Definition: glext.h:11365
void Set(int numVertices, Vector3< Real > const *vertices)
GLuint GLfloat GLfloat GLfloat x1
Definition: glext.h:9013
GLfloat GLfloat GLfloat v2
Definition: glcorearb.h:813
GLfixed GLfixed x2
Definition: glext.h:4952
GLuint GLfloat GLfloat y0
Definition: glext.h:9013
GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint GLdouble w1
Definition: glext.h:11365


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