File size: 11,879 Bytes
3a78f46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
"""
Convert HumanML3D data (SMPL-based .npy format) into our unified representation.

HumanML3D stores motions as:
- new_joints/XXXXXX.npy: [T, 22, 3] joint positions (SMPL 22-joint skeleton)
- new_joint_vecs/XXXXXX.npy: [T, 263] rotation-invariant features
- texts/XXXXXX.txt: text descriptions (multiple per motion)

We convert to:
- SkeletonGraph (fixed SMPL-22 topology)
- Motion dict with positions, velocities, and text annotations
"""

import numpy as np
from pathlib import Path
from typing import Optional

from .skeleton_graph import SkeletonGraph


# SMPL 22-joint skeleton definition
SMPL_22_JOINT_NAMES = [
    'pelvis',           # 0
    'left_hip',         # 1
    'right_hip',        # 2
    'spine1',           # 3
    'left_knee',        # 4
    'right_knee',       # 5
    'spine2',           # 6
    'left_ankle',       # 7
    'right_ankle',      # 8
    'spine3',           # 9
    'left_foot',        # 10
    'right_foot',       # 11
    'neck',             # 12
    'left_collar',      # 13
    'right_collar',     # 14
    'head',             # 15
    'left_shoulder',    # 16
    'right_shoulder',   # 17
    'left_elbow',       # 18
    'right_elbow',      # 19
    'left_wrist',       # 20
    'right_wrist',      # 21
]

SMPL_22_PARENTS = [
    -1,  # 0 pelvis (root)
    0,   # 1 left_hip -> pelvis
    0,   # 2 right_hip -> pelvis
    0,   # 3 spine1 -> pelvis
    1,   # 4 left_knee -> left_hip
    2,   # 5 right_knee -> right_hip
    3,   # 6 spine2 -> spine1
    4,   # 7 left_ankle -> left_knee
    5,   # 8 right_ankle -> right_knee
    6,   # 9 spine3 -> spine2
    7,   # 10 left_foot -> left_ankle
    8,   # 11 right_foot -> right_ankle
    9,   # 12 neck -> spine3
    9,   # 13 left_collar -> spine3
    9,   # 14 right_collar -> spine3
    12,  # 15 head -> neck
    13,  # 16 left_shoulder -> left_collar
    14,  # 17 right_shoulder -> right_collar
    16,  # 18 left_elbow -> left_shoulder
    17,  # 19 right_elbow -> right_shoulder
    18,  # 20 left_wrist -> left_elbow
    19,  # 21 right_wrist -> right_elbow
]


def get_smpl22_skeleton(rest_pose: Optional[np.ndarray] = None) -> SkeletonGraph:
    """
    Get the SMPL 22-joint skeleton graph.

    Args:
        rest_pose: [22, 3] rest-pose joint positions. If None, uses default T-pose offsets.

    Returns:
        SkeletonGraph for SMPL-22.
    """
    if rest_pose is None:
        # Default T-pose offsets (approximate, from HumanML3D average)
        rest_pose = np.array([
            [0.0, 0.0, 0.0],       # pelvis
            [0.08, -0.05, 0.0],     # left_hip
            [-0.08, -0.05, 0.0],    # right_hip
            [0.0, 0.1, 0.0],        # spine1
            [0.0, -0.4, 0.0],       # left_knee
            [0.0, -0.4, 0.0],       # right_knee
            [0.0, 0.15, 0.0],       # spine2
            [0.0, -0.4, 0.0],       # left_ankle
            [0.0, -0.4, 0.0],       # right_ankle
            [0.0, 0.15, 0.0],       # spine3
            [0.0, -0.05, 0.1],      # left_foot
            [0.0, -0.05, 0.1],      # right_foot
            [0.0, 0.12, 0.0],       # neck
            [0.05, 0.0, 0.0],       # left_collar
            [-0.05, 0.0, 0.0],      # right_collar
            [0.0, 0.12, 0.0],       # head
            [0.15, 0.0, 0.0],       # left_shoulder
            [-0.15, 0.0, 0.0],      # right_shoulder
            [0.25, 0.0, 0.0],       # left_elbow
            [-0.25, 0.0, 0.0],      # right_elbow
            [0.25, 0.0, 0.0],       # left_wrist
            [-0.25, 0.0, 0.0],      # right_wrist
        ], dtype=np.float32)

    # Compute offsets from parent
    offsets = np.zeros_like(rest_pose)
    for j in range(len(SMPL_22_PARENTS)):
        p = SMPL_22_PARENTS[j]
        if p >= 0:
            offsets[j] = rest_pose[j] - rest_pose[p]
        else:
            offsets[j] = rest_pose[j]

    return SkeletonGraph(
        joint_names=SMPL_22_JOINT_NAMES,
        parent_indices=SMPL_22_PARENTS,
        rest_offsets=offsets,
    )


def load_humanml3d_motion(
    motion_id: str,
    data_dir: str | Path,
) -> dict:
    """
    Load a single HumanML3D motion sample.

    Args:
        motion_id: e.g., '000000'
        data_dir: path to HumanML3D directory

    Returns:
        dict with keys:
        - 'joint_positions': [T, 22, 3] global joint positions
        - 'joint_vecs': [T, 263] rotation-invariant features (if available)
        - 'texts': list of text descriptions
        - 'motion_id': str
    """
    data_dir = Path(data_dir)

    # Load joint positions
    joints_path = data_dir / 'new_joints' / f'{motion_id}.npy'
    joint_positions = np.load(joints_path)  # [T, 22, 3]

    # Load joint vectors (rotation-invariant features) if available
    vecs_path = data_dir / 'new_joint_vecs' / f'{motion_id}.npy'
    joint_vecs = None
    if vecs_path.exists():
        joint_vecs = np.load(vecs_path)  # [T, 263]

    # Load text descriptions
    text_path = data_dir / 'texts' / f'{motion_id}.txt'
    texts = []
    if text_path.exists():
        with open(text_path, 'r') as f:
            for line in f:
                line = line.strip()
                if line:
                    # Format: "text#token1 token2#start#end"
                    parts = line.split('#')
                    if parts:
                        texts.append(parts[0].strip())

    return {
        'joint_positions': joint_positions.astype(np.float32),
        'joint_vecs': joint_vecs,
        'texts': texts,
        'motion_id': motion_id,
    }


def compute_motion_features(
    joint_positions: np.ndarray,
    skeleton: SkeletonGraph,
    fps: float = 20.0,
) -> dict:
    """
    Compute motion features from joint positions for TopoSlots (Scheme C).

    Scheme C:
      - Slot tokens: per-joint [local_pos(3) + velocity(3)] = 6D (cross-skeleton compatible)
      - Decoder GT: per-joint rotations via FK supervision (skeleton-specific)
      - Root trajectory: separate track
      - Foot contact: auxiliary loss

    Args:
        joint_positions: [T, J, 3] global joint positions
        skeleton: SkeletonGraph
        fps: frames per second

    Returns:
        dict with:
        - 'root_position': [T, 3]
        - 'root_velocity': [T, 3]
        - 'local_positions': [T, J, 3] root-relative joint positions
        - 'velocities': [T, J, 3] joint velocities
        - 'accelerations': [T, J, 3] joint accelerations
        - 'bone_lengths': [T, J] per-frame bone lengths
        - 'foot_contact': [T, 4] 4-channel (l_heel, l_toe, r_heel, r_toe)
    """
    T, J, _ = joint_positions.shape

    # Root position (joint 0)
    root_pos = joint_positions[:, 0, :]  # [T, 3]

    # Local positions (relative to root)
    local_pos = joint_positions - root_pos[:, None, :]  # [T, J, 3]

    # Velocities (finite difference)
    vel = np.zeros_like(joint_positions)
    vel[1:] = (joint_positions[1:] - joint_positions[:-1]) * fps
    vel[0] = vel[1]

    root_vel = vel[:, 0, :]  # [T, 3]

    # Accelerations (finite difference of velocity)
    acc = np.zeros_like(vel)
    acc[1:] = (vel[1:] - vel[:-1]) * fps
    acc[0] = acc[1]

    # Bone lengths per frame
    bone_lengths = np.zeros((T, J), dtype=np.float32)
    for j in range(J):
        p = skeleton.parent_indices[j]
        if p >= 0:
            bone_lengths[:, j] = np.linalg.norm(
                joint_positions[:, j] - joint_positions[:, p], axis=-1
            )

    # Foot contact: 4-channel detection via velocity + height
    foot_contact = _detect_foot_contact(joint_positions, vel, skeleton)

    return {
        'root_position': root_pos,
        'root_velocity': root_vel,
        'local_positions': local_pos,
        'velocities': vel,
        'accelerations': acc,
        'bone_lengths': bone_lengths,
        'foot_contact': foot_contact,
    }


def _detect_foot_contact(
    positions: np.ndarray,
    velocities: np.ndarray,
    skeleton: SkeletonGraph,
    vel_thresh: float = None,
) -> np.ndarray:
    """
    Detect 4-channel foot contact: [l_heel, l_toe, r_heel, r_toe].

    Auto-adapts thresholds based on data scale (meters vs centimeters).
    """
    T = positions.shape[0]
    foot_contact = np.zeros((T, 4), dtype=np.float32)

    # Auto-detect scale for thresholds
    body_height = positions[0, :, 1].max() - positions[0, :, 1].min()
    if body_height < 0.01:
        return foot_contact  # degenerate
    # Velocity threshold proportional to body height
    # ~0.5 m/s for 1.7m human → 0.3 * body_height
    if vel_thresh is None:
        vel_thresh = 0.3 * body_height
    height_margin = 0.03 * body_height  # ~5cm for 1.7m human

    names_lower = [n.lower() for n in skeleton.joint_names]

    # Find foot-related joints with broader matching
    joint_map = {
        'l_heel': None, 'l_toe': None,
        'r_heel': None, 'r_toe': None,
    }
    for j, n in enumerate(names_lower):
        is_left = 'left' in n or n.startswith('l_') or n.startswith('l ') or 'leftfoot' in n.replace(' ', '')
        is_right = 'right' in n or n.startswith('r_') or n.startswith('r ') or 'rightfoot' in n.replace(' ', '')
        is_ankle = 'ankle' in n or 'heel' in n
        is_foot = 'foot' in n or 'toe' in n

        if is_left and is_ankle and joint_map['l_heel'] is None:
            joint_map['l_heel'] = j
        elif is_left and is_foot and joint_map['l_toe'] is None:
            joint_map['l_toe'] = j
        elif is_right and is_ankle and joint_map['r_heel'] is None:
            joint_map['r_heel'] = j
        elif is_right and is_foot and joint_map['r_toe'] is None:
            joint_map['r_toe'] = j

    channels = ['l_heel', 'l_toe', 'r_heel', 'r_toe']
    for ch_idx, ch_name in enumerate(channels):
        jidx = joint_map[ch_name]
        if jidx is None:
            continue
        jvel = np.linalg.norm(velocities[:, jidx, :], axis=-1)
        jheight = positions[:, jidx, 1]
        height_thresh = np.percentile(jheight, 10) + height_margin
        foot_contact[:, ch_idx] = (
            (jvel < vel_thresh) & (jheight < height_thresh)
        ).astype(np.float32)

    return foot_contact


def extract_rotations_from_263d(joint_vecs: np.ndarray) -> dict:
    """
    Extract structured features from HumanML3D 263D vector.

    Layout (22-joint SMPL):
        [0:1]    root angular velocity (y-axis)
        [1:3]    root linear velocity (xz)
        [3:4]    root height (y)
        [4:67]   joint positions relative to root  (21 × 3 = 63)
        [67:193]  joint 6D continuous rotations      (21 × 6 = 126)
        [193:259] joint velocities                   (22 × 3 = 66)
        [259:263] foot contact (4 channels)

    Returns:
        dict with:
        - 'root_angular_vel': [T, 1]
        - 'root_linear_vel': [T, 2]
        - 'root_height': [T, 1]
        - 'ric_positions': [T, 21, 3]
        - 'local_rotations_6d': [T, 21, 6]
        - 'joint_velocities': [T, 22, 3]
        - 'foot_contact_4ch': [T, 4]
    """
    T = joint_vecs.shape[0]
    return {
        'root_angular_vel': joint_vecs[:, 0:1],
        'root_linear_vel': joint_vecs[:, 1:3],
        'root_height': joint_vecs[:, 3:4],
        'ric_positions': joint_vecs[:, 4:67].reshape(T, 21, 3),
        'local_rotations_6d': joint_vecs[:, 67:193].reshape(T, 21, 6),
        'joint_velocities': joint_vecs[:, 193:259].reshape(T, 22, 3),
        'foot_contact_4ch': joint_vecs[:, 259:263],
    }


def load_humanml3d_split(
    data_dir: str | Path,
    split: str = 'train',
) -> list[str]:
    """Load motion IDs for a data split."""
    data_dir = Path(data_dir)
    split_file = data_dir / f'{split}.txt'

    if not split_file.exists():
        raise FileNotFoundError(f"Split file not found: {split_file}")

    motion_ids = []
    with open(split_file, 'r') as f:
        for line in f:
            line = line.strip()
            if line:
                motion_ids.append(line)

    return motion_ids