Ocean
BoundingBox.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_MATH_BOUNDING_BOX_H
9 #define META_OCEAN_MATH_BOUNDING_BOX_H
10 
11 #include "ocean/math/Math.h"
12 #include "ocean/math/Box3.h"
14 
15 namespace Ocean
16 {
17 
18 /**
19  * This class implements a 3D bounding box.
20  * @ingroup math
21  */
22 class OCEAN_MATH_EXPORT BoundingBox : public Box3
23 {
24  public:
25 
26  /**
27  * Creates an invalid bounding box.
28  */
29  inline BoundingBox();
30 
31  /**
32  * Creates an invalid bounding box.
33  */
34  inline BoundingBox(const Box3& box);
35 
36  /**
37  * Creates a new bounding box by two given corners.
38  * @param lower Lower corner
39  * @param higher Higher corner
40  */
41  inline BoundingBox(const Vector3& lower, const Vector3& higher);
42 
43  /**
44  * Creates a new bounding box enclosing a given set of 3D points.
45  * @param points Points to be enclosed by the bounding box
46  */
47  inline explicit BoundingBox(const Vectors3& points);
48 
49  /**
50  * Creates a new bounding box enclosing a given set of 3D points.
51  * @param points Points to be enclosed by the bounding box
52  * @param number Number of points
53  */
54  inline BoundingBox(const Vector3* points, const unsigned int number);
55 
56  /**
57  * Returns the front intersection point between a given ray and this box whenever the distance is positive.
58  * The dot product between the ray direction and the intersection normal will be negative.<br>
59  * @param ray Ray to determine the intersection point for
60  * @param position Resulting intersection position
61  * @param distance Resulting intersection distance
62  * @return True, if so
63  */
64  bool positiveFrontIntersection(const Line3& ray, Vector3& position, Scalar& distance) const;
65 
66  /**
67  * Returns the front intersection point between a given ray and this box whenever the distance is positive.
68  * The dot product between the ray direction and the intersection normal will be negative.<br>
69  * @param ray Ray to determine the intersection point for
70  * @param position Resulting intersection position
71  * @param distance Resulting intersection distance
72  * @param normal Resulting intersection normal
73  * @return True, if so
74  */
75  bool positiveFrontIntersection(const Line3& ray, Vector3& position, Scalar& distance, Vector3& normal) const;
76 
77  /**
78  * Returns the front intersection point between a given ray and this box whenever the distance is positive.
79  * The dot product between the ray direction and the intersection normal will be negative.<br>
80  * @param ray Ray to determine the intersection point for
81  * @param position Resulting intersection position
82  * @param distance Resulting intersection distance
83  * @param normal Resulting intersection normal
84  * @param textureCoordinate Resulting texture coordinate
85  * @return True, if so
86  */
87  bool positiveFrontIntersection(const Line3& ray, Vector3& position, Scalar& distance, Vector3& normal, Vector2& textureCoordinate) const;
88 
89  /**
90  * Returns whether a given ray has an intersection with this box.
91  * @param ray Ray to be tested
92  * @param transformation Transformation of the given world transformation (for the box)
93  * @param invertedTransformation Inverted transformation of the given world transformation (for this box)
94  * @param position Resulting nearest intersection position
95  * @param distance Resulting intersection distance
96  * @return True, if so
97  */
98  inline bool positiveFrontIntersection(const Line3& ray, const HomogenousMatrix4& transformation, const HomogenousMatrix4& invertedTransformation, Vector3& position, Scalar& distance) const;
99 
100  /**
101  * Returns whether a given ray has an intersection with this box.
102  * @param ray Ray to be tested
103  * @param transformation Transformation of the given world transformation (for the box)
104  * @param invertedTransformation Inverted transformation of the given world transformation (for this box)
105  * @param position Resulting nearest intersection position
106  * @param distance Resulting intersection distance
107  * @param normal Resulting intersection normal
108  * @return True, if so
109  */
110  inline bool positiveFrontIntersection(const Line3& ray, const HomogenousMatrix4& transformation, const HomogenousMatrix4& invertedTransformation, Vector3& position, Scalar& distance, Vector3& normal) const;
111 
112  /**
113  * Returns the front intersection point between a given ray and this box whenever the distance is positive.
114  * The dot product between the ray direction and the intersection normal will be negative.<br>
115  * @param ray Ray to determine the intersection point for
116  * @param transformation Transformation of the given world transformation (for the box)
117  * @param invertedTransformation Inverted transformation of the given world transformation (for this box)
118  * @param position Resulting intersection position
119  * @param distance Resulting intersection distance
120  * @param normal Resulting intersection normal
121  * @param textureCoordinate Resulting texture coordinate
122  * @return True, if so
123  */
124  inline bool positiveFrontIntersection(const Line3& ray, const HomogenousMatrix4& transformation, const HomogenousMatrix4& invertedTransformation, Vector3& position, Scalar& distance, Vector3& normal, Vector2& textureCoordinate) const;
125 
126  /**
127  * Returns the back intersection point between a given ray and this box whenever the distance is positive.
128  * The dot product between the ray direction and the intersection normal will be positive.<br>
129  * @param ray Ray to determine the intersection point for
130  * @param position Resulting intersection position
131  * @param distance Resulting intersection distance
132  * @return True, if so
133  */
134  bool positiveBackIntersection(const Line3& ray, Vector3& position, Scalar& distance) const;
135 
136  /**
137  * Returns the back intersection point between a given ray and this box whenever the distance is positive.
138  * The dot product between the ray direction and the intersection normal will be positive.<br>
139  * @param ray Ray to determine the intersection point for
140  * @param position Resulting intersection position
141  * @param distance Resulting intersection distance
142  * @param normal Resulting intersection normal
143  * @return True, if so
144  */
145  bool positiveBackIntersection(const Line3& ray, Vector3& position, Scalar& distance, Vector3& normal) const;
146 
147  /**
148  * Returns the back intersection point between a given ray and this box whenever the distance is positive.
149  * The dot product between the ray direction and the intersection normal will be positive.<br>
150  * @param ray Ray to determine the intersection point for
151  * @param position Resulting intersection position
152  * @param distance Resulting intersection distance
153  * @param normal Resulting intersection normal
154  * @param textureCoordinate Resulting texture coordinate
155  * @return True, if so
156  */
157  bool positiveBackIntersection(const Line3& ray, Vector3& position, Scalar& distance, Vector3& normal, Vector2& textureCoordinate) const;
158 
159  /**
160  * Returns the back intersection point between a given ray and this box whenever the distance is positive.
161  * The dot product between the ray direction and the intersection normal will be positive.<br>
162  * @param ray Ray to determine the intersection point for
163  * @param transformation Transformation of the given world transformation (for the sphere)
164  * @param invertedTransformation Inverted transformation of the given world transformation (for this box)
165  * @param position Resulting intersection position
166  * @param distance Resulting intersection distance
167  * @return True, if so
168  */
169  inline bool positiveBackIntersection(const Line3& ray, const HomogenousMatrix4& transformation, const HomogenousMatrix4& invertedTransformation, Vector3& position, Scalar& distance) const;
170 
171  /**
172  * Returns the back intersection point between a given ray and this box whenever the distance is positive.
173  * The dot product between the ray direction and the intersection normal will be positive.<br>
174  * @param ray Ray to determine the intersection point for
175  * @param transformation Transformation of the given world transformation (for the sphere)
176  * @param invertedTransformation Inverted transformation of the given world transformation (for this box)
177  * @param position Resulting intersection position
178  * @param distance Resulting intersection distance
179  * @param normal Resulting intersection normal
180  * @return True, if so
181  */
182  inline bool positiveBackIntersection(const Line3& ray, const HomogenousMatrix4& transformation, const HomogenousMatrix4& invertedTransformation, Vector3& position, Scalar& distance, Vector3& normal) const;
183 
184  /**
185  * Returns the back intersection point between a given ray and this box whenever the distance is positive.
186  * The dot product between the ray direction and the intersection normal will be positive.<br>
187  * @param ray Ray to determine the intersection point for
188  * @param transformation Transformation of the given world transformation (for the sphere)
189  * @param invertedTransformation Inverted transformation of the given world transformation (for this box)
190  * @param position Resulting intersection position
191  * @param distance Resulting intersection distance
192  * @param normal Resulting intersection normal
193  * @param textureCoordinate Resulting texture coordinate
194  * @return True, if so
195  */
196  inline bool positiveBackIntersection(const Line3& ray, const HomogenousMatrix4& transformation, const HomogenousMatrix4& invertedTransformation, Vector3& position, Scalar& distance, Vector3& normal, Vector2& textureCoordinate) const;
197 };
198 
200  Box3()
201 {
202  // nothing to do here
203 }
204 
205 inline BoundingBox::BoundingBox(const Box3& box) :
206  Box3(box)
207 {
208  // nothing to do here
209 }
210 
211 inline BoundingBox::BoundingBox(const Vector3& lower, const Vector3& higher) :
212  Box3(lower, higher)
213 {
214  // nothing to do here
215 }
216 
217 inline BoundingBox::BoundingBox(const Vectors3& points) :
218  Box3(points)
219 {
220  // nothing to do here
221 }
222 
223 inline BoundingBox::BoundingBox(const Vector3* points, const unsigned int number) :
224  Box3(points, number)
225 {
226  // nothing to do here
227 }
228 
229 inline bool BoundingBox::positiveFrontIntersection(const Line3& ray, const HomogenousMatrix4& transformation, const HomogenousMatrix4& invertedTransformation, Vector3& position, Scalar& distance) const
230 {
231  ocean_assert(invertedTransformation.isValid());
232  ocean_assert(ray.isValid());
233 
234  if (positiveFrontIntersection(Line3(invertedTransformation * ray.point(), invertedTransformation.rotationMatrix(ray.direction())), position, distance))
235  {
236  position = transformation * position;
237  return true;
238  }
239 
240  return false;
241 }
242 
243 inline bool BoundingBox::positiveFrontIntersection(const Line3& ray, const HomogenousMatrix4& transformation, const HomogenousMatrix4& invertedTransformation, Vector3& position, Scalar& distance, Vector3& normal) const
244 {
245  ocean_assert(invertedTransformation.isValid());
246  ocean_assert(ray.isValid());
247 
248  if (positiveFrontIntersection(Line3(invertedTransformation * ray.point(), invertedTransformation.rotationMatrix(ray.direction())), position, distance, normal))
249  {
250  position = transformation * position;
251  normal = invertedTransformation.transposedRotationMatrix(normal).normalizedOrZero();
252  return true;
253  }
254 
255  return false;
256 }
257 
258 inline bool BoundingBox::positiveFrontIntersection(const Line3& ray, const HomogenousMatrix4& transformation, const HomogenousMatrix4& invertedTransformation, Vector3& position, Scalar& distance, Vector3& normal, Vector2& textureCoordinate) const
259 {
260  ocean_assert(invertedTransformation.isValid());
261  ocean_assert(ray.isValid());
262 
263  if (positiveFrontIntersection(Line3(invertedTransformation * ray.point(), invertedTransformation.rotationMatrix(ray.direction())), position, distance, normal, textureCoordinate))
264  {
265  position = transformation * position;
266  normal = invertedTransformation.transposedRotationMatrix(normal).normalizedOrZero();
267  return true;
268  }
269 
270  return false;
271 }
272 
273 inline bool BoundingBox::positiveBackIntersection(const Line3& ray, const HomogenousMatrix4& transformation, const HomogenousMatrix4& invertedTransformation, Vector3& position, Scalar& distance) const
274 {
275  ocean_assert(invertedTransformation.isValid());
276  ocean_assert(ray.isValid());
277 
278  if (positiveBackIntersection(Line3(invertedTransformation * ray.point(), invertedTransformation.rotationMatrix(ray.direction())), position, distance))
279  {
280  position = transformation * position;
281  return true;
282  }
283 
284  return false;
285 }
286 
287 inline bool BoundingBox::positiveBackIntersection(const Line3& ray, const HomogenousMatrix4& transformation, const HomogenousMatrix4& invertedTransformation, Vector3& position, Scalar& distance, Vector3& normal) const
288 {
289  ocean_assert(invertedTransformation.isValid());
290  ocean_assert(ray.isValid());
291 
292  if (positiveBackIntersection(Line3(invertedTransformation * ray.point(), invertedTransformation.rotationMatrix(ray.direction())), position, distance, normal))
293  {
294  position = transformation * position;
295  normal = invertedTransformation.transposedRotationMatrix(normal).normalizedOrZero();
296  return true;
297  }
298 
299  return false;
300 }
301 
302 inline bool BoundingBox::positiveBackIntersection(const Line3& ray, const HomogenousMatrix4& transformation, const HomogenousMatrix4& invertedTransformation, Vector3& position, Scalar& distance, Vector3& normal, Vector2& textureCoordinate) const
303 {
304  ocean_assert(invertedTransformation.isValid());
305  ocean_assert(ray.isValid());
306 
307  if (positiveBackIntersection(Line3(invertedTransformation * ray.point(), invertedTransformation.rotationMatrix(ray.direction())), position, distance, normal, textureCoordinate))
308  {
309  position = transformation * position;
310  normal = invertedTransformation.transposedRotationMatrix(normal).normalizedOrZero();
311  return true;
312  }
313 
314  return false;
315 }
316 
317 }
318 
319 #endif // META_OCEAN_MATH_BOUNDING_BOX_H
This class implements a 3D bounding box.
Definition: BoundingBox.h:23
BoundingBox()
Creates an invalid bounding box.
Definition: BoundingBox.h:199
bool positiveFrontIntersection(const Line3 &ray, Vector3 &position, Scalar &distance, Vector3 &normal, Vector2 &textureCoordinate) const
Returns the front intersection point between a given ray and this box whenever the distance is positi...
bool positiveBackIntersection(const Line3 &ray, Vector3 &position, Scalar &distance, Vector3 &normal, Vector2 &textureCoordinate) const
Returns the back intersection point between a given ray and this box whenever the distance is positiv...
bool positiveBackIntersection(const Line3 &ray, Vector3 &position, Scalar &distance, Vector3 &normal) const
Returns the back intersection point between a given ray and this box whenever the distance is positiv...
bool positiveFrontIntersection(const Line3 &ray, Vector3 &position, Scalar &distance, Vector3 &normal) const
Returns the front intersection point between a given ray and this box whenever the distance is positi...
bool positiveFrontIntersection(const Line3 &ray, Vector3 &position, Scalar &distance) const
Returns the front intersection point between a given ray and this box whenever the distance is positi...
bool positiveBackIntersection(const Line3 &ray, Vector3 &position, Scalar &distance) const
Returns the back intersection point between a given ray and this box whenever the distance is positiv...
VectorT3< T > transposedRotationMatrix(const VectorT3< T > &vector) const
Transforms a 3D vector by application of only the inner transposed rotation matrix (including scale a...
Definition: HomogenousMatrix4.h:1947
SquareMatrixT3< T > rotationMatrix() const
Returns the rotation matrix of the transformation.
Definition: HomogenousMatrix4.h:1493
bool isValid() const
Returns whether this matrix is a valid homogeneous transformation.
Definition: HomogenousMatrix4.h:1806
This class implements an infinite line in 3D space.
Definition: Line3.h:70
bool isValid() const
Returns whether this line has valid parameters.
Definition: Line3.h:303
const VectorT3< T > & direction() const
Returns the direction of the line.
Definition: Line3.h:284
const VectorT3< T > & point() const
Returns a point on the line.
Definition: Line3.h:271
VectorT3< T > normalizedOrZero() const
Returns the normalized vector.
Definition: Vector3.h:619
LineT3< Scalar > Line3
Definition of the Line3 object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION either with single o...
Definition: Line3.h:21
float Scalar
Definition of a scalar type.
Definition: Math.h:128
std::vector< Vector3 > Vectors3
Definition of a vector holding Vector3 objects.
Definition: Vector3.h:65
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15