lbbox.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // ======================================================================== //
  2. // Copyright 2009-2017 Intel Corporation //
  3. // //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); //
  5. // you may not use this file except in compliance with the License. //
  6. // You may obtain a copy of the License at //
  7. // //
  8. // http://www.apache.org/licenses/LICENSE-2.0 //
  9. // //
  10. // Unless required by applicable law or agreed to in writing, software //
  11. // distributed under the License is distributed on an "AS IS" BASIS, //
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
  13. // See the License for the specific language governing permissions and //
  14. // limitations under the License. //
  15. // ======================================================================== //
  16. #pragma once
  17. #include "bbox.h"
  18. namespace embree
  19. {
  20. template<typename T>
  21. __forceinline std::pair<T,T> globalLinear(const std::pair<T,T> v, const BBox1f& dt)
  22. {
  23. const float rcp_dt_size = float(1.0f)/dt.size();
  24. const T g0 = lerp(v.first,v.second,-dt.lower*rcp_dt_size);
  25. const T g1 = lerp(v.first,v.second,(1.0f-dt.lower)*rcp_dt_size);
  26. return std::make_pair(g0,g1);
  27. }
  28. template<typename T>
  29. struct LBBox
  30. {
  31. public:
  32. __forceinline LBBox () {}
  33. __forceinline LBBox ( const LBBox& other ) {
  34. bounds0 = other.bounds0; bounds1 = other.bounds1;
  35. }
  36. __forceinline LBBox& operator= ( const LBBox& other ) {
  37. bounds0 = other.bounds0; bounds1 = other.bounds1; return *this;
  38. }
  39. __forceinline LBBox (EmptyTy)
  40. : bounds0(EmptyTy()), bounds1(EmptyTy()) {}
  41. __forceinline explicit LBBox ( const BBox<T>& bounds)
  42. : bounds0(bounds), bounds1(bounds) { }
  43. __forceinline LBBox ( const BBox<T>& bounds0, const BBox<T>& bounds1)
  44. : bounds0(bounds0), bounds1(bounds1) { }
  45. LBBox ( const avector<BBox<T>>& bounds )
  46. {
  47. assert(bounds.size());
  48. BBox<T> b0 = bounds.front();
  49. BBox<T> b1 = bounds.back();
  50. for (size_t i=1; i<bounds.size()-1; i++) {
  51. const float f = float(i)/float(bounds.size()-1);
  52. const BBox<T> bt = lerp(b0,b1,f);
  53. const T dlower = min(bounds[i].lower-bt.lower,T(zero));
  54. const T dupper = max(bounds[i].upper-bt.upper,T(zero));
  55. b0.lower += dlower; b1.lower += dlower;
  56. b0.upper += dupper; b1.upper += dupper;
  57. }
  58. bounds0 = b0;
  59. bounds1 = b1;
  60. }
  61. public:
  62. __forceinline bool empty() const {
  63. return bounds().empty();
  64. }
  65. __forceinline BBox<T> bounds () const {
  66. return merge(bounds0,bounds1);
  67. }
  68. __forceinline BBox<T> interpolate( const float t ) const {
  69. return lerp(bounds0,bounds1,t);
  70. }
  71. __forceinline LBBox<T> interpolate( const BBox1f& dt ) const {
  72. return LBBox<T>(interpolate(dt.lower),interpolate(dt.upper));
  73. }
  74. __forceinline void extend( const LBBox& other ) {
  75. bounds0.extend(other.bounds0);
  76. bounds1.extend(other.bounds1);
  77. }
  78. __forceinline float expectedHalfArea() const;
  79. __forceinline float expectedHalfArea(const BBox1f& dt) const {
  80. return interpolate(dt).expectedHalfArea();
  81. }
  82. __forceinline float expectedApproxHalfArea() const {
  83. return 0.5f*(halfArea(bounds0) + halfArea(bounds1));
  84. }
  85. /* calculates bounds for [0,1] time range from bounds in dt time range */
  86. __forceinline LBBox global(const BBox1f& dt) const
  87. {
  88. const float rcp_dt_size = 1.0f/dt.size();
  89. const BBox<T> b0 = interpolate(-dt.lower*rcp_dt_size);
  90. const BBox<T> b1 = interpolate((1.0f-dt.lower)*rcp_dt_size);
  91. return LBBox(b0,b1);
  92. }
  93. /*! Comparison Operators */
  94. template<typename TT> friend __forceinline bool operator==( const LBBox<TT>& a, const LBBox<TT>& b ) { return a.bounds0 == b.bounds0 && a.bounds1 == b.bounds1; }
  95. template<typename TT> friend __forceinline bool operator!=( const LBBox<TT>& a, const LBBox<TT>& b ) { return a.bounds0 != b.bounds0 || a.bounds1 != b.bounds1; }
  96. /*! output operator */
  97. friend __forceinline std::ostream& operator<<(std::ostream& cout, const LBBox& box) {
  98. return cout << "LBBox { " << box.bounds0 << "; " << box.bounds1 << " }";
  99. }
  100. public:
  101. BBox<T> bounds0, bounds1;
  102. };
  103. template<typename T>
  104. __forceinline T expectedArea(const T& a0, const T& a1, const T& b0, const T& b1)
  105. {
  106. const T da = a1-a0;
  107. const T db = b1-b0;
  108. return a0*b0+(a0*db+da*b0)*T(0.5f) + da*db*T(1.0f/3.0f);
  109. }
  110. template<> __forceinline float LBBox<Vec3fa>::expectedHalfArea() const
  111. {
  112. const Vec3fa d0 = bounds0.size();
  113. const Vec3fa d1 = bounds1.size();
  114. return reduce_add(expectedArea(Vec3fa(d0.x,d0.y,d0.z),
  115. Vec3fa(d1.x,d1.y,d1.z),
  116. Vec3fa(d0.y,d0.z,d0.x),
  117. Vec3fa(d1.y,d1.z,d1.x)));
  118. }
  119. template<typename T>
  120. __forceinline float expectedApproxHalfArea(const LBBox<T>& box) {
  121. return box.expectedApproxHalfArea();
  122. }
  123. template<typename T>
  124. __forceinline LBBox<T> merge(const LBBox<T>& a, const LBBox<T>& b) {
  125. return LBBox<T>(merge(a.bounds0, b.bounds0), merge(a.bounds1, b.bounds1));
  126. }
  127. /*! default template instantiations */
  128. typedef LBBox<float> LBBox1f;
  129. typedef LBBox<Vec2f> LBBox2f;
  130. typedef LBBox<Vec3f> LBBox3f;
  131. typedef LBBox<Vec3fa> LBBox3fa;
  132. }