@loaders.gl/ply 3.3.0-alpha.5 → 3.3.0-alpha.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/es5/bundle.js +0 -1
- package/dist/es5/bundle.js.map +1 -1
- package/dist/es5/index.js +4 -17
- package/dist/es5/index.js.map +1 -1
- package/dist/es5/lib/get-ply-schema.js +0 -6
- package/dist/es5/lib/get-ply-schema.js.map +1 -1
- package/dist/es5/lib/normalize-ply.js +1 -8
- package/dist/es5/lib/normalize-ply.js.map +1 -1
- package/dist/es5/lib/parse-ply-in-batches.js +98 -169
- package/dist/es5/lib/parse-ply-in-batches.js.map +1 -1
- package/dist/es5/lib/parse-ply.js +1 -53
- package/dist/es5/lib/parse-ply.js.map +1 -1
- package/dist/es5/lib/ply-types.js.map +1 -1
- package/dist/es5/ply-loader.js +3 -1
- package/dist/es5/ply-loader.js.map +1 -1
- package/dist/es5/workers/ply-worker.js +0 -2
- package/dist/es5/workers/ply-worker.js.map +1 -1
- package/dist/esm/bundle.js +1 -1
- package/dist/esm/bundle.js.map +1 -1
- package/dist/esm/index.js +4 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/lib/get-ply-schema.js +0 -4
- package/dist/esm/lib/get-ply-schema.js.map +1 -1
- package/dist/esm/lib/normalize-ply.js +2 -5
- package/dist/esm/lib/normalize-ply.js.map +1 -1
- package/dist/esm/lib/parse-ply-in-batches.js +6 -38
- package/dist/esm/lib/parse-ply-in-batches.js.map +1 -1
- package/dist/esm/lib/parse-ply.js +8 -52
- package/dist/esm/lib/parse-ply.js.map +1 -1
- package/dist/esm/lib/ply-types.js.map +1 -1
- package/dist/esm/ply-loader.js +4 -1
- package/dist/esm/ply-loader.js.map +1 -1
- package/dist/esm/workers/ply-worker.js.map +1 -1
- package/dist/ply-worker.js +1 -1
- package/package.json +4 -4
|
@@ -1,16 +1,17 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
1
3
|
import { makeLineIterator, makeTextDecoderIterator, forEach } from '@loaders.gl/loader-utils';
|
|
2
4
|
import normalizePLY from './normalize-ply';
|
|
3
5
|
let currentElement;
|
|
6
|
+
|
|
4
7
|
export default async function* parsePLYInBatches(iterator, options) {
|
|
5
8
|
const lineIterator = makeLineIterator(makeTextDecoderIterator(iterator));
|
|
6
9
|
const header = await parsePLYHeader(lineIterator, options);
|
|
7
10
|
let attributes;
|
|
8
|
-
|
|
9
11
|
switch (header.format) {
|
|
10
12
|
case 'ascii':
|
|
11
13
|
attributes = await parseASCII(lineIterator, header);
|
|
12
14
|
break;
|
|
13
|
-
|
|
14
15
|
default:
|
|
15
16
|
throw new Error('Binary PLY can not yet be parsed in streaming mode');
|
|
16
17
|
}
|
|
@@ -23,6 +24,7 @@ async function parsePLYHeader(lineIterator, options) {
|
|
|
23
24
|
comments: [],
|
|
24
25
|
elements: []
|
|
25
26
|
};
|
|
27
|
+
|
|
26
28
|
await forEach(lineIterator, line => {
|
|
27
29
|
line = line.trim();
|
|
28
30
|
|
|
@@ -37,56 +39,44 @@ async function parsePLYHeader(lineIterator, options) {
|
|
|
37
39
|
const lineValues = line.split(/\s+/);
|
|
38
40
|
const lineType = lineValues.shift();
|
|
39
41
|
line = lineValues.join(' ');
|
|
40
|
-
|
|
41
42
|
switch (lineType) {
|
|
42
43
|
case 'ply':
|
|
43
44
|
break;
|
|
44
|
-
|
|
45
45
|
case 'format':
|
|
46
46
|
header.format = lineValues[0];
|
|
47
47
|
header.version = lineValues[1];
|
|
48
48
|
break;
|
|
49
|
-
|
|
50
49
|
case 'comment':
|
|
51
50
|
header.comments.push(line);
|
|
52
51
|
break;
|
|
53
|
-
|
|
54
52
|
case 'element':
|
|
55
53
|
if (currentElement) {
|
|
56
54
|
header.elements.push(currentElement);
|
|
57
55
|
}
|
|
58
|
-
|
|
59
56
|
currentElement = {
|
|
60
57
|
name: lineValues[0],
|
|
61
58
|
count: parseInt(lineValues[1], 10),
|
|
62
59
|
properties: []
|
|
63
60
|
};
|
|
64
61
|
break;
|
|
65
|
-
|
|
66
62
|
case 'property':
|
|
67
63
|
const property = makePLYElementProperty(lineValues, options.propertyNameMapping);
|
|
68
64
|
currentElement.properties.push(property);
|
|
69
65
|
break;
|
|
70
|
-
|
|
71
66
|
default:
|
|
72
67
|
console.log('unhandled', lineType, lineValues);
|
|
73
68
|
}
|
|
74
|
-
|
|
75
69
|
return false;
|
|
76
70
|
});
|
|
77
|
-
|
|
78
71
|
if (currentElement) {
|
|
79
72
|
header.elements.push(currentElement);
|
|
80
73
|
}
|
|
81
|
-
|
|
82
74
|
return header;
|
|
83
75
|
}
|
|
84
|
-
|
|
85
76
|
function makePLYElementProperty(propertValues, propertyNameMapping) {
|
|
86
77
|
const property = {
|
|
87
78
|
type: propertValues[0]
|
|
88
79
|
};
|
|
89
|
-
|
|
90
80
|
if (property.type === 'list') {
|
|
91
81
|
property.name = propertValues[3];
|
|
92
82
|
property.countType = propertValues[1];
|
|
@@ -94,11 +84,9 @@ function makePLYElementProperty(propertValues, propertyNameMapping) {
|
|
|
94
84
|
} else {
|
|
95
85
|
property.name = propertValues[1];
|
|
96
86
|
}
|
|
97
|
-
|
|
98
87
|
if (propertyNameMapping && property.name in propertyNameMapping) {
|
|
99
88
|
property.name = propertyNameMapping[property.name];
|
|
100
89
|
}
|
|
101
|
-
|
|
102
90
|
return property;
|
|
103
91
|
}
|
|
104
92
|
|
|
@@ -112,25 +100,20 @@ async function parseASCII(lineIterator, header) {
|
|
|
112
100
|
};
|
|
113
101
|
let currentElement = 0;
|
|
114
102
|
let currentElementCount = 0;
|
|
115
|
-
|
|
116
103
|
for await (let line of lineIterator) {
|
|
117
104
|
line = line.trim();
|
|
118
|
-
|
|
119
105
|
if (line !== '') {
|
|
120
106
|
if (currentElementCount >= header.elements[currentElement].count) {
|
|
121
107
|
currentElement++;
|
|
122
108
|
currentElementCount = 0;
|
|
123
109
|
}
|
|
124
|
-
|
|
125
110
|
const element = parseASCIIElement(header.elements[currentElement].properties, line);
|
|
126
111
|
handleElement(attributes, header.elements[currentElement].name, element);
|
|
127
112
|
currentElementCount++;
|
|
128
113
|
}
|
|
129
114
|
}
|
|
130
|
-
|
|
131
115
|
return attributes;
|
|
132
116
|
}
|
|
133
|
-
|
|
134
117
|
function parseASCIINumber(n, type) {
|
|
135
118
|
switch (type) {
|
|
136
119
|
case 'char':
|
|
@@ -146,71 +129,56 @@ function parseASCIINumber(n, type) {
|
|
|
146
129
|
case 'int32':
|
|
147
130
|
case 'uint32':
|
|
148
131
|
return parseInt(n, 10);
|
|
149
|
-
|
|
150
132
|
case 'float':
|
|
151
133
|
case 'double':
|
|
152
134
|
case 'float32':
|
|
153
135
|
case 'float64':
|
|
154
136
|
return parseFloat(n);
|
|
155
|
-
|
|
156
137
|
default:
|
|
157
138
|
throw new Error(type);
|
|
158
139
|
}
|
|
159
140
|
}
|
|
160
|
-
|
|
161
141
|
function parseASCIIElement(properties, line) {
|
|
162
142
|
const values = line.split(/\s+/);
|
|
163
143
|
const element = {};
|
|
164
|
-
|
|
165
144
|
for (let i = 0; i < properties.length; i++) {
|
|
166
145
|
if (properties[i].type === 'list') {
|
|
167
146
|
const list = [];
|
|
168
147
|
const n = parseASCIINumber(values.shift(), properties[i].countType);
|
|
169
|
-
|
|
170
148
|
for (let j = 0; j < n; j++) {
|
|
171
149
|
list.push(parseASCIINumber(values.shift(), properties[i].itemType));
|
|
172
150
|
}
|
|
173
|
-
|
|
174
151
|
element[properties[i].name] = list;
|
|
175
152
|
} else {
|
|
176
153
|
element[properties[i].name] = parseASCIINumber(values.shift(), properties[i].type);
|
|
177
154
|
}
|
|
178
155
|
}
|
|
179
|
-
|
|
180
156
|
return element;
|
|
181
157
|
}
|
|
182
|
-
|
|
183
|
-
|
|
158
|
+
function handleElement(buffer, elementName) {
|
|
159
|
+
let element = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
184
160
|
switch (elementName) {
|
|
185
161
|
case 'vertex':
|
|
186
162
|
buffer.vertices.push(element.x, element.y, element.z);
|
|
187
|
-
|
|
188
163
|
if ('nx' in element && 'ny' in element && 'nz' in element) {
|
|
189
164
|
buffer.normals.push(element.nx, element.ny, element.nz);
|
|
190
165
|
}
|
|
191
|
-
|
|
192
166
|
if ('s' in element && 't' in element) {
|
|
193
167
|
buffer.uvs.push(element.s, element.t);
|
|
194
168
|
}
|
|
195
|
-
|
|
196
169
|
if ('red' in element && 'green' in element && 'blue' in element) {
|
|
197
170
|
buffer.colors.push(element.red / 255.0, element.green / 255.0, element.blue / 255.0);
|
|
198
171
|
}
|
|
199
|
-
|
|
200
172
|
break;
|
|
201
|
-
|
|
202
173
|
case 'face':
|
|
203
174
|
const vertexIndices = element.vertex_indices || element.vertex_index;
|
|
204
|
-
|
|
205
175
|
if (vertexIndices.length === 3) {
|
|
206
176
|
buffer.indices.push(vertexIndices[0], vertexIndices[1], vertexIndices[2]);
|
|
207
177
|
} else if (vertexIndices.length === 4) {
|
|
208
178
|
buffer.indices.push(vertexIndices[0], vertexIndices[1], vertexIndices[3]);
|
|
209
179
|
buffer.indices.push(vertexIndices[1], vertexIndices[2], vertexIndices[3]);
|
|
210
180
|
}
|
|
211
|
-
|
|
212
181
|
break;
|
|
213
|
-
|
|
214
182
|
default:
|
|
215
183
|
break;
|
|
216
184
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/lib/parse-ply-in-batches.ts"],"names":["makeLineIterator","makeTextDecoderIterator","forEach","normalizePLY","currentElement","parsePLYInBatches","iterator","options","lineIterator","header","parsePLYHeader","attributes","format","parseASCII","Error","comments","elements","line","trim","lineValues","split","lineType","shift","join","version","push","name","count","parseInt","properties","property","makePLYElementProperty","propertyNameMapping","console","log","propertValues","type","countType","itemType","indices","vertices","normals","uvs","colors","currentElementCount","element","parseASCIIElement","handleElement","parseASCIINumber","n","parseFloat","values","i","length","list","j","buffer","elementName","x","y","z","nx","ny","nz","s","t","red","green","blue","vertexIndices","vertex_indices","vertex_index"],"mappings":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OAAO,CAACsB,KAAR,GAAgB,KAAxD,EAA+DtB,OAAO,CAACuB,IAAR,GAAe,KAA9E;AACD;;AACD;;AAEF,SAAK,MAAL;AACE,YAAMC,aAAa,GAAGxB,OAAO,CAACyB,cAAR,IAA0BzB,OAAO,CAAC0B,YAAxD;;AACA,UAAIF,aAAa,CAAChB,MAAd,KAAyB,CAA7B,EAAgC;AAC9BG,QAAAA,MAAM,CAACjB,OAAP,CAAed,IAAf,CAAoB4C,aAAa,CAAC,CAAD,CAAjC,EAAsCA,aAAa,CAAC,CAAD,CAAnD,EAAwDA,aAAa,CAAC,CAAD,CAArE;AACD,OAFD,MAEO,IAAIA,aAAa,CAAChB,MAAd,KAAyB,CAA7B,EAAgC;AACrCG,QAAAA,MAAM,CAACjB,OAAP,CAAed,IAAf,CAAoB4C,aAAa,CAAC,CAAD,CAAjC,EAAsCA,aAAa,CAAC,CAAD,CAAnD,EAAwDA,aAAa,CAAC,CAAD,CAArE;AACAb,QAAAA,MAAM,CAACjB,OAAP,CAAed,IAAf,CAAoB4C,aAAa,CAAC,CAAD,CAAjC,EAAsCA,aAAa,CAAC,CAAD,CAAnD,EAAwDA,aAAa,CAAC,CAAD,CAArE;AACD;;AACD;;AAEF;AACE;AAzBJ;AA2BD","sourcesContent":["// PLY Loader, adapted from THREE.js (MIT license)\n//\n// Attributions per original THREE.js source file:\n//\n// @author Wei Meng / http://about.me/menway\n//\n// Description: A loader for PLY ASCII files (known as the Polygon File Format\n// or the Stanford Triangle Format).\n//\n// Limitations: ASCII decoding assumes file is UTF-8.\n//\n// If the PLY file uses non standard property names, they can be mapped while\n// loading. For example, the following maps the properties\n// “diffuse_(red|green|blue)” in the file to standard color names.\n//\n// parsePLY(data, {\n// propertyNameMapping: {\n// diffuse_red: 'red',\n// diffuse_green: 'green',\n// diffuse_blue: 'blue'\n// }\n// });\n\nimport {makeLineIterator, makeTextDecoderIterator, forEach} from '@loaders.gl/loader-utils';\nimport normalizePLY from './normalize-ply';\nimport {PLYMesh, PLYHeader, ASCIIElement, PLYAttributes} from './ply-types';\n\nlet currentElement: ASCIIElement;\n\n/**\n * PARSER\n * @param iterator\n * @param options\n */\nexport default async function* parsePLYInBatches(\n iterator: AsyncIterable<ArrayBuffer> | Iterable<ArrayBuffer>,\n options: any\n): AsyncIterable<PLYMesh> {\n const lineIterator = makeLineIterator(makeTextDecoderIterator(iterator));\n const header = await parsePLYHeader(lineIterator, options);\n\n let attributes: PLYAttributes;\n switch (header.format) {\n case 'ascii':\n attributes = await parseASCII(lineIterator, header);\n break;\n default:\n throw new Error('Binary PLY can not yet be parsed in streaming mode');\n // attributes = await parseBinary(lineIterator, header);\n }\n\n yield normalizePLY(header, attributes, options);\n}\n\n/**\n * Parses header\n * @param lineIterator\n * @param options\n * @returns\n */\nasync function parsePLYHeader(\n lineIterator: AsyncIterable<string> | Iterable<string>,\n options: {[key: string]: any}\n): Promise<PLYHeader> {\n const header: PLYHeader = {\n comments: [],\n elements: []\n // headerLength\n };\n\n // Note: forEach does not reset iterator if exiting loop prematurely\n // so that iteration can continue in a second loop\n await forEach(lineIterator, (line: string) => {\n line = line.trim();\n\n // End of header\n if (line === 'end_header') {\n return true; // Returning true cancels `forEach`\n }\n\n // Ignore empty lines\n if (line === '') {\n // eslint-disable-next-line\n return false; // Returning false does not cancel `forEach`\n }\n\n const lineValues = line.split(/\\s+/);\n const lineType = lineValues.shift();\n line = lineValues.join(' ');\n\n switch (lineType) {\n case 'ply':\n // First line magic characters, ignore\n break;\n\n case 'format':\n header.format = lineValues[0];\n header.version = lineValues[1];\n break;\n\n case 'comment':\n header.comments.push(line);\n break;\n\n case 'element':\n if (currentElement) {\n header.elements.push(currentElement);\n }\n\n currentElement = {\n name: lineValues[0],\n count: parseInt(lineValues[1], 10),\n properties: []\n };\n break;\n\n case 'property':\n const property = makePLYElementProperty(lineValues, options.propertyNameMapping);\n currentElement.properties.push(property);\n break;\n\n default:\n // eslint-disable-next-line\n console.log('unhandled', lineType, lineValues);\n }\n\n return false;\n });\n\n if (currentElement) {\n header.elements.push(currentElement);\n }\n\n return header;\n}\n\nfunction makePLYElementProperty(propertValues: string[], propertyNameMapping: []) {\n const property: {[index: string]: string} = {\n type: propertValues[0]\n };\n\n if (property.type === 'list') {\n property.name = propertValues[3];\n property.countType = propertValues[1];\n property.itemType = propertValues[2];\n } else {\n property.name = propertValues[1];\n }\n\n if (propertyNameMapping && property.name in propertyNameMapping) {\n property.name = propertyNameMapping[property.name];\n }\n\n return property;\n}\n\n// ASCII PARSING\n/**\n * @param lineIterator\n * @param header\n * @returns\n */\nasync function parseASCII(lineIterator: AsyncIterable<string>, header: PLYHeader) {\n // PLY ascii format specification, as per http://en.wikipedia.org/wiki/PLY_(file_format)\n const attributes: PLYAttributes = {\n indices: [],\n vertices: [],\n normals: [],\n uvs: [],\n colors: []\n };\n\n let currentElement = 0;\n let currentElementCount = 0;\n\n for await (let line of lineIterator) {\n line = line.trim();\n\n if (line !== '') {\n if (currentElementCount >= header.elements[currentElement].count) {\n currentElement++;\n currentElementCount = 0;\n }\n\n const element = parseASCIIElement(header.elements[currentElement].properties, line);\n handleElement(attributes, header.elements[currentElement].name, element);\n currentElementCount++;\n }\n }\n\n return attributes;\n}\n/**\n * Parses ASCII number\n * @param n\n * @param type\n * @returns ASCII number\n */\n// eslint-disable-next-line complexity\nfunction parseASCIINumber(n: string, type: string): number {\n switch (type) {\n case 'char':\n case 'uchar':\n case 'short':\n case 'ushort':\n case 'int':\n case 'uint':\n case 'int8':\n case 'uint8':\n case 'int16':\n case 'uint16':\n case 'int32':\n case 'uint32':\n return parseInt(n, 10);\n\n case 'float':\n case 'double':\n case 'float32':\n case 'float64':\n return parseFloat(n);\n\n default:\n throw new Error(type);\n }\n}\n/**\n * Parses ASCII element\n * @param properties\n * @param line\n * @returns element\n */\nfunction parseASCIIElement(properties: any[], line: string) {\n const values: any = line.split(/\\s+/);\n\n const element = {};\n\n for (let i = 0; i < properties.length; i++) {\n if (properties[i].type === 'list') {\n const list: any = [];\n const n = parseASCIINumber(values.shift(), properties[i].countType);\n\n for (let j = 0; j < n; j++) {\n list.push(parseASCIINumber(values.shift(), properties[i].itemType));\n }\n\n element[properties[i].name] = list;\n } else {\n element[properties[i].name] = parseASCIINumber(values.shift(), properties[i].type);\n }\n }\n\n return element;\n}\n/**\n * @param buffer\n * @param elementName\n * @param element\n */\n// HELPER FUNCTIONS\n// eslint-disable-next-line complexity\nfunction handleElement(\n buffer: {[index: string]: number[]},\n elementName: string,\n element: any = {}\n) {\n switch (elementName) {\n case 'vertex':\n buffer.vertices.push(element.x, element.y, element.z);\n if ('nx' in element && 'ny' in element && 'nz' in element) {\n buffer.normals.push(element.nx, element.ny, element.nz);\n }\n if ('s' in element && 't' in element) {\n buffer.uvs.push(element.s, element.t);\n }\n if ('red' in element && 'green' in element && 'blue' in element) {\n buffer.colors.push(element.red / 255.0, element.green / 255.0, element.blue / 255.0);\n }\n break;\n\n case 'face':\n const vertexIndices = element.vertex_indices || element.vertex_index; // issue #9338\n if (vertexIndices.length === 3) {\n buffer.indices.push(vertexIndices[0], vertexIndices[1], vertexIndices[2]);\n } else if (vertexIndices.length === 4) {\n buffer.indices.push(vertexIndices[0], vertexIndices[1], vertexIndices[3]);\n buffer.indices.push(vertexIndices[1], vertexIndices[2], vertexIndices[3]);\n }\n break;\n\n default:\n break;\n }\n}\n"],"file":"parse-ply-in-batches.js"}
|
|
1
|
+
{"version":3,"file":"parse-ply-in-batches.js","names":["makeLineIterator","makeTextDecoderIterator","forEach","normalizePLY","currentElement","parsePLYInBatches","iterator","options","lineIterator","header","parsePLYHeader","attributes","format","parseASCII","Error","comments","elements","line","trim","lineValues","split","lineType","shift","join","version","push","name","count","parseInt","properties","property","makePLYElementProperty","propertyNameMapping","console","log","propertValues","type","countType","itemType","indices","vertices","normals","uvs","colors","currentElementCount","element","parseASCIIElement","handleElement","parseASCIINumber","n","parseFloat","values","i","length","list","j","buffer","elementName","x","y","z","nx","ny","nz","s","t","red","green","blue","vertexIndices","vertex_indices","vertex_index"],"sources":["../../../src/lib/parse-ply-in-batches.ts"],"sourcesContent":["// PLY Loader, adapted from THREE.js (MIT license)\n//\n// Attributions per original THREE.js source file:\n//\n// @author Wei Meng / http://about.me/menway\n//\n// Description: A loader for PLY ASCII files (known as the Polygon File Format\n// or the Stanford Triangle Format).\n//\n// Limitations: ASCII decoding assumes file is UTF-8.\n//\n// If the PLY file uses non standard property names, they can be mapped while\n// loading. For example, the following maps the properties\n// “diffuse_(red|green|blue)” in the file to standard color names.\n//\n// parsePLY(data, {\n// propertyNameMapping: {\n// diffuse_red: 'red',\n// diffuse_green: 'green',\n// diffuse_blue: 'blue'\n// }\n// });\n\nimport {makeLineIterator, makeTextDecoderIterator, forEach} from '@loaders.gl/loader-utils';\nimport normalizePLY from './normalize-ply';\nimport {PLYMesh, PLYHeader, ASCIIElement, PLYAttributes} from './ply-types';\n\nlet currentElement: ASCIIElement;\n\n/**\n * PARSER\n * @param iterator\n * @param options\n */\nexport default async function* parsePLYInBatches(\n iterator: AsyncIterable<ArrayBuffer> | Iterable<ArrayBuffer>,\n options: any\n): AsyncIterable<PLYMesh> {\n const lineIterator = makeLineIterator(makeTextDecoderIterator(iterator));\n const header = await parsePLYHeader(lineIterator, options);\n\n let attributes: PLYAttributes;\n switch (header.format) {\n case 'ascii':\n attributes = await parseASCII(lineIterator, header);\n break;\n default:\n throw new Error('Binary PLY can not yet be parsed in streaming mode');\n // attributes = await parseBinary(lineIterator, header);\n }\n\n yield normalizePLY(header, attributes, options);\n}\n\n/**\n * Parses header\n * @param lineIterator\n * @param options\n * @returns\n */\nasync function parsePLYHeader(\n lineIterator: AsyncIterable<string> | Iterable<string>,\n options: {[key: string]: any}\n): Promise<PLYHeader> {\n const header: PLYHeader = {\n comments: [],\n elements: []\n // headerLength\n };\n\n // Note: forEach does not reset iterator if exiting loop prematurely\n // so that iteration can continue in a second loop\n await forEach(lineIterator, (line: string) => {\n line = line.trim();\n\n // End of header\n if (line === 'end_header') {\n return true; // Returning true cancels `forEach`\n }\n\n // Ignore empty lines\n if (line === '') {\n // eslint-disable-next-line\n return false; // Returning false does not cancel `forEach`\n }\n\n const lineValues = line.split(/\\s+/);\n const lineType = lineValues.shift();\n line = lineValues.join(' ');\n\n switch (lineType) {\n case 'ply':\n // First line magic characters, ignore\n break;\n\n case 'format':\n header.format = lineValues[0];\n header.version = lineValues[1];\n break;\n\n case 'comment':\n header.comments.push(line);\n break;\n\n case 'element':\n if (currentElement) {\n header.elements.push(currentElement);\n }\n\n currentElement = {\n name: lineValues[0],\n count: parseInt(lineValues[1], 10),\n properties: []\n };\n break;\n\n case 'property':\n const property = makePLYElementProperty(lineValues, options.propertyNameMapping);\n currentElement.properties.push(property);\n break;\n\n default:\n // eslint-disable-next-line\n console.log('unhandled', lineType, lineValues);\n }\n\n return false;\n });\n\n if (currentElement) {\n header.elements.push(currentElement);\n }\n\n return header;\n}\n\nfunction makePLYElementProperty(propertValues: string[], propertyNameMapping: []) {\n const property: {[index: string]: string} = {\n type: propertValues[0]\n };\n\n if (property.type === 'list') {\n property.name = propertValues[3];\n property.countType = propertValues[1];\n property.itemType = propertValues[2];\n } else {\n property.name = propertValues[1];\n }\n\n if (propertyNameMapping && property.name in propertyNameMapping) {\n property.name = propertyNameMapping[property.name];\n }\n\n return property;\n}\n\n// ASCII PARSING\n/**\n * @param lineIterator\n * @param header\n * @returns\n */\nasync function parseASCII(lineIterator: AsyncIterable<string>, header: PLYHeader) {\n // PLY ascii format specification, as per http://en.wikipedia.org/wiki/PLY_(file_format)\n const attributes: PLYAttributes = {\n indices: [],\n vertices: [],\n normals: [],\n uvs: [],\n colors: []\n };\n\n let currentElement = 0;\n let currentElementCount = 0;\n\n for await (let line of lineIterator) {\n line = line.trim();\n\n if (line !== '') {\n if (currentElementCount >= header.elements[currentElement].count) {\n currentElement++;\n currentElementCount = 0;\n }\n\n const element = parseASCIIElement(header.elements[currentElement].properties, line);\n handleElement(attributes, header.elements[currentElement].name, element);\n currentElementCount++;\n }\n }\n\n return attributes;\n}\n/**\n * Parses ASCII number\n * @param n\n * @param type\n * @returns ASCII number\n */\n// eslint-disable-next-line complexity\nfunction parseASCIINumber(n: string, type: string): number {\n switch (type) {\n case 'char':\n case 'uchar':\n case 'short':\n case 'ushort':\n case 'int':\n case 'uint':\n case 'int8':\n case 'uint8':\n case 'int16':\n case 'uint16':\n case 'int32':\n case 'uint32':\n return parseInt(n, 10);\n\n case 'float':\n case 'double':\n case 'float32':\n case 'float64':\n return parseFloat(n);\n\n default:\n throw new Error(type);\n }\n}\n/**\n * Parses ASCII element\n * @param properties\n * @param line\n * @returns element\n */\nfunction parseASCIIElement(properties: any[], line: string) {\n const values: any = line.split(/\\s+/);\n\n const element = {};\n\n for (let i = 0; i < properties.length; i++) {\n if (properties[i].type === 'list') {\n const list: any = [];\n const n = parseASCIINumber(values.shift(), properties[i].countType);\n\n for (let j = 0; j < n; j++) {\n list.push(parseASCIINumber(values.shift(), properties[i].itemType));\n }\n\n element[properties[i].name] = list;\n } else {\n element[properties[i].name] = parseASCIINumber(values.shift(), properties[i].type);\n }\n }\n\n return element;\n}\n/**\n * @param buffer\n * @param elementName\n * @param element\n */\n// HELPER FUNCTIONS\n// eslint-disable-next-line complexity\nfunction handleElement(\n buffer: {[index: string]: number[]},\n elementName: string,\n element: any = {}\n) {\n switch (elementName) {\n case 'vertex':\n buffer.vertices.push(element.x, element.y, element.z);\n if ('nx' in element && 'ny' in element && 'nz' in element) {\n buffer.normals.push(element.nx, element.ny, element.nz);\n }\n if ('s' in element && 't' in element) {\n buffer.uvs.push(element.s, element.t);\n }\n if ('red' in element && 'green' in element && 'blue' in element) {\n buffer.colors.push(element.red / 255.0, element.green / 255.0, element.blue / 255.0);\n }\n break;\n\n case 'face':\n const vertexIndices = element.vertex_indices || element.vertex_index; // issue #9338\n if (vertexIndices.length === 3) {\n buffer.indices.push(vertexIndices[0], vertexIndices[1], vertexIndices[2]);\n } else if (vertexIndices.length === 4) {\n buffer.indices.push(vertexIndices[0], vertexIndices[1], vertexIndices[3]);\n buffer.indices.push(vertexIndices[1], vertexIndices[2], vertexIndices[3]);\n }\n break;\n\n default:\n break;\n }\n}\n"],"mappings":";;AAuBA,SAAQA,gBAAgB,EAAEC,uBAAuB,EAAEC,OAAO,QAAO,0BAA0B;AAC3F,OAAOC,YAAY,MAAM,iBAAiB;AAG1C,IAAIC,cAA4B;;AAOhC,eAAe,gBAAgBC,iBAAiB,CAC9CC,QAA4D,EAC5DC,OAAY,EACY;EACxB,MAAMC,YAAY,GAAGR,gBAAgB,CAACC,uBAAuB,CAACK,QAAQ,CAAC,CAAC;EACxE,MAAMG,MAAM,GAAG,MAAMC,cAAc,CAACF,YAAY,EAAED,OAAO,CAAC;EAE1D,IAAII,UAAyB;EAC7B,QAAQF,MAAM,CAACG,MAAM;IACnB,KAAK,OAAO;MACVD,UAAU,GAAG,MAAME,UAAU,CAACL,YAAY,EAAEC,MAAM,CAAC;MACnD;IACF;MACE,MAAM,IAAIK,KAAK,CAAC,oDAAoD,CAAC;EAAC;;EAI1E,MAAMX,YAAY,CAACM,MAAM,EAAEE,UAAU,EAAEJ,OAAO,CAAC;AACjD;;AAQA,eAAeG,cAAc,CAC3BF,YAAsD,EACtDD,OAA6B,EACT;EACpB,MAAME,MAAiB,GAAG;IACxBM,QAAQ,EAAE,EAAE;IACZC,QAAQ,EAAE;EAEZ,CAAC;;EAID,MAAMd,OAAO,CAACM,YAAY,EAAGS,IAAY,IAAK;IAC5CA,IAAI,GAAGA,IAAI,CAACC,IAAI,EAAE;;IAGlB,IAAID,IAAI,KAAK,YAAY,EAAE;MACzB,OAAO,IAAI;IACb;;IAGA,IAAIA,IAAI,KAAK,EAAE,EAAE;MAEf,OAAO,KAAK;IACd;;IAEA,MAAME,UAAU,GAAGF,IAAI,CAACG,KAAK,CAAC,KAAK,CAAC;IACpC,MAAMC,QAAQ,GAAGF,UAAU,CAACG,KAAK,EAAE;IACnCL,IAAI,GAAGE,UAAU,CAACI,IAAI,CAAC,GAA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AAIF,aAAa,CAAChB,MAAM,KAAK,CAAC,EAAE;QAC9BG,MAAM,CAACjB,OAAO,CAACd,IAAI,CAAC4C,aAAa,CAAC,CAAC,CAAC,EAAEA,aAAa,CAAC,CAAC,CAAC,EAAEA,aAAa,CAAC,CAAC,CAAC,CAAC;MAC3E,CAAC,MAAM,IAAIA,aAAa,CAAChB,MAAM,KAAK,CAAC,EAAE;QACrCG,MAAM,CAACjB,OAAO,CAACd,IAAI,CAAC4C,aAAa,CAAC,CAAC,CAAC,EAAEA,aAAa,CAAC,CAAC,CAAC,EAAEA,aAAa,CAAC,CAAC,CAAC,CAAC;QACzEb,MAAM,CAACjB,OAAO,CAACd,IAAI,CAAC4C,aAAa,CAAC,CAAC,CAAC,EAAEA,aAAa,CAAC,CAAC,CAAC,EAAEA,aAAa,CAAC,CAAC,CAAC,CAAC;MAC3E;MACA;IAEF;MACE;EAAM;AAEZ"}
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
1
3
|
import normalizePLY from './normalize-ply';
|
|
2
|
-
|
|
4
|
+
|
|
5
|
+
export default function parsePLY(data) {
|
|
6
|
+
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
3
7
|
let header;
|
|
4
8
|
let attributes;
|
|
5
|
-
|
|
6
9
|
if (data instanceof ArrayBuffer) {
|
|
7
10
|
const text = new TextDecoder().decode(data);
|
|
8
11
|
header = parseHeader(text, options);
|
|
@@ -11,7 +14,6 @@ export default function parsePLY(data, options = {}) {
|
|
|
11
14
|
header = parseHeader(data, options);
|
|
12
15
|
attributes = parseASCII(data, header);
|
|
13
16
|
}
|
|
14
|
-
|
|
15
17
|
return normalizePLY(header, attributes);
|
|
16
18
|
}
|
|
17
19
|
|
|
@@ -20,12 +22,10 @@ function parseHeader(data, options) {
|
|
|
20
22
|
let headerText = '';
|
|
21
23
|
let headerLength = 0;
|
|
22
24
|
const result = PLY_HEADER_PATTERN.exec(data);
|
|
23
|
-
|
|
24
25
|
if (result !== null) {
|
|
25
26
|
headerText = result[1];
|
|
26
27
|
headerLength = result[0].length;
|
|
27
28
|
}
|
|
28
|
-
|
|
29
29
|
const lines = headerText.split('\n');
|
|
30
30
|
const header = parseHeaderLines(lines, headerLength, options);
|
|
31
31
|
return header;
|
|
@@ -40,58 +40,46 @@ function parseHeaderLines(lines, headerLength, options) {
|
|
|
40
40
|
let lineType;
|
|
41
41
|
let lineValues;
|
|
42
42
|
let currentElement = null;
|
|
43
|
-
|
|
44
43
|
for (let i = 0; i < lines.length; i++) {
|
|
45
44
|
let line = lines[i];
|
|
46
45
|
line = line.trim();
|
|
47
|
-
|
|
48
46
|
if (line === '') {
|
|
49
47
|
continue;
|
|
50
48
|
}
|
|
51
|
-
|
|
52
49
|
lineValues = line.split(/\s+/);
|
|
53
50
|
lineType = lineValues.shift();
|
|
54
51
|
line = lineValues.join(' ');
|
|
55
|
-
|
|
56
52
|
switch (lineType) {
|
|
57
53
|
case 'format':
|
|
58
54
|
header.format = lineValues[0];
|
|
59
55
|
header.version = lineValues[1];
|
|
60
56
|
break;
|
|
61
|
-
|
|
62
57
|
case 'comment':
|
|
63
58
|
header.comments.push(line);
|
|
64
59
|
break;
|
|
65
|
-
|
|
66
60
|
case 'element':
|
|
67
61
|
if (currentElement) {
|
|
68
62
|
header.elements.push(currentElement);
|
|
69
63
|
}
|
|
70
|
-
|
|
71
64
|
currentElement = {
|
|
72
65
|
name: lineValues[0],
|
|
73
66
|
count: parseInt(lineValues[1], 10),
|
|
74
67
|
properties: []
|
|
75
68
|
};
|
|
76
69
|
break;
|
|
77
|
-
|
|
78
70
|
case 'property':
|
|
79
71
|
if (!currentElement) {
|
|
80
72
|
break;
|
|
81
73
|
}
|
|
82
|
-
|
|
83
74
|
currentElement.properties.push(makePLYElementProperty(lineValues, options.propertyNameMapping));
|
|
84
75
|
break;
|
|
85
|
-
|
|
86
76
|
default:
|
|
87
77
|
console.log('unhandled', lineType, lineValues);
|
|
88
78
|
}
|
|
89
79
|
}
|
|
90
|
-
|
|
91
80
|
if (currentElement !== undefined) {
|
|
92
81
|
header.elements.push(currentElement);
|
|
93
82
|
}
|
|
94
|
-
|
|
95
83
|
return header;
|
|
96
84
|
}
|
|
97
85
|
|
|
@@ -99,7 +87,6 @@ function makePLYElementProperty(propertValues, propertyNameMapping) {
|
|
|
99
87
|
const property = {
|
|
100
88
|
type: propertValues[0]
|
|
101
89
|
};
|
|
102
|
-
|
|
103
90
|
if (property.type === 'list') {
|
|
104
91
|
property.name = propertValues[3];
|
|
105
92
|
property.countType = propertValues[1];
|
|
@@ -107,11 +94,9 @@ function makePLYElementProperty(propertValues, propertyNameMapping) {
|
|
|
107
94
|
} else {
|
|
108
95
|
property.name = propertValues[1];
|
|
109
96
|
}
|
|
110
|
-
|
|
111
97
|
if (propertyNameMapping && property.name in propertyNameMapping) {
|
|
112
98
|
property.name = propertyNameMapping[property.name];
|
|
113
99
|
}
|
|
114
|
-
|
|
115
100
|
return property;
|
|
116
101
|
}
|
|
117
102
|
|
|
@@ -130,13 +115,11 @@ function parseASCIINumber(n, type) {
|
|
|
130
115
|
case 'int32':
|
|
131
116
|
case 'uint32':
|
|
132
117
|
return parseInt(n, 10);
|
|
133
|
-
|
|
134
118
|
case 'float':
|
|
135
119
|
case 'double':
|
|
136
120
|
case 'float32':
|
|
137
121
|
case 'float64':
|
|
138
122
|
return parseFloat(n);
|
|
139
|
-
|
|
140
123
|
default:
|
|
141
124
|
throw new Error(type);
|
|
142
125
|
}
|
|
@@ -145,26 +128,23 @@ function parseASCIINumber(n, type) {
|
|
|
145
128
|
function parseASCIIElement(properties, line) {
|
|
146
129
|
const values = line.split(/\s+/);
|
|
147
130
|
const element = {};
|
|
148
|
-
|
|
149
131
|
for (let i = 0; i < properties.length; i++) {
|
|
150
132
|
if (properties[i].type === 'list') {
|
|
151
133
|
const list = [];
|
|
152
134
|
const n = parseASCIINumber(values.shift(), properties[i].countType);
|
|
153
|
-
|
|
154
135
|
for (let j = 0; j < n; j++) {
|
|
155
136
|
list.push(parseASCIINumber(values.shift(), properties[i].itemType));
|
|
156
137
|
}
|
|
157
|
-
|
|
158
138
|
element[properties[i].name] = list;
|
|
159
139
|
} else {
|
|
160
140
|
element[properties[i].name] = parseASCIINumber(values.shift(), properties[i].type);
|
|
161
141
|
}
|
|
162
142
|
}
|
|
163
|
-
|
|
164
143
|
return element;
|
|
165
144
|
}
|
|
166
145
|
|
|
167
146
|
function parseASCII(data, header) {
|
|
147
|
+
|
|
168
148
|
const attributes = {
|
|
169
149
|
indices: [],
|
|
170
150
|
vertices: [],
|
|
@@ -175,46 +155,38 @@ function parseASCII(data, header) {
|
|
|
175
155
|
let result;
|
|
176
156
|
const patternBody = /end_header\s([\s\S]*)$/;
|
|
177
157
|
let body = '';
|
|
178
|
-
|
|
179
158
|
if ((result = patternBody.exec(data)) !== null) {
|
|
180
159
|
body = result[1];
|
|
181
160
|
}
|
|
182
|
-
|
|
183
161
|
const lines = body.split('\n');
|
|
184
162
|
let currentElement = 0;
|
|
185
163
|
let currentElementCount = 0;
|
|
186
|
-
|
|
187
164
|
for (let i = 0; i < lines.length; i++) {
|
|
188
165
|
let line = lines[i];
|
|
189
166
|
line = line.trim();
|
|
190
|
-
|
|
191
167
|
if (line !== '') {
|
|
192
168
|
if (currentElementCount >= header.elements[currentElement].count) {
|
|
193
169
|
currentElement++;
|
|
194
170
|
currentElementCount = 0;
|
|
195
171
|
}
|
|
196
|
-
|
|
197
172
|
const element = parseASCIIElement(header.elements[currentElement].properties, line);
|
|
198
173
|
handleElement(attributes, header.elements[currentElement].name, element);
|
|
199
174
|
currentElementCount++;
|
|
200
175
|
}
|
|
201
176
|
}
|
|
202
|
-
|
|
203
177
|
return attributes;
|
|
204
178
|
}
|
|
205
179
|
|
|
206
|
-
function handleElement(buffer, elementName
|
|
180
|
+
function handleElement(buffer, elementName) {
|
|
181
|
+
let element = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
207
182
|
if (elementName === 'vertex') {
|
|
208
183
|
buffer.vertices.push(element.x, element.y, element.z);
|
|
209
|
-
|
|
210
184
|
if ('nx' in element && 'ny' in element && 'nz' in element) {
|
|
211
185
|
buffer.normals.push(element.nx, element.ny, element.nz);
|
|
212
186
|
}
|
|
213
|
-
|
|
214
187
|
if ('s' in element && 't' in element) {
|
|
215
188
|
buffer.uvs.push(element.s, element.t);
|
|
216
189
|
}
|
|
217
|
-
|
|
218
190
|
if ('red' in element && 'green' in element && 'blue' in element) {
|
|
219
191
|
buffer.colors.push(element.red, element.green, element.blue);
|
|
220
192
|
}
|
|
@@ -235,35 +207,27 @@ function binaryRead(dataview, at, type, littleEndian) {
|
|
|
235
207
|
case 'int8':
|
|
236
208
|
case 'char':
|
|
237
209
|
return [dataview.getInt8(at), 1];
|
|
238
|
-
|
|
239
210
|
case 'uint8':
|
|
240
211
|
case 'uchar':
|
|
241
212
|
return [dataview.getUint8(at), 1];
|
|
242
|
-
|
|
243
213
|
case 'int16':
|
|
244
214
|
case 'short':
|
|
245
215
|
return [dataview.getInt16(at, littleEndian), 2];
|
|
246
|
-
|
|
247
216
|
case 'uint16':
|
|
248
217
|
case 'ushort':
|
|
249
218
|
return [dataview.getUint16(at, littleEndian), 2];
|
|
250
|
-
|
|
251
219
|
case 'int32':
|
|
252
220
|
case 'int':
|
|
253
221
|
return [dataview.getInt32(at, littleEndian), 4];
|
|
254
|
-
|
|
255
222
|
case 'uint32':
|
|
256
223
|
case 'uint':
|
|
257
224
|
return [dataview.getUint32(at, littleEndian), 4];
|
|
258
|
-
|
|
259
225
|
case 'float32':
|
|
260
226
|
case 'float':
|
|
261
227
|
return [dataview.getFloat32(at, littleEndian), 4];
|
|
262
|
-
|
|
263
228
|
case 'float64':
|
|
264
229
|
case 'double':
|
|
265
230
|
return [dataview.getFloat64(at, littleEndian), 8];
|
|
266
|
-
|
|
267
231
|
default:
|
|
268
232
|
throw new Error(type);
|
|
269
233
|
}
|
|
@@ -273,20 +237,17 @@ function binaryReadElement(dataview, at, properties, littleEndian) {
|
|
|
273
237
|
const element = {};
|
|
274
238
|
let result;
|
|
275
239
|
let read = 0;
|
|
276
|
-
|
|
277
240
|
for (let i = 0; i < properties.length; i++) {
|
|
278
241
|
if (properties[i].type === 'list') {
|
|
279
242
|
const list = [];
|
|
280
243
|
result = binaryRead(dataview, at + read, properties[i].countType, littleEndian);
|
|
281
244
|
const n = result[0];
|
|
282
245
|
read += result[1];
|
|
283
|
-
|
|
284
246
|
for (let j = 0; j < n; j++) {
|
|
285
247
|
result = binaryRead(dataview, at + read, properties[i].itemType, littleEndian);
|
|
286
248
|
list.push(result[0]);
|
|
287
249
|
read += result[1];
|
|
288
250
|
}
|
|
289
|
-
|
|
290
251
|
element[properties[i].name] = list;
|
|
291
252
|
} else {
|
|
292
253
|
result = binaryRead(dataview, at + read, properties[i].type, littleEndian);
|
|
@@ -294,10 +255,8 @@ function binaryReadElement(dataview, at, properties, littleEndian) {
|
|
|
294
255
|
read += result[1];
|
|
295
256
|
}
|
|
296
257
|
}
|
|
297
|
-
|
|
298
258
|
return [element, read];
|
|
299
259
|
}
|
|
300
|
-
|
|
301
260
|
function parseBinary(data, header) {
|
|
302
261
|
const attributes = {
|
|
303
262
|
indices: [],
|
|
@@ -310,10 +269,8 @@ function parseBinary(data, header) {
|
|
|
310
269
|
const body = new DataView(data, header.headerLength);
|
|
311
270
|
let result;
|
|
312
271
|
let loc = 0;
|
|
313
|
-
|
|
314
272
|
for (let currentElement = 0; currentElement < header.elements.length; currentElement++) {
|
|
315
273
|
const count = header.elements[currentElement].count;
|
|
316
|
-
|
|
317
274
|
for (let currentElementCount = 0; currentElementCount < count; currentElementCount++) {
|
|
318
275
|
result = binaryReadElement(body, loc, header.elements[currentElement].properties, littleEndian);
|
|
319
276
|
loc += result[1];
|
|
@@ -321,7 +278,6 @@ function parseBinary(data, header) {
|
|
|
321
278
|
handleElement(attributes, header.elements[currentElement].name, element);
|
|
322
279
|
}
|
|
323
280
|
}
|
|
324
|
-
|
|
325
281
|
return attributes;
|
|
326
282
|
}
|
|
327
283
|
//# sourceMappingURL=parse-ply.js.map
|