Merge the input tree into current tree. Traverse the tree and find the smallest node, where the whole new tree fits. When we find the node we create one new node pointing to the original children and the to the input tree root.
| 835 | // Traverse the tree and find the smallest node, where the whole new tree fits. When we find the node |
| 836 | // we create one new node pointing to the original children and the to the input tree root. |
| 837 | void AABBTree::mergeTree(const AABBTreeMergeData& treeParams) |
| 838 | { |
| 839 | // allocate new indices buffer |
| 840 | PxU32* newIndices = reinterpret_cast<PxU32*>(PX_ALLOC(sizeof(PxU32)*(mNbIndices + treeParams.mNbIndices), "AABB tree indices")); |
| 841 | PxMemCopy(newIndices, mIndices, sizeof(PxU32)*mNbIndices); |
| 842 | PX_FREE(mIndices); |
| 843 | mIndices = newIndices; |
| 844 | mTotalPrims += treeParams.mNbIndices; |
| 845 | |
| 846 | // copy the new indices, re-index using the provided indicesOffset. Note that indicesOffset |
| 847 | // must be provided, as original mNbIndices can be different than indicesOffset dues to object releases. |
| 848 | for (PxU32 i = 0; i < treeParams.mNbIndices; i++) |
| 849 | { |
| 850 | mIndices[mNbIndices + i] = treeParams.mIndicesOffset + treeParams.mIndices[i]; |
| 851 | } |
| 852 | |
| 853 | // check the mRefitBitmask if we fit all the new nodes |
| 854 | mRefitBitmask.resize(mTotalNbNodes + treeParams.mNbNodes + 1); |
| 855 | |
| 856 | // create the parent information so we can update it |
| 857 | if(!mParentIndices) |
| 858 | { |
| 859 | mParentIndices = reinterpret_cast<PxU32*>(PX_ALLOC(sizeof(PxU32)*mTotalNbNodes, "AABB parent indices")); |
| 860 | _createParentArray(mTotalNbNodes, mParentIndices, mRuntimePool, mRuntimePool, mRuntimePool); |
| 861 | } |
| 862 | |
| 863 | // if new tree is inside the root AABB we will traverse the tree to find better node where to attach the tree subnodes |
| 864 | // if the root is a leaf we merge with the root. |
| 865 | if(treeParams.getRootNode().mBV.isInside(mRuntimePool[0].mBV) && !mRuntimePool[0].isLeaf()) |
| 866 | { |
| 867 | traverseRuntimeNode(mRuntimePool[0], treeParams, 0); |
| 868 | } |
| 869 | else |
| 870 | { |
| 871 | if(mRuntimePool[0].isLeaf()) |
| 872 | { |
| 873 | mergeRuntimeLeaf(mRuntimePool[0], treeParams, 0); |
| 874 | } |
| 875 | else |
| 876 | { |
| 877 | mergeRuntimeNode(mRuntimePool[0], treeParams, 0); |
| 878 | } |
| 879 | |
| 880 | // increase the tree root AABB |
| 881 | mRuntimePool[0].mBV.include(treeParams.getRootNode().mBV); |
| 882 | } |
| 883 | |
| 884 | #ifdef _DEBUG |
| 885 | //verify parent indices |
| 886 | for (PxU32 i = 0; i < mTotalNbNodes; i++) |
| 887 | { |
| 888 | if (i) |
| 889 | { |
| 890 | PX_ASSERT(mRuntimePool[mParentIndices[i]].getPosIndex() == i || mRuntimePool[mParentIndices[i]].getNegIndex() == i); |
| 891 | } |
| 892 | if (!mRuntimePool[i].isLeaf()) |
| 893 | { |
| 894 | PX_ASSERT(mParentIndices[mRuntimePool[i].getPosIndex()] == i); |
no test coverage detected