|
2 | 2 | import sys |
3 | 3 | from plyfile import PlyData, PlyElement |
4 | 4 | import numpy as np |
5 | | -from pytinymesh import Mesh |
| 5 | +from tinymesh import Mesh |
6 | 6 |
|
7 | | -def main(filename): |
8 | | - # Construct a simple tetrahedron |
9 | | - a = np.asarray([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype='double') |
10 | | - b = np.asarray([0, 1, 2, 0, 2, 3, 0, 3, 1, 3, 2, 1], dtype='uint32') |
11 | | - mesh = Mesh(a, b) |
12 | | - mesh.save('tetra.ply') |
13 | 7 |
|
14 | | - # Load .PLY with "plyfile" and construct a mesh by "tinymesh" |
| 8 | +def main(filename): |
| 9 | + # Load .PLY with "plyfile" |
15 | 10 | plydata = PlyData.read(filename) |
16 | 11 | vx = plydata['vertex']['x'] |
17 | 12 | vy = plydata['vertex']['y'] |
18 | 13 | vz = plydata['vertex']['z'] |
19 | 14 | verts = np.stack([vx, vy, vz], axis=1).astype('double') |
20 | | - elems = np.concatenate(plydata['face']['vertex_indices']).astype('uint32') |
21 | | - mesh = Mesh(verts, elems) |
22 | | - mesh.save('output.ply') |
| 15 | + faces = np.concatenate(plydata['face']['vertex_indices']).astype('uint32') |
| 16 | + |
| 17 | + # Construct halfedge structure by "tinymesh" |
| 18 | + mesh = Mesh(verts, faces) |
| 19 | + |
| 20 | + # Get vertices and indices back to Python |
| 21 | + verts = mesh.get_vertices() |
| 22 | + faces = mesh.get_vertex_indices() |
| 23 | + |
| 24 | + verts = np.asarray(verts, dtype='float32') |
| 25 | + faces = np.asarray(faces, dtype='int32').reshape((-1, 3)) |
| 26 | + |
| 27 | + vert_el = np.array([tuple(v) for v in verts], dtype=[('x', 'f4'), ('y', 'f4'), ('z', 'f4')]) |
| 28 | + face_el = np.array([(tuple(f),) for f in faces], dtype=[('vertex_indices', 'i4', (3,))]) |
| 29 | + plydata = PlyData([PlyElement.describe(vert_el, 'vertex'), |
| 30 | + PlyElement.describe(face_el, 'face')], text=False) |
| 31 | + |
| 32 | + base, ext = os.path.splitext(filename) |
| 33 | + outfile = base + "_copy" + ext |
| 34 | + plydata.write(outfile) |
23 | 35 |
|
24 | 36 |
|
25 | 37 | if __name__ == '__main__': |
|
0 commit comments