This repository has been archived by the owner on Oct 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
ply_reader.py
89 lines (71 loc) · 2.64 KB
/
ply_reader.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
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
\file ply_reader.py
\brief Ply binary reader. This code is a modification from the file ply.py
from the project pyntcloud of David de la Iglesia Castro
(https://github.com/daavoo/pyntcloud)
\copyright Copyright (c) 2019 Visual Computing group of Ulm University,
Germany. See the LICENSE file at the top-level directory of
this distribution.
\author pedro hermosilla (pedro-1.hermosilla-casajus@uni-ulm.de)
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
import sys
import numpy as np
from collections import defaultdict
sys_byteorder = ('>', '<')[sys.byteorder == 'little']
ply_dtypes = dict([
(b'int8', 'i1'),
(b'char', 'i1'),
(b'uint8', 'u1'),
(b'uchar', 'b1'),
(b'uchar', 'u1'),
(b'int16', 'i2'),
(b'short', 'i2'),
(b'uint16', 'u2'),
(b'ushort', 'u2'),
(b'int32', 'i4'),
(b'int', 'i4'),
(b'uint32', 'u4'),
(b'uint', 'u4'),
(b'float32', 'f4'),
(b'float', 'f4'),
(b'float64', 'f8'),
(b'double', 'f8')
])
valid_formats = {'ascii': '', 'binary_big_endian': '>',
'binary_little_endian': '<'}
def read_points_binary_ply(filename):
with open(filename, 'rb') as ply:
if b'ply' not in ply.readline():
raise ValueError('The file does not start whith the word ply')
fmt = ply.readline().split()[1].decode()
if fmt == "ascii":
raise ValueError('The file format is ascii not binary')
ext = valid_formats[fmt]
line = []
dtypes = defaultdict(list)
count = 2
points_size = None
mesh_size = None
while b'end_header' not in line and line != b'':
line = ply.readline()
if b'element' in line:
line = line.split()
name = line[1].decode()
size = int(line[2])
if name == "vertex":
points_size = size
elif name == "face":
mesh_size = size
elif b'property' in line:
line = line.split()
if b'list' not in line:
dtypes[name].append((line[2].decode(), ext + ply_dtypes[line[1]]))
count += 1
end_header = ply.tell()
with open(filename, 'rb') as ply:
ply.seek(end_header)
currType = np.dtype(dtypes["vertex"])
points_np = np.fromfile(ply, dtype=currType, count=points_size)
if ext != sys_byteorder:
points_np = points_np.byteswap().newbyteorder()
return points_np