Optimized SubMesh to hold only used vertices #84

Merged
Bogdan Nagirniak merged 3 commits from BLEN-474 into hydra-render 2023-08-02 16:45:15 +02:00
Showing only changes of commit a422272471 - Show all commits

View File

@ -283,20 +283,14 @@ void MeshData::write_submeshes(Mesh *mesh)
else { else {
/* Optimizing submeshes: getting only used vertices, rearranged indices */ /* Optimizing submeshes: getting only used vertices, rearranged indices */
for (SubMesh &sm : submeshes_) { for (SubMesh &sm : submeshes_) {
Map<int, int> index_map; Vector<int> index_map(vertices.size(), 0);

I would guess something like this is more efficient, instead of multiple loops and hash maps.

Vector<int> index_map(vertices.size(), 0);
for (int& face_vertex_index : sm.face_vertex_indices) {
  const int v = face_vertex_index;
  if (index_map[v] == 0) {
    sm.vertices.append(vertices[v]);
    index_map[v] = sm.vertices.size();
  }
  face_vertex_index = index_map[v] - 1;
}
I would guess something like this is more efficient, instead of multiple loops and hash maps. ``` Vector<int> index_map(vertices.size(), 0); for (int& face_vertex_index : sm.face_vertex_indices) { const int v = face_vertex_index; if (index_map[v] == 0) { sm.vertices.append(vertices[v]); index_map[v] = sm.vertices.size(); } face_vertex_index = index_map[v] - 1; } ```

You are right, your algorithm is more efficient and faster, thank you.

You are right, your algorithm is more efficient and faster, thank you.
for (int i : sm.face_vertex_indices) { for (int &face_vertex_index : sm.face_vertex_indices) {
index_map.add(i, 0); const int v = face_vertex_index;
if (index_map[v] == 0) {
sm.vertices.push_back(vertices[v]);
index_map[v] = sm.vertices.size();
} }
i = 0; face_vertex_index = index_map[v] - 1;
for (int &v : index_map.values()) {
v = i++;
}
sm.vertices.resize(index_map.size());
for (auto it : index_map.items()) {
sm.vertices[it.value] = vertices[it.key];
}
for (int &v : sm.face_vertex_indices) {
v = index_map.lookup(v);
} }
} }
} }