Skip to content

Official Codebase of 3DCodeVerse

Python

Joint sweep: one float instead of an unread SweepSummary; sweep constants stated once; finish three truncated docstrings

gaoypengb82c55402b13
diff --git a/harness/codeverse3d/languages/urdf/__init__.py b/harness/codeverse3d/languages/urdf/__init__.py
index 107240f7..590ab8fb 100644
--- a/harness/codeverse3d/languages/urdf/__init__.py
+++ b/harness/codeverse3d/languages/urdf/__init__.py
@@ -41,7 +41,11 @@ from codeverse3d.spatial.joints_model import (
parse_joint,
parse_origin,
)
-from codeverse3d.spatial.joints_sweep import sweep_collisions, sweep_findings
+from codeverse3d.spatial.joints_sweep import (
+ REST_PENETRATION_MAX_M,
+ sweep_collisions,
+ sweep_findings,
+)
from codeverse3d.workspace import ArtifactStage, Workspace
# ===================================================================== consistency
@@ -621,7 +625,6 @@ def write_skeleton(ws: Workspace, plan: ArticulatedPlan) -> list[Path]:
# ===================================================================== runtime
WRAPPER = Path(__file__).resolve().parent.parent / "wrappers" / "run_bpy_links.py"
-REST_PENETRATION_MAX_M = 0.005
FK_TOL_M = 0.001
#: every canonical artifact this build owns, staged together
@@ -722,12 +725,12 @@ class UrdfBlenderRuntime(RuntimeLayout):
res = BuildResult(ok=True, language=self.language.value, glb_path=str(art / "object.glb"), extra_paths=extra,
stdout_tail=wrapped.stdout_tail, stderr_tail=wrapped.stderr_tail,
duration_ms=int((time.time() - t0) * 1000), census=census)
- if report.summary.rest_max_penetration_m > REST_PENETRATION_MAX_M:
+ if report.rest_max_penetration_m > REST_PENETRATION_MAX_M:
worst = [f for f in findings if f.severity == "error"]
res.ok = False
res.error_type = "RestPenetration"
res.error_file = "src/model.py"
- res.error_message = (f"links interpenetrate by {report.summary.rest_max_penetration_m*1000:.1f} mm at the rest pose "
+ res.error_message = (f"links interpenetrate by {report.rest_max_penetration_m*1000:.1f} mm at the rest pose "
f"(max {REST_PENETRATION_MAX_M*1000:.0f} mm):\n" +
"\n".join(f"- {f.as_line()}" for f in worst[:8]))
res.glb_path = None # the fresh GLB is NOT published on a failed build
diff --git a/harness/codeverse3d/spatial/contract.py b/harness/codeverse3d/spatial/contract.py
index 7749a4cc..e8b84971 100644
--- a/harness/codeverse3d/spatial/contract.py
+++ b/harness/codeverse3d/spatial/contract.py
@@ -53,7 +53,7 @@ PLACEHOLDER_MIN_FILL = 1.0 - 1e-3
# --------------------------------------------------------------------------- frames
def frame_label(language: str) -> str:
- """Short frame tag for fix hints, e.g."""
+ """Short frame tag for fix hints, e.g. ``blender frame: Z-up, -Y front``."""
if language_frame(language) is Frame.Z_UP_NEG_Y_FRONT:
return f"{language} frame: Z-up, -Y front"
return f"{language} frame: Y-up, +Z front" if language else "GLB frame: Y-up, +Z front"
diff --git a/harness/codeverse3d/spatial/joints_sweep.py b/harness/codeverse3d/spatial/joints_sweep.py
index 5f8cf006..e481a462 100644
--- a/harness/codeverse3d/spatial/joints_sweep.py
+++ b/harness/codeverse3d/spatial/joints_sweep.py
@@ -24,6 +24,9 @@ from codeverse3d.workspace import Workspace
log = logging.getLogger(__name__)
SWEEP_GATE = "joint_sweep"
+#: rest penetration beyond this fails the URDF build; a moving child must be this close to its parent
+REST_PENETRATION_MAX_M = 0.005
+HINGE_CLEARANCE_M = 0.01
# ------------------------------------------------------------------ report types
@@ -50,26 +53,13 @@ class PoseReport(BaseModel):
q: dict[str, float]
overlaps: list[Overlap] = Field(default_factory=list)
floating: list[FloatingLink] = Field(default_factory=list)
- n_contacts: int = 0
-
-
-class SweepSummary(BaseModel):
- n_poses: int
- n_links: int
- max_penetration_m: float = 0.0
- worst_pose: str = ""
- rest_max_penetration_m: float = 0.0
- overlapping_poses: list[str] = Field(default_factory=list)
- floating_links: list[str] = Field(default_factory=list, description="floating in ANY sampled pose")
- floating_at_rest: list[str] = Field(default_factory=list)
- link_islands: dict[str, int] = Field(default_factory=dict)
class SweepReport(BaseModel):
tol_m: float
contact_gap_m: float
per_pose: list[PoseReport]
- summary: SweepSummary
+ rest_max_penetration_m: float = Field(default=0.0, description="deepest rest-pose or rigid (welded) overlap")
class MotionCheck(BaseModel):
@@ -89,7 +79,7 @@ def sweep_collisions(
*,
tol_m: float = 0.002,
contact_gap_m: float = CONTACT_GAP_M,
- hinge_clearance_m: float = 0.01,
+ hinge_clearance_m: float = HINGE_CLEARANCE_M,
) -> SweepReport:
"""Collide every link pair in every pose."""
names = robot.meshed_links()
@@ -97,7 +87,6 @@ def sweep_collisions(
raise UrdfError("sweep_collisions: robot has no link meshes")
bodies = {n: collide.LinkBody(n, robot.links[n].mesh) for n in names} # type: ignore[arg-type]
per_pose: list[PoseReport] = []
- islands = {n: len(bodies[n].islands) for n in names}
comps = collide.components(list(robot.links), {(j.parent, j.child) for j in robot.joints.values() if j.type == "fixed"})
rigid_group = {n: i for i, comp in enumerate(comps) for n in comp}
labels = [pose_label(robot, q) for q in poses]
@@ -108,7 +97,6 @@ def sweep_collisions(
for n, body in bodies.items():
body.set_pose(T[n])
overlaps: list[Overlap] = []
- touching: set[tuple[str, str]] = set()
nearest: dict[str, dict[str, float]] = {n: {} for n in names}
for i, a in enumerate(names):
for b in names[i + 1 :]:
@@ -131,8 +119,6 @@ def sweep_collisions(
else:
dist = collide.distance(ba, bb)
nearest[a][b] = nearest[b][a] = dist
- if dist <= contact_gap_m:
- touching.add((a, b))
floating: list[FloatingLink] = []
if pose_label(robot, q) == "rest":
for n in names:
@@ -151,33 +137,15 @@ def sweep_collisions(
if d > thr:
floating.append(FloatingLink(link=n, nearest=j.parent, joint_type=j.type,
gap_m=round(d, 6) if math.isfinite(d) else math.inf))
- per_pose.append(PoseReport(label=labels[idx], q=dict(q), overlaps=overlaps, floating=floating,
- n_contacts=len(touching)))
-
- summary = _summarise(per_pose, len(names), islands)
- return SweepReport(tol_m=tol_m, contact_gap_m=contact_gap_m, per_pose=per_pose, summary=summary)
+ per_pose.append(PoseReport(label=labels[idx], q=dict(q), overlaps=overlaps, floating=floating))
-
-def _summarise(per_pose: list[PoseReport], n_links: int, islands: dict[str, int]) -> SweepSummary:
- s = SweepSummary(n_poses=len(per_pose), n_links=n_links, link_islands=islands)
- floating_any: set[str] = set()
- for pr in per_pose:
- for o in pr.overlaps:
- if o.depth_m > s.max_penetration_m:
- s.max_penetration_m, s.worst_pose = o.depth_m, pr.label
- if pr.label == "rest" or o.rigid:
- s.rest_max_penetration_m = max(s.rest_max_penetration_m, o.depth_m)
- if pr.overlaps:
- s.overlapping_poses.append(pr.label)
- floating_any.update(f.link for f in pr.floating)
- if pr.label == "rest":
- s.floating_at_rest = [f.link for f in pr.floating]
- s.floating_links = sorted(floating_any)
- return s
+ rest_max = max((o.depth_m for pr in per_pose for o in pr.overlaps if pr.label == "rest" or o.rigid), default=0.0)
+ return SweepReport(tol_m=tol_m, contact_gap_m=contact_gap_m, per_pose=per_pose, rest_max_penetration_m=rest_max)
# ------------------------------------------------------------------ gate findings
-def sweep_findings(report: SweepReport, *, rest_max_m: float = 0.005, hinge_clearance_m: float = 0.01) -> list[GateFinding]:
+def sweep_findings(report: SweepReport, *, rest_max_m: float = REST_PENETRATION_MAX_M,
+ hinge_clearance_m: float = HINGE_CLEARANCE_M) -> list[GateFinding]:
"""Turn a sweep report into gate findings: rest penetration beyond ``rest_max_m``, moved-pose overlaps, and floating links beyond ``hinge_clearance_m``."""
gate = SWEEP_GATE
out: list[GateFinding] = []
@@ -339,7 +307,8 @@ _DIRS: dict[str, tuple[float, float, float]] = {
def motion_direction_check(robot: Robot, joint: str, expected: str, *, probe: float | None = None) -> MotionCheck:
"""Does the child's centroid move along ``expected`` (``'+z'``, ``'-y'``, ``'up'``,
- ``'front'``, ``'open_up'`` ...) when ``joint`` moves positively from rest? Revolute joints"""
+ ``'front'``, ``'open_up'`` ...) when ``joint`` moves positively from rest? Revolute joints
+ are judged by the child centroid's swing about the pivot, so a wrong sign is reported with the corrected axis."""
key = expected.strip().lower()
if key not in _DIRS:
raise UrdfError(f"motion_direction_check: unknown direction {expected!r}; use one of {sorted(_DIRS)}")
diff --git a/harness/codeverse3d/spatial/measure.py b/harness/codeverse3d/spatial/measure.py
index 5a25ace5..77675ba9 100644
--- a/harness/codeverse3d/spatial/measure.py
+++ b/harness/codeverse3d/spatial/measure.py
@@ -142,7 +142,7 @@ def _node_has_geometry(scene: trimesh.Scene, node: str) -> bool:
def effective_top_nodes(scene: trimesh.Scene) -> list[str]:
"""Direct children of the scene root, descending through single wrapper nodes that carry no
- geometry themselves (a three.js root Group, a URDF ``<robot>`` node) so that parts are"""
+ geometry themselves (a three.js root Group, a URDF ``<robot>`` node) so that parts are the top nodes."""
tops = _children(scene, scene.graph.base_frame)
seen = 0
while len(tops) == 1 and not _node_has_geometry(scene, tops[0]) and _children(scene, tops[0]) and seen < 4:
diff --git a/harness/tests/urdf_joints/test_sweep.py b/harness/tests/urdf_joints/test_sweep.py
index 5eb039ed..39763942 100644
--- a/harness/tests/urdf_joints/test_sweep.py
+++ b/harness/tests/urdf_joints/test_sweep.py
@@ -23,8 +23,8 @@ def test_rest_penetration_and_floating(tmp_path):
urdf.write_text(text)
r = load_urdf(urdf, meshes)
rep = sweep_collisions(r, [{}])
- assert 0.028 < rep.summary.rest_max_penetration_m < 0.032
- assert rep.summary.floating_at_rest == ["handle"]
+ assert 0.028 < rep.rest_max_penetration_m < 0.032
+ assert [f.link for f in rep.per_pose[0].floating] == ["handle"]
fl = rep.per_pose[0].floating[0]
# attachment is judged against the PARENT link (door), not the grounded body
assert fl.nearest == "door" and fl.joint_type == "fixed" and 0.03 < fl.gap_m < 0.06