84 lines
3.2 KiB
Python
84 lines
3.2 KiB
Python
"""Unit tests for time-sampled OpenUSD animation stage generator."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
import pytest
|
|
|
|
from warehouse_twin.config.loader import ConfigLoader
|
|
from warehouse_twin.visualization.animated_usd import (
|
|
AnimatedUSDGenerator,
|
|
compute_battery_indicator_color,
|
|
)
|
|
from warehouse_twin.visualization.trajectory_player import TrajectoryData
|
|
|
|
|
|
def test_battery_indicator_colors():
|
|
"""Verify RGB color codes across high, medium, and low battery levels."""
|
|
green = compute_battery_indicator_color(85.0)
|
|
assert green[1] > 0.8 # Strong green channel
|
|
|
|
amber = compute_battery_indicator_color(35.0)
|
|
assert amber[0] > 0.8 and amber[1] > 0.7 # Amber / Yellow
|
|
|
|
red = compute_battery_indicator_color(15.0)
|
|
assert red[0] > 0.8 and red[1] < 0.3 # Bright red
|
|
|
|
|
|
def test_animated_usd_generation_and_export(project_config_dir: Path, tmp_path: Path):
|
|
"""Verify generated USDA syntax, time-sampled attributes, and file export."""
|
|
loader = ConfigLoader(project_config_dir)
|
|
bundle = loader.load_all()
|
|
|
|
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": [10.0, 36.0, 0.0], "yaw_deg": 0.0, "state": "LOADED_TRANSIT", "battery_percent": 98.0, "is_loaded": True},
|
|
{"t": 10.0, "pos": [15.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": [2.0, 38.0, 0.0], "yaw_deg": 0.0, "state": "IDLE", "battery_percent": 100.0, "is_loaded": False},
|
|
{"t": 10.0, "pos": [5.0, 38.0, 0.0], "yaw_deg": 0.0, "state": "CHARGING", "battery_percent": 80.0, "is_loaded": False},
|
|
],
|
|
},
|
|
}
|
|
|
|
data = TrajectoryData.from_dict(mock_trajectory)
|
|
|
|
generator = AnimatedUSDGenerator.from_config_and_trajectory(
|
|
bundle=bundle,
|
|
trajectory_data=data,
|
|
sample_step_seconds=2.0,
|
|
timecodes_per_second=24.0,
|
|
)
|
|
|
|
usda_text = generator.generate_animated_usda()
|
|
|
|
# Verify stage-level animation header
|
|
assert 'upAxis = "Z"' in usda_text
|
|
assert "startTimeCode = 0.0" in usda_text
|
|
assert "endTimeCode = 240.0" in usda_text # 10s * 24 fps
|
|
assert "timeCodesPerSecond = 24.0" in usda_text
|
|
|
|
# Verify time-sampled kinematic tracks
|
|
assert 'double3 xformOp:translate.timeSamples = {' in usda_text
|
|
assert 'double3 xformOp:rotateXYZ.timeSamples = {' in usda_text
|
|
assert 'color3f[] primvars:displayColor.timeSamples = {' in usda_text
|
|
assert 'token visibility.timeSamples = {' in usda_text
|
|
|
|
# Verify robot prims
|
|
assert 'def Xform "amr_light_01"' in usda_text
|
|
assert 'def Xform "amr_heavy_01"' in usda_text
|
|
assert 'def Cube "StatusDome"' in usda_text
|
|
assert 'def Cube "BatteryLED"' in usda_text
|
|
assert 'def Cube "Payload"' in usda_text
|
|
|
|
# Test file export
|
|
out_file = tmp_path / "warehouse_animated.usda"
|
|
saved = generator.export_usda(out_file)
|
|
assert saved.exists()
|
|
assert saved.stat().st_size > 5000
|