#  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)

[((232, 264), 'os.walk', 'os.walk', (['root_dir'], {'topdown': '(False)'}), '(root_dir, topdown=False)\n', (239, 264), False, 'import os, open3d, numpy as np\n'), ((804, 855), 'open3d.io.write_triangle_mesh', 'open3d.io.write_triangle_mesh', (['out_file_name', 'amesh'], {}), '(out_file_name, amesh)\n', (833, 855), False, 'import os, open3d, numpy as np\n'), ((377, 401), 'os.path.join', 'os.path.join', (['root', 'file'], {}), '(root, file)\n', (389, 401), False, 'import os, open3d, numpy as np\n'), ((435, 459), 'os.path.join', 'os.path.join', (['root', 'file'], {}), '(root, file)\n', (447, 459), False, 'import os, open3d, numpy as np\n'), ((605, 631), 'numpy.asarray', 'np.asarray', (['amesh.vertices'], {}), '(amesh.vertices)\n', (615, 631), True, 'import os, open3d, numpy as np\n')]