def load_data(k):
        for i, j in enumerate(range(k * n, (k + 1) * n)):

            if i % 10 == 0:
                print('reading number', i + 1,
                      'th lab' + str(k + 1) + ' point clouds')

            if use_key_feature:
                pc = np.loadtxt(
                    base_path + '/lab' + str(k + 1) + '/lab_project' + str(i) +
                    '.txt')  # pc = tf.convert_to_tensor(pc, dtype=tf.float32)
                pc = PointCloud(pc)
                pc.normalize()

                expand = np.expand_dims(pc.position, axis=0)
                pc_tile[j, :, :] = expand
                # print('*****************************************')
                # print('reading point cloud cost time:{}'.format(t1 - t0))
                pc_key_eig = get_local_eig_np(expand)  # 1 x nb_keypoints x 9

                # print('*****************************************')
                # print('get local cost time:{}'.format(t2 - t1))
                #pc_key_feature[i, :, :] = np.squeeze(sess.run(pc_key_eig, feed_dict={pc_pl: pc}))
                pc_key_feature[j, :, :] = np.squeeze(pc_key_eig)
            else:
                pc_tile[j, :, :] = np.expand_dims(
                    np.loadtxt(base_path + '/lab' + str(k + 1) +
                               '/lab_project' + str(i) + '.txt'),
                    axis=0)

            # print('-----------------------------------------')
            # print('one pc cost total:{}second'.format(te-ts))
            # print('----------------------------------------')
        print('current thread endingļ¼š ', threading.current_thread().name)
def save_data(save_path='',
              base_path='',
              n=5000,
              use_key_feature=True,
              train_data=True,
              nb_types=4,
              show_result=False,
              normalize=True,
              shuffle=True):
    """
    transform the txt point clouds into h5py dataset for simplicity. data augmentation of projection is implemented here
    :param save_path:
    :param n:
    :param train_data whether it is training data or it is test data. if its testdata, label is random.
    :param base_path:  path contains txt or ply point cloud data
    :param use_key_feature: if you want to use the local key features
    :param nb_types: number of classes of used object
    :return:
    """
    compute_time = []
    if train_data:
        pc_tile = np.empty(shape=(nb_types * n, 1024, 3))
        if use_key_feature:
            pc_key_feature = np.empty(shape=(nb_types * n, int(
                1024 * 0.1), 9))  # key feature space, 102=1024*0.1,
            # 9 for multi-scale eigen-value
            #pc_pl = tf.placeholder(tf.float32, shape=(1, 1024, 3))

        for k in range(
                nb_types
        ):  # number of types of  objects model, test data can ignore type label
            for i, j in enumerate(range(k * n, (k + 1) * n)):

                if i % 10 == 0:
                    print('reading number', i + 1,
                          'th lab' + str(k + 1) + ' point clouds')

                if use_key_feature:
                    pc = np.loadtxt(
                        base_path + '/lab' + str(k + 1) + '/lab_project' +
                        str(i) + '.txt'
                    )  # pc = tf.convert_to_tensor(pc, dtype=tf.float32)
                    pc = PointCloud(pc)
                    if normalize:
                        pc.normalize(
                        )  # partial point cloud should not normalize

                    expand = np.expand_dims(pc.position, axis=0)
                    pc_tile[j, :, :] = expand
                    # print('*****************************************')
                    # print('reading point cloud cost time:{}'.format(t1 - t0))

                    pc_key_eig = get_local_eig_np(
                        expand, useiss=False)  # 1 x nb_keypoints x 9

                    # print('*****************************************')
                    # print('get local cost time:{}'.format(t2 - t1))
                    #pc_key_feature[i, :, :] = np.squeeze(sess.run(pc_key_eig, feed_dict={pc_pl: pc}))
                    pc_key_feature[j, :, :] = np.squeeze(pc_key_eig)
                else:

                    pc_tile[j, :, :] = np.expand_dims(
                        np.loadtxt(base_path + '/lab' + str(k + 1) +
                                   '/lab_project' + str(i) + '.txt'),
                        axis=0)

                # print('-----------------------------------------')
                # print('one pc cost total:{}second'.format(te-ts))
                # print('----------------------------------------')

        pc_label = np.tile(np.arange(nb_types), (n, 1)).reshape((-1, ),
                                                                order='F')
        train_set_shape = (nb_types * n, 1024, 3)
        train_set_local_shape = (nb_types * n, 102, 9)
        train_label_shape = (nb_types * n, )
    else:
        ply_list = [
            base_path + '/' + i for i in os.listdir(base_path)
            if os.path.splitext(i)[1] == '.ply'
            or os.path.splitext(i)[1] == '.txt'
        ]
        n = len(ply_list)
        less_than_th = []
        for i, j in enumerate(ply_list):
            pc = PointCloud(j)
            if pc.nb_points < 1024:
                less_than_th.append(i)

        n = n - len(less_than_th)
        print("there are: ,", n, ' point clouds with # of points available')
        pc_label = np.arange(n)
        train_set_shape = (n, 1024, 3)
        train_set_local_shape = (n, 102, 9)
        train_label_shape = (n, )

        pc_tile = np.empty(shape=(n, 1024, 3))
        pc_key_feature = np.empty(shape=(n, int(
            1024 * 0.1), 9))  # key feature space, 102=1024*0.1,

        for i, j in enumerate(ply_list):
            if i not in less_than_th:
                print(j)
                start_time = time.clock()
                mypc = PointCloud(j)
                if mypc.nb_points > 1024:
                    mypc.down_sample(number_of_downsample=1024)
                    if normalize:
                        mypc.normalize()
                    expand = np.expand_dims(mypc.position, axis=0)
                    pc_tile[i, :, :] = expand
                    pc_key_eig = get_local_eig_np(expand, useiss=False)
                    end_time = time.clock()
                    compute_time.append([end_time - start_time])
                    if use_key_feature:
                        pc_key_feature[i, :, :] = np.squeeze(pc_key_eig)

    hdf5_file = h5py.File(save_path, mode='a')
    hdf5_file.create_dataset('train_set', train_set_shape,
                             np.float32)  # be careful about the dtype
    hdf5_file.create_dataset('train_labels', train_label_shape, np.uint8)
    hdf5_file.create_dataset('train_set_local', train_set_local_shape,
                             np.float32)
    if shuffle:

        idx = np.arange(np.shape(pc_tile)[0])
        np.random.shuffle(idx)
        pc_tile = pc_tile[idx, ...]
        pc_label = pc_label[idx, ...]
        pc_key_feature = pc_key_feature[idx, ...]

    hdf5_file["train_set"][...] = pc_tile
    hdf5_file["train_labels"][...] = pc_label
    hdf5_file["train_set_local"][...] = pc_key_feature
    hdf5_file.close()
    return compute_time