bbox.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "vec2.h"
  5. #include "vec3.h"
  6. namespace embree
  7. {
  8. namespace internal {
  9. template <typename T> __forceinline T divideByTwo(const T& v) { return v / T(2); }
  10. template <> __forceinline float divideByTwo<float>(const float& v) { return v * 0.5f; }
  11. template <> __forceinline double divideByTwo<double>(const double& v) { return v * 0.5; }
  12. } // namespace internal
  13. template<typename T>
  14. struct BBox
  15. {
  16. T lower, upper;
  17. ////////////////////////////////////////////////////////////////////////////////
  18. /// Construction
  19. ////////////////////////////////////////////////////////////////////////////////
  20. __forceinline BBox ( ) { }
  21. template<typename T1>
  22. __forceinline BBox ( const BBox<T1>& other ) : lower(other.lower), upper(other.upper) {}
  23. __forceinline BBox& operator=( const BBox& other ) { lower = other.lower; upper = other.upper; return *this; }
  24. __forceinline BBox ( const T& v ) : lower(v), upper(v) {}
  25. __forceinline BBox ( const T& lower, const T& upper ) : lower(lower), upper(upper) {}
  26. ////////////////////////////////////////////////////////////////////////////////
  27. /// Extending Bounds
  28. ////////////////////////////////////////////////////////////////////////////////
  29. __forceinline const BBox& extend(const BBox& other) { lower = min(lower,other.lower); upper = max(upper,other.upper); return *this; }
  30. __forceinline const BBox& extend(const T & other) { lower = min(lower,other ); upper = max(upper,other ); return *this; }
  31. /*! tests if box is empty */
  32. __forceinline bool empty() const { for (int i=0; i<T::N; i++) if (lower[i] > upper[i]) return true; return false; }
  33. /*! computes the size of the box */
  34. __forceinline T size() const { return upper - lower; }
  35. /*! computes the center of the box */
  36. __forceinline T center() const { return internal::divideByTwo<T>(lower+upper); }
  37. /*! computes twice the center of the box */
  38. __forceinline T center2() const { return lower+upper; }
  39. /*! merges two boxes */
  40. __forceinline static const BBox merge (const BBox& a, const BBox& b) {
  41. return BBox(min(a.lower, b.lower), max(a.upper, b.upper));
  42. }
  43. /*! enlarge box by some scaling factor */
  44. __forceinline BBox enlarge_by(const float a) const {
  45. return BBox(lower - T(a)*abs(lower), upper + T(a)*abs(upper));
  46. }
  47. ////////////////////////////////////////////////////////////////////////////////
  48. /// Constants
  49. ////////////////////////////////////////////////////////////////////////////////
  50. __forceinline BBox( EmptyTy ) : lower(pos_inf), upper(neg_inf) {}
  51. __forceinline BBox( FullTy ) : lower(neg_inf), upper(pos_inf) {}
  52. __forceinline BBox( FalseTy ) : lower(pos_inf), upper(neg_inf) {}
  53. __forceinline BBox( TrueTy ) : lower(neg_inf), upper(pos_inf) {}
  54. __forceinline BBox( NegInfTy ): lower(pos_inf), upper(neg_inf) {}
  55. __forceinline BBox( PosInfTy ): lower(neg_inf), upper(pos_inf) {}
  56. };
  57. template<> __forceinline bool BBox<float>::empty() const {
  58. return lower > upper;
  59. }
  60. #if defined(__SSE__) || defined(__ARM_NEON)
  61. template<> __forceinline bool BBox<Vec3fa>::empty() const {
  62. return !all(le_mask(lower,upper));
  63. }
  64. template<> __forceinline bool BBox<Vec3fx>::empty() const {
  65. return !all(le_mask(lower,upper));
  66. }
  67. #endif
  68. /*! tests if box is finite */
  69. __forceinline bool isvalid( const BBox<Vec3fa>& v ) {
  70. return all(gt_mask(v.lower,Vec3fa_t(-FLT_LARGE)) & lt_mask(v.upper,Vec3fa_t(+FLT_LARGE)));
  71. }
  72. /*! tests if box is finite and non-empty*/
  73. __forceinline bool isvalid_non_empty( const BBox<Vec3fa>& v ) {
  74. return all(gt_mask(v.lower,Vec3fa_t(-FLT_LARGE)) & lt_mask(v.upper,Vec3fa_t(+FLT_LARGE)) & le_mask(v.lower,v.upper));
  75. }
  76. /*! tests if box has finite entries */
  77. __forceinline bool is_finite( const BBox<Vec3fa>& b) {
  78. return is_finite(b.lower) && is_finite(b.upper);
  79. }
  80. /*! test if point contained in box */
  81. __forceinline bool inside ( const BBox<Vec3fa>& b, const Vec3fa& p ) { return all(ge_mask(p,b.lower) & le_mask(p,b.upper)); }
  82. /*! computes the center of the box */
  83. template<typename T> __forceinline const T center2(const BBox<T>& box) { return box.lower + box.upper; }
  84. template<typename T> __forceinline const T center (const BBox<T>& box) { return internal::divideByTwo<T>(center2(box)); }
  85. /*! computes the volume of a bounding box */
  86. __forceinline float volume ( const BBox<Vec3fa>& b ) { return reduce_mul(b.size()); }
  87. __forceinline float safeVolume( const BBox<Vec3fa>& b ) { if (b.empty()) return 0.0f; else return volume(b); }
  88. /*! computes the volume of a bounding box */
  89. __forceinline float volume( const BBox<Vec3f>& b ) { return reduce_mul(b.size()); }
  90. /*! computes the surface area of a bounding box */
  91. template<typename T> __forceinline const T area( const BBox<Vec2<T> >& b ) { const Vec2<T> d = b.size(); return d.x*d.y; }
  92. template<typename T> __forceinline const T halfArea( const BBox<Vec3<T> >& b ) { return halfArea(b.size()); }
  93. template<typename T> __forceinline const T area( const BBox<Vec3<T> >& b ) { return T(2)*halfArea(b); }
  94. __forceinline float halfArea( const BBox<Vec3fa>& b ) { return halfArea(b.size()); }
  95. __forceinline float area( const BBox<Vec3fa>& b ) { return 2.0f*halfArea(b); }
  96. __forceinline float halfArea( const BBox<Vec3fx>& b ) { return halfArea(b.size()); }
  97. __forceinline float area( const BBox<Vec3fx>& b ) { return 2.0f*halfArea(b); }
  98. template<typename Vec> __forceinline float safeArea( const BBox<Vec>& b ) { if (b.empty()) return 0.0f; else return area(b); }
  99. template<typename T> __forceinline float expectedApproxHalfArea(const BBox<T>& box) {
  100. return halfArea(box);
  101. }
  102. /*! merges bounding boxes and points */
  103. template<typename T> __forceinline const BBox<T> merge( const BBox<T>& a, const T& b ) { return BBox<T>(min(a.lower, b ), max(a.upper, b )); }
  104. template<typename T> __forceinline const BBox<T> merge( const T& a, const BBox<T>& b ) { return BBox<T>(min(a , b.lower), max(a , b.upper)); }
  105. template<typename T> __forceinline const BBox<T> merge( const BBox<T>& a, const BBox<T>& b ) { return BBox<T>(min(a.lower, b.lower), max(a.upper, b.upper)); }
  106. /*! Merges three boxes. */
  107. template<typename T> __forceinline const BBox<T> merge( const BBox<T>& a, const BBox<T>& b, const BBox<T>& c ) { return merge(a,merge(b,c)); }
  108. /*! Merges four boxes. */
  109. template<typename T> __forceinline BBox<T> merge(const BBox<T>& a, const BBox<T>& b, const BBox<T>& c, const BBox<T>& d) {
  110. return merge(merge(a,b),merge(c,d));
  111. }
  112. /*! Comparison Operators */
  113. template<typename T> __forceinline bool operator==( const BBox<T>& a, const BBox<T>& b ) { return a.lower == b.lower && a.upper == b.upper; }
  114. template<typename T> __forceinline bool operator!=( const BBox<T>& a, const BBox<T>& b ) { return a.lower != b.lower || a.upper != b.upper; }
  115. /*! scaling */
  116. template<typename T> __forceinline BBox<T> operator *( const float& a, const BBox<T>& b ) { return BBox<T>(a*b.lower,a*b.upper); }
  117. template<typename T> __forceinline BBox<T> operator *( const T& a, const BBox<T>& b ) { return BBox<T>(a*b.lower,a*b.upper); }
  118. /*! translations */
  119. template<typename T> __forceinline BBox<T> operator +( const BBox<T>& a, const BBox<T>& b ) { return BBox<T>(a.lower+b.lower,a.upper+b.upper); }
  120. template<typename T> __forceinline BBox<T> operator -( const BBox<T>& a, const BBox<T>& b ) { return BBox<T>(a.lower-b.lower,a.upper-b.upper); }
  121. template<typename T> __forceinline BBox<T> operator +( const BBox<T>& a, const T & b ) { return BBox<T>(a.lower+b ,a.upper+b ); }
  122. template<typename T> __forceinline BBox<T> operator -( const BBox<T>& a, const T & b ) { return BBox<T>(a.lower-b ,a.upper-b ); }
  123. /*! extension */
  124. template<typename T> __forceinline BBox<T> enlarge(const BBox<T>& a, const T& b) { return BBox<T>(a.lower-b, a.upper+b); }
  125. /*! intersect bounding boxes */
  126. template<typename T> __forceinline const BBox<T> intersect( const BBox<T>& a, const BBox<T>& b ) { return BBox<T>(max(a.lower, b.lower), min(a.upper, b.upper)); }
  127. template<typename T> __forceinline const BBox<T> intersect( const BBox<T>& a, const BBox<T>& b, const BBox<T>& c ) { return intersect(a,intersect(b,c)); }
  128. template<typename T> __forceinline const BBox<T> intersect( const BBox<T>& a, const BBox<T>& b, const BBox<T>& c, const BBox<T>& d ) { return intersect(intersect(a,b),intersect(c,d)); }
  129. /*! subtract bounds from each other */
  130. template<typename T> __forceinline void subtract(const BBox<T>& a, const BBox<T>& b, BBox<T>& c, BBox<T>& d)
  131. {
  132. c.lower = a.lower;
  133. c.upper = min(a.upper,b.lower);
  134. d.lower = max(a.lower,b.upper);
  135. d.upper = a.upper;
  136. }
  137. /*! tests if bounding boxes (and points) are disjoint (empty intersection) */
  138. template<typename T> __inline bool disjoint( const BBox<T>& a, const BBox<T>& b ) { return intersect(a,b).empty(); }
  139. template<typename T> __inline bool disjoint( const BBox<T>& a, const T& b ) { return disjoint(a,BBox<T>(b)); }
  140. template<typename T> __inline bool disjoint( const T& a, const BBox<T>& b ) { return disjoint(BBox<T>(a),b); }
  141. /*! tests if bounding boxes (and points) are conjoint (non-empty intersection) */
  142. template<typename T> __inline bool conjoint( const BBox<T>& a, const BBox<T>& b ) { return !intersect(a,b).empty(); }
  143. template<typename T> __inline bool conjoint( const BBox<T>& a, const T& b ) { return conjoint(a,BBox<T>(b)); }
  144. template<typename T> __inline bool conjoint( const T& a, const BBox<T>& b ) { return conjoint(BBox<T>(a),b); }
  145. /*! subset relation */
  146. template<typename T> __inline bool subset( const BBox<T>& a, const BBox<T>& b )
  147. {
  148. for ( size_t i = 0; i < T::N; i++ ) if ( a.lower[i] < b.lower[i] ) return false;
  149. for ( size_t i = 0; i < T::N; i++ ) if ( a.upper[i] > b.upper[i] ) return false;
  150. return true;
  151. }
  152. template<> __inline bool subset( const BBox<Vec3fa>& a, const BBox<Vec3fa>& b ) {
  153. return all(ge_mask(a.lower,b.lower)) && all(le_mask(a.upper,b.upper));
  154. }
  155. template<> __inline bool subset( const BBox<Vec3fx>& a, const BBox<Vec3fx>& b ) {
  156. return all(ge_mask(a.lower,b.lower)) && all(le_mask(a.upper,b.upper));
  157. }
  158. /*! blending */
  159. template<typename T>
  160. __forceinline BBox<T> lerp(const BBox<T>& b0, const BBox<T>& b1, const float t) {
  161. return BBox<T>(lerp(b0.lower,b1.lower,t),lerp(b0.upper,b1.upper,t));
  162. }
  163. /*! output operator */
  164. template<typename T> __forceinline embree_ostream operator<<(embree_ostream cout, const BBox<T>& box) {
  165. return cout << "[" << box.lower << "; " << box.upper << "]";
  166. }
  167. /*! default template instantiations */
  168. typedef BBox<float> BBox1f;
  169. typedef BBox<Vec2f> BBox2f;
  170. typedef BBox<Vec2fa> BBox2fa;
  171. typedef BBox<Vec3f> BBox3f;
  172. typedef BBox<Vec3fa> BBox3fa;
  173. typedef BBox<Vec3fx> BBox3fx;
  174. typedef BBox<Vec3ff> BBox3ff;
  175. }
  176. ////////////////////////////////////////////////////////////////////////////////
  177. /// SSE / AVX / MIC specializations
  178. ////////////////////////////////////////////////////////////////////////////////
  179. #if defined (__SSE__) || defined(__ARM_NEON)
  180. #include "../simd/sse.h"
  181. #endif
  182. #if defined (__AVX__)
  183. #include "../simd/avx.h"
  184. #endif
  185. #if defined(__AVX512F__)
  186. #include "../simd/avx512.h"
  187. #endif
  188. namespace embree
  189. {
  190. template<int N>
  191. __forceinline BBox<Vec3<vfloat<N>>> transpose(const BBox3fa* bounds);
  192. template<>
  193. __forceinline BBox<Vec3<vfloat4>> transpose<4>(const BBox3fa* bounds)
  194. {
  195. BBox<Vec3<vfloat4>> dest;
  196. transpose((vfloat4&)bounds[0].lower,
  197. (vfloat4&)bounds[1].lower,
  198. (vfloat4&)bounds[2].lower,
  199. (vfloat4&)bounds[3].lower,
  200. dest.lower.x,
  201. dest.lower.y,
  202. dest.lower.z);
  203. transpose((vfloat4&)bounds[0].upper,
  204. (vfloat4&)bounds[1].upper,
  205. (vfloat4&)bounds[2].upper,
  206. (vfloat4&)bounds[3].upper,
  207. dest.upper.x,
  208. dest.upper.y,
  209. dest.upper.z);
  210. return dest;
  211. }
  212. #if defined(__AVX__)
  213. template<>
  214. __forceinline BBox<Vec3<vfloat8>> transpose<8>(const BBox3fa* bounds)
  215. {
  216. BBox<Vec3<vfloat8>> dest;
  217. transpose((vfloat4&)bounds[0].lower,
  218. (vfloat4&)bounds[1].lower,
  219. (vfloat4&)bounds[2].lower,
  220. (vfloat4&)bounds[3].lower,
  221. (vfloat4&)bounds[4].lower,
  222. (vfloat4&)bounds[5].lower,
  223. (vfloat4&)bounds[6].lower,
  224. (vfloat4&)bounds[7].lower,
  225. dest.lower.x,
  226. dest.lower.y,
  227. dest.lower.z);
  228. transpose((vfloat4&)bounds[0].upper,
  229. (vfloat4&)bounds[1].upper,
  230. (vfloat4&)bounds[2].upper,
  231. (vfloat4&)bounds[3].upper,
  232. (vfloat4&)bounds[4].upper,
  233. (vfloat4&)bounds[5].upper,
  234. (vfloat4&)bounds[6].upper,
  235. (vfloat4&)bounds[7].upper,
  236. dest.upper.x,
  237. dest.upper.y,
  238. dest.upper.z);
  239. return dest;
  240. }
  241. #endif
  242. template<int N>
  243. __forceinline BBox3fa merge(const BBox3fa* bounds);
  244. template<>
  245. __forceinline BBox3fa merge<4>(const BBox3fa* bounds)
  246. {
  247. const Vec3fa lower = min(min(bounds[0].lower,bounds[1].lower),
  248. min(bounds[2].lower,bounds[3].lower));
  249. const Vec3fa upper = max(max(bounds[0].upper,bounds[1].upper),
  250. max(bounds[2].upper,bounds[3].upper));
  251. return BBox3fa(lower,upper);
  252. }
  253. #if defined(__AVX__)
  254. template<>
  255. __forceinline BBox3fa merge<8>(const BBox3fa* bounds)
  256. {
  257. const Vec3fa lower = min(min(min(bounds[0].lower,bounds[1].lower),min(bounds[2].lower,bounds[3].lower)),
  258. min(min(bounds[4].lower,bounds[5].lower),min(bounds[6].lower,bounds[7].lower)));
  259. const Vec3fa upper = max(max(max(bounds[0].upper,bounds[1].upper),max(bounds[2].upper,bounds[3].upper)),
  260. max(max(bounds[4].upper,bounds[5].upper),max(bounds[6].upper,bounds[7].upper)));
  261. return BBox3fa(lower,upper);
  262. }
  263. #endif
  264. }