forked from nschloe/meshio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_ply.py
379 lines (331 loc) · 10.8 KB
/
_ply.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
"""
I/O for the PLY format, cf.
<https://en.wikipedia.org/wiki/PLY_(file_format)>.
<https://web.archive.org/web/20161221115231/http://www.cs.virginia.edu/~gfx/Courses/2001/Advanced.spring.01/plylib/Ply.txt>.
"""
import re
import sys
import warnings
import numpy
from ._mesh import Mesh
# Reference dtypes
ply_to_numpy_dtype = {
"short": numpy.int16,
"ushort": numpy.uint16,
"int": numpy.int32,
"int8": numpy.int8,
"int32": numpy.int32,
"int64": numpy.int64,
"uint": numpy.uint32,
"uint8": numpy.uint8,
"float": numpy.float32,
"float32": numpy.float32,
"double": numpy.float64,
}
numpy_to_ply_dtype = {numpy.dtype(v): k for k, v in ply_to_numpy_dtype.items()}
def read(filename):
with open(filename, "rb") as f:
mesh = read_buffer(f)
return mesh
def _fast_forward(f):
# fast forward to the next significant line
while True:
line = f.readline().decode("utf-8").strip()
if line and line[:7] != "comment":
break
return line
def read_buffer(f):
# assert that the first line reads `ply`
line = f.readline().decode("utf-8").strip()
assert line == "ply"
line = _fast_forward(f)
if line == "format ascii 1.0":
is_binary = False
elif line == "format binary_big_endian 1.0":
is_binary = True
endianness = ">"
else:
assert line == "format binary_little_endian 1.0"
is_binary = True
endianness = "<"
line = _fast_forward(f)
m = re.match("element vertex (\\d+)", line)
num_verts = int(m.groups()[0])
# fast forward to the next significant line
point_data_formats = []
point_data_names = []
line = _fast_forward(f)
while line[:8] == "property":
m = re.match("property (.+) (.+)", line)
point_data_formats.append(m.groups()[0])
point_data_names.append(m.groups()[1])
line = _fast_forward(f)
m = re.match("element face (\\d+)", line)
num_cells = int(m.groups()[0])
# read property lists
line = _fast_forward(f)
cell_data_names = []
cell_dtypes = []
while line != "end_header":
m = re.match("property list (.+) (.+) (.+)", line)
types = m.groups()[:2]
name = m.groups()[2]
cell_data_names.append(name)
cell_dtypes.append(tuple(types))
line = _fast_forward(f)
assert line == "end_header"
if is_binary:
mesh = _read_binary(
f,
endianness,
point_data_names,
point_data_formats,
num_verts,
num_cells,
cell_data_names,
cell_dtypes,
)
else:
mesh = _read_ascii(
f,
point_data_names,
point_data_formats,
num_verts,
num_cells,
cell_data_names,
cell_dtypes,
)
return mesh
def _read_ascii(
f,
point_data_names,
point_data_formats,
num_verts,
num_cells,
cell_data_names,
cell_dtypes,
):
# assert that all formats are the same
# Now read the data
dtype = numpy.dtype(
[
(name, ply_to_numpy_dtype[fmt])
for name, fmt in zip(point_data_names, point_data_formats)
]
)
pd = numpy.genfromtxt(f, max_rows=num_verts, dtype=dtype)
# split off coordinate data and additional point data
verts = []
k = 0
if point_data_names[0] == "x":
verts.append(pd["x"])
k += 1
if point_data_names[1] == "y":
verts.append(pd["y"])
k += 1
if point_data_names[2] == "z":
verts.append(pd["z"])
k += 1
verts = numpy.column_stack(verts)
point_data = {
point_data_names[i]: pd[point_data_names[i]]
for i in range(k, len(point_data_names))
}
# the faces must be read line-by-line
triangles = []
quads = []
for k in range(num_cells):
line = f.readline().decode("utf-8").strip()
data = line.split()
if k == 0:
# initialize the cell data arrays
n = []
i = 0
cell_data = {}
for name, dtype in zip(cell_data_names, cell_dtypes):
n = int(data[i])
if name != "vertex_indices":
cell_data[name] = []
i += n + 1
i = 0
for name, dtype in zip(cell_data_names, cell_dtypes):
n = int(data[i])
dtype = ply_to_numpy_dtype[dtype[1]]
data = [dtype(data[j]) for j in range(i + 1, i + n + 1)]
if name == "vertex_indices":
if n == 3:
triangles.append(data)
else:
assert n == 4
quads.append(data)
else:
cell_data[name].append(data)
i += n + 1
cells = {}
if len(triangles) > 0:
cells["triangle"] = numpy.array(triangles)
# cell_data = {"triangle": cell_data}
if len(quads) > 0:
cells["quad"] = numpy.array(quads)
# cell_data = {"quad": cell_data}
return Mesh(verts, cells, point_data=point_data, cell_data=cell_data)
def _read_binary(
f,
endianness,
point_data_names,
formats,
num_verts,
num_cells,
cell_data_names,
cell_dtypes,
):
ply_to_numpy_dtype_string = {
"uchar": "i1",
"uint8": "u1",
"int8": "i1",
"int32": "i4",
"int64": "i8",
"float": "f4",
"float32": "f4",
"double": "f8",
}
# read point data
dtype = [
(name, endianness + ply_to_numpy_dtype_string[fmt])
for name, fmt in zip(point_data_names, formats)
]
point_data = numpy.fromfile(f, count=num_verts, dtype=dtype)
verts = numpy.column_stack([point_data["x"], point_data["y"], point_data["z"]])
point_data = {
name: point_data[name]
for name in point_data_names
if name not in ["x", "y", "z"]
}
# Convert strings to proper numpy dtypes
dt = [
(
endianness + ply_to_numpy_dtype_string[dtypes[0]],
endianness + ply_to_numpy_dtype_string[dtypes[1]],
)
for dtypes in cell_dtypes
]
# read cell data -- this part is really slow
triangles = []
quads = []
for _ in range(num_cells):
count = numpy.fromfile(f, count=1, dtype=dt[0][0])[0]
data = numpy.fromfile(f, count=count, dtype=dt[0][1])
if count == 3:
triangles.append(data)
else:
assert count == 4
quads.append(data)
# cell data
for dtypes in dt[1:]:
pass
cells = {}
if len(triangles) > 0:
cells["triangle"] = numpy.array(triangles)
if len(quads) > 0:
cells["quad"] = numpy.array(quads)
return Mesh(verts, cells, point_data=point_data, cell_data={})
def write(filename, mesh, binary=True):
for key in mesh.cells:
assert key in [
"triangle",
"quad",
], "Can only deal with triangular and quadrilateral faces"
with open(filename, "wb") as fh:
fh.write(b"ply\n")
fh.write(b"comment Created by meshio\n")
if binary:
fh.write(
"format binary_{}_endian 1.0\n".format(sys.byteorder).encode("utf-8")
)
else:
fh.write(b"format ascii 1.0\n")
# counts
fh.write("element vertex {:d}\n".format(mesh.points.shape[0]).encode("utf-8"))
#
dim_names = ["x", "y", "z"]
type_name_table = {
numpy.dtype(numpy.int8): "int8",
numpy.dtype(numpy.int64): "int64",
numpy.dtype(numpy.float32): "float",
numpy.dtype(numpy.float64): "double",
}
for k in range(mesh.points.shape[1]):
type_name = type_name_table[mesh.points.dtype]
fh.write("property {} {}\n".format(type_name, dim_names[k]).encode("utf-8"))
for key, value in mesh.point_data.items():
type_name = type_name_table[value.dtype]
fh.write("property {} {}\n".format(type_name, key).encode("utf-8"))
num_cells = 0
if "triangle" in mesh.cells:
num_cells += mesh.cells["triangle"].shape[0]
if "quad" in mesh.cells:
num_cells += mesh.cells["quad"].shape[0]
fh.write("element face {:d}\n".format(num_cells).encode("utf-8"))
# possibly cast down to int32
cells = mesh.cells
has_cast = False
for key in mesh.cells:
if mesh.cells[key].dtype == numpy.int64:
has_cast = True
cells[key] = mesh.cells[key].astype(numpy.int32)
if has_cast:
warnings.warn(
"PLY doesn't support 64-bit integers. Casting down to 32-bit."
)
# TODO use uint8 for cell count
# assert that all cell dtypes are equal
cell_dtype = None
for cell in cells.values():
if cell_dtype is None:
cell_dtype = cell.dtype
assert cell.dtype == cell_dtype
ply_type = numpy_to_ply_dtype[cell_dtype]
fh.write(
"property list {} {} vertex_indices\n".format(ply_type, ply_type).encode(
"utf-8"
)
)
# TODO other cell data
fh.write(b"end_header\n")
if binary:
# points and point_data
out = numpy.rec.fromarrays(
[coord for coord in mesh.points.T] + list(mesh.point_data.values())
)
fh.write(out.tostring())
# cells
for key in ["triangle", "quad"]:
if key not in cells:
continue
dat = cells[key]
# prepend with count
count = numpy.full(dat.shape[0], dat.shape[1], dtype=dat.dtype)
out = numpy.column_stack([count, dat])
fh.write(out.tostring())
else:
# vertices
# numpy.savetxt(fh, mesh.points, "%r") # slower
# out = numpy.column_stack([mesh.points] + list(mesh.point_data.values()))
out = numpy.rec.fromarrays(
[coord for coord in mesh.points.T] + list(mesh.point_data.values())
)
fmt = " ".join(["{}"] * len(out[0]))
out = "\n".join([fmt.format(*row) for row in out]) + "\n"
fh.write(out.encode("utf-8"))
# cells
for key in ["triangle", "quad"]:
if key not in cells:
continue
dat = cells[key]
out = numpy.column_stack([numpy.full(dat.shape[0], dat.shape[1]), dat])
# savetxt is slower
# numpy.savetxt(fh, out, "%d %d %d %d")
fmt = " ".join(["{}"] * out.shape[1])
out = "\n".join([fmt.format(*row) for row in out]) + "\n"
fh.write(out.encode("utf-8"))
return