gridrange.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. struct __aligned(16) GridRange
  21. {
  22. unsigned int u_start;
  23. unsigned int u_end;
  24. unsigned int v_start;
  25. unsigned int v_end;
  26. __forceinline GridRange() {}
  27. __forceinline GridRange(unsigned int u_start, unsigned int u_end, unsigned int v_start, unsigned int v_end)
  28. : u_start(u_start), u_end(u_end), v_start(v_start), v_end(v_end) {}
  29. __forceinline unsigned int width() const {
  30. return u_end-u_start+1;
  31. }
  32. __forceinline unsigned int height() const {
  33. return v_end-v_start+1;
  34. }
  35. __forceinline bool hasLeafSize() const
  36. {
  37. const unsigned int u_size = u_end-u_start+1;
  38. const unsigned int v_size = v_end-v_start+1;
  39. assert(u_size >= 1);
  40. assert(v_size >= 1);
  41. return u_size <= 3 && v_size <= 3;
  42. }
  43. static __forceinline unsigned int split(unsigned int start,unsigned int end)
  44. {
  45. const unsigned int center = (start+end)/2;
  46. assert (center > start);
  47. assert (center < end);
  48. return center;
  49. }
  50. __forceinline void split(GridRange& r0, GridRange& r1) const
  51. {
  52. assert( hasLeafSize() == false );
  53. const unsigned int u_size = u_end-u_start+1;
  54. const unsigned int v_size = v_end-v_start+1;
  55. r0 = *this;
  56. r1 = *this;
  57. if (u_size >= v_size)
  58. {
  59. const unsigned int u_mid = split(u_start,u_end);
  60. r0.u_end = u_mid;
  61. r1.u_start = u_mid;
  62. }
  63. else
  64. {
  65. const unsigned int v_mid = split(v_start,v_end);
  66. r0.v_end = v_mid;
  67. r1.v_start = v_mid;
  68. }
  69. }
  70. __forceinline unsigned int splitIntoSubRanges(GridRange r[4]) const
  71. {
  72. assert( !hasLeafSize() );
  73. unsigned int children = 0;
  74. GridRange first,second;
  75. split(first,second);
  76. if (first.hasLeafSize()) {
  77. r[0] = first;
  78. children++;
  79. }
  80. else {
  81. first.split(r[0],r[1]);
  82. children += 2;
  83. }
  84. if (second.hasLeafSize()) {
  85. r[children] = second;
  86. children++;
  87. }
  88. else {
  89. second.split(r[children+0],r[children+1]);
  90. children += 2;
  91. }
  92. return children;
  93. }
  94. };
  95. }