static int32_t kdBinGetNodes(const kdTree &tree, const kdNode *node, u::vector<kdBinPlane> &planes, u::vector<kdBinNode> &nodes, u::vector<kdBinLeaf> &leafs) { if (node->isLeaf()) return kdBinInsertLeaf(node, leafs); // We only care about the distance and axis type for the plane. kdBinPlane binPlane; binPlane.d = node->m_splitPlane.d; for (size_t i = 0; i < 3; i++) { if (m::abs(node->m_splitPlane.n[i]) > m::kEpsilon) { binPlane.type = i; break; } } planes.push_back(binPlane); const size_t planeIndex = planes.size() - 1; const size_t nodeIndex = nodes.size(); kdBinNode binNode; binNode.plane = planeIndex; binNode.sphereRadius = node->m_sphereRadius; binNode.sphereOrigin = node->m_sphereOrigin; nodes.push_back(binNode); nodes[nodeIndex].children[0] = kdBinGetNodes(tree, node->m_front, planes, nodes, leafs); nodes[nodeIndex].children[1] = kdBinGetNodes(tree, node->m_back, planes, nodes, leafs); return nodeIndex; }
void kdNode::split(const kdTree *tree, const u::vector<int> &tris, m::axis axis, u::vector<int> &frontList, u::vector<int> &backList, u::vector<int> &splitList, m::plane &plane) const { const size_t triangleCount = tris.size(); plane = findSplittingPlane(tree, tris, axis); for (size_t i = 0; i < triangleCount; i++) { switch (tree->testTriangle(tris[i], plane)) { case kPolyPlaneCoplanar: case kPolyPlaneSplit: splitList.push_back(tris[i]); break; case kPolyPlaneFront: frontList.push_back(tris[i]); break; case kPolyPlaneBack: backList.push_back(tris[i]); break; } } }
static uint32_t kdBinAddTexture(u::vector<kdBinTexture> &textures, const u::string &texturePath) { uint32_t index = 0; for (const auto &it : textures) { if (it.name == texturePath) return index; index++; } kdBinTexture texture; // Truncate the string if it doesn't fit const size_t length = u::min(sizeof texture.name - 1, texturePath.size()); memcpy(texture.name, (const void *)texturePath.c_str(), length); texture.name[length] = '\0'; textures.push_back(texture); return textures.size() - 1; }
static int32_t kdBinInsertLeaf(const kdNode *leaf, u::vector<kdBinLeaf> &leafs) { kdBinLeaf binLeaf; binLeaf.triangles.insert(binLeaf.triangles.begin(), leaf->m_triangles.begin(), leaf->m_triangles.end()); leafs.push_back(binLeaf); return -(int32_t)leafs.size(); // leaf indices are stored with negative index }