anxuanng commited on
Commit
e9a7165
·
verified ·
1 Parent(s): 5888c2b

Fix hull inertia winding and floor, and the movable_joints cycle (discussion #2)

Browse files
README.md CHANGED
@@ -166,6 +166,9 @@ In a file with joints, the loader raises any part's inertia below 1e-4 kg·m² t
166
  than the source says, and it no longer shakes or goes NaN. Set `jointed_part_inertia_floor` to change the value. Files
167
  with one rigid body use 1e-6 kg·m² (`inertia_floor`).
168
 
 
 
 
169
  43 files across the categories were simulated at 60 fps with 8 substeps. 42 of them were compared with their SimReady USD:
170
 
171
  | Check | Result |
@@ -227,6 +230,12 @@ Two joints in the USD are left out because they hold nothing: the fixed joint of
227
 
228
  ## Changelog
229
 
 
 
 
 
 
 
230
  - 2026-11-01: Added collision hulls that keep concave parts hollow, joint drives and the inertia of small parts.
231
  Added a loader for NVIDIA Newton. The set grows by 0.17%. Meshes, materials and textures do not change.
232
  - 2026-10-29: Added physics from the USD files: rigid bodies, mass, physics materials and 5,550 joints, as
 
166
  than the source says, and it no longer shakes or goes NaN. Set `jointed_part_inertia_floor` to change the value. Files
167
  with one rigid body use 1e-6 kg·m² (`inertia_floor`).
168
 
169
+ A part with a mass but no inertia in the file gets its inertia from its hulls, scaled to that mass. This applies to
170
+ 2,026 parts.
171
+
172
  43 files across the categories were simulated at 60 fps with 8 substeps. 42 of them were compared with their SimReady USD:
173
 
174
  | Check | Result |
 
230
 
231
  ## Changelog
232
 
233
+ - 2026-11-03: Fixed the loader's inertia for parts with no inertia in the file. qhull does not orient its hull
234
+ triangles, so Newton's signed-volume integral cancelled most of the volume and the median part was 63 times off on
235
+ one axis (reported by jayscambler in discussion #2). Triangles of these hulls now face outward; collision-only hulls
236
+ are unchanged. These parts now also get the inertia floor, and flat parts with no hull volume (29 buttons and
237
+ hinges) keep the file's mass instead of ending with zero mass and inertia. `checks.movable_joints` no longer recurses forever on the loop-closing joints of
238
+ `instrument_adams_pliers_b01`. The GLB files do not change.
239
  - 2026-11-01: Added collision hulls that keep concave parts hollow, joint drives and the inertia of small parts.
240
  Added a loader for NVIDIA Newton. The set grows by 0.17%. Meshes, materials and textures do not change.
241
  - 2026-10-29: Added physics from the USD files: rigid bodies, mass, physics materials and 5,550 joints, as
newton_loader/simready_newton/checks.py CHANGED
@@ -158,8 +158,11 @@ def movable_joints(model):
158
  for joint_index in range(model.joint_count):
159
  children_of_body.setdefault(int(parents[joint_index]), []).append(int(children[joint_index]))
160
 
161
- def subtree_mass(body):
162
- return float(masses[body]) + sum(subtree_mass(child) for child in children_of_body.get(body, []))
 
 
 
163
 
164
  gravity = 9.81
165
  centers = model.body_com.numpy()
 
158
  for joint_index in range(model.joint_count):
159
  children_of_body.setdefault(int(parents[joint_index]), []).append(int(children[joint_index]))
160
 
161
+ def subtree_mass(body, visited=frozenset()):
162
+ # Loop-closing joints stay in the model, so the parent-to-child map can have a cycle (adams pliers).
163
+ visited = visited | {body}
164
+ children_left = [child for child in children_of_body.get(body, []) if child not in visited]
165
+ return float(masses[body]) + sum(subtree_mass(child, visited) for child in children_left)
166
 
167
  gravity = 9.81
168
  centers = model.body_com.numpy()
newton_loader/simready_newton/loader.py CHANGED
@@ -196,9 +196,17 @@ def _hull_mesh(vertices_body, compute_inertia):
196
  """A Newton mesh for a convex point set: triangles from qhull, so Newton gets a closed surface."""
197
  points = numpy.asarray(vertices_body, dtype=numpy.float64)
198
  try:
199
- triangles = ConvexHull(points).simplices
200
  except Exception:
201
- triangles = ConvexHull(points, qhull_options="QJ").simplices
 
 
 
 
 
 
 
 
202
  return newton.Mesh(points.astype(numpy.float32), triangles.flatten(), compute_inertia=compute_inertia)
203
 
204
 
@@ -316,7 +324,7 @@ def add_simready_glb(
316
  label=f"{loaded.name}/{glb.nodes[body_node].get('name', body_node)}",
317
  )
318
  for node_index, (mass, center) in needs_scaling.items():
319
- _scale_computed_mass(builder, index_by_node[node_index], mass, center)
320
 
321
  joints = _collect_joints(glb, world_by_node, parent_by_node, body_node_set)
322
  bodies_by_mass = sorted(body_nodes, key=lambda node: -mass_by_node[node])
@@ -353,15 +361,19 @@ def add_simready_glb(
353
  return loaded
354
 
355
 
356
- def _scale_computed_mass(builder, body_index, mass, center):
357
  """Hulls gave a shape for the inertia. The file's mass and center of mass, when present, are still the truth."""
358
  computed_mass = builder.body_mass[body_index]
359
- if mass > 0 and computed_mass > 0:
360
- factor = mass / computed_mass
 
 
 
 
361
  builder.body_mass[body_index] = mass
362
  builder.body_inv_mass[body_index] = 1.0 / mass
363
- builder.body_inertia[body_index] = builder.body_inertia[body_index] * factor
364
- builder.body_inv_inertia[body_index] = builder.body_inv_inertia[body_index] * (1.0 / factor)
365
  if center is not None:
366
  builder.body_com[body_index] = warp.vec3(*center)
367
 
 
196
  """A Newton mesh for a convex point set: triangles from qhull, so Newton gets a closed surface."""
197
  points = numpy.asarray(vertices_body, dtype=numpy.float64)
198
  try:
199
+ hull = ConvexHull(points)
200
  except Exception:
201
+ hull = ConvexHull(points, qhull_options="QJ")
202
+ triangles = hull.simplices.copy()
203
+ if compute_inertia:
204
+ # qhull does not orient its simplices, and Newton's inertia is a signed-volume integral that needs every
205
+ # triangle facing outward. Collision-only hulls keep qhull's order so contacts stay as released.
206
+ for index, (triangle, equation) in enumerate(zip(triangles, hull.equations)):
207
+ first, second, third = points[triangle]
208
+ if numpy.dot(numpy.cross(second - first, third - first), equation[:3]) < 0:
209
+ triangles[index] = triangle[[0, 2, 1]]
210
  return newton.Mesh(points.astype(numpy.float32), triangles.flatten(), compute_inertia=compute_inertia)
211
 
212
 
 
324
  label=f"{loaded.name}/{glb.nodes[body_node].get('name', body_node)}",
325
  )
326
  for node_index, (mass, center) in needs_scaling.items():
327
+ _scale_computed_mass(builder, index_by_node[node_index], mass, center, body_inertia_floor)
328
 
329
  joints = _collect_joints(glb, world_by_node, parent_by_node, body_node_set)
330
  bodies_by_mass = sorted(body_nodes, key=lambda node: -mass_by_node[node])
 
361
  return loaded
362
 
363
 
364
+ def _scale_computed_mass(builder, body_index, mass, center, inertia_floor):
365
  """Hulls gave a shape for the inertia. The file's mass and center of mass, when present, are still the truth."""
366
  computed_mass = builder.body_mass[body_index]
367
+ if mass > 0:
368
+ inertia = numpy.array(builder.body_inertia[body_index], dtype=numpy.float64).reshape(3, 3)
369
+ # A flat hull (a button, a decal) has no volume, so it gives no mass to scale and keeps only the floor.
370
+ inertia = inertia * (mass / computed_mass) if computed_mass > 0 else numpy.zeros((3, 3))
371
+ principal, axes = numpy.linalg.eigh(inertia)
372
+ inertia = axes @ numpy.diag(numpy.maximum(principal, inertia_floor)) @ axes.T
373
  builder.body_mass[body_index] = mass
374
  builder.body_inv_mass[body_index] = 1.0 / mass
375
+ builder.body_inertia[body_index] = warp.mat33(*inertia.flatten())
376
+ builder.body_inv_inertia[body_index] = warp.mat33(*numpy.linalg.pinv(inertia).flatten())
377
  if center is not None:
378
  builder.body_com[body_index] = warp.vec3(*center)
379