@@ -362,6 +362,7 @@ void mjCMesh::LoadSDF() {
362
362
}
363
363
364
364
needreorient_ = false ;
365
+ needoct_ = false ;
365
366
normal_ = std::move (usernormal);
366
367
face_ = std::move (userface);
367
368
ProcessVertices (uservert);
@@ -631,8 +632,7 @@ void mjCMesh::TryCompile(const mjVFS* vfs) {
631
632
resource_ = LoadResource (modelfiledir_.Str (), filename.Str (), vfs);
632
633
633
634
// 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_)) {
636
636
mju_closeResource (resource_);
637
637
resource_ = nullptr ;
638
638
fromCache = true ;
@@ -682,12 +682,39 @@ void mjCMesh::TryCompile(const mjVFS* vfs) {
682
682
// compute mesh properties
683
683
if (!fromCache) {
684
684
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_);
685
693
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
+ }
688
710
}
689
711
}
690
712
713
+ // cache mesh
714
+ if (!fromCache && !file_.empty ()) {
715
+ CacheMesh (cache, resource_);
716
+ }
717
+
691
718
// close resource
692
719
if (resource_ != nullptr ) {
693
720
mju_closeResource (resource_);
@@ -1549,32 +1576,6 @@ void mjCMesh::Process() {
1549
1576
}
1550
1577
Rotate (quattmp);
1551
1578
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
-
1578
1579
// save the pos and quat that was used to transform the mesh
1579
1580
mjuu_copyvec (pos_, CoM, 3 );
1580
1581
mjuu_copyvec (quat_, quattmp, 4 );
0 commit comments