#  Copyright (c) 2020. Hanchen Wang, hw501@cam.ac.uk

import os, open3d, numpy as np

File_ = open('ModelNet_flist_short.txt', 'w')

if __name__ == "__main__":
    root_dir = "../data/ModelNet_subset/"

    for root, dirs, files in os.walk(root_dir, topdown=False):
        for file in files:
            if '.ply' in file:
                amesh = open3d.io.read_triangle_mesh(os.path.join(root, file))
                out_file_name = os.path.join(root, file).replace('.ply', '_normalised.obj')

                center = amesh.get_center()
                amesh.translate(-center)
                maxR = (np.asarray(amesh.vertices)**2).sum(axis=1).max()**(1/2)
                # we found divided by (2*maxR) has best rendered visualisation results
                amesh.scale(1/(2*maxR))
                open3d.io.write_triangle_mesh(out_file_name, amesh)
                File_.writelines(out_file_name.replace('.obj', '').replace(root_dir, '') + '\n')
                print(out_file_name)

[((10, 29, 10, 61), 'os.walk', 'os.walk', (), '', False, 'import os, open3d, numpy as np\n'), ((21, 16, 21, 67), 'open3d.io.write_triangle_mesh', 'open3d.io.write_triangle_mesh', ({(21, 46, 21, 59): 'out_file_name', (21, 61, 21, 66): 'amesh'}, {}), '(out_file_name, amesh)', False, 'import os, open3d, numpy as np\n'), ((13, 53, 13, 77), 'os.path.join', 'os.path.join', ({(13, 66, 13, 70): 'root', (13, 72, 13, 76): 'file'}, {}), '(root, file)', False, 'import os, open3d, numpy as np\n'), ((14, 32, 14, 56), 'os.path.join', 'os.path.join', ({(14, 45, 14, 49): 'root', (14, 51, 14, 55): 'file'}, {}), '(root, file)', False, 'import os, open3d, numpy as np\n'), ((18, 24, 18, 50), 'numpy.asarray', 'np.asarray', ({(18, 35, 18, 49): 'amesh.vertices'}, {}), '(amesh.vertices)', True, 'import os, open3d, numpy as np\n')]