File size: 12,641 Bytes
bd95c9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

"""Tests for PoseInversion.

Tests both ``fit()`` (analytical Kabsch) and ``fit(autograd_iters=...)``
(FK-based gradient optimization) against ground-truth posed vertices
from example_animation.npy.

Pose conventions
~~~~~~~~~~~~~~~~
- example_animation.npy stores local rotations *relative to T-pose*
  (joint orient not applied).  demo_soma_vis.py applies a t-pose
  correction before passing to ``soma.pose(absolute_pose=False)``.

- Both ``fit()`` and ``fit(autograd_iters=...)`` return *absolute*
  local rotations (joint orient already baked in), suitable for
  ``soma.pose(absolute_pose=True)`` or direct LBS via
  ``BatchedSkinning.pose(absolute_pose=True)``.

Requires CUDA and assets/.
"""

from pathlib import Path

import numpy as np
import pytest
import torch

REPO_ROOT = Path(__file__).resolve().parents[1]
ASSETS_DIR = REPO_ROOT / "assets"
MOTION_FILE = ASSETS_DIR / "example_animation.npy"

# 94-joint skeleton to 77-joint mapping (from demo_soma_vis.py)
# fmt: off
_NVSKEL93_NAMES = [
    "Hips", "Spine1", "Spine2", "Chest", "Neck1", "Neck2", "Head", "HeadEnd", "Jaw",
    "LeftEye", "RightEye", "LeftShoulder", "LeftArm", "LeftForeArm", "LeftHand",
    "LeftHandThumb1", "LeftHandThumb2", "LeftHandThumb3", "LeftHandThumbEnd",
    "LeftHandIndex1", "LeftHandIndex2", "LeftHandIndex3", "LeftHandIndex4", "LeftHandIndexEnd",
    "LeftHandMiddle1", "LeftHandMiddle2", "LeftHandMiddle3", "LeftHandMiddle4", "LeftHandMiddleEnd",
    "LeftHandRing1", "LeftHandRing2", "LeftHandRing3", "LeftHandRing4", "LeftHandRingEnd",
    "LeftHandPinky1", "LeftHandPinky2", "LeftHandPinky3", "LeftHandPinky4", "LeftHandPinkyEnd",
    "LeftForeArmTwist1", "LeftForeArmTwist2", "LeftArmTwist1", "LeftArmTwist2",
    "RightShoulder", "RightArm", "RightForeArm", "RightHand",
    "RightHandThumb1", "RightHandThumb2", "RightHandThumb3", "RightHandThumbEnd",
    "RightHandIndex1", "RightHandIndex2", "RightHandIndex3", "RightHandIndex4", "RightHandIndexEnd",
    "RightHandMiddle1", "RightHandMiddle2", "RightHandMiddle3", "RightHandMiddle4", "RightHandMiddleEnd",
    "RightHandRing1", "RightHandRing2", "RightHandRing3", "RightHandRing4", "RightHandRingEnd",
    "RightHandPinky1", "RightHandPinky2", "RightHandPinky3", "RightHandPinky4", "RightHandPinkyEnd",
    "RightForeArmTwist1", "RightForeArmTwist2", "RightArmTwist1", "RightArmTwist2",
    "LeftLeg", "LeftShin", "LeftFoot", "LeftToeBase", "LeftToeEnd",
    "LeftShinTwist1", "LeftShinTwist2", "LeftLegTwist1", "LeftLegTwist2",
    "RightLeg", "RightShin", "RightFoot", "RightToeBase", "RightToeEnd",
    "RightShinTwist1", "RightShinTwist2", "RightLegTwist1", "RightLegTwist2",
]
_NVSKEL77_NAMES = [
    "Hips", "Spine1", "Spine2", "Chest", "Neck1", "Neck2", "Head", "HeadEnd", "Jaw",
    "LeftEye", "RightEye",
    "LeftShoulder", "LeftArm", "LeftForeArm", "LeftHand",
    "LeftHandThumb1", "LeftHandThumb2", "LeftHandThumb3", "LeftHandThumbEnd",
    "LeftHandIndex1", "LeftHandIndex2", "LeftHandIndex3", "LeftHandIndex4", "LeftHandIndexEnd",
    "LeftHandMiddle1", "LeftHandMiddle2", "LeftHandMiddle3", "LeftHandMiddle4", "LeftHandMiddleEnd",
    "LeftHandRing1", "LeftHandRing2", "LeftHandRing3", "LeftHandRing4", "LeftHandRingEnd",
    "LeftHandPinky1", "LeftHandPinky2", "LeftHandPinky3", "LeftHandPinky4", "LeftHandPinkyEnd",
    "RightShoulder", "RightArm", "RightForeArm", "RightHand",
    "RightHandThumb1", "RightHandThumb2", "RightHandThumb3", "RightHandThumbEnd",
    "RightHandIndex1", "RightHandIndex2", "RightHandIndex3", "RightHandIndex4", "RightHandIndexEnd",
    "RightHandMiddle1", "RightHandMiddle2", "RightHandMiddle3", "RightHandMiddle4", "RightHandMiddleEnd",
    "RightHandRing1", "RightHandRing2", "RightHandRing3", "RightHandRing4", "RightHandRingEnd",
    "RightHandPinky1", "RightHandPinky2", "RightHandPinky3", "RightHandPinky4", "RightHandPinkyEnd",
    "LeftLeg", "LeftShin", "LeftFoot", "LeftToeBase", "LeftToeEnd",
    "RightLeg", "RightShin", "RightFoot", "RightToeBase", "RightToeEnd",
]
# fmt: on
_93TO77_IDX = [_NVSKEL93_NAMES.index(n) for n in _NVSKEL77_NAMES]

requires_cuda = pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA not available")


def _load_motion(soma, frames):
    """Load example_animation.npy frames, return ground-truth posed vertices.

    Follows the same pipeline as tools/demo_soma_vis.py:
    1. Remap 94-joint → 78-joint (root + 77)
    2. Apply t-pose correction
    3. Forward pass through soma.pose()

    Returns (posed_vertices, root_translation).
    """
    from soma.geometry.rig_utils import joint_local_to_world, joint_world_to_local

    device = soma.device
    motion_full = torch.from_numpy(np.load(MOTION_FILE)).float().to(device)
    rot_local = motion_full[..., :3, :3]
    root_trans = motion_full[:, 1, :3, 3]

    # Remap 94 → 78 joints (root + 77)
    if rot_local.shape[1] == 94:
        subset_idx = [0] + [i + 1 for i in _93TO77_IDX]
        rot_local = rot_local[:, subset_idx]

    # T-pose correction: animation data is in a different skeleton
    # convention; rotate world transforms to match SOMA's joint orient.
    correction = soma.t_pose_world[:, :3, :3].transpose(-2, -1)
    rot_world = joint_local_to_world(rot_local, soma.joint_parent_ids)
    rot_world = rot_world @ correction
    rot_local = joint_world_to_local(rot_world, soma.joint_parent_ids)

    # Build pose: global_orient (Hips=joint 1) + body (joints 2:)
    global_orient = rot_local[:, 1]
    body_pose = rot_local[:, 2:]
    pose = torch.cat([global_orient.unsqueeze(1), body_pose], dim=1)
    transl = root_trans

    # Select frames
    pose = pose[frames]
    transl = transl[frames]

    # Forward pass — these rotations are relative to T-pose
    with torch.no_grad():
        out = soma.pose(pose, transl=transl, pose2rot=False, absolute_pose=False)

    return out["vertices"], transl


@pytest.fixture(scope="module")
def soma_and_inv():
    """Create SOMALayer + PoseInversion, prepare mean-shape identity."""
    if not torch.cuda.is_available():
        pytest.skip("CUDA not available")
    if not ASSETS_DIR.is_dir():
        pytest.fail(f"Assets directory not found: {ASSETS_DIR}")
    if not MOTION_FILE.is_file():
        pytest.fail(f"Motion file not found: {MOTION_FILE}")

    from soma.pose_inversion import PoseInversion
    from soma.soma import SOMALayer

    device = "cuda"
    soma = SOMALayer(
        data_root=str(ASSETS_DIR),
        identity_model_type="soma",
        device=device,
        mode="warp",
        low_lod=True,
    )

    # Prepare mean shape
    n_id = soma.identity_model.num_identity_coeffs
    identity_coeffs = torch.zeros(1, n_id, device=device)
    soma.prepare_identity(identity_coeffs)

    inv = PoseInversion(soma, low_lod=True)
    inv.prepare_identity(identity_coeffs)

    return soma, inv


@requires_cuda
class TestInvert:
    """Tests for PoseInversion.fit() (analytical Kabsch)."""

    def test_single_frame_roundtrip(self, soma_and_inv):
        """Single frame: fit recovers pose with low error."""
        soma, inv = soma_and_inv
        verts, _ = _load_motion(soma, frames=[0])

        result = inv.fit(verts, body_iters=10, finger_iters=2, full_iters=1)

        J = result["rotations"].shape[1]  # 78 (root + 77 joints)
        assert result["rotations"].shape == (1, J, 3, 3)
        assert result["root_translation"].shape == (1, 3)
        assert result["per_vertex_error"].shape[0] == 1

        mean_err = result["per_vertex_error"].mean().item()
        max_err = result["per_vertex_error"].max().item()
        assert mean_err < 0.01, f"Mean vertex error too high: {mean_err:.6f} m"
        assert max_err < 0.05, f"Max vertex error too high: {max_err:.6f} m"

    def test_batch_roundtrip(self, soma_and_inv):
        """Multiple diverse frames: consistent low error across batch."""
        soma, inv = soma_and_inv
        verts, _ = _load_motion(soma, frames=[0, 100, 300, 600])

        result = inv.fit(verts, body_iters=10, finger_iters=2, full_iters=1)

        J = result["rotations"].shape[1]
        assert result["rotations"].shape == (4, J, 3, 3)
        assert result["per_vertex_error"].shape[0] == 4

        mean_err = result["per_vertex_error"].mean().item()
        assert mean_err < 0.01, f"Mean vertex error too high: {mean_err:.6f} m"

    def test_roundtrip_forward_pass(self, soma_and_inv):
        """Verify inverted rotations reproduce vertices via soma.pose().

        fit returns absolute local rotations for 78 joints
        (root + 77).  Strip the root (index 0) and pass to
        soma.pose(absolute_pose=True) to reconstruct.
        """
        soma, inv = soma_and_inv
        verts_gt, _ = _load_motion(soma, frames=[50, 200])

        result = inv.fit(verts_gt, body_iters=10, finger_iters=2, full_iters=1)

        # Strip root joint (index 0) — soma.pose() expects 77 joints
        rotations_no_root = result["rotations"][:, 1:]
        # fit uses raw LBS without correctives, so disable
        # correctives in the forward pass for a fair comparison.
        with torch.no_grad():
            out = soma.pose(
                rotations_no_root,
                transl=result["root_translation"],
                pose2rot=False,
                absolute_pose=True,
                apply_correctives=False,
            )
        verts_recon = out["vertices"]

        err = torch.norm(verts_recon - verts_gt, dim=-1)
        mean_err = err.mean().item()
        # Slightly higher threshold than internal per_vertex_error because
        # soma.pose() uses full skinning weights while fit
        # uses sparse top-K weights internally.
        assert mean_err < 0.02, f"Forward-pass roundtrip error too high: {mean_err:.6f} m"

    def test_batch_size_chunking(self, soma_and_inv):
        """batch_size parameter produces comparable results to all-at-once."""
        soma, inv = soma_and_inv
        verts, _ = _load_motion(soma, frames=[0, 50, 100, 150])

        result_all = inv.fit(verts, body_iters=5, finger_iters=2)
        result_chunked = inv.fit(verts, body_iters=5, finger_iters=2, batch_size=2)

        assert result_chunked["rotations"].shape == result_all["rotations"].shape

        # Analytical is deterministic, so results should be very close
        err_all = result_all["per_vertex_error"].mean().item()
        err_chunked = result_chunked["per_vertex_error"].mean().item()
        assert abs(err_all - err_chunked) < 0.005, (
            f"Chunked vs all-at-once error mismatch: {err_all:.6f} vs {err_chunked:.6f}"
        )

    def test_identity_pose_near_zero_error(self, soma_and_inv):
        """Rest pose (identity rotations) should fit with near-zero error."""
        soma, inv = soma_and_inv
        device = soma.device

        J = 77
        rot_mats = torch.eye(3, device=device).expand(1, J, 3, 3).clone()

        transl = torch.zeros(1, 3, device=device)
        with torch.no_grad():
            out = soma.pose(rot_mats, transl=transl, pose2rot=False)
        verts = out["vertices"]

        result = inv.fit(verts, body_iters=5, finger_iters=2, full_iters=1)

        mean_err = result["per_vertex_error"].mean().item()
        assert mean_err < 0.02, f"Identity pose error too high: {mean_err:.6f} m"


@requires_cuda
class TestInvertAutogradFK:
    """Tests for PoseInversion.fit(autograd_iters=...)."""

    def test_single_frame_roundtrip(self, soma_and_inv):
        """Single frame: fit(autograd_iters) recovers pose with low error."""
        soma, inv = soma_and_inv
        verts, _ = _load_motion(soma, frames=[0])

        result = inv.fit(verts, body_iters=0, full_iters=0, autograd_iters=20, autograd_lr=5e-3)

        J = result["rotations"].shape[1]
        assert result["rotations"].shape == (1, J, 3, 3)
        assert result["root_translation"].shape == (1, 3)
        assert result["per_vertex_error"].shape[0] == 1

        mean_err = result["per_vertex_error"].mean().item()
        assert mean_err < 0.01, f"Mean vertex error too high: {mean_err:.6f} m"

    def test_batch_roundtrip(self, soma_and_inv):
        """Multiple diverse frames: consistent low error across batch."""
        soma, inv = soma_and_inv
        verts, _ = _load_motion(soma, frames=[0, 100, 300, 600])

        result = inv.fit(verts, body_iters=0, full_iters=0, autograd_iters=20, autograd_lr=5e-3)

        assert result["rotations"].shape[0] == 4
        mean_err = result["per_vertex_error"].mean().item()
        assert mean_err < 0.01, f"Mean vertex error too high: {mean_err:.6f} m"