1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
void generate_blockmesh(BlockMesh& blockMesh, std::vector<Triangle>& triangles, int maxDepth) {
// Build the octree
std::shared_ptr<OctreeNode> rootNode = std::make_shared<OctreeNode>();
rootNode->indices.resize(triangles.size());
std::iota(rootNode->indices.begin(), rootNode->indices.end(), 0);
build_octree(rootNode, triangles, maxDepth);
// Traverse the octree and generate the blockmesh
std::vector<std::vector<int>> blocks;
int blockCounter = 0; // new counter variable
std::vector<double> vertices;
std::unordered_map<int, int> vertexMap;
int vertexIndex = 0;
std::queue<std::shared_ptr<OctreeNode>> nodeQueue;
nodeQueue.push(rootNode);
while (!nodeQueue.empty()) {
std::shared_ptr<OctreeNode> node = nodeQueue.front();
nodeQueue.pop();
if (node->indices.size() == 0) {
continue;
}
if (node->children[0] != nullptr && node->children[0]->indices.size() > 0) {
// Non-leaf node with non-empty children, enqueue children
for (int i = 0; i < 8; i++) {
nodeQueue.push(node->children[i]);
}
}
else {
// Leaf node, generate block
std::vector<int> blockIndices(8, -1);
int numVertices = 0;
for (int index : node->indices) {
Triangle& tri = triangles[index];
double x1 = tri.x1;
double y1 = tri.y1;
double z1 = tri.z1;
double x2 = tri.x2;
double y2 = tri.y2;
double z2 = tri.z2;
double x3 = tri.x3;
double y3 = tri.y3;
double z3 = tri.z3;
//Add the vertices to the block mesh
std::vector<double> triVertices = { x1, y1, z1, x2, y2, z2, x3, y3, z3 };
int blockIndex = numVertices * 3;
for (int i = 0; i < 3; i++) {
double x = triVertices[i * 3];
double y = triVertices[i * 3 + 1];
double z = triVertices[i * 3 + 2];
auto it = vertexMap.find(blockIndex + i);
if (it == vertexMap.end()) {
// Vertex not in map, add it to vertices and map
vertexMap[blockIndex + i] = vertexIndex;
vertices.push_back(x);
vertices.push_back(y);
vertices.push_back(z);
vertexIndex++;
}
blockIndices[blockIndex / 3 + i] = vertexMap[blockIndex + i];
}
numVertices += 3;
}
// Reset the numVertices to 0
numVertices = 0;
// Add the block to the block mesh
blocks.emplace_back(std::move(blockIndices));
}
}
// Set the vertices and blocks of the block mesh
blockMesh.vertices = vertices;
blockMesh.blocks = blocks;
// Clean up memory used by the octree
delete_octree(rootNode);
}
|