File size: 3,204 Bytes
d4159be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Decode portable_preview's SKQ1 posed hand cache, without changing its source."""
import gzip
import json
from pathlib import Path
import numpy as np


def vertex_normals(vertices, faces):
    """Area-weighted smooth normals, matching THREE.BufferGeometry.computeVertexNormals."""
    triangles = vertices[faces]
    face_normals = np.cross(triangles[:, 1] - triangles[:, 0], triangles[:, 2] - triangles[:, 0])
    normals = np.zeros_like(vertices)
    for corner in range(3):
        np.add.at(normals, faces[:, corner], face_normals)
    lengths = np.linalg.norm(normals, axis=1, keepdims=True)
    return normals / np.maximum(lengths, 1e-20)


class Reader:
    def __init__(self, path):
        self.data = gzip.decompress(Path(path).read_bytes())
        self.offset = 0

    def array(self, dtype, count):
        result = np.frombuffer(self.data, dtype=dtype, count=count, offset=self.offset)
        self.offset += result.nbytes
        return result

    def uint(self):
        return int(self.array('<u4', 1)[0])

    def done(self):
        assert self.offset == len(self.data), (self.offset, len(self.data))


def load(cache, count):
    cache = Path(cache)
    meta = json.loads((cache / 'index.json').read_text())
    assert meta['format'] == 'SKQ1' and meta['frames'] == count
    reader = Reader(cache / 'topology.bin.gz')
    assert reader.uint() == 827607891
    topology = {}
    for side in ('left', 'right'):
        vertices, triangles = reader.uint(), reader.uint()
        faces = reader.array('<u4', triangles * 3).copy().reshape(-1, 3)
        assert faces.max() < vertices
        topology[side] = (vertices, faces)
    reader.done()
    frames = []
    for segment in range(meta['segments']):
        reader = Reader(cache / f'segment-{segment:04d}.bin.gz')
        assert reader.uint() == 827411283
        n = reader.uint()
        batch = [dict(left=None, right=None) for _ in range(n)]
        for side in ('left', 'right'):
            vertices, faces = topology[side]
            assert reader.uint() == vertices
            base = reader.array('<f4', 3).astype(np.float64)
            scale = reader.array('<f4', 3).astype(np.float64)
            valid = []
            for frame in batch:
                flags = reader.uint()
                if flags:
                    wrist = reader.array('<f4', 3).copy()
                    transform = reader.array('<f4', 16).copy().reshape(4, 4)
                    record = dict(wrist=wrist, transform=transform if flags & 2 else None, faces=faces)
                    frame[side] = record
                    valid.append(record)
            size = len(valid) * vertices * 3
            low = reader.array('u1', size).astype(np.uint16)
            high = reader.array('u1', size).astype(np.uint16)
            delta = (low + (high << 8)).reshape(len(valid), vertices, 3)
            values = np.cumsum(delta, axis=0, dtype=np.uint32).astype(np.uint16)
            positions = (base + values * scale).astype(np.float32)
            for record, points in zip(valid, positions):
                record['vertices'] = points
        reader.done()
        frames.extend(batch)
    assert len(frames) == count
    return frames