Avoid duplicated vertices in the SDF octree.

Before: nnode: 42777, nvert: 342216
After: nnode: 42777, nvert: 77588
PiperOrigin-RevId: 792115571
Change-Id: I17913ed3d22f22a0370dc4cbd2b833a0f5983cda
This commit is contained in:
Alessio Quaglino
2025-08-07 05:26:04 -07:00
committed by Copybara-Service
parent 840108cf7c
commit 781242ae95
3 changed files with 52 additions and 15 deletions
+17 -9
View File
@@ -23,7 +23,6 @@
#include <cstring>
#include <functional>
#include <limits>
#include <map>
#include <memory>
#include <new>
#include <optional>
@@ -31,6 +30,7 @@
#include <sstream>
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>
#include <vector>
@@ -623,7 +623,8 @@ void mjCOctree::CreateOctree(const double aamm[6]) {
std::vector<Triangle*> elements_ptrs(elements.size());
std::transform(elements.begin(), elements.end(), elements_ptrs.begin(),
[](Triangle& triangle) { return &triangle; });
MakeOctree(elements_ptrs, box);
std::unordered_map<Point, int> vert_map;
MakeOctree(elements_ptrs, box, 0, vert_map);
}
@@ -684,7 +685,8 @@ static bool boxTriangle(const Triangle& v, const double aamm[6]) {
}
int mjCOctree::MakeOctree(const std::vector<Triangle*>& elements, const double aamm[6], int lev) {
int mjCOctree::MakeOctree(const std::vector<Triangle*>& elements, const double aamm[6], int lev,
std::unordered_map<Point, int>& vert_map) {
node_.push_back(OctNode());
OctNode& node = node_.back();
node.level = lev;
@@ -697,11 +699,17 @@ int mjCOctree::MakeOctree(const std::vector<Triangle*>& elements, const double a
(aamm[4] - aamm[1]) / 2, (aamm[5] - aamm[2]) / 2};
node.aabb = aabb;
for (int i = 0; i < 8; i++) {
Point v = {(i & 1) ? aamm[3] : aamm[0],
(i & 2) ? aamm[4] : aamm[1],
(i & 4) ? aamm[5] : aamm[2]};
vert_.push_back(v);
node.vertid[i] = nvert_++;
Point v = {{(i & 1) ? aamm[3] : aamm[0],
(i & 2) ? aamm[4] : aamm[1],
(i & 4) ? aamm[5] : aamm[2]}};
auto it = vert_map.find(v);
if (it != vert_map.end()) {
node.vertid[i] = it->second;
} else {
node.vertid[i] = nvert_;
vert_map[v] = nvert_++;
vert_.push_back(v);
}
node.child[i] = -1;
}
@@ -731,7 +739,7 @@ int mjCOctree::MakeOctree(const std::vector<Triangle*>& elements, const double a
// recursive calls to create sub-boxes
for (int i = 0; i < 8; i++) {
node_[index].child[i] = MakeOctree(colliding, new_aamm[i], lev + 1);
node_[index].child[i] = MakeOctree(colliding, new_aamm[i], lev + 1, vert_map);
}
return index;