Files
Warehouse-Digital-Twin-and-…/tests/unit/test_realistic_3d_assets.py
T

189 lines
7.4 KiB
Python

"""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