uint32_t GatherForestBuilder::_SplitPoints(vector<GatherNode> &tree, vector<GatherPoint> &gps, vector<GatherPoint>::iterator start, vector<GatherPoint>::iterator end ) { Range3f pBox(Vec3f(FLT_MAX, FLT_MAX, FLT_MAX), Vec3f(-FLT_MAX, -FLT_MAX, -FLT_MAX)); Range3f nBox(Vec3f(FLT_MAX, FLT_MAX, FLT_MAX), Vec3f(-FLT_MAX, -FLT_MAX, -FLT_MAX)); Conef cone; vector<GatherPoint>::iterator it = start; while (it != end) { pBox.Grow(it->position); nBox.Grow(it->normal); if (it == start) cone = Conef(it->normal); else cone.Grow(it->normal); it++; } GatherNode node(_tsample); node.bbox = pBox; node.cone = cone; tree.push_back(node); uint32_t curIdx = tree.size() - 1; #ifdef _DEBUG vector<GatherPoint> gp(start, end); #endif // _DEBUG if (end - start > 2) { Vec3f nbSize = nBox.GetSize(); Vec3f pbSize = pBox.GetSize(); bool splitType; uint32_t splitDim; if (pbSize.MaxComponent() > nbSize.MaxComponent()) { splitType = true; splitDim = pbSize.MaxComponentIndex(); } else { splitType = false; splitDim = nbSize.MaxComponentIndex(); } vector<GatherPoint>::iterator splitPos = start + (end - start) / 2; std::nth_element(start, splitPos, end, CompareNode<GatherPoint>(splitType, splitDim)); #ifdef _DEBUG vector<GatherPoint> gp1(start, splitPos); vector<GatherPoint> gp2(splitPos, end); #endif // _DEBUG uint32_t leftIdx = _SplitPoints(tree, gps, start, splitPos); uint32_t rightIdx = _SplitPoints(tree, gps, splitPos, end); GatherNode &curNode = tree[curIdx]; curNode.leftIdx = leftIdx; curNode.rightIdx = rightIdx; vector<uint32_t> &reps = curNode.reps; vector<uint32_t> &lreps = tree[leftIdx].reps; vector<uint32_t> &rreps = tree[rightIdx].reps; for (uint32_t i = 0; i < reps.size(); i++) { if (lreps[i] != -1 && rreps[i] != -1) { //GatherPoint &p1 = gps[lreps[i]]; //GatherPoint &p2 = gps[reps[i]]; reps[i] = (__rand() > 0.5f) ? lreps[i] : rreps[i]; } else { reps[i] = lreps[i] + 1 + rreps[i]; } } } else { vector<GatherPoint>::iterator it = start; while (it != end) { gps.push_back(*it); tree[curIdx].reps[it->timeInst] = gps.size() - 1; it++; } } return curIdx; }