feat(assets): integrate realistic 3d openusd assets, modular builder, and robot animator
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
"""Unit tests for Phase 2: 3D Asset Validation and Manifest Integrity."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def manifest_path(project_root: Path) -> Path:
|
||||
return project_root / "config" / "asset_manifest.json"
|
||||
|
||||
|
||||
def test_asset_manifest_structure_and_completeness(manifest_path: Path):
|
||||
"""Verify that asset_manifest.json exists, is valid JSON, and contains required assets."""
|
||||
assert manifest_path.exists(), f"Asset manifest missing at {manifest_path}"
|
||||
|
||||
with open(manifest_path, "r", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
|
||||
assert data["version"] == "1.0.0"
|
||||
assert data["meters_per_unit"] == 1.0
|
||||
assert data["up_axis"] == "Z"
|
||||
assert "assets" in data
|
||||
|
||||
assets = data["assets"]
|
||||
required_keys = [
|
||||
"pallet_rack_heavy",
|
||||
"shelving_unit_light",
|
||||
"euro_pallet",
|
||||
"pallet_with_cartons",
|
||||
"amr_light",
|
||||
"amr_heavy",
|
||||
"dock_inbound",
|
||||
"picking_station",
|
||||
"charging_station",
|
||||
"safety_barrier",
|
||||
"building_shell",
|
||||
]
|
||||
|
||||
for key in required_keys:
|
||||
assert key in assets, f"Required asset '{key}' missing from manifest"
|
||||
entry = assets[key]
|
||||
assert "path" in entry
|
||||
assert "dimensions" in entry
|
||||
assert len(entry["dimensions"]) == 3
|
||||
assert "role" in entry
|
||||
|
||||
|
||||
def test_manifest_assets_exist_on_disk(project_root: Path, manifest_path: Path):
|
||||
"""Verify that every asset referenced in the manifest physically exists and is non-empty."""
|
||||
with open(manifest_path, "r", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
|
||||
for asset_id, entry in data["assets"].items():
|
||||
rel_path = entry["path"]
|
||||
file_path = project_root / rel_path
|
||||
assert file_path.exists(), f"Asset file for '{asset_id}' does not exist at {file_path}"
|
||||
assert file_path.stat().st_size > 100, f"Asset file for '{asset_id}' is empty or too small"
|
||||
|
||||
|
||||
def test_rack_and_robot_usda_syntax(project_root: Path):
|
||||
"""Verify OpenUSD syntax conventions for rack and robot models (Phase 2 validation)."""
|
||||
rack_file = project_root / "assets" / "racks" / "pallet_rack_heavy.usda"
|
||||
robot_file = project_root / "assets" / "robots" / "amr_light.usda"
|
||||
|
||||
for fpath in (rack_file, robot_file):
|
||||
text = fpath.read_text(encoding="utf-8")
|
||||
assert text.startswith("#usda 1.0"), f"{fpath} missing #usda 1.0 header"
|
||||
assert 'metersPerUnit = 1.0' in text
|
||||
assert 'upAxis = "Z"' in text
|
||||
assert "def Xform" in text
|
||||
assert "color3f[] primvars:displayColor" in text
|
||||
@@ -0,0 +1,188 @@
|
||||
"""Unit tests for realistic 3D asset integration, warehouse builder, and robot animator."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
import pytest
|
||||
|
||||
from warehouse_twin.config.loader import ConfigLoader
|
||||
from warehouse_twin.visualization.asset_loader import AssetManifest, USDAssetLoader
|
||||
from warehouse_twin.visualization.coordinate_mapper import CoordinateMapper
|
||||
from warehouse_twin.visualization.materials import MaterialManager
|
||||
from warehouse_twin.visualization.robot_animator import Robot3DAnimator
|
||||
from warehouse_twin.visualization.route_visualizer import RouteVisualizer
|
||||
from warehouse_twin.visualization.trajectory_player import TrajectoryData
|
||||
from warehouse_twin.visualization.warehouse_builder import Warehouse3DBuilder
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sample_bundle(project_config_dir: Path):
|
||||
loader = ConfigLoader(project_config_dir)
|
||||
return loader.load_all()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def manifest_file(project_root: Path) -> Path:
|
||||
return project_root / "config" / "asset_manifest.json"
|
||||
|
||||
|
||||
def test_coordinate_mapper_bidirectional():
|
||||
"""Verify coordinate mapper maps between graph space and USD world space correctly."""
|
||||
mapper = CoordinateMapper(origin=(10.0, 5.0, 0.0), floor_elevation=0.2, scale_factor=1.0)
|
||||
wx, wy, wz = mapper.graph_to_world(5.0, 15.0, 0.0)
|
||||
assert wx == 15.0
|
||||
assert wy == 20.0
|
||||
assert wz == 0.2
|
||||
|
||||
gx, gy, gz = mapper.world_to_graph(wx, wy, wz)
|
||||
assert gx == 5.0
|
||||
assert gy == 15.0
|
||||
assert gz == 0.0
|
||||
|
||||
|
||||
def test_asset_loader_creates_valid_references(manifest_file: Path, tmp_path: Path):
|
||||
"""Verify USDAssetLoader correctly generates OpenUSD ASCII reference syntax."""
|
||||
manifest = AssetManifest(manifest_file)
|
||||
loader = USDAssetLoader(manifest)
|
||||
|
||||
instance = loader.create_reference(
|
||||
prim_path="/World/Warehouse/Racks/rack_a_01",
|
||||
asset_id="pallet_rack_heavy",
|
||||
translation=(8.0, 10.0, 0.0),
|
||||
rotation_xyz=(0.0, 0.0, 0.0),
|
||||
)
|
||||
|
||||
assert instance.prim_path == "/World/Warehouse/Racks/rack_a_01"
|
||||
assert "/World/Warehouse/Racks/rack_a_01" in loader.registered_prim_paths
|
||||
|
||||
snippet = instance.generate_usda_reference(scene_directory=tmp_path)
|
||||
assert 'def Xform "rack_a_01"' in snippet
|
||||
assert "references = @" in snippet
|
||||
assert "double3 xformOp:translate = (8.000, 10.000, 0.000)" in snippet
|
||||
|
||||
|
||||
def test_warehouse_builder_generates_valid_scene(sample_bundle, manifest_file: Path, tmp_path: Path):
|
||||
"""Verify Warehouse3DBuilder generates complete scene with modular referenced 3D assets."""
|
||||
builder = Warehouse3DBuilder.from_config_bundle(sample_bundle, manifest_path=manifest_file)
|
||||
scene_file = tmp_path / "warehouse_realistic.usda"
|
||||
saved = builder.export_usda(scene_file)
|
||||
|
||||
assert saved.exists()
|
||||
content = saved.read_text(encoding="utf-8")
|
||||
|
||||
# Header and stage configuration
|
||||
assert content.startswith("#usda 1.0")
|
||||
assert 'metersPerUnit = 1.0' in content
|
||||
assert 'upAxis = "Z"' in content
|
||||
|
||||
# Environment lighting
|
||||
assert 'def DistantLight "SunLight"' in content
|
||||
assert 'def DomeLight "SkyLight"' in content
|
||||
|
||||
# Materials scope
|
||||
assert 'def Scope "Materials"' in content
|
||||
assert 'def Material "ConcreteFloor"' in content
|
||||
assert 'def Material "SafetyYellow"' in content
|
||||
|
||||
# Architecture referencing building_shell.usda
|
||||
assert 'def Scope "Architecture"' in content
|
||||
assert 'def Xform "BuildingShell"' in content
|
||||
assert "building_shell.usda" in content
|
||||
|
||||
# Zones
|
||||
assert 'def Scope "Zones"' in content
|
||||
assert 'def Cube "zone_a"' in content
|
||||
assert 'def Cube "zone_b"' in content
|
||||
assert 'def Cube "zone_c"' in content
|
||||
|
||||
# Racks & Shelving units
|
||||
assert 'def Scope "Racks"' in content
|
||||
assert 'def Xform "rack_a_01"' in content
|
||||
assert 'def Xform "rack_b_01"' in content
|
||||
assert "shelving_unit_light.usda" in content
|
||||
assert "pallet_rack_heavy.usda" in content
|
||||
|
||||
# Multi-tier inventory pallets & cartons
|
||||
assert 'def Scope "Inventory"' in content
|
||||
assert "pallet_with_cartons.usda" in content
|
||||
assert "euro_pallet.usda" in content
|
||||
|
||||
# Stations
|
||||
assert 'def Scope "Stations"' in content
|
||||
assert 'def Xform "dock_inbound_1"' in content
|
||||
assert 'def Xform "picking_station_a1"' in content
|
||||
assert "dock_inbound.usda" in content
|
||||
assert "picking_station.usda" in content
|
||||
|
||||
# Charging Bays
|
||||
assert 'def Scope "ChargingStations"' in content
|
||||
assert 'def Xform "charge_bay_1"' in content
|
||||
assert "charging_station.usda" in content
|
||||
|
||||
# Safety Barriers & Navigation Routes
|
||||
assert 'def Scope "SafetyBarriers"' in content
|
||||
assert 'def Scope "Routes"' in content
|
||||
|
||||
# Cameras
|
||||
assert 'def Camera "OverviewCamera"' in content
|
||||
assert 'def Camera "TopDownCamera"' in content
|
||||
|
||||
# Fleet references
|
||||
assert 'def Scope "Robots"' in content
|
||||
assert 'def Xform "amr_light_01"' in content
|
||||
assert 'def Xform "amr_heavy_01"' in content
|
||||
assert "amr_light.usda" in content
|
||||
assert "amr_heavy.usda" in content
|
||||
|
||||
|
||||
def test_robot_animator_single_and_multi_robot(sample_bundle, manifest_file: Path, tmp_path: Path):
|
||||
"""Verify Robot3DAnimator generates time-sampled animation tracks for single and full fleet."""
|
||||
mock_trajectory = {
|
||||
"metadata": {"simulation_duration": 10.0},
|
||||
"robots": {
|
||||
"amr_light_01": [
|
||||
{"t": 0.0, "pos": [2.0, 36.0, 0.0], "yaw_deg": 0.0, "state": "IDLE", "battery_percent": 100.0, "is_loaded": False},
|
||||
{"t": 5.0, "pos": [12.0, 36.0, 0.0], "yaw_deg": 0.0, "state": "LOADED_TRANSIT", "battery_percent": 98.0, "is_loaded": True},
|
||||
{"t": 10.0, "pos": [20.0, 36.0, 0.0], "yaw_deg": 90.0, "state": "DROPPING", "battery_percent": 96.0, "is_loaded": True},
|
||||
],
|
||||
"amr_heavy_01": [
|
||||
{"t": 0.0, "pos": [6.0, 36.0, 0.0], "yaw_deg": 0.0, "state": "IDLE", "battery_percent": 100.0, "is_loaded": False},
|
||||
{"t": 10.0, "pos": [15.0, 20.0, 0.0], "yaw_deg": 180.0, "state": "CHARGING", "battery_percent": 82.0, "is_loaded": False},
|
||||
],
|
||||
},
|
||||
}
|
||||
traj_data = TrajectoryData.from_dict(mock_trajectory)
|
||||
|
||||
animator = Robot3DAnimator.from_config_and_trajectory(
|
||||
bundle=sample_bundle,
|
||||
trajectory_data=traj_data,
|
||||
manifest_path=manifest_file,
|
||||
sample_step_seconds=2.0,
|
||||
timecodes_per_second=24.0,
|
||||
)
|
||||
|
||||
# Test single robot animation (Phase 5)
|
||||
single_file = tmp_path / "single_robot_animated.usda"
|
||||
saved_single = animator.export_usda(single_file, target_robot_ids=["amr_light_01"])
|
||||
assert saved_single.exists()
|
||||
single_content = saved_single.read_text(encoding="utf-8")
|
||||
|
||||
assert 'def Xform "amr_light_01"' in single_content
|
||||
assert "amr_light.usda" in single_content
|
||||
assert "amr_heavy_01" not in single_content # Only target robot
|
||||
assert "double3 xformOp:translate.timeSamples = {" in single_content
|
||||
assert "double3 xformOp:rotateXYZ.timeSamples = {" in single_content
|
||||
assert 'def Xform "CargoAttachment"' in single_content
|
||||
assert "pallet_with_cartons.usda" in single_content
|
||||
|
||||
# Test full fleet animation (Phase 6)
|
||||
fleet_file = tmp_path / "fleet_animated.usda"
|
||||
saved_fleet = animator.export_usda(fleet_file)
|
||||
assert saved_fleet.exists()
|
||||
fleet_content = saved_fleet.read_text(encoding="utf-8")
|
||||
|
||||
assert 'def Xform "amr_light_01"' in fleet_content
|
||||
assert 'def Xform "amr_heavy_01"' in fleet_content
|
||||
assert "amr_light.usda" in fleet_content
|
||||
assert "amr_heavy.usda" in fleet_content
|
||||
Reference in New Issue
Block a user