libs/yocto/yocto_scene.cpp
static pair<int, int> split_balanced(
    vector<scene_bvh_primitive>& primitives, int start, int end) {
  // initialize split axis and position
  auto axis = 0;
  auto mid  = (start + end) / 2;

  // compute primintive bounds and size
  auto cbbox = invalidb3f;
  for (auto i = start; i < end; i++) cbbox = merge(cbbox, primitives[i].center);
  auto csize = cbbox.max - cbbox.min;
  if (csize == zero3f) return {mid, axis};

  // split along largest
  if (csize.x >= csize.y && csize.x >= csize.z) axis = 0;
  if (csize.y >= csize.x && csize.y >= csize.z) axis = 1;
  if (csize.z >= csize.x && csize.z >= csize.y) axis = 2;

  // balanced tree split: find the largest axis of the
  // bounding box and split along this one right in the middle
  mid = (start + end) / 2;
  std::nth_element(primitives.data() + start, primitives.data() + mid,
      primitives.data() + end, [axis](auto& primitive_a, auto& primitive_b) {
        return primitive_a.center[axis] < primitive_b.center[axis];
      });

  // if we were not able to split, just break the primitives in half
  if (mid == start || mid == end) {
    // throw std::runtime_error("bad bvh split");
    mid = (start + end) / 2;
  }

  return {mid, axis};
}
// Splits a BVH node using the balance heuristic. Returns split position and
// axis.
Splits a BVH node using the balance heuristic. Returns split position and
axis.
Splits a BVH node using the balance heuristic . Returns split position and axis .
static pair<int, int> split_balanced(
    vector<scene_bvh_primitive>& primitives, int start, int end) {
  auto axis = 0;
  auto mid  = (start + end) / 2;
  auto cbbox = invalidb3f;
  for (auto i = start; i < end; i++) cbbox = merge(cbbox, primitives[i].center);
  auto csize = cbbox.max - cbbox.min;
  if (csize == zero3f) return {mid, axis};
  if (csize.x >= csize.y && csize.x >= csize.z) axis = 0;
  if (csize.y >= csize.x && csize.y >= csize.z) axis = 1;
  if (csize.z >= csize.x && csize.z >= csize.y) axis = 2;
  mid = (start + end) / 2;
  std::nth_element(primitives.data() + start, primitives.data() + mid,
      primitives.data() + end, [axis](auto& primitive_a, auto& primitive_b) {
        return primitive_a.center[axis] < primitive_b.center[axis];
      });
  if (mid == start || mid == end) {
    mid = (start + end) / 2;
  }
  return {mid, axis};
}
static pair < int , int > split_balanced ( vector < scene_bvh_primitive > & primitives , int start , int end ) { auto axis = 0 ; auto mid = ( start + end ) / 2 ; auto cbbox = invalidb3f ; for ( auto i = start ; i < end ; i ++ ) cbbox = merge ( cbbox , primitives [ i ] . center ) ; auto csize = cbbox . max - cbbox . min ; if ( csize == zero3f ) return { mid , axis } ; if ( csize . x >= csize . y && csize . x >= csize . z ) axis = 0 ; if ( csize . y >= csize . x && csize . y >= csize . z ) axis = 1 ; if ( csize . z >= csize . x && csize . z >= csize . y ) axis = 2 ; mid = ( start + end ) / 2 ; std :: nth_element ( primitives . data ( ) + start , primitives . data ( ) + mid , primitives . data ( ) + end , [ axis ] ( auto & primitive_a , auto & primitive_b ) { return primitive_a . center [ axis ] < primitive_b . center [ axis ] ; } ) ; if ( mid == start || mid == end ) { mid = ( start + end ) / 2 ; } return { mid , axis } ; }
Splits a BVH node using the balance heuristic.
Splits a BVH node using the balance heuristic .
// initialize split axis and position // compute primintive bounds and size // split along largest // balanced tree split: find the largest axis of the // bounding box and split along this one right in the middle // if we were not able to split, just break the primitives in half // throw std::runtime_error("bad bvh split");