HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ImathFrustumTest.h
Go to the documentation of this file.
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright Contributors to the OpenEXR Project.
4 //
5 
6 //
7 // A viewing frustum class
8 //
9 // This file contains algorithms applied to or in conjunction with
10 // Frustum visibility testing (Imath::Frustum).
11 //
12 // Methods for frustum-based rejection of primitives are contained here.
13 //
14 
15 #ifndef INCLUDED_IMATHFRUSTUMTEST_H
16 #define INCLUDED_IMATHFRUSTUMTEST_H
17 
18 #include "ImathExport.h"
19 #include "ImathNamespace.h"
20 
21 #include "ImathBox.h"
22 #include "ImathFrustum.h"
23 #include "ImathMatrix.h"
24 #include "ImathSphere.h"
25 #include "ImathVec.h"
26 
27 IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
28 
29 ///
30 /// template class FrustumTest<T>
31 ///
32 /// This is a helper class, designed to accelerate the case
33 /// where many tests are made against the same frustum.
34 /// That's a really common case.
35 ///
36 /// The acceleration is achieved by pre-computing the planes of
37 /// the frustum, along with the ablsolute values of the plane normals.
38 ///
39 /// How to use this
40 ///
41 /// Given that you already have:
42 /// Imath::Frustum myFrustum
43 /// Imath::Matrix44 myCameraWorldMatrix
44 ///
45 /// First, make a frustum test object:
46 /// FrustumTest myFrustumTest(myFrustum, myCameraWorldMatrix)
47 ///
48 /// Whenever the camera or frustum changes, call:
49 /// myFrustumTest.setFrustum(myFrustum, myCameraWorldMatrix)
50 ///
51 /// For each object you want to test for visibility, call:
52 /// myFrustumTest.isVisible(myBox)
53 /// myFrustumTest.isVisible(mySphere)
54 /// myFrustumTest.isVisible(myVec3)
55 /// myFrustumTest.completelyContains(myBox)
56 /// myFrustumTest.completelyContains(mySphere)
57 ///
58 /// Explanation of how it works
59 ///
60 /// We store six world-space Frustum planes (nx, ny, nz, offset)
61 ///
62 /// Points: To test a Vec3 for visibility, test it against each plane
63 /// using the normal (v dot n - offset) method. (the result is exact)
64 ///
65 /// BBoxes: To test an axis-aligned bbox, test the center against each plane
66 /// using the normal (v dot n - offset) method, but offset by the
67 /// box extents dot the abs of the plane normal. (the result is NOT
68 /// exact, but will not return false-negatives.)
69 ///
70 /// Spheres: To test a sphere, test the center against each plane
71 /// using the normal (v dot n - offset) method, but offset by the
72 /// sphere's radius. (the result is NOT exact, but will not return
73 /// false-negatives.)
74 ///
75 ///
76 /// SPECIAL NOTE: "Where are the dot products?"
77 /// Actual dot products are currently slow for most SIMD architectures.
78 /// In order to keep this code optimization-ready, the dot products
79 /// are all performed using vector adds and multipies.
80 ///
81 /// In order to do this, the plane equations are stored in "transpose"
82 /// form, with the X components grouped into an X vector, etc.
83 ///
84 
85 template <class T> class IMATH_EXPORT_TEMPLATE_TYPE FrustumTest
86 {
87 public:
88  /// @{
89  /// @name Constructors
90 
91  /// Initialize camera matrix to identity
93  {
94  Frustum<T> frust;
95  Matrix44<T> cameraMat;
96  cameraMat.makeIdentity ();
97  setFrustum (frust, cameraMat);
98  }
99 
100  /// Initialize to a given frustum and camera matrix.
101  FrustumTest (const Frustum<T>& frustum, const Matrix44<T>& cameraMat)
103  {
104  setFrustum (frustum, cameraMat);
105  }
106 
107  /// @}
108 
109  /// @{
110  /// @name Set Value
111 
112  /// Update the frustum test with a new frustum and matrix.
113  /// This should usually be called just once per frame, or however
114  /// often the camera moves.
115  void setFrustum (const Frustum<T>& frustum, const Matrix44<T>& cameraMat)
117 
118  /// @}
119 
120  /// @{
121  /// @name Query
122 
123  /// Return true if any part of the sphere is inside the frustum.
124  /// The result MAY return close false-positives, but not false-negatives.
125  bool isVisible (const Sphere3<T>& sphere) const IMATH_NOEXCEPT;
126 
127  /// Return true if any part of the box is inside the frustum.
128  /// The result MAY return close false-positives, but not false-negatives.
129  bool isVisible (const Box<Vec3<T>>& box) const IMATH_NOEXCEPT;
130 
131  /// Return true if the point is inside the frustum.
132  bool isVisible (const Vec3<T>& vec) const IMATH_NOEXCEPT;
133 
134  /// Return true if every part of the sphere is inside the frustum.
135  /// The result MAY return close false-negatives, but not false-positives.
136  bool completelyContains (const Sphere3<T>& sphere) const IMATH_NOEXCEPT;
137 
138  /// Return true if every part of the box is inside the frustum.
139  /// The result MAY return close false-negatives, but not false-positives.
140  bool completelyContains (const Box<Vec3<T>>& box) const IMATH_NOEXCEPT;
141 
142  /// Return the camera matrix (primarily for debugging)
143  IMATH_INTERNAL_NAMESPACE::Matrix44<T> cameraMat () const IMATH_NOEXCEPT
144  {
145  return cameraMatrix;
146  }
147 
148  /// Return the viewing frustum (primarily for debugging)
149  IMATH_INTERNAL_NAMESPACE::Frustum<T> currentFrustum () const IMATH_NOEXCEPT
150  {
151  return currFrustum;
152  }
153 
154  /// @}
155 
156  typedef T BaseType;
157  typedef T value_type;
158 
159 protected:
160  // To understand why the planes are stored this way, see
161  // the SPECIAL NOTE above.
162 
163  /// @cond Doxygen_Suppress
164 
165  Vec3<T> planeNormX[2]; // The X components from 6 plane equations
166  Vec3<T> planeNormY[2]; // The Y components from 6 plane equations
167  Vec3<T> planeNormZ[2]; // The Z components from 6 plane equations
168 
169  Vec3<T> planeOffsetVec[2]; // The distance offsets from 6 plane equations
170 
171  // The absolute values are stored to assist with bounding box tests.
172  Vec3<T> planeNormAbsX[2]; // The abs(X) components from 6 plane equations
173  Vec3<T> planeNormAbsY[2]; // The abs(X) components from 6 plane equations
174  Vec3<T> planeNormAbsZ[2]; // The abs(X) components from 6 plane equations
175 
176  // These are kept primarily for debugging tools.
177  Frustum<T> currFrustum;
178  Matrix44<T> cameraMatrix;
179  /// @endcond
180 };
181 
182 template <class T>
183 void
185  const Frustum<T>& frustum, const Matrix44<T>& cameraMat) IMATH_NOEXCEPT
186 {
187  Plane3<T> frustumPlanes[6];
188  frustum.planes (frustumPlanes, cameraMat);
189 
190  // Here's where we effectively transpose the plane equations.
191  // We stuff all six X's into the two planeNormX vectors, etc.
192  for (int i = 0; i < 2; ++i)
193  {
194  int index = i * 3;
195 
196  planeNormX[i] = Vec3<T> (
197  frustumPlanes[index + 0].normal.x,
198  frustumPlanes[index + 1].normal.x,
199  frustumPlanes[index + 2].normal.x);
200  planeNormY[i] = Vec3<T> (
201  frustumPlanes[index + 0].normal.y,
202  frustumPlanes[index + 1].normal.y,
203  frustumPlanes[index + 2].normal.y);
204  planeNormZ[i] = Vec3<T> (
205  frustumPlanes[index + 0].normal.z,
206  frustumPlanes[index + 1].normal.z,
207  frustumPlanes[index + 2].normal.z);
208 
209  planeNormAbsX[i] = Vec3<T> (
210  std::abs (planeNormX[i].x),
211  std::abs (planeNormX[i].y),
212  std::abs (planeNormX[i].z));
213  planeNormAbsY[i] = Vec3<T> (
214  std::abs (planeNormY[i].x),
215  std::abs (planeNormY[i].y),
216  std::abs (planeNormY[i].z));
217  planeNormAbsZ[i] = Vec3<T> (
218  std::abs (planeNormZ[i].x),
219  std::abs (planeNormZ[i].y),
220  std::abs (planeNormZ[i].z));
221 
222  planeOffsetVec[i] = Vec3<T> (
223  frustumPlanes[index + 0].distance,
224  frustumPlanes[index + 1].distance,
225  frustumPlanes[index + 2].distance);
226  }
227  currFrustum = frustum;
228  cameraMatrix = cameraMat;
229 }
230 
231 template <typename T>
232 bool
234 {
235  Vec3<T> center = sphere.center;
236  Vec3<T> radiusVec = Vec3<T> (sphere.radius, sphere.radius, sphere.radius);
237 
238  // This is a vertical dot-product on three vectors at once.
239  Vec3<T> d0 = planeNormX[0] * center.x + planeNormY[0] * center.y +
240  planeNormZ[0] * center.z - radiusVec - planeOffsetVec[0];
241 
242  if (d0.x >= 0 || d0.y >= 0 || d0.z >= 0) return false;
243 
244  Vec3<T> d1 = planeNormX[1] * center.x + planeNormY[1] * center.y +
245  planeNormZ[1] * center.z - radiusVec - planeOffsetVec[1];
246 
247  if (d1.x >= 0 || d1.y >= 0 || d1.z >= 0) return false;
248 
249  return true;
250 }
251 
252 template <typename T>
253 bool
256 {
257  Vec3<T> center = sphere.center;
258  Vec3<T> radiusVec = Vec3<T> (sphere.radius, sphere.radius, sphere.radius);
259 
260  // This is a vertical dot-product on three vectors at once.
261  Vec3<T> d0 = planeNormX[0] * center.x + planeNormY[0] * center.y +
262  planeNormZ[0] * center.z + radiusVec - planeOffsetVec[0];
263 
264  if (d0.x >= 0 || d0.y >= 0 || d0.z >= 0) return false;
265 
266  Vec3<T> d1 = planeNormX[1] * center.x + planeNormY[1] * center.y +
267  planeNormZ[1] * center.z + radiusVec - planeOffsetVec[1];
268 
269  if (d1.x >= 0 || d1.y >= 0 || d1.z >= 0) return false;
270 
271  return true;
272 }
273 
274 template <typename T>
275 bool
277 {
278  if (box.isEmpty ()) return false;
279 
280  Vec3<T> center = (box.min + box.max) / 2;
281  Vec3<T> extent = (box.max - center);
282 
283  // This is a vertical dot-product on three vectors at once.
284  Vec3<T> d0 = planeNormX[0] * center.x + planeNormY[0] * center.y +
285  planeNormZ[0] * center.z - planeNormAbsX[0] * extent.x -
286  planeNormAbsY[0] * extent.y - planeNormAbsZ[0] * extent.z -
287  planeOffsetVec[0];
288 
289  if (d0.x >= 0 || d0.y >= 0 || d0.z >= 0) return false;
290 
291  Vec3<T> d1 = planeNormX[1] * center.x + planeNormY[1] * center.y +
292  planeNormZ[1] * center.z - planeNormAbsX[1] * extent.x -
293  planeNormAbsY[1] * extent.y - planeNormAbsZ[1] * extent.z -
294  planeOffsetVec[1];
295 
296  if (d1.x >= 0 || d1.y >= 0 || d1.z >= 0) return false;
297 
298  return true;
299 }
300 
301 template <typename T>
302 bool
305 {
306  if (box.isEmpty ()) return false;
307 
308  Vec3<T> center = (box.min + box.max) / 2;
309  Vec3<T> extent = (box.max - center);
310 
311  // This is a vertical dot-product on three vectors at once.
312  Vec3<T> d0 = planeNormX[0] * center.x + planeNormY[0] * center.y +
313  planeNormZ[0] * center.z + planeNormAbsX[0] * extent.x +
314  planeNormAbsY[0] * extent.y + planeNormAbsZ[0] * extent.z -
315  planeOffsetVec[0];
316 
317  if (d0.x >= 0 || d0.y >= 0 || d0.z >= 0) return false;
318 
319  Vec3<T> d1 = planeNormX[1] * center.x + planeNormY[1] * center.y +
320  planeNormZ[1] * center.z + planeNormAbsX[1] * extent.x +
321  planeNormAbsY[1] * extent.y + planeNormAbsZ[1] * extent.z -
322  planeOffsetVec[1];
323 
324  if (d1.x >= 0 || d1.y >= 0 || d1.z >= 0) return false;
325 
326  return true;
327 }
328 
329 template <typename T>
330 bool
332 {
333  // This is a vertical dot-product on three vectors at once.
334  Vec3<T> d0 = (planeNormX[0] * vec.x) + (planeNormY[0] * vec.y) +
335  (planeNormZ[0] * vec.z) - planeOffsetVec[0];
336 
337  if (d0.x >= 0 || d0.y >= 0 || d0.z >= 0) return false;
338 
339  Vec3<T> d1 = (planeNormX[1] * vec.x) + (planeNormY[1] * vec.y) +
340  (planeNormZ[1] * vec.z) - planeOffsetVec[1];
341 
342  if (d1.x >= 0 || d1.y >= 0 || d1.z >= 0) return false;
343 
344  return true;
345 }
346 
347 /// FrustymTest of type float
349 
350 /// FrustymTest of type double
352 
353 IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
354 
355 #endif // INCLUDED_IMATHFRUSTUMTEST_H
T z
Definition: ImathVec.h:368
#define IMATH_NOEXCEPT
Definition: ImathConfig.h:78
#define IMATH_INTERNAL_NAMESPACE
Definition: ImathConfig.h:39
Definition: ImathVec.h:40
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:848
IMATH_INTERNAL_NAMESPACE::Frustum< T > currentFrustum() const IMATH_NOEXCEPT
Return the viewing frustum (primarily for debugging)
GLint y
Definition: glcorearb.h:103
FrustumTest< float > FrustumTestf
FrustymTest of type float.
T distance
The distance from the origin to the plane.
Definition: ImathPlane.h:41
FrustumTest() IMATH_NOEXCEPT
Initialize camera matrix to identity.
T x
Definition: ImathVec.h:368
FrustumTest(const Frustum< T > &frustum, const Matrix44< T > &cameraMat) IMATH_NOEXCEPT
Initialize to a given frustum and camera matrix.
GLint GLenum GLint x
Definition: glcorearb.h:409
bool isVisible(const Sphere3< T > &sphere) const IMATH_NOEXCEPT
FrustumTest< double > FrustumTestd
FrustymTest of type double.
GLuint index
Definition: glcorearb.h:786
#define IMATH_EXPORT_TEMPLATE_TYPE
Definition: ImathExport.h:61
T y
Definition: ImathVec.h:368
IMATH_INTERNAL_NAMESPACE_HEADER_ENTER IMATH_HOSTDEVICE constexpr T abs(T a) IMATH_NOEXCEPT
Definition: ImathFun.h:26
Definition: ImathBox.h:37
bool completelyContains(const Sphere3< T > &sphere) const IMATH_NOEXCEPT
Vec3< T > normal
The normal to the plane.
Definition: ImathPlane.h:38
IMATH_HOSTDEVICE void makeIdentity() IMATH_NOEXCEPT
Set to the identity matrix.
Definition: ImathMatrix.h:3641
void setFrustum(const Frustum< T > &frustum, const Matrix44< T > &cameraMat) IMATH_NOEXCEPT