| |
| |
| |
| |
|
|
| """Launch Isaac Sim Simulator first.""" |
|
|
| from isaaclab.app import AppLauncher |
|
|
| |
| simulation_app = AppLauncher(headless=True).app |
|
|
| """Rest everything follows.""" |
|
|
| import math |
|
|
| import numpy as np |
| import pytest |
| import torch |
|
|
| from pxr import Gf, Sdf, Usd, UsdGeom |
|
|
| import isaaclab.sim as sim_utils |
| import isaaclab.utils.math as math_utils |
|
|
|
|
| @pytest.fixture(autouse=True) |
| def test_setup_teardown(): |
| """Create a blank new stage for each test.""" |
| |
| sim_utils.create_new_stage() |
| sim_utils.update_stage() |
|
|
| |
| yield |
|
|
| |
| sim_utils.clear_stage() |
|
|
|
|
| def assert_vec3_close(v1: Gf.Vec3d | Gf.Vec3f, v2: tuple | Gf.Vec3d | Gf.Vec3f, eps: float = 1e-6): |
| """Assert two 3D vectors are close.""" |
| if isinstance(v2, tuple): |
| v2 = Gf.Vec3d(*v2) |
| for i in range(3): |
| assert math.isclose(v1[i], v2[i], abs_tol=eps), f"Vector mismatch at index {i}: {v1[i]} != {v2[i]}" |
|
|
|
|
| def assert_quat_close(q1: Gf.Quatf | Gf.Quatd, q2: Gf.Quatf | Gf.Quatd | tuple, eps: float = 1e-6): |
| """Assert two quaternions are close, accounting for double-cover (q and -q represent same rotation).""" |
| if isinstance(q2, tuple): |
| q2 = Gf.Quatd(*q2) |
| |
| real_match = math.isclose(q1.GetReal(), q2.GetReal(), abs_tol=eps) |
| imag_match = all(math.isclose(q1.GetImaginary()[i], q2.GetImaginary()[i], abs_tol=eps) for i in range(3)) |
|
|
| real_match_neg = math.isclose(q1.GetReal(), -q2.GetReal(), abs_tol=eps) |
| imag_match_neg = all(math.isclose(q1.GetImaginary()[i], -q2.GetImaginary()[i], abs_tol=eps) for i in range(3)) |
|
|
| assert (real_match and imag_match) or (real_match_neg and imag_match_neg), ( |
| f"Quaternion mismatch: {q1} != {q2} (and not equal to negative either)" |
| ) |
|
|
|
|
| def get_xform_ops(prim: Usd.Prim) -> list[str]: |
| """Get the ordered list of xform operation names for a prim.""" |
| xformable = UsdGeom.Xformable(prim) |
| return [op.GetOpName() for op in xformable.GetOrderedXformOps()] |
|
|
|
|
| """ |
| Test standardize_xform_ops() function. |
| """ |
|
|
|
|
| def test_standardize_xform_ops_basic(): |
| """Test basic functionality of standardize_xform_ops on a simple prim.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| prim = sim_utils.create_prim( |
| "/World/TestXform", |
| "Xform", |
| translation=(1.0, 2.0, 3.0), |
| orientation=(1.0, 0.0, 0.0, 0.0), |
| scale=(1.0, 1.0, 1.0), |
| stage=stage, |
| ) |
|
|
| |
| result = sim_utils.standardize_xform_ops(prim) |
|
|
| |
| assert result is True |
| assert prim.IsValid() |
|
|
| |
| xform_ops = get_xform_ops(prim) |
| assert xform_ops == [ |
| "xformOp:translate", |
| "xformOp:orient", |
| "xformOp:scale", |
| ], f"Expected standard xform order, got {xform_ops}" |
|
|
| |
| assert_vec3_close(prim.GetAttribute("xformOp:translate").Get(), (1.0, 2.0, 3.0)) |
| assert_quat_close(prim.GetAttribute("xformOp:orient").Get(), (1.0, 0.0, 0.0, 0.0)) |
| assert_vec3_close(prim.GetAttribute("xformOp:scale").Get(), (1.0, 1.0, 1.0)) |
|
|
|
|
| def test_standardize_xform_ops_with_rotation_xyz(): |
| """Test standardize_xform_ops removes deprecated rotateXYZ operations.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| prim_path = "/World/TestRotateXYZ" |
| prim = stage.DefinePrim(prim_path, "Xform") |
| xformable = UsdGeom.Xformable(prim) |
| |
| rotate_xyz_op = xformable.AddRotateXYZOp(UsdGeom.XformOp.PrecisionDouble) |
| rotate_xyz_op.Set(Gf.Vec3d(45.0, 30.0, 60.0)) |
| |
| translate_op = xformable.AddTranslateOp(UsdGeom.XformOp.PrecisionDouble) |
| translate_op.Set(Gf.Vec3d(1.0, 2.0, 3.0)) |
|
|
| |
| assert "xformOp:rotateXYZ" in prim.GetPropertyNames() |
|
|
| |
| pos_before, quat_before = sim_utils.resolve_prim_pose(prim) |
|
|
| |
| result = sim_utils.standardize_xform_ops(prim) |
| assert result is True |
|
|
| |
| pos_after, quat_after = sim_utils.resolve_prim_pose(prim) |
| |
| assert_vec3_close(Gf.Vec3d(*pos_before), pos_after, eps=1e-4) |
| assert_quat_close(Gf.Quatd(*quat_before), quat_after, eps=1e-4) |
|
|
| |
| assert "xformOp:rotateXYZ" not in prim.GetPropertyNames() |
| |
| assert "xformOp:translate" in prim.GetPropertyNames() |
| assert "xformOp:orient" in prim.GetPropertyNames() |
| assert "xformOp:scale" in prim.GetPropertyNames() |
| |
| xform_ops = get_xform_ops(prim) |
| assert xform_ops == ["xformOp:translate", "xformOp:orient", "xformOp:scale"] |
|
|
|
|
| def test_standardize_xform_ops_with_transform_matrix(): |
| """Test standardize_xform_ops removes transform matrix operations.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| prim_path = "/World/TestTransformMatrix" |
| prim = stage.DefinePrim(prim_path, "Xform") |
| xformable = UsdGeom.Xformable(prim) |
|
|
| |
| transform_op = xformable.AddTransformOp(UsdGeom.XformOp.PrecisionDouble) |
| |
| matrix = Gf.Matrix4d().SetTranslate(Gf.Vec3d(5.0, 10.0, 15.0)) |
| transform_op.Set(matrix) |
|
|
| |
| assert "xformOp:transform" in prim.GetPropertyNames() |
|
|
| |
| pos_before, quat_before = sim_utils.resolve_prim_pose(prim) |
|
|
| |
| result = sim_utils.standardize_xform_ops(prim) |
| assert result is True |
|
|
| |
| pos_after, quat_after = sim_utils.resolve_prim_pose(prim) |
| |
| assert_vec3_close(Gf.Vec3d(*pos_before), pos_after, eps=1e-5) |
| assert_quat_close(Gf.Quatd(*quat_before), quat_after, eps=1e-5) |
|
|
| |
| assert "xformOp:transform" not in prim.GetPropertyNames() |
| |
| assert "xformOp:translate" in prim.GetPropertyNames() |
| assert "xformOp:orient" in prim.GetPropertyNames() |
| assert "xformOp:scale" in prim.GetPropertyNames() |
|
|
|
|
| def test_standardize_xform_ops_preserves_world_pose(): |
| """Test that standardize_xform_ops preserves the world-space pose of the prim.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| translation = (10.0, 20.0, 30.0) |
| |
| orientation = (0.7071068, 0.0, 0.0, 0.7071068) |
| scale = (2.0, 3.0, 4.0) |
|
|
| prim = sim_utils.create_prim( |
| "/World/TestPreservePose", |
| "Xform", |
| translation=translation, |
| orientation=orientation, |
| scale=scale, |
| stage=stage, |
| ) |
|
|
| |
| pos_before, quat_before = sim_utils.resolve_prim_pose(prim) |
|
|
| |
| result = sim_utils.standardize_xform_ops(prim) |
| assert result is True |
|
|
| |
| pos_after, quat_after = sim_utils.resolve_prim_pose(prim) |
| |
| assert_vec3_close(Gf.Vec3d(*pos_before), pos_after, eps=1e-5) |
| assert_quat_close(Gf.Quatd(*quat_before), quat_after, eps=1e-5) |
|
|
|
|
| def test_standardize_xform_ops_with_units_resolve(): |
| """Test standardize_xform_ops handles scale:unitsResolve attribute.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| prim_path = "/World/TestUnitsResolve" |
| prim = stage.DefinePrim(prim_path, "Xform") |
| xformable = UsdGeom.Xformable(prim) |
|
|
| |
| scale_op = xformable.AddScaleOp(UsdGeom.XformOp.PrecisionDouble) |
| scale_op.Set(Gf.Vec3d(1.0, 1.0, 1.0)) |
|
|
| |
| units_resolve_attr = prim.CreateAttribute("xformOp:scale:unitsResolve", Sdf.ValueTypeNames.Double3) |
| units_resolve_attr.Set(Gf.Vec3d(100.0, 100.0, 100.0)) |
|
|
| |
| assert "xformOp:scale" in prim.GetPropertyNames() |
| assert "xformOp:scale:unitsResolve" in prim.GetPropertyNames() |
|
|
| |
| pos_before, quat_before = sim_utils.resolve_prim_pose(prim) |
|
|
| |
| result = sim_utils.standardize_xform_ops(prim) |
| assert result is True |
|
|
| |
| pos_after, quat_after = sim_utils.resolve_prim_pose(prim) |
| |
| assert_vec3_close(Gf.Vec3d(*pos_before), pos_after, eps=1e-5) |
| assert_quat_close(Gf.Quatd(*quat_before), quat_after, eps=1e-5) |
|
|
| |
| assert "xformOp:scale:unitsResolve" not in prim.GetPropertyNames() |
|
|
| |
| scale = prim.GetAttribute("xformOp:scale").Get() |
| assert_vec3_close(scale, (100.0, 100.0, 100.0)) |
|
|
|
|
| def test_standardize_xform_ops_with_hierarchy(): |
| """Test standardize_xform_ops works correctly with prim hierarchies.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| parent_prim = sim_utils.create_prim( |
| "/World/Parent", |
| "Xform", |
| translation=(5.0, 0.0, 0.0), |
| orientation=(1.0, 0.0, 0.0, 0.0), |
| scale=(2.0, 2.0, 2.0), |
| stage=stage, |
| ) |
|
|
| |
| child_prim = sim_utils.create_prim( |
| "/World/Parent/Child", |
| "Xform", |
| translation=(0.0, 3.0, 0.0), |
| orientation=(0.7071068, 0.0, 0.7071068, 0.0), |
| scale=(0.5, 0.5, 0.5), |
| stage=stage, |
| ) |
|
|
| |
| parent_pos_before, parent_quat_before = sim_utils.resolve_prim_pose(parent_prim) |
| child_pos_before, child_quat_before = sim_utils.resolve_prim_pose(child_prim) |
|
|
| |
| sim_utils.standardize_xform_ops(parent_prim) |
| sim_utils.standardize_xform_ops(child_prim) |
|
|
| |
| parent_pos_after, parent_quat_after = sim_utils.resolve_prim_pose(parent_prim) |
| child_pos_after, child_quat_after = sim_utils.resolve_prim_pose(child_prim) |
|
|
| |
| assert_vec3_close(Gf.Vec3d(*parent_pos_before), parent_pos_after, eps=1e-5) |
| assert_quat_close(Gf.Quatd(*parent_quat_before), parent_quat_after, eps=1e-5) |
| assert_vec3_close(Gf.Vec3d(*child_pos_before), child_pos_after, eps=1e-5) |
| assert_quat_close(Gf.Quatd(*child_quat_before), child_quat_after, eps=1e-5) |
|
|
|
|
| def test_standardize_xform_ops_multiple_deprecated_ops(): |
| """Test standardize_xform_ops removes multiple deprecated operations.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| prim_path = "/World/TestMultipleDeprecated" |
| prim = stage.DefinePrim(prim_path, "Xform") |
| xformable = UsdGeom.Xformable(prim) |
|
|
| |
| rotate_x_op = xformable.AddRotateXOp(UsdGeom.XformOp.PrecisionDouble) |
| rotate_x_op.Set(45.0) |
| rotate_y_op = xformable.AddRotateYOp(UsdGeom.XformOp.PrecisionDouble) |
| rotate_y_op.Set(30.0) |
| rotate_z_op = xformable.AddRotateZOp(UsdGeom.XformOp.PrecisionDouble) |
| rotate_z_op.Set(60.0) |
|
|
| |
| assert "xformOp:rotateX" in prim.GetPropertyNames() |
| assert "xformOp:rotateY" in prim.GetPropertyNames() |
| assert "xformOp:rotateZ" in prim.GetPropertyNames() |
|
|
| |
| pos, quat = sim_utils.resolve_prim_pose(prim) |
|
|
| |
| sim_utils.standardize_xform_ops(prim) |
|
|
| |
| pos_after, quat_after = sim_utils.resolve_prim_pose(prim) |
| |
| assert_vec3_close(Gf.Vec3d(*pos), Gf.Vec3d(*pos_after), eps=1e-5) |
| assert_quat_close(Gf.Quatd(*quat), Gf.Quatd(*quat_after), eps=1e-5) |
|
|
| |
| assert "xformOp:rotateX" not in prim.GetPropertyNames() |
| assert "xformOp:rotateY" not in prim.GetPropertyNames() |
| assert "xformOp:rotateZ" not in prim.GetPropertyNames() |
| |
| xform_ops = get_xform_ops(prim) |
| assert xform_ops == ["xformOp:translate", "xformOp:orient", "xformOp:scale"] |
|
|
|
|
| def test_standardize_xform_ops_with_existing_standard_ops(): |
| """Test standardize_xform_ops when prim already has standard operations.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| prim = sim_utils.create_prim( |
| "/World/TestExistingStandard", |
| "Xform", |
| translation=(7.0, 8.0, 9.0), |
| orientation=(0.9238795, 0.3826834, 0.0, 0.0), |
| scale=(1.5, 2.5, 3.5), |
| stage=stage, |
| ) |
|
|
| |
| initial_translate = prim.GetAttribute("xformOp:translate").Get() |
| initial_orient = prim.GetAttribute("xformOp:orient").Get() |
| initial_scale = prim.GetAttribute("xformOp:scale").Get() |
|
|
| |
| pos_before, quat_before = sim_utils.resolve_prim_pose(prim) |
|
|
| |
| result = sim_utils.standardize_xform_ops(prim) |
| assert result is True |
|
|
| |
| pos_after, quat_after = sim_utils.resolve_prim_pose(prim) |
| |
| assert_vec3_close(Gf.Vec3d(*pos_before), pos_after, eps=1e-5) |
| assert_quat_close(Gf.Quatd(*quat_before), quat_after, eps=1e-5) |
|
|
| |
| xform_ops = get_xform_ops(prim) |
| assert xform_ops == ["xformOp:translate", "xformOp:orient", "xformOp:scale"] |
|
|
| |
| final_translate = prim.GetAttribute("xformOp:translate").Get() |
| final_orient = prim.GetAttribute("xformOp:orient").Get() |
| final_scale = prim.GetAttribute("xformOp:scale").Get() |
|
|
| assert_vec3_close(initial_translate, final_translate, eps=1e-5) |
| assert_quat_close(initial_orient, final_orient, eps=1e-5) |
| assert_vec3_close(initial_scale, final_scale, eps=1e-5) |
|
|
|
|
| def test_standardize_xform_ops_invalid_prim(): |
| """Test standardize_xform_ops raises error for invalid prim.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| invalid_prim = stage.GetPrimAtPath("/World/NonExistent") |
|
|
| |
| assert not invalid_prim.IsValid() |
|
|
| |
| with pytest.raises(ValueError, match="not valid"): |
| sim_utils.standardize_xform_ops(invalid_prim) |
|
|
|
|
| def test_standardize_xform_ops_on_geometry_prim(): |
| """Test standardize_xform_ops on a geometry prim (Cube, Sphere, etc.).""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| cube_prim = sim_utils.create_prim( |
| "/World/TestCube", |
| "Cube", |
| translation=(1.0, 2.0, 3.0), |
| orientation=(1.0, 0.0, 0.0, 0.0), |
| scale=(2.0, 2.0, 2.0), |
| attributes={"size": 1.0}, |
| stage=stage, |
| ) |
|
|
| |
| pos_before, quat_before = sim_utils.resolve_prim_pose(cube_prim) |
|
|
| |
| sim_utils.standardize_xform_ops(cube_prim) |
|
|
| |
| pos_after, quat_after = sim_utils.resolve_prim_pose(cube_prim) |
| |
| assert_vec3_close(Gf.Vec3d(*pos_before), pos_after, eps=1e-5) |
| assert_quat_close(Gf.Quatd(*quat_before), quat_after, eps=1e-5) |
|
|
| |
| xform_ops = get_xform_ops(cube_prim) |
| assert xform_ops == ["xformOp:translate", "xformOp:orient", "xformOp:scale"] |
|
|
|
|
| def test_standardize_xform_ops_with_non_uniform_scale(): |
| """Test standardize_xform_ops with non-uniform scale.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| prim = sim_utils.create_prim( |
| "/World/TestNonUniformScale", |
| "Xform", |
| translation=(5.0, 10.0, 15.0), |
| orientation=(0.7071068, 0.0, 0.7071068, 0.0), |
| scale=(1.0, 2.0, 3.0), |
| stage=stage, |
| ) |
|
|
| |
| initial_scale = prim.GetAttribute("xformOp:scale").Get() |
|
|
| |
| pos_before, quat_before = sim_utils.resolve_prim_pose(prim) |
|
|
| |
| result = sim_utils.standardize_xform_ops(prim) |
| assert result is True |
|
|
| |
| pos_after, quat_after = sim_utils.resolve_prim_pose(prim) |
| |
| assert_vec3_close(Gf.Vec3d(*pos_before), pos_after, eps=1e-5) |
| assert_quat_close(Gf.Quatd(*quat_before), quat_after, eps=1e-5) |
| |
| final_scale = prim.GetAttribute("xformOp:scale").Get() |
| assert_vec3_close(initial_scale, final_scale, eps=1e-5) |
|
|
|
|
| def test_standardize_xform_ops_identity_transform(): |
| """Test standardize_xform_ops with identity transform (no translation, rotation, or scale).""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| prim = sim_utils.create_prim( |
| "/World/TestIdentity", |
| "Xform", |
| translation=(0.0, 0.0, 0.0), |
| orientation=(1.0, 0.0, 0.0, 0.0), |
| scale=(1.0, 1.0, 1.0), |
| stage=stage, |
| ) |
|
|
| |
| sim_utils.standardize_xform_ops(prim) |
|
|
| |
| xform_ops = get_xform_ops(prim) |
| assert xform_ops == ["xformOp:translate", "xformOp:orient", "xformOp:scale"] |
|
|
| |
| assert_vec3_close(prim.GetAttribute("xformOp:translate").Get(), (0.0, 0.0, 0.0)) |
| assert_quat_close(prim.GetAttribute("xformOp:orient").Get(), (1.0, 0.0, 0.0, 0.0)) |
| assert_vec3_close(prim.GetAttribute("xformOp:scale").Get(), (1.0, 1.0, 1.0)) |
|
|
|
|
| def test_standardize_xform_ops_with_explicit_values(): |
| """Test standardize_xform_ops with explicit translation, orientation, and scale values.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| prim = sim_utils.create_prim( |
| "/World/TestExplicitValues", |
| "Xform", |
| translation=(10.0, 10.0, 10.0), |
| orientation=(0.7071068, 0.7071068, 0.0, 0.0), |
| scale=(5.0, 5.0, 5.0), |
| stage=stage, |
| ) |
|
|
| |
| new_translation = (1.0, 2.0, 3.0) |
| new_orientation = (1.0, 0.0, 0.0, 0.0) |
| new_scale = (2.0, 2.0, 2.0) |
|
|
| result = sim_utils.standardize_xform_ops( |
| prim, translation=new_translation, orientation=new_orientation, scale=new_scale |
| ) |
| assert result is True |
|
|
| |
| assert_vec3_close(prim.GetAttribute("xformOp:translate").Get(), new_translation) |
| assert_quat_close(prim.GetAttribute("xformOp:orient").Get(), new_orientation) |
| assert_vec3_close(prim.GetAttribute("xformOp:scale").Get(), new_scale) |
|
|
| |
| pos_after, quat_after = sim_utils.resolve_prim_pose(prim) |
| assert_vec3_close(Gf.Vec3d(*pos_after), new_translation, eps=1e-5) |
| assert_quat_close(Gf.Quatd(*quat_after), new_orientation, eps=1e-5) |
|
|
| |
| xform_ops = get_xform_ops(prim) |
| assert xform_ops == ["xformOp:translate", "xformOp:orient", "xformOp:scale"] |
|
|
|
|
| def test_standardize_xform_ops_with_partial_values(): |
| """Test standardize_xform_ops with only some values specified.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| prim = sim_utils.create_prim( |
| "/World/TestPartialValues", |
| "Xform", |
| translation=(1.0, 2.0, 3.0), |
| orientation=(0.9238795, 0.3826834, 0.0, 0.0), |
| scale=(2.0, 2.0, 2.0), |
| stage=stage, |
| ) |
|
|
| |
| pos_before, quat_before = sim_utils.resolve_prim_pose(prim, ref_prim=prim.GetParent()) |
| scale_before = prim.GetAttribute("xformOp:scale").Get() |
|
|
| |
| new_translation = (10.0, 20.0, 30.0) |
| result = sim_utils.standardize_xform_ops(prim, translation=new_translation) |
| assert result is True |
|
|
| |
| assert_vec3_close(prim.GetAttribute("xformOp:translate").Get(), new_translation) |
|
|
| |
| quat_after = prim.GetAttribute("xformOp:orient").Get() |
| scale_after = prim.GetAttribute("xformOp:scale").Get() |
| assert_quat_close(Gf.Quatd(*quat_before), quat_after, eps=1e-5) |
| assert_vec3_close(scale_before, scale_after, eps=1e-5) |
|
|
| |
| _, quat_after_world = sim_utils.resolve_prim_pose(prim) |
| assert_quat_close(Gf.Quatd(*quat_before), quat_after_world, eps=1e-5) |
|
|
|
|
| def test_standardize_xform_ops_non_xformable_prim(caplog): |
| """Test standardize_xform_ops returns False for non-Xformable prims and logs error.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| from pxr import UsdShade |
|
|
| material_prim = UsdShade.Material.Define(stage, "/World/TestMaterial").GetPrim() |
|
|
| |
| assert material_prim.IsValid() |
| assert not material_prim.IsA(UsdGeom.Xformable) |
|
|
| |
| caplog.clear() |
|
|
| |
| with caplog.at_level("ERROR"): |
| result = sim_utils.standardize_xform_ops(material_prim) |
|
|
| assert result is False |
|
|
| |
| assert len(caplog.records) == 1 |
| assert caplog.records[0].levelname == "ERROR" |
| assert "not an Xformable" in caplog.records[0].message |
| assert "/World/TestMaterial" in caplog.records[0].message |
|
|
|
|
| def test_standardize_xform_ops_preserves_reset_xform_stack(): |
| """Test that standardize_xform_ops preserves the resetXformStack attribute.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| prim = sim_utils.create_prim("/World/TestResetStack", "Xform", stage=stage) |
| xformable = UsdGeom.Xformable(prim) |
|
|
| |
| xformable.SetResetXformStack(True) |
| assert xformable.GetResetXformStack() is True |
|
|
| |
| result = sim_utils.standardize_xform_ops(prim) |
| assert result is True |
|
|
| |
| assert xformable.GetResetXformStack() is True |
|
|
|
|
| def test_standardize_xform_ops_with_complex_hierarchy(): |
| """Test standardize_xform_ops on deeply nested hierarchy.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| root = sim_utils.create_prim("/World/Root", "Xform", translation=(1.0, 0.0, 0.0), stage=stage) |
| child1 = sim_utils.create_prim("/World/Root/Child1", "Xform", translation=(0.0, 1.0, 0.0), stage=stage) |
| child2 = sim_utils.create_prim("/World/Root/Child1/Child2", "Xform", translation=(0.0, 0.0, 1.0), stage=stage) |
| child3 = sim_utils.create_prim("/World/Root/Child1/Child2/Child3", "Cube", translation=(1.0, 1.0, 1.0), stage=stage) |
|
|
| |
| poses_before = {} |
| for name, prim in [("root", root), ("child1", child1), ("child2", child2), ("child3", child3)]: |
| poses_before[name] = sim_utils.resolve_prim_pose(prim) |
|
|
| |
| assert sim_utils.standardize_xform_ops(root) is True |
| assert sim_utils.standardize_xform_ops(child1) is True |
| assert sim_utils.standardize_xform_ops(child2) is True |
| assert sim_utils.standardize_xform_ops(child3) is True |
|
|
| |
| poses_after = {} |
| for name, prim in [("root", root), ("child1", child1), ("child2", child2), ("child3", child3)]: |
| poses_after[name] = sim_utils.resolve_prim_pose(prim) |
|
|
| |
| for name in poses_before: |
| pos_before, quat_before = poses_before[name] |
| pos_after, quat_after = poses_after[name] |
| assert_vec3_close(Gf.Vec3d(*pos_before), pos_after, eps=1e-5) |
| assert_quat_close(Gf.Quatd(*quat_before), quat_after, eps=1e-5) |
|
|
|
|
| def test_standardize_xform_ops_preserves_float_precision(): |
| """Test that standardize_xform_ops preserves float precision when it already exists.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| prim_path = "/World/TestFloatPrecision" |
| prim = stage.DefinePrim(prim_path, "Xform") |
| xformable = UsdGeom.Xformable(prim) |
|
|
| |
| translate_op = xformable.AddTranslateOp(UsdGeom.XformOp.PrecisionFloat) |
| translate_op.Set(Gf.Vec3f(1.0, 2.0, 3.0)) |
|
|
| orient_op = xformable.AddOrientOp(UsdGeom.XformOp.PrecisionFloat) |
| orient_op.Set(Gf.Quatf(1.0, 0.0, 0.0, 0.0)) |
|
|
| scale_op = xformable.AddScaleOp(UsdGeom.XformOp.PrecisionFloat) |
| scale_op.Set(Gf.Vec3f(1.0, 1.0, 1.0)) |
|
|
| |
| assert translate_op.GetPrecision() == UsdGeom.XformOp.PrecisionFloat |
| assert orient_op.GetPrecision() == UsdGeom.XformOp.PrecisionFloat |
| assert scale_op.GetPrecision() == UsdGeom.XformOp.PrecisionFloat |
|
|
| |
| new_translation = (5.0, 10.0, 15.0) |
| new_orientation = (0.7071068, 0.7071068, 0.0, 0.0) |
| new_scale = (2.0, 3.0, 4.0) |
|
|
| result = sim_utils.standardize_xform_ops( |
| prim, translation=new_translation, orientation=new_orientation, scale=new_scale |
| ) |
| assert result is True |
|
|
| |
| translate_op_after = UsdGeom.XformOp(prim.GetAttribute("xformOp:translate")) |
| orient_op_after = UsdGeom.XformOp(prim.GetAttribute("xformOp:orient")) |
| scale_op_after = UsdGeom.XformOp(prim.GetAttribute("xformOp:scale")) |
|
|
| assert translate_op_after.GetPrecision() == UsdGeom.XformOp.PrecisionFloat |
| assert orient_op_after.GetPrecision() == UsdGeom.XformOp.PrecisionFloat |
| assert scale_op_after.GetPrecision() == UsdGeom.XformOp.PrecisionFloat |
|
|
| |
| translate_value = prim.GetAttribute("xformOp:translate").Get() |
| assert isinstance(translate_value, Gf.Vec3f), f"Expected Gf.Vec3f, got {type(translate_value)}" |
| assert_vec3_close(translate_value, new_translation, eps=1e-5) |
|
|
| orient_value = prim.GetAttribute("xformOp:orient").Get() |
| assert isinstance(orient_value, Gf.Quatf), f"Expected Gf.Quatf, got {type(orient_value)}" |
| assert_quat_close(orient_value, new_orientation, eps=1e-5) |
|
|
| scale_value = prim.GetAttribute("xformOp:scale").Get() |
| assert isinstance(scale_value, Gf.Vec3f), f"Expected Gf.Vec3f, got {type(scale_value)}" |
| assert_vec3_close(scale_value, new_scale, eps=1e-5) |
|
|
| |
| pos_after, quat_after = sim_utils.resolve_prim_pose(prim) |
| assert_vec3_close(Gf.Vec3d(*pos_after), new_translation, eps=1e-4) |
| assert_quat_close(Gf.Quatd(*quat_after), new_orientation, eps=1e-4) |
|
|
|
|
| """ |
| Test validate_standard_xform_ops() function. |
| """ |
|
|
|
|
| def test_validate_standard_xform_ops_valid(): |
| """Test validate_standard_xform_ops returns True for standardized prims.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| prim = sim_utils.create_prim( |
| "/World/TestValid", |
| "Xform", |
| translation=(1.0, 2.0, 3.0), |
| orientation=(1.0, 0.0, 0.0, 0.0), |
| scale=(1.0, 1.0, 1.0), |
| stage=stage, |
| ) |
|
|
| |
| sim_utils.standardize_xform_ops(prim) |
|
|
| |
| assert sim_utils.validate_standard_xform_ops(prim) is True |
|
|
|
|
| def test_validate_standard_xform_ops_invalid_order(): |
| """Test validate_standard_xform_ops returns False for non-standard operation order.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| prim_path = "/World/TestInvalidOrder" |
| prim = stage.DefinePrim(prim_path, "Xform") |
| xformable = UsdGeom.Xformable(prim) |
|
|
| |
| scale_op = xformable.AddScaleOp(UsdGeom.XformOp.PrecisionDouble) |
| scale_op.Set(Gf.Vec3d(1.0, 1.0, 1.0)) |
|
|
| translate_op = xformable.AddTranslateOp(UsdGeom.XformOp.PrecisionDouble) |
| translate_op.Set(Gf.Vec3d(1.0, 2.0, 3.0)) |
|
|
| orient_op = xformable.AddOrientOp(UsdGeom.XformOp.PrecisionDouble) |
| orient_op.Set(Gf.Quatd(1.0, 0.0, 0.0, 0.0)) |
|
|
| |
| assert sim_utils.validate_standard_xform_ops(prim) is False |
|
|
|
|
| def test_validate_standard_xform_ops_with_deprecated_ops(): |
| """Test validate_standard_xform_ops returns False when deprecated operations exist.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| prim_path = "/World/TestDeprecated" |
| prim = stage.DefinePrim(prim_path, "Xform") |
| xformable = UsdGeom.Xformable(prim) |
|
|
| |
| rotate_xyz_op = xformable.AddRotateXYZOp(UsdGeom.XformOp.PrecisionDouble) |
| rotate_xyz_op.Set(Gf.Vec3d(45.0, 30.0, 60.0)) |
|
|
| |
| assert sim_utils.validate_standard_xform_ops(prim) is False |
|
|
|
|
| def test_validate_standard_xform_ops_missing_operations(): |
| """Test validate_standard_xform_ops returns False when standard operations are missing.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| prim_path = "/World/TestMissing" |
| prim = stage.DefinePrim(prim_path, "Xform") |
| xformable = UsdGeom.Xformable(prim) |
|
|
| translate_op = xformable.AddTranslateOp(UsdGeom.XformOp.PrecisionDouble) |
| translate_op.Set(Gf.Vec3d(1.0, 2.0, 3.0)) |
|
|
| |
| assert sim_utils.validate_standard_xform_ops(prim) is False |
|
|
|
|
| def test_validate_standard_xform_ops_invalid_prim(): |
| """Test validate_standard_xform_ops returns False for invalid prim.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| invalid_prim = stage.GetPrimAtPath("/World/NonExistent") |
|
|
| |
| assert sim_utils.validate_standard_xform_ops(invalid_prim) is False |
|
|
|
|
| def test_validate_standard_xform_ops_non_xformable(): |
| """Test validate_standard_xform_ops returns False for non-Xformable prims.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| from pxr import UsdShade |
|
|
| material_prim = UsdShade.Material.Define(stage, "/World/TestMaterial").GetPrim() |
|
|
| |
| assert sim_utils.validate_standard_xform_ops(material_prim) is False |
|
|
|
|
| def test_validate_standard_xform_ops_with_transform_matrix(): |
| """Test validate_standard_xform_ops returns False when transform matrix operation exists.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| prim_path = "/World/TestTransformMatrix" |
| prim = stage.DefinePrim(prim_path, "Xform") |
| xformable = UsdGeom.Xformable(prim) |
|
|
| |
| transform_op = xformable.AddTransformOp(UsdGeom.XformOp.PrecisionDouble) |
| matrix = Gf.Matrix4d().SetTranslate(Gf.Vec3d(5.0, 10.0, 15.0)) |
| transform_op.Set(matrix) |
|
|
| |
| assert sim_utils.validate_standard_xform_ops(prim) is False |
|
|
|
|
| def test_validate_standard_xform_ops_extra_operations(): |
| """Test validate_standard_xform_ops returns False when extra operations exist.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| prim = sim_utils.create_prim( |
| "/World/TestExtra", |
| "Xform", |
| translation=(1.0, 2.0, 3.0), |
| orientation=(1.0, 0.0, 0.0, 0.0), |
| scale=(1.0, 1.0, 1.0), |
| stage=stage, |
| ) |
|
|
| |
| sim_utils.standardize_xform_ops(prim) |
|
|
| |
| xformable = UsdGeom.Xformable(prim) |
| extra_op = xformable.AddRotateXOp(UsdGeom.XformOp.PrecisionDouble) |
| extra_op.Set(45.0) |
|
|
| |
| assert sim_utils.validate_standard_xform_ops(prim) is False |
|
|
|
|
| def test_validate_standard_xform_ops_after_standardization(): |
| """Test validate_standard_xform_ops returns True after standardization of non-standard prim.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| prim_path = "/World/TestBeforeAfter" |
| prim = stage.DefinePrim(prim_path, "Xform") |
| xformable = UsdGeom.Xformable(prim) |
|
|
| |
| rotate_x_op = xformable.AddRotateXOp(UsdGeom.XformOp.PrecisionDouble) |
| rotate_x_op.Set(45.0) |
| translate_op = xformable.AddTranslateOp(UsdGeom.XformOp.PrecisionDouble) |
| translate_op.Set(Gf.Vec3d(1.0, 2.0, 3.0)) |
|
|
| |
| assert sim_utils.validate_standard_xform_ops(prim) is False |
|
|
| |
| sim_utils.standardize_xform_ops(prim) |
|
|
| |
| assert sim_utils.validate_standard_xform_ops(prim) is True |
|
|
|
|
| def test_validate_standard_xform_ops_on_geometry(): |
| """Test validate_standard_xform_ops works correctly on geometry prims.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| cube_prim = sim_utils.create_prim( |
| "/World/TestCube", |
| "Cube", |
| translation=(1.0, 2.0, 3.0), |
| orientation=(1.0, 0.0, 0.0, 0.0), |
| scale=(2.0, 2.0, 2.0), |
| stage=stage, |
| ) |
|
|
| |
| sim_utils.standardize_xform_ops(cube_prim) |
|
|
| |
| assert sim_utils.validate_standard_xform_ops(cube_prim) is True |
|
|
|
|
| def test_validate_standard_xform_ops_empty_prim(): |
| """Test validate_standard_xform_ops on prim with no xform operations.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| prim_path = "/World/TestEmpty" |
| prim = stage.DefinePrim(prim_path, "Xform") |
|
|
| |
| assert sim_utils.validate_standard_xform_ops(prim) is False |
|
|
|
|
| """ |
| Test resolve_prim_pose() function. |
| """ |
|
|
|
|
| def test_resolve_prim_pose(): |
| """Test resolve_prim_pose() function.""" |
| |
| num_objects = 20 |
| |
| rand_scales = np.random.uniform(0.5, 1.5, size=(num_objects, 3, 3)) |
| rand_widths = np.random.uniform(0.1, 10.0, size=(num_objects,)) |
| |
| rand_positions = np.random.uniform(-100, 100, size=(num_objects, 3, 3)) |
| |
| rand_quats = np.random.randn(num_objects, 3, 4) |
| rand_quats /= np.linalg.norm(rand_quats, axis=2, keepdims=True) |
|
|
| |
| for i in range(num_objects): |
| |
| cube_prim = sim_utils.create_prim( |
| f"/World/Cubes/instance_{i:02d}", |
| "Cube", |
| translation=rand_positions[i, 0], |
| orientation=rand_quats[i, 0], |
| scale=rand_scales[i, 0], |
| attributes={"size": rand_widths[i]}, |
| ) |
| |
| xform_prim = sim_utils.create_prim( |
| f"/World/Xform/instance_{i:02d}", |
| "Xform", |
| translation=rand_positions[i, 1], |
| orientation=rand_quats[i, 1], |
| scale=rand_scales[i, 1], |
| ) |
| geometry_prim = sim_utils.create_prim( |
| f"/World/Xform/instance_{i:02d}/geometry", |
| "Sphere", |
| translation=rand_positions[i, 2], |
| orientation=rand_quats[i, 2], |
| scale=rand_scales[i, 2], |
| attributes={"radius": rand_widths[i]}, |
| ) |
| dummy_prim = sim_utils.create_prim( |
| f"/World/Xform/instance_{i:02d}/dummy", |
| "Sphere", |
| ) |
|
|
| |
| pos, quat = sim_utils.resolve_prim_pose(cube_prim) |
| pos, quat = np.array(pos), np.array(quat) |
| quat = quat if np.sign(rand_quats[i, 0, 0]) == np.sign(quat[0]) else -quat |
| np.testing.assert_allclose(pos, rand_positions[i, 0], atol=1e-3) |
| np.testing.assert_allclose(quat, rand_quats[i, 0], atol=1e-3) |
| |
| pos, quat = sim_utils.resolve_prim_pose(xform_prim) |
| pos, quat = np.array(pos), np.array(quat) |
| quat = quat if np.sign(rand_quats[i, 1, 0]) == np.sign(quat[0]) else -quat |
| np.testing.assert_allclose(pos, rand_positions[i, 1], atol=1e-3) |
| np.testing.assert_allclose(quat, rand_quats[i, 1], atol=1e-3) |
| |
| pos, quat = sim_utils.resolve_prim_pose(dummy_prim) |
| pos, quat = np.array(pos), np.array(quat) |
| quat = quat if np.sign(rand_quats[i, 1, 0]) == np.sign(quat[0]) else -quat |
| np.testing.assert_allclose(pos, rand_positions[i, 1], atol=1e-3) |
| np.testing.assert_allclose(quat, rand_quats[i, 1], atol=1e-3) |
|
|
| |
| pos, quat = sim_utils.resolve_prim_pose(geometry_prim, ref_prim=xform_prim) |
| pos, quat = np.array(pos), np.array(quat) |
| quat = quat if np.sign(rand_quats[i, 2, 0]) == np.sign(quat[0]) else -quat |
| np.testing.assert_allclose(pos, rand_positions[i, 2] * rand_scales[i, 1], atol=1e-3) |
| |
| |
| |
| |
|
|
| |
| pos, quat = sim_utils.resolve_prim_pose(dummy_prim, ref_prim=xform_prim) |
| pos, quat = np.array(pos), np.array(quat) |
| np.testing.assert_allclose(pos, np.zeros(3), atol=1e-3) |
| np.testing.assert_allclose(quat, np.array([1, 0, 0, 0]), atol=1e-3) |
| |
| pos, quat = sim_utils.resolve_prim_pose(xform_prim, ref_prim=cube_prim) |
| pos, quat = np.array(pos), np.array(quat) |
| |
| gt_pos, gt_quat = math_utils.subtract_frame_transforms( |
| torch.from_numpy(rand_positions[i, 0]).unsqueeze(0), |
| torch.from_numpy(rand_quats[i, 0]).unsqueeze(0), |
| torch.from_numpy(rand_positions[i, 1]).unsqueeze(0), |
| torch.from_numpy(rand_quats[i, 1]).unsqueeze(0), |
| ) |
| gt_pos, gt_quat = gt_pos.squeeze(0).numpy(), gt_quat.squeeze(0).numpy() |
| quat = quat if np.sign(gt_quat[0]) == np.sign(quat[0]) else -quat |
| np.testing.assert_allclose(pos, gt_pos, atol=1e-3) |
| np.testing.assert_allclose(quat, gt_quat, atol=1e-3) |
|
|
|
|
| """ |
| Test resolve_prim_scale() function. |
| """ |
|
|
|
|
| def test_resolve_prim_scale(): |
| """Test resolve_prim_scale() function. |
| |
| To simplify the test, we assume that the effective scale at a prim |
| is the product of the scales of the prims in the hierarchy: |
| |
| scale = scale_of_xform * scale_of_geometry_prim |
| |
| This is only true when rotations are identity or the transforms are |
| orthogonal and uniformly scaled. Otherwise, scale is not composable |
| like that in local component-wise fashion. |
| """ |
| |
| num_objects = 20 |
| |
| rand_scales = np.random.uniform(0.5, 1.5, size=(num_objects, 3, 3)) |
| rand_widths = np.random.uniform(0.1, 10.0, size=(num_objects,)) |
| |
| rand_positions = np.random.uniform(-100, 100, size=(num_objects, 3, 3)) |
|
|
| |
| for i in range(num_objects): |
| |
| cube_prim = sim_utils.create_prim( |
| f"/World/Cubes/instance_{i:02d}", |
| "Cube", |
| translation=rand_positions[i, 0], |
| scale=rand_scales[i, 0], |
| attributes={"size": rand_widths[i]}, |
| ) |
| |
| xform_prim = sim_utils.create_prim( |
| f"/World/Xform/instance_{i:02d}", |
| "Xform", |
| translation=rand_positions[i, 1], |
| scale=rand_scales[i, 1], |
| ) |
| geometry_prim = sim_utils.create_prim( |
| f"/World/Xform/instance_{i:02d}/geometry", |
| "Sphere", |
| translation=rand_positions[i, 2], |
| scale=rand_scales[i, 2], |
| attributes={"radius": rand_widths[i]}, |
| ) |
| dummy_prim = sim_utils.create_prim( |
| f"/World/Xform/instance_{i:02d}/dummy", |
| "Sphere", |
| ) |
|
|
| |
| scale = sim_utils.resolve_prim_scale(cube_prim) |
| scale = np.array(scale) |
| np.testing.assert_allclose(scale, rand_scales[i, 0], atol=1e-5) |
| |
| scale = sim_utils.resolve_prim_scale(xform_prim) |
| scale = np.array(scale) |
| np.testing.assert_allclose(scale, rand_scales[i, 1], atol=1e-5) |
| |
| scale = sim_utils.resolve_prim_scale(geometry_prim) |
| scale = np.array(scale) |
| np.testing.assert_allclose(scale, rand_scales[i, 1] * rand_scales[i, 2], atol=1e-5) |
| |
| scale = sim_utils.resolve_prim_scale(dummy_prim) |
| scale = np.array(scale) |
| np.testing.assert_allclose(scale, rand_scales[i, 1], atol=1e-5) |
|
|
|
|
| """ |
| Test convert_world_pose_to_local() function. |
| """ |
|
|
|
|
| def test_convert_world_pose_to_local_basic(): |
| """Test basic world-to-local pose conversion.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| parent_prim = sim_utils.create_prim( |
| "/World/Parent", |
| "Xform", |
| translation=(5.0, 0.0, 0.0), |
| orientation=(1.0, 0.0, 0.0, 0.0), |
| scale=(1.0, 1.0, 1.0), |
| stage=stage, |
| ) |
|
|
| |
| world_position = (10.0, 3.0, 0.0) |
| world_orientation = (1.0, 0.0, 0.0, 0.0) |
|
|
| |
| local_translation, local_orientation = sim_utils.convert_world_pose_to_local( |
| world_position, world_orientation, parent_prim |
| ) |
| |
| assert local_orientation is not None |
|
|
| |
| assert_vec3_close(Gf.Vec3d(*local_translation), (5.0, 3.0, 0.0), eps=1e-5) |
| assert_quat_close(Gf.Quatd(*local_orientation), (1.0, 0.0, 0.0, 0.0), eps=1e-5) |
|
|
|
|
| def test_convert_world_pose_to_local_with_rotation(): |
| """Test world-to-local conversion with parent rotation.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| parent_prim = sim_utils.create_prim( |
| "/World/RotatedParent", |
| "Xform", |
| translation=(0.0, 0.0, 0.0), |
| orientation=(0.7071068, 0.0, 0.0, 0.7071068), |
| scale=(1.0, 1.0, 1.0), |
| stage=stage, |
| ) |
|
|
| |
| world_position = (1.0, 0.0, 0.0) |
| world_orientation = (1.0, 0.0, 0.0, 0.0) |
|
|
| |
| local_translation, local_orientation = sim_utils.convert_world_pose_to_local( |
| world_position, world_orientation, parent_prim |
| ) |
|
|
| |
| child_prim = sim_utils.create_prim( |
| "/World/RotatedParent/Child", |
| "Xform", |
| translation=local_translation, |
| orientation=local_orientation, |
| stage=stage, |
| ) |
|
|
| |
| child_world_pos, child_world_quat = sim_utils.resolve_prim_pose(child_prim) |
|
|
| |
| assert_vec3_close(Gf.Vec3d(*child_world_pos), world_position, eps=1e-5) |
| assert_quat_close(Gf.Quatd(*child_world_quat), world_orientation, eps=1e-5) |
|
|
|
|
| def test_convert_world_pose_to_local_with_scale(): |
| """Test world-to-local conversion with parent scale.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| parent_prim = sim_utils.create_prim( |
| "/World/ScaledParent", |
| "Xform", |
| translation=(1.0, 2.0, 3.0), |
| orientation=(1.0, 0.0, 0.0, 0.0), |
| scale=(2.0, 2.0, 2.0), |
| stage=stage, |
| ) |
|
|
| |
| world_position = (5.0, 6.0, 7.0) |
| world_orientation = (0.7071068, 0.7071068, 0.0, 0.0) |
|
|
| |
| local_translation, local_orientation = sim_utils.convert_world_pose_to_local( |
| world_position, world_orientation, parent_prim |
| ) |
|
|
| |
| child_prim = sim_utils.create_prim( |
| "/World/ScaledParent/Child", |
| "Xform", |
| translation=local_translation, |
| orientation=local_orientation, |
| stage=stage, |
| ) |
|
|
| |
| child_world_pos, child_world_quat = sim_utils.resolve_prim_pose(child_prim) |
|
|
| |
| assert_vec3_close(Gf.Vec3d(*child_world_pos), world_position, eps=1e-4) |
| assert_quat_close(Gf.Quatd(*child_world_quat), world_orientation, eps=1e-4) |
|
|
|
|
| def test_convert_world_pose_to_local_invalid_parent(): |
| """Test world-to-local conversion with invalid parent returns world pose unchanged.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| invalid_prim = stage.GetPrimAtPath("/World/NonExistent") |
| assert not invalid_prim.IsValid() |
|
|
| world_position = (10.0, 20.0, 30.0) |
| world_orientation = (0.7071068, 0.0, 0.7071068, 0.0) |
|
|
| |
| with pytest.raises(ValueError): |
| sim_utils.convert_world_pose_to_local(world_position, world_orientation, invalid_prim) |
|
|
|
|
| def test_convert_world_pose_to_local_root_parent(): |
| """Test world-to-local conversion with root as parent returns world pose unchanged.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| root_prim = stage.GetPrimAtPath("/") |
|
|
| world_position = (15.0, 25.0, 35.0) |
| world_orientation = (0.9238795, 0.3826834, 0.0, 0.0) |
|
|
| |
| local_translation, local_orientation = sim_utils.convert_world_pose_to_local( |
| world_position, world_orientation, root_prim |
| ) |
| |
| assert local_orientation is not None |
|
|
| |
| assert_vec3_close(Gf.Vec3d(*local_translation), world_position, eps=1e-10) |
| assert_quat_close(Gf.Quatd(*local_orientation), world_orientation, eps=1e-10) |
|
|
|
|
| def test_convert_world_pose_to_local_none_orientation(): |
| """Test world-to-local conversion with None orientation.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| parent_prim = sim_utils.create_prim( |
| "/World/ParentNoOrient", |
| "Xform", |
| translation=(3.0, 4.0, 5.0), |
| orientation=(0.7071068, 0.0, 0.0, 0.7071068), |
| stage=stage, |
| ) |
|
|
| world_position = (10.0, 10.0, 10.0) |
|
|
| |
| local_translation, local_orientation = sim_utils.convert_world_pose_to_local(world_position, None, parent_prim) |
|
|
| |
| assert local_orientation is None |
| |
| assert local_translation is not None |
|
|
|
|
| def test_convert_world_pose_to_local_complex_hierarchy(): |
| """Test world-to-local conversion in a complex hierarchy.""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| _ = sim_utils.create_prim( |
| "/World/Grandparent", |
| "Xform", |
| translation=(10.0, 0.0, 0.0), |
| orientation=(0.7071068, 0.0, 0.0, 0.7071068), |
| scale=(2.0, 2.0, 2.0), |
| stage=stage, |
| ) |
|
|
| parent = sim_utils.create_prim( |
| "/World/Grandparent/Parent", |
| "Xform", |
| translation=(5.0, 0.0, 0.0), |
| orientation=(0.7071068, 0.7071068, 0.0, 0.0), |
| scale=(0.5, 0.5, 0.5), |
| stage=stage, |
| ) |
|
|
| |
| world_position = (20.0, 15.0, 10.0) |
| world_orientation = (1.0, 0.0, 0.0, 0.0) |
|
|
| |
| local_translation, local_orientation = sim_utils.convert_world_pose_to_local( |
| world_position, world_orientation, parent |
| ) |
|
|
| |
| child = sim_utils.create_prim( |
| "/World/Grandparent/Parent/Child", |
| "Xform", |
| translation=local_translation, |
| orientation=local_orientation, |
| stage=stage, |
| ) |
|
|
| |
| child_world_pos, child_world_quat = sim_utils.resolve_prim_pose(child) |
|
|
| |
| assert_vec3_close(Gf.Vec3d(*child_world_pos), world_position, eps=1e-4) |
| assert_quat_close(Gf.Quatd(*child_world_quat), world_orientation, eps=1e-4) |
|
|
|
|
| def test_convert_world_pose_to_local_with_mixed_prim_types(): |
| """Test world-to-local conversion with mixed prim types (Xform, Scope, Mesh).""" |
| |
| stage = sim_utils.get_current_stage() |
|
|
| |
| |
| sim_utils.create_prim( |
| "/World/Grandparent", |
| "Xform", |
| translation=(5.0, 3.0, 2.0), |
| orientation=(0.7071068, 0.0, 0.0, 0.7071068), |
| scale=(2.0, 2.0, 2.0), |
| stage=stage, |
| ) |
|
|
| |
| parent = stage.DefinePrim("/World/Grandparent/Parent", "Scope") |
|
|
| |
| parent_pos, parent_quat = sim_utils.resolve_prim_pose(parent) |
| assert_vec3_close(Gf.Vec3d(*parent_pos), (5.0, 3.0, 2.0), eps=1e-5) |
| assert_quat_close(Gf.Quatd(*parent_quat), (0.7071068, 0.0, 0.0, 0.7071068), eps=1e-5) |
|
|
| |
| child = sim_utils.create_prim("/World/Grandparent/Parent/Child", "Mesh", stage=stage) |
|
|
| |
| world_position = (10.0, 5.0, 3.0) |
| world_orientation = (1.0, 0.0, 0.0, 0.0) |
|
|
| |
| local_translation, local_orientation = sim_utils.convert_world_pose_to_local( |
| world_position, world_orientation, child |
| ) |
|
|
| |
| assert local_orientation is not None, "Expected orientation to be computed" |
|
|
| |
| xformable = UsdGeom.Xformable(child) |
| translate_op = xformable.GetTranslateOp() |
| translate_op.Set(Gf.Vec3d(*local_translation)) |
| orient_op = xformable.GetOrientOp() |
| orient_op.Set(Gf.Quatd(*local_orientation)) |
|
|
| |
| child_world_pos, child_world_quat = sim_utils.resolve_prim_pose(child) |
|
|
| |
| |
| |
| assert_vec3_close(Gf.Vec3d(*child_world_pos), world_position, eps=1e-10) |
| assert_quat_close(Gf.Quatd(*child_world_quat), world_orientation, eps=1e-10) |
|
|