File size: 9,214 Bytes
f499d3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from dataclasses import dataclass
import numpy as np
from numpy import ndarray

import os
from typing import Union, List, Tuple

from .exporter import Exporter

from ..tokenizer.spec import DetokenzeOutput
from .order import Order

@dataclass(frozen=True)
class RawData(Exporter):
    '''
    Dataclass to handle data from processed model files.
    '''
    
    # vertices of the mesh, shape (N, 3), float32
    vertices: Union[ndarray, None]
    
    # normals of vertices, shape (N, 3), float32
    vertex_normals: Union[ndarray, None]
    
    # faces of mesh, shape (F, 3), face id starts from 0 to F-1, int64
    faces: Union[ndarray, None]
    
    # face normal of mesh, shape (F, 3), float32
    face_normals: Union[ndarray, None]
    
    # joints of bones, shape (J, 3), float32
    joints: Union[ndarray, None]
    
    # tails of joints, shape (J, 3), float32
    tails: Union[ndarray, None]
    
    # skinning of joints, shape (N, J), float32
    skin: Union[ndarray, None]
    
    # whether the joint has skin, bool
    no_skin: Union[ndarray, None]
    
    # parents of joints, None represents no parent(a root joint)
    # make sure parent[k] < k
    parents: Union[List[Union[int, None]], None]
    
    # names of joints
    names: Union[List[str], None]
    
    # local coordinate
    matrix_local: Union[ndarray, None]
    
    # path to data
    path: Union[str, None]=None
    
    # data cls
    cls: Union[str, None]=None
    
    @staticmethod
    def load(path: str) -> 'RawData':
        data = np.load(path, allow_pickle=True)
        d = {name: data[name][()] for name in data}
        d['path'] = path
        return RawData(**d)
    
    def save(self, path: str):
        os.makedirs(os.path.dirname(path), exist_ok=True)
        np.savez(file=path, **self.__dict__)
    
    @property
    def N(self):
        '''
        number of vertices
        '''
        return self.vertices.shape[0]
    
    @property
    def F(self):
        '''
        number of faces
        '''
        return self.faces.shape[0]
    
    @property
    def J(self):
        '''
        number of joints
        '''
        return self.joints.shape[0]
    
    def check(self):
        if self.names is not None and self.joints is not None:
            assert len(self.names) == self.J
        if self.names is not None and self.parents is not None:
            assert len(self.names) == len(self.parents)
        if self.parents is not None:
            for (i, pid) in enumerate(self.parents):
                if i==0:
                    assert pid is None
                else:
                    assert pid is not None
                    assert pid < i
    
    def export_pc(self, path: str, with_normal: bool=True, normal_size=0.01):
        '''
        export point cloud
        '''
        if with_normal:
            self._export_pc(vertices=self.vertices, path=path, vertex_normals=self.vertex_normals, normal_size=normal_size)
        else:
            self._export_pc(vertices=self.vertices, path=path, vertex_normals=None, normal_size=normal_size)
    
    def export_mesh(self, path: str):
        '''
        export mesh
        '''
        self._export_mesh(vertices=self.vertices, faces=self.faces, path=path)
    
    def export_skeleton(self, path: str):
        '''
        export spring
        '''
        self._export_skeleton(joints=self.joints, parents=self.parents, path=path)
    
    def export_skeleton_sequence(self, path: str):
        '''
        export spring
        '''
        self._export_skeleton_sequence(joints=self.joints, parents=self.parents, path=path)
    
    def export_fbx(
        self,
        path: str,
        extrude_size: float=0.03,
        group_per_vertex: int=-1,
        add_root: bool=False,
        do_not_normalize: bool=False,
        use_extrude_bone: bool=True,
        use_connect_unique_child: bool=True,
        extrude_from_parent: bool=True,
        use_tail: bool=False,
        custom_vertex_group: Union[ndarray, None]=None,
    ):
        '''
        export the whole model with skining
        '''
        self._export_fbx(
            path=path,
            vertices=self.vertices,
            joints=self.joints,
            skin=self.skin if custom_vertex_group is None else custom_vertex_group,
            parents=self.parents,
            names=self.names,
            faces=self.faces,
            extrude_size=extrude_size,
            group_per_vertex=group_per_vertex,
            add_root=add_root,
            do_not_normalize=do_not_normalize,
            use_extrude_bone=use_extrude_bone,
            use_connect_unique_child=use_connect_unique_child,
            extrude_from_parent=extrude_from_parent,
            tails=self.tails if use_tail else None,
        )
    
    def export_render(self, path: str, resolution: Tuple[int, int]=[256, 256]):
        self._export_render(
            path=path,
            vertices=self.vertices,
            faces=self.faces,
            bones=np.concatenate([self.joints, self.tails], axis=-1),
            resolution=resolution,
        )

@dataclass(frozen=True)
class RawSkeleton(Exporter):
    '''
    Dataclass to handle skeleton from AR.
    '''
    # joints of bones, shape (J, 3), float32
    joints: Union[ndarray, None]
    
    # tails of joints, shape (J, 3), float32
    tails: Union[ndarray, None]
    
    # whether the joint has skin, bool
    no_skin: Union[ndarray, None]
    
    # parents of joints, None represents no parent(a root joint)
    # make sure parent[k] < k
    parents: Union[List[Union[int, None]], None]
    
    # names of joints
    names: Union[List[str], None]
    
    @staticmethod
    def load(path: str) -> 'RawSkeleton':
        data = np.load(path, allow_pickle=True)
        return RawSkeleton(**{name: data[name][()] for name in data})
    
    def save(self, path: str):
        os.makedirs(os.path.dirname(path), exist_ok=True)
        np.savez(file=path, **self.__dict__)
    
    @staticmethod
    def from_detokenize_output(res: DetokenzeOutput, order: Union[Order, None]) -> 'RawSkeleton':
        J = len(res.bones)
        names = order.make_names(cls=res.cls, parts=res.parts, num_bones=J)
        joints = res.joints
        p_joints = res.p_joints
        parents = []
        for (i, joint) in enumerate(joints):
            if i == 0:
                parents.append(None)
                continue
            p_joint = p_joints[i]
            dis = 999999
            pid = None
            for j in reversed(range(i)):
                n_dis = ((joints[j] - p_joint)**2).sum()
                if n_dis < dis:
                    pid = j
                    dis = n_dis
            parents.append(pid)
        return RawSkeleton(
            joints=joints,
            tails=res.tails,
            no_skin=res.no_skin,
            parents=parents,
            names=names,
        )
        
    def export_skeleton(self, path: str):
        '''
        export spring
        '''
        self._export_skeleton(joints=self.joints, parents=self.parents, path=path)
    
    def export_skeleton_sequence(self, path: str):
        '''
        export spring
        '''
        self._export_skeleton_sequence(joints=self.joints, parents=self.parents, path=path)
    
    def export_fbx(
        self,
        path: str,
        extrude_size: float=0.03,
        group_per_vertex: int=-1,
        add_root: bool=False,
        do_not_normalize: bool=False,
        use_extrude_bone: bool=True,
        use_connect_unique_child: bool=True,
        extrude_from_parent: bool=True,
        use_tail: bool=False,
    ):
        '''
        export the whole model with skining
        '''
        self._export_fbx(
            path=path,
            vertices=None,
            joints=self.joints,
            skin=None,
            parents=self.parents,
            names=self.names,
            faces=None,
            extrude_size=extrude_size,
            group_per_vertex=group_per_vertex,
            add_root=add_root,
            do_not_normalize=do_not_normalize,
            use_extrude_bone=use_extrude_bone,
            use_connect_unique_child=use_connect_unique_child,
            extrude_from_parent=extrude_from_parent,
            tails=self.tails if use_tail else None,
        )
    
    def export_render(self, path: str, resolution: Tuple[int, int]=[256, 256]):
        self._export_render(
            path=path,
            vertices=None,
            faces=None,
            bones=np.concatenate([self.joints, self.tails], axis=-1),
            resolution=resolution,
        )

@dataclass
class RawSkin(Exporter):
    '''
    Dataclass to handle skeleton from AR.
    '''
    # skin, shape (J, N)
    skin: ndarray
    
    # always sampled, shape (N, 3)
    vertices: Union[ndarray, None]=None
    
    # for future use, shape (J, 3)
    joints: Union[ndarray, None]=None
    
    @staticmethod
    def load(path: str) -> 'RawSkin':
        data = np.load(path, allow_pickle=True)
        return RawSkin(**{name: data[name][()] for name in data})
    
    def save(self, path: str):
        os.makedirs(os.path.dirname(path), exist_ok=True)
        np.savez(file=path, **self.__dict__)