-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
ply-reader.js
216 lines (180 loc) · 6.71 KB
/
ply-reader.js
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
const magicBytes = new Uint8Array([112, 108, 121, 10]); // ply\n
const endHeaderBytes = new Uint8Array([10, 101, 110, 100, 95, 104, 101, 97, 100, 101, 114, 10]); // \nend_header\n
const dataTypeMap = new Map([
['char', Int8Array],
['uchar', Uint8Array],
['short', Int16Array],
['ushort', Uint16Array],
['int', Int32Array],
['uint', Uint32Array],
['float', Float32Array],
['double', Float64Array]
]);
class PlyProperty {
type;
name;
storage;
byteSize;
}
class PlyElement {
name;
count;
properties;
}
// asynchronously read a ply file data
const readPly = async (reader, propertyFilter = null) => {
const concat = (a, b) => {
const c = new Uint8Array(a.byteLength + b.byteLength);
c.set(a);
c.set(b, a.byteLength);
return c;
};
const find = (buf, search) => {
const endIndex = buf.length - search.length;
let i, j;
for (i = 0; i < endIndex; ++i) {
for (j = 0; j < search.length; ++j) {
if (buf[i + j] !== search[j]) {
break;
}
}
if (j === search.length) {
return i;
}
}
return -1;
};
const startsWith = (a, b) => {
if (a.length < b.length) {
return false;
}
for (let i = 0; i < b.length; ++i) {
if (a[i] !== b[i]) {
return false;
}
}
return true;
};
let buf;
let endHeaderIndex;
while (true) {
// get the next chunk
/* eslint-disable no-await-in-loop */
const { value, done } = await reader.read();
if (done) {
throw new Error('Stream finished before end of header');
}
// combine new chunk with the previous
buf = buf ? concat(buf, value) : value;
// check magic bytes
if (buf.length >= magicBytes.length && !startsWith(buf, magicBytes)) {
throw new Error('Invalid ply header');
}
// check if we can find the end-of-header marker
endHeaderIndex = find(buf, endHeaderBytes);
if (endHeaderIndex !== -1) {
break;
}
}
// decode buffer header text
const headerText = new TextDecoder('ascii').decode(buf.slice(0, endHeaderIndex));
// split into lines and remove comments
const headerLines = headerText.split('\n')
.filter(line => !line.startsWith('comment '));
// decode header and allocate data storage
const elements = [];
for (let i = 1; i < headerLines.length; ++i) {
const words = headerLines[i].split(' ');
switch (words[0]) {
case 'format':
if (words[1] !== 'binary_little_endian') {
throw new Error('Unsupported ply format');
}
break;
case 'element':
elements.push({
name: words[1],
count: parseInt(words[2], 10),
properties: []
});
break;
case 'property': {
if (!dataTypeMap.has(words[1])) {
throw new Error(`Unrecognized property data type '${words[1]}' in ply header`);
}
const element = elements[elements.length - 1];
const storageType = dataTypeMap.get(words[1]);
const storage = (!propertyFilter || propertyFilter.has(words[2])) ? new storageType(element.count) : null;
element.properties.push({
type: words[1],
name: words[2],
storage: storage,
byteSize: storageType.BYTES_PER_ELEMENT
});
break;
}
default:
throw new Error(`Unrecognized header value '${words[0]}' in ply header`);
}
}
// read data
let readIndex = endHeaderIndex + endHeaderBytes.length;
let remaining = buf.length - readIndex;
let dataView = new DataView(buf.buffer);
for (let i = 0; i < elements.length; ++i) {
const element = elements[i];
for (let e = 0; e < element.count; ++e) {
for (let j = 0; j < element.properties.length; ++j) {
const property = element.properties[j];
// if we've run out of data, load the next chunk
while (remaining < property.byteSize) {
const { value, done } = await reader.read();
if (done) {
throw new Error('Stream finished before end of data');
}
// create buffer with left-over data from previous chunk and the new data
const tmp = new Uint8Array(remaining + value.byteLength);
tmp.set(buf.slice(readIndex));
tmp.set(value, remaining);
buf = tmp;
dataView = new DataView(buf.buffer);
readIndex = 0;
remaining = buf.length;
}
if (property.storage) {
switch (property.type) {
case 'char':
property.storage[e] = dataView.getInt8(readIndex);
break;
case 'uchar':
property.storage[e] = dataView.getUint8(readIndex);
break;
case 'short':
property.storage[e] = dataView.getInt16(readIndex, true);
break;
case 'ushort':
property.storage[e] = dataView.getUint16(readIndex, true);
break;
case 'int':
property.storage[e] = dataView.getInt32(readIndex, true);
break;
case 'uint':
property.storage[e] = dataView.getUint32(readIndex, true);
break;
case 'float':
property.storage[e] = dataView.getFloat32(readIndex, true);
break;
case 'double':
property.storage[e] = dataView.getFloat64(readIndex, true);
break;
}
}
readIndex += property.byteSize;
remaining -= property.byteSize;
}
}
}
// console.log(elements);
return elements;
};
export { readPly, PlyProperty, PlyElement };