def calculate_mean_and_std():

    vnum = SET.VERTS_NUM
    case_folder_list = os.listdir(SET.CASE)

    md = [np.load(os.path.join(SET.JOINTS_ANIM_PATH))]
    case_list = [1]

    with open(os.path.join(SET.INFO_BASIS, SET.TRAIN_LIST)) as f:
        content = f.readlines()

    flist_train = [x.strip().split(' ') for x in content]

    random.shuffle(flist_train)

    uid = 4000

    verts = np.zeros((uid, vnum, 3))

    for fid in tqdm(range(uid)):
        fr = flist_train[fid]
        frid = int(fr[2])
        case_id = int(fr[0][6:])
        case_index = case_list.index(case_id)
        root_joint = 0
        myr = np.asarray(md[case_index][frid, root_joint, :9]).reshape((3, 3))
        myt = np.asarray(md[case_index][frid, root_joint, 9:]).reshape((1, 3))

        npy_path = os.path.join(SET.CASE, case_folder_list[case_id - 1],
                                fr[1] + '_npy',
                                str(frid).zfill(5) + '.npy')
        x = np.load(npy_path)

        y = np.dot(x - np.tile(myt, [vnum, 1]), np.linalg.inv(myr))
        verts[fid, :, :] = y.reshape(1, vnum, 3)

    for i in range(40):
        xid = random.randint(0, uid)
        obj.objexport(verts[xid, :, :], SET.MESH_FACES,
                      os.path.join(SET.DATA_SAMPLE, "s_%s.obj" % str(i)))

    m = np.mean(verts, axis=0)
    s = np.std(verts, axis=0)

    np.save(os.path.join(SET.INFO_BASIS, SET.DATA_MEAN), m)
    np.save(os.path.join(SET.INFO_BASIS, SET.DATA_STD), s)
def visualize_skeleton():

    md = np.load(SET.JOINTS_ANIM_PATH)

    xid = 10
    ts = [-1, 1]

    fs = [[1, 2, 3], [3, 2, 4], [7, 6, 5], [6, 7, 8], [1, 3, 5], [5, 3, 7],
          [2, 6, 4], [4, 6, 8], [3, 4, 7], [7, 4, 8], [1, 5, 2], [2, 5, 6]]

    for tid in range(md.shape[1]):
        myt = np.asarray(md[xid, tid, 9:]).reshape((1, 3))
        vs = [[0, 0, 0]] * 8
        ct = -1
        for p1 in ts:
            for p2 in ts:
                for p3 in ts:
                    ct = ct + 1
                    vs[ct] = [myt[0, 0] + p1, myt[0, 1] + p2, myt[0, 2] + p3]

        file_name = "fr_%s.sk_%s.obj" % (str(xid).zfill(5), str(tid).zfill(5))
        obj.objexport(vs, fs, os.path.join(SET.SKELETON, file_name))
        z = torch.div(basis_net.g(y) - basis_mean, basis_std)

        res = z.clone()
        res[z != z] = 0.0
        z = res

        temp_data = model.g(z, w)
        data[bid] = temp_data
        zs[bid] = z
        weights[bid] = w

    loss_1 = loss_1 + torch.sum(data.std(dim=0))
    dmean = data.mean(dim=0)

    for bid in range(batchsize_2):

        q = model.h(dmean, weights[bid])
        w = basis_net.h(torch.mul(q, basis_std) + basis_mean)
        u_ = np.multiply(w.cpu().data.numpy().reshape(vnum, 3),
                         data_std) + data_mean
        u = np.dot(u_, (myr)) + np.tile(myt, [vnum, 1])
        loss_2 = tools.calculate_dist(q, zs[bid])

        file_name_grt = 'eval_%s_%s_%s_%s_grt.obj' % (str(bid).zfill(3), fr[0],
                                                      fr[1], fr[2])
        file_name_rec = 'eval_%s_%s_%s_%s_%s_rec.obj' % (
            str(bid).zfill(3), fr[0], fr[1], fr[2], str(float(loss_2.cpu())))

        obj.objexport(x, tmp_f, os.path.join(SET.EVAL_MIE, file_name_grt))
        obj.objexport(u, tmp_f, os.path.join(SET.EVAL_MIE, file_name_rec))
verts = np.zeros((uid, vnum, 3))

for fid in tqdm(range(uid)):
    fr = flist_train[fid]
    frid = int(fr[2])
    case_id = int(fr[0][6:])
    case_index = case_list.index(case_id)
    root_joint = 0
    myr = np.asarray(md[case_index][frid, root_joint, :9]).reshape((3, 3))
    myt = np.asarray(md[case_index][frid, root_joint, 9:]).reshape((1, 3))

    npy_path = os.path.join(SET.CASE, case_folder_list[case_id - 1],
                            fr[1] + '_npy',
                            str(frid).zfill(5) + '.npy')
    x = np.load(npy_path)

    y = np.dot(x - np.tile(myt, [vnum, 1]), np.linalg.inv(myr))
    verts[fid, :, :] = y.reshape(1, vnum, 3)

for i in range(40):
    xid = random.randint(0, uid)
    obj.objexport(verts[xid, :, :], SET.MESH_FACES,
                  os.path.join(SET.DATA_SAMPLE, "s_%s.obj" % str(i)))

m = np.mean(verts, axis=0)
s = np.std(verts, axis=0)

np.save(os.path.join(SET.INFO_BASIS, SET.DATA_MEAN), m)
np.save(os.path.join(SET.INFO_BASIS, SET.DATA_STD), s)
import os

from common import obj_operation as obj
from common import setting as SET
from tqdm import tqdm

md = np.load(SET.JOINTS_ANIM_PATH)

xid = 10
ts = [-1,1]

fs = [[1,2,3],[3,2,4],[7,6,5],[6,7,8],[1,3,5],[5,3,7],[2,6,4],[4,6,8],[3,4,7],[7,4,8],[1,5,2],[2,5,6]]

for tid in range(md.shape[1]):
	myt = np.asarray(md[xid,tid,9:]).reshape((1,3))
	vs = [[0,0,0]] *8
	ct = -1
	for p1 in ts:
		for p2 in ts:
			for p3 in ts:
				ct = ct + 1
				vs[ct] = [myt[0,0]+p1, myt[0,1]+p2, myt[0,2]+p3]

	file_name = "fr_%s.sk_%s.obj" % (str(xid).zfill(5), str(tid).zfill(5))
	obj.objexport(vs, fs, os.path.join(SET.SKELETON, file_name))