GatherNode* GatherTreeBuilder::_Build( vector<GpKdItem>::iterator start, vector<GpKdItem>::iterator end, vector<mdGatherPoint> &points, PathSampler* sampler) { assert(end > start); if (end - start == 1) return _MakeLeaf(start, points); BBox<Vec6f> bbox = BBox<Vec6f>(EmptyTy()); for (vector<GpKdItem>::iterator it = start; it != end; it++) bbox.grow(it->point); uint32_t dim = maxDim(bbox.size()); vector<GpKdItem>::iterator mid = start + (end - start) / 2; std::nth_element(start, mid, end, [dim](const GpKdItem &d1, const GpKdItem &d2) -> bool { return d1.point[dim] < d2.point[dim]; }); GatherNode* node = new GatherNode(); node->left = _Build(start, mid, points, sampler); node->right = _Build(mid, end, points, sampler); _UpdateNode(node, sampler); return node; }