Replace TriangleMeshDistance with custom BVH-based SDF computation.

The `mjCOctree::ComputeSdfCoeffs` function has been replaced by `mjCOctree::ComputeSdf`. The new implementation removes the dependency on the `triangle_mesh_distance` library. Instead, it uses a provided `mjCBoundingVolumeHierarchy` to perform closest point queries on the mesh. The signed distance is computed by finding the closest triangle face via the BVH and determining the sign based on the dot product of the vector from the closest point on the triangle to the octree vertex and the triangle's normal.

Optional Laplacian smoothing has been added to the SDF coefficients. This helps smooth out potential discontinuities at octree level boundaries.

PiperOrigin-RevId: 875093374
Change-Id: I6fa53243a2dda107ccf255bc6b876ca5a4554d4f
This commit is contained in:
Alessio Quaglino
2026-02-25 04:46:49 -08:00
committed by Copybara-Service
parent 52ed96bc3a
commit 22e3217fc0
5 changed files with 411 additions and 43 deletions
+12 -2
View File
@@ -200,6 +200,10 @@ class mjCBoundingVolumeHierarchy : public mjCBoundingVolumeHierarchy_ {
+ sizeof(int) * nodeid_.size() + sizeof(int) * level_.size();
}
// query signed distance from point to mesh surface
double QuerySignedDistance(const double* point, const double* vert,
const int* face) const;
private:
// internal class used during BVH construction, for partial sorting of bounding volumes
struct BVElement {
@@ -275,6 +279,7 @@ struct mjCOctree_ {
std::vector<std::vector<int>> hang_; // hanging nodes status (nvert x 1)
double ipos_[3] = {0, 0, 0};
double iquat_[4] = {1, 0, 0, 0};
int smoothing_iterations_ = 0; // Laplacian smoothing iterations (0 = disabled)
};
class mjCOctree : public mjCOctree_ {
@@ -303,8 +308,13 @@ class mjCOctree : public mjCOctree_ {
void AddCoeff(int n, int v, double coeff) { node_[n].coeff[v] = coeff; }
double Coeff(int n, int v) const { return node_[n].coeff[v]; }
// compute SDF coefficients at octree vertices using triangle mesh distance
void ComputeSdfCoeffs(const double* vert, int nvert, const int* face, int nface);
// Set number of Laplacian smoothing iterations (0 = disabled, default)
void SetSmoothingIterations(int iterations) { smoothing_iterations_ = iterations; }
int SmoothingIterations() const { return smoothing_iterations_; }
// compute SDF coefficients via BVH queries, optionally with Laplacian smoothing
void ComputeSdfCoeffs(const double* vert, int nvert, const int* face, int nface,
const mjCBoundingVolumeHierarchy& tree);
private:
void Make(std::vector<Triangle>& elements);