Official Codebase of 3DCodeVerse
Python
planner repair 2: revolute limits written in degrees become radians, not a free axle
gaoypengfc246358c279
diff --git a/harness/codeverse/contracts/plan.py b/harness/codeverse/contracts/plan.pyindex b3e3c7a6..1d8bda3d 100644--- a/harness/codeverse/contracts/plan.py+++ b/harness/codeverse/contracts/plan.py@@ -48,6 +48,13 @@ class AcceptanceItem(BaseModel):#: entirely", never a 3 mm rounding.SUBPART_SLACK_M = 0.01SUBPART_REL_SLACK = 0.15+#: A ``revolute`` range over 2π whose limits both sit inside ±``DEGREES_MAX_ABS`` and whose span is+#: at least ``DEGREES_MIN_SPAN`` was written in DEGREES (door ``0..90``, lid ``-180..0``): 30 rad is+#: nearly five turns, which no hinge is planned in. A span between 2π and 30 (``0..4π``, ``0..6.5``,+#: ``0..10``) is read as radians — an implausible hinge either way — and becomes ``continuous``+#: (``ArticulatedPlan._normalise_raw``).+DEGREES_MAX_ABS = 360.0+DEGREES_MIN_SPAN = 30.0class SubPartPlan(BaseModel):@@ -189,14 +196,20 @@ class JointPlan(BaseModel):return self+def _looks_like_degrees(lower: float, upper: float) -> bool:+ """Revolute limits the planner wrote in degrees: inside ±360 with a span of at least 30 (see the constants)."""+ return abs(lower) <= DEGREES_MAX_ABS and abs(upper) <= DEGREES_MAX_ABS and upper - lower >= DEGREES_MIN_SPAN++class ArticulatedPlan(StaticPlan):root_link: strjoints: list[JointPlan] = Field(min_length=1)normalisations: list[str] = Field(default_factory=list,description="filled by the harness, leave empty: automatic corrections applied to the planner's answer "- "before validation (a sub-part promoted to a link because a joint moves it, a revolute joint "- "with a > 2π range made continuous, a parent bbox grown around a sub-part)")+ "before validation (a sub-part promoted to a link because a joint moves it, revolute limits "+ "written in degrees converted to radians or a > 2π radian range made continuous, a parent "+ "bbox grown around a sub-part)")@model_validator(mode="before")@classmethod@@ -207,7 +220,14 @@ class ArticulatedPlan(StaticPlan):1. a joint whose ``parent`` / ``child`` names a SUB-PART: the sub-part is promoted to atop-level part (``attach_to`` = its former parent) — anything a joint moves is a link;- 2. a ``revolute`` joint whose range exceeds 2π becomes ``continuous`` (limits dropped);+ 2. a ``revolute`` joint whose range exceeds 2π: with both limits inside ±360 and a span of+ at least 30 the planner wrote DEGREES (door ``0..90``, lid ``-180..0`` — the template says+ rad), so the limits and a ``rest`` inside them are converted with ``math.radians`` and the+ joint stays ``revolute``; any other span over 2π (``0..4π``, ``-4π..4π``) becomes+ ``continuous`` (limits dropped). The band 2π < span < 30 (``0..6.5``, ``0..10``) is+ ambiguous and is read as radians: a wrong ``continuous`` there is what the run got before,+ while a wrong degrees reading would squeeze a full turn into a 6° hinge without a trace.+ A converted range that is still over 2π (``-360..360``) falls through to ``continuous``;3. a sub-part sticking out of its parent's bbox by more than the slack: the parent bboxgrows to enclose it (planner boxes are design intent, not measurements).Every repair is recorded in ``normalisations`` so the record shows what the planner@@ -246,13 +266,29 @@ class ArticulatedPlan(StaticPlan):kept.append(child)part["children"] = kept- # 2. revolute joints with a > 2π range → continuous+ # 2. revolute joints with a > 2π range: degrees written for radians → radians; a radian+ # range over 2π → continuousfor j in joints:+ if j.get("type") != "revolute":+ continuetry:- span = float(j.get("upper", 0.0)) - float(j.get("lower", 0.0))+ lower, upper = float(j.get("lower", 0.0)), float(j.get("upper", 0.0))except (TypeError, ValueError):continue- if j.get("type") == "revolute" and span > 2 * math.pi + 1e-6:+ if upper - lower <= 2 * math.pi + 1e-6:+ continue+ if _looks_like_degrees(lower, upper):+ j.update(lower=math.radians(lower), upper=math.radians(upper))+ try:+ rest = float(j.get("rest", 0.0))+ except (TypeError, ValueError):+ rest = None+ if rest is not None and lower - 1e-9 <= rest <= upper + 1e-9:+ j["rest"] = math.radians(rest)+ notes.append(f"joint {j.get('name')}: limits looked like degrees ({lower:g}..{upper:g}) → radians")+ lower, upper = float(j["lower"]), float(j["upper"])+ span = upper - lower+ if span > 2 * math.pi + 1e-6:j.update(type="continuous", lower=0.0, upper=0.0, rest=0.0)notes.append(f"joint {j.get('name')}: revolute range {span:.2f} rad > 2π → continuous")diff --git a/harness/tests/core/test_plan_normalise.py b/harness/tests/core/test_plan_normalise.pyindex 7882d840..39ae34b6 100644--- a/harness/tests/core/test_plan_normalise.py+++ b/harness/tests/core/test_plan_normalise.py@@ -44,14 +44,61 @@ def test_a_joint_that_moves_a_sub_part_promotes_it_to_a_link():assert any("promoted sub-part" in n and "UpperSash" in n for n in p.normalisations)-def test_a_revolute_joint_over_two_pi_becomes_continuous():- d = _raw()+def _revolute(d: dict, lower: float, upper: float, rest: float = 0.0) -> dict:j = d["joints"][0]- j.update(type="revolute", lower=0.0, upper=4 * math.pi, rest=0.0)+ j.update(type="revolute", lower=lower, upper=upper, rest=rest)+ return j+++def _joint(p: ArticulatedPlan, j: dict):+ return next(x for x in p.joints if x.name == j["name"])++[email protected]("lower,upper", [(0.0, 4 * math.pi), (-4 * math.pi, 4 * math.pi), (0.0, 6.5), (0.0, 10.0)])+def test_a_revolute_joint_over_two_pi_in_radians_becomes_continuous(lower: float, upper: float):+ """A radian range over 2π — including the ambiguous 2π..30 band (0..6.5, 0..10), which is read+ as radians (contracts/plan.py DEGREES_MIN_SPAN) — is a free axle, never a degrees conversion."""+ d = _raw()+ j = _revolute(d, lower, upper)p = ArticulatedPlan.model_validate(d)- fixed = next(x for x in p.joints if x.name == j["name"])+ fixed = _joint(p, j)assert fixed.type == "continuous" and fixed.lower == fixed.upper == 0.0assert any("> 2π → continuous" in n for n in p.normalisations)+ assert not any("degrees" in n for n in p.normalisations)+++def test_a_door_planned_in_degrees_stays_a_quarter_turn_hinge():+ """The review's case: `0..90` is degrees for radians; the old repair made it a free 360° axle+ and the sweep drove the door through its cabinet. It stays revolute, 0..1.5708 rad."""+ d = _raw()+ j = _revolute(d, 0.0, 90.0)+ p = ArticulatedPlan.model_validate(d)+ fixed = _joint(p, j)+ assert fixed.type == "revolute"+ assert fixed.lower == 0.0 and fixed.upper == pytest.approx(1.5708, abs=1e-4) and fixed.rest == 0.0+ assert any("looked like degrees (0..90) → radians" in n for n in p.normalisations)+ assert not any("continuous" in n for n in p.normalisations)++[email protected]("lower,upper,rest", [(-180.0, 0.0, 0.0), (0.0, 90.0, 90.0), (-45.0, 45.0, 0.0), (0.0, 30.0, 0.0)])+def test_degree_shaped_limits_and_rest_are_converted_together(lower: float, upper: float, rest: float):+ d = _raw()+ j = _revolute(d, lower, upper, rest)+ p = ArticulatedPlan.model_validate(d)+ fixed = _joint(p, j)+ assert fixed.type == "revolute"+ assert fixed.lower == pytest.approx(math.radians(lower)) and fixed.upper == pytest.approx(math.radians(upper))+ assert fixed.rest == pytest.approx(math.radians(rest))+ assert sum("looked like degrees" in n for n in p.normalisations) == 1+++def test_a_two_turn_degree_range_falls_through_to_continuous():+ d = _raw()+ j = _revolute(d, -360.0, 360.0)+ p = ArticulatedPlan.model_validate(d)+ fixed = _joint(p, j)+ assert fixed.type == "continuous"+ assert any("looked like degrees" in n for n in p.normalisations) and any("> 2π → continuous" in n for n in p.normalisations)def test_a_sub_part_outside_its_parent_grows_the_parent_bbox():