priminfo.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 "../common/default.h"
  18. namespace embree
  19. {
  20. namespace isa
  21. {
  22. template<typename BBox>
  23. class CentGeom
  24. {
  25. public:
  26. __forceinline CentGeom () {}
  27. __forceinline CentGeom (EmptyTy)
  28. : geomBounds(empty), centBounds(empty) {}
  29. __forceinline CentGeom (const BBox& geomBounds, const BBox3fa& centBounds)
  30. : geomBounds(geomBounds), centBounds(centBounds) {}
  31. __forceinline void extend(const BBox& geomBounds_, const BBox3fa& centBounds_) {
  32. geomBounds.extend(geomBounds_);
  33. centBounds.extend(centBounds_);
  34. }
  35. __forceinline void reset() {
  36. geomBounds = empty;
  37. centBounds = empty;
  38. }
  39. template<typename PrimRef>
  40. __forceinline void extend_primref(const PrimRef& prim)
  41. {
  42. BBox bounds; Vec3fa center;
  43. prim.binBoundsAndCenter(bounds,center);
  44. geomBounds.extend(bounds);
  45. centBounds.extend(center);
  46. }
  47. __forceinline void extend(const BBox& geomBounds_) {
  48. geomBounds.extend(geomBounds_);
  49. centBounds.extend(center2(geomBounds_));
  50. }
  51. __forceinline void merge(const CentGeom& other)
  52. {
  53. geomBounds.extend(other.geomBounds);
  54. centBounds.extend(other.centBounds);
  55. }
  56. static __forceinline const CentGeom merge2(const CentGeom& a, const CentGeom& b) {
  57. CentGeom r = a; r.merge(b); return r;
  58. }
  59. public:
  60. BBox geomBounds; //!< geometry bounds of primitives
  61. BBox3fa centBounds; //!< centroid bounds of primitives
  62. };
  63. typedef CentGeom<BBox3fa> CentGeomBBox3fa;
  64. /*! stores bounding information for a set of primitives */
  65. template<typename BBox>
  66. class PrimInfoT : public CentGeom<BBox>
  67. {
  68. public:
  69. using CentGeom<BBox>::geomBounds;
  70. using CentGeom<BBox>::centBounds;
  71. __forceinline PrimInfoT () {}
  72. __forceinline PrimInfoT (EmptyTy)
  73. : CentGeom<BBox>(empty), begin(0), end(0) {}
  74. __forceinline void reset() {
  75. CentGeom<BBox>::reset();
  76. begin = end = 0;
  77. }
  78. __forceinline PrimInfoT (size_t num, const BBox& geomBounds, const BBox3fa& centBounds)
  79. : CentGeom<BBox>(geomBounds,centBounds), begin(0), end(num) {}
  80. __forceinline PrimInfoT (size_t begin, size_t end, const BBox& geomBounds, const BBox3fa& centBounds)
  81. : CentGeom<BBox>(geomBounds,centBounds), begin(begin), end(end) {}
  82. template<typename PrimRef>
  83. __forceinline void add_primref(const PrimRef& prim)
  84. {
  85. CentGeom<BBox>::extend_primref(prim);
  86. end++;
  87. }
  88. __forceinline void add(const BBox& geomBounds_) {
  89. CentGeom<BBox>::extend(geomBounds_);
  90. end++;
  91. }
  92. __forceinline void add(const BBox& geomBounds_, const size_t i) {
  93. CentGeom<BBox>::extend(geomBounds_,center2(geomBounds_));
  94. end+=i;
  95. }
  96. __forceinline void add(const size_t i=1) {
  97. end+=i;
  98. }
  99. __forceinline void add(const BBox& geomBounds_, const BBox3fa& centBounds_, size_t num_ = 1) {
  100. CentGeom<BBox>::extend(geomBounds_,centBounds_);
  101. end += num_;
  102. }
  103. __forceinline void merge(const PrimInfoT& other)
  104. {
  105. CentGeom<BBox>::merge(other);
  106. //assert(begin == 0);
  107. begin += other.begin;
  108. end += other.end;
  109. }
  110. static __forceinline const PrimInfoT merge(const PrimInfoT& a, const PrimInfoT& b) {
  111. PrimInfoT r = a; r.merge(b); return r;
  112. }
  113. /*! returns the number of primitives */
  114. __forceinline size_t size() const {
  115. return end-begin;
  116. }
  117. __forceinline float leafSAH() const {
  118. return expectedApproxHalfArea(geomBounds)*float(size());
  119. //return halfArea(geomBounds)*blocks(num);
  120. }
  121. __forceinline float leafSAH(size_t block_shift) const {
  122. return expectedApproxHalfArea(geomBounds)*float((size()+(size_t(1)<<block_shift)-1) >> block_shift);
  123. //return halfArea(geomBounds)*float((num+3) >> 2);
  124. //return halfArea(geomBounds)*blocks(num);
  125. }
  126. /*! stream output */
  127. friend std::ostream& operator<<(std::ostream& cout, const PrimInfoT& pinfo) {
  128. return cout << "PrimInfo { begin = " << pinfo.begin << ", end = " << pinfo.end << ", geomBounds = " << pinfo.geomBounds << ", centBounds = " << pinfo.centBounds << "}";
  129. }
  130. public:
  131. size_t begin,end; //!< number of primitives
  132. };
  133. typedef PrimInfoT<BBox3fa> PrimInfo;
  134. typedef PrimInfoT<LBBox3fa> PrimInfo2;
  135. }
  136. }