Datasets:
Loader computes wrong inertia for parts with no inertia in the file (hull triangles aren't oriented)
Thanks for the conversion and the Newton loader. They let us simulate all 2,595 files. While checking the physics, we found that the loader computes wrong inertia for most parts whose file gives a mass but no inertia.
Cause. When a part has no inertia, add_simready_glb computes it from the collision hulls. _hull_mesh passes ConvexHull(points).simplices straight to newton.Mesh(..., compute_inertia=True). qhull doesn't orient its simplices consistently. Newton's mesh inertia is a signed-volume integral that needs every triangle facing outward, so most of the volume cancels out. _scale_computed_mass then rescales the result to the file's mass, and Newton corrects the invalid tensor at finalize(). The part ends up with an inertia that looks valid but is wrong.
How many parts. At revision 5888c2b, still the current one, 2,024 parts in 903 files go through this path. We compared each one against the same loader with outward-facing triangles:
| Off by more than this on some axis | Parts | Files |
|---|---|---|
| 2× | 1,841 | 788 |
| 10× | 1,448 | 519 |
| 100× | 861 | 315 |
The median part is off by 63× on its worst axis. Where the error is over 2×, the inertia is too large in 1,702 of 1,841 parts. 63 parts end up with zero inertia, and 170 with over 1,000 kg·m²: food_processed_chocolate_i01 gets about 1e11.
Effect in simulation. In zero gravity, a torque that should spin the mango up to 1.0 rad/s in 0.1 s gives 0.054 rad/s. The result is the same on XPBD, MuJoCo and Featherstone, and it becomes 0.999 rad/s after the fix. All three solvers agree, so comparing solvers doesn't reveal the problem. Checks that a part stays finite and comes to rest, like the README's, don't test inertia directly either.
To reproduce. Run this from the dataset root with Newton 1.7.0 and SciPy 1.18.1: PYTHONPATH=newton_loader python repro.py
import numpy as np
import newton
from scipy.spatial import ConvexHull
from simready_newton import loader
def outward(points, hull):
"""qhull's triangles, each wound so its normal points out of the hull."""
triangles = hull.simplices.copy()
for i, (triangle, equation) in enumerate(zip(triangles, hull.equations)):
a, b, c = points[triangle]
if np.dot(np.cross(b - a, c - a), equation[:3]) < 0:
triangles[i] = triangle[[0, 2, 1]]
return triangles
def sampled_inertia(points, count=2_000_000, seed=0):
"""Inertia of the hull at density 1 by sampling, which doesn't depend on triangle order."""
hull = ConvexHull(points)
samples = np.random.default_rng(seed).uniform(points.min(0), points.max(0), size=(count, 3))
inside = samples[np.all(samples @ hull.equations[:, :3].T + hull.equations[:, 3] <= 0, axis=1)]
offsets = inside - inside.mean(0)
second = offsets.T @ offsets / len(inside) * hull.volume
return np.trace(second) * np.eye(3) - second
def eigs(matrix):
return np.round(np.linalg.eigvalsh(np.asarray(matrix, float).reshape(3, 3)), 6)
# 1. The hull of 400 points on a unit sphere, at density 1.
points = np.random.default_rng(0).normal(size=(400, 3))
points /= np.linalg.norm(points, axis=1, keepdims=True)
hull = ConvexHull(points)
print("sphere reference mass", round(hull.volume, 4), "inertia", eigs(sampled_inertia(points)))
for label, triangles in (("qhull order", hull.simplices), ("outward", outward(points, hull))):
mesh = newton.Mesh(points.astype(np.float32), triangles.flatten(), compute_inertia=True)
print(f"sphere {label:11s} mass", round(float(mesh.mass), 4), "inertia", eigs(mesh.inertia))
# 2. food_produce_mango_h01 has a mass but no inertia, so the loader computes it from the hull.
path = "glb/food_produce_mango_h01.glb"
glb = loader.read_glb(path)
world, _ = loader.scene_graph(glb)
node = next(index for index in world if "motion" in loader.physics_of(glb.nodes[index]))
hull_points = loader._baked_hulls(glb, node)[0][0]
mass = loader.physics_of(glb.nodes[node])["motion"]["mass"]
print("mango reference ", eigs(sampled_inertia(hull_points) * mass / ConvexHull(hull_points).volume))
def outward_mesh(vertices_body, compute_inertia):
points = np.asarray(vertices_body, dtype=np.float64)
triangles = outward(points, ConvexHull(points))
return newton.Mesh(points.astype(np.float32), triangles.flatten(), compute_inertia=compute_inertia)
for label, hull_mesh in (("shipped", loader._hull_mesh), ("outward", outward_mesh)):
loader._hull_mesh = hull_mesh
builder = newton.ModelBuilder(up_axis=newton.Axis.Y)
loader.add_simready_glb(builder, path)
print(f"mango {label:11s}", eigs(builder.finalize().body_inertia.numpy()[0]))
Output, with Warp's start-up lines removed and the warning's file prefix cut:
UserWarning: Inertia validation corrected 1 bodies. Set validate_inertia_detailed=True for detailed per-body warnings.
sphere reference mass 4.0666 inertia [1.591367 1.597612 1.600495]
sphere qhull order mass 0.001 inertia [-11.729593 -11.726384 -0.012544]
sphere outward mass 4.0666 inertia [1.590213 1.596283 1.599195]
mango reference [0.000526 0.000722 0.000775]
mango shipped [0.010014 0.01102 0.021033]
mango outward [0.000526 0.000722 0.000775]
The sphere shows the cause without the loader: in qhull's order the mass comes out 0.001 instead of 4.07, and the inertia is negative. The mango shows the effect through the loader. Its reference is sampled from the same hull, so it doesn't depend on triangle order, and the outward-facing version matches it to 6 digits. The validation warning comes from the shipped load.
Fix. Turn each triangle to face along qhull's outward facet normal before building the mesh:
def _hull_mesh(vertices_body, compute_inertia):
"""A Newton mesh for a convex point set: triangles from qhull, so Newton gets a closed surface."""
points = numpy.asarray(vertices_body, dtype=numpy.float64)
try:
hull = ConvexHull(points)
except Exception:
hull = ConvexHull(points, qhull_options="QJ")
triangles = hull.simplices.copy()
# qhull doesn't orient simplices, and Newton's inertia integral needs every triangle facing outward.
for index, (triangle, equation) in enumerate(zip(triangles, hull.equations)):
a, b, c = points[triangle]
if numpy.dot(numpy.cross(b - a, c - a), equation[:3]) < 0:
triangles[index] = triangle[[0, 2, 1]]
return newton.Mesh(points.astype(numpy.float32), triangles.flatten(), compute_inertia=compute_inertia)
In our copy, triangles are only reordered when compute_inertia is true. Reordering also shifted contact results slightly, and we wanted collision to stay exactly as shipped. You may want the same guard. We haven't tested reordering every hull.
A smaller one: movable_joints recurses forever on instrument_adams_pliers_b01. The loader keeps the pliers' two loop-closing joints outside the articulation, but they're still in the model. That gives the parent-to-child map built in checks.movable_joints a cycle (body 3 → 1 → 3), so subtree_mass recurses until Python's limit, and movable_joints and hinge_test raise RecursionError. It was the only one of the 2,595 files where we hit this. Tracking visited bodies in subtree_mass should avoid it, though we haven't tested that.
Thanks for the detailed report and the repro. Both bugs were real, and both are fixed in e9a7165.
Inertia. _hull_mesh now turns each triangle to face along qhull's outward facet normal, as you suggested. It only does this when the loader computes inertia, so collision hulls stay as shipped. Your repro now gives the same mango inertia for the shipped loader as for the outward version: [0.000526 0.000722 0.000775].
While re-checking every file I found one more problem on the same path: parts whose inertia comes from their hulls never got the inertia floor. 29 flat parts (buttons, eyeglass hinges) have hulls with no volume, so they ended up with zero mass and zero inertia. They now keep the file's mass and get the floor.
Checked across all 2,595 files with Newton 1.7.0:
- 2,026 parts take the hull path. For 60 random single-hull parts, the loaded inertia is within 0.5% of a sampled reference on every axis.
- Newton's inertia validation now corrects 12 bodies, down from 25. All 12 carry inertia from the source USD that breaks the triangle inequality, so I left them as NVIDIA authored them.
Pliers. subtree_mass in checks.movable_joints now tracks visited bodies, and instrument_adams_pliers_b01 returns its 3 movable joints.
The GLB files don't change, only newton_loader and the README changelog. Closing this, but reopen it if anything still looks off.