void flattenTree(TreeNode *root, TreeNode *(&tail)) { if (root == NULL) { return; } TreeNode *tmp_left = root->left; TreeNode *tmp_right = root->right; tail->right = root; tail->left = NULL; tail = tail->right; flattenTree(tmp_left, tail); flattenTree(tmp_right, tail); }
int BVH::flattenTree(BVHBuildNode *buildNode, int *offset) { BVHLinearNode *node = &m_root[*offset]; node->m_bbox = buildNode->m_bbox; int tmpOffset = (*offset)++; if(buildNode->m_numTris > 0){ node->m_firstTriOffset = buildNode->m_firstTriOffset; node->m_numTris = buildNode->m_numTris; node->m_axis = buildNode->m_type; } else{ node->m_axis = buildNode->m_splitAxis; node->m_numTris = 0; flattenTree(buildNode->m_children[0],offset); node->m_secondChildOffset = flattenTree(buildNode->m_children[1],offset); } return tmpOffset; }
void BVH::build(std::vector<Triangle *> triangles, std::vector<Instance *> *instances) { std::cout << "Building BVH" << std::endl; m_buildData.reserve(triangles.size()); m_triangles = triangles; for(int i=0;i<triangles.size();i++){ m_buildData.push_back(BVHTriangleInfo(i,AABoundingBox(triangles[i]), 0, 1 )); } if(instances != 0){ m_instances = *instances; for(int i=0;i<instances->size();i++){ m_buildData.push_back(BVHTriangleInfo(i,(*instances)[i]->m_bounds, 1, (*instances)[i]->getNumTris())); } } int numTotalNodes = 0; BVHBuildNode *buildRoot = recursiveBuild(0,m_buildData.size(),&numTotalNodes); m_root = new BVHLinearNode[numTotalNodes]; int offset = 0; flattenTree(buildRoot,&offset); recursiveDelete(buildRoot); m_buildData.clear(); writeObjFile("/tmp/bvh.obj"); }
void flatten(TreeNode *root) { TreeNode dummy(-1); TreeNode *tail = &dummy; flattenTree(root, tail); root = dummy.right; }