diff --git a/agent_context/topics/expansion-engine/expansion-engine.md b/agent_context/topics/expansion-engine/expansion-engine.md
index 84c13178e..14eca61ae 100644
--- a/agent_context/topics/expansion-engine/expansion-engine.md
+++ b/agent_context/topics/expansion-engine/expansion-engine.md
@@ -100,14 +100,31 @@ cross-group frame through the public marker layer. Task assets and any collectio
process orchestration remain outside the replay core.
`embodichain expansion preview --input` reads confirmed job outputs through `_preview_data.py`,
-then replays full measured states using `_preview_render.py` and a loopback-only
-`_preview_server.py`. Inputs retain their original evidence; no collection or
+then replays full measured states using `_preview_render.py` and
+`_preview_server.py`. Browser replay defaults to the DexSim WebRTC observer
+and its owner-thread application. `_frontend/` owns the packaged preview
+HTML, CSS and JavaScript and the offline distribution report template.
+Video is separate from the HTTP replay state; authenticated commands queue only
+validated replay controls. Inputs retain their original evidence; no collection or
physics stepping occurs. It accepts one URDF robot and recorded rigid visual
objects (including file Mesh/USD via native visual loading), with fixed layouts;
unsupported geometry is rejected. Constant static backgrounds remain single
-instances. Space filters group
-input directories; missing motion annotations remain explicit. HTTP publishes a
-single immutable image/state snapshot; WebRTC is follow-up work. FK and episode
+instances. The solid robot and objects always follow the explicit reference,
+or the first input recording when a reference is absent. The play button and
+Space key toggle playback. Candidate buttons toggle individual ghosts. See the
+[UR5 layout and grasp tutorial](../../../scripts/tutorials/expansion/ur5_pick_place/README.md)
+for collection and replay. Filters preserve each toggle and playback of the solid
+trajectory. Space filters group input directories and missing motion annotations
+remain explicit. `_preview_deviations.py` reuses recorded world FK and gripper
+calibration for TCP errors. At each reference time it reports the minimum, mean
+and maximum across recordings still in range for TCP translation distance,
+TCP rotation angle and each episode's worst joint deviation. `_preview_heatmap.py`
+bins the measured first-frame XY of each rigid target in an `object_pose` input
+episode's `joint_scene.pose_changes`. Each axis fits the object's measured starts
+independently with half a bin of padding at either end, or a 1 mm window for a
+constant coordinate. The recipe's selected motion operator takes
+precedence over the job-level operator. The explicit reference belongs to replay.
+Static chart payloads and HTTP replay metadata are independent of the encoded video. FK and episode
budgets bound preload, controls are queued for the render owner, and native
resources/ports close on exit. See the example's `ghost-preview.md` for usage.
diff --git a/agent_context/topics/expansion-engine/workloads.md b/agent_context/topics/expansion-engine/workloads.md
index c4396fe6f..7e9e1b758 100644
--- a/agent_context/topics/expansion-engine/workloads.md
+++ b/agent_context/topics/expansion-engine/workloads.md
@@ -228,6 +228,16 @@ preparation/conditional generation and `RobotFeasibilityBackend`/`plan_task_mode
it creates no physics World and owns only the isolated collision-query resources.
`layout` and `task_mode` are explicit, validated task declarations. Root layout
variables currently use XY only; selected nested members retain declared XY/yaw.
+Placement checks group bounds on the main support. For a bounded leaf root
+overhanging that rectangle, preparation can verify the projected centroid
+against contact patches at the authored height on the named support. Full
+projected extents still determine clearance to other groups.
+All layout-screened operators use native convex shapes and primitives. Physical
+triangle colliders on rigid objects and robot links are decomposed in their scaled local frame for the isolated
+Newton phase/path query. V-HACD handles volumetric concavities; planar meshes
+retain their footprint and holes. `scene-first-checks.json` records part counts
+by physical collider in `mesh_convex_parts`. Rollout contacts use the
+simulator's original physical shapes.
Homogeneous replicas support rolling W times B collection, including eight rows.
Preparation snapshots the common prototype geometry, installation and nominal
full-joint planning start before any row executes. The feasibility adapter uses
diff --git a/agent_context/topics/scene-expansion/feasibility.md b/agent_context/topics/scene-expansion/feasibility.md
index 27596fb74..949cc1525 100644
--- a/agent_context/topics/scene-expansion/feasibility.md
+++ b/agent_context/topics/scene-expansion/feasibility.md
@@ -39,12 +39,16 @@ mimic constraints. Hypothetical whole-robot FK supplies solver-root transforms;
using live roots for projected torso/base states is incorrect. Complete path
screening uses explicit physical geometry and phase contact permissions.
`collision.py` lazily builds a Newton `CollisionPipeline` over isolated query
-states. The physical extractor preserves native convex shapes, includes the
-global ground, and rejects triangle meshes, SDFs, non-robot articulations and
-unsupported robot asset scales/formats. No collider is silently convexified.
+states. The standalone adapter uses native convex shapes, primitives and the
+global ground. Triangle meshes, SDFs, non-robot articulations and unsupported
+robot scales require an explicit adapter. The layout-screened collector cooks
+physical triangle colliders on rigid objects and robot links into convex parts.
+Permissions and reported failures retain the original physical collider IDs.
Halfspaces use analytic physical-shape support. Phase masks do not mutate live
-filters, and the query never advances the simulation clock. Shape/pair/contact
-storage is bounded; saturation and nonfinite results remain unverified.
+filters, and the query never advances the simulation clock. Explicit pair
+lists and phase masks contain only eligible pairs. Batched rows share a
+bounded pair/contact budget and run in chunks when needed. Callers can set a
+separate shape quota. Saturation and nonfinite results remain unverified.
The host closes the owned query arrays/graphs after every attempt.
Query transforms are rebased before composing local offsets; precision budgets
diff --git a/agent_context/topics/scene-expansion/scene-expansion.md b/agent_context/topics/scene-expansion/scene-expansion.md
index ae6adb910..4d90f9aa5 100644
--- a/agent_context/topics/scene-expansion/scene-expansion.md
+++ b/agent_context/topics/scene-expansion/scene-expansion.md
@@ -17,6 +17,11 @@ must also be anchored. Anchored roots reserve space before unanchored groups
are sampled. Invalid domains reject before generation; unsupported
geometry returns budget exhaustion rather than silently moving the anchors.
The source/policy remain reusable; empty conditions preserve the old stream.
+A rigid leaf root with an explicit XY offset range may overhang the primary
+support rectangle. Preparation verifies a bounded translation range on the
+named mesh using faces near the authored bottom, the projected mass centroid
+and the convex hull of the contact patches. Fixed neighbors retain their full
+projected clearance bounds. Saved layouts revalidate these offsets on load.
`SimulationSceneExpansionHost` prepares/restores ordered rows with independent
validation. Offline scheduling and confirmed persistence belong to the
[Expansion Engine](../expansion-engine/expansion-engine.md).
diff --git a/embodichain/cli/_task_catalog.py b/embodichain/cli/_task_catalog.py
index 201fc657f..bef906786 100644
--- a/embodichain/cli/_task_catalog.py
+++ b/embodichain/cli/_task_catalog.py
@@ -30,6 +30,8 @@
from pathlib import Path, PurePosixPath
from typing import Any
+from embodichain.utils.config_paths import resolve_config_path
+
try:
from importlib.resources.abc import Traversable
except ModuleNotFoundError:
@@ -316,10 +318,10 @@ def _backend(config: Mapping[str, Any], resource: Traversable) -> str | None:
if isinstance(environment, Mapping) and isinstance(
environment.get("component"), str
):
- # Components are normally siblings or ancestors. Path resources support
- # this without importing simulator-dependent configuration decoders.
if isinstance(resource, Path):
- component = resource.parent / environment["component"]
+ component = resolve_config_path(
+ environment["component"], base_dir=resource.parent
+ )
if not component.is_file():
raise ValueError(f"Missing environment component: {component}")
return _load_mapping(component).get("physics")
diff --git a/embodichain/cli/expansion.py b/embodichain/cli/expansion.py
index d3774a65d..955de3b06 100644
--- a/embodichain/cli/expansion.py
+++ b/embodichain/cli/expansion.py
@@ -82,7 +82,10 @@ def main(argv: Sequence[str] | None = None) -> int:
)
command.add_argument(
"--reference",
- help="Optional result directory containing exactly one reference episode",
+ help=(
+ "Job directory containing one confirmed original trajectory for solid "
+ "playback. Defaults to the first loaded input recording as a comparison."
+ ),
)
command.add_argument(
"--robot-asset", help="Matching URDF override if recorded asset paths moved"
@@ -93,7 +96,10 @@ def main(argv: Sequence[str] | None = None) -> int:
)
command.add_argument("--gpu", type=int, default=0)
command.add_argument(
- "--port", type=int, default=8769, help="Loopback port; 0 selects a free port"
+ "--port",
+ type=int,
+ default=8769,
+ help="Browser preview port; 0 selects an available port",
)
command.add_argument("--max-episodes", type=int, default=32)
command.add_argument(
diff --git a/embodichain/compute/geometry/_hierarchical.py b/embodichain/compute/geometry/_hierarchical.py
index 88587d293..1be9312ab 100644
--- a/embodichain/compute/geometry/_hierarchical.py
+++ b/embodichain/compute/geometry/_hierarchical.py
@@ -213,6 +213,7 @@ def _sample_hierarchy(
movable_roots: tuple[int, ...] | None = None,
root_offset_bounds: dict[int, torch.Tensor] | None = None,
anchored_roots: tuple[int, ...] = (),
+ root_support_offsets: dict[int, torch.Tensor] | None = None,
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
"""One proposal per ordinal, with no per-member retry loops."""
local = [matrix.expand(count, 4, 4).clone() for matrix in relative]
@@ -300,6 +301,7 @@ def _sample_hierarchy(
world.append(
local[node] if parent < 0 else _matrix_product(world[parent], local[node])
)
+ root_support_offsets = root_support_offsets or {}
root_boxes, deltas = [], {}
fixed_boxes = {}
if randomize_groups and movable_roots is not None:
@@ -316,9 +318,13 @@ def _sample_hierarchy(
box = torch.stack(
(points[:, :, :2].amin(1), points[:, :, :2].amax(1)), dim=1
)
- valid &= (box[:, 0] >= table[0] - 1e-10).all(-1) & (
- box[:, 1] <= table[1] + 1e-10
- ).all(-1)
+ if root in root_support_offsets:
+ low, high = root_support_offsets[root]
+ valid &= (low <= 0).all() & (high >= 0).all()
+ else:
+ valid &= (box[:, 0] >= table[0] - 1e-10).all(-1) & (
+ box[:, 1] <= table[1] + 1e-10
+ ).all(-1)
for other in root_boxes:
valid &= (
torch.maximum(
@@ -354,7 +360,10 @@ def _sample_hierarchy(
key=key, start=start, count=count, width=3, device=relative.device
)
half = (box[:, 1] - box[:, 0]) / 2
- center_region = torch.stack((table[0] + half, table[1] - half), dim=1)
+ if root in root_support_offsets:
+ center_region = box.mean(1)[:, None] + root_support_offsets[root]
+ else:
+ center_region = torch.stack((table[0] + half, table[1] - half), dim=1)
if root_offset_bounds and root in root_offset_bounds:
offset = root_offset_bounds[root]
center_region = torch.stack(
@@ -392,9 +401,13 @@ def _sample_hierarchy(
(count, 2), dtype=relative.dtype, device=relative.device
)
box = box + delta[:, None, :]
- valid &= (box[:, 0] >= table[0] - 1e-10).all(-1) & (
- box[:, 1] <= table[1] + 1e-10
- ).all(-1)
+ if root in root_support_offsets:
+ low, high = root_support_offsets[root]
+ valid &= (delta >= low - 1e-10).all(-1) & (delta <= high + 1e-10).all(-1)
+ else:
+ valid &= (box[:, 0] >= table[0] - 1e-10).all(-1) & (
+ box[:, 1] <= table[1] + 1e-10
+ ).all(-1)
for other in root_boxes:
valid &= (
torch.maximum(box[:, 0] - other[:, 1], other[:, 0] - box[:, 1]).amax(-1)
diff --git a/embodichain/compute/geometry/_warp/batch_contact_query.py b/embodichain/compute/geometry/_warp/batch_contact_query.py
index af31b4fa4..3083890b2 100644
--- a/embodichain/compute/geometry/_warp/batch_contact_query.py
+++ b/embodichain/compute/geometry/_warp/batch_contact_query.py
@@ -21,6 +21,8 @@
import warp as wp
+from .contact_query import _pair_slot
+
__all__: list[str] = []
@@ -36,6 +38,7 @@ def contact_summary(
margin1: wp.array(dtype=float),
shape_body: wp.array(dtype=wp.int32),
poses: wp.array(dtype=wp.transform),
+ pair_keys: wp.array(dtype=wp.int64),
active: wp.array(dtype=wp.int32),
width: int,
batches: int,
@@ -74,7 +77,10 @@ def contact_summary(
else:
row = a // width
x, y = a % width, b % width
- if active[x * width + y] != 0:
+ slot = _pair_slot(pair_keys, width, x, y)
+ if slot < 0:
+ wp.atomic_min(output, row, 2, 0.0)
+ elif active[slot] != 0:
p0 = wp.transform_point(poses[shape_body[a]], point0[index])
p1 = wp.transform_point(poses[shape_body[b]], point1[index])
distance = (
@@ -85,6 +91,4 @@ def contact_summary(
else:
wp.atomic_min(output, row, 0, distance)
if distance <= required + tolerance[row]:
- wp.atomic_min(
- output, row, 1, float(wp.min(x, y) * width + wp.max(x, y))
- )
+ wp.atomic_min(output, row, 1, float(slot))
diff --git a/embodichain/compute/geometry/_warp/contact_query.py b/embodichain/compute/geometry/_warp/contact_query.py
index 70f808542..37c22ea3c 100644
--- a/embodichain/compute/geometry/_warp/contact_query.py
+++ b/embodichain/compute/geometry/_warp/contact_query.py
@@ -23,6 +23,16 @@
__all__ = []
+@wp.func
+def _pair_slot(keys: wp.array(dtype=wp.int64), width: int, a: int, b: int):
+ key = wp.int64(wp.min(a, b)) * wp.int64(width) + wp.int64(wp.max(a, b))
+ slot = wp.lower_bound(keys, key)
+ if slot < keys.shape[0]:
+ if keys[slot] == key:
+ return slot
+ return int(-1)
+
+
@wp.kernel(enable_backward=False)
def _contact_summary(
count: wp.array(dtype=wp.int32),
@@ -35,6 +45,7 @@ def _contact_summary(
margin1: wp.array(dtype=float),
shape_body: wp.array(dtype=wp.int32),
poses: wp.array(dtype=wp.transform),
+ pair_keys: wp.array(dtype=wp.int64),
active: wp.array(dtype=wp.int32),
shapes: int,
required: float,
@@ -65,7 +76,11 @@ def _contact_summary(
a, b = first[index], second[index]
if a < 0 or b < 0 or a >= shapes or b >= shapes:
wp.atomic_min(output, 3, 0.0)
- elif active[a * shapes + b] != 0:
+ return
+ slot = _pair_slot(pair_keys, shapes, a, b)
+ if slot < 0:
+ wp.atomic_min(output, 3, 0.0)
+ elif active[slot] != 0:
p0 = wp.vec3(point0[index][0], point0[index][1], point0[index][2])
p1 = wp.vec3(point1[index][0], point1[index][1], point1[index][2])
owner0, owner1 = shape_body[a], shape_body[b]
@@ -79,6 +94,4 @@ def _contact_summary(
else:
wp.atomic_min(output, 0, distance)
if distance <= required + tolerance[0]:
- wp.atomic_min(
- output, 1, float(wp.min(a, b) * shapes + wp.max(a, b))
- )
+ wp.atomic_min(output, 1, float(slot))
diff --git a/embodichain/lab/expansion/_distribution.py b/embodichain/lab/expansion/_distribution.py
index cb2ec8643..608f31364 100644
--- a/embodichain/lab/expansion/_distribution.py
+++ b/embodichain/lab/expansion/_distribution.py
@@ -26,11 +26,14 @@
from collections.abc import Mapping
from copy import deepcopy
import hashlib
+from importlib.resources import files
import json
from pathlib import Path
from typing import Any
import numpy as np
+from . import _frontend
+
__all__: list[str] = []
@@ -345,7 +348,9 @@ def _run(
(output / "summary.json").write_text(
json.dumps(report, ensure_ascii=False, indent=2, allow_nan=False)
)
- template = Path(__file__).with_name("_distribution.html").read_text()
+ template = (
+ files(_frontend).joinpath("distribution.html").read_text(encoding="utf-8")
+ )
(output / "report.html").write_text(
template.replace(
"__REPORT__",
diff --git a/embodichain/lab/expansion/_frontend/__init__.py b/embodichain/lab/expansion/_frontend/__init__.py
new file mode 100644
index 000000000..7dcd44e46
--- /dev/null
+++ b/embodichain/lab/expansion/_frontend/__init__.py
@@ -0,0 +1,49 @@
+# ----------------------------------------------------------------------------
+# Copyright (c) 2021-2026 DexForce Technology Co., Ltd.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ----------------------------------------------------------------------------
+
+"""Packaged browser assets for expansion playback and distribution reports."""
+
+from __future__ import annotations
+
+from importlib.resources import files
+
+__all__: list[str] = []
+
+
+def _preview_assets() -> dict[str, tuple[str, bytes]]:
+ """Load same-origin assets for the native WebRTC preview."""
+ directory = files(__name__)
+ assets = {
+ "/": (
+ "text/html; charset=utf-8",
+ directory.joinpath("preview.html").read_bytes(),
+ ),
+ "/preview.css": (
+ "text/css; charset=utf-8",
+ directory.joinpath("preview.css").read_bytes(),
+ ),
+ }
+ for name in (
+ "preview-text.js",
+ "preview-controls.js",
+ "preview-charts.js",
+ "preview-heatmap.js",
+ ):
+ assets[f"/{name}"] = (
+ "text/javascript; charset=utf-8",
+ directory.joinpath(name).read_bytes(),
+ )
+ return assets
diff --git a/embodichain/lab/expansion/_distribution.html b/embodichain/lab/expansion/_frontend/distribution.html
similarity index 100%
rename from embodichain/lab/expansion/_distribution.html
rename to embodichain/lab/expansion/_frontend/distribution.html
diff --git a/embodichain/lab/expansion/_frontend/preview-charts.js b/embodichain/lab/expansion/_frontend/preview-charts.js
new file mode 100644
index 000000000..e1a42ad18
--- /dev/null
+++ b/embodichain/lab/expansion/_frontend/preview-charts.js
@@ -0,0 +1,229 @@
+// ----------------------------------------------------------------------------
+// Copyright (c) 2021-2026 DexForce Technology Co., Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+// ----------------------------------------------------------------------------
+
+const deviationCharts = (() => {
+ const left = 48, right = 12, top = 8, bottom = 24;
+ const plots = [];
+ let data, playbackFrame = 0, hoverFrame = null, failed = false;
+ let number, tcpNote, jointNote;
+ const description = document.getElementById('deviation-description');
+ const readout = document.getElementById('deviation-time');
+
+ function updateReadout() {
+ if (!data || !plots.length) return;
+ const frame = hoverFrame ?? playbackFrame;
+ const time = data.time_s[frame];
+ const fraction = time / data.time_s.at(-1);
+ readout.textContent = previewText('deviationTime', {
+ time: number.format(time),
+ count: data.count[frame],
+ total: data.episodes,
+ });
+ for (const plot of plots) {
+ const value = plot.values[frame];
+ const format = v => `${number.format(v)} ${plot.unit}`;
+ if (value === null) {
+ plot.value.textContent = previewText('deviationNoSample');
+ } else {
+ plot.value.textContent = previewText('deviationRange', {
+ min: format(plot.range.min[frame]),
+ mean: format(value),
+ max: format(plot.range.max[frame]),
+ });
+ }
+ plot.cursor.style.left = `calc(${fraction * 100}% + ${left - fraction * (left + right)}px)`;
+ }
+ }
+
+ function nearestFrame(time) {
+ let lo = 0, hi = data.time_s.length - 1;
+ while (lo < hi) {
+ const mid = Math.floor((lo + hi) / 2);
+ if (data.time_s[mid] < time) lo = mid + 1;
+ else hi = mid;
+ }
+ if (lo > 0 && time - data.time_s[lo - 1] < data.time_s[lo] - time) return lo - 1;
+ return lo;
+ }
+
+ function tickStep(span) {
+ const target = span / 4;
+ const magnitude = 10 ** Math.floor(Math.log10(target));
+ return magnitude * [1, 2, 2.5, 5, 10].find(step => step * magnitude >= target);
+ }
+
+ function drawBand(ctx, range, color, x, y) {
+ // All episodes start at zero. Only the tail can have no samples.
+ const last = range.max.findLastIndex(value => value !== null);
+ if (last < 0) return;
+ ctx.beginPath();
+ ctx.moveTo(x(0), y(range.max[0]));
+ for (let i = 1; i <= last; i++) ctx.lineTo(x(i), y(range.max[i]));
+ for (let i = last; i >= 0; i--) ctx.lineTo(x(i), y(range.min[i]));
+ ctx.closePath();
+ ctx.fillStyle = `${color}40`;
+ ctx.fill();
+ ctx.strokeStyle = `${color}99`;
+ ctx.stroke();
+ }
+
+ function draw(plot) {
+ const canvas = plot.canvas;
+ const width = canvas.clientWidth, height = canvas.clientHeight;
+ if (width <= left + right || height <= top + bottom) return;
+ const scale = window.devicePixelRatio || 1;
+ canvas.width = Math.round(width * scale);
+ canvas.height = Math.round(height * scale);
+ const ctx = canvas.getContext('2d');
+ ctx.scale(scale, scale);
+ const w = width - left - right, h = height - top - bottom;
+ const duration = data.time_s.at(-1);
+ ctx.font = '11px system-ui, sans-serif';
+ ctx.lineWidth = 1;
+ ctx.textBaseline = 'middle';
+ ctx.strokeStyle = '#384451';
+ ctx.fillStyle = '#adbdcb';
+ ctx.textAlign = 'right';
+ for (let i = 0; i <= plot.ticks; i++) {
+ const y = top + h * (1 - i / plot.ticks);
+ ctx.beginPath();
+ ctx.moveTo(left, y);
+ ctx.lineTo(width - right, y);
+ ctx.stroke();
+ ctx.fillText(number.format(i * plot.step), left - 5, y, left - 6);
+ }
+ const seconds = tickStep(duration);
+ for (let i = 0; i <= Math.floor(duration / seconds); i++) {
+ const fraction = i * seconds / duration;
+ ctx.textAlign = fraction > 0.95 ? 'right' : 'center';
+ ctx.fillText(`${number.format(i * seconds)} s`, left + w * fraction, height - 9);
+ }
+ const x = frame => left + w * data.time_s[frame] / duration;
+ const y = value => top + h * (1 - value / plot.max);
+ drawBand(ctx, plot.range, plot.color, x, y);
+ ctx.strokeStyle = plot.color; ctx.lineWidth = 2;
+ ctx.beginPath();
+ let previous = false;
+ for (let i = 0; i < plot.values.length; i++) {
+ const value = plot.values[i];
+ if (value === null) {
+ previous = false;
+ continue;
+ }
+ const px = x(i), py = y(value);
+ if (previous) ctx.lineTo(px, py);
+ else {
+ ctx.moveTo(px, py);
+ ctx.fillStyle = plot.color;
+ ctx.fillRect(px - 1, py - 1, 2, 2);
+ }
+ previous = true;
+ }
+ ctx.stroke();
+ }
+
+ function addCurve(panel, key, titleKey, unit, color) {
+ const values = data.mean[key];
+ if (!values) return;
+ const range = data.range[key];
+ const figure = document.createElement('figure');
+ figure.className = 'curve'; figure.style.setProperty('--line', color);
+ const caption = document.createElement('figcaption');
+ const label = document.createElement('span');
+ const value = document.createElement('output');
+ caption.append(label, value);
+ const area = document.createElement('div');
+ area.className = 'curve-plot';
+ const canvas = document.createElement('canvas');
+ canvas.setAttribute('role', 'img');
+ const cursor = document.createElement('i');
+ area.append(canvas, cursor); figure.append(caption, area); panel.append(figure);
+ const peak = range.max.reduce((max, v) => v === null ? max : Math.max(max, v), 0);
+ const step = tickStep(peak || 1);
+ const ticks = Math.ceil((peak || 1) / step);
+ const plot = {values, value, range, titleKey, label, unit, color, canvas, cursor, step, ticks, max: step * ticks};
+ plots.push(plot);
+ new ResizeObserver(() => draw(plot)).observe(canvas);
+ canvas.addEventListener('pointermove', event => {
+ const rect = canvas.getBoundingClientRect();
+ const ratio = Math.min(1, Math.max(0, (event.clientX - rect.left - left) / (rect.width - left - right)));
+ hoverFrame = nearestFrame(ratio * data.time_s.at(-1)); updateReadout();
+ });
+ canvas.addEventListener('pointerleave', () => { hoverFrame = null; updateReadout(); });
+ }
+
+ function renderLanguage() {
+ number = new Intl.NumberFormat(document.documentElement.lang, {maximumSignificantDigits: 4});
+ if (failed || !data?.episodes) {
+ const key = failed ? 'deviationsFailed' : data ? 'deviationsEmpty' : 'deviationsLoading';
+ description.textContent = previewText(key);
+ return;
+ }
+ description.textContent = previewText('deviationsHelp', {
+ reference: previewText(data.explicit_reference ? 'reference' : 'firstReference'),
+ count: data.episodes,
+ });
+ if (plots.length) description.append(' ', previewText('deviationsBand'));
+ if (tcpNote) tcpNote.textContent = previewText('tcpUnavailable');
+ if (jointNote) {
+ const separator = document.documentElement.lang === 'en' ? ', ' : '、';
+ const names = Object.values(data.arm_joint_groups).flat();
+ jointNote.textContent = names.length
+ ? previewText('jointNames', {names: names.join(separator)})
+ : previewText('jointsUnavailable');
+ }
+ for (const plot of plots) {
+ const values = {title: previewText(plot.titleKey), unit: plot.unit};
+ plot.label.textContent = previewText('curveLabel', values);
+ plot.canvas.setAttribute('aria-label', previewText('curveWithBand', values));
+ draw(plot);
+ }
+ updateReadout();
+ }
+
+ async function load() {
+ try {
+ const response = await fetch('/deviations');
+ if (!response.ok) throw Error('Could not read deviations');
+ data = await response.json();
+ if (data.episodes) {
+ document.getElementById('deviation-panels').hidden = false;
+ const tcp = document.getElementById('tcp-deviations');
+ addCurve(tcp, 'translation_mm', 'translationDistance', 'mm', '#80c7ff');
+ addCurve(tcp, 'rotation_deg', 'rotationAngle', '°', '#e6af77');
+ if (!data.mean.translation_mm) {
+ tcpNote = document.createElement('p');
+ tcp.append(tcpNote);
+ }
+ const joints = document.getElementById('joint-deviations');
+ addCurve(joints, 'qpos_deg', 'revoluteDeviation', '°', '#85d5b8');
+ addCurve(joints, 'qpos_mm', 'prismaticDeviation', 'mm', '#c9a9f2');
+ jointNote = document.createElement('p');
+ joints.append(jointNote);
+ }
+ } catch {
+ failed = true;
+ }
+ renderLanguage();
+ }
+
+ document.addEventListener('preview-language-change', renderLanguage);
+ renderLanguage();
+ load();
+ return {setFrame(frame) {
+ if (frame !== playbackFrame) { playbackFrame = frame; updateReadout(); }
+ }};
+})();
diff --git a/embodichain/lab/expansion/_frontend/preview-controls.js b/embodichain/lab/expansion/_frontend/preview-controls.js
new file mode 100644
index 000000000..b8d941674
--- /dev/null
+++ b/embodichain/lab/expansion/_frontend/preview-controls.js
@@ -0,0 +1,272 @@
+// ----------------------------------------------------------------------------
+// Copyright (c) 2021-2026 DexForce Technology Co., Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+// ----------------------------------------------------------------------------
+
+let status = null, busy = false, dragging = false, filterKey = null, sending = false;
+const pending = [];
+const filterKeys = ['space', 'aff', 'variant'];
+const selectKeys = [...filterKeys, 'align'];
+const layerKeys = ['paths', 'targets', 'positions'];
+const $ = id => document.getElementById(id);
+const viewport = $('viewport');
+let videoConnectionFailed = false;
+let video;
+const recordedLabel = value => value === 'unrecorded' ? previewText('unrecorded') : value;
+let connection = 'connecting';
+function setConnection(state) {
+ connection = state;
+ $('connection').textContent = previewText(state);
+ $('connection').classList.toggle('error', state === 'controlFailed' || state === 'disconnected');
+}
+async function connectVideo() {
+ while (!window.RTC_SceneLeaving) {
+ try {
+ const description = await RTC_ReadSceneDescription();
+ if (description.state === 'error' || !description.room || !description.sources?.length) {
+ throw Error('Native video source unavailable');
+ }
+ const sources = description.sources;
+ await window.RTC_Run({
+ Room: description.room, Role: 'offerer', Video: true, View: true,
+ TestPattern: false, Timeout: 30, Input: false,
+ InputStreams: sources.map(() => false), Cameras: sources.length,
+ FramesPerSecond: description.fps, BitrateBps: description.bitrate,
+ StreamNames: sources.map(source => source.name),
+ StreamSizes: sources.map(source => [source.width, source.height]),
+ });
+ } catch {
+ // The page shows a localized reconnecting status below.
+ }
+ videoConnectionFailed = true;
+ await new Promise(resolve => setTimeout(resolve, 1000));
+ }
+}
+
+function updateVideo() {
+ const peer = window.RTC_ViewPeer;
+ const next = peer?.Videos[0]?.Element;
+ if (next && next !== video) {
+ video?.remove();
+ video = next;
+ video.controls = false;
+ video.dataset.label = 'frameAlt';
+ video.setAttribute('aria-label', previewText('frameAlt'));
+ viewport.insertBefore(video, $('caption'));
+ }
+ if (peer?.ViewReady && !peer.Stopped && video?.readyState >= 2) {
+ videoConnectionFailed = false;
+ return 'connected';
+ }
+ return videoConnectionFailed ? 'reconnecting' : 'connecting';
+}
+
+function isDiscrete(update){return 'toggle_playback' in update || 'toggle_ghost' in update || 'ghosts' in update}
+async function control(update) {
+ const last = pending.at(-1);
+ // Coalesce continuous controls, preserving individual clicks and their filters.
+ if (last && !isDiscrete(last) && !isDiscrete(update)) Object.assign(last, update);
+ else pending.push(update);
+ if (sending) return;
+ sending = true;
+ try {
+ while (pending.length) {
+ const peer = window.RTC_ViewPeer;
+ if (!peer?.ViewReady || peer.Stopped) throw Error('WebRTC viewer is not ready');
+ // The native listener validates and strips these room credentials.
+ const body = {...pending.shift(), room: peer.Config.Room, token: peer.Token, after: peer.After};
+ const response = await fetch('/api/command', {
+ method: 'POST',
+ headers: {'Content-Type': 'application/json'},
+ body: JSON.stringify(body),
+ });
+ if (!response.ok) throw Error('Preview control request failed');
+ }
+ } catch {
+ pending.length = 0;
+ setConnection('controlFailed');
+ } finally {
+ sending = false;
+ }
+}
+for (const key of selectKeys) $(key).onchange = () => control({[key]: $(key).value});
+for (const key of ['ghosts', ...layerKeys]) $(key).onchange = () => control({[key]: $(key).checked});
+$('play').onclick=()=>control({toggle_playback:true});
+$('reset').onclick=()=>control({progress:0,playing:false});
+document.addEventListener('keydown',event=>{
+ if((event.code!=='Space' && event.key!==' ') || event.defaultPrevented || event.isComposing ||
+ event.altKey || event.ctrlKey || event.metaKey || event.shiftKey)return;
+ if(event.target.isContentEditable || event.target.closest?.('select, textarea, input:not([type="range"]), summary'))return;
+ event.preventDefault();
+ if(!event.repeat)$('play').click();
+});
+$('progress').onpointerdown=()=>dragging=true;$('progress').oninput=()=>control({progress:+$('progress').value/1000,playing:false});window.addEventListener('pointerup',()=>dragging=false);
+const cameras={orbit:{yaw:-2.15,elevation:.55,distance:1.65},top:{yaw:-1.57,elevation:1.48,distance:1.5},front:{yaw:-1.57,elevation:.2,distance:1.5},close:{yaw:-2.15,elevation:.7,distance:.85}};
+$('camera').onchange=()=>{if(cameras[$('camera').value])control(cameras[$('camera').value])};
+let pointer = null;
+viewport.onpointerdown = event => {
+ if (!status) return;
+ pointer = {x: event.clientX, y: event.clientY, yaw: status.yaw, elevation: status.elevation};
+ viewport.setPointerCapture(event.pointerId);
+};
+viewport.onpointermove = event => {
+ if (pointer) control({
+ yaw: Math.max(-20, Math.min(20, pointer.yaw - (event.clientX - pointer.x) * .008)),
+ elevation: Math.max(.1, Math.min(1.5, pointer.elevation + (event.clientY - pointer.y) * .006)),
+ });
+};
+viewport.onpointerup = viewport.onpointercancel = () => { pointer = null; };
+viewport.addEventListener('wheel', event => {
+ event.preventDefault();
+ if (status) control({distance: Math.max(.2, Math.min(20, status.distance + event.deltaY * .001))});
+}, {passive: false});
+function updateCandidates(s){
+ const list=$('candidates');
+ const key=s.visible.join(',');
+ if(key!==filterKey){
+ filterKey=key;
+ const buttons=s.clips.filter(c=>s.visible.includes(c.index)).map(c=>{
+ const button=document.createElement('button');
+ button.type='button';
+ button.dataset.index=c.index;
+ button.textContent=c.label;
+ const details=document.createElement('small');
+ details.textContent = previewText('candidateInfo', {
+ space: c.space,
+ aff: recordedLabel(c.aff),
+ variant: recordedLabel(c.variant),
+ });
+ button.append(details);
+ button.onclick=()=>control({toggle_ghost:c.index});
+ return button;
+ });
+ list.replaceChildren(...buttons);
+ }
+ const shown=new Set(s.visible_ghosts);
+ for(const button of list.children){
+ button.setAttribute('aria-pressed', String(shown.has(Number(button.dataset.index))));
+ }
+ const all=$('ghosts');
+ all.disabled=s.count===0;
+ all.checked=s.count>0 && shown.size===s.count;
+ all.indeterminate=shown.size>0 && !all.checked;
+}
+function renderCoverage(coverage) {
+ $('coverage-panel').hidden = false;
+ $('coverage-summary').textContent = previewText('coverageCounts', {
+ planned: coverage.planned_attempts,
+ attempted: coverage.records.reduce((count, record) => count + record.attempts, 0),
+ confirmed: coverage.records.reduce((count, record) => count + record.confirmed, 0),
+ result: previewText(coverage.coverage_complete ? 'coverageComplete' : 'coverageIncomplete'),
+ });
+ const axes = coverage.axes.map(axis => `${axis.name}: [${axis.lower}, ${axis.upper}]`);
+ const records = coverage.records.map(record => {
+ const summary = previewText('coverageRecord', {
+ ...record,
+ planned: record.planned ?? coverage.recipes_per_point,
+ });
+ const values = Object.entries(record.values).map(([name, value]) => `${name}=${Number(value).toPrecision(4)}`);
+ return `${summary}\n${values.join(', ')}`;
+ });
+ $('coverage-details').textContent = [previewText('coverageScope'), axes.join('\n'), records.join('\n\n')].join('\n\n');
+}
+
+function renderReplayText() {
+ setConnection(connection);
+ $('play').textContent = previewText(status?.playing ? 'pause' : 'play');
+ if (!status) {
+ $('caption').textContent = previewText('loadingModel');
+ $('detail').textContent = previewText('waitingResults');
+ return;
+ }
+
+ const reference = status.has_reference
+ ? previewText('reference')
+ : previewText('firstRecording', {label: status.label});
+ $('total').textContent = previewText('matchingTotal', {total: status.total});
+ $('clock').textContent = previewText('clock', {
+ time: status.time.toFixed(2),
+ frame: status.source_frame,
+ last: status.source_frames - 1,
+ });
+ $('phase').textContent = recordedLabel(status.phase);
+ $('caption').textContent = previewText('caption', {reference, count: status.visible_ghosts.length});
+ $('detail').textContent = previewText('executionDetail', {
+ reference,
+ candidate: recordedLabel(status.candidate),
+ steps: status.physics_steps,
+ episode: status.episode,
+ });
+}
+
+function populateFilters(s) {
+ for (const key of filterKeys) {
+ for (const value of s.options[key]) {
+ const option = new Option(recordedLabel(value), value);
+ if (value === 'unrecorded') option.dataset.text = 'unrecorded';
+ $(key).append(option);
+ }
+ }
+ if (!s.options.align.includes('phase')) $('align').querySelector('[value="phase"]').remove();
+}
+
+async function poll() {
+ if (busy || document.hidden) return;
+ busy = true;
+ try {
+ const response = await fetch('/api/state', {cache: 'no-store'});
+ if (!response.ok) throw Error('Could not read replay state');
+ const s = await response.json();
+ if (s.frame === undefined) return;
+ if (!status) {
+ populateFilters(s);
+ if (s.coverage) renderCoverage(s.coverage);
+ }
+ status = s;
+ if (!sending) {
+ for (const key of selectKeys) {
+ if (document.activeElement !== $(key)) $(key).value = s[key];
+ }
+ for (const key of layerKeys) $(key).checked = s[key];
+ if (document.activeElement !== $('camera')) {
+ $('camera').value = Object.keys(cameras).find(key =>
+ Object.keys(cameras[key]).every(axis => Math.abs(s[axis] - cameras[key][axis]) < .001)
+ ) || 'custom';
+ }
+ }
+ connection = updateVideo();
+ renderReplayText();
+ $('count').textContent = s.count;
+ if (!dragging) $('progress').value = Math.round(s.progress * 1000);
+ updateCandidates(s);
+ deviationCharts.setFrame(s.source_frame);
+ } catch {
+ setConnection('disconnected');
+ } finally {
+ busy = false;
+ }
+}
+
+document.addEventListener('preview-language-change', () => {
+ renderReplayText();
+ if (status) {
+ filterKey = null;
+ updateCandidates(status);
+ if (status.coverage) renderCoverage(status.coverage);
+ }
+});
+renderReplayText();
+connectVideo();
+setInterval(poll, 100);
+poll();
diff --git a/embodichain/lab/expansion/_frontend/preview-heatmap.js b/embodichain/lab/expansion/_frontend/preview-heatmap.js
new file mode 100644
index 000000000..616c27bb1
--- /dev/null
+++ b/embodichain/lab/expansion/_frontend/preview-heatmap.js
@@ -0,0 +1,208 @@
+// ----------------------------------------------------------------------------
+// Copyright (c) 2021-2026 DexForce Technology Co., Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+// ----------------------------------------------------------------------------
+
+(() => {
+ const element = name => document.getElementById('heatmap-' + name);
+ const canvas = element('plot');
+ const ctx = canvas.getContext('2d');
+ const objectSelect = element('object');
+ const peak = counts => Math.max(...counts.map(row => Math.max(...row)));
+ let data, object, view, xAxis, yAxis, colors, area, cell = null, failed = false;
+
+ function axisScale(edges) {
+ const bins = edges.length - 1;
+ const step = (edges[bins] - edges[0]) / bins;
+ const digits = Math.min(20, Math.max(0, 1 - Math.floor(Math.log10(step))));
+ const number = new Intl.NumberFormat(document.documentElement.lang, {
+ maximumFractionDigits: digits,
+ useGrouping: false,
+ });
+ const ticks = [0, 0.25, 0.5, 0.75, 1].map(t => number.format(edges[Math.round(t * bins)]));
+ const meters = (a, b) => previewText('meters', {low: number.format(a), high: number.format(b)});
+ return {step, ticks, meters};
+ }
+
+ function colorScale(max) {
+ const stops = [[70, 111, 172], [47, 178, 166], [245, 215, 98]];
+ const scale = ['#202c38'];
+ for (let n = 1; n <= max; n++) {
+ const t = max === 1 ? 2 : 2 * (n - 1) / (max - 1);
+ const i = Math.min(1, Math.floor(t));
+ const rgb = stops[i].map((v, c) => Math.round(v + (stops[i + 1][c] - v) * (t - i)));
+ scale.push(`rgb(${rgb.join(',')})`);
+ }
+ return scale;
+ }
+
+ function draw() {
+ if (!view || !canvas.clientWidth) return;
+ const width = canvas.clientWidth, height = canvas.clientHeight;
+ const dpr = window.devicePixelRatio || 1;
+ if (canvas.width !== Math.round(width * dpr) || canvas.height !== Math.round(height * dpr)) {
+ canvas.width = Math.round(width * dpr);
+ canvas.height = Math.round(height * dpr);
+ }
+ ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
+ ctx.clearRect(0, 0, width, height);
+ ctx.font = '11px system-ui, sans-serif';
+ const labelWidth = axis => Math.max(...axis.ticks.map(label => ctx.measureText(label).width));
+ const xLabelWidth = labelWidth(xAxis);
+ const left = Math.ceil(Math.max(48, labelWidth(yAxis) + 9, xLabelWidth / 2 + 2));
+ const right = Math.ceil(Math.max(18, xLabelWidth / 2 + 2));
+ const top = 22, size = Math.min(width - left - right, height - top - 40);
+ if (size <= 0) {
+ area = null;
+ return;
+ }
+ const n = view.counts.length, step = size / n;
+ area = {left, top, size};
+ ctx.fillStyle = colors[0];
+ ctx.fillRect(left, top, size, size);
+ for (let row = 0; row < n; row++) {
+ view.counts[row].forEach((count, col) => {
+ if (!count) return;
+ ctx.fillStyle = colors[count];
+ ctx.fillRect(left + col * step, top + (n - 1 - row) * step, step, step);
+ });
+ }
+
+ ctx.fillStyle = '#b6c5d3';
+ ctx.strokeStyle = '#94b2c340';
+ ctx.lineWidth = 1;
+ const stride = [1, 2, 4].find(skip => size * skip / 4 >= xLabelWidth + 6) ?? 4;
+ for (let i = 0; i <= 4; i += stride) {
+ const fraction = i / 4;
+ const x = left + size * fraction, y = top + size * (1 - fraction);
+ ctx.beginPath();
+ ctx.moveTo(x, top); ctx.lineTo(x, top + size);
+ ctx.moveTo(left, y); ctx.lineTo(left + size, y);
+ ctx.stroke();
+ ctx.textAlign = 'center';
+ ctx.fillText(xAxis.ticks[i], x, top + size + 16);
+ ctx.textAlign = 'right';
+ ctx.fillText(yAxis.ticks[i], left - 7, y + 4);
+ }
+ ctx.fillStyle = '#d4dfe9';
+ ctx.textAlign = 'left';
+ ctx.fillText('Y (m)', left, top - 9);
+ ctx.textAlign = 'center';
+ ctx.fillText('X (m)', left + size / 2, top + size + 33);
+ if (cell) {
+ ctx.strokeStyle = '#ffffff';
+ ctx.lineWidth = 2;
+ ctx.strokeRect(left + cell.col * step, top + (n - 1 - cell.row) * step, step, step);
+ }
+ }
+
+ function reportCell() {
+ const output = element('cell');
+ if (!cell) {
+ output.textContent = previewText('heatmapInspect');
+ return;
+ }
+ const {row, col} = cell;
+ const x = object.x_edges_m, y = object.y_edges_m;
+ output.textContent = previewText('heatmapCell', {
+ count: view.counts[row][col],
+ total: object.episodes,
+ x: xAxis.meters(x[col], x[col + 1]),
+ y: yAxis.meters(y[row], y[row + 1]),
+ });
+ }
+
+ function renderLanguage() {
+ const description = element('description');
+ if (failed || !object) {
+ const key = failed ? 'heatmapFailed' : data ? 'heatmapEmpty' : 'heatmapLoading';
+ description.textContent = previewText(key);
+ return;
+ }
+ xAxis = axisScale(object.x_edges_m);
+ yAxis = axisScale(object.y_edges_m);
+ description.textContent = previewText('heatmapHelp', {count: object.episodes, object: object.uid});
+ const [low, high] = view.bounds_m;
+ const x = xAxis.meters(low[0], high[0]);
+ const y = yAxis.meters(low[1], high[1]);
+ const millimeters = new Intl.NumberFormat(document.documentElement.lang, {maximumSignificantDigits: 4});
+ element('x').textContent = x;
+ element('y').textContent = y;
+ element('step').textContent = `${millimeters.format(xAxis.step * 1000)} × ${millimeters.format(yAxis.step * 1000)} mm`;
+ const max = peak(view.counts);
+ element('peak').textContent = previewText('heatmapPeak', {max, total: object.episodes});
+ canvas.setAttribute('aria-label', previewText('heatmapAlt', {object: object.uid, x, y, max}));
+ reportCell();
+ draw();
+ }
+
+ function selectObject() {
+ object = data.objects[objectSelect.selectedIndex];
+ view = object.views.start;
+ cell = null;
+ const max = peak(view.counts);
+ colors = colorScale(max);
+ element('scale-max').textContent = max;
+ element('scale').replaceChildren(...colors.map(color => {
+ const swatch = document.createElement('i');
+ swatch.style.backgroundColor = color;
+ return swatch;
+ }));
+ renderLanguage();
+ }
+
+ function inspect(event) {
+ if (!area) return;
+ const rect = canvas.getBoundingClientRect(), n = view.counts.length;
+ const col = Math.floor((event.clientX - rect.left - area.left) / area.size * n);
+ const row = n - 1 - Math.floor((event.clientY - rect.top - area.top) / area.size * n);
+ const inside = row >= 0 && row < n && col >= 0 && col < n;
+ if (inside ? cell?.row === row && cell.col === col : !cell) return;
+ cell = inside ? {row, col} : null;
+ reportCell();
+ draw();
+ }
+
+ objectSelect.addEventListener('change', selectObject);
+ canvas.addEventListener('pointermove', inspect);
+ canvas.addEventListener('pointerdown', inspect);
+ canvas.addEventListener('pointerleave', event => {
+ if (event.pointerType !== 'mouse' || !cell) return;
+ cell = null;
+ reportCell();
+ draw();
+ });
+ new ResizeObserver(draw).observe(canvas);
+
+ async function load() {
+ try {
+ const response = await fetch('/object-heatmaps');
+ if (!response.ok) throw Error('Could not read starting positions');
+ data = await response.json();
+ if (data.objects.length) {
+ for (const item of data.objects) objectSelect.add(new Option(item.uid, item.uid));
+ element('panel').hidden = false;
+ selectObject();
+ return;
+ }
+ } catch {
+ failed = true;
+ }
+ renderLanguage();
+ }
+
+ document.addEventListener('preview-language-change', renderLanguage);
+ renderLanguage();
+ load();
+})();
diff --git a/embodichain/lab/expansion/_frontend/preview-text.js b/embodichain/lab/expansion/_frontend/preview-text.js
new file mode 100644
index 000000000..8dd80e52a
--- /dev/null
+++ b/embodichain/lab/expansion/_frontend/preview-text.js
@@ -0,0 +1,146 @@
+// ----------------------------------------------------------------------------
+// Copyright (c) 2021-2026 DexForce Technology Co., Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+// ----------------------------------------------------------------------------
+
+const previewText = (() => {
+ // Keep labels and runtime messages together. Recorded IDs are passed as values.
+ const messages = {
+ title: ['EmbodiChain · 扩增结果回放', 'EmbodiChain · Expansion replay'],
+ subtitle: ['完整模型虚影 · 已确认的真实扩增结果', 'Full-model overlays of confirmed expansion recordings'],
+ language: ['语言', 'Language'],
+ disconnected: ['渲染服务未连接', 'Renderer disconnected'],
+ connected: ['● 原生视频 · WebRTC', '● Native video · WebRTC'],
+ connecting: ['正在连接 WebRTC 视频…', 'Connecting WebRTC video…'],
+ reconnecting: ['WebRTC 视频连接失败,正在重试…', 'WebRTC video unavailable. Reconnecting…'],
+ controlFailed: ['控制未成功应用', 'Could not apply replay control'],
+ play: ['播放', 'Play'],
+ pause: ['暂停', 'Pause'],
+ reset: ['回到起点', 'Back to start'],
+ alignment: ['对齐方式', 'Alignment'],
+ alignPhase: ['按动作阶段对齐', 'Align by motion phase'],
+ alignTime: ['按真实时间对齐', 'Align by recorded time'],
+ camera: ['观察视角', 'Camera view'],
+ cameraCustom: ['自由视角', 'Free view'],
+ cameraOrbit: ['斜视', 'Orbit view'],
+ cameraTop: ['俯视', 'Top view'],
+ cameraSide: ['侧视', 'Side view'],
+ cameraClose: ['抓取区域近景', 'Grasp close-up'],
+ spaceKey: ['空格', 'Space'],
+ controlsHelp: ['播放 / 暂停 · 拖动画面旋转 · 滚轮缩放', 'Play / pause · Drag to rotate · Scroll to zoom'],
+ frameAlt: ['EmbodiChain 原生场景渲染画面', 'Native EmbodiChain scene rendering'],
+ loadingModel: ['正在加载完整机器人模型…', 'Loading the full robot model…'],
+ progress: ['回放进度', 'Playback progress'],
+ coverageTitle: ['采样覆盖 · 包含未通过点', 'Sampling coverage, including unqualified points'],
+ coverageDetails: ['边界与样本明细', 'Bounds and sample details'],
+ coverageCounts: ['计划 {planned} · 已尝试 {attempted} · 已确认 {confirmed}。{result}',
+ 'Planned {planned} · Attempted {attempted} · Confirmed {confirmed}. {result}'],
+ coverageComplete: ['全部计划样本已确认', 'All planned samples confirmed.'],
+ coverageIncomplete: ['包含未通过、回退或未执行点', 'Includes rejected, fallback or unrun points.'],
+ coverageScope: ['有限参数采样,不代表连续空间或全部边界组合穷举。',
+ 'This finite sample covers selected parameter points.'],
+ coverageRecord: ['#{index} {role} · {status} · {confirmed}/{planned} 确认',
+ '#{index} {role} · {status} · {confirmed}/{planned} confirmed'],
+ filters: ['分布筛选 · 三维联动', 'Filter 3D overlays'],
+ layoutGroup: ['参数组', 'Layout group'],
+ allSpaces: ['全部空间组', 'All layout groups'],
+ affordance: ['抓取分支', 'Affordance'],
+ allAffordances: ['全部抓取分支', 'All grasp branches'],
+ trajectory: ['轨迹', 'Trajectory'],
+ allVariants: ['全部变体', 'All variants'],
+ matching: [' 条符合筛选', ' matching'],
+ matchingTotal: [' / {total} 条符合筛选', ' / {total} matching'],
+ unrecorded: ['未记录', 'Unrecorded'],
+ candidateInfo: ['{space} · 抓取 {aff} · 变体 {variant}', '{space} · Grasp {aff} · Variant {variant}'],
+ layers: ['场景图层', 'Scene layers'],
+ showGhosts: ['显示全部匹配虚影', 'Show all matching overlays'],
+ showPaths: ['末端轨迹线', 'End-effector paths'],
+ showTargets: ['抓取目标坐标轴', 'Grasp target axes'],
+ showPositions: ['初始空间位置', 'Initial positions'],
+ layersHelp: ['实色模型始终播放固定对照。点击轨迹按钮可分别显示或隐藏对应的虚影。',
+ 'The solid model follows the reference. Click a trajectory button to toggle its overlay.'],
+ execution: ['真实执行记录', 'Recorded execution'],
+ waitingResults: ['等待渲染结果…', 'Waiting for the first rendered frame…'],
+ reference: ['原始轨迹', 'Original trajectory'],
+ firstReference: ['首条对照记录', 'First recording'],
+ firstRecording: ['首条记录 {label}(固定对照)', 'First recording {label} (fixed reference)'],
+ clock: ['{time} s · 记录帧 {frame} / {last}', '{time} s · Recorded frame {frame} / {last}'],
+ caption: ['{reference} · {count} 条扩增虚影', '{reference} · Overlays shown: {count}'],
+ executionDetail: ['{reference} · 已通过验收\n抓取候选 {candidate} · 物理推进 {steps} 步\n{episode}',
+ '{reference} · Accepted\nGrasp candidate: {candidate} · Physics steps: {steps}\n{episode}'],
+ deviations: ['扩增轨迹偏差', 'Expansion trajectory deviations'],
+ deviationsLoading: ['正在读取统计曲线…', 'Loading deviation curves…'],
+ deviationsEmpty: ['当前只有对照轨迹。加入扩增轨迹即可显示偏差曲线。',
+ 'Only the reference is available. Add expanded trajectories to display deviation curves.'],
+ deviationsFailed: ['偏差曲线加载失败,请刷新页面重试。', 'Could not load deviation curves. Refresh to try again.'],
+ deviationsHelp: ['相对{reference},按实测时间对齐全部 {count} 条扩增轨迹。每个时刻只计仍有记录的轨迹。',
+ 'All {count} expanded trajectories are aligned to the {reference} by recorded time. Each time uses trajectories with data at that point.'],
+ deviationsBand: ['色带覆盖最小值至最大值,实线表示均值。', 'The band spans the minimum to maximum. The line shows the mean.'],
+ deviationTime: ['{time} s · {count} / {total} 条轨迹', '{time} s · Trajectories: {count} / {total}'],
+ deviationRange: ['最小 {min} · 均值 {mean} · 最大 {max}', 'Min {min} · Mean {mean} · Max {max}'],
+ deviationNoSample: ['无样本', 'No samples'],
+ tcpDeviations: ['TCP 位姿偏差', 'TCP pose deviation'],
+ jointDeviations: ['机械臂最大关节偏差', 'Maximum arm joint deviation'],
+ translationDistance: ['平移距离', 'Translation distance'],
+ rotationAngle: ['相对旋转角', 'Relative rotation angle'],
+ revoluteDeviation: ['转动关节最大绝对偏差', 'Maximum absolute revolute-joint deviation'],
+ prismaticDeviation: ['移动关节最大绝对偏差', 'Maximum absolute prismatic-joint deviation'],
+ curveLabel: ['{title}({unit})', '{title} ({unit})'],
+ curveWithBand: ['{title}随实测时间的均值曲线及最小值至最大值色带,单位 {unit}',
+ '{title} over recorded time, with mean and minimum-to-maximum range, in {unit}'],
+ tcpUnavailable: ['记录中缺少所选机械臂的完整 TCP 标定。', 'The selected arm has incomplete TCP calibration in the recordings.'],
+ jointsUnavailable: ['记录中缺少可识别的机械臂关节组。', 'The recordings do not identify an arm joint group.'],
+ jointNames: ['统计关节 · {names}', 'Included joints · {names}'],
+ heatmapTitle: ['object_pose 物体起点分布', 'object_pose starting positions'],
+ heatmapLoading: ['正在读取扩增起点…', 'Loading starting positions…'],
+ heatmapEmpty: ['当前输入中没有记录 object_pose 扩增物体的起点。',
+ 'The inputs contain no recorded object_pose starting positions.'],
+ heatmapFailed: ['扩增起点加载失败,请刷新页面重试。', 'Could not load starting positions. Refresh to try again.'],
+ heatmapObject: ['扩增物体', 'Expanded object'],
+ heatmapX: ['实测 X 范围', 'Recorded X range'],
+ heatmapY: ['实测 Y 范围', 'Recorded Y range'],
+ heatmapCellSize: ['单格尺寸(X × Y)', 'Cell size (X × Y)'],
+ heatmapMostVisited: ['单格最多轨迹', 'Most trajectories in one cell'],
+ heatmapLegend: ['起点落入该格的轨迹数', 'Trajectories starting in each cell'],
+ heatmapInspect: ['指向或轻点网格,查看该位置的坐标和轨迹数。', 'Hover or tap a cell to see its coordinates and count.'],
+ heatmapCell: ['起点 {count} / {total} 条轨迹 · X {x} · Y {y}', 'Starts: {count} / {total} trajectories · X {x} · Y {y}'],
+ heatmapHelp: ['{count} 条 object_pose 扩增轨迹记录了 {object} 的起点。X、Y 轴按各自的首帧实测范围缩放。',
+ 'The starting positions of {object} come from {count} object_pose trajectories. Each axis fits its recorded first-frame range.'],
+ heatmapPeak: ['{max} / {total} 条', '{max} / {total}'],
+ heatmapAlt: ['{object} 扩增起点 · X {x} · Y {y} · 单格最多 {max} 条轨迹',
+ '{object} starting positions · X {x} · Y {y} · Most trajectories in one cell: {max}'],
+ meters: ['{low} ~ {high} m', '{low} to {high} m'],
+ footer: ['EmbodiChain SimulationManager / RobotMarkers / VisualizationMarkers → DexSim Hybrid 原生渲染。仅回放,物理推进 0 步。机器人与物体使用逐帧记录;轨迹线每 4 帧取点并连接。视角、灯光与透明材质仅用于验收展示。',
+ 'Native DexSim Hybrid rendering through EmbodiChain. Replay advances physics by zero steps. Robot and object states come from recorded frames. Path lines connect every fourth frame. Camera, lighting and transparency are for display.'],
+ };
+ const language = document.getElementById('language');
+ function t(key, values = {}) {
+ const message = messages[key][language.value === 'en' ? 1 : 0];
+ return message.replace(/\{(\w+)\}/g, (_, name) => values[name]);
+ }
+ function applyLanguage() {
+ document.documentElement.lang = language.value;
+ document.title = t('title');
+ for (const node of document.querySelectorAll('[data-text]')) {
+ node.textContent = t(node.dataset.text);
+ }
+ for (const node of document.querySelectorAll('[data-label]')) {
+ node.setAttribute('aria-label', t(node.dataset.label));
+ }
+ document.dispatchEvent(new Event('preview-language-change'));
+ }
+ language.addEventListener('change', applyLanguage);
+ applyLanguage();
+ return t;
+})();
diff --git a/embodichain/lab/expansion/_frontend/preview.css b/embodichain/lab/expansion/_frontend/preview.css
new file mode 100644
index 000000000..3b6b7a039
--- /dev/null
+++ b/embodichain/lab/expansion/_frontend/preview.css
@@ -0,0 +1,37 @@
+/*
+ * ----------------------------------------------------------------------------
+ * Copyright (c) 2021-2026 DexForce Technology Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ----------------------------------------------------------------------------
+ */
+
+*{box-sizing:border-box}body{margin:0;background:#10151b;color:#e8edf3;font:14px system-ui,sans-serif}header{padding:22px 28px;border-bottom:1px solid #303944;display:flex;align-items:center;justify-content:space-between}h1{font-size:20px;margin:0 0 6px}p{color:#9eacba;margin:0;line-height:1.6}.badge{color:#77dbb2;border:1px solid #376455;border-radius:20px;padding:7px 12px}main{display:grid;grid-template-columns:minmax(0,1fr) 290px;gap:18px;padding:20px;max-width:1700px;margin:auto}.stage{background:#1b222b;border:1px solid #303944;border-radius:12px;overflow:hidden}.toolbar{display:flex;gap:10px;padding:12px;align-items:center;flex-wrap:wrap}button,select{background:#27323f;color:#e8edf3;border:1px solid #465363;border-radius:6px;padding:9px;cursor:pointer}button:hover{background:#34475a}button:focus-visible,select:focus-visible,input:focus-visible{outline:2px solid #63b7ff}select{max-width:100%}.viewport{position:relative;background:#222;aspect-ratio:1100/760;touch-action:none;cursor:grab}.overlay{position:absolute;left:16px;top:14px;background:#10151bdc;padding:9px 12px;border-radius:7px;pointer-events:none;font-size:12px}.timeline{padding:16px}input[type=range]{width:100%;accent-color:#6bb7ff}.timeinfo{display:flex;justify-content:space-between;gap:10px;color:#a8bacb;font-size:12px;margin-top:9px}.side{display:flex;flex-direction:column;gap:14px}.panel{padding:17px;background:#1b222b;border:1px solid #303944;border-radius:12px}.panel h2{font-size:14px;margin:0 0 15px}.field{display:flex;align-items:center;justify-content:space-between;gap:10px;margin:11px 0}.field select{width:160px}label{color:#c9d4df}input[type=checkbox]{accent-color:#6bb7ff}.count{font-size:30px;color:#80bfff}.candidates{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:7px;margin-top:12px}.candidates button{text-align:left;font-size:12px}.candidates button small{display:block;color:#a5b5c6;margin-top:4px}.candidates button[aria-pressed="true"]{border-color:#7cbcff;background:#253e58}.legend{font-size:12px;color:#a7b6c4;line-height:1.9}.detail{font-size:12px;line-height:1.9;overflow-wrap:anywhere}.error{color:#ffba9b}footer{padding:0 22px 20px;color:#8393a3;font-size:12px}@media(max-width:850px){main{grid-template-columns:1fr;padding:10px}.side{display:grid;grid-template-columns:1fr 1fr}header{padding:15px}.badge{display:none}}@media(max-width:550px){.side{grid-template-columns:1fr}.toolbar{gap:5px}h1{font-size:17px}}
+.deviations{grid-column:1/-1;min-width:0}.deviations h2{font-size:18px;margin:0 0 6px}.chart-heading{display:flex;flex-wrap:wrap;justify-content:space-between;gap:8px;align-items:end;margin:6px 0 14px}.chart-heading output,.curve output{font-variant-numeric:tabular-nums}.chart-heading output{color:#cad9e6}.chart-grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:18px}.chart-grid h3{font-size:15px;margin:0 0 14px}.curve{margin:12px 0 0}.curve figcaption{display:flex;align-items:baseline;justify-content:space-between;gap:8px;font-size:13px}.curve figcaption span{border-left:3px solid var(--line);padding-left:7px}.curve output{color:var(--line)}.curve-plot{position:relative}.curve canvas{display:block;width:100%;height:120px}.curve-plot i{position:absolute;top:8px;bottom:24px;width:0;border-left:1px dashed #e2eaf3;pointer-events:none}.chart-grid section:has(.curve:only-of-type) canvas{height:250px}
+.chart-grid[hidden]{display:none}
+.curve figcaption{flex-wrap:wrap;row-gap:4px}
+.viewport video{position:absolute;inset:0;width:100%;height:100%;object-fit:contain;pointer-events:none}.header-controls{display:flex;align-items:center;gap:12px}.language-control{display:flex;gap:6px;align-items:center}.candidates small{overflow-wrap:anywhere}
+@media(max-width:550px){header{flex-wrap:wrap;gap:10px}}
+.controls-help{font-size:12px;color:#a8bacb}.controls-help kbd{font:inherit;border:1px solid #465363;border-radius:4px;padding:1px 4px}
+@media(max-width:850px){.chart-grid{grid-template-columns:1fr}.chart-grid section:has(.curve:only-of-type) canvas{height:150px}}
+.heatmap{grid-column:1/-1;min-width:0}.heatmap h2{font-size:18px;margin:0 0 6px}.heatmap-panel{margin-top:14px;display:grid;grid-template-columns:minmax(0,470px) minmax(200px,340px);gap:clamp(20px,6vw,90px);justify-content:center;align-items:center}.heatmap-panel[hidden]{display:none}
+.heatmap canvas{display:block;width:100%;aspect-ratio:1;cursor:crosshair}.heatmap-controls{display:grid;gap:6px}.heatmap-controls select{width:100%}
+.heatmap dl{margin:18px 0;border-block:1px solid #384451}.heatmap dl div{display:flex;justify-content:space-between;align-items:baseline;gap:12px;padding:6px 0}.heatmap dt,.heatmap-legend{color:#a7b6c4}.heatmap dd{margin:0;font-variant-numeric:tabular-nums}
+.heatmap-scale{display:flex;height:12px;overflow:hidden;border-radius:3px;border:1px solid #465363;margin:7px 0 3px}.heatmap-scale i{flex:1}.heatmap-ticks{display:flex;justify-content:space-between;font-size:12px}.heatmap-readout{display:block;min-height:3.3em;margin-top:18px;color:#c9d4df;font-size:13px;line-height:1.6;font-variant-numeric:tabular-nums}
+@media(max-width:650px){.heatmap-panel{display:flex;flex-direction:column;align-items:stretch;gap:12px}.heatmap-controls{margin-top:2px}.heatmap canvas{align-self:center;max-width:450px}.heatmap-readout{min-height:3.2em}}
+#detail,#coverage-details{white-space:pre-line}
+
+/* The SDK stages clips here before the active video moves to the viewport. */
+#RTC_Videos {
+ display: none !important;
+}
diff --git a/embodichain/lab/expansion/_frontend/preview.html b/embodichain/lab/expansion/_frontend/preview.html
new file mode 100644
index 000000000..a04b7b311
--- /dev/null
+++ b/embodichain/lab/expansion/_frontend/preview.html
@@ -0,0 +1,131 @@
+
EmbodiChain · 扩增结果回放
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/embodichain/lab/expansion/_preview.html b/embodichain/lab/expansion/_preview.html
deleted file mode 100644
index f1615652c..000000000
--- a/embodichain/lab/expansion/_preview.html
+++ /dev/null
@@ -1,35 +0,0 @@
-EmbodiChain · 扩增结果回放
-
-EmbodiChain · 扩增结果回放
完整模型虚影 · 已确认的真实扩增结果
正在连接原生渲染器
-拖动画面旋转 · 滚轮缩放
![EmbodiChain 原生场景渲染画面]()
正在加载完整机器人模型…
-
-
-
\ No newline at end of file
diff --git a/embodichain/lab/expansion/_preview_data.py b/embodichain/lab/expansion/_preview_data.py
index 2fa9ce66d..6b39ac9ae 100644
--- a/embodichain/lab/expansion/_preview_data.py
+++ b/embodichain/lab/expansion/_preview_data.py
@@ -18,7 +18,7 @@
from __future__ import annotations
-from dataclasses import dataclass
+from dataclasses import dataclass, field
from pathlib import Path
from typing import Any
import json
@@ -28,6 +28,7 @@
from .comparison import MeasuredTrajectory
from .persistence import ArtifactEpisodeSink, load_expansion_episode
+from ._preview_deviations import recorded_coordinates
__all__: list[str] = []
@@ -52,6 +53,26 @@ class PreviewClip:
label: str = ""
links: np.ndarray | None = None
tips: np.ndarray | None = None
+ tcp_frame: tuple[str, np.ndarray] | None = None
+ object_pose_uids: tuple[str, ...] = ()
+
+
+def _object_pose_uids(
+ metadata: Any, job_operator: str | None, measured: MeasuredTrajectory
+) -> tuple[str, ...]:
+ """Read the selected operator and its rigid layout targets from the saved recipe."""
+ operator = job_operator
+ changes = {}
+ for operation in metadata.get("recipe_spec", {}).get("operations", ()):
+ if operation["name"] == "joint_scene":
+ changes = json.loads(operation["payload_json"]).get("pose_changes", {})
+ elif operation["name"] == "motion":
+ operator = json.loads(operation["payload_json"]).get(
+ "operator", job_operator
+ )
+ if operator != "object_pose":
+ return ()
+ return tuple(uid for uid in changes if uid in measured.object_poses)
def annotations(metadata: Any, measured: MeasuredTrajectory) -> dict[str, Any]:
@@ -275,6 +296,16 @@ class PreviewData:
ground_visible: bool = True
coverage: dict[str, Any] | None = None
object_specs: dict[str, dict[str, Any]] | None = None
+ arm_joint_groups: dict[str, list[str]] = field(default_factory=dict)
+
+ @property
+ def solid_clip(self) -> PreviewClip:
+ """Pin the supplied reference, or the first input recording for comparison."""
+ return self.reference if self.reference is not None else self.clips[0]
+
+ @property
+ def ghost_indices(self) -> range:
+ return range(0 if self.reference is not None else 1, len(self.clips))
@property
def all_clips(self) -> list[PreviewClip]:
@@ -327,6 +358,7 @@ def load_preview(
boxes = None
robot_spec = None
object_specs = None
+ arm_joint_groups = None
end = None
ground = True
seen = set()
@@ -360,8 +392,9 @@ def load_preview(
# URDF assembly comments may differ; compare the actual XML model.
import xml.etree.ElementTree as ET
+ model = ET.parse(resolved_asset).getroot()
if asset is not None and ET.tostring(ET.parse(asset).getroot()) != ET.tostring(
- ET.parse(resolved_asset).getroot()
+ model
):
raise ValueError("Output directories reference different robot models")
asset = resolved_asset
@@ -376,6 +409,11 @@ def load_preview(
"Specify --eef-link when the recorded solver endpoint is ambiguous"
)
end = inferred
+ groups, tcp_frame = recorded_coordinates(robot, end, model)
+ if arm_joint_groups is None:
+ arm_joint_groups = groups
+ elif groups != arm_joint_groups:
+ arm_joint_groups = {}
if group == 0:
ground = job["resolved_deployment"].get("ground_plane_visible") is not False
elif ground != (
@@ -425,6 +463,10 @@ def load_preview(
clip = PreviewClip(
measured=measured,
space=f"S{group+1} · {path.name}",
+ tcp_frame=tcp_frame,
+ object_pose_uids=_object_pose_uids(
+ episode.metadata, job.get("operator"), measured
+ ),
**annotations(episode.metadata, measured),
)
if is_reference:
@@ -434,22 +476,28 @@ def load_preview(
clips.append(clip)
clip.label = f"C-{len(clips):03d}"
return PreviewData(
- clips, reference_clip, asset, end, boxes, ground, coverage, object_specs
+ clips,
+ reference_clip,
+ asset,
+ end,
+ boxes,
+ ground,
+ coverage,
+ object_specs,
+ arm_joint_groups=arm_joint_groups or {},
)
class PreviewState:
def __init__(self, data: PreviewData) -> None:
self.data = data
+ self.enabled_ghosts: set[int] = set()
self.values = dict(
space="all",
aff="all",
variant="all",
- selected=0,
progress=0.0,
playing=False,
- reference=data.reference is not None,
- ghosts=True,
paths=True,
targets=True,
positions=True,
@@ -458,10 +506,11 @@ def __init__(self, data: PreviewData) -> None:
elevation=0.55,
distance=1.65,
)
+ candidates = [data.clips[i] for i in data.ghost_indices]
self.options = {
- "space": sorted({c.space for c in data.clips}),
- "aff": sorted({c.affordance for c in data.clips}),
- "variant": sorted({c.variant for c in data.clips}),
+ "space": sorted({c.space for c in candidates}),
+ "aff": sorted({c.affordance for c in candidates}),
+ "variant": sorted({c.variant for c in candidates}),
"align": ["time"] + (["phase"] if data.phase_alignment else []),
}
@@ -469,18 +518,21 @@ def validate(self, update: Any) -> dict[str, Any]:
if (
not isinstance(update, dict)
or not update
- or set(update) - self.values.keys()
+ or set(update)
+ - (self.values.keys() | {"toggle_playback", "toggle_ghost", "ghosts"})
):
raise ValueError("Invalid preview controls")
bounds = {
- "selected": (0, len(self.data.clips) - 1),
"progress": (0, 1),
"yaw": (-20, 20),
"elevation": (0.1, 1.5),
"distance": (0.2, 20),
}
for key, value in update.items():
- if key in self.options:
+ if key == "toggle_ghost":
+ if type(value) is not int or value not in self.data.ghost_indices:
+ raise ValueError(key)
+ elif key in self.options:
if value not in self.options[key] + ([] if key == "align" else ["all"]):
raise ValueError(key)
elif key in bounds:
@@ -488,7 +540,6 @@ def validate(self, update: Any) -> dict[str, Any]:
type(value) not in (float, int)
or not math.isfinite(value)
or not bounds[key][0] <= value <= bounds[key][1]
- or (key == "selected" and type(value) is not int)
):
raise ValueError(key)
elif type(value) is not bool:
@@ -496,28 +547,35 @@ def validate(self, update: Any) -> dict[str, Any]:
return dict(update)
def apply(self, update: dict[str, Any]) -> None:
- self.values.update(self.validate(update))
- visible = self.visible()
- if visible and self.values["selected"] not in visible:
- self.values["selected"] = visible[0]
- if not visible:
- self.values["playing"] = False
-
- def visible(self) -> list[int]:
+ controls = self.validate(update)
+ toggle = controls.pop("toggle_ghost", None)
+ show_matching = controls.pop("ghosts", None)
+ toggle_playback = controls.pop("toggle_playback", False)
+ self.values.update(controls)
+ if toggle_playback:
+ self.values["playing"] = not self.values["playing"]
+ if self.values["playing"] and self.values["progress"] >= 1:
+ self.values["progress"] = 0.0
+ if show_matching is True:
+ self.enabled_ghosts.update(self.matching())
+ elif show_matching is False:
+ self.enabled_ghosts.difference_update(self.matching())
+ if toggle is not None:
+ self.enabled_ghosts.symmetric_difference_update((toggle,))
+
+ def matching(self) -> list[int]:
v = self.values
return [
i
- for i, c in enumerate(self.data.clips)
- if all(
- v[k] == "all" or v[k] == actual
- for k, actual in [
- ("space", c.space),
- ("aff", c.affordance),
- ("variant", c.variant),
- ]
- )
+ for i in self.data.ghost_indices
+ if v["space"] in ("all", self.data.clips[i].space)
+ and v["aff"] in ("all", self.data.clips[i].affordance)
+ and v["variant"] in ("all", self.data.clips[i].variant)
]
+ def visible_ghosts(self) -> list[int]:
+ return [i for i in self.matching() if i in self.enabled_ghosts]
+
def frame_at(self, clip: PreviewClip) -> int:
p = self.values["progress"]
if self.values["align"] == "phase":
@@ -533,34 +591,31 @@ def duration(self) -> float:
return max(float(c.measured.timestamps[-1]) for c in self.data.all_clips)
def snapshot(self) -> dict[str, Any]:
- visible = self.visible()
- c = self.data.clips[self.values["selected"]]
+ matching = self.matching()
+ c = self.data.solid_clip
f = self.frame_at(c)
return {
**self.values,
- "visible": visible,
- "count": len(visible),
- "total": len(self.data.clips),
+ "visible": matching,
+ "visible_ghosts": [i for i in matching if i in self.enabled_ghosts],
+ "count": len(matching),
+ "total": len(self.data.ghost_indices),
"options": self.options,
"has_reference": self.data.reference is not None,
"coverage": self.data.coverage,
- "label": c.label if visible else "No matches",
- "source_frame": f if visible else None,
- "source_frames": len(c.measured.timestamps) if visible else 0,
- "time": float(c.measured.timestamps[f]) if visible else None,
- "episode": c.measured.name if visible else "",
+ "label": c.label,
+ "source_frame": f,
+ "source_frames": len(c.measured.timestamps),
+ "time": float(c.measured.timestamps[f]),
+ "episode": c.measured.name,
"candidate": ", ".join(c.candidate_ids) or "unrecorded",
- "phase": (
- next(
- (
- name
- for name, t in reversed(list(zip(c.phase_names, c.anchors)))
- if t <= c.measured.timestamps[f]
- ),
- "unrecorded",
- )
- if visible
- else ""
+ "phase": next(
+ (
+ name
+ for name, t in reversed(list(zip(c.phase_names, c.anchors)))
+ if t <= c.measured.timestamps[f]
+ ),
+ "unrecorded",
),
"clips": [
dict(
@@ -571,5 +626,6 @@ def snapshot(self) -> dict[str, Any]:
variant=c.variant,
)
for i, c in enumerate(self.data.clips)
+ if i in self.data.ghost_indices
],
}
diff --git a/embodichain/lab/expansion/_preview_deviations.py b/embodichain/lab/expansion/_preview_deviations.py
new file mode 100644
index 000000000..6180120d1
--- /dev/null
+++ b/embodichain/lab/expansion/_preview_deviations.py
@@ -0,0 +1,196 @@
+# ----------------------------------------------------------------------------
+# Copyright (c) 2021-2026 DexForce Technology Co., Ltd.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ----------------------------------------------------------------------------
+
+"""Time-aligned deviations from the fixed reference in the expansion viewer."""
+
+from __future__ import annotations
+
+import re
+from typing import TYPE_CHECKING, Any
+from xml.etree import ElementTree as ET
+
+import numpy as np
+from scipy.spatial.transform import Rotation, Slerp
+
+from embodichain.robot_registry import _validate_gripper_transform
+from embodichain.utils.string import resolve_matching_names
+
+if TYPE_CHECKING:
+ from ._preview_data import PreviewClip, PreviewData
+
+__all__: list[str] = []
+
+
+def recorded_coordinates(
+ robot: dict, endpoint: str, model: ET.Element
+) -> tuple[dict[str, list[str]], tuple[str, np.ndarray] | None]:
+ """Resolve the selected motion group and its own recorded gripper calibration."""
+ solvers = robot.get("solver_cfg") or {}
+ bindings = list((robot.get("gripper_bindings") or {}).values())
+ parts = [
+ part for part, cfg in solvers.items() if cfg.get("end_link_name") == endpoint
+ ]
+
+ def motion_part(binding: dict) -> str | None:
+ declaration = binding.get("declaration") or {}
+ defaults = binding.get("defaults", {}).get("binding") or {}
+ return declaration.get("motion_part") or defaults.get("motion_part")
+
+ if len(parts) == 1:
+ part = parts[0]
+ matched = [b for b in bindings if motion_part(b) == part]
+ if len(bindings) == len(solvers) == 1 and motion_part(bindings[0]) is None:
+ matched = bindings
+ elif not parts:
+ matched = [b for b in bindings if b.get("root_link") == endpoint]
+ if len(matched) != 1:
+ return {}, None
+ part = motion_part(matched[0])
+ else:
+ return {}, None
+ solver = solvers.get(part, {})
+ names = solver.get("joint_names") or (robot.get("control_parts") or {}).get(part)
+ joints = {j.get("name"): j for j in model.findall("joint")}
+ units = {"continuous": "deg", "revolute": "deg", "prismatic": "mm"}
+ if names and any(name not in joints for name in names):
+ try:
+ _, names = resolve_matching_names(
+ names, [n for n, j in joints.items() if j.get("type") in units]
+ )
+ except (ValueError, re.error):
+ names = []
+ elif not names:
+ # Older jobs can declare just the solver's root and end links.
+ parents = {j.find("child").get("link"): j for j in joints.values()}
+ link = solver.get("end_link_name") or endpoint
+ root = solver.get("root_link_name")
+ path = []
+ while link != root and link in parents:
+ joint = parents.pop(link)
+ if joint.get("type") != "fixed":
+ path.append(joint.get("name"))
+ link = joint.find("parent").get("link")
+ names = list(reversed(path)) if root is None or link == root else []
+
+ groups: dict[str, list[str]] = {}
+ for name in names:
+ if name not in joints or joints[name].get("type") not in units:
+ groups = {}
+ break
+ groups.setdefault(units[joints[name].get("type")], []).append(name)
+
+ if len(matched) == 1:
+ binding = matched[0]
+ offset = binding.get("defaults", {}).get("default_root_from_tcp")
+ if binding.get("root_link") and offset is not None:
+ try:
+ return groups, (
+ binding["root_link"],
+ np.asarray(_validate_gripper_transform(offset)),
+ )
+ except ValueError:
+ pass # The joint measurements still support a qpos comparison.
+ return groups, None
+
+
+def _tcp(clip: PreviewClip, links: dict[str, int]) -> np.ndarray:
+ root, offset = clip.tcp_frame
+ return clip.links[:, links[root]] @ offset
+
+
+def deviation_curves(data: PreviewData, link_names: list[str]) -> dict[str, Any]:
+ """Report the range and mean of each deviation on the reference clock."""
+ ref = data.solid_clip
+ times = ref.measured.timestamps
+ candidates = [data.clips[i] for i in data.ghost_indices]
+ counts = np.zeros(len(times), dtype=np.int64)
+ sums = {}
+ links = {name: i for i, name in enumerate(link_names)}
+ tcp_available = all(
+ c.tcp_frame is not None and c.tcp_frame[0] in links for c in (ref, *candidates)
+ )
+ if tcp_available:
+ original = _tcp(ref, links)
+ original_inverse = Rotation.from_matrix(original[:, :3, :3]).inv()
+ sums["translation_mm"] = np.zeros(len(times))
+ sums["rotation_deg"] = np.zeros(len(times))
+ for unit in data.arm_joint_groups:
+ sums[f"qpos_{unit}"] = np.zeros(len(times))
+ ranges = {key: (np.full(len(times), np.inf), np.zeros(len(times))) for key in sums}
+
+ def accumulate(key: str, values: np.ndarray) -> None:
+ end = len(values)
+ sums[key][:end] += values
+ low, high = ranges[key]
+ np.minimum(low[:end], values, out=low[:end])
+ np.maximum(high[:end], values, out=high[:end])
+
+ for c in candidates:
+ sample = c.measured
+ end = times.searchsorted(sample.timestamps[-1], side="right")
+ grid = times[:end]
+ counts[:end] += 1
+ if tcp_available:
+ tcp = _tcp(c, links)
+ xyz = np.column_stack(
+ [
+ np.interp(grid, sample.timestamps, tcp[:, axis, 3])
+ for axis in range(3)
+ ]
+ )
+ rotations = Slerp(sample.timestamps, Rotation.from_matrix(tcp[:, :3, :3]))(
+ grid
+ )
+ accumulate(
+ "translation_mm",
+ 1000 * np.linalg.norm(xyz - original[:end, :3, 3], axis=1),
+ )
+ accumulate(
+ "rotation_deg",
+ np.rad2deg((original_inverse[:end] * rotations).magnitude()),
+ )
+ for unit, names in data.arm_joint_groups.items():
+ worst = np.zeros(end)
+ for name in names:
+ q = sample.qpos[:, sample.joint_names.index(name)]
+ q_ref = ref.measured.qpos[:end, ref.measured.joint_names.index(name)]
+ delta = np.interp(grid, sample.timestamps, q) - q_ref
+ np.maximum(worst, np.abs(delta), out=worst)
+ accumulate(
+ f"qpos_{unit}", np.rad2deg(worst) if unit == "deg" else 1000 * worst
+ )
+
+ # Every recording starts at zero, so uncovered reference samples form a tail.
+ covered = np.count_nonzero(counts)
+ empty_tail = [None] * (len(times) - covered)
+ return {
+ "time_s": times.tolist(),
+ "count": counts.tolist(),
+ "episodes": len(candidates),
+ "explicit_reference": data.reference is not None,
+ "arm_joint_groups": data.arm_joint_groups,
+ "mean": {
+ key: (values[:covered] / counts[:covered]).tolist() + empty_tail
+ for key, values in sums.items()
+ },
+ "range": {
+ key: {
+ "min": low[:covered].tolist() + empty_tail,
+ "max": high[:covered].tolist() + empty_tail,
+ }
+ for key, (low, high) in ranges.items()
+ },
+ }
diff --git a/embodichain/lab/expansion/_preview_heatmap.py b/embodichain/lab/expansion/_preview_heatmap.py
new file mode 100644
index 000000000..87cf72ff5
--- /dev/null
+++ b/embodichain/lab/expansion/_preview_heatmap.py
@@ -0,0 +1,87 @@
+# ----------------------------------------------------------------------------
+# Copyright (c) 2021-2026 DexForce Technology Co., Ltd.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ----------------------------------------------------------------------------
+
+"""Measured starting positions of object_pose layout targets in the expansion replay."""
+
+from __future__ import annotations
+
+from typing import TYPE_CHECKING, Any
+
+import numpy as np
+
+if TYPE_CHECKING:
+ from ._preview_data import PreviewData
+
+__all__: list[str] = []
+
+_BINS = 32
+
+
+def _grid_edges(bounds: np.ndarray) -> np.ndarray:
+ """Fit each axis with half-cell margins or a 1 mm window for a constant coordinate."""
+ low, high = bounds
+ span = high - low
+ step = np.where(span > 0, span / (_BINS - 1), 0.001 / _BINS)
+ # Keep adjacent edges distinguishable at the measured world coordinates.
+ step = np.maximum(step, 2 * np.spacing(np.maximum(np.abs(low), np.abs(high))))
+ center = low + span / 2
+ return center[:, None] + (np.arange(_BINS + 1) - _BINS / 2) * step[:, None]
+
+
+def object_heatmaps(data: PreviewData) -> dict[str, Any]:
+ """Bin one measured start per targeted rigid object and object_pose input episode."""
+ starts: dict[str, list[np.ndarray]] = {}
+ episodes = 0
+ for clip in data.clips:
+ if clip.object_pose_uids:
+ episodes += 1
+ for uid in clip.object_pose_uids:
+ starts.setdefault(uid, []).append(clip.measured.object_poses[uid][0, :2])
+
+ specs = data.object_specs or {}
+ uids = sorted(
+ starts,
+ key=lambda uid: (
+ specs.get(uid, {}).get("body_type") in {"static", "kinematic"},
+ uid,
+ ),
+ )
+ objects = []
+ for uid in uids:
+ xy = np.asarray(starts[uid], dtype=float)
+ bounds = np.stack([xy.min(axis=0), xy.max(axis=0)])
+ x_edges, y_edges = _grid_edges(bounds)
+ # Canvas rows are Y and columns are X.
+ counts, _, _ = np.histogram2d(xy[:, 1], xy[:, 0], bins=(y_edges, x_edges))
+ objects.append(
+ {
+ "uid": uid,
+ "episodes": len(xy),
+ "x_edges_m": x_edges.tolist(),
+ "y_edges_m": y_edges.tolist(),
+ "views": {
+ "start": {
+ "counts": counts.astype(int).tolist(),
+ "bounds_m": bounds.tolist(),
+ }
+ },
+ }
+ )
+ return {
+ "episodes": episodes,
+ "explicit_reference": data.reference is not None,
+ "objects": objects,
+ }
diff --git a/embodichain/lab/expansion/_preview_render.py b/embodichain/lab/expansion/_preview_render.py
index 4591772de..1f2c7c409 100644
--- a/embodichain/lab/expansion/_preview_render.py
+++ b/embodichain/lab/expansion/_preview_render.py
@@ -21,11 +21,9 @@
from contextlib import contextmanager
from collections.abc import Iterator
from typing import Any
-import io
import math
import numpy as np
from scipy.spatial.transform import Rotation
-from PIL import Image
from ._preview_data import PreviewData, PreviewState, pose_matrix
__all__: list[str] = []
@@ -94,6 +92,7 @@ def __init__(
)
self.data, self.sim, self.publisher, self.center = data, sim, publisher, center
+ self.link_names = names
scene = sim.spawn_result
self.step = sim.physics_step_index
self.previous = None
@@ -106,7 +105,7 @@ def __init__(
and all(
np.allclose(
c.measured.object_poses[uid],
- data.clips[0].measured.object_poses[uid][0],
+ data.solid_clip.measured.object_poses[uid][0],
rtol=0,
atol=1e-7,
)
@@ -114,18 +113,16 @@ def __init__(
)
}
self.moving_objects = [uid for uid in data.boxes if uid not in self.backgrounds]
- for uid in self.backgrounds:
- self.models[uid].set_local_pose(
- data.clips[0].measured.object_poses[uid][0:1]
- )
- self.models[uid].set_visible(True)
+ for uid, model in self.models.items():
+ model.set_local_pose(data.solid_clip.measured.object_poses[uid][0:1])
+ model.set_visible(True)
palette = [
(0.18, 0.58, 1),
(0.15, 0.9, 0.5),
(0.9, 0.55, 0.2),
(0.7, 0.4, 0.95),
]
- variants = sorted({c.variant for c in data.clips})
+ variants = sorted({data.clips[i].variant for i in data.ghost_indices})
self.colors = {v: palette[i % len(palette)] for i, v in enumerate(variants)}
for name, pos, intensity in [
("preview_key", [-1, -2, 3], 35),
@@ -138,20 +135,16 @@ def __init__(
name=name, light_type="POINT", pose=pose, intensity=intensity
)
)
- self.robots = []
- self.objects = []
+ self.ghosts = {}
def markers(name: str, shapes: dict) -> Any:
return VisualizationMarkers(
VisualizationMarkersCfg(prim_path=name, markers=shapes), publisher
)
- for i, c in enumerate(data.all_clips):
- color = (
- self.colors.get(c.variant, (0.65, 0.7, 0.75))
- if i < len(data.clips)
- else (0.65, 0.7, 0.75)
- )
+ for i in data.ghost_indices:
+ c = data.clips[i]
+ color = self.colors[c.variant]
robot = RobotMarkers(
RobotMarkersCfg(
name=f"preview_robot_{i}",
@@ -160,7 +153,7 @@ def markers(name: str, shapes: dict) -> Any:
),
publisher,
)
- self.robots.append((robot, [names.index(n) for n in robot.link_names]))
+ link_order = [names.index(n) for n in robot.link_names]
objects = (
markers(
f"preview_objects_{i}",
@@ -172,10 +165,8 @@ def markers(name: str, shapes: dict) -> Any:
if self.moving_objects
else None
)
- self.objects.append(objects)
- robot.visualize_link_poses(
- c.links[0, [names.index(n) for n in robot.link_names]][None]
- )
+ self.ghosts[i] = (robot, link_order, objects)
+ robot.visualize_link_poses(c.links[0, link_order][None])
robot.set_visibility(False)
if objects:
p = np.array(
@@ -183,21 +174,21 @@ def markers(name: str, shapes: dict) -> Any:
)
objects.visualize(p[:, :3], p[:, 3:], marker_indices=np.arange(len(p)))
objects.set_visibility(False)
- self.selected = RobotMarkers(
+ self.solid_robot = RobotMarkers(
RobotMarkersCfg(
- name="selected_model",
+ name="solid_model",
asset_path=str(data.asset),
appearance=RobotMarkersAppearanceCfg(mode="asset", opacity=1),
),
publisher,
)
- self.selected_order = [names.index(n) for n in self.selected.link_names]
- self.selected.visualize_link_poses(
- data.clips[0].links[0, self.selected_order][None]
+ self.solid_order = [names.index(n) for n in self.solid_robot.link_names]
+ self.solid_robot.visualize_link_poses(
+ data.solid_clip.links[0, self.solid_order][None]
)
- self.selected_objects = (
+ self.solid_objects = (
markers(
- "selected_objects",
+ "solid_objects",
{
uid: self._object_prototype(
uid, color=(0.9, 0.58, 0.2, 1), scene_lighting=True, unlit=False
@@ -208,16 +199,15 @@ def markers(name: str, shapes: dict) -> Any:
if self.moving_objects and not self.models
else None
)
- self.paths = markers(
- "measured_paths",
- {
- str(i): MarkerPrototypeCfg(
- shape="cylinder", color=(*self.colors[v], 0.8)
- )
- for i, v in enumerate(variants)
- },
- )
- self.variant_ids = {v: i for i, v in enumerate(variants)}
+ path_shapes = {
+ "solid": MarkerPrototypeCfg(shape="cylinder", color=(1, 0.9, 0.7, 1))
+ }
+ self.variant_ids = {v: i for i, v in enumerate(variants, start=1)}
+ for variant, index in self.variant_ids.items():
+ path_shapes[str(index)] = MarkerPrototypeCfg(
+ shape="cylinder", color=(*self.colors[variant], 0.8)
+ )
+ self.paths = markers("measured_paths", path_shapes)
self.targets = markers(
"recorded_grasp_targets",
{"target": MarkerPrototypeCfg(shape="frame", scale=(0.065, 0.065, 0.065))},
@@ -255,11 +245,7 @@ def markers(name: str, shapes: dict) -> Any:
np.asarray(points).reshape(-1, 3),
marker_indices=np.asarray(types, dtype=int),
)
- self.camera = scene.add_camera(
- spawn.CameraDesc(name="expansion_preview_camera", resolution=(1100, 760))
- )
- self.camera.open_camera()
- self.camera.set_visual_overlay_enabled(True)
+ self.web_viewer = sim.viewer
publisher.publish(preload=True)
def _object_prototype(self, uid: str, **appearance: Any) -> Any:
@@ -272,16 +258,18 @@ def _object_prototype(self, uid: str, **appearance: Any) -> Any:
)
return MarkerPrototypeCfg(shape="box", scale=self.data.boxes[uid], **appearance)
- def draw(self, state: PreviewState) -> bytes:
+ def draw(self, state: PreviewState) -> None:
v = state.values
- visible = state.visible()
+ solid = self.data.solid_clip
+ visible = state.visible_ghosts()
if tuple(visible) != self.previous:
+ displayed = [solid, *(self.data.clips[i] for i in visible)]
positions = []
rotations = []
scales = []
types = []
- for i in visible:
- c = self.data.clips[i]
+ for c in displayed:
+ path_type = 0 if c is solid else self.variant_ids[c.variant]
points = c.tips[
np.unique(np.r_[np.arange(0, len(c.tips), 4), len(c.tips) - 1])
]
@@ -303,18 +291,14 @@ def draw(self, state: PreviewState) -> bytes:
positions.append((a + b) / 2)
rotations.append(rotation.as_quat())
scales.append([0.002, 0.002, length])
- types.append(self.variant_ids[c.variant])
+ types.append(path_type)
self.paths.visualize(
np.array(positions).reshape(-1, 3),
np.array(rotations).reshape(-1, 4),
np.array(scales).reshape(-1, 3),
np.asarray(types, dtype=int),
)
- targets = (
- np.concatenate([self.data.clips[i].targets for i in visible])
- if visible
- else np.empty((0, 4, 4))
- )
+ targets = np.concatenate([c.targets for c in displayed])
self.targets.visualize(
targets[:, :3, 3],
(
@@ -325,48 +309,33 @@ def draw(self, state: PreviewState) -> bytes:
)
initial = [
p[0, :3]
- for i in visible
- for uid, p in self.data.clips[i].measured.object_poses.items()
+ for c in displayed
+ for uid, p in c.measured.object_poses.items()
if uid not in self.backgrounds
]
self.positions.visualize(np.asarray(initial).reshape(-1, 3))
self.previous = tuple(visible)
- self.selected.set_visibility(bool(visible))
- if self.selected_objects:
- self.selected_objects.set_visibility(bool(visible))
- for uid, obj in self.models.items():
- if uid not in self.backgrounds:
- obj.set_visible(bool(visible))
- if visible:
- c = self.data.clips[v["selected"]]
- f = state.frame_at(c)
- self.selected.visualize_link_poses(c.links[f, self.selected_order][None])
- for uid in self.moving_objects:
- if uid in self.models:
- self.models[uid].set_local_pose(
- c.measured.object_poses[uid][f : f + 1]
- )
- if self.selected_objects:
- p = np.array(
- [c.measured.object_poses[uid][f] for uid in self.moving_objects]
- )
- self.selected_objects.visualize(
- p[:, :3], p[:, 3:], marker_indices=np.arange(len(p))
+ f = state.frame_at(solid)
+ self.solid_robot.visualize_link_poses(solid.links[f, self.solid_order][None])
+ for uid in self.moving_objects:
+ if uid in self.models:
+ self.models[uid].set_local_pose(
+ solid.measured.object_poses[uid][f : f + 1]
)
- ghost_count = 0
- for i, (c, (robot, order), objects) in enumerate(
- zip(self.data.all_clips, self.robots, self.objects)
- ):
- show = (
- v["reference"]
- if i == len(self.data.clips)
- else v["ghosts"] and i in visible and i != v["selected"]
+ if self.solid_objects:
+ p = np.array(
+ [solid.measured.object_poses[uid][f] for uid in self.moving_objects]
)
+ self.solid_objects.visualize(
+ p[:, :3], p[:, 3:], marker_indices=np.arange(len(p))
+ )
+ for i, (robot, order, objects) in self.ghosts.items():
+ show = i in visible
robot.set_visibility(show)
if objects:
objects.set_visibility(show)
if show:
- ghost_count += 1
+ c = self.data.clips[i]
f = state.frame_at(c)
robot.visualize_link_poses(c.links[f, order][None])
if objects:
@@ -387,36 +356,34 @@ def draw(self, state: PreviewState) -> bytes:
eye = self.center + d * np.array(
[math.cos(e) * math.cos(yaw), math.cos(e) * math.sin(yaw), math.sin(e)]
)
- self.camera.look_at(eye.tolist(), self.center.tolist(), [0, 0, 1])
- with self.sim.render_frame(capture_camera_images=False):
- self.sim.sync_render_state()
- self.camera.render()
+ self.web_viewer.set_look_at(eye.tolist(), self.center.tolist())
+ if not self.sim.capture_visualization(force=True, capture_camera_images=False):
+ raise RuntimeError(
+ f"WebRTC observer did not render: {self.web_viewer.error}"
+ )
if self.sim.physics_step_index != self.step:
raise RuntimeError("Preview unexpectedly advanced physics")
- image = Image.fromarray(
- np.ascontiguousarray(self.camera.get_rgb_map()[..., :3], dtype=np.uint8)
- )
- buffer = io.BytesIO()
- image.save(buffer, format="JPEG", quality=88)
self.stats = {
"physics_steps": self.sim.physics_step_index - self.step,
- "ghost_count": ghost_count,
+ "ghost_count": len(visible),
"path_segments": self.paths.count if v["paths"] else 0,
"grasp_targets": self.targets.count if v["targets"] else 0,
"scene_objects": len(self.data.boxes),
"fixed_backgrounds": len(self.backgrounds),
}
- return buffer.getvalue()
@contextmanager
-def native_renderer(data: PreviewData, gpu: int = 0) -> Iterator[NativeRenderer]:
+def native_renderer(
+ data: PreviewData, gpu: int = 0, *, web_port: int
+) -> Iterator[NativeRenderer]:
import os
os.environ.setdefault("SDL_VIDEODRIVER", "offscreen")
import torch
from embodichain.lab.sim import SimulationManager, SimulationManagerCfg
from embodichain.lab.sim.cfg import RenderCfg, DenoisingCfg
+ from embodichain.lab.visualization import DexSimWebCfg, VisualizationCfg
from embodichain.lab.visualization.markers import MarkerFramePublisher
torch.set_num_threads(1)
@@ -429,6 +396,10 @@ def native_renderer(data: PreviewData, gpu: int = 0) -> Iterator[NativeRenderer]
num_envs=1,
enable_entity_gizmo=False,
startup_summary="off",
+ web=True,
+ visualization=VisualizationCfg(
+ web=DexSimWebCfg(port=web_port, width=1100, height=760, fps=25)
+ ),
render_cfg=RenderCfg(
renderer="hybrid",
spp=4,
diff --git a/embodichain/lab/expansion/_preview_server.py b/embodichain/lab/expansion/_preview_server.py
index fa8392b29..d55473864 100644
--- a/embodichain/lab/expansion/_preview_server.py
+++ b/embodichain/lab/expansion/_preview_server.py
@@ -14,122 +14,67 @@
# limitations under the License.
# ----------------------------------------------------------------------------
-"""Loopback-only native preview transport. WebRTC integration remains a TODO."""
+"""Serve recorded expansion playback through the native WebRTC viewport."""
from __future__ import annotations
-import base64
+from collections import deque
import json
-import queue
-import threading
+import socket
import time
import webbrowser
-from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
-from pathlib import Path
-from typing import Any
-from urllib.parse import urlsplit
+from typing import TYPE_CHECKING
+from ._frontend import _preview_assets
from ._preview_data import PreviewData, PreviewState
+from ._preview_heatmap import object_heatmaps
+
+if TYPE_CHECKING:
+ from embodichain.lab.visualization._web_viewer import _WebViewer
__all__: list[str] = []
-class PreviewTransport:
- def __init__(self, state: PreviewState, port: int) -> None:
+def _json(value: object) -> str:
+ return json.dumps(value, allow_nan=False, separators=(",", ":"))
+
+
+class _WebPreviewTransport:
+ """Serve replay controls and statistics over the native video listener."""
+
+ def __init__(
+ self, state: PreviewState, viewer: _WebViewer, deviations: bytes
+ ) -> None:
self.state = state
- self.commands: queue.Queue[dict[str, Any]] = queue.Queue(maxsize=64)
- self.lock = threading.Lock()
- self.status: dict[str, Any] = {}
- self.jpeg = b""
- self.snapshot = b"{}"
- html = Path(__file__).with_name("_preview.html").read_bytes()
- owner = self
-
- class Handler(BaseHTTPRequestHandler):
- def setup(self) -> None:
- super().setup()
- self.connection.settimeout(3)
-
- def log_message(self, *_: object) -> None:
- pass
-
- def do_GET(self) -> None:
- if urlsplit("//" + self.headers.get("Host", "")).hostname not in {
- "127.0.0.1",
- "localhost",
- "::1",
- }:
- self.send_error(403)
- return
- route = self.path.split("?")[0]
- with owner.lock:
- if route == "/":
- data, kind = html, "text/html; charset=utf-8"
- elif route == "/state":
- data, kind = (
- json.dumps(owner.status, allow_nan=False).encode(),
- "application/json",
- )
- elif route == "/snapshot":
- data, kind = owner.snapshot, "application/json"
- elif route == "/frame.jpg":
- data, kind = owner.jpeg, "image/jpeg"
- else:
- self.send_error(404)
- return
- self.send_response(200)
- self.send_header("Content-Type", kind)
- self.send_header("Cache-Control", "no-store")
- self.send_header("X-Content-Type-Options", "nosniff")
- self.send_header("Content-Length", str(len(data)))
- self.end_headers()
- try:
- self.wfile.write(data)
- except (BrokenPipeError, ConnectionResetError):
- pass
-
- def do_POST(self) -> None:
- if self.path != "/control":
- self.send_error(404)
- return
- origin = self.headers.get("Origin")
- host = self.headers.get("Host", "")
- # Same-origin loopback controls also work through a different forwarded port.
- if (
- urlsplit("//" + host).hostname
- not in {"127.0.0.1", "localhost", "::1"}
- or self.headers.get("Content-Type") != "application/json"
- or (origin and origin != "http://" + host)
- ):
- self.send_error(403)
- return
- try:
- size = int(self.headers.get("Content-Length", "0"))
- if not 0 < size <= 4096:
- raise ValueError("control size")
- update = owner.state.validate(json.loads(self.rfile.read(size)))
- owner.commands.put_nowait(update)
- except (ValueError, TypeError, queue.Full, TimeoutError):
- self.send_error(400)
- return
- self.send_response(204)
- self.end_headers()
-
- self.server = ThreadingHTTPServer(("127.0.0.1", port), Handler)
- self.server.daemon_threads = True
- self.url = f"http://127.0.0.1:{self.server.server_port}/"
-
- def publish(self, status: dict[str, Any], jpeg: bytes) -> None:
- snapshot = json.dumps(
- {
- **status,
- "image": "data:image/jpeg;base64," + base64.b64encode(jpeg).decode(),
- },
- allow_nan=False,
- ).encode()
- with self.lock:
- self.status = status
- self.jpeg = jpeg
- self.snapshot = snapshot
+ self.commands: deque[dict[str, object]] = deque()
+ self.url = viewer.endpoint
+ self.status = "{}"
+ assets = _preview_assets()
+ assets["/object-heatmaps"] = (
+ "application/json",
+ _json(object_heatmaps(state.data)).encode(),
+ )
+ assets["/deviations"] = ("application/json", deviations)
+ viewer._replace_application(assets, self._request)
+
+ def _request(self, path: str, raw: str) -> tuple[int, str]:
+ if path == "/api/state":
+ return 200, self.status
+ if path != "/api/command":
+ return 404, '{"error":"unknown route"}'
+ # DexSim has already checked same-origin and the current viewer token.
+ if len(self.commands) >= 64:
+ return 429, '{"error":"replay control queue is full"}'
+ try:
+ if len(raw) > 4096:
+ raise ValueError("control size")
+ update = self.state.validate(json.loads(raw))
+ except (ValueError, TypeError):
+ return 400, '{"error":"invalid replay control"}'
+ self.commands.append(update)
+ return 200, "{}"
+
+ def publish(self, status: dict[str, object]) -> None:
+ self.status = _json(status)
def serve_preview(
@@ -140,73 +85,77 @@ def serve_preview(
open_browser: bool = False,
duration: float = 0,
) -> None:
+ """Replay confirmed episodes in a browser using the native observer."""
+ from ._preview_deviations import deviation_curves
from ._preview_render import native_renderer
- state = PreviewState(data)
- transport = PreviewTransport(state, port)
- thread = None
- try:
- with native_renderer(data, gpu) as renderer:
- frame = 0
-
- def publish() -> None:
- nonlocal frame
- jpeg = renderer.draw(state)
- transport.publish(
- {
- **state.snapshot(),
- **renderer.stats,
- "frame": frame,
- "renderer": "EmbodiChain / DexSim Hybrid + OptiX",
- "transport": "HTTP JPEG",
- },
- jpeg,
- )
- frame += 1
+ if port == 0:
+ # The native World configuration requires a concrete listening port.
+ with socket.socket() as probe:
+ probe.bind(("0.0.0.0", 0))
+ port = probe.getsockname()[1]
- publish()
- thread = threading.Thread(
- target=transport.server.serve_forever, daemon=True
- )
- thread.start()
- print(
- json.dumps(
- {
- "state": "ready",
- "url": transport.url,
- "episodes": len(data.clips),
- "physics_steps": 0,
- "transport": "HTTP JPEG",
- }
- ),
- flush=True,
+ state = PreviewState(data)
+ with native_renderer(data, gpu, web_port=port) as renderer:
+ viewer = renderer.web_viewer
+ deviations = _json(deviation_curves(data, renderer.link_names)).encode()
+ transport = _WebPreviewTransport(state, viewer, deviations)
+ frame_period = 1 / viewer.cfg.fps
+ frame = 0
+
+ def publish() -> None:
+ nonlocal frame
+ renderer.draw(state)
+ transport.publish(
+ {
+ **state.snapshot(),
+ **renderer.stats,
+ "frame": frame,
+ "transport": "WebRTC",
+ }
)
- if open_browser:
- webbrowser.open(transport.url)
- start = last = time.monotonic()
- while not duration or time.monotonic() - start < duration:
- now = time.monotonic()
- elapsed = now - last
- last = now
+ frame += 1
+
+ publish()
+ print(
+ json.dumps(
+ {
+ "state": "ready",
+ "url": transport.url,
+ "episodes": len(data.clips),
+ "physics_steps": 0,
+ "transport": "WebRTC",
+ }
+ ),
+ flush=True,
+ )
+ if open_browser:
+ webbrowser.open(transport.url)
+
+ start = last = next_frame = time.monotonic()
+ next_idle_frame = start + 0.25
+ dirty = False
+ while not duration or time.monotonic() - start < duration:
+ now = time.monotonic()
+ elapsed = now - last
+ last = now
+ while transport.commands:
+ state.apply(transport.commands.popleft())
+ dirty = True
+ if state.values["playing"]:
+ state.values["progress"] = min(
+ 1.0, state.values["progress"] + elapsed / state.duration
+ )
+ if state.values["progress"] >= 1:
+ state.values["playing"] = False
+ dirty = True
+ if now >= next_frame and (dirty or now >= next_idle_frame):
+ # Capturing the native observer also services its app requests.
+ publish()
dirty = False
- while True:
- try:
- state.apply(transport.commands.get_nowait())
- dirty = True
- except queue.Empty:
- break
- if state.values["playing"]:
- state.values["progress"] = min(
- 1.0, state.values["progress"] + elapsed / state.duration
- )
- if state.values["progress"] >= 1:
- state.values["playing"] = False
- dirty = True
- if dirty:
- publish()
- time.sleep(max(0.01, 0.1 - (time.monotonic() - now)))
- finally:
- if thread is not None:
- transport.server.shutdown()
- thread.join(timeout=5)
- transport.server.server_close()
+ next_frame = now + frame_period
+ next_idle_frame = now + 0.25
+ else:
+ viewer._process_application_requests()
+ wait = min(0.01, next_frame - time.monotonic()) if dirty else 0.01
+ time.sleep(max(0.001, wait))
diff --git a/embodichain/lab/expansion/_scene_first_sampling.py b/embodichain/lab/expansion/_scene_first_sampling.py
index ff94f5d5a..26d82b118 100644
--- a/embodichain/lab/expansion/_scene_first_sampling.py
+++ b/embodichain/lab/expansion/_scene_first_sampling.py
@@ -39,6 +39,16 @@
__all__: list[str] = []
+def _layout_collision_bodies(env):
+ from embodichain.lab.task_program.integrations.simulation.feasibility_geometry import (
+ _physical_collision_bodies,
+ )
+
+ mesh_parts = {}
+ bodies = _physical_collision_bodies(env, env_id=0, layout_mesh_parts=mesh_parts)
+ return bodies, mesh_parts
+
+
class _LayoutPoseSource(_AdaptivePoseSource):
"""Generate or certify complete layouts before checking task branches.
@@ -141,18 +151,15 @@ def __init__(self, env, options, *, seed, revision):
)
self._workspace_seconds = time.perf_counter() - started
- from embodichain.lab.task_program.integrations.simulation.feasibility_geometry import (
- collision_bodies_from_simulation,
- )
from .acceptance import _GeometryRow
from embodichain.lab.sim.scene.feasibility import NewtonCollisionChecker
- bodies = collision_bodies_from_simulation(env)
+ bodies, self._layout_mesh_parts = _layout_collision_bodies(env)
known = {b.frame_uid for b in bodies}
ignored, _ = _GeometryRow.contact_permissions(env, options["validation"], known)
try:
self._collision = NewtonCollisionChecker(
- bodies, ignored_pairs=ignored, device="cpu", max_shapes=512
+ bodies, ignored_pairs=ignored, device="cpu"
)
self._check_clock(env.sim, before)
except BaseException:
@@ -381,6 +388,8 @@ def checkpoint(self):
dict(
phase_checks=self._task_checks,
layout_source=self._prepared_layout.source.fingerprint,
+ mesh_convex_parts=self._layout_mesh_parts,
+ rigid_mesh_box_proxies=[], # Retained for existing report readers.
conditions=self._conditions.to_dict(),
workspace_prior=[pool.metrics() for pool in self._anchor_pools],
workspace_anchor_checks=self._anchor_checks,
diff --git a/embodichain/lab/expansion/acceptance.py b/embodichain/lab/expansion/acceptance.py
index 2b7937e28..8dfba2625 100644
--- a/embodichain/lab/expansion/acceptance.py
+++ b/embodichain/lab/expansion/acceptance.py
@@ -162,7 +162,7 @@ def __init__(
checker = None if checker_pool is None else checker_pool.get(key)
if checker is None:
checker = NewtonCollisionChecker(
- bodies, ignored_pairs=ignored, device=device, max_shapes=512
+ bodies, ignored_pairs=ignored, device=device
)
if checker_pool is not None:
checker_pool[key] = checker
@@ -733,20 +733,13 @@ def _sample_geometry(self, ids, action):
moving = tuple(uid for uid in check.moving if uid in aliases)
groups.setdefault((checker, tuple(allowed), moving), []).append(row)
for (checker, allowed, moving), rows in groups.items():
- # Respect the existing aggregate contact-buffer ceiling. Compact
- # selected rows also prevent inactive/heterogeneous rows consuming it.
- width = max(1, min(env.num_envs, 1048576 // checker._capacity))
- for start in range(0, len(rows), width):
- selected = rows[start : start + width]
- reports = checker.check_batch(
- {uid: values[selected] for uid, values in frames.items()},
- allowed_contacts=allowed,
- moving_objects=moving,
- )
- for row, report, pair in zip(
- selected, reports, checker.last_batch_pairs
- ):
- self.rows[row].record_geometry(report, pair, action)
+ reports = checker.check_batch(
+ {uid: values[rows] for uid, values in frames.items()},
+ allowed_contacts=allowed,
+ moving_objects=moving,
+ )
+ for row, report, pair in zip(rows, reports, checker.last_batch_pairs):
+ self.rows[row].record_geometry(report, pair, action)
def close(self) -> None:
"""Detach owned observers before releasing resources; safe on failure."""
diff --git a/embodichain/lab/expansion/preview.py b/embodichain/lab/expansion/preview.py
index b136d8586..a0b5b4d39 100644
--- a/embodichain/lab/expansion/preview.py
+++ b/embodichain/lab/expansion/preview.py
@@ -144,7 +144,7 @@ def launch_expansion_preview(
Args:
paths: Native job, aggregate result or preview-plan directories.
output: New directory for owned service status and logs.
- port: Loopback port; zero allocates an available port.
+ port: Browser preview port; zero selects an available port.
gpu: Visible GPU index used by the read-only renderer.
duration: Service seconds after readiness; zero waits for interruption.
open_browser: Open a browser after native rendering is ready.
diff --git a/embodichain/lab/expansion/task_program_jobs.py b/embodichain/lab/expansion/task_program_jobs.py
index d62af0d1c..728965261 100644
--- a/embodichain/lab/expansion/task_program_jobs.py
+++ b/embodichain/lab/expansion/task_program_jobs.py
@@ -68,18 +68,17 @@ def source(env):
nonlocal pose_source
from ._scene_first_sampling import _LayoutPoseSource
- source_type = (
- _LayoutPoseSource
- if "layout" in options.get("scene_sampling", {})
- else _AdaptivePoseSource
- )
- pose_source = (
- source_type(env, options, seed=seed, revision=report["semantic_id"])
- if "scene_sampling" in options
- else _BoundedPoseSource(
- env, options["scene"], seed=seed, revision=report["semantic_id"]
+ revision = report["semantic_id"]
+ if "layout" in options.get("scene_sampling", {}):
+ pose_source = _LayoutPoseSource(env, options, seed=seed, revision=revision)
+ elif "scene_sampling" in options:
+ pose_source = _AdaptivePoseSource(
+ env, options, seed=seed, revision=revision
+ )
+ else:
+ pose_source = _BoundedPoseSource(
+ env, options["scene"], seed=seed, revision=revision
)
- )
return pose_source
def feedback(env, source, path):
diff --git a/embodichain/lab/sim/scene/expansion/_layout_numeric.py b/embodichain/lab/sim/scene/expansion/_layout_numeric.py
index 3e6175cc9..3899df861 100644
--- a/embodichain/lab/sim/scene/expansion/_layout_numeric.py
+++ b/embodichain/lab/sim/scene/expansion/_layout_numeric.py
@@ -226,7 +226,17 @@ def descendants(root: int) -> list[int]:
if validate:
table = policy["_table"]
for i, box in enumerate(group_boxes):
- if np.any(box[:, 0] < np.asarray(table)[0] - 1e-8) or np.any(
+ offsets = geometry.get("root_support_offsets", {}).get(
+ nodes[roots[i]]["uid"]
+ )
+ if offsets is not None:
+ if np.any(deltas[:, i] < np.asarray(offsets)[0] - 1e-8) or np.any(
+ deltas[:, i] > np.asarray(offsets)[1] + 1e-8
+ ):
+ raise ValueError(
+ "Root displacement exceeds its supported contact patch."
+ )
+ elif np.any(box[:, 0] < np.asarray(table)[0] - 1e-8) or np.any(
box[:, 1] > np.asarray(table)[1] + 1e-8
):
raise ValueError("Hierarchical group lies outside the table bounds.")
diff --git a/embodichain/lab/sim/scene/expansion/_root_support.py b/embodichain/lab/sim/scene/expansion/_root_support.py
new file mode 100644
index 000000000..93935f4ae
--- /dev/null
+++ b/embodichain/lab/sim/scene/expansion/_root_support.py
@@ -0,0 +1,131 @@
+# ----------------------------------------------------------------------------
+# Copyright (c) 2021-2026 DexForce Technology Co., Ltd.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ----------------------------------------------------------------------------
+
+"""Contact-supported XY translations of rigid objects in bounded layout proposals."""
+
+from __future__ import annotations
+
+import numpy as np
+import shapely
+import trimesh
+from scipy.spatial import QhullError
+
+from ..geometry import SupportSurfaceConfig
+
+__all__: list[str] = []
+
+# Accommodate small authored mesh gaps before the physical settling step.
+_CONTACT_HEIGHT_TOLERANCE_M = 0.003
+
+
+def _horizontal_patch(triangles, bottom, *, upward):
+ """Project near-contact faces at the authored base height, retaining holes."""
+ cross = np.cross(
+ triangles[:, 1] - triangles[:, 0], triangles[:, 2] - triangles[:, 0]
+ )
+ norm = np.linalg.norm(cross, axis=1)
+ z = cross[:, 2] if upward else -cross[:, 2]
+ mask = (
+ (norm > 1e-14)
+ & (z >= SupportSurfaceConfig().normal_z_min * norm)
+ & (
+ np.abs(triangles[:, :, 2] - bottom).max(axis=1)
+ <= _CONTACT_HEIGHT_TOLERANCE_M
+ )
+ )
+ return shapely.union_all(shapely.polygons(triangles[mask, :, :2]))
+
+
+def _root_support_offsets(source, nodes, table, bounds, movable_roots):
+ """Certify bounded displacements from a measured contact patch on the named support.
+
+ Roots translate at fixed height and orientation. Eroding the support by the
+ displacement radius leaves contact points that stay supported at *every*
+ admitted offset. Their convex hull must strictly surround the projected
+ uniform-density centroid (convex envelope for open meshes). Body extents
+ still govern obstacle clearance.
+ """
+ if not bounds:
+ return {}
+ parents = {node["parent"] for node in nodes}
+ result = {}
+ support_triangles = None
+ for index, node in enumerate(nodes):
+ uid = node["uid"]
+ if (
+ node["parent"] >= 0
+ or index in parents
+ or (uid in movable_roots and uid not in bounds)
+ ):
+ continue
+ pose = np.asarray(source.poses[uid])
+ corners = np.asarray(node["corners"]) @ pose[:3, :3].T + pose[:3, 3]
+ if np.all(corners[:, :2].min(0) >= table[0]) and np.all(
+ corners[:, :2].max(0) <= table[1]
+ ):
+ continue
+ if support_triangles is None:
+ support = source.support_uid
+ transform = np.asarray(source.poses[support])
+ vertices = source.vertices[support] @ transform[:3, :3].T + transform[:3, 3]
+ support_triangles = vertices[source.faces[support]]
+ vertices = source.vertices[uid] @ pose[:3, :3].T + pose[:3, 3]
+ bottom = vertices[:, 2].min()
+ footprint = _horizontal_patch(vertices[source.faces[uid]], bottom, upward=False)
+ support_patch = _horizontal_patch(support_triangles, bottom, upward=True)
+ if footprint.is_empty or support_patch.is_empty:
+ continue
+ mesh = trimesh.Trimesh(vertices, source.faces[uid])
+ # Open surfaces do not define a physical volume. Their convex envelope
+ # supplies a reproducible centroid estimate for proposal generation.
+ try:
+ volume = mesh if mesh.is_volume else mesh.convex_hull
+ except QhullError:
+ continue
+ center = shapely.Point(volume.center_mass[:2])
+
+ def stable(radius):
+ # GEOS approximates circular arcs with chords. Enlarge the erosion
+ # radius so even a chord around a hole clears the requested disk.
+ interior = (
+ support_patch.buffer(-radius / np.cos(np.pi / 64), quad_segs=16)
+ if radius
+ else support_patch
+ )
+ contact = interior.intersection(footprint)
+ return contact.area > 0 and contact.convex_hull.contains(center)
+
+ if not stable(0):
+ continue
+ offsets = np.asarray(bounds.get(uid, [[0, 0], [0, 0]]), dtype=float)
+ radius = np.linalg.norm(np.max(np.abs(offsets), axis=0))
+ if not stable(radius):
+ low, high = 0.0, radius
+ for _ in range(16):
+ mid = (low + high) / 2
+ if stable(mid):
+ low = mid
+ else:
+ high = mid
+ # Preserve the requested aspect ratio, including one-dimensional ranges.
+ half = np.max(np.abs(offsets), axis=0) * (low / radius)
+ offsets = np.array(
+ [np.maximum(offsets[0], -half), np.minimum(offsets[1], half)]
+ )
+ if np.any(offsets[1] < offsets[0]):
+ continue
+ result[uid] = offsets.tolist()
+ return result
diff --git a/embodichain/lab/sim/scene/expansion/layout.py b/embodichain/lab/sim/scene/expansion/layout.py
index 17763a96f..47b59e731 100644
--- a/embodichain/lab/sim/scene/expansion/layout.py
+++ b/embodichain/lab/sim/scene/expansion/layout.py
@@ -38,6 +38,7 @@
from ..query import _backend_tensor
from .contracts import ScenePoseChange, SceneVariant
from .conditions import SceneLayoutCondition, _condition_prepared
+from ._root_support import _root_support_offsets
from ._layout_numeric import (
_hierarchy_batch_limit,
_numeric_geometry,
@@ -359,7 +360,9 @@ def subtree(uid: str) -> set[str]:
key = _digest(
{
"source": source.fingerprint,
- "extractor": "scene-layout-support/v3",
+ "extractor": "scene-layout-support/v4",
+ "root_offsets": policy["group_xy_offset_bounds_m"],
+ "movable_roots": sorted(selected_roots),
"intra": policy["intra_group"],
}
)
@@ -433,6 +436,11 @@ def subtree(uid: str) -> set[str]:
)
lo_x, lo_y, hi_x, hi_y = rectangle.bounds
table = ((lo_x, lo_y), (hi_x, hi_y))
+ offsets = _root_support_offsets(
+ source, nodes, table, policy["group_xy_offset_bounds_m"], selected_roots
+ )
+ if offsets:
+ geometry["root_support_offsets"] = offsets
if path is not None:
from .persistence import _write_atomic
@@ -560,6 +568,14 @@ def generate_scene_variants(
if policy["randomize_groups"]
else policy["batch_size"]
)
+ support_offsets = prepared._geometry.get("root_support_offsets", {})
+ root_support_offsets = {
+ i: torch.as_tensor(
+ support_offsets[node["uid"]], dtype=spec[0].dtype, device=device
+ )
+ for i, node in enumerate(prepared._geometry["nodes"])
+ if node["uid"] in support_offsets
+ }
deltas, samples, ordinals = [], [], []
accepted = attempts = 0
while attempts < max_attempts and accepted < candidate_count:
@@ -575,6 +591,7 @@ def generate_scene_variants(
support_gap=policy["intra_group"]["support_gap_m"],
movable_roots=prepared._movable_roots,
anchored_roots=anchored_roots,
+ root_support_offsets=root_support_offsets,
root_offset_bounds={
i: torch.tensor(
policy["group_xy_offset_bounds_m"][node["uid"]],
diff --git a/embodichain/lab/sim/scene/expansion/persistence.py b/embodichain/lab/sim/scene/expansion/persistence.py
index ac04e8569..df6e9bfb0 100644
--- a/embodichain/lab/sim/scene/expansion/persistence.py
+++ b/embodichain/lab/sim/scene/expansion/persistence.py
@@ -39,6 +39,7 @@
prepare_scene_randomization,
)
from ._layout_numeric import _numeric_geometry
+from ._root_support import _root_support_offsets
__all__ = ["LoadedSceneVariants", "save_scene_variants", "load_scene_variants"]
@@ -505,9 +506,33 @@ def load_scene_variants(
or np.any(bounds[0] >= bounds[1])
):
raise ValueError("Invalid saved support bounds.")
+ policy = _policy_data(policy)
+ if "root_support_offsets" in geometry:
+ # Anchors can narrow the saved policy after preparation. Verify
+ # the full stored rectangle independently against the source.
+ offsets = geometry["root_support_offsets"]
+ for value in offsets.values():
+ limits = np.asarray(value, dtype=float)
+ if (
+ limits.shape != (2, 2)
+ or not np.isfinite(limits).all()
+ or np.any(limits[0] > limits[1])
+ ):
+ raise ValueError("Invalid saved root support offsets.")
+ verified = _root_support_offsets(
+ source,
+ geometry["nodes"],
+ bounds,
+ offsets,
+ {uids[i] for i in saved["movable_roots"]},
+ )
+ if offsets != verified:
+ raise ValueError(
+ "Saved root support offsets differ from the source contact patches."
+ )
prepared = PreparedSceneRandomization(
source,
- _policy_data(policy),
+ policy,
manifest["geometry_sha256"],
moving,
geometry,
diff --git a/embodichain/lab/sim/scene/feasibility/_collision_batches.py b/embodichain/lab/sim/scene/feasibility/_collision_batches.py
index 6ffe907fc..fd9d4ca01 100644
--- a/embodichain/lab/sim/scene/feasibility/_collision_batches.py
+++ b/embodichain/lab/sim/scene/feasibility/_collision_batches.py
@@ -57,6 +57,7 @@ def __init__(self, checker, count, allowed, moving, all_pairs=False):
gap=checker.detection_gap_m / 2,
margin=0.0,
density=0.0,
+ collision_group=0, # All query pairs are supplied explicitly.
collision_filter_parent=False,
)
for index in checker._native:
@@ -98,19 +99,8 @@ def __init__(self, checker, count, allowed, moving, all_pairs=False):
self.model = batch.finalize(device=checker.device)
self.state = self.model.state()
width = len(checker._native)
- pairs = [
- (checker._native_lookup[i], checker._native_lookup[j])
- for i, j in checker._pairs
- if i in checker._native_lookup and j in checker._native_lookup
- ]
- explicit = np.asarray(
- [
- (a + row * width, b + row * width)
- for row in range(count)
- for a, b in pairs
- ],
- dtype=np.int32,
- )
+ offsets = np.arange(count, dtype=np.int32) * width
+ explicit = (checker._shape_pairs + offsets[:, None, None]).reshape(-1, 2)
self.explicit = wp.array(explicit, dtype=wp.vec2i, device=checker.device)
self.capacity = checker._capacity * count
self.pipeline = newton.CollisionPipeline(
@@ -170,6 +160,7 @@ def _run(self):
contacts.rigid_contact_margin1,
self.model.shape_body,
self.state.body_q,
+ checker._pair_keys,
self.active,
len(checker._native),
self.count,
@@ -303,14 +294,13 @@ def check(self, frame_poses, rows):
"Batched Newton query overflowed or returned invalid evidence"
)
if row not in failures and math.isfinite(summary[row, 1]):
- a, b = divmod(int(summary[row, 1]), len(checker._native))
- failures[row] = (checker._native[a], checker._native[b])
+ failures[row] = checker._native_pairs[int(summary[row, 1])]
self.queries += 1
result = []
for row in rows:
if row in failures:
i, j = failures[row]
- pair = checker.bodies[i].collision_id, checker.bodies[j].collision_id
+ pair = checker._physical_pair(i, j)
report = FeasibilityCheck(
"collision",
"failed",
diff --git a/embodichain/lab/sim/scene/feasibility/collision.py b/embodichain/lab/sim/scene/feasibility/collision.py
index f45478e3e..3a8da03df 100644
--- a/embodichain/lab/sim/scene/feasibility/collision.py
+++ b/embodichain/lab/sim/scene/feasibility/collision.py
@@ -21,9 +21,12 @@
from collections.abc import Mapping, Sequence
from collections import OrderedDict
from dataclasses import dataclass
+from functools import cached_property
from importlib.metadata import version
+from itertools import islice
import math
from numbers import Integral
+from typing import ClassVar
import numpy as np
@@ -33,6 +36,11 @@
__all__ = ["CollisionBody", "NewtonCollisionChecker", "UnsupportedFeasibilityError"]
+# Each explicit pair has one mask entry. Its ordinal fits exactly in the
+# float32 contact summary, including when a heavily decomposed shape has a large ID.
+_MAX_QUERY_PAIRS = 1 << 20
+_MAX_CONTACTS = 1 << 20
+
class UnsupportedFeasibilityError(RuntimeError):
"""Required geometry, task adaptation or runtime capability is unavailable."""
@@ -70,6 +78,7 @@ class CollisionBody:
vertices: Mesh-local positions; scale must already be applied.
triangles: Mesh triangle indices.
robot: Whether this shape belongs to a robot.
+ source_collision_id: Physical collider shared by its convex parts.
"""
collision_id: str
@@ -85,10 +94,13 @@ class CollisionBody:
vertices: tuple[tuple[float, float, float], ...] = ()
triangles: tuple[tuple[int, int, int], ...] = ()
robot: bool = False
+ source_collision_id: str | None = None
def __post_init__(self) -> None:
_identifier(self.collision_id, "collision_id")
_identifier(self.frame_uid, "frame_uid")
+ if self.source_collision_id is not None:
+ _identifier(self.source_collision_id, "source_collision_id")
object.__setattr__(self, "local_pose", _rigid_pose(self.local_pose))
size = finite(self.dimensions, "shape dimensions")
expected = {
@@ -151,7 +163,8 @@ class NewtonCollisionChecker:
numeric_tolerance_m: Conservative contact threshold and returned-distance
allowance for float32 geometry.
use_cuda_graph: Capture repeated CUDA queries after a warm call.
- max_shapes: Explicit shape budget, at most 512; bounds pair storage.
+ max_shapes: Optional caller limit on physical shapes. Storage is budgeted
+ separately for eligible collision pairs and actual contacts.
max_contacts: Upper contact-buffer budget; saturation is unsupported.
Returns from check contain a capped conservative clearance estimate, not
@@ -163,6 +176,8 @@ class NewtonCollisionChecker:
representation/precision budgets return unsupported.
"""
+ query_contract: ClassVar[str] = "newton-convex-query/v2"
+
def __init__(
self,
bodies: Sequence[CollisionBody],
@@ -173,15 +188,18 @@ def __init__(
detection_gap_m: float = 0.03,
numeric_tolerance_m: float = 1e-6,
use_cuda_graph: bool = True,
- max_shapes: int = 256,
- max_contacts: int = 1048576,
+ max_shapes: int | None = None,
+ max_contacts: int = _MAX_CONTACTS,
) -> None:
self.bodies = tuple(bodies)
- count(max_shapes, "max_shapes")
+ if max_shapes is not None:
+ count(max_shapes, "max_shapes")
count(max_contacts, "max_contacts")
- if max_contacts > 1048576:
- raise ValueError("max_contacts must not exceed 1048576")
- if max_shapes > 512 or not 1 <= len(self.bodies) <= max_shapes:
+ if max_contacts > _MAX_CONTACTS:
+ raise ValueError(f"max_contacts must not exceed {_MAX_CONTACTS}")
+ if not self.bodies or (
+ max_shapes is not None and len(self.bodies) > max_shapes
+ ):
raise UnsupportedFeasibilityError(
"Physical collision shape budget exceeded"
)
@@ -257,8 +275,21 @@ def __init__(
self._use_cuda_graph = use_cuda_graph
self._aliases = {}
ids = {b.collision_id for b in self.bodies}
+ sources = {}
for body in self.bodies:
self._aliases.setdefault(body.frame_uid, set()).add(body.collision_id)
+ if body.source_collision_id is not None:
+ frame, parts = sources.setdefault(
+ body.source_collision_id, (body.frame_uid, set())
+ )
+ if frame != body.frame_uid:
+ raise ValueError("A source collider must belong to a single frame")
+ parts.add(body.collision_id)
+ for source, (_, parts) in sources.items():
+ if self._aliases.setdefault(source, parts) != parts:
+ raise ValueError(
+ "Source collider and frame aliases must be unambiguous"
+ )
if any(
token in ids and targets != {token}
for token, targets in self._aliases.items()
@@ -268,7 +299,8 @@ def __init__(
)
for collision_id in ids:
self._aliases.setdefault(collision_id, {collision_id})
- self.ignored_pairs = frozenset(self._expand_pairs(ignored_pairs))
+ self._ignored_alias_pairs = self._permission_pairs(ignored_pairs)
+ ignored_targets = self._permission_targets(self._ignored_alias_pairs)
self.last_pair = None
self.last_clearance_interval: tuple[float, float] | None = None
self._frame_uids = tuple(dict.fromkeys(b.frame_uid for b in self.bodies))
@@ -278,23 +310,36 @@ def __init__(
self._native_lookup = {
logical: index for index, logical in enumerate(self._native)
}
- self._pairs = tuple(
+ eligible = (
(i, j)
for i in range(len(self.bodies))
for j in range(i + 1, len(self.bodies))
if self.bodies[i].frame_uid != self.bodies[j].frame_uid
- and frozenset((self.bodies[i].collision_id, self.bodies[j].collision_id))
- not in self.ignored_pairs
+ and self.bodies[j].frame_uid not in ignored_targets[i]
+ and self.bodies[j].collision_id not in ignored_targets[i]
+ and self.bodies[j].source_collision_id not in ignored_targets[i]
)
- native_pairs = [
- (self._native_lookup[i], self._native_lookup[j])
- for i, j in self._pairs
+ pairs = tuple(islice(eligible, _MAX_QUERY_PAIRS + 1))
+ if len(pairs) > _MAX_QUERY_PAIRS:
+ raise UnsupportedFeasibilityError("Explicit collision pair budget exceeded")
+ self._native_pairs = tuple(
+ (i, j)
+ for i, j in pairs
if i in self._native_lookup and j in self._native_lookup
- ]
- requested_capacity = max(64, 16 * len(native_pairs))
- if requested_capacity > 1048576:
- raise UnsupportedFeasibilityError("Physical collision pair budget exceeded")
- self._capacity = min(max_contacts, requested_capacity)
+ )
+ self._plane_pairs = tuple(
+ (i, j)
+ for i, j in pairs
+ if i not in self._native_lookup or j not in self._native_lookup
+ )
+ self._shape_pairs = np.asarray(
+ [
+ (self._native_lookup[i], self._native_lookup[j])
+ for i, j in self._native_pairs
+ ],
+ dtype=np.int32,
+ ).reshape(-1, 2)
+ self._capacity = min(max_contacts, max(64, 16 * len(self._native_pairs)))
self._mask_cache = OrderedDict()
self._batch_queries = OrderedDict()
self.last_batch_pairs: tuple[tuple[str, str] | None, ...] = ()
@@ -302,17 +347,17 @@ def __init__(
self._graph = self._active = self._output = self._empty_count = (
self._explicit
) = None
- self._tolerance = None
+ self._pair_keys = self._tolerance = None
self._meshes = []
self._closed = False
self._version = version("newton")
self.verification_id = digest(
(
- "newton-convex-query/v1",
+ self.query_contract,
"relative-origin/scale-allowance/v1",
self._version,
self.bodies,
- self.ignored_pairs,
+ self._ignored_alias_pairs,
self.clearance_m,
self.detection_gap_m,
self.numeric_tolerance_m,
@@ -323,8 +368,8 @@ def __init__(
)
)
try:
- if native_pairs:
- self._build_query(native_pairs)
+ if self._native_pairs:
+ self._build_query()
except Exception:
self.close()
raise
@@ -348,13 +393,14 @@ def metadata(self) -> dict[str, str | float | int | bool]:
"max_effective_tolerance_m": self._max_effective_tolerance,
"max_abs_query_coordinate_m": 1e6,
"shape_count": len(self.bodies),
+ "native_pair_count": len(self._native_pairs),
"contact_capacity": self._capacity,
"cuda_graph": "captured" if self._graph is not None else "disabled",
"halfspace_check": "analytic_physical_support",
"continuous": False,
}
- def _build_query(self, pairs: list[tuple[int, int]]) -> None:
+ def _build_query(self) -> None:
import newton
import warp as wp
from embodichain.compute.geometry._warp.contact_query import _contact_summary
@@ -366,6 +412,9 @@ def _build_query(self, pairs: list[tuple[int, int]]) -> None:
gap=self.detection_gap_m / 2,
margin=0.0,
density=0.0,
+ # Leave the builder's default pair table empty. The explicit
+ # pipeline supplies all eligible pairs.
+ collision_group=0,
collision_filter_parent=False,
)
for index in self._native:
@@ -406,9 +455,13 @@ def _build_query(self, pairs: list[tuple[int, int]]) -> None:
)
self._model = builder.finalize(device=self.device)
self._state = self._model.state()
- self._explicit = wp.array(
- np.asarray(pairs, dtype=np.int32), dtype=wp.vec2i, device=self.device
+ self._explicit = wp.array(self._shape_pairs, dtype=wp.vec2i, device=self.device)
+ # Native shape IDs follow logical order, so these keys are sorted.
+ keys = (
+ self._shape_pairs[:, 0].astype(np.int64) * len(self._native)
+ + self._shape_pairs[:, 1]
)
+ self._pair_keys = wp.array(keys, dtype=wp.int64, device=self.device)
self._pipeline = newton.CollisionPipeline(
self._model,
broad_phase="explicit",
@@ -423,13 +476,26 @@ def _build_query(self, pairs: list[tuple[int, int]]) -> None:
)
self._contacts = self._pipeline.contacts()
self._active = wp.zeros(
- len(self._native) ** 2, dtype=wp.int32, device=self.device
+ len(self._native_pairs), dtype=wp.int32, device=self.device
)
self._output = wp.empty(4, dtype=float, device=self.device)
self._empty_count = wp.zeros(1, dtype=wp.int32, device=self.device)
self._tolerance = wp.empty(1, dtype=float, device=self.device)
- def _expand_pairs(self, pairs: Sequence[tuple[str, str]]) -> set[frozenset[str]]:
+ @cached_property
+ def ignored_pairs(self) -> frozenset[frozenset[str]]:
+ """Return the expanded physical IDs for callers exporting the query."""
+ return frozenset(
+ frozenset((a, b))
+ for left, right in self._ignored_alias_pairs
+ for a in self._aliases[left]
+ for b in self._aliases[right]
+ if a != b
+ )
+
+ def _permission_pairs(
+ self, pairs: Sequence[tuple[str, str]]
+ ) -> frozenset[tuple[str, str]]:
output = set()
for pair in pairs:
if (
@@ -440,13 +506,23 @@ def _expand_pairs(self, pairs: Sequence[tuple[str, str]]) -> set[frozenset[str]]
raise ValueError(
"contact permissions require two distinct known physical IDs or frames"
)
- output.update(
- frozenset((a, b))
- for a in self._aliases[pair[0]]
- for b in self._aliases[pair[1]]
- if a != b
- )
- return output
+ output.add(tuple(sorted(pair)))
+ return frozenset(output)
+
+ def _permission_targets(self, pairs):
+ # A permission can name a whole frame or just one of its colliders.
+ # Keep frame exemptions compact even for heavily decomposed assets.
+ targets = {}
+ for left, right in pairs:
+ targets.setdefault(left, set()).add(right)
+ targets.setdefault(right, set()).add(left)
+ empty = frozenset()
+ return tuple(
+ targets.get(body.frame_uid, empty)
+ | targets.get(body.collision_id, empty)
+ | targets.get(body.source_collision_id, empty)
+ for body in self.bodies
+ )
def _mask(
self,
@@ -461,30 +537,38 @@ def _mask(
raise UnsupportedFeasibilityError(
"Moving object has no physical collision geometry"
)
- allowed = frozenset(self._expand_pairs(allowed_contacts))
+ allowed = self._permission_pairs(allowed_contacts)
key = (allowed, frozenset(moving), all_pairs)
if key in self._mask_cache:
self._mask_cache.move_to_end(key)
return self._mask_cache[key]
- mask = np.zeros(len(self._native) ** 2, dtype=np.int32)
- planes = []
- for i, j in self._pairs:
+
+ allowed_targets = self._permission_targets(allowed)
+
+ def active(pair):
+ i, j = pair
a, b = self.bodies[i], self.bodies[j]
- if frozenset((a.collision_id, b.collision_id)) in allowed or not (
- all_pairs
- or a.robot
- or b.robot
- or a.frame_uid in moving
- or b.frame_uid in moving
- ):
- continue
- if a.kind == "halfspace" or b.kind == "halfspace":
- planes.append((i, j))
- else:
- x, y = self._native_lookup[i], self._native_lookup[j]
- mask[x * len(self._native) + y] = mask[y * len(self._native) + x] = 1
+ return (
+ b.frame_uid not in allowed_targets[i]
+ and b.collision_id not in allowed_targets[i]
+ and b.source_collision_id not in allowed_targets[i]
+ and (
+ all_pairs
+ or a.robot
+ or b.robot
+ or a.frame_uid in moving
+ or b.frame_uid in moving
+ )
+ )
+
+ mask = np.fromiter(
+ map(active, self._native_pairs),
+ dtype=np.int32,
+ count=len(self._native_pairs),
+ )
+ planes = tuple(filter(active, self._plane_pairs))
mask.setflags(write=False)
- value = mask, tuple(planes), bool(mask.any())
+ value = mask, planes, bool(mask.any())
self._mask_cache[key] = value
if len(self._mask_cache) > 16:
self._mask_cache.popitem(last=False)
@@ -521,6 +605,7 @@ def _run_query(self) -> None:
c.rigid_contact_margin1,
self._model.shape_body,
self._state.body_q,
+ self._pair_keys,
self._active,
len(self._native),
self.clearance_m,
@@ -696,8 +781,7 @@ def check(
upper_clearance = min(upper_clearance, max(0.0, separation + tolerance))
if math.isfinite(summary[1]):
self.last_clearance_interval = (clearance, upper_clearance)
- a, b = divmod(int(summary[1]), len(self._native))
- return self._failure(self._native[a], self._native[b]), clearance
+ return self._failure(*self._native_pairs[int(summary[1])]), clearance
self.last_clearance_interval = (clearance, upper_clearance)
return FeasibilityCheck(
"collision",
@@ -714,7 +798,7 @@ def check_batch(
all_pairs: bool = False,
env_ids: Sequence[int] | None = None,
) -> tuple[FeasibilityCheck, ...]:
- """Check independent measured rows with one bounded numerical query.
+ """Check independent measured rows in bounded numerical batches.
Args:
frame_poses: Every declared frame with arena poses ``(B, 4, 4)``.
@@ -763,30 +847,43 @@ def check_batch(
if type(all_pairs) is not bool:
raise TypeError("all_pairs must be a bool")
# Resolve permissions with the same native validation before caching.
- allowed = frozenset(self._expand_pairs(allowed_contacts))
+ allowed = self._permission_pairs(allowed_contacts)
self._mask(allowed_contacts, moving_objects, all_pairs)
- key = size, allowed, frozenset(moving_objects), all_pairs
- query = self._batch_queries.get(key)
- if query is None:
- if size * self._capacity > 1048576:
- raise UnsupportedFeasibilityError(
- "Batched collision contact budget exceeded"
+ width = min(
+ _MAX_CONTACTS // self._capacity,
+ _MAX_QUERY_PAIRS // max(1, len(self._native_pairs)),
+ )
+ reports = {}
+ for start in range(0, size, width):
+ count = min(width, size - start)
+ key = count, allowed, frozenset(moving_objects), all_pairs
+ query = self._batch_queries.get(key)
+ if query is None:
+ query = _CollisionBatches(
+ self, count, allowed_contacts, moving_objects, all_pairs
)
- query = _CollisionBatches(
- self, size, allowed_contacts, moving_objects, all_pairs
- )
- self._batch_queries[key] = query
- while len(self._batch_queries) > 2:
- _, old = self._batch_queries.popitem(last=False)
- old.close()
- else:
- self._batch_queries.move_to_end(key)
- result = query.check(frames, rows)
- self.last_batch_pairs = tuple(pair for _, _, pair in result)
- return tuple(report for _, report, _ in result)
+ self._batch_queries[key] = query
+ if len(self._batch_queries) > 2:
+ _, old = self._batch_queries.popitem(last=False)
+ old.close()
+ else:
+ self._batch_queries.move_to_end(key)
+ local_rows = [row - start for row in rows if start <= row < start + count]
+ chunk = {uid: poses[start : start + count] for uid, poses in frames.items()}
+ for row, report, pair in query.check(chunk, local_rows):
+ reports[start + row] = report, pair
+ self.last_batch_pairs = tuple(reports[row][1] for row in rows)
+ return tuple(reports[row][0] for row in rows)
+
+ def _physical_pair(self, i: int, j: int) -> tuple[str, str]:
+ a, b = self.bodies[i], self.bodies[j]
+ return (
+ a.source_collision_id or a.collision_id,
+ b.source_collision_id or b.collision_id,
+ )
def _failure(self, i: int, j: int) -> FeasibilityCheck:
- self.last_pair = self.bodies[i].collision_id, self.bodies[j].collision_id
+ self.last_pair = self._physical_pair(i, j)
return FeasibilityCheck(
"collision",
"failed",
@@ -821,7 +918,7 @@ def _close_arrays(self) -> None:
self._graph = self._pipeline = self._contacts = self._state = self._model = None
self._active = self._output = self._empty_count = None
self._tolerance = None
- self._explicit = None
+ self._explicit = self._pair_keys = None
self._meshes.clear()
self._mask_cache.clear()
self._closed = True
diff --git a/embodichain/lab/sim/scene/feasibility/planning.py b/embodichain/lab/sim/scene/feasibility/planning.py
index d07d7dec5..1edd2c0e0 100644
--- a/embodichain/lab/sim/scene/feasibility/planning.py
+++ b/embodichain/lab/sim/scene/feasibility/planning.py
@@ -466,6 +466,11 @@ def solutions(
mask, q = result
q = q.detach().cpu().double().numpy()
valid = mask.detach().cpu().numpy()
+ # Analytic Warp solvers encode branch validity as integer 0/1.
+ if np.issubdtype(valid.dtype, np.integer) and np.all(
+ (valid == 0) | (valid == 1)
+ ):
+ valid = valid.astype(bool)
if (
q.ndim not in (2, 3)
or q.shape[0] != 1
diff --git a/embodichain/lab/task_program/integrations/feasibility.py b/embodichain/lab/task_program/integrations/feasibility.py
index 279b2e5f3..79cdddd2d 100644
--- a/embodichain/lab/task_program/integrations/feasibility.py
+++ b/embodichain/lab/task_program/integrations/feasibility.py
@@ -634,7 +634,7 @@ def build_feasibility_host(
+ digest(
(
adapter,
- "newton-convex-query/v1",
+ NewtonCollisionChecker.query_contract,
version("newton"),
cfg.collision_device,
cfg.collision_use_cuda_graph,
diff --git a/embodichain/lab/task_program/integrations/simulation/feasibility_geometry.py b/embodichain/lab/task_program/integrations/simulation/feasibility_geometry.py
index 668a6084c..b8e40c086 100644
--- a/embodichain/lab/task_program/integrations/simulation/feasibility_geometry.py
+++ b/embodichain/lab/task_program/integrations/simulation/feasibility_geometry.py
@@ -18,6 +18,7 @@
from __future__ import annotations
+from functools import lru_cache
from typing import Any
import numpy as np
@@ -45,6 +46,91 @@ def collision_bodies_from_simulation(
Unsupported physical geometry APIs/scales and shapes fail explicitly.
The caller owns permanent structural and phase-specific contact permissions.
"""
+ return _physical_collision_bodies(env, env_id=env_id)
+
+
+def _planar_convex_parts(points, faces, origin, axes):
+ """Extrude the actual planar footprint by one micrometre for Newton."""
+ import shapely
+ import trimesh
+
+ xy = (points - origin) @ axes[:2].T
+ triangles = shapely.polygons(xy[faces])
+ surface = shapely.union_all(triangles[shapely.area(triangles) > 0])
+ polygons = shapely.get_parts(surface)
+ convex = [polygon.equals(polygon.convex_hull) for polygon in polygons]
+ if all(convex):
+ footprints = [polygon.convex_hull for polygon in polygons]
+ elif hasattr(
+ shapely, "constrained_delaunay_triangles"
+ ) and shapely.geos_version >= (3, 10, 0):
+ footprints = []
+ for polygon, is_convex in zip(polygons, convex):
+ if is_convex:
+ footprints.append(polygon.convex_hull)
+ else:
+ footprints.extend(
+ shapely.get_parts(shapely.constrained_delaunay_triangles(polygon))
+ )
+ else:
+ # Older GEOS versions still have the exact source triangulation.
+ footprints = triangles[shapely.area(triangles) > 0]
+ offset = axes[-1] * 5e-7
+ parts = []
+ for polygon in footprints:
+ ring = np.asarray(polygon.exterior.coords)[:-1] @ axes[:2] + origin
+ prism = trimesh.convex.convex_hull(np.vstack((ring - offset, ring + offset)))
+ parts.append((prism.vertices, prism.faces))
+ return parts
+
+
+@lru_cache(maxsize=8)
+def _convex_parts(vertices: bytes, triangles: bytes) -> tuple:
+ """Cook scaled collider-local surfaces once across layouts and operators."""
+ import trimesh
+
+ points = np.frombuffer(vertices, dtype=np.float64).reshape(-1, 3)
+ faces = np.frombuffer(triangles, dtype=np.int32).reshape(-1, 3)
+ edges = points[faces[:, 1:]] - points[faces[:, :1]]
+ faces = faces[np.any(np.cross(edges[:, 0], edges[:, 1]) != 0, axis=1)]
+ if not len(faces):
+ raise ValueError("Collision mesh has no surface area")
+ used, remapped = np.unique(faces, return_inverse=True)
+ if len(used) != len(points):
+ points = points[used]
+ faces = remapped.reshape(-1, 3).astype(np.int32)
+ origin = points.mean(axis=0)
+ centered = points - origin
+ _, singular, axes = np.linalg.svd(centered, full_matrices=False)
+ if singular[1] <= singular[0] * 1e-12:
+ raise ValueError("Collision mesh has no surface area")
+ if np.max(np.abs(centered @ axes[-1])) <= 1e-9:
+ parts = _planar_convex_parts(points, faces, origin, axes)
+ else:
+ mesh = trimesh.Trimesh(points, faces)
+ if mesh.is_convex and mesh.is_volume:
+ hull = mesh.convex_hull
+ parts = [(hull.vertices, hull.faces)]
+ else:
+ # Use the engine's CPU V-HACD directly on materialized geometry:
+ # a second asset import or vertex-welding pass can erase thin walls.
+ from dexsim.cuda.pybind.meshproc import ProcMesh, vhacd
+
+ cooked = vhacd(
+ ProcMesh(points, faces), max_conv_hull=64, max_vert_conv_hull=64
+ )
+ if not cooked.is_success:
+ raise ValueError("V-HACD failed to decompose the collision mesh")
+ parts = [(part.vertices, part.triangles) for part in cooked.convex_list]
+ if not parts:
+ raise ValueError("Convex decomposition produced no parts")
+ return tuple((tuple(map(tuple, v)), tuple(map(tuple, f))) for v, f in parts)
+
+
+def _physical_collision_bodies(
+ env: Any, *, env_id: int, layout_mesh_parts: dict[str, int] | None = None
+) -> tuple[CollisionBody, ...]:
+ """Optionally decompose physical triangle meshes for isolated layout screening."""
robot = env.robot
if env.sim.get_articulation_uid_list():
raise UnsupportedFeasibilityError(
@@ -58,6 +144,7 @@ def collision_bodies_from_simulation(
def convert(shape, collision_id, frame_uid, *, is_robot=False):
kind = shape.shape_type.name.lower()
kwargs = {}
+ local_pose = shape.local_pose.cpu().numpy()
if kind == "box":
dimensions = tuple((shape.half_extents.cpu().numpy() * 2).tolist())
elif kind == "sphere":
@@ -78,18 +165,66 @@ def convert(shape, collision_id, frame_uid, *, is_robot=False):
for f in shape.triangles.cpu().numpy().reshape(-1, 3)
),
}
+ elif kind == "mesh" and layout_mesh_parts is not None:
+ if shape.vertices is None or shape.triangles is None:
+ raise UnsupportedFeasibilityError(
+ f"Missing physical mesh for {collision_id}"
+ )
+ points = shape.vertices.cpu().numpy()
+ faces = shape.triangles.cpu().numpy()
+ if (
+ points.ndim != 2
+ or points.shape[1] != 3
+ or len(points) < 3
+ or not np.isfinite(points).all()
+ or faces.dtype.kind not in "iu"
+ or faces.ndim not in (1, 2)
+ or (faces.ndim == 2 and faces.shape[1] != 3)
+ or not faces.size
+ or faces.size % 3
+ or faces.min() < 0
+ or faces.max() >= len(points)
+ ):
+ raise UnsupportedFeasibilityError(
+ f"Invalid physical mesh for {collision_id}"
+ )
+ try:
+ parts = _convex_parts(
+ points.astype(np.float64, copy=False).tobytes(),
+ faces.astype(np.int32, copy=False).tobytes(),
+ )
+ except (RuntimeError, ValueError) as error:
+ raise UnsupportedFeasibilityError(
+ f"Cannot decompose {collision_id}: {error}"
+ ) from error
+ layout_mesh_parts[collision_id] = len(parts)
+ return tuple(
+ CollisionBody(
+ f"{collision_id}/hull/{i}",
+ frame_uid,
+ "convex",
+ local_pose=local_pose.tolist(),
+ vertices=v,
+ triangles=f,
+ robot=is_robot,
+ source_collision_id=collision_id,
+ )
+ for i, (v, f) in enumerate(parts)
+ )
else:
raise UnsupportedFeasibilityError(
f"Native physical shape {collision_id}: {kind} is unsupported"
)
- return CollisionBody(
- collision_id,
- frame_uid,
- kind,
- dimensions,
- shape.local_pose.cpu().tolist(),
- robot=is_robot,
- **kwargs,
+ return (
+ CollisionBody(
+ collision_id,
+ frame_uid,
+ kind,
+ dimensions,
+ local_pose.tolist(),
+ robot=is_robot,
+ **kwargs,
+ ),
)
bodies = []
@@ -101,22 +236,18 @@ def convert(shape, collision_id, frame_uid, *, is_robot=False):
except RuntimeError as error:
raise UnsupportedFeasibilityError(str(error)) from error
frame = f"{robot.uid}/{link_name}"
- bodies.extend(
- convert(shape, f"{frame}/{index}", frame, is_robot=True)
- for index, shape in enumerate(shapes)
- )
+ for index, shape in enumerate(shapes):
+ bodies.extend(convert(shape, f"{frame}/{index}", frame, is_robot=True))
if not bodies:
raise UnsupportedFeasibilityError(
"Robot has no accessible physical collision geometry"
)
for uid in env.sim.get_rigid_object_uid_list():
obj = env.sim.get_rigid_object(uid)
- bodies.extend(
- convert(shape, f"{uid}/{index}", uid)
- for index, shape in enumerate(
- obj.get_collision_shapes(**({"env_id": env_id} if env_id else {}))
- )
- )
+ for index, shape in enumerate(
+ obj.get_collision_shapes(**({"env_id": env_id} if env_id else {}))
+ ):
+ bodies.extend(convert(shape, f"{uid}/{index}", uid))
if getattr(env.sim, "_default_plane", None) is not None:
bodies.append(
CollisionBody(
diff --git a/embodichain/lab/visualization/_web_viewer.py b/embodichain/lab/visualization/_web_viewer.py
index ff8699d7f..f40fddf5a 100644
--- a/embodichain/lab/visualization/_web_viewer.py
+++ b/embodichain/lab/visualization/_web_viewer.py
@@ -20,6 +20,7 @@
import json
import ctypes
+from collections.abc import Callable
from copy import deepcopy
import os
from pathlib import Path
@@ -121,11 +122,34 @@ def __init__(self, sim: SimulationManager) -> None:
("/style.css", "style.css", "text/css; charset=utf-8"),
)
}
- self.application = dexsim.WebApplication(sim.get_world(), assets)
+ self._replace_application(assets, self._request)
except BaseException:
self.close()
raise
+ def _replace_application(
+ self,
+ assets: dict[str, tuple[str, bytes]],
+ handler: Callable[[str, str], tuple[int, str]],
+ ) -> None:
+ """Install host-owned routes on the existing observer and listener."""
+ import dexsim
+
+ self._assert_owner()
+ if self.closed:
+ raise RuntimeError("WebRTC viewer is closed.")
+ if self.application is not None:
+ self.application.close()
+ self.application = None
+ self.application = dexsim.WebApplication(self.sim.get_world(), assets)
+ self._application_handler = handler
+
+ def _process_application_requests(self) -> None:
+ """Service application requests without scheduling an observer render."""
+ self._assert_owner()
+ if self.application is not None:
+ self.application.process(self._application_handler)
+
def _assert_owner(self) -> None:
if threading.get_ident() != self.owner:
raise RuntimeError("WebRTC viewer must run on the World creator thread.")
@@ -210,7 +234,7 @@ def capture(self, *, force: bool = False) -> bool:
self._assert_owner()
if self.closed:
return False
- self.application.process(self._request)
+ self._process_application_requests()
if self.error is not None:
return False
if not (force or self.dirty or time.monotonic() >= self.next_render):
diff --git a/examples/expansion/engine/README.md b/examples/expansion/engine/README.md
index e1165c764..f02d6c232 100644
--- a/examples/expansion/engine/README.md
+++ b/examples/expansion/engine/README.md
@@ -45,7 +45,7 @@ folder; it is no longer the existing-result input flag. `--plan-only` freezes an
validates samples without executing, and `--no-open` suppresses automatic opening.
The [preview guide](ghost-preview.md) explains budgets, failed-boundary evidence,
scene-atlas domains, finite-sampling limitations and the WebRTC TODO. Rendering is
-native; browser transport remains HTTP JPEG, not WebRTC.
+native; the browser receives H.264 video over WebRTC.
## Run a configured job
diff --git a/examples/expansion/engine/ghost-preview.md b/examples/expansion/engine/ghost-preview.md
index 376d3ca9d..df880edbc 100644
--- a/examples/expansion/engine/ghost-preview.md
+++ b/examples/expansion/engine/ghost-preview.md
@@ -8,14 +8,11 @@
| 路径 | 渲染与交互 | 状态 |
| --- | --- | --- |
| SDK 原生窗口对照 | `preview_expansion_comparison` 加载已验收轨迹,显示参考与选中虚影;N 切换、Space 暂停、Q 关闭 | 仓库已有能力,见 [comparison.py](../../../embodichain/lab/expansion/comparison.py) |
-| 浏览器结果预览 | EmbodiChain 原生离屏渲染完整模型;HTTP JPEG 传图,HTTP 控制筛选与回放 | `embodichain expansion preview`,直接读取已确认结果 |
-| 原生 WebRTC 推流 | 复用 EmbodiChain 的原生推流生命周期与控制接口 | TODO,尚未接入本预览 |
+| 浏览器 WebRTC 回放 | 原生观察相机输出 H.264 视频,SDK 鉴权后控制回放与视角 | `embodichain expansion preview` |
-“原生渲染”指场景由 `SimulationManager`、`RobotMarkers`、
-`VisualizationMarkers` 和 DexSim Hybrid 渲染器生成,**不表示采用 WebRTC**。
-浏览器预览没有自行绘制机器人骨架,也不使用 Viser 重建场景;它显示原生离屏
-相机输出的 JPEG。本次使用 OptiX 去噪。展示灯光、视角和透明材质与采集时可以不同,
-不能把预览图片当作采集传感器的原始观测。
+浏览器回放使用 `SimulationManager`、`RobotMarkers`、`VisualizationMarkers` 和带 OptiX
+去噪的 DexSim Hybrid 场景。展示灯光、视角和透明材质用于观看实测状态,采集传感器
+图像可在原始作业中查看。
最初实验使用采集时冻结的源码 `ab04d972b5b75042434498db9a889b4edd03b6fe`。
仓库入口直接读取原始结果,不需要该源码快照、本地预处理 JSON 或实验脚本。
@@ -89,8 +86,8 @@ job 输出目录旁创建唯一目录。原 job 文件和原产物不修改,
保留报告和日志,不伪造成功回放。
页面显示所有计划点的参数和状态,候选虚影只来自确认结果;初始位置图层中,
-红色计划点表示未完全确认的参数点,不代表一条失败轨迹。筛选后没有匹配样本时
-显示空结果,不用别的候选替代。WebRTC 接入仍为 TODO。
+红色计划点表示未完全确认的参数点,不代表一条失败轨迹。筛选后没有匹配样本时,
+候选列表为空,固定对照继续显示。
## 从已有结果启动
@@ -112,7 +109,9 @@ embodichain expansion preview \
数据集或单个 NPZ。可重复传入 `--input`,空间筛选以输入结果目录分组。
已有结果直接读取仍要求固定布局;`--job` 会将 scene-atlas 的每个采样点冻结为
独立布局子作业,再作为一个参数组加载。
-`--reference` 可省略;指定时必须恰好含一个已确认参考 episode,且不能与候选重复。
+`--reference` 指向原始轨迹的作业目录,其中应有一条独立的已确认记录。
+提供该参数时,原始轨迹始终以实体播放,所有输入候选都可以作为虚影叠加。
+省略该参数时,预览固定使用第一份输入中首条载入的记录,页面标记为「首条记录(固定对照)」。
模型从 `job.json.resolved_deployment.robot` 的 URDF 路径恢复,场景刚体几何从
同一快照恢复。方块、球和文件 Mesh/USD 资产通过原生纯视觉对象加载,保留原生
@@ -158,6 +157,12 @@ embodichain expansion preview \
9 条轨迹均通过原有校验并确认回执,共保留 3642 个原始状态帧。
该结果只代表这个固定小配方,不代表更大规模、其他任务或其他后端的资格结论。
+### Piper 书架取书
+
+`piper-coverage-expansion` 使用 8 个环境,`object_pose`、`affordance` 和
+`joint_residual` 各有 3 条通过验收的记录。回放以首条记录 C-001 为
+固定对照,可同时开启 C-004 和 C-009 比较提书阶段的抓取与关节扰动。
+
## 数据到画面的运行逻辑
1. 读取 `ArtifactEpisodeSink` 的确认回执,并通过 `load_expansion_episode`
@@ -165,7 +170,7 @@ embodichain expansion preview \
2. 核对 episode 身份、机器人模型、关节名称与顺序,以及动作和状态的 T+1 对齐。
加载逐帧完整关节位置、机器人根位姿、物体位姿和时间戳。
3. 使用与记录匹配的 URDF 和实测关节计算 FK,驱动 `RobotMarkers` 的完整视觉网格。
- 选中样本显示原始模型外观,其余可见候选显示半透明虚影;参考单独开关。
+ 固定对照始终显示原始模型外观。每条启用的扩增轨迹叠加半透明机器人和物体。
4. 用真实物体位姿驱动物体模型,并通过 `VisualizationMarkers` 显示初始位置、
抓取目标坐标轴和末端路径。
5. 启动时分块预计算 FK,使用 `MarkerFramePublisher` 发布一致的跨组帧;
@@ -179,10 +184,13 @@ embodichain expansion preview \
## 联动筛选与时间语义
-空间、affordance、轨迹三个筛选器取交集,同时作用于候选列表、虚影、物体、
-初始位置、抓取目标和轨迹线。选中样本被筛掉时,选择交集内的一个有效样本。
-没有匹配候选时显示空结果、停止播放并隐藏候选图层;参考对照仍可独立显示。
-参考轨迹是独立对照,不计入 8 条候选数量,也不受候选筛选限制。
+回放工具栏列出了快捷键。按空格播放或暂停,轨迹结束后再按空格从头播放。
+从采集开始对比两种布局和抓取分支的完整步骤见 [UR5 布局与抓取姿态扩增教程](../../../scripts/tutorials/expansion/ur5_pick_place/README.md)。
+
+页面初始只显示固定对照。点击候选按钮会切换该条轨迹的高亮和虚影,其他按钮保留各自状态。
+空间、affordance、轨迹三个筛选器取交集,临时隐藏不匹配的虚影,切回筛选时恢复之前的选择。
+「显示全部匹配虚影」批量切换当前筛选内的候选。末端轨迹线、抓取目标和初始位置图层
+跟随固定对照与当前显示的虚影。匹配数为零时,固定对照仍可播放和拖动。
- **空间**:显示实际初始位置,不给样本添加人为排列偏移。
- **Affordance**:A0/A1 是配方分支编号,不是底层候选 ID。读取规划诊断时按实际
@@ -196,6 +204,27 @@ embodichain expansion preview \
时刻之前最近的已记录帧,不插值生成新姿态;短轨迹到末帧后保持。
阶段对齐只改变浏览进度映射,不改变原始时间戳。页面保留实际秒数和源帧号。
+## 按实测时间比较偏差
+
+回放页下方的两张统计图使用全部扩增轨迹。横轴是固定对照的实测时间(秒)。
+第一张图从各轨迹记录的世界位姿、URDF 正运动学和夹爪 TCP 标定恢复末端位姿,
+逐条计算与对照轨迹的平移距离(mm)和相对旋转的最小转角(°),分别绘图。
+第二张图在每条轨迹内先取机械臂关节的最大绝对 qpos 差。
+每幅图的色带下沿和上沿分别是该时刻所有参与轨迹的最小值和最大值,带内实线是均值。只有一条轨迹仍有记录时,三者重合。
+转动关节使用记录的关节坐标差,移动关节以 mm 单列。
+时间戳不同时,位置和 qpos 线性插值,TCP 姿态按最短弧球面插值。
+每个时刻只统计仍有记录的轨迹,样本数为零的区间留空。悬停可读取各项偏差的三项统计值和参与条数,
+移开鼠标后读数跟随对照的回放时间。
+
+### object_pose 物体起点分布
+
+热力图从 `--input` 读取已确认的 `object_pose` 扩增记录。物体下拉框列出这些记录在
+`joint_scene.pose_changes` 中指定的刚体。每条记录取所选物体首帧实测原点的场景 XY 坐标,
+以米为单位计入一个网格。X、Y 轴分别按该物体的起点范围缩放,每端留半格。
+若某个轴的起点坐标都相同,就以该坐标为中心显示 1 mm 的范围。
+图旁标有两轴各自的单格尺寸,指向或轻点网格可查看边界及轨迹数。
+统计使用全部符合条件的输入,与当前启用的虚影和回放时间独立。单独传入的 `--reference` 用于回放对照。
+
## 验收与当前限制
本地浏览器验收覆盖:筛选数量 8 → 4 → 2 → 1,筛选后原生画面改变,
@@ -219,19 +248,18 @@ CLI 不触发采集和安装包页面资源由生产模块的聚焦测试覆盖
上述文件是原型的历史实验产物;仓库入口不再依赖它们。结果读取与控制语义由
`embodichain/lab/expansion/_preview_data.py` 负责,原生渲染与资源释放由
-`_preview_render.py` 负责,HTTP 服务与页面由 `_preview_server.py`、`_preview.html`
+`_preview_render.py` 负责,HTTP 服务与页面由 `_preview_server.py`、`_frontend/preview.html`
负责,CLI 由 `embodichain/cli/expansion.py` 分发。
当前渲染支持单 URDF 机器人、单位机器人缩放,以及有完整实测位姿的刚体场景。
-文件 Mesh 和 USD 通过原生视觉资产加载,选中物体保留源模型外观,虚影复用实测几何。
+文件 Mesh 和 USD 通过原生视觉资产加载,固定对照中的物体保留源模型外观,虚影复用实测几何。
其他 articulated
objects、软体、流体、线缆、显式 visual replacement 和直接 scene-atlas 输入会被拒绝;
不会用方块代替不支持的几何。预览保持记录中的运动与尺寸,虚影材质与灯光用于展示,
不声称精确恢复采集传感器的原始图像。新任务支持应扩展场景恢复契约并验证实测坐标。
-HTTP JPEG 轮询不提供 WebRTC 的编码、拥塞控制、连接恢复或低延迟保证。
-页面是单个服务的共享回放会话,多人操作会改变同一状态;尚未提供独立会话、
-认证或通用资产包恢复。当前不应对外公开监听。
+页面由单个 WebRTC 查看者操作,适用于可信网络。回放控件只改变预览的时间、筛选、
+图层和视角。
## 一体化入口的实测验收
@@ -245,18 +273,13 @@ XY、安装 XY/yaw、质量、摩擦与六个受控关节残差,共 13 个变
图层;回放物理推进为 0,自动关闭后端口释放。另行验证了无 motion profile 的
普通 Task Program 作业也能重新计算并进入预览;批内诊断由来源行号绑定。
-## TODO:接入 EmbodiChain 原生 WebRTC 推流
-
-后续直接复用 EmbodiChain 原生 WebRTC 能力,替换 HTTP JPEG 轮询;先核对实际
-推流入口、信令与关闭接口,不在本示例另建一套独立 WebRTC 栈。
-保留真实结果加载、原生场景、三类筛选联动、播放、时间轴及视角控制。
-控制通道的实现需按原生接口确定,不能预先宣称已经通过 DataChannel 接入。
+## WebRTC 视频回放
-验收条件:
+`embodichain expansion preview` 默认使用 WebRTC。服务复用
+[DexSim WebRTC 观察视口](../../../embodichain/lab/visualization/README.md),以最高 25 帧/秒更新
+回放相机。视频通过 H.264 发送,时间轴和图表读取独立的回放状态。语言、筛选、虚影、播放、
+拖动和视角沿用同一页面。时间轴上的源帧描述服务端回放位置;浏览器的视频显示另有编解码和网络延迟。
-- 浏览器建立真实 WebRTC 连接并接收原生相机视频,确认画面不再依赖 JPEG 轮询。
-- 沿用这 8 条扩增结果和参考轨迹,复测筛选、拖动、暂停、切换视角及重新连接。
-- 控制状态、源帧和视频画面的对应关系可检查;显示延迟不冒充记录时间。
-- 回放物理步数仍为 0,不触发重采集、不修改原始证据或成功计数。
-- 关闭预览后释放推流、相机、端口及场景资源;记录端到端延迟与实际帧率,
- 再据此报告改善,不能用传输协议名称推断性能。
+原生 SDK 需要 Linux/NVIDIA 和 NVENC。其信令监听所有 IPv4 接口,适合可信网络;
+客户端需要 ICE 媒体连通性。通过 SSH 转发页面时,仍需配置媒体路径。使用完毕可通过
+Ctrl+C 或 `--duration` 结束服务,观察相机、应用与端口由原有 World 生命周期统一释放。
diff --git a/scripts/tutorials/expansion/ur5_pick_place/README.md b/scripts/tutorials/expansion/ur5_pick_place/README.md
new file mode 100644
index 000000000..2370ca839
--- /dev/null
+++ b/scripts/tutorials/expansion/ur5_pick_place/README.md
@@ -0,0 +1,56 @@
+# UR5 抓取放置的布局与抓取姿态扩增
+
+这个教程用同一台 UR5 和平行夹爪抓取桌上的立方体,把它放到目标位置。先记录一条原始轨迹作为对照,再用两种策略改变布局。每种策略各采集 4 条固定抓取轨迹(`object_pose`)和 4 条抓取姿态扩增轨迹(`affordance`),共 16 条扩增轨迹。
+
+| 采集模式 | 改变的内容 |
+| --- | --- |
+| `reference` | 立方体和放置目标使用任务的初始位置,执行一次固定抓取。 |
+| `scene_first` | 先在桌面范围内提出布局,再检查机械臂能否完成接近、抓取、抬升和放置。 |
+| `robot_first` | 先从机械臂工作空间选取可达抓取位姿,再以此为约束生成布局。 |
+
+## 采集
+
+从仓库根目录运行。在装有 EmbodiChain、DexSim 和 LeRobot 的 `py312` 环境中执行以下命令。每个模式把实际配置和采集结果写入单独的子目录。已有完整结果时可以直接进入「网页回放」。重新采集时给 `out` 换一个新目录。
+
+```bash
+conda activate py312
+demo=scripts/tutorials/expansion/ur5_pick_place
+out=outputs/ur5-affordance-demo
+
+python "$demo/collect.py" reference --output "$out"
+python "$demo/collect.py" scene_first --output "$out"
+python "$demo/collect.py" robot_first --output "$out"
+```
+
+三个命令默认使用种子 7,可用 `--seed` 指定。每次采集结束都会打印各算子通过验收并保存的轨迹数。各模式的 `expansion.yaml` 保存实际使用的参数,`collection/expansion.json` 记录完成情况,`collection/dataset/<算子>/` 保存轨迹和验收记录。
+
+## 网页回放
+
+已有原始录制时,跳过 `reference` 采集,将下面的 `reference` 变量指向原始录制的 `job.json` 所在目录,即可逐帧复用同一条实体轨迹和图表基准。
+
+```bash
+conda activate py312
+out=outputs/ur5-affordance-demo
+reference="$out/reference/collection/dataset/object_pose"
+python -m embodichain expansion preview \
+ --reference "$reference" \
+ --input "$out/scene_first/collection" \
+ --input "$out/robot_first/collection" \
+ --port 0 --open
+```
+
+- 回放通过原生 WebRTC 视频呈现。连接要求见[预览说明](../../../../examples/expansion/engine/ghost-preview.md#webrtc-视频回放)。
+- 页眉的 **语言**可以在简体中文和 English 之间切换。播放进度和当前筛选会保留。
+- **空格**播放或暂停,播放结束后再按空格从头开始。拖动进度条可定位到某一帧。使用下拉框或复选框时,空格操作当前控件,点击回放画面即可继续用空格控制播放。
+- 原始轨迹始终以实体显示。右侧每个轨迹按钮独立切换一条半透明虚影。按上述命令加载时,前两个参数组来自 `scene_first`,后两个来自 `robot_first`。
+- 在 **抓取分支**(**Affordance**)下拉框中选择 `1` 或 `2`,可以查看采用该抓取分支的轨迹。使用任务固定抓取姿态的 `object_pose` 记录显示为「未记录」。
+- 画面下方的 TCP 图分开显示相对原始轨迹的平移距离(mm)和最短旋转角(°)。关节图显示每条轨迹最大的臂关节绝对偏差。三条曲线都用色带标出同一时刻各轨迹的最小值至最大值,用实线标出均值。悬停可读取三项数值和参与轨迹数。
+- **object_pose 物体起点分布**统计两种布局策略各 4 条 `object_pose` 扩增轨迹。选择抓取方块 `cube` 或放置目标 `goal`,可查看该物体在 8 条轨迹首帧的实测 XY 位置。X、Y 轴分别贴合该物体的起点范围,两端各留半格。图旁标有单格的实际宽、高。悬停或轻点网格可读取坐标和轨迹数。
+
+可在 9 秒附近暂停,选择抓取分支 `2`,分别开启 `scene_first` 和 `robot_first` 的一条虚影。点击其中一条的按钮即可单独隐藏它。
+
+## 修改配置或可视化
+
+两种策略共用一份配置。默认布局允许立方体和放置目标在 X、Y 方向各偏移 ±6 cm。`trajectory.affordance.call_indices: [0]` 将抓取扩增应用到第一次调用 `pick`。`fixed_object_to_eef: null` 在 affordance 组启用夹爪的对向接触候选生成器。`branches: 3` 让采集器轮流请求分支 1 和 2,再按实际抓取、搬运、释放和放置结果验收。可以在同一处调整分支数、布局范围和每组轨迹数。[扩增配置](expansion.yaml) · [任务与机器人配置](../../../../extensions/embodichain_tasks/configs/tasks/manipulation/workspace_pose_pick_place/acceptance/scene_first/task.yaml)
+
+播放键、筛选器和时间轴在回放页面中实现,偏差曲线和物体热力图各有独立的绘图脚本。改完重新运行「网页回放」命令即可用同一批轨迹查看效果。[回放页面](../../../../embodichain/lab/expansion/_frontend/preview.html) · [偏差曲线脚本](../../../../embodichain/lab/expansion/_frontend/preview-charts.js) · [热力图脚本](../../../../embodichain/lab/expansion/_frontend/preview-heatmap.js)
diff --git a/scripts/tutorials/expansion/ur5_pick_place/collect.py b/scripts/tutorials/expansion/ur5_pick_place/collect.py
new file mode 100644
index 000000000..96ab84324
--- /dev/null
+++ b/scripts/tutorials/expansion/ur5_pick_place/collect.py
@@ -0,0 +1,77 @@
+# ----------------------------------------------------------------------------
+# Copyright (c) 2021-2026 DexForce Technology Co., Ltd.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ----------------------------------------------------------------------------
+
+"""Collect the UR5 reference or one layout strategy with native grasp expansion."""
+
+from __future__ import annotations
+
+import argparse
+from pathlib import Path
+
+import yaml
+
+from embodichain.lab.expansion import run_task_program_expansion
+
+HERE = Path(__file__).resolve().parent
+TASK = (
+ HERE.parents[3]
+ / "extensions/embodichain_tasks/configs/tasks/manipulation"
+ / "workspace_pose_pick_place/acceptance/scene_first/task.yaml"
+)
+
+
+def main() -> None:
+ """Save the resolved configuration and collect one group of measured episodes."""
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument("mode", choices=("reference", "scene_first", "robot_first"))
+ parser.add_argument("--output", type=Path, required=True, help="Result root")
+ parser.add_argument("--seed", type=int, default=7)
+ args = parser.parse_args()
+
+ config = yaml.safe_load((HERE / "expansion.yaml").read_text())
+ sampling = config["scene_sampling"]
+ if args.mode == "reference":
+ config["engine"]["episodes_per_operator"] = 1
+ config["trajectory"]["operators"] = ["object_pose"]
+ config["trajectory"].pop("affordance", None)
+ sampling.update(strategy="scene_first", max_rollouts=1)
+ for ranges in config["scene"].values():
+ ranges.update(translation_m=[[0, 0], [0, 0], [0, 0]], yaw_rad=[0, 0])
+ bounds = sampling["layout"]["policy"]["group_xy_offset_bounds_m"]
+ for uid in bounds:
+ bounds[uid] = [[0, 0], [0, 0]]
+ else:
+ sampling["strategy"] = args.mode
+
+ directory = args.output.resolve() / args.mode
+ if directory.exists():
+ parser.error(f"{directory} already exists. Choose a fresh output directory.")
+ directory.mkdir(parents=True)
+ snapshot = directory / "expansion.yaml"
+ snapshot.write_text(yaml.safe_dump(config, sort_keys=False))
+ result = run_task_program_expansion(
+ TASK, snapshot, directory / "collection", seed=args.seed
+ )
+ for name, group in result["operators"].items():
+ print(f"{args.mode}/{name}: {group['committed_episodes']} confirmed")
+ if not result["complete"]:
+ raise SystemExit(
+ f"Collection quota incomplete. See {directory / 'collection'}."
+ )
+
+
+if __name__ == "__main__":
+ main()
diff --git a/scripts/tutorials/expansion/ur5_pick_place/expansion.yaml b/scripts/tutorials/expansion/ur5_pick_place/expansion.yaml
new file mode 100644
index 000000000..2bbfd6642
--- /dev/null
+++ b/scripts/tutorials/expansion/ur5_pick_place/expansion.yaml
@@ -0,0 +1,122 @@
+schema_version: 1
+engine:
+ episodes_per_operator: 4
+ max_attempts: 32
+ max_steps: 2400
+ max_wall_time_s: 240
+ settling_steps: 30
+ capture_media: false
+ collision_backend: newton
+ collision_device: cpu
+
+scene:
+ cube: &xy_range
+ translation_m: [[-0.06, 0.06], [-0.06, 0.06], [0, 0]]
+ yaw_rad: [0, 0]
+ goal: *xy_range
+
+trajectory:
+ operators: [object_pose, affordance]
+ affordance:
+ branches: 3
+ call_indices: [0] # Pick the cube; the second call places it.
+ action_options:
+ pick:
+ fixed_object_to_eef: null # Use the gripper's antipodal candidate generator.
+ grasp_generators:
+ eef:
+ approach_deviation_angle: 0.12 # radians
+ max_candidates: 64
+ joint_offset_scale: 0.005
+ via_count: 1
+ max_proposals: 64
+ minimum_joint_delta: 0.002
+ velocity_limit: 0.5
+ acceleration_limit: 0.5
+ nullspace_task_rows: [0, 1, 2, 3, 4, 5]
+ nullspace_translation_tolerance_m: 0.001
+ nullspace_rotation_tolerance_rad: 0.01
+ free_phases: {place: [retract]}
+
+validation:
+ initial_checks:
+ - {kind: support_bounds, entity: cube, lower: [-1, -0.4, -0.003], upper: [-0.2, 0.7, 0.05]}
+ final_checks:
+ - &placed
+ kind: pose_relative
+ entity: cube
+ other: goal
+ position_tolerance: [0.02, 0.02, 0.01]
+ position_offset: [0, 0, 0.12]
+ yaw_tolerance_rad: 0.17453292519943295
+ yaw_period_rad: 3.141592653589793
+ tilt_tolerance_rad: 0.17453292519943295
+ - &released {kind: joint_range, joint: gripper_finger1_joint_1, lower: -0.001, upper: 0.005}
+ - &stable {kind: speed_below, entity: cube, linear_m_s: 0.03, angular_rad_s: 0.2}
+ - {kind: transport_guard, entity: cube, other: goal, lift_height_m: 0.05, minimum_transit_height_m: 0.025, goal_tolerance_m: 0.03}
+ events:
+ - event_id: lift
+ conditions: [{kind: lifted, entity: cube, height_m: 0.05}]
+ - event_id: placed_and_released
+ conditions: [*placed, *released, *stable]
+ consecutive_steps: 3
+ allowed_contacts: &contacts
+ - [cube, default_plane]
+ - [cube, support_surface]
+ - [cube, UR5/gripper_finger1_link_1]
+ - [cube, UR5/gripper_finger2_link_1]
+ mounting_contacts: [['@root', default_plane], ['@root', support_surface]]
+
+scene_sampling:
+ strategy: scene_first
+ workspace_source: joint_uniform_fk
+ control_part: arm
+ sample_count: 16384
+ robot_anchors: [{phase_id: grasp, control_part: arm}]
+ uniform_fraction: 0.25
+ max_candidates: 32
+ max_rollouts: 12
+ max_ik_calls: 16384
+ max_planning_calls: 2048
+ targets: {}
+ layout:
+ support_uid: support_surface
+ parent_by_uid: {cube: support_surface}
+ policy:
+ group_targets: [cube]
+ batch_size: 1
+ collision_margin_m: 0.02
+ group_xy_offset_bounds_m: {cube: [[-0.06, -0.06], [0.06, 0.06]]}
+ task_mode:
+ mode_id: runtime
+ phases:
+ - phase_id: approach
+ dependencies: []
+ targets:
+ - &above_cube
+ control_part: arm
+ frame_uid: cube
+ pose: [[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0.1], [0, 0, 0, 1]]
+ allowed_contacts: *contacts
+ - phase_id: grasp
+ dependencies: [approach]
+ targets:
+ - control_part: arm
+ frame_uid: cube
+ pose: &grasp_pose [[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]]
+ allowed_contacts: *contacts
+ - phase_id: lift
+ dependencies: [grasp]
+ targets: [*above_cube]
+ allowed_contacts: *contacts
+ held_objects: &held [{entity_uid: cube, control_part: arm, tcp_from_object: *grasp_pose}]
+ - phase_id: place
+ dependencies: [lift]
+ targets:
+ - control_part: arm
+ frame_uid: goal
+ pose: [[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0.12], [0, 0, 0, 1]]
+ allowed_contacts: *contacts
+ held_objects: *held
+
+scheduling: {policy: fifo, pool_size: 1, exploration_fraction: 0.25}
diff --git a/setup.py b/setup.py
index fdd3eb1f7..aee874174 100644
--- a/setup.py
+++ b/setup.py
@@ -153,7 +153,7 @@ def get_package_data() -> dict[str, list[str]]:
]
return {
"embodichain": ["VERSION"],
- "embodichain.lab.expansion": ["_preview.html", "_distribution.html"],
+ "embodichain.lab.expansion._frontend": ["*.html", "*.css", "*.js"],
"embodichain.lab.visualization": [
"web_assets/*.html",
"web_assets/*.js",
diff --git a/tests/lab/expansion/test_preview.py b/tests/lab/expansion/test_preview.py
index 1c9054d10..4c6d83d4b 100644
--- a/tests/lab/expansion/test_preview.py
+++ b/tests/lab/expansion/test_preview.py
@@ -14,18 +14,14 @@
# limitations under the License.
# ----------------------------------------------------------------------------
-"""Result-only preview: provenance, row selection, controls and HTTP lifecycle."""
+"""Result-only preview: provenance, row selection and WebRTC controls."""
from __future__ import annotations
-import base64
from dataclasses import replace
import hashlib
import json
from pathlib import Path
-import threading
-import urllib.error
-import urllib.request
import numpy as np
import pytest
import torch
@@ -38,8 +34,13 @@
)
from embodichain.lab.expansion.persistence import ArtifactEpisodeSink
from embodichain.lab.expansion._preview_data import load_preview, PreviewState
+from embodichain.lab.expansion._preview_deviations import (
+ deviation_curves,
+ recorded_coordinates,
+)
+from embodichain.lab.expansion._preview_heatmap import object_heatmaps
from embodichain.lab.expansion._preview_render import prepare_geometry
-from embodichain.lab.expansion._preview_server import PreviewTransport
+from embodichain.lab.expansion._preview_server import _WebPreviewTransport
@pytest.fixture
@@ -164,6 +165,12 @@ def test_result_loading_binds_grasp_row_and_fk_without_writing_results(recorded_
}
data = load_preview([path])
clip = data.clips[0]
+ state = PreviewState(data)
+ assert data.solid_clip is clip and state.snapshot()["total"] == 0
+ state.apply({"playing": True, "progress": 1})
+ assert state.snapshot()["source_frame"] == 2 and state.values["playing"]
+ with pytest.raises(ValueError, match="toggle_ghost"):
+ state.apply({"toggle_ghost": 0})
assert (
clip.affordance == "1" and clip.candidate_ids == ("31",) and clip.variant == "1"
)
@@ -197,6 +204,207 @@ def test_result_loader_rejects_incomplete_scene_and_budget(recorded_job, tmp_pat
load_preview([path])
+def test_curves_use_calibrated_tcp_and_per_episode_joint_maxima(recorded_job):
+ from scipy.spatial.transform import Rotation
+ from xml.etree import ElementTree as ET
+
+ path, _, _ = recorded_job
+ data = load_preview([path])
+ sample = data.clips[0]
+ offset = np.eye(4)
+ offset[0, 3] = 1
+ robot = {
+ "solver_cfg": {"wrist": {"end_link_name": "tool"}},
+ "control_parts": {"wrist": ["a|b"]},
+ "gripper_bindings": {
+ "other": {
+ "declaration": {"motion_part": "other_arm"},
+ "root_link": "other_tool",
+ },
+ "hand": {
+ "declaration": {"motion_part": "wrist"},
+ "root_link": "hand_root",
+ "defaults": {"default_root_from_tcp": offset.tolist()},
+ },
+ },
+ }
+ model = ET.fromstring(
+ ''
+ )
+ data.arm_joint_groups, tcp_frame = recorded_coordinates(robot, "tool", model)
+ assert data.arm_joint_groups == {"deg": ["a", "b"]}
+
+ def clip(times, yaw_deg, joint_names, qpos_deg):
+ transforms = np.tile(np.eye(4), (len(times), 1, 1))
+ transforms[:, :3, :3] = Rotation.from_euler(
+ "z", np.asarray(yaw_deg)[:, None], degrees=True
+ ).as_matrix()
+ measured = replace(
+ sample.measured,
+ timestamps=np.array(times),
+ joint_names=joint_names,
+ qpos=np.deg2rad(qpos_deg),
+ root_poses=np.tile([0, 0, 0, 0, 0, 0, 1], (len(times), 1)),
+ object_poses={},
+ )
+ return replace(
+ sample, measured=measured, links=transforms[:, None], tcp_frame=tcp_frame
+ )
+
+ data.reference = clip(
+ [0, 1, 2, 3], [170] * 4, ("a", "b", "finger"), np.zeros((4, 3))
+ )
+ data.clips = [
+ clip([0, 2], [170, -100], ("finger", "b", "a"), [[180, 0, 0], [180, 0, 60]]),
+ clip([0, 1], [170, 80], ("a", "finger", "b"), [[0, 180, 0], [0, 180, -60]]),
+ clip([0, 1], [170, -160], ("a", "b", "finger"), [[0, 0, 180], [-36, 0, 180]]),
+ ]
+ report = deviation_curves(data, ["hand_root"])
+ assert report["time_s"] == [0, 1, 2, 3] and report["count"] == [3, 3, 1, 0]
+ means = report["mean"]
+ # At 1 s the three rotations are 45, 90 and 30 degrees. The first
+ # position is interpolated halfway along the 90-degree TCP chord.
+ chord_mm = 1000 * np.sqrt(2)
+ short_chord_mm = 2000 * np.sin(np.deg2rad(15))
+ mean_mm = (0.5 * chord_mm + chord_mm + short_chord_mm) / 3
+ expected = {
+ "translation_mm": (
+ [0, short_chord_mm, chord_mm],
+ [0, mean_mm, chord_mm],
+ [0, chord_mm, chord_mm],
+ ),
+ "rotation_deg": ([0, 30, 90], [0, 55, 90], [0, 90, 90]),
+ # Per-episode worst joint deviations at 1 s are 30, 60 and 36 degrees.
+ "qpos_deg": ([0, 30, 60], [0, 42, 60], [0, 60, 60]),
+ }
+ for key, (low, mean, high) in expected.items():
+ band = report["range"][key]
+ np.testing.assert_allclose(band["min"][:3], low, atol=1e-9)
+ np.testing.assert_allclose(means[key][:3], mean, atol=1e-9)
+ np.testing.assert_allclose(band["max"][:3], high, atol=1e-9)
+ assert (
+ band["min"][-1] is None
+ and means[key][-1] is None
+ and band["max"][-1] is None
+ )
+ fallback = replace(data, clips=[data.reference, *data.clips], reference=None)
+ implicit = deviation_curves(fallback, ["hand_root"])
+ assert implicit["mean"] == means and implicit["range"] == report["range"]
+ # Older jobs may lack TCP calibration while retaining valid arm measurements.
+ data.clips[0].tcp_frame = None
+ no_tcp = deviation_curves(data, ["hand_root"])
+ assert no_tcp["mean"] == {"qpos_deg": means["qpos_deg"]}
+ assert no_tcp["range"] == {"qpos_deg": report["range"]["qpos_deg"]}
+
+
+def test_heatmap_bins_each_object_pose_target_once_at_its_measured_start(recorded_job):
+ path, _, _ = recorded_job
+ data = load_preview([path])
+ sample = data.solid_clip
+ anchor = sample.measured.object_poses["box"]
+
+ def clip(start, targets):
+ poses = anchor.copy()
+ poses[0, :2] = start
+ poses[1:, :2] = [50, -60]
+ measured = replace(
+ sample.measured,
+ object_poses={"box": poses, "goal": anchor, "table": anchor},
+ )
+ return replace(sample, measured=measured, object_pose_uids=targets)
+
+ a, b = (-0.25, 0.5 + 2**-20), (0.125, 0.5)
+ data.reference = clip((20, 30), ("box", "goal"))
+ data.clips = [
+ clip(a, ("box", "goal")),
+ clip(a, ("box",)),
+ clip(b, ("box",)),
+ clip((6, 7), ()),
+ ]
+ report = object_heatmaps(data)
+ assert report["episodes"] == 3
+ box, goal = report["objects"]
+ assert [box["uid"], goal["uid"]] == ["box", "goal"]
+ assert set(box["views"]) == {"start"}
+ view = box["views"]["start"]
+ np.testing.assert_array_equal(view["bounds_m"], [[a[0], b[1]], [b[0], a[1]]])
+ edges = np.array([box["x_edges_m"], box["y_edges_m"]])
+ centers = (edges[:, :-1] + edges[:, 1:]) / 2
+ np.testing.assert_allclose(
+ centers[:, [0, -1]].T, view["bounds_m"], rtol=0, atol=1e-12
+ )
+ expected = np.zeros_like(view["counts"])
+ expected[-1, 0] = 2
+ expected[0, -1] = 1
+ np.testing.assert_array_equal(view["counts"], expected)
+ assert box["episodes"] == 3
+ assert goal["episodes"] == np.sum(goal["views"]["start"]["counts"]) == 1
+ goal_edges = np.array([goal["x_edges_m"], goal["y_edges_m"]])
+ np.testing.assert_allclose(
+ goal_edges[:, [0, -1]],
+ anchor[0, :2, None] + [-0.0005, 0.0005],
+ rtol=0,
+ atol=1e-12,
+ )
+
+ # A genuine input augmentation still contributes when it doubles as the solid replay.
+ implicit = object_heatmaps(replace(data, reference=None))
+ assert implicit["objects"] == report["objects"] and implicit["episodes"] == 3
+
+
+def test_heatmap_reads_selected_operator_and_rigid_targets_from_saved_recipe(
+ recorded_job,
+):
+ path, job, episode = recorded_job
+ job["operator"] = "object_pose"
+ (path / "job.json").write_text(json.dumps(job))
+ sink = ArtifactEpisodeSink(path)
+ for ordinal, selected in enumerate(("object_pose", "affordance", None), start=1):
+ motion = {"options": {"operators": ["object_pose", "affordance"]}}
+ if selected:
+ motion["operator"] = selected
+ operations = [
+ {"name": "motion", "payload_json": json.dumps(motion)},
+ {
+ "name": "joint_scene",
+ "payload_json": json.dumps(
+ {
+ "pose_changes": {
+ "box": np.eye(4).tolist(),
+ "robot": np.eye(4).tolist(),
+ }
+ }
+ ),
+ },
+ ]
+ sink.submit(
+ replace(
+ episode,
+ episode_id=f"episode{ordinal}",
+ commit_id=f"commit{ordinal}",
+ metadata={
+ **episode.metadata,
+ "recipe_spec": {"operations": operations},
+ },
+ ),
+ submission_id=ordinal,
+ )
+
+ data = load_preview([path])
+ targets = {c.measured.name: c.object_pose_uids for c in data.clips}
+ assert targets == {
+ "episode": (),
+ "episode1": ("box",),
+ "episode2": (),
+ "episode3": ("box",),
+ }
+ report = object_heatmaps(data)
+ assert report["episodes"] == 2
+ box = report["objects"][0]
+ assert box["views"]["start"]["bounds_m"] == [[10, 20], [10, 20]]
+ assert np.sum(box["views"]["start"]["counts"]) == 2
+
+
def test_result_loader_keeps_real_mesh_spec_and_native_operator_labels(
recorded_job, tmp_path
):
@@ -226,21 +434,35 @@ def test_result_loader_keeps_real_mesh_spec_and_native_operator_labels(
assert result["affordance"] == "unrecorded"
-def test_filter_empty_intersection_and_phase_alignment(recorded_job):
+def test_independent_ghosts_keep_reference_playing_across_filters(recorded_job):
path, _, _ = recorded_job
data = load_preview([path])
first = data.clips[0]
+ data.reference = replace(first, measured=replace(first.measured, name="original"))
data.clips = [
replace(first, space="A", affordance="0", variant="0"),
replace(first, space="B", affordance="1", variant="1"),
]
state = PreviewState(data)
+ assert state.snapshot()["visible_ghosts"] == []
+ state.apply({"toggle_ghost": 0})
+ state.apply({"toggle_ghost": 1})
+ assert state.visible_ghosts() == [0, 1]
+ state.apply({"toggle_ghost": 1})
+ assert state.visible_ghosts() == [0]
state.apply({"space": "B"})
- assert state.visible() == [1] and state.values["selected"] == 1
+ assert state.matching() == [1] and state.visible_ghosts() == []
+ state.apply({"ghosts": True})
+ assert state.visible_ghosts() == [1]
state.apply({"aff": "0", "playing": True})
- assert state.visible() == [] and not state.values["playing"]
- assert state.snapshot()["source_frame"] is None
+ assert state.matching() == [] and state.values["playing"]
+ assert state.snapshot()["episode"] == "original"
+ assert state.snapshot()["source_frame"] == 0
state.apply({"space": "all", "aff": "all", "progress": 0.5})
+ assert state.visible_ghosts() == [0, 1]
+ state.apply({"space": "B", "ghosts": False})
+ state.apply({"space": "all"})
+ assert state.visible_ghosts() == [0]
assert state.frame_at(first) == 0 # phase boundary .4 s -> recorded frame at 0 s
state.apply({"align": "time"})
assert state.frame_at(first) == 1 # exactly .5 s
@@ -248,7 +470,8 @@ def test_filter_empty_intersection_and_phase_alignment(recorded_job):
assert state.frame_at(first) == 2
for update in (
{"progress": float("nan")},
- {"selected": True},
+ {"toggle_ghost": True},
+ {"toggle_ghost": 2},
{"playing": "true"},
{"unknown": 0},
{"aff": "missing"},
@@ -259,41 +482,53 @@ def test_filter_empty_intersection_and_phase_alignment(recorded_job):
assert state.values == before
-def test_http_snapshot_control_and_shutdown(recorded_job):
+def test_playback_toggles_in_order_and_restarts_from_the_end(recorded_job):
path, _, _ = recorded_job
state = PreviewState(load_preview([path]))
- transport = PreviewTransport(state, 0)
- transport.publish({"frame": 7, "source_frame": 2}, b"jpeg-frame-7")
- thread = threading.Thread(target=transport.server.serve_forever)
- thread.start()
- try:
- snapshot = json.load(urllib.request.urlopen(transport.url + "snapshot"))
- assert snapshot["frame"] == 7 and snapshot["source_frame"] == 2
- assert base64.b64decode(snapshot["image"].split(",")[1]) == b"jpeg-frame-7"
- req = urllib.request.Request(
- transport.url + "control",
- data=b'{"progress":0.5}',
- headers={"Content-Type": "application/json"},
- method="POST",
- )
- assert urllib.request.urlopen(req).status == 204
- assert (
- state.values["progress"] == 0
- ) # only the render owner applies queued commands
- state.apply(transport.commands.get_nowait())
- assert state.values["progress"] == 0.5
- req.add_header("Origin", "https://untrusted.example")
- with pytest.raises(urllib.error.HTTPError) as error:
- urllib.request.urlopen(req)
- assert error.value.code == 403
- finally:
- transport.server.shutdown()
- thread.join(timeout=5)
- transport.server.server_close()
- assert not thread.is_alive()
-
-
-def test_preview_cli_dispatch_does_not_collect(monkeypatch, recorded_job):
+ state.apply({"progress": 0.5})
+ toggle = {"toggle_playback": True}
+ state.apply(toggle)
+ assert state.values["playing"] and state.values["progress"] == 0.5
+ state.apply(toggle)
+ assert not state.values["playing"] and state.values["progress"] == 0.5
+ state.apply({"progress": 1})
+ state.apply(toggle)
+ assert state.values["playing"] and state.values["progress"] == 0
+
+
+def test_native_preview_publishes_metadata_and_queues_valid_replay_controls(
+ recorded_job,
+):
+ from types import SimpleNamespace
+
+ path, _, _ = recorded_job
+ state = PreviewState(load_preview([path]))
+ application = {}
+
+ def install(assets, request):
+ application.update(assets=assets, request=request)
+
+ viewer = SimpleNamespace(
+ endpoint="http://127.0.0.1:8769/", _replace_application=install
+ )
+ transport = _WebPreviewTransport(state, viewer, b'{"episodes":0}')
+ transport.publish({"frame": 7, "source_frame": 2})
+ request = application["request"]
+ code, body = request("/api/state", "{}")
+ assert code == 200
+ assert json.loads(body) == {"frame": 7, "source_frame": 2}
+ assert b'' in application["assets"]["/"][1]
+ assert request("/api/command", '{"progress":0.5}')[0] == 200
+ assert state.values["progress"] == 0
+ state.apply(transport.commands.popleft())
+ assert state.values["progress"] == 0.5
+ assert request("/api/command", '{"playing":"yes"}')[0] == 400
+ assert not transport.commands
+
+
+def test_preview_cli_dispatches_confirmed_records_without_collecting(
+ monkeypatch, recorded_job
+):
from embodichain.cli.expansion import main
import embodichain.lab.expansion._preview_server as server
from embodichain.lab.expansion import ExpansionEngine
@@ -314,20 +549,22 @@ def test_preview_cli_dispatch_does_not_collect(monkeypatch, recorded_job):
)
-def test_viewer_html_is_in_package_data(tmp_path):
+def test_expansion_frontend_assets_are_in_package_data(tmp_path):
from setup import get_package_data
from setuptools import Distribution
from setuptools.command.build_py import build_py
- import shutil
- package = "embodichain.lab.expansion"
- source = tmp_path / "source"
- source.mkdir()
- original = (
- Path(__file__).resolve().parents[3] / "embodichain/lab/expansion/_preview.html"
+ package = "embodichain.lab.expansion._frontend"
+ source = Path(__file__).resolve().parents[3] / "embodichain/lab/expansion/_frontend"
+ assets = (
+ "distribution.html",
+ "preview.html",
+ "preview.css",
+ "preview-text.js",
+ "preview-controls.js",
+ "preview-charts.js",
+ "preview-heatmap.js",
)
- shutil.copyfile(original, source / "_preview.html")
- (source / "__init__.py").write_text("")
distribution = Distribution(
{
"packages": [package],
@@ -341,8 +578,9 @@ def test_viewer_html_is_in_package_data(tmp_path):
command.build_lib = str(tmp_path / "build")
command.ensure_finalized()
command.run()
- packaged = Path(command.build_lib) / "embodichain/lab/expansion/_preview.html"
- assert packaged.read_bytes() == original.read_bytes()
+ packaged = Path(command.build_lib) / "embodichain/lab/expansion/_frontend"
+ for name in assets:
+ assert (packaged / name).read_bytes() == (source / name).read_bytes()
def test_manifest_keeps_rejected_points_visible_in_coverage(recorded_job):
@@ -456,3 +694,12 @@ def test_aggregate_input_keeps_explicit_reference_separate(recorded_job, second_
assert len(data.clips) == 1 + second_count
assert data.reference.space.endswith("reference")
assert all(not clip.space.endswith("reference") for clip in data.clips)
+ # Without a separate original, the first input stays solid across group changes.
+ fallback = PreviewState(replace(data, reference=None))
+ fallback.apply({"space": data.clips[-1].space, "ghosts": True})
+ snapshot = fallback.snapshot()
+ assert snapshot["episode"] == "first-0"
+ assert snapshot["total"] == second_count
+ assert {data.clips[i].measured.name for i in snapshot["visible_ghosts"]} == {
+ f"second-{index}" for index in range(second_count)
+ }
diff --git a/tests/lab/expansion/test_scene_first_sampling.py b/tests/lab/expansion/test_scene_first_sampling.py
index 85f8b6ba5..d5263f3fc 100644
--- a/tests/lab/expansion/test_scene_first_sampling.py
+++ b/tests/lab/expansion/test_scene_first_sampling.py
@@ -25,9 +25,100 @@
import torch
from scene_layout_support import make_source
-from embodichain.lab.expansion._scene_first_sampling import _LayoutPoseSource
+from embodichain.lab.expansion._scene_first_sampling import (
+ _LayoutPoseSource,
+ _layout_collision_bodies,
+)
from embodichain.lab.expansion._pose_sampling_cfg import validate_sampling
-from embodichain.lab.sim.scene.feasibility import FeasibilityBudgetExhausted
+from embodichain.lab.sim.scene.feasibility import (
+ FeasibilityBudgetExhausted,
+ UnsupportedFeasibilityError,
+)
+
+
+def test_layout_mesh_parts_preserve_the_physical_frame_and_openings():
+ import trimesh
+ from scipy.spatial import ConvexHull
+
+ from embodichain.lab.task_program.integrations.simulation.feasibility_geometry import (
+ collision_bodies_from_simulation,
+ )
+
+ hull = SimpleNamespace(
+ shape_type=SimpleNamespace(name="CONVEX"),
+ local_pose=torch.eye(4),
+ vertices=torch.tensor([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]]),
+ triangles=torch.tensor([[0, 2, 1], [0, 1, 3], [0, 3, 2], [1, 2, 3]]),
+ )
+ pose = torch.tensor([[0, -1, 0, 10], [1, 0, 0, 0], [0, 0, 1, 1], [0, 0, 0, 1.0]])
+ mesh = SimpleNamespace(
+ shape_type=SimpleNamespace(name="MESH"),
+ local_pose=pose.clone(),
+ vertices=torch.tensor([[1, 2, 0], [5, 2, 0], [1, 4, 0]]),
+ triangles=torch.tensor([[0, 1, 2]]),
+ )
+ robot = SimpleNamespace(
+ uid="robot",
+ cfg=SimpleNamespace(body_scale=(1, 1, 1)),
+ link_names=("arm",),
+ get_link_collision_shapes=lambda link: [hull],
+ )
+ env = SimpleNamespace(
+ robot=robot,
+ sim=SimpleNamespace(
+ get_articulation_uid_list=lambda: (),
+ get_rigid_object_uid_list=lambda: ["shelf"],
+ get_rigid_object=lambda uid: SimpleNamespace(
+ get_collision_shapes=lambda: [mesh]
+ ),
+ ),
+ )
+ (arm, shelf), counts = _layout_collision_bodies(env)
+ assert arm.kind == "convex" and arm.robot
+ np.testing.assert_array_equal(arm.vertices, hull.vertices)
+ assert counts == {"shelf/0": 1}
+ assert shelf.source_collision_id == "shelf/0" and shelf.kind == "convex"
+ np.testing.assert_allclose(shelf.local_pose, pose)
+ np.testing.assert_allclose(
+ np.unique(np.round(np.asarray(shelf.vertices)[:, :2], 8), axis=0),
+ [[1, 2], [1, 4], [5, 2]],
+ )
+ np.testing.assert_allclose(np.ptp(np.asarray(shelf.vertices)[:, 2]), 1e-6)
+ torch.testing.assert_close(mesh.local_pose, pose)
+
+ ring = trimesh.creation.annulus(r_min=0.5, r_max=1.0, height=0.2, sections=32)
+ mesh.vertices = torch.tensor(ring.vertices)
+ mesh.triangles = torch.tensor(ring.faces)
+ bodies, counts = _layout_collision_bodies(env)
+ parts = bodies[1:]
+ assert counts == {"shelf/0": len(parts)} and len(parts) > 1
+ planes = [ConvexHull(part.vertices).equations for part in parts]
+
+ def contains(point):
+ return any(np.all(eq[:, :3] @ point + eq[:, 3] <= 1e-8) for eq in planes)
+
+ assert not contains([0, 0, 0])
+ assert contains([0.75, 0, 0])
+ assert contains([-0.75, 0, 0])
+ assert all(part.source_collision_id == "shelf/0" for part in parts)
+
+ with pytest.raises(UnsupportedFeasibilityError, match="shelf/0: mesh"):
+ collision_bodies_from_simulation(env)
+ robot.get_link_collision_shapes = lambda link: [mesh]
+ with pytest.raises(UnsupportedFeasibilityError, match="robot/arm/0: mesh"):
+ collision_bodies_from_simulation(env)
+ attached, both = _layout_collision_bodies(env)
+ robot_parts = [body for body in attached if body.robot]
+ assert both == {"robot/arm/0": len(parts), "shelf/0": len(parts)}
+ assert len(robot_parts) == len(parts) and all(
+ body.frame_uid == "robot/arm" for body in robot_parts
+ )
+ robot.get_link_collision_shapes = lambda link: [hull]
+ mesh.triangles = torch.tensor([[0, 1, len(mesh.vertices)]])
+ with pytest.raises(
+ UnsupportedFeasibilityError, match="Invalid physical mesh for shelf/0"
+ ):
+ _layout_collision_bodies(env)
def setup_source(
@@ -77,10 +168,7 @@ def get_local_pose(self, to_matrix=True):
checked = []
closed = []
monkeypatch.setattr(module, "scene_layout_input_from_sim", lambda *a, **kw: source)
- monkeypatch.setattr(
- "embodichain.lab.task_program.integrations.simulation.feasibility_geometry.collision_bodies_from_simulation",
- lambda env: (),
- )
+ monkeypatch.setattr(module, "_layout_collision_bodies", lambda env: ((), {}))
monkeypatch.setattr(
"embodichain.lab.expansion.acceptance._GeometryRow.contact_permissions",
lambda *a: ((), ()),
diff --git a/tests/sim/scene/expansion/test_layout.py b/tests/sim/scene/expansion/test_layout.py
index f1440977c..371e0dbb5 100644
--- a/tests/sim/scene/expansion/test_layout.py
+++ b/tests/sim/scene/expansion/test_layout.py
@@ -24,6 +24,7 @@
import torch
from embodichain.lab.sim.scene.expansion import (
+ SceneLayoutCondition,
SceneLayoutInput,
SceneRandomizationPolicyCfg,
ScenePoseChange,
@@ -146,6 +147,72 @@ def test_root_offset_bounds_preserve_batch_prefix_and_saved_poses(
assert tuple(load_scene_variants(run).variants) == tuple(results[0].variants)
+def test_bounded_roots_on_separate_support_patches_and_over_a_gap(tmp_path) -> None:
+ import trimesh
+
+ def pad(width, x):
+ part = trimesh.creation.box([width, 0.18, 0.04])
+ part.apply_translation([x, 0, 0])
+ return part
+
+ # The largest top is elsewhere. Two planks overhang their own smaller pads.
+ support = trimesh.util.concatenate(
+ [pad(0.5, 0.55), pad(0.12, -0.4), pad(0.12, -0.05)]
+ )
+ plank = trimesh.creation.box([0.20, 0.06, 0.04])
+ meshes = {"rack": support, "moving": plank, "fixed": plank}
+ poses = {uid: np.eye(4) for uid in meshes}
+ poses["moving"][:3, 3] = [-0.4, 0, 0.04]
+ poses["fixed"][:3, 3] = [-0.05, 0, 0.04]
+ source = SceneLayoutInput(
+ "rack-layout",
+ "rack",
+ poses,
+ {u: m.vertices for u, m in meshes.items()},
+ {u: m.faces for u, m in meshes.items()},
+ {"moving": "rack", "fixed": "rack"},
+ ("moving",),
+ )
+ policy = dict(
+ group_targets=["moving"],
+ group_xy_offset_bounds_m={"moving": [[-0.1, -0.1], [0.1, 0.1]]},
+ )
+ prepared = prepare_scene_randomization(source, policy)
+ result = generate_scene_variants(prepared, candidate_count=8, seed=13)
+ assert result.status == "completed"
+ centers = np.array([v.pose_changes[0].arena_pose for v in result.variants])[
+ :, :2, 3
+ ]
+ assert np.ptp(centers[:, 0]) > 0.005
+ assert np.all(np.abs(centers - [-0.4, 0]) <= [0.06, 0.09])
+ anchor = poses["moving"].copy()
+ anchor[0, 3] += 0.01
+ anchored = generate_scene_variants(
+ prepared, candidate_count=1, conditions=SceneLayoutCondition({"moving": anchor})
+ )
+ assert anchored.status == "completed"
+ run = save_scene_variants(anchored, tmp_path / "supported")
+ assert tuple(load_scene_variants(run, scene_input=source).variants) == tuple(
+ anchored.variants
+ )
+ anchor[0, 3] = -0.31
+ assert (
+ generate_scene_variants(
+ prepared,
+ candidate_count=1,
+ max_attempts=4,
+ conditions=SceneLayoutCondition({"moving": anchor}),
+ ).status
+ == "budget_exhausted"
+ )
+ poses["fixed"][1, 3] = 0.35
+ unsupported = prepare_scene_randomization(replace(source, poses=poses), policy)
+ assert (
+ generate_scene_variants(unsupported, candidate_count=1, max_attempts=4).status
+ == "budget_exhausted"
+ )
+
+
def test_fixed_descendant_permission_is_not_implicitly_relaxed() -> None:
source = replace(make_source(), movable_entity_ids=("tray",))
with pytest.raises(ValueError, match="fixed"):
diff --git a/tests/sim/scene/feasibility/test_collision_planning.py b/tests/sim/scene/feasibility/test_collision_planning.py
index 12b028b1e..2ddd54f38 100644
--- a/tests/sim/scene/feasibility/test_collision_planning.py
+++ b/tests/sim/scene/feasibility/test_collision_planning.py
@@ -130,6 +130,94 @@ def test_batched_geometry_matches_scalar_rows_and_rebinds_permissions(device):
checker.close()
+@pytest.mark.parametrize(
+ "device", ["cpu", pytest.param("cuda:0", marks=pytest.mark.gpu)]
+)
+def test_decomposed_geometry_crosses_512_shapes_with_physical_pair_identity(
+ monkeypatch, device
+):
+ pytest.importorskip("newton")
+ bodies = [CollisionBody("ground", "ground", "halfspace", (0, 0, 1, 0))]
+ for frame, axis, count in (("arm", 0, 257), ("payload", 1, 256)):
+ for i in range(count):
+ local = np.eye(4)
+ local[axis, 3] = i / 8
+ bodies.append(
+ CollisionBody(
+ f"{frame}/{i}",
+ frame,
+ "sphere",
+ (0.01,),
+ local,
+ robot=frame == "arm",
+ source_collision_id=(
+ "payload/fixture" if frame == "payload" and i >= 254 else None
+ ),
+ )
+ )
+ with pytest.raises(UnsupportedFeasibilityError, match="shape budget"):
+ NewtonCollisionChecker(bodies, max_shapes=512)
+ checker = NewtonCollisionChecker(
+ bodies, device=device, ignored_pairs=(("ground", "arm"),), max_contacts=64
+ )
+ try:
+ frames = {
+ name: np.repeat(np.eye(4)[None], 2, axis=0)
+ for name in ("arm", "payload", "ground")
+ }
+ frames["ground"][:, 2, 3] = -1
+ frames["payload"][:, :3, 3] = [256 / 8, -255 / 8, 0]
+ frames["payload"][1, 2, 3] = 1
+ pair = ("arm/256", "payload/fixture")
+ assert checker.metadata["native_pair_count"] == 257 * 256
+ assert len(checker.ignored_pairs) == 257
+ assert frozenset(("ground", "arm/256")) in checker.ignored_pairs
+ assert checker.check({k: v[0] for k, v in frames.items()})[0].status == "failed"
+ assert checker.last_pair == pair
+ assert [r.status for r in checker.check_batch(frames)] == ["failed", "passed"]
+ assert checker.last_batch_pairs == (pair, None)
+ assert (
+ checker.check_batch(frames, allowed_contacts=(pair,))[0].status == "passed"
+ )
+ assert (
+ checker.check_batch(frames, allowed_contacts=(("payload", "arm/256"),))[
+ 0
+ ].status
+ == "passed"
+ )
+ assert (
+ checker.check_batch(frames, allowed_contacts=(("payload/254", "arm"),))[
+ 0
+ ].status
+ == "failed"
+ )
+ assert (
+ checker.check_batch(frames, allowed_contacts=(("payload/255", "arm"),))[
+ 0
+ ].status
+ == "passed"
+ )
+ assert (
+ checker.check(
+ {key: value[0] for key, value in frames.items()},
+ allowed_contacts=(pair,),
+ )[0].status
+ == "passed"
+ )
+ # One row fits the aggregate pair budget. The reversed selection crosses chunks.
+ monkeypatch.setattr(
+ "embodichain.lab.sim.scene.feasibility.collision._MAX_QUERY_PAIRS",
+ 257 * 256,
+ )
+ assert [r.status for r in checker.check_batch(frames, env_ids=(1, 0))] == [
+ "passed",
+ "failed",
+ ]
+ assert checker.last_batch_pairs == (None, pair)
+ finally:
+ checker.close()
+
+
def test_phase_branches_must_connect_and_dual_targets_stay_synchronized():
identity = np.eye(4).tolist()
mode = TaskOperationMode(
@@ -297,6 +385,19 @@ def check(self, *args, **kwargs):
result = plan_task_mode(simultaneous, backend, {}, FeasibilityBudget(cfg))
assert result.accepted and result.paths[-1][-1] == pytest.approx((1.0, 0.0))
+ # OPW supplies one integer flag per analytic branch on the selected row.
+ validity = torch.tensor([[0, 1]], dtype=torch.int32)
+ backend.robot.compute_ik = lambda pose, **kw: (
+ validity,
+ pose[:, None, 2, 3, None].expand(-1, 2, -1),
+ )
+ assert plan_task_mode(mode, backend, {}, FeasibilityBudget(cfg)).accepted
+ validity[0, 1] = 0
+ assert not plan_task_mode(mode, backend, {}, FeasibilityBudget(cfg)).accepted
+ validity[0, 1] = 2
+ with pytest.raises(ValueError, match="IK result shape/type"):
+ plan_task_mode(mode, backend, {}, FeasibilityBudget(cfg))
+
def test_robot_adapter_projects_mimic_follower_for_collision():
from types import SimpleNamespace
diff --git a/tests/test_task_catalog.py b/tests/test_task_catalog.py
index 6b194be45..9c3fb39e5 100644
--- a/tests/test_task_catalog.py
+++ b/tests/test_task_catalog.py
@@ -268,14 +268,27 @@ def test_show_and_filtered_gallery_use_static_records(
assert "Pick <cube>" not in output.read_text()
-def test_environment_component_supplies_backend(task_root: Path) -> None:
+@pytest.mark.parametrize(
+ ("component", "backend"),
+ [
+ ("env.yaml", "newton"),
+ (
+ "embodichain_tasks/configs/tasks/manipulation/tableware/stack_blocks_two/env.yaml",
+ "default",
+ ),
+ ],
+ ids=["relative", "packaged"],
+)
+def test_environment_component_supplies_backend(
+ task_root: Path, component: str, backend: str
+) -> None:
write(
task_root / "manipulation/pick/task.a.yaml",
- {"id": "Pick-A-v1", "environment": {"component": "env.yaml"}},
+ {"id": "Pick-A-v1", "environment": {"component": component}},
)
write(task_root / "manipulation/pick/env.yaml", {"physics": "newton"})
(task,) = catalog._load_catalog({"demo": task_root})
- assert task.deployments[0].physics == "newton"
+ assert task.deployments[0].physics == backend
def test_duplicate_authored_fields_fail(task_root: Path) -> None:
diff --git a/tests/visualization/test_web_viewer.py b/tests/visualization/test_web_viewer.py
index 6592c354b..ebc18bba9 100644
--- a/tests/visualization/test_web_viewer.py
+++ b/tests/visualization/test_web_viewer.py
@@ -65,6 +65,7 @@ def viewer():
value.orbit = np.array(value.cfg.target)
value.group = object()
value.application = Mock()
+ value._application_handler = value._request
value.arena = Mock()
value.error = None
value.frames = 0