Skip to content

Official Codebase of 3DCodeVerse

Python

connectivity: interpenetration was never detected between non-watertight parts

gaoypeng28de927f98a3
diff --git a/harness/codeverse/spatial/connectivity.py b/harness/codeverse/spatial/connectivity.py
index 224d23b8..82154e1e 100644
--- a/harness/codeverse/spatial/connectivity.py
+++ b/harness/codeverse/spatial/connectivity.py
@@ -39,7 +39,7 @@ import trimesh
from codeverse.contracts.artifacts import GateFinding, GateReport, Severity
from codeverse.conventions import CONTACT_GAP_M, Frame
from codeverse.spatial.contract import frame_label, glb_vec_to_plan, language_frame
-from codeverse.spatial.joints_collide import fcl_collision_object
+from codeverse.spatial.joints_collide import fcl_collision_object, inside_island, oriented_islands
from codeverse.spatial.measure import GlbLoadError, fmt_vec, solid_parts
GATE = "connectivity"
@@ -148,22 +148,40 @@ def _sample(mesh: trimesh.Trimesh, n: int, rng: np.random.Generator) -> np.ndarr
def penetration_depth(a: trimesh.Trimesh, b: trimesh.Trimesh, n: int = N_SAMPLES) -> tuple[float, float]:
- """(max depth, fraction of samples inside) of ``a``'s surface inside ``b`` and vice versa."""
+ """(max depth, fraction of samples inside) of ``a``'s surface inside ``b`` and vice versa.
+
+ Containment is the per-island, fixed-direction parity ray test the articulation gate
+ already uses (:func:`codeverse.spatial.joints_collide.inside_island`), NOT
+ ``Trimesh.contains``. ``contains`` needs a watertight mesh, so this used to
+ ``continue`` past any open shell — and when NEITHER part was watertight it returned
+ (0.0, 0.0), indistinguishable from "no overlap", with no warning that the check had
+ not run. A 50 mm post driven into a base passed the gate as soon as both parts were
+ modelled as open-bottomed shells, which is the common case for agent-exported meshes
+ (open shells, boolean leftovers, mirrored halves, single-sided planes); only the
+ mixed closed/open case still fired, which is why the shipped tests stayed green.
+ Islands that are not watertight are tested the same way and the answer is
+ approximate — the same trade joints_collide.penetration documents and flags.
+ """
rng = np.random.default_rng(_RNG_SEED)
worst_depth, worst_frac = 0.0, 0.0
for src, dst in ((a, b), (b, a)):
- if not dst.is_watertight: # containment is meaningless on open shells
- continue
pts = _sample(src, n, rng)
- try:
- inside = dst.contains(pts)
- except Exception:
- continue
+ inside = np.zeros(len(pts), dtype=bool)
+ depth = 0.0
+ for island in oriented_islands(dst):
+ try:
+ hit = inside_island(island, pts)
+ except Exception: # noqa: BLE001 - a degenerate island must not fail the gate
+ continue
+ if not hit.any():
+ continue
+ _, dist, _ = trimesh.proximity.closest_point(island, pts[hit])
+ depth = max(depth, float(np.max(dist)))
+ inside |= hit
frac = float(np.mean(inside)) if len(inside) else 0.0
if frac <= 0.0:
continue
- _, dist, _ = trimesh.proximity.closest_point(dst, pts[inside])
- worst_depth = max(worst_depth, float(np.max(dist)))
+ worst_depth = max(worst_depth, depth)
worst_frac = max(worst_frac, frac)
return worst_depth, worst_frac
diff --git a/harness/codeverse/spatial/joints_collide.py b/harness/codeverse/spatial/joints_collide.py
index d7108855..d6e467a0 100644
--- a/harness/codeverse/spatial/joints_collide.py
+++ b/harness/codeverse/spatial/joints_collide.py
@@ -79,8 +79,12 @@ def oriented_islands(mesh: trimesh.Trimesh) -> list[trimesh.Trimesh]:
return islands
-def _inside(island: trimesh.Trimesh, points: np.ndarray) -> np.ndarray:
- """Deterministic parity containment (fixed ray direction, both directions must agree)."""
+def inside_island(island: trimesh.Trimesh, points: np.ndarray) -> np.ndarray:
+ """Deterministic parity containment (fixed ray direction, both directions must agree).
+
+ Public because the connectivity gate measures interpenetration the same way: it must
+ work on the non-watertight meshes agents actually export, where ``Trimesh.contains``
+ cannot answer at all."""
if len(points) == 0:
return np.zeros(0, dtype=bool)
return np.asarray(ray_util.contains_points(island.ray, points, check_direction=_RAY_DIR), dtype=bool)
@@ -132,14 +136,14 @@ class LinkBody:
local = trimesh.transform_points(points_world, self.T_inv)
depth = 0.0
for island, pq in zip(self.islands, self._island_pq, strict=True):
- inside = _inside(island, local)
+ inside = inside_island(island, local)
if inside.any():
depth = max(depth, float(pq.on_surface(local[inside])[1].max()))
return depth
def any_inside(self, points_world: np.ndarray) -> bool:
local = trimesh.transform_points(points_world, self.T_inv)
- return any(bool(_inside(i, local).any()) for i in self.islands)
+ return any(bool(inside_island(i, local).any()) for i in self.islands)
def aabb_gap(a: np.ndarray, b: np.ndarray) -> float:
diff --git a/harness/tests/spatial_tools/test_connectivity.py b/harness/tests/spatial_tools/test_connectivity.py
index 5393fa4b..34734fa7 100644
--- a/harness/tests/spatial_tools/test_connectivity.py
+++ b/harness/tests/spatial_tools/test_connectivity.py
@@ -119,3 +119,41 @@ def test_penetration_depth_zero_when_apart() -> None:
b = trimesh.creation.box(extents=(1, 1, 1))
b.apply_translation((2, 0, 0))
assert penetration_depth(a, b) == (0.0, 0.0)
+
+
+def _open_bottom_box(extents, translation) -> trimesh.Trimesh:
+ """A box with its bottom face dropped — an open shell, what agents actually export."""
+ m = trimesh.creation.box(extents=extents)
+ m.apply_translation(translation)
+ low = m.vertices[m.faces].mean(axis=1)[:, 1]
+ shell = trimesh.Trimesh(vertices=m.vertices, faces=m.faces[low > low.min() + 1e-9], process=False)
+ assert not shell.is_watertight
+ return shell
+
+
+def test_interpenetration_is_found_between_two_non_watertight_parts(tmp_path: Path) -> None:
+ """CG-2: penetration_depth skipped any direction whose target was not watertight
+ (``Trimesh.contains`` needs one), so when NEITHER part was watertight it returned
+ (0.0, 0.0) — indistinguishable from "no overlap", with no warning that the check had
+ not run. A 0.2 m post driven 50 mm into a base then passed the gate as soon as both
+ were modelled as open shells; only the mixed closed/open case still fired, which is
+ why the shipped tests stayed green."""
+ base_ext, post_ext = (0.4, 0.2, 0.4), (0.2, 0.2, 0.2)
+ base_at, post_at = (0, 0.1, 0), (0, 0.25, 0) # post bottom 50 mm below the base's top face
+ closed = (trimesh.creation.box(extents=base_ext), trimesh.creation.box(extents=post_ext))
+ closed[0].apply_translation(base_at)
+ closed[1].apply_translation(post_at)
+ shells = (_open_bottom_box(base_ext, base_at), _open_bottom_box(post_ext, post_at))
+
+ for tag, (a, b) in (("closed", closed), ("open shells", shells)):
+ depth, frac = penetration_depth(a, b)
+ assert depth == pytest.approx(0.05, abs=1e-3), f"{tag}: 50 mm of interpenetration must be measured"
+ assert frac > 0.05, tag
+
+ scene = trimesh.Scene()
+ scene.add_geometry(shells[0], node_name="Base", geom_name="Base")
+ scene.add_geometry(shells[1], node_name="Post", geom_name="Post")
+ glb = tmp_path / "open_shells.glb"
+ scene.export(str(glb))
+ r = check_connectivity(glb)
+ assert not r.passed and any("interpenetrate" in f.message for f in r.errors)