feat(phase-7): implement OpenUSD animation, trajectory player, and Omniverse Kit extension
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
"""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
|
||||
@@ -0,0 +1,81 @@
|
||||
"""Unit tests for Omniverse Kit extension structure and lifecycle."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
import pytest
|
||||
try:
|
||||
import tomllib
|
||||
except ImportError:
|
||||
import tomli as tomllib
|
||||
|
||||
from omni.warehouse.twin.visualizer.extension import WarehouseVisualizerExtension
|
||||
|
||||
|
||||
def test_extension_toml_metadata():
|
||||
"""Verify extension.toml adheres to Omniverse Kit 110.4 schema requirements."""
|
||||
toml_path = Path("omniverse/extensions/omni.warehouse.twin.visualizer/config/extension.toml").resolve()
|
||||
assert toml_path.exists()
|
||||
|
||||
with open(toml_path, "rb") as f:
|
||||
config = tomllib.load(f)
|
||||
|
||||
assert "package" in config
|
||||
pkg = config["package"]
|
||||
assert pkg["title"] == "Warehouse Robot Fleet Digital Twin Visualizer"
|
||||
assert pkg["version"] == "0.7.0"
|
||||
assert "python.module" in config or "python" in config
|
||||
mod_name = config["python"]["module"][0]["name"] if "python" in config else config["python.module"][0]["name"]
|
||||
assert mod_name == "omni.warehouse.twin.visualizer"
|
||||
|
||||
|
||||
def test_extension_lifecycle_and_teardown(tmp_path: Path):
|
||||
"""Verify extension instantiation, trajectory loading, playback controls, and safe shutdown."""
|
||||
ext = WarehouseVisualizerExtension()
|
||||
assert not ext.is_active
|
||||
|
||||
# Create dummy trajectory file
|
||||
mock_traj = {
|
||||
"metadata": {"simulation_duration": 60.0},
|
||||
"robots": {
|
||||
"amr_01": [
|
||||
{"t": 0.0, "pos": [5.0, 5.0, 0.0], "yaw_deg": 0.0, "state": "IDLE"},
|
||||
{"t": 60.0, "pos": [25.0, 5.0, 0.0], "yaw_deg": 0.0, "state": "LOADED_TRANSIT"},
|
||||
]
|
||||
},
|
||||
}
|
||||
traj_file = tmp_path / "test_lifecycle_traj.json"
|
||||
traj_file.write_text(json.dumps(mock_traj), encoding="utf-8")
|
||||
|
||||
# Start up extension
|
||||
ext.on_startup("omni.warehouse.twin.visualizer-0.7.0")
|
||||
assert ext.is_active
|
||||
|
||||
# Load trajectory
|
||||
loaded = ext.load_trajectory(str(traj_file))
|
||||
assert loaded
|
||||
assert ext._player is not None
|
||||
assert ext._player.duration == 60.0
|
||||
|
||||
# Test playback controls
|
||||
ext.play()
|
||||
assert ext._player.is_playing
|
||||
|
||||
ext.pause()
|
||||
assert not ext._player.is_playing
|
||||
|
||||
ext.set_speed(5.0)
|
||||
assert ext._player.playback_speed == 5.0
|
||||
|
||||
ext.seek(30.0)
|
||||
assert ext._player.current_time == 30.0
|
||||
|
||||
ext.reset()
|
||||
assert ext._player.current_time == 0.0
|
||||
|
||||
# Test clean shutdown and resource teardown
|
||||
ext.on_shutdown()
|
||||
assert not ext.is_active
|
||||
assert ext._player is None
|
||||
assert ext._update_sub is None
|
||||
@@ -0,0 +1,171 @@
|
||||
"""Unit tests for trajectory playback and spatial interpolation engine."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
import pytest
|
||||
|
||||
from warehouse_twin.visualization.trajectory_player import (
|
||||
RobotPlaybackState,
|
||||
TrajectoryData,
|
||||
TrajectoryPlayer,
|
||||
interpolate_robot_state,
|
||||
interpolate_yaw,
|
||||
)
|
||||
|
||||
|
||||
def test_interpolate_yaw_standard_and_wrap():
|
||||
"""Verify angular interpolation along shortest circular arc."""
|
||||
# Standard interpolation
|
||||
assert pytest.approx(interpolate_yaw(0.0, 90.0, 0.5)) == 45.0
|
||||
assert pytest.approx(interpolate_yaw(10.0, 50.0, 0.25)) == 20.0
|
||||
|
||||
# Wrap-around across 180 / -180 boundary
|
||||
# e.g. from 170 to -170 is a 20 degree positive rotation through 180
|
||||
res = interpolate_yaw(170.0, -170.0, 0.5)
|
||||
assert pytest.approx(abs(res)) == 180.0
|
||||
|
||||
# From -170 to 170 (negative direction through -180)
|
||||
res2 = interpolate_yaw(-170.0, 170.0, 0.5)
|
||||
assert pytest.approx(abs(res2)) == 180.0
|
||||
|
||||
|
||||
def test_interpolate_robot_state_endpoints_and_interior():
|
||||
"""Verify state interpolation at endpoints and midway between trajectory waypoints."""
|
||||
points = [
|
||||
{
|
||||
"t": 0.0,
|
||||
"pos": [10.0, 5.0, 0.2],
|
||||
"yaw_deg": 0.0,
|
||||
"state": "IDLE",
|
||||
"battery_percent": 100.0,
|
||||
"is_loaded": False,
|
||||
},
|
||||
{
|
||||
"t": 10.0,
|
||||
"pos": [20.0, 15.0, 0.2],
|
||||
"yaw_deg": 90.0,
|
||||
"state": "LOADED_TRANSIT",
|
||||
"battery_percent": 98.0,
|
||||
"is_loaded": True,
|
||||
},
|
||||
]
|
||||
|
||||
# Before start
|
||||
s_before = interpolate_robot_state("robot_1", points, -5.0)
|
||||
assert s_before.x == 10.0
|
||||
assert s_before.y == 5.0
|
||||
assert s_before.yaw_degrees == 0.0
|
||||
assert s_before.state == "IDLE"
|
||||
assert s_before.battery_percent == 100.0
|
||||
assert not s_before.is_loaded
|
||||
|
||||
# After end
|
||||
s_after = interpolate_robot_state("robot_1", points, 15.0)
|
||||
assert s_after.x == 20.0
|
||||
assert s_after.y == 15.0
|
||||
assert s_after.yaw_degrees == 90.0
|
||||
assert s_after.state == "LOADED_TRANSIT"
|
||||
assert s_after.battery_percent == 98.0
|
||||
assert s_after.is_loaded
|
||||
|
||||
# Midpoint (t = 5.0)
|
||||
s_mid = interpolate_robot_state("robot_1", points, 5.0)
|
||||
assert pytest.approx(s_mid.x) == 15.0
|
||||
assert pytest.approx(s_mid.y) == 10.0
|
||||
assert pytest.approx(s_mid.yaw_degrees) == 45.0
|
||||
assert pytest.approx(s_mid.battery_percent) == 99.0
|
||||
# At alpha = 0.5, picks p2 discrete state
|
||||
assert s_mid.state == "LOADED_TRANSIT"
|
||||
assert s_mid.is_loaded
|
||||
|
||||
|
||||
def test_trajectory_data_loading(tmp_path: Path):
|
||||
"""Verify TrajectoryData parsing from dict and json file."""
|
||||
mock_payload = {
|
||||
"metadata": {"simulation_duration": 100.0, "coordinate_system": "meters_z_up"},
|
||||
"robots": {
|
||||
"amr_1": [
|
||||
{"t": 0.0, "pos": [0, 0, 0], "yaw_deg": 0, "state": "IDLE"},
|
||||
{"t": 50.0, "pos": [10, 0, 0], "yaw_deg": 0, "state": "DISPATCHED"},
|
||||
],
|
||||
"amr_2": [
|
||||
{"t": 10.0, "pos": [5, 5, 0], "yaw_deg": 90, "state": "PICKING"},
|
||||
{"t": 80.0, "pos": [5, 20, 0], "yaw_deg": 90, "state": "DROPPING"},
|
||||
],
|
||||
},
|
||||
}
|
||||
|
||||
file_path = tmp_path / "test_traj.json"
|
||||
file_path.write_text(json.dumps(mock_payload), encoding="utf-8")
|
||||
|
||||
data = TrajectoryData.from_file(file_path)
|
||||
assert data.get_robot_ids() == ["amr_1", "amr_2"]
|
||||
assert data.get_point_count() == 4
|
||||
assert data.start_time == 0.0
|
||||
assert data.end_time == 80.0
|
||||
assert data.duration == 80.0
|
||||
|
||||
|
||||
def test_trajectory_player_playback_flow():
|
||||
"""Verify player play, pause, seek, speed, advance, and callbacks."""
|
||||
raw_data = {
|
||||
"robots": {
|
||||
"r1": [
|
||||
{"t": 0.0, "pos": [0.0, 0.0, 0.0], "yaw_deg": 0.0, "state": "IDLE"},
|
||||
{"t": 100.0, "pos": [100.0, 0.0, 0.0], "yaw_deg": 0.0, "state": "LOADED_TRANSIT"},
|
||||
]
|
||||
}
|
||||
}
|
||||
data = TrajectoryData.from_dict(raw_data)
|
||||
player = TrajectoryPlayer(data, playback_speed=2.0)
|
||||
|
||||
callback_states = []
|
||||
|
||||
def on_frame(state):
|
||||
callback_states.append(state)
|
||||
|
||||
player.register_callback(on_frame)
|
||||
|
||||
assert not player.is_playing
|
||||
assert player.current_time == 0.0
|
||||
assert player.progress_fraction == 0.0
|
||||
|
||||
# Advance when paused does not change time
|
||||
st = player.advance(1.0)
|
||||
assert player.current_time == 0.0
|
||||
|
||||
# Start playback
|
||||
player.play()
|
||||
assert player.is_playing
|
||||
|
||||
# Advance 5 wall-clock seconds at 2.0x speed = 10 simulation seconds
|
||||
st = player.advance(5.0)
|
||||
assert player.current_time == 10.0
|
||||
assert pytest.approx(st["r1"].x) == 10.0
|
||||
assert len(callback_states) == 1
|
||||
|
||||
# Seek
|
||||
player.seek(50.0)
|
||||
assert player.current_time == 50.0
|
||||
assert player.progress_fraction == 0.5
|
||||
assert len(callback_states) == 2
|
||||
|
||||
# Advance past end without loop
|
||||
player.advance(40.0) # 40 * 2 = 80s -> 50 + 80 = 130 >= 100
|
||||
assert player.current_time == 100.0
|
||||
assert not player.is_playing
|
||||
|
||||
# Test loop wrap-around
|
||||
player.loop = True
|
||||
player.reset()
|
||||
player.play()
|
||||
player.advance(60.0) # 60 * 2 = 120s -> wraps to 20s
|
||||
assert player.is_playing
|
||||
assert pytest.approx(player.current_time) == 20.0
|
||||
|
||||
# Clean teardown
|
||||
player.cleanup()
|
||||
assert not player.is_playing
|
||||
assert len(player._callbacks) == 0
|
||||
Reference in New Issue
Block a user