The derived classes must compute the centroids of the primitives and store them in the member mCentroids before calling create. The input height specifies the desired height of the tree and must be no larger than 31. If std::numeric_limits ::max(), the the entire tree is built and the actual height is computed from centroids.size(). If larger than 31, the height is clamped to 31.
| 101 | // the entire tree is built and the actual height is computed from |
| 102 | // centroids.size(). If larger than 31, the height is clamped to 31. |
| 103 | void Create(size_t height) |
| 104 | { |
| 105 | LogAssert(mCentroids.size() > 0, "Invalid input."); |
| 106 | |
| 107 | if (height == std::numeric_limits<size_t>::max()) |
| 108 | { |
| 109 | uint64_t minPowerOfTwo = BitHacks::RoundUpToPowerOfTwo( |
| 110 | static_cast<uint32_t>(mCentroids.size())); |
| 111 | uint32_t logMinPowerOfTwo = BitHacks::Log2OfPowerOfTwo( |
| 112 | static_cast<uint32_t>(minPowerOfTwo)); |
| 113 | mHeight = static_cast<size_t>(logMinPowerOfTwo); |
| 114 | } |
| 115 | else |
| 116 | { |
| 117 | mHeight = std::min(height, static_cast<size_t>(31)); |
| 118 | } |
| 119 | |
| 120 | // The tree is built recursively. Preallocate the nodes because |
| 121 | // the BuiltTree function declares references on the stack, so |
| 122 | // we must guarantee that no reallocations occur in order to avoid |
| 123 | // invalidating those references. |
| 124 | size_t const numNodes = (static_cast<size_t>(1) << (mHeight + 1)) - 1; |
| 125 | mNodes.resize(numNodes); |
| 126 | |
| 127 | // The array mPartition stores indices into mCentroids so that at |
| 128 | // a node, the centroids represented by the node are the indices |
| 129 | // [mPartition[node.minIndex], mPartition[node.maxIndex]]. |
| 130 | mPartition.resize(mCentroids.size()); |
| 131 | std::iota(mPartition.begin(), mPartition.end(), 0); |
| 132 | |
| 133 | // Build the tree recursively. |
| 134 | size_t const depth = 0; |
| 135 | size_t const nodeIndex = 0; |
| 136 | size_t const i0 = 0; |
| 137 | size_t const i1 = mCentroids.size() - 1; |
| 138 | BuildTree(depth, nodeIndex, i0, i1); |
| 139 | } |
| 140 | |
| 141 | // Member access. |
| 142 | inline std::vector<Vector3<T>> const& GetCentroids() const |
no test coverage detected