# split/divide/extract/separate mesh indices&vertex in n

I'm totally lost. I'm trying to split/divide/extract/separate a mesh into sub-meshes via bbox test. But I just realized that I need to properly split indices and vertex and I have no idea how o.o... here is the splitting code that "fails" because my indices/vertex needs a proper re-arranging that I have no idea how to solve o.o.

for (auto &box:aabb) { //// list of bboxes
for (int indi = 0; indi < mIndices.size(); indi += 3) { /// get 3 indices for 1 triangle
offX = mIndices[indi];
offY = mIndices[indi + 1];
offZ = mIndices[indi + 2];
/// Check if any of the vertex is inside the box
if (box.contains(mVertex[offX]) || box.contains(mVertex[offY]) || box.contains(mVertex[offZ]))
{
/// bellow code is wrong, I'm over-adding vertex even if they
/// exist, and I'm adding wrong indices because new mVertex/indices arrays are of
/// different sizes to source one... oh dear... I need to add an only missing vertex,
/// and indices re-mapped to proper existing/new vertexes.

box.mVertex.push_back(mVertex[offX]);
box.mVertex.push_back(mVertex[offY]);
box.mVertex.push_back(mVertex[offZ]);
box.mIndices.push_back(offX);
box.mIndices.push_back(offY);
box.mIndices.push_back(offZ);
};
}
}


If any1 could help with this, this would be amazing. I'm not worried if 1 triangle is being duplicated across 2 bounding boxes or even more. As the aaBB can have random positions. I just need each aabb with its own vertex/indices set that can create mesh.

EDIT1.

The only way I could think of doing it is this > it feels quite..."bad tho...

void aabb::addTriangle(QVector3D &x, QVector3D &y, QVector3D &z) {
if (mVertex.contains(x)) {
mIndices.push_back(mVertex.indexOf(x));
} else {
mVertex.push_back(x);
mIndices.push_back(mVertex.size() - 1);
}
if (mVertex.contains(y)) {
mIndices.push_back(mVertex.indexOf(y));
} else {
mVertex.push_back(y);
mIndices.push_back(mVertex.size() - 1);
}
if (mVertex.contains(z)) {
mIndices.push_back(mVertex.indexOf(z));
} else {
mVertex.push_back(z);
mIndices.push_back(mVertex.size() - 1);
}


}

EDIT 2. Thanks to ErnieDingo I'm now not copying vertex in to each aabb object, but only adding triangles(3indices) that if either is being contained by bbox. Then I pass pointer to base mesh vertexArray for further tests down the line

Looking at your code, i will ask, are you referencing the verts local to your BBOX. I assume you are. Your indices are incorrectly indexing. Don't add the indices you tested with, your index values will be the new index of your vertices you have added.

ALA, if index 6,7 and 8 are added to box1, you would need to make them 0, 1 and 2 in your bounding box when you add the vertices.

box.mVertex.push_back(mVertex[offX]);
box.mVertex.push_back(mVertex[offY]);
box.mVertex.push_back(mVertex[offZ]);

box.mIndices.push_back(box.mVertex.size() - 3);
box.mIndices.push_back(box.mVertex.size() - 2);
box.mIndices.push_back(box.mVertex.size() - 1);


Edit: I meant, add box.mVertex.size(), not mVertex.size()

• They, hmm thanks for the answer! but this would add up on the mVertex I think, as I would be adding existing vertexes in to the array. I currently do a check before doing that. So far it looks to be quite expensive operation this division of mesh... hmmm Optimization ideas? :- ) – Dariusz Jan 31 '19 at 21:50
• It's not too expensive, but I would ask, are you looking to just break up at an index level or vertex level. That is, have one set of vertices, and only group the indices in the bounding box? – ErnieDingo Jan 31 '19 at 21:53
• fixed code mistake, I had referenced the wrong array in my sample – ErnieDingo Jan 31 '19 at 21:56
• Woah I had no idea I could just take indexes or vertexes, thanks! For some reason I was copying them all/creating new "sub" meshes, but now that I think of it, all I have to do is pass a pointer to baseMesh vertex array and just store indices in each bbox. woah! Thats awesome thanks! - testes "appear to work" ! :- ) and a lot faster! No need to copy vertex to each bbox. Just indices and pointer to main vertex array. – Dariusz Jan 31 '19 at 23:06
• Ahh good. Enjoy. Happy that solves the issue – ErnieDingo Feb 1 '19 at 1:03