Octree data structure method

Can anyone tell my why I'm getting out of subscription error for blockIndices? This code reads the triangulation of the surface of a volume and uses Octree method to generate blockmesh inside the volume. here is the function for creating blockmesh.

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);
} 
Last edited on
what happens when you step into readSTLASCII in your debugger?
@jonnin It reads the STL File correctly and I get the vector of triangle containing the coordinate of vertices.
ok, and what does it do in generate hex mesh?
you just say main doesn't do anything .. is it infinite looping?
I don't know this code well enough but I see
148 - 160 up there...

while queue not empty
queue pop once
for... queue push possibly many
so if oct children isn't right, then it could go nuts.
Or it could be right... but if you want to fix it, you have to trace into this stuff and see where its going weird.
Topic archived. No new replies allowed.