// Copyright 2009-2021 Intel Corporation // SPDX-License-Identifier: Apache-2.0 #pragma once #include "vec2.h" #include "vec3.h" namespace embree { namespace internal { template __forceinline T divideByTwo(const T& v) { return v / T(2); } template <> __forceinline float divideByTwo(const float& v) { return v * 0.5f; } template <> __forceinline double divideByTwo(const double& v) { return v * 0.5; } } // namespace internal template struct BBox { T lower, upper; //////////////////////////////////////////////////////////////////////////////// /// Construction //////////////////////////////////////////////////////////////////////////////// __forceinline BBox ( ) { } template __forceinline BBox ( const BBox& other ) : lower(other.lower), upper(other.upper) {} __forceinline BBox& operator=( const BBox& other ) { lower = other.lower; upper = other.upper; return *this; } __forceinline BBox ( const T& v ) : lower(v), upper(v) {} __forceinline BBox ( const T& lower, const T& upper ) : lower(lower), upper(upper) {} //////////////////////////////////////////////////////////////////////////////// /// Extending Bounds //////////////////////////////////////////////////////////////////////////////// __forceinline const BBox& extend(const BBox& other) { lower = min(lower,other.lower); upper = max(upper,other.upper); return *this; } __forceinline const BBox& extend(const T & other) { lower = min(lower,other ); upper = max(upper,other ); return *this; } /*! tests if box is empty */ __forceinline bool empty() const { for (int i=0; i upper[i]) return true; return false; } /*! computes the size of the box */ __forceinline T size() const { return upper - lower; } /*! computes the center of the box */ __forceinline T center() const { return internal::divideByTwo(lower+upper); } /*! computes twice the center of the box */ __forceinline T center2() const { return lower+upper; } /*! merges two boxes */ __forceinline static const BBox merge (const BBox& a, const BBox& b) { return BBox(min(a.lower, b.lower), max(a.upper, b.upper)); } /*! enlarge box by some scaling factor */ __forceinline BBox enlarge_by(const float a) const { return BBox(lower - T(a)*abs(lower), upper + T(a)*abs(upper)); } //////////////////////////////////////////////////////////////////////////////// /// Constants //////////////////////////////////////////////////////////////////////////////// __forceinline BBox( EmptyTy ) : lower(pos_inf), upper(neg_inf) {} __forceinline BBox( FullTy ) : lower(neg_inf), upper(pos_inf) {} __forceinline BBox( FalseTy ) : lower(pos_inf), upper(neg_inf) {} __forceinline BBox( TrueTy ) : lower(neg_inf), upper(pos_inf) {} __forceinline BBox( NegInfTy ): lower(pos_inf), upper(neg_inf) {} __forceinline BBox( PosInfTy ): lower(neg_inf), upper(pos_inf) {} }; template<> __forceinline bool BBox::empty() const { return lower > upper; } #if defined(__SSE__) template<> __forceinline bool BBox::empty() const { return !all(le_mask(lower,upper)); } template<> __forceinline bool BBox::empty() const { return !all(le_mask(lower,upper)); } #endif /*! tests if box is finite */ __forceinline bool isvalid( const BBox& v ) { return all(gt_mask(v.lower,Vec3fa_t(-FLT_LARGE)) & lt_mask(v.upper,Vec3fa_t(+FLT_LARGE))); } /*! tests if box is finite and non-empty*/ __forceinline bool isvalid_non_empty( const BBox& v ) { return all(gt_mask(v.lower,Vec3fa_t(-FLT_LARGE)) & lt_mask(v.upper,Vec3fa_t(+FLT_LARGE)) & le_mask(v.lower,v.upper)); } /*! tests if box has finite entries */ __forceinline bool is_finite( const BBox& b) { return is_finite(b.lower) && is_finite(b.upper); } /*! test if point contained in box */ __forceinline bool inside ( const BBox& b, const Vec3fa& p ) { return all(ge_mask(p,b.lower) & le_mask(p,b.upper)); } /*! computes the center of the box */ template __forceinline const T center2(const BBox& box) { return box.lower + box.upper; } template __forceinline const T center (const BBox& box) { return internal::divideByTwo(center2(box)); } /*! computes the volume of a bounding box */ __forceinline float volume ( const BBox& b ) { return reduce_mul(b.size()); } __forceinline float safeVolume( const BBox& b ) { if (b.empty()) return 0.0f; else return volume(b); } /*! computes the volume of a bounding box */ __forceinline float volume( const BBox& b ) { return reduce_mul(b.size()); } /*! computes the surface area of a bounding box */ template __forceinline const T area( const BBox >& b ) { const Vec2 d = b.size(); return d.x*d.y; } template __forceinline const T halfArea( const BBox >& b ) { return halfArea(b.size()); } template __forceinline const T area( const BBox >& b ) { return T(2)*halfArea(b); } __forceinline float halfArea( const BBox& b ) { return halfArea(b.size()); } __forceinline float area( const BBox& b ) { return 2.0f*halfArea(b); } __forceinline float halfArea( const BBox& b ) { return halfArea(b.size()); } __forceinline float area( const BBox& b ) { return 2.0f*halfArea(b); } template __forceinline float safeArea( const BBox& b ) { if (b.empty()) return 0.0f; else return area(b); } template __forceinline float expectedApproxHalfArea(const BBox& box) { return halfArea(box); } /*! merges bounding boxes and points */ template __forceinline const BBox merge( const BBox& a, const T& b ) { return BBox(min(a.lower, b ), max(a.upper, b )); } template __forceinline const BBox merge( const T& a, const BBox& b ) { return BBox(min(a , b.lower), max(a , b.upper)); } template __forceinline const BBox merge( const BBox& a, const BBox& b ) { return BBox(min(a.lower, b.lower), max(a.upper, b.upper)); } /*! Merges three boxes. */ template __forceinline const BBox merge( const BBox& a, const BBox& b, const BBox& c ) { return merge(a,merge(b,c)); } /*! Merges four boxes. */ template __forceinline BBox merge(const BBox& a, const BBox& b, const BBox& c, const BBox& d) { return merge(merge(a,b),merge(c,d)); } /*! Comparison Operators */ template __forceinline bool operator==( const BBox& a, const BBox& b ) { return a.lower == b.lower && a.upper == b.upper; } template __forceinline bool operator!=( const BBox& a, const BBox& b ) { return a.lower != b.lower || a.upper != b.upper; } /*! scaling */ template __forceinline BBox operator *( const float& a, const BBox& b ) { return BBox(a*b.lower,a*b.upper); } template __forceinline BBox operator *( const T& a, const BBox& b ) { return BBox(a*b.lower,a*b.upper); } /*! translations */ template __forceinline BBox operator +( const BBox& a, const BBox& b ) { return BBox(a.lower+b.lower,a.upper+b.upper); } template __forceinline BBox operator -( const BBox& a, const BBox& b ) { return BBox(a.lower-b.lower,a.upper-b.upper); } template __forceinline BBox operator +( const BBox& a, const T & b ) { return BBox(a.lower+b ,a.upper+b ); } template __forceinline BBox operator -( const BBox& a, const T & b ) { return BBox(a.lower-b ,a.upper-b ); } /*! extension */ template __forceinline BBox enlarge(const BBox& a, const T& b) { return BBox(a.lower-b, a.upper+b); } /*! intersect bounding boxes */ template __forceinline const BBox intersect( const BBox& a, const BBox& b ) { return BBox(max(a.lower, b.lower), min(a.upper, b.upper)); } template __forceinline const BBox intersect( const BBox& a, const BBox& b, const BBox& c ) { return intersect(a,intersect(b,c)); } template __forceinline const BBox intersect( const BBox& a, const BBox& b, const BBox& c, const BBox& d ) { return intersect(intersect(a,b),intersect(c,d)); } /*! subtract bounds from each other */ template __forceinline void subtract(const BBox& a, const BBox& b, BBox& c, BBox& d) { c.lower = a.lower; c.upper = min(a.upper,b.lower); d.lower = max(a.lower,b.upper); d.upper = a.upper; } /*! tests if bounding boxes (and points) are disjoint (empty intersection) */ template __inline bool disjoint( const BBox& a, const BBox& b ) { return intersect(a,b).empty(); } template __inline bool disjoint( const BBox& a, const T& b ) { return disjoint(a,BBox(b)); } template __inline bool disjoint( const T& a, const BBox& b ) { return disjoint(BBox(a),b); } /*! tests if bounding boxes (and points) are conjoint (non-empty intersection) */ template __inline bool conjoint( const BBox& a, const BBox& b ) { return !intersect(a,b).empty(); } template __inline bool conjoint( const BBox& a, const T& b ) { return conjoint(a,BBox(b)); } template __inline bool conjoint( const T& a, const BBox& b ) { return conjoint(BBox(a),b); } /*! subset relation */ template __inline bool subset( const BBox& a, const BBox& b ) { for ( size_t i = 0; i < T::N; i++ ) if ( a.lower[i] < b.lower[i] ) return false; for ( size_t i = 0; i < T::N; i++ ) if ( a.upper[i] > b.upper[i] ) return false; return true; } template<> __inline bool subset( const BBox& a, const BBox& b ) { return all(ge_mask(a.lower,b.lower)) & all(le_mask(a.upper,b.upper)); } template<> __inline bool subset( const BBox& a, const BBox& b ) { return all(ge_mask(a.lower,b.lower)) & all(le_mask(a.upper,b.upper)); } /*! blending */ template __forceinline BBox lerp(const BBox& b0, const BBox& b1, const float t) { return BBox(lerp(b0.lower,b1.lower,t),lerp(b0.upper,b1.upper,t)); } /*! output operator */ template __forceinline embree_ostream operator<<(embree_ostream cout, const BBox& box) { return cout << "[" << box.lower << "; " << box.upper << "]"; } /*! default template instantiations */ typedef BBox BBox1f; typedef BBox BBox2f; typedef BBox BBox2fa; typedef BBox BBox3f; typedef BBox BBox3fa; typedef BBox BBox3fx; typedef BBox BBox3ff; } //////////////////////////////////////////////////////////////////////////////// /// SSE / AVX / MIC specializations //////////////////////////////////////////////////////////////////////////////// #if defined __SSE__ #include "../simd/sse.h" #endif #if defined __AVX__ #include "../simd/avx.h" #endif #if defined(__AVX512F__) #include "../simd/avx512.h" #endif namespace embree { template __forceinline BBox>> transpose(const BBox3fa* bounds); template<> __forceinline BBox> transpose<4>(const BBox3fa* bounds) { BBox> dest; transpose((vfloat4&)bounds[0].lower, (vfloat4&)bounds[1].lower, (vfloat4&)bounds[2].lower, (vfloat4&)bounds[3].lower, dest.lower.x, dest.lower.y, dest.lower.z); transpose((vfloat4&)bounds[0].upper, (vfloat4&)bounds[1].upper, (vfloat4&)bounds[2].upper, (vfloat4&)bounds[3].upper, dest.upper.x, dest.upper.y, dest.upper.z); return dest; } #if defined(__AVX__) template<> __forceinline BBox> transpose<8>(const BBox3fa* bounds) { BBox> dest; transpose((vfloat4&)bounds[0].lower, (vfloat4&)bounds[1].lower, (vfloat4&)bounds[2].lower, (vfloat4&)bounds[3].lower, (vfloat4&)bounds[4].lower, (vfloat4&)bounds[5].lower, (vfloat4&)bounds[6].lower, (vfloat4&)bounds[7].lower, dest.lower.x, dest.lower.y, dest.lower.z); transpose((vfloat4&)bounds[0].upper, (vfloat4&)bounds[1].upper, (vfloat4&)bounds[2].upper, (vfloat4&)bounds[3].upper, (vfloat4&)bounds[4].upper, (vfloat4&)bounds[5].upper, (vfloat4&)bounds[6].upper, (vfloat4&)bounds[7].upper, dest.upper.x, dest.upper.y, dest.upper.z); return dest; } #endif template __forceinline BBox3fa merge(const BBox3fa* bounds); template<> __forceinline BBox3fa merge<4>(const BBox3fa* bounds) { const Vec3fa lower = min(min(bounds[0].lower,bounds[1].lower), min(bounds[2].lower,bounds[3].lower)); const Vec3fa upper = max(max(bounds[0].upper,bounds[1].upper), max(bounds[2].upper,bounds[3].upper)); return BBox3fa(lower,upper); } #if defined(__AVX__) template<> __forceinline BBox3fa merge<8>(const BBox3fa* bounds) { const Vec3fa lower = min(min(min(bounds[0].lower,bounds[1].lower),min(bounds[2].lower,bounds[3].lower)), min(min(bounds[4].lower,bounds[5].lower),min(bounds[6].lower,bounds[7].lower))); const Vec3fa upper = max(max(max(bounds[0].upper,bounds[1].upper),max(bounds[2].upper,bounds[3].upper)), max(max(bounds[4].upper,bounds[5].upper),max(bounds[6].upper,bounds[7].upper))); return BBox3fa(lower,upper); } #endif }