Skip to content

Commit d81ab17

Browse files
quaglacopybara-github
authored andcommitted
Cache SDF octree.
PiperOrigin-RevId: 781500550 Change-Id: I0077d41f13be0243bff292534cc6bce2bdc1caa4
1 parent fbea72d commit d81ab17

File tree

1 file changed

+31
-30
lines changed

1 file changed

+31
-30
lines changed

src/user/user_mesh.cc

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ void mjCMesh::LoadSDF() {
362362
}
363363

364364
needreorient_ = false;
365+
needoct_ = false;
365366
normal_ = std::move(usernormal);
366367
face_ = std::move(userface);
367368
ProcessVertices(uservert);
@@ -631,8 +632,7 @@ void mjCMesh::TryCompile(const mjVFS* vfs) {
631632
resource_ = LoadResource(modelfiledir_.Str(), filename.Str(), vfs);
632633

633634
// try loading from cache
634-
// TODO: move octree to mesh frame so it can be cached
635-
if (cache != nullptr && !needoct_ && LoadCachedMesh(cache, resource_)) {
635+
if (cache != nullptr && LoadCachedMesh(cache, resource_)) {
636636
mju_closeResource(resource_);
637637
resource_ = nullptr;
638638
fromCache = true;
@@ -682,12 +682,39 @@ void mjCMesh::TryCompile(const mjVFS* vfs) {
682682
// compute mesh properties
683683
if (!fromCache) {
684684
Process();
685+
}
686+
687+
// make octree
688+
if (!needoct_) {
689+
octree_.Clear(); // this occurs when a non-SDF mesh is loaded from a cached SDF mesh
690+
} else if (octree_.Nodes().empty()) {
691+
octree_.SetFace(vert_, face_);
692+
octree_.CreateOctree(aamm_);
685693

686-
if (!file_.empty()) {
687-
CacheMesh(cache, resource_);
694+
// compute sdf coefficients
695+
if (!plugin.active) {
696+
tmd::TriangleMeshDistance sdf(vert_.data(), nvert(), face_.data(), nface());
697+
698+
// TODO: do not evaluate the SDF multiple times at the same vertex
699+
// TODO: the value at hanging vertices should be computed from the parent
700+
const double* nodes = octree_.Nodes().data();
701+
for (int i = 0; i < octree_.NumNodes(); ++i) {
702+
for (int j = 0; j < 8; j++) {
703+
mjtNum v[3];
704+
v[0] = nodes[6*i+0] + (j&1 ? 1 : -1) * nodes[6*i+3];
705+
v[1] = nodes[6*i+1] + (j&2 ? 1 : -1) * nodes[6*i+4];
706+
v[2] = nodes[6*i+2] + (j&4 ? 1 : -1) * nodes[6*i+5];
707+
octree_.AddCoeff(sdf.signed_distance(v).distance);
708+
}
709+
}
688710
}
689711
}
690712

713+
// cache mesh
714+
if (!fromCache && !file_.empty()) {
715+
CacheMesh(cache, resource_);
716+
}
717+
691718
// close resource
692719
if (resource_ != nullptr) {
693720
mju_closeResource(resource_);
@@ -1549,32 +1576,6 @@ void mjCMesh::Process() {
15491576
}
15501577
Rotate(quattmp);
15511578

1552-
// make octree in mesh frame
1553-
if (!needoct_) {
1554-
octree_.Clear();
1555-
} else if (octree_.Nodes().empty()) {
1556-
octree_.SetFace(vert_, face_);
1557-
octree_.CreateOctree(aamm_);
1558-
1559-
// compute sdf coefficients
1560-
if (!plugin.active) {
1561-
tmd::TriangleMeshDistance sdf(vert_.data(), nvert(), face_.data(), nface());
1562-
1563-
// TODO: do not evaluate the SDF multiple times at the same vertex
1564-
// TODO: the value at hanging vertices should be computed from the parent
1565-
const double* nodes = octree_.Nodes().data();
1566-
for (int i = 0; i < octree_.NumNodes(); ++i) {
1567-
for (int j = 0; j < 8; j++) {
1568-
mjtNum v[3];
1569-
v[0] = nodes[6*i+0] + (j&1 ? 1 : -1) * nodes[6*i+3];
1570-
v[1] = nodes[6*i+1] + (j&2 ? 1 : -1) * nodes[6*i+4];
1571-
v[2] = nodes[6*i+2] + (j&4 ? 1 : -1) * nodes[6*i+5];
1572-
octree_.AddCoeff(sdf.signed_distance(v).distance);
1573-
}
1574-
}
1575-
}
1576-
}
1577-
15781579
// save the pos and quat that was used to transform the mesh
15791580
mjuu_copyvec(pos_, CoM, 3);
15801581
mjuu_copyvec(quat_, quattmp, 4);

0 commit comments

Comments
 (0)