| 1 | // |
| 2 | // SPDX-License-Identifier: BSD-3-Clause |
| 3 | // Copyright Contributors to the OpenEXR Project. |
| 4 | // |
| 5 | |
| 6 | // |
| 7 | // Axis-aligned bounding box |
| 8 | // |
| 9 | |
| 10 | #ifndef INCLUDED_IMATHBOX_H |
| 11 | #define INCLUDED_IMATHBOX_H |
| 12 | |
| 13 | #include "ImathExport.h" |
| 14 | #include "ImathNamespace.h" |
| 15 | |
| 16 | #include "ImathVec.h" |
| 17 | |
| 18 | IMATH_INTERNAL_NAMESPACE_HEADER_ENTER |
| 19 | |
| 20 | /// |
| 21 | /// The `Box<V>` template represents an axis-aligned bounding box defined by |
| 22 | /// minimum and maximum values of type `V`. The `min` and `max` members are |
| 23 | /// public. |
| 24 | /// |
| 25 | /// The type `V` is typically an Imath vector (i.e. `V2i`, `V3f`, etc) and must |
| 26 | /// implement an index `operator[]` that returns a type (typically as scalar) |
| 27 | /// that supports assignment, comparison, and arithmetic operators. |
| 28 | /// |
| 29 | /// `V` must also provide a constructor that takes a float and/or double for |
| 30 | /// use in initializing the box. |
| 31 | /// |
| 32 | /// `V` must also provide a function `V::dimensions()` which returns the |
| 33 | /// number of dimensions in the class (since its assumed its a vector) -- |
| 34 | /// preferably, this returns a constant expression, typically 2 or 3. |
| 35 | /// |
| 36 | |
| 37 | template <class V> class IMATH_EXPORT_TEMPLATE_TYPE Box |
| 38 | { |
| 39 | public: |
| 40 | |
| 41 | /// @{ |
| 42 | /// @name Direct access to bounds |
| 43 | |
| 44 | /// The minimum value of the box. |
| 45 | V min; |
| 46 | |
| 47 | /// The maximum value of the box. |
| 48 | V max; |
| 49 | |
| 50 | /// @} |
| 51 | |
| 52 | /// @{ |
| 53 | /// @name Constructors |
| 54 | |
| 55 | /// Construct an empty bounding box. This initializes the mimimum to |
| 56 | /// std::numeric_limits<V::baseType>::max() and the maximum to |
| 57 | /// std::numeric_limits<V::baseType>::lowest(). |
| 58 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Box() IMATH_NOEXCEPT; |
| 59 | |
| 60 | /// Construct a bounding box that contains a single point. |
| 61 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Box (const V& point) IMATH_NOEXCEPT; |
| 62 | |
| 63 | /// Construct a bounding box with the given minimum and maximum values. |
| 64 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Box (const V& minV, const V& maxV) IMATH_NOEXCEPT; |
| 65 | |
| 66 | /// @} |
| 67 | |
| 68 | /// @{ |
| 69 | /// @name Comparison |
| 70 | |
| 71 | /// Equality |
| 72 | IMATH_HOSTDEVICE constexpr bool operator== (const Box<V>& src) const IMATH_NOEXCEPT; |
| 73 | |
| 74 | /// Inequality |
| 75 | IMATH_HOSTDEVICE constexpr bool operator!= (const Box<V>& src) const IMATH_NOEXCEPT; |
| 76 | |
| 77 | /// @} |
| 78 | |
| 79 | /// @{ |
| 80 | /// @name Manipulation |
| 81 | |
| 82 | /// Set the box to be empty. A box is empty if the mimimum is greater |
| 83 | /// than the maximum. makeEmpty() sets the mimimum to `V::baseTypeMax()` |
| 84 | /// and the maximum to `V::baseTypeLowest()`. |
| 85 | IMATH_HOSTDEVICE void makeEmpty() IMATH_NOEXCEPT; |
| 86 | |
| 87 | /// Extend the box to include the given point. |
| 88 | IMATH_HOSTDEVICE void extendBy (const V& point) IMATH_NOEXCEPT; |
| 89 | |
| 90 | /// Extend the box to include the given box. |
| 91 | IMATH_HOSTDEVICE void extendBy (const Box<V>& box) IMATH_NOEXCEPT; |
| 92 | |
| 93 | /// Make the box include the entire range of `V`. |
| 94 | IMATH_HOSTDEVICE void makeInfinite() IMATH_NOEXCEPT; |
| 95 | |
| 96 | /// @} |
| 97 | |
| 98 | /// @{ |
| 99 | /// @name Query |
| 100 | |
| 101 | /// Return the size of the box. The size is of type `V`, defined |
| 102 | /// as `(max-min)`. An empty box has a size of `V(0)`, i.e. 0 in |
| 103 | /// each dimension. |
| 104 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 V size() const IMATH_NOEXCEPT; |
| 105 | |
| 106 | /// Return the center of the box. The center is defined as |
| 107 | /// `(max+min)/2`. The center of an empty box is undefined. |
| 108 | IMATH_HOSTDEVICE constexpr V center() const IMATH_NOEXCEPT; |
| 109 | |
| 110 | /// Return true if the given point is inside the box, false otherwise. |
| 111 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool intersects (const V& point) const IMATH_NOEXCEPT; |
| 112 | |
| 113 | /// Return true if the given box is inside the box, false otherwise. |
| 114 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool intersects (const Box<V>& box) const IMATH_NOEXCEPT; |
| 115 | |
| 116 | /// Return the major axis of the box. The major axis is the dimension with |
| 117 | /// the greatest difference between maximum and minimum. |
| 118 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 unsigned int majorAxis() const IMATH_NOEXCEPT; |
| 119 | |
| 120 | /// Return true if the box is empty, false otherwise. An empty box's |
| 121 | /// minimum is greater than its maximum. |
| 122 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool isEmpty() const IMATH_NOEXCEPT; |
| 123 | |
| 124 | /// Return true if the box is larger than a single point, false otherwise. |
| 125 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool hasVolume() const IMATH_NOEXCEPT; |
| 126 | |
| 127 | /// Return true if the box contains all points, false otherwise. |
| 128 | /// An infinite box has a mimimum of`V::baseTypeLowest()` |
| 129 | /// and a maximum of `V::baseTypeMax()`. |
| 130 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool isInfinite() const IMATH_NOEXCEPT; |
| 131 | |
| 132 | /// @} |
| 133 | }; |
| 134 | |
| 135 | //-------------------- |
| 136 | // Convenient typedefs |
| 137 | //-------------------- |
| 138 | |
| 139 | /// 2D box of base type `short`. |
| 140 | typedef Box<V2s> Box2s; |
| 141 | |
| 142 | /// 2D box of base type `int`. |
| 143 | typedef Box<V2i> Box2i; |
| 144 | |
| 145 | /// 2D box of base type `int64_t`. |
| 146 | typedef Box<V2i64> Box2i64; |
| 147 | |
| 148 | /// 2D box of base type `float`. |
| 149 | typedef Box<V2f> Box2f; |
| 150 | |
| 151 | /// 2D box of base type `double`. |
| 152 | typedef Box<V2d> Box2d; |
| 153 | |
| 154 | /// 3D box of base type `short`. |
| 155 | typedef Box<V3s> Box3s; |
| 156 | |
| 157 | /// 3D box of base type `int`. |
| 158 | typedef Box<V3i> Box3i; |
| 159 | |
| 160 | /// 3D box of base type `int64_t`. |
| 161 | typedef Box<V3i64> Box3i64; |
| 162 | |
| 163 | /// 3D box of base type `float`. |
| 164 | typedef Box<V3f> Box3f; |
| 165 | |
| 166 | /// 3D box of base type `double`. |
| 167 | typedef Box<V3d> Box3d; |
| 168 | |
| 169 | template <class V> |
| 170 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Box<V>::Box() IMATH_NOEXCEPT |
| 171 | { |
| 172 | makeEmpty(); |
| 173 | } |
| 174 | |
| 175 | template <class V> |
| 176 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Box<V>::Box (const V& point) IMATH_NOEXCEPT |
| 177 | { |
| 178 | min = point; |
| 179 | max = point; |
| 180 | } |
| 181 | |
| 182 | template <class V> |
| 183 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Box<V>::Box (const V& minV, const V& maxV) IMATH_NOEXCEPT |
| 184 | { |
| 185 | min = minV; |
| 186 | max = maxV; |
| 187 | } |
| 188 | |
| 189 | template <class V> |
| 190 | IMATH_HOSTDEVICE constexpr inline bool |
| 191 | Box<V>::operator== (const Box<V>& src) const IMATH_NOEXCEPT |
| 192 | { |
| 193 | return (min == src.min && max == src.max); |
| 194 | } |
| 195 | |
| 196 | template <class V> |
| 197 | IMATH_HOSTDEVICE constexpr inline bool |
| 198 | Box<V>::operator!= (const Box<V>& src) const IMATH_NOEXCEPT |
| 199 | { |
| 200 | return (min != src.min || max != src.max); |
| 201 | } |
| 202 | |
| 203 | template <class V> |
| 204 | IMATH_HOSTDEVICE inline void |
| 205 | Box<V>::makeEmpty() IMATH_NOEXCEPT |
| 206 | { |
| 207 | min = V (V::baseTypeMax()); |
| 208 | max = V (V::baseTypeLowest()); |
| 209 | } |
| 210 | |
| 211 | template <class V> |
| 212 | IMATH_HOSTDEVICE inline void |
| 213 | Box<V>::makeInfinite() IMATH_NOEXCEPT |
| 214 | { |
| 215 | min = V (V::baseTypeLowest()); |
| 216 | max = V (V::baseTypeMax()); |
| 217 | } |
| 218 | |
| 219 | template <class V> |
| 220 | IMATH_HOSTDEVICE inline void |
| 221 | Box<V>::extendBy (const V& point) IMATH_NOEXCEPT |
| 222 | { |
| 223 | for (unsigned int i = 0; i < min.dimensions(); i++) |
| 224 | { |
| 225 | if (point[i] < min[i]) |
| 226 | min[i] = point[i]; |
| 227 | |
| 228 | if (point[i] > max[i]) |
| 229 | max[i] = point[i]; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | template <class V> |
| 234 | IMATH_HOSTDEVICE inline void |
| 235 | Box<V>::extendBy (const Box<V>& box) IMATH_NOEXCEPT |
| 236 | { |
| 237 | for (unsigned int i = 0; i < min.dimensions(); i++) |
| 238 | { |
| 239 | if (box.min[i] < min[i]) |
| 240 | min[i] = box.min[i]; |
| 241 | |
| 242 | if (box.max[i] > max[i]) |
| 243 | max[i] = box.max[i]; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | template <class V> |
| 248 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool |
| 249 | Box<V>::intersects (const V& point) const IMATH_NOEXCEPT |
| 250 | { |
| 251 | for (unsigned int i = 0; i < min.dimensions(); i++) |
| 252 | { |
| 253 | if (point[i] < min[i] || point[i] > max[i]) |
| 254 | return false; |
| 255 | } |
| 256 | |
| 257 | return true; |
| 258 | } |
| 259 | |
| 260 | template <class V> |
| 261 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool |
| 262 | Box<V>::intersects (const Box<V>& box) const IMATH_NOEXCEPT |
| 263 | { |
| 264 | for (unsigned int i = 0; i < min.dimensions(); i++) |
| 265 | { |
| 266 | if (box.max[i] < min[i] || box.min[i] > max[i]) |
| 267 | return false; |
| 268 | } |
| 269 | |
| 270 | return true; |
| 271 | } |
| 272 | |
| 273 | template <class V> |
| 274 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline V |
| 275 | Box<V>::size() const IMATH_NOEXCEPT |
| 276 | { |
| 277 | if (isEmpty()) |
| 278 | return V (0); |
| 279 | |
| 280 | return max - min; |
| 281 | } |
| 282 | |
| 283 | template <class V> |
| 284 | IMATH_HOSTDEVICE constexpr inline V |
| 285 | Box<V>::center() const IMATH_NOEXCEPT |
| 286 | { |
| 287 | return (max + min) / 2; |
| 288 | } |
| 289 | |
| 290 | template <class V> |
| 291 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool |
| 292 | Box<V>::isEmpty() const IMATH_NOEXCEPT |
| 293 | { |
| 294 | for (unsigned int i = 0; i < min.dimensions(); i++) |
| 295 | { |
| 296 | if (max[i] < min[i]) |
| 297 | return true; |
| 298 | } |
| 299 | |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | template <class V> |
| 304 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool |
| 305 | Box<V>::isInfinite() const IMATH_NOEXCEPT |
| 306 | { |
| 307 | for (unsigned int i = 0; i < min.dimensions(); i++) |
| 308 | { |
| 309 | if (min[i] != V::baseTypeLowest() || max[i] != V::baseTypeMax()) |
| 310 | return false; |
| 311 | } |
| 312 | |
| 313 | return true; |
| 314 | } |
| 315 | |
| 316 | template <class V> |
| 317 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool |
| 318 | Box<V>::hasVolume() const IMATH_NOEXCEPT |
| 319 | { |
| 320 | for (unsigned int i = 0; i < min.dimensions(); i++) |
| 321 | { |
| 322 | if (max[i] <= min[i]) |
| 323 | return false; |
| 324 | } |
| 325 | |
| 326 | return true; |
| 327 | } |
| 328 | |
| 329 | template <class V> |
| 330 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline unsigned int |
| 331 | Box<V>::majorAxis() const IMATH_NOEXCEPT |
| 332 | { |
| 333 | unsigned int major = 0; |
| 334 | V s = size(); |
| 335 | |
| 336 | for (unsigned int i = 1; i < min.dimensions(); i++) |
| 337 | { |
| 338 | if (s[i] > s[major]) |
| 339 | major = i; |
| 340 | } |
| 341 | |
| 342 | return major; |
| 343 | } |
| 344 | |
| 345 | //------------------------------------------------------------------- |
| 346 | // |
| 347 | // Partial class specializations for Imath::Vec2<T> and Imath::Vec3<T> |
| 348 | // |
| 349 | //------------------------------------------------------------------- |
| 350 | |
| 351 | template <typename V> class Box; |
| 352 | |
| 353 | /// |
| 354 | /// The Box<Vec2<T>> template represents a 2D bounding box defined by |
| 355 | /// minimum and maximum values of type Vec2<T>. The min and max members are |
| 356 | /// public. |
| 357 | /// |
| 358 | |
| 359 | template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Box<Vec2<T>> |
| 360 | { |
| 361 | public: |
| 362 | |
| 363 | /// @{ |
| 364 | /// @name Direct access to bounds |
| 365 | |
| 366 | /// The minimum value of the box. |
| 367 | Vec2<T> min; |
| 368 | |
| 369 | /// The maximum value of the box. |
| 370 | Vec2<T> max; |
| 371 | |
| 372 | /// @} |
| 373 | |
| 374 | /// @{ |
| 375 | /// @name Constructors and Assignment |
| 376 | |
| 377 | /// Empty by default |
| 378 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Box() IMATH_NOEXCEPT; |
| 379 | |
| 380 | /// Construct a bounding box that contains a single point. |
| 381 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Box (const Vec2<T>& point) IMATH_NOEXCEPT; |
| 382 | |
| 383 | /// Construct a bounding box with the given minimum and maximum points |
| 384 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Box (const Vec2<T>& minT, const Vec2<T>& maxT) IMATH_NOEXCEPT; |
| 385 | |
| 386 | /// @} |
| 387 | |
| 388 | /// @{ |
| 389 | /// @name Comparison |
| 390 | |
| 391 | /// Equality |
| 392 | IMATH_HOSTDEVICE constexpr bool operator== (const Box<Vec2<T>>& src) const IMATH_NOEXCEPT; |
| 393 | |
| 394 | /// Inequality |
| 395 | IMATH_HOSTDEVICE constexpr bool operator!= (const Box<Vec2<T>>& src) const IMATH_NOEXCEPT; |
| 396 | |
| 397 | /// @} |
| 398 | |
| 399 | /// @{ |
| 400 | /// @name Manipulation |
| 401 | |
| 402 | /// Set the Box to be empty. A Box is empty if the mimimum is |
| 403 | /// greater than the maximum. makeEmpty() sets the mimimum to |
| 404 | /// std::numeric_limits<T>::max() and the maximum to |
| 405 | /// std::numeric_limits<T>::lowest(). |
| 406 | IMATH_HOSTDEVICE void makeEmpty() IMATH_NOEXCEPT; |
| 407 | |
| 408 | /// Extend the Box to include the given point. |
| 409 | IMATH_HOSTDEVICE void extendBy (const Vec2<T>& point) IMATH_NOEXCEPT; |
| 410 | |
| 411 | /// Extend the Box to include the given box. |
| 412 | IMATH_HOSTDEVICE void extendBy (const Box<Vec2<T>>& box) IMATH_NOEXCEPT; |
| 413 | |
| 414 | /// Make the box include the entire range of T. |
| 415 | IMATH_HOSTDEVICE void makeInfinite() IMATH_NOEXCEPT; |
| 416 | |
| 417 | /// @} |
| 418 | |
| 419 | /// @{ |
| 420 | /// @name Query |
| 421 | |
| 422 | /// Return the size of the box. The size is of type `V`, defined as |
| 423 | /// `(max-min)`. An empty box has a size of `V(0)`, i.e. 0 in each dimension. |
| 424 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Vec2<T> size() const IMATH_NOEXCEPT; |
| 425 | |
| 426 | /// Return the center of the box. The center is defined as |
| 427 | /// `(max+min)/2`. The center of an empty box is undefined. |
| 428 | IMATH_HOSTDEVICE constexpr Vec2<T> center() const IMATH_NOEXCEPT; |
| 429 | |
| 430 | /// Return true if the given point is inside the box, false otherwise. |
| 431 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool intersects (const Vec2<T>& point) const IMATH_NOEXCEPT; |
| 432 | |
| 433 | /// Return true if the given box is inside the box, false otherwise. |
| 434 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool intersects (const Box<Vec2<T>>& box) const IMATH_NOEXCEPT; |
| 435 | |
| 436 | /// Return the major axis of the box. The major axis is the dimension with |
| 437 | /// the greatest difference between maximum and minimum. |
| 438 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 unsigned int majorAxis() const IMATH_NOEXCEPT; |
| 439 | |
| 440 | /// Return true if the box is empty, false otherwise. An empty box's |
| 441 | /// minimum is greater than its maximum. |
| 442 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool isEmpty() const IMATH_NOEXCEPT; |
| 443 | |
| 444 | /// Return true if the box is larger than a single point, false otherwise. |
| 445 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool hasVolume() const IMATH_NOEXCEPT; |
| 446 | |
| 447 | /// Return true if the box contains all points, false otherwise. |
| 448 | /// An infinite box has a mimimum of `V::baseTypeMin()` |
| 449 | /// and a maximum of `V::baseTypeMax()`. |
| 450 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool isInfinite() const IMATH_NOEXCEPT; |
| 451 | |
| 452 | /// @} |
| 453 | }; |
| 454 | |
| 455 | //---------------- |
| 456 | // Implementation |
| 457 | //---------------- |
| 458 | |
| 459 | template <class T> IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Box<Vec2<T>>::Box() IMATH_NOEXCEPT |
| 460 | { |
| 461 | makeEmpty(); |
| 462 | } |
| 463 | |
| 464 | template <class T> IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Box<Vec2<T>>::Box (const Vec2<T>& point) IMATH_NOEXCEPT |
| 465 | { |
| 466 | min = point; |
| 467 | max = point; |
| 468 | } |
| 469 | |
| 470 | template <class T> |
| 471 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Box<Vec2<T>>::Box (const Vec2<T>& minT, const Vec2<T>& maxT) IMATH_NOEXCEPT |
| 472 | { |
| 473 | min = minT; |
| 474 | max = maxT; |
| 475 | } |
| 476 | |
| 477 | template <class T> |
| 478 | IMATH_HOSTDEVICE constexpr inline bool |
| 479 | Box<Vec2<T>>::operator== (const Box<Vec2<T>>& src) const IMATH_NOEXCEPT |
| 480 | { |
| 481 | return (min == src.min && max == src.max); |
| 482 | } |
| 483 | |
| 484 | template <class T> |
| 485 | IMATH_HOSTDEVICE constexpr inline bool |
| 486 | Box<Vec2<T>>::operator!= (const Box<Vec2<T>>& src) const IMATH_NOEXCEPT |
| 487 | { |
| 488 | return (min != src.min || max != src.max); |
| 489 | } |
| 490 | |
| 491 | template <class T> |
| 492 | IMATH_HOSTDEVICE inline void |
| 493 | Box<Vec2<T>>::makeEmpty() IMATH_NOEXCEPT |
| 494 | { |
| 495 | min = Vec2<T> (Vec2<T>::baseTypeMax()); |
| 496 | max = Vec2<T> (Vec2<T>::baseTypeLowest()); |
| 497 | } |
| 498 | |
| 499 | template <class T> |
| 500 | IMATH_HOSTDEVICE inline void |
| 501 | Box<Vec2<T>>::makeInfinite() IMATH_NOEXCEPT |
| 502 | { |
| 503 | min = Vec2<T> (Vec2<T>::baseTypeLowest()); |
| 504 | max = Vec2<T> (Vec2<T>::baseTypeMax()); |
| 505 | } |
| 506 | |
| 507 | template <class T> |
| 508 | IMATH_HOSTDEVICE inline void |
| 509 | Box<Vec2<T>>::extendBy (const Vec2<T>& point) IMATH_NOEXCEPT |
| 510 | { |
| 511 | if (point[0] < min[0]) |
| 512 | min[0] = point[0]; |
| 513 | |
| 514 | if (point[0] > max[0]) |
| 515 | max[0] = point[0]; |
| 516 | |
| 517 | if (point[1] < min[1]) |
| 518 | min[1] = point[1]; |
| 519 | |
| 520 | if (point[1] > max[1]) |
| 521 | max[1] = point[1]; |
| 522 | } |
| 523 | |
| 524 | template <class T> |
| 525 | IMATH_HOSTDEVICE inline void |
| 526 | Box<Vec2<T>>::extendBy (const Box<Vec2<T>>& box) IMATH_NOEXCEPT |
| 527 | { |
| 528 | if (box.min[0] < min[0]) |
| 529 | min[0] = box.min[0]; |
| 530 | |
| 531 | if (box.max[0] > max[0]) |
| 532 | max[0] = box.max[0]; |
| 533 | |
| 534 | if (box.min[1] < min[1]) |
| 535 | min[1] = box.min[1]; |
| 536 | |
| 537 | if (box.max[1] > max[1]) |
| 538 | max[1] = box.max[1]; |
| 539 | } |
| 540 | |
| 541 | template <class T> |
| 542 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool |
| 543 | Box<Vec2<T>>::intersects (const Vec2<T>& point) const IMATH_NOEXCEPT |
| 544 | { |
| 545 | if (point[0] < min[0] || point[0] > max[0] || point[1] < min[1] || point[1] > max[1]) |
| 546 | return false; |
| 547 | |
| 548 | return true; |
| 549 | } |
| 550 | |
| 551 | template <class T> |
| 552 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool |
| 553 | Box<Vec2<T>>::intersects (const Box<Vec2<T>>& box) const IMATH_NOEXCEPT |
| 554 | { |
| 555 | if (box.max[0] < min[0] || box.min[0] > max[0] || box.max[1] < min[1] || box.min[1] > max[1]) |
| 556 | return false; |
| 557 | |
| 558 | return true; |
| 559 | } |
| 560 | |
| 561 | template <class T> |
| 562 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Vec2<T> |
| 563 | Box<Vec2<T>>::size() const IMATH_NOEXCEPT |
| 564 | { |
| 565 | if (isEmpty()) |
| 566 | return Vec2<T> (0); |
| 567 | |
| 568 | return max - min; |
| 569 | } |
| 570 | |
| 571 | template <class T> |
| 572 | IMATH_HOSTDEVICE constexpr inline Vec2<T> |
| 573 | Box<Vec2<T>>::center() const IMATH_NOEXCEPT |
| 574 | { |
| 575 | return (max + min) / 2; |
| 576 | } |
| 577 | |
| 578 | template <class T> |
| 579 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool |
| 580 | Box<Vec2<T>>::isEmpty() const IMATH_NOEXCEPT |
| 581 | { |
| 582 | if (max[0] < min[0] || max[1] < min[1]) |
| 583 | return true; |
| 584 | |
| 585 | return false; |
| 586 | } |
| 587 | |
| 588 | template <class T> |
| 589 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool |
| 590 | Box<Vec2<T>>::isInfinite() const IMATH_NOEXCEPT |
| 591 | { |
| 592 | if (min[0] != std::numeric_limits<T>::lowest() || |
| 593 | max[0] != std::numeric_limits<T>::max() || |
| 594 | min[1] != std::numeric_limits<T>::lowest() || |
| 595 | max[1] != std::numeric_limits<T>::max()) |
| 596 | return false; |
| 597 | |
| 598 | return true; |
| 599 | } |
| 600 | |
| 601 | template <class T> |
| 602 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool |
| 603 | Box<Vec2<T>>::hasVolume() const IMATH_NOEXCEPT |
| 604 | { |
| 605 | if (max[0] <= min[0] || max[1] <= min[1]) |
| 606 | return false; |
| 607 | |
| 608 | return true; |
| 609 | } |
| 610 | |
| 611 | template <class T> |
| 612 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline unsigned int |
| 613 | Box<Vec2<T>>::majorAxis() const IMATH_NOEXCEPT |
| 614 | { |
| 615 | unsigned int major = 0; |
| 616 | Vec2<T> s = size(); |
| 617 | |
| 618 | if (s[1] > s[major]) |
| 619 | major = 1; |
| 620 | |
| 621 | return major; |
| 622 | } |
| 623 | |
| 624 | /// |
| 625 | /// The Box<Vec3> template represents a 3D bounding box defined by |
| 626 | /// minimum and maximum values of type Vec3. |
| 627 | /// |
| 628 | template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Box<Vec3<T>> |
| 629 | { |
| 630 | public: |
| 631 | |
| 632 | /// @{ |
| 633 | /// @name Direct access to bounds |
| 634 | |
| 635 | /// The minimum value of the box. |
| 636 | Vec3<T> min; |
| 637 | |
| 638 | /// The maximum value of the box. |
| 639 | Vec3<T> max; |
| 640 | |
| 641 | /// @} |
| 642 | |
| 643 | /// @{ |
| 644 | /// @name Constructors |
| 645 | |
| 646 | /// Empty by default |
| 647 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Box() IMATH_NOEXCEPT; |
| 648 | |
| 649 | /// Construct a bounding box that contains a single point. |
| 650 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Box (const Vec3<T>& point) IMATH_NOEXCEPT; |
| 651 | |
| 652 | /// Construct a bounding box with the given minimum and maximum points |
| 653 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Box (const Vec3<T>& minT, const Vec3<T>& maxT) IMATH_NOEXCEPT; |
| 654 | |
| 655 | /// @} |
| 656 | |
| 657 | /// Equality |
| 658 | IMATH_HOSTDEVICE constexpr bool operator== (const Box<Vec3<T>>& src) const IMATH_NOEXCEPT; |
| 659 | |
| 660 | /// Inequality |
| 661 | IMATH_HOSTDEVICE constexpr bool operator!= (const Box<Vec3<T>>& src) const IMATH_NOEXCEPT; |
| 662 | |
| 663 | /// Set the Box to be empty. A Box is empty if the mimimum is |
| 664 | /// greater than the maximum. makeEmpty() sets the mimimum to |
| 665 | /// std::numeric_limits<T>::max() and the maximum to |
| 666 | /// std::numeric_limits<T>::lowest(). |
| 667 | IMATH_HOSTDEVICE void makeEmpty() IMATH_NOEXCEPT; |
| 668 | |
| 669 | /// Extend the Box to include the given point. |
| 670 | IMATH_HOSTDEVICE void extendBy (const Vec3<T>& point) IMATH_NOEXCEPT; |
| 671 | /// Extend the Box to include the given box. |
| 672 | |
| 673 | IMATH_HOSTDEVICE void extendBy (const Box<Vec3<T>>& box) IMATH_NOEXCEPT; |
| 674 | |
| 675 | /// Make the box include the entire range of T. |
| 676 | IMATH_HOSTDEVICE void makeInfinite() IMATH_NOEXCEPT; |
| 677 | |
| 678 | /// Return the size of the box. The size is of type `V`, defined as |
| 679 | /// (max-min). An empty box has a size of V(0), i.e. 0 in each dimension. |
| 680 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Vec3<T> size() const IMATH_NOEXCEPT; |
| 681 | |
| 682 | /// Return the center of the box. The center is defined as |
| 683 | /// (max+min)/2. The center of an empty box is undefined. |
| 684 | IMATH_HOSTDEVICE constexpr Vec3<T> center() const IMATH_NOEXCEPT; |
| 685 | |
| 686 | /// Return true if the given point is inside the box, false otherwise. |
| 687 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool intersects (const Vec3<T>& point) const IMATH_NOEXCEPT; |
| 688 | |
| 689 | /// Return true if the given box is inside the box, false otherwise. |
| 690 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool intersects (const Box<Vec3<T>>& box) const IMATH_NOEXCEPT; |
| 691 | |
| 692 | /// Return the major axis of the box. The major axis is the dimension with |
| 693 | /// the greatest difference between maximum and minimum. |
| 694 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 unsigned int majorAxis() const IMATH_NOEXCEPT; |
| 695 | |
| 696 | /// Return true if the box is empty, false otherwise. An empty box's |
| 697 | /// minimum is greater than its maximum. |
| 698 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool isEmpty() const IMATH_NOEXCEPT; |
| 699 | |
| 700 | /// Return true if the box is larger than a single point, false otherwise. |
| 701 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool hasVolume() const IMATH_NOEXCEPT; |
| 702 | |
| 703 | /// Return true if the box contains all points, false otherwise. |
| 704 | /// An infinite box has a mimimum of`V::baseTypeMin()` |
| 705 | /// and a maximum of `V::baseTypeMax()`. |
| 706 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool isInfinite() const IMATH_NOEXCEPT; |
| 707 | }; |
| 708 | |
| 709 | //---------------- |
| 710 | // Implementation |
| 711 | //---------------- |
| 712 | |
| 713 | template <class T> IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Box<Vec3<T>>::Box() IMATH_NOEXCEPT |
| 714 | { |
| 715 | makeEmpty(); |
| 716 | } |
| 717 | |
| 718 | template <class T> IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Box<Vec3<T>>::Box (const Vec3<T>& point) IMATH_NOEXCEPT |
| 719 | { |
| 720 | min = point; |
| 721 | max = point; |
| 722 | } |
| 723 | |
| 724 | template <class T> |
| 725 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Box<Vec3<T>>::Box (const Vec3<T>& minT, const Vec3<T>& maxT) IMATH_NOEXCEPT |
| 726 | { |
| 727 | min = minT; |
| 728 | max = maxT; |
| 729 | } |
| 730 | |
| 731 | template <class T> |
| 732 | IMATH_HOSTDEVICE constexpr inline bool |
| 733 | Box<Vec3<T>>::operator== (const Box<Vec3<T>>& src) const IMATH_NOEXCEPT |
| 734 | { |
| 735 | return (min == src.min && max == src.max); |
| 736 | } |
| 737 | |
| 738 | template <class T> |
| 739 | IMATH_HOSTDEVICE constexpr inline bool |
| 740 | Box<Vec3<T>>::operator!= (const Box<Vec3<T>>& src) const IMATH_NOEXCEPT |
| 741 | { |
| 742 | return (min != src.min || max != src.max); |
| 743 | } |
| 744 | |
| 745 | template <class T> |
| 746 | IMATH_HOSTDEVICE inline void |
| 747 | Box<Vec3<T>>::makeEmpty() IMATH_NOEXCEPT |
| 748 | { |
| 749 | min = Vec3<T> (Vec3<T>::baseTypeMax()); |
| 750 | max = Vec3<T> (Vec3<T>::baseTypeLowest()); |
| 751 | } |
| 752 | |
| 753 | template <class T> |
| 754 | IMATH_HOSTDEVICE inline void |
| 755 | Box<Vec3<T>>::makeInfinite() IMATH_NOEXCEPT |
| 756 | { |
| 757 | min = Vec3<T> (Vec3<T>::baseTypeLowest()); |
| 758 | max = Vec3<T> (Vec3<T>::baseTypeMax()); |
| 759 | } |
| 760 | |
| 761 | template <class T> |
| 762 | IMATH_HOSTDEVICE inline void |
| 763 | Box<Vec3<T>>::extendBy (const Vec3<T>& point) IMATH_NOEXCEPT |
| 764 | { |
| 765 | if (point[0] < min[0]) |
| 766 | min[0] = point[0]; |
| 767 | |
| 768 | if (point[0] > max[0]) |
| 769 | max[0] = point[0]; |
| 770 | |
| 771 | if (point[1] < min[1]) |
| 772 | min[1] = point[1]; |
| 773 | |
| 774 | if (point[1] > max[1]) |
| 775 | max[1] = point[1]; |
| 776 | |
| 777 | if (point[2] < min[2]) |
| 778 | min[2] = point[2]; |
| 779 | |
| 780 | if (point[2] > max[2]) |
| 781 | max[2] = point[2]; |
| 782 | } |
| 783 | |
| 784 | template <class T> |
| 785 | IMATH_HOSTDEVICE inline void |
| 786 | Box<Vec3<T>>::extendBy (const Box<Vec3<T>>& box) IMATH_NOEXCEPT |
| 787 | { |
| 788 | if (box.min[0] < min[0]) |
| 789 | min[0] = box.min[0]; |
| 790 | |
| 791 | if (box.max[0] > max[0]) |
| 792 | max[0] = box.max[0]; |
| 793 | |
| 794 | if (box.min[1] < min[1]) |
| 795 | min[1] = box.min[1]; |
| 796 | |
| 797 | if (box.max[1] > max[1]) |
| 798 | max[1] = box.max[1]; |
| 799 | |
| 800 | if (box.min[2] < min[2]) |
| 801 | min[2] = box.min[2]; |
| 802 | |
| 803 | if (box.max[2] > max[2]) |
| 804 | max[2] = box.max[2]; |
| 805 | } |
| 806 | |
| 807 | template <class T> |
| 808 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool |
| 809 | Box<Vec3<T>>::intersects (const Vec3<T>& point) const IMATH_NOEXCEPT |
| 810 | { |
| 811 | if (point[0] < min[0] || point[0] > max[0] || point[1] < min[1] || point[1] > max[1] || |
| 812 | point[2] < min[2] || point[2] > max[2]) |
| 813 | return false; |
| 814 | |
| 815 | return true; |
| 816 | } |
| 817 | |
| 818 | template <class T> |
| 819 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool |
| 820 | Box<Vec3<T>>::intersects (const Box<Vec3<T>>& box) const IMATH_NOEXCEPT |
| 821 | { |
| 822 | if (box.max[0] < min[0] || box.min[0] > max[0] || box.max[1] < min[1] || box.min[1] > max[1] || |
| 823 | box.max[2] < min[2] || box.min[2] > max[2]) |
| 824 | return false; |
| 825 | |
| 826 | return true; |
| 827 | } |
| 828 | |
| 829 | template <class T> |
| 830 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Vec3<T> |
| 831 | Box<Vec3<T>>::size() const IMATH_NOEXCEPT |
| 832 | { |
| 833 | if (isEmpty()) |
| 834 | return Vec3<T> (0); |
| 835 | |
| 836 | return max - min; |
| 837 | } |
| 838 | |
| 839 | template <class T> |
| 840 | IMATH_HOSTDEVICE constexpr inline Vec3<T> |
| 841 | Box<Vec3<T>>::center() const IMATH_NOEXCEPT |
| 842 | { |
| 843 | return (max + min) / 2; |
| 844 | } |
| 845 | |
| 846 | template <class T> |
| 847 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool |
| 848 | Box<Vec3<T>>::isEmpty() const IMATH_NOEXCEPT |
| 849 | { |
| 850 | if (max[0] < min[0] || max[1] < min[1] || max[2] < min[2]) |
| 851 | return true; |
| 852 | |
| 853 | return false; |
| 854 | } |
| 855 | |
| 856 | template <class T> |
| 857 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool |
| 858 | Box<Vec3<T>>::isInfinite() const IMATH_NOEXCEPT |
| 859 | { |
| 860 | if (min[0] != std::numeric_limits<T>::lowest() || |
| 861 | max[0] != std::numeric_limits<T>::max() || |
| 862 | min[1] != std::numeric_limits<T>::lowest() || |
| 863 | max[1] != std::numeric_limits<T>::max() || |
| 864 | min[2] != std::numeric_limits<T>::lowest() || |
| 865 | max[2] != std::numeric_limits<T>::max()) |
| 866 | return false; |
| 867 | |
| 868 | return true; |
| 869 | } |
| 870 | |
| 871 | template <class T> |
| 872 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool |
| 873 | Box<Vec3<T>>::hasVolume() const IMATH_NOEXCEPT |
| 874 | { |
| 875 | if (max[0] <= min[0] || max[1] <= min[1] || max[2] <= min[2]) |
| 876 | return false; |
| 877 | |
| 878 | return true; |
| 879 | } |
| 880 | |
| 881 | template <class T> |
| 882 | IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline unsigned int |
| 883 | Box<Vec3<T>>::majorAxis() const IMATH_NOEXCEPT |
| 884 | { |
| 885 | unsigned int major = 0; |
| 886 | Vec3<T> s = size(); |
| 887 | |
| 888 | if (s[1] > s[major]) |
| 889 | major = 1; |
| 890 | |
| 891 | if (s[2] > s[major]) |
| 892 | major = 2; |
| 893 | |
| 894 | return major; |
| 895 | } |
| 896 | |
| 897 | IMATH_INTERNAL_NAMESPACE_HEADER_EXIT |
| 898 | |
| 899 | #endif // INCLUDED_IMATHBOX_H |
| 900 | |