@loaders.gl/ply 4.0.0-alpha.4 → 4.0.0-alpha.6

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.
Files changed (82) hide show
  1. package/dist/bundle.d.ts +2 -0
  2. package/dist/bundle.d.ts.map +1 -0
  3. package/dist/bundle.js +2 -2
  4. package/dist/dist.min.js +833 -0
  5. package/dist/es5/bundle.js +6 -0
  6. package/dist/es5/bundle.js.map +1 -0
  7. package/dist/es5/index.js +51 -0
  8. package/dist/es5/index.js.map +1 -0
  9. package/dist/es5/lib/get-ply-schema.js +28 -0
  10. package/dist/es5/lib/get-ply-schema.js.map +1 -0
  11. package/dist/es5/lib/normalize-ply.js +93 -0
  12. package/dist/es5/lib/normalize-ply.js.map +1 -0
  13. package/dist/es5/lib/parse-ply-in-batches.js +289 -0
  14. package/dist/es5/lib/parse-ply-in-batches.js.map +1 -0
  15. package/dist/es5/lib/parse-ply.js +348 -0
  16. package/dist/es5/lib/parse-ply.js.map +1 -0
  17. package/dist/es5/lib/ply-types.js +2 -0
  18. package/dist/es5/lib/ply-types.js.map +1 -0
  19. package/dist/es5/ply-loader.js +26 -0
  20. package/dist/es5/ply-loader.js.map +1 -0
  21. package/dist/es5/workers/ply-worker.js +6 -0
  22. package/dist/es5/workers/ply-worker.js.map +1 -0
  23. package/dist/esm/bundle.js +4 -0
  24. package/dist/esm/bundle.js.map +1 -0
  25. package/dist/esm/index.js +12 -0
  26. package/dist/esm/index.js.map +1 -0
  27. package/dist/esm/lib/get-ply-schema.js +22 -0
  28. package/dist/esm/lib/get-ply-schema.js.map +1 -0
  29. package/dist/esm/lib/normalize-ply.js +86 -0
  30. package/dist/esm/lib/normalize-ply.js.map +1 -0
  31. package/dist/esm/lib/parse-ply-in-batches.js +177 -0
  32. package/dist/esm/lib/parse-ply-in-batches.js.map +1 -0
  33. package/dist/esm/lib/parse-ply.js +319 -0
  34. package/dist/esm/lib/parse-ply.js.map +1 -0
  35. package/dist/esm/lib/ply-types.js +2 -0
  36. package/dist/esm/lib/ply-types.js.map +1 -0
  37. package/dist/esm/ply-loader.js +18 -0
  38. package/dist/esm/ply-loader.js.map +1 -0
  39. package/dist/esm/workers/ply-worker.js +4 -0
  40. package/dist/esm/workers/ply-worker.js.map +1 -0
  41. package/dist/index.d.ts +13 -0
  42. package/dist/index.d.ts.map +1 -0
  43. package/dist/index.js +18 -11
  44. package/dist/lib/get-ply-schema.d.ts +10 -0
  45. package/dist/lib/get-ply-schema.d.ts.map +1 -0
  46. package/dist/lib/get-ply-schema.js +34 -24
  47. package/dist/lib/normalize-ply.d.ts +8 -0
  48. package/dist/lib/normalize-ply.d.ts.map +1 -0
  49. package/dist/lib/normalize-ply.js +76 -65
  50. package/dist/lib/parse-ply-in-batches.d.ts +8 -0
  51. package/dist/lib/parse-ply-in-batches.d.ts.map +1 -0
  52. package/dist/lib/parse-ply-in-batches.js +237 -202
  53. package/dist/lib/parse-ply.d.ts +11 -0
  54. package/dist/lib/parse-ply.d.ts.map +1 -0
  55. package/dist/lib/parse-ply.js +378 -299
  56. package/dist/lib/ply-types.d.ts +37 -0
  57. package/dist/lib/ply-types.d.ts.map +1 -0
  58. package/dist/lib/ply-types.js +2 -2
  59. package/dist/ply-loader.d.ts +10 -0
  60. package/dist/ply-loader.d.ts.map +1 -0
  61. package/dist/ply-loader.js +27 -18
  62. package/dist/ply-worker.js +240 -578
  63. package/dist/workers/ply-worker.d.ts +2 -0
  64. package/dist/workers/ply-worker.d.ts.map +1 -0
  65. package/dist/workers/ply-worker.js +5 -4
  66. package/package.json +9 -12
  67. package/src/index.ts +15 -10
  68. package/src/lib/get-ply-schema.ts +11 -10
  69. package/src/lib/normalize-ply.ts +34 -12
  70. package/src/lib/parse-ply-in-batches.ts +23 -26
  71. package/src/lib/parse-ply.ts +114 -55
  72. package/src/lib/ply-types.ts +21 -12
  73. package/src/ply-loader.ts +4 -3
  74. package/dist/bundle.js.map +0 -1
  75. package/dist/index.js.map +0 -1
  76. package/dist/lib/get-ply-schema.js.map +0 -1
  77. package/dist/lib/normalize-ply.js.map +0 -1
  78. package/dist/lib/parse-ply-in-batches.js.map +0 -1
  79. package/dist/lib/parse-ply.js.map +0 -1
  80. package/dist/lib/ply-types.js.map +0 -1
  81. package/dist/ply-loader.js.map +0 -1
  82. package/dist/workers/ply-worker.js.map +0 -1
@@ -1,218 +1,253 @@
1
- import { makeLineIterator, makeTextDecoderIterator, forEach } from '@loaders.gl/core';
2
- import normalizePLY from './normalize-ply';
1
+ "use strict";
2
+ // PLY Loader, adapted from THREE.js (MIT license)
3
+ //
4
+ // Attributions per original THREE.js source file:
5
+ //
6
+ // @author Wei Meng / http://about.me/menway
7
+ //
8
+ // Description: A loader for PLY ASCII files (known as the Polygon File Format
9
+ // or the Stanford Triangle Format).
10
+ //
11
+ // Limitations: ASCII decoding assumes file is UTF-8.
12
+ //
13
+ // If the PLY file uses non standard property names, they can be mapped while
14
+ // loading. For example, the following maps the properties
15
+ // “diffuse_(red|green|blue)” in the file to standard color names.
16
+ //
17
+ // parsePLY(data, {
18
+ // propertyNameMapping: {
19
+ // diffuse_red: 'red',
20
+ // diffuse_green: 'green',
21
+ // diffuse_blue: 'blue'
22
+ // }
23
+ // });
24
+ var __importDefault = (this && this.__importDefault) || function (mod) {
25
+ return (mod && mod.__esModule) ? mod : { "default": mod };
26
+ };
27
+ Object.defineProperty(exports, "__esModule", { value: true });
28
+ exports.parsePLYInBatches = void 0;
29
+ const loader_utils_1 = require("@loaders.gl/loader-utils");
30
+ const normalize_ply_1 = __importDefault(require("./normalize-ply"));
3
31
  let currentElement;
4
- export default async function* parsePLYInBatches(iterator, options) {
5
- const lineIterator = makeLineIterator(makeTextDecoderIterator(iterator));
6
- const header = await parsePLYHeader(lineIterator, options);
7
- let attributes;
8
-
9
- switch (header.format) {
10
- case 'ascii':
11
- attributes = await parseASCII(lineIterator, header);
12
- break;
13
-
14
- default:
15
- throw new Error('Binary PLY can not yet be parsed in streaming mode');
16
- }
17
-
18
- yield normalizePLY(header, attributes, options);
32
+ /**
33
+ * PARSER
34
+ * @param iterator
35
+ * @param options
36
+ */
37
+ async function* parsePLYInBatches(iterator, options) {
38
+ const lineIterator = (0, loader_utils_1.makeLineIterator)((0, loader_utils_1.makeTextDecoderIterator)(iterator));
39
+ const header = await parsePLYHeader(lineIterator, options);
40
+ let attributes;
41
+ switch (header.format) {
42
+ case 'ascii':
43
+ attributes = await parseASCII(lineIterator, header);
44
+ break;
45
+ default:
46
+ throw new Error('Binary PLY can not yet be parsed in streaming mode');
47
+ // attributes = await parseBinary(lineIterator, header);
48
+ }
49
+ yield (0, normalize_ply_1.default)(header, attributes, options);
19
50
  }
20
-
51
+ exports.parsePLYInBatches = parsePLYInBatches;
52
+ /**
53
+ * Parses header
54
+ * @param lineIterator
55
+ * @param options
56
+ * @returns
57
+ */
21
58
  async function parsePLYHeader(lineIterator, options) {
22
- const header = {
23
- comments: [],
24
- elements: []
25
- };
26
- await forEach(lineIterator, line => {
27
- line = line.trim();
28
-
29
- if (line === 'end_header') {
30
- return true;
31
- }
32
-
33
- if (line === '') {
34
- return false;
35
- }
36
-
37
- const lineValues = line.split(/\s+/);
38
- const lineType = lineValues.shift();
39
- line = lineValues.join(' ');
40
-
41
- switch (lineType) {
42
- case 'ply':
43
- break;
44
-
45
- case 'format':
46
- header.format = lineValues[0];
47
- header.version = lineValues[1];
48
- break;
49
-
50
- case 'comment':
51
- header.comments.push(line);
52
- break;
53
-
54
- case 'element':
55
- if (currentElement) {
56
- header.elements.push(currentElement);
59
+ const header = {
60
+ comments: [],
61
+ elements: []
62
+ // headerLength
63
+ };
64
+ // Note: forEach does not reset iterator if exiting loop prematurely
65
+ // so that iteration can continue in a second loop
66
+ await (0, loader_utils_1.forEach)(lineIterator, (line) => {
67
+ line = line.trim();
68
+ // End of header
69
+ if (line === 'end_header') {
70
+ return true; // Returning true cancels `forEach`
57
71
  }
58
-
59
- currentElement = {
60
- name: lineValues[0],
61
- count: parseInt(lineValues[1], 10),
62
- properties: []
63
- };
64
- break;
65
-
66
- case 'property':
67
- const property = makePLYElementProperty(lineValues, options.propertyNameMapping);
68
- currentElement.properties.push(property);
69
- break;
70
-
71
- default:
72
- console.log('unhandled', lineType, lineValues);
72
+ // Ignore empty lines
73
+ if (line === '') {
74
+ // eslint-disable-next-line
75
+ return false; // Returning false does not cancel `forEach`
76
+ }
77
+ const lineValues = line.split(/\s+/);
78
+ const lineType = lineValues.shift();
79
+ line = lineValues.join(' ');
80
+ switch (lineType) {
81
+ case 'ply':
82
+ // First line magic characters, ignore
83
+ break;
84
+ case 'format':
85
+ header.format = lineValues[0];
86
+ header.version = lineValues[1];
87
+ break;
88
+ case 'comment':
89
+ header.comments.push(line);
90
+ break;
91
+ case 'element':
92
+ if (currentElement) {
93
+ header.elements.push(currentElement);
94
+ }
95
+ currentElement = {
96
+ name: lineValues[0],
97
+ count: parseInt(lineValues[1], 10),
98
+ properties: []
99
+ };
100
+ break;
101
+ case 'property':
102
+ const property = makePLYElementProperty(lineValues, options.propertyNameMapping);
103
+ currentElement.properties.push(property);
104
+ break;
105
+ default:
106
+ // eslint-disable-next-line
107
+ console.log('unhandled', lineType, lineValues);
108
+ }
109
+ return false;
110
+ });
111
+ if (currentElement) {
112
+ header.elements.push(currentElement);
73
113
  }
74
-
75
- return false;
76
- });
77
-
78
- if (currentElement) {
79
- header.elements.push(currentElement);
80
- }
81
-
82
- return header;
114
+ return header;
83
115
  }
84
-
85
- function makePLYElementProperty(propertValues, propertyNameMapping) {
86
- const property = {
87
- type: propertValues[0]
88
- };
89
-
90
- if (property.type === 'list') {
91
- property.name = propertValues[3];
92
- property.countType = propertValues[1];
93
- property.itemType = propertValues[2];
94
- } else {
95
- property.name = propertValues[1];
96
- }
97
-
98
- if (propertyNameMapping && property.name in propertyNameMapping) {
99
- property.name = propertyNameMapping[property.name];
100
- }
101
-
102
- return property;
116
+ function makePLYElementProperty(propertyValues, propertyNameMapping) {
117
+ const type = propertyValues[0];
118
+ switch (type) {
119
+ case 'list':
120
+ return {
121
+ type,
122
+ name: propertyValues[3],
123
+ countType: propertyValues[1],
124
+ itemType: propertyValues[2]
125
+ };
126
+ default:
127
+ return {
128
+ type,
129
+ name: propertyValues[1]
130
+ };
131
+ }
103
132
  }
104
-
133
+ // ASCII PARSING
134
+ /**
135
+ * @param lineIterator
136
+ * @param header
137
+ * @returns
138
+ */
105
139
  async function parseASCII(lineIterator, header) {
106
- const attributes = {
107
- indices: [],
108
- vertices: [],
109
- normals: [],
110
- uvs: [],
111
- colors: []
112
- };
113
- let currentElement = 0;
114
- let currentElementCount = 0;
115
-
116
- for await (let line of lineIterator) {
117
- line = line.trim();
118
-
119
- if (line !== '') {
120
- if (currentElementCount >= header.elements[currentElement].count) {
121
- currentElement++;
122
- currentElementCount = 0;
123
- }
124
-
125
- const element = parseASCIIElement(header.elements[currentElement].properties, line);
126
- handleElement(attributes, header.elements[currentElement].name, element);
127
- currentElementCount++;
140
+ // PLY ascii format specification, as per http://en.wikipedia.org/wiki/PLY_(file_format)
141
+ const attributes = {
142
+ indices: [],
143
+ vertices: [],
144
+ normals: [],
145
+ uvs: [],
146
+ colors: []
147
+ };
148
+ let currentElement = 0;
149
+ let currentElementCount = 0;
150
+ for await (let line of lineIterator) {
151
+ line = line.trim();
152
+ if (line !== '') {
153
+ if (currentElementCount >= header.elements[currentElement].count) {
154
+ currentElement++;
155
+ currentElementCount = 0;
156
+ }
157
+ const element = parsePLYElement(header.elements[currentElement].properties, line);
158
+ handleElement(attributes, header.elements[currentElement].name, element);
159
+ currentElementCount++;
160
+ }
128
161
  }
129
- }
130
-
131
- return attributes;
162
+ return attributes;
132
163
  }
133
-
164
+ /**
165
+ * Parses ASCII number
166
+ * @param n
167
+ * @param type
168
+ * @returns ASCII number
169
+ */
170
+ // eslint-disable-next-line complexity
134
171
  function parseASCIINumber(n, type) {
135
- switch (type) {
136
- case 'char':
137
- case 'uchar':
138
- case 'short':
139
- case 'ushort':
140
- case 'int':
141
- case 'uint':
142
- case 'int8':
143
- case 'uint8':
144
- case 'int16':
145
- case 'uint16':
146
- case 'int32':
147
- case 'uint32':
148
- return parseInt(n, 10);
149
-
150
- case 'float':
151
- case 'double':
152
- case 'float32':
153
- case 'float64':
154
- return parseFloat(n);
155
-
156
- default:
157
- throw new Error(type);
158
- }
172
+ switch (type) {
173
+ case 'char':
174
+ case 'uchar':
175
+ case 'short':
176
+ case 'ushort':
177
+ case 'int':
178
+ case 'uint':
179
+ case 'int8':
180
+ case 'uint8':
181
+ case 'int16':
182
+ case 'uint16':
183
+ case 'int32':
184
+ case 'uint32':
185
+ return parseInt(n, 10);
186
+ case 'float':
187
+ case 'double':
188
+ case 'float32':
189
+ case 'float64':
190
+ return parseFloat(n);
191
+ default:
192
+ throw new Error(type);
193
+ }
159
194
  }
160
-
161
- function parseASCIIElement(properties, line) {
162
- const values = line.split(/\s+/);
163
- const element = {};
164
-
165
- for (let i = 0; i < properties.length; i++) {
166
- if (properties[i].type === 'list') {
167
- const list = [];
168
- const n = parseASCIINumber(values.shift(), properties[i].countType);
169
-
170
- for (let j = 0; j < n; j++) {
171
- list.push(parseASCIINumber(values.shift(), properties[i].itemType));
172
- }
173
-
174
- element[properties[i].name] = list;
175
- } else {
176
- element[properties[i].name] = parseASCIINumber(values.shift(), properties[i].type);
195
+ /**
196
+ * Parses ASCII element
197
+ * @param properties
198
+ * @param line
199
+ * @returns element
200
+ */
201
+ function parsePLYElement(properties, line) {
202
+ const values = line.split(/\s+/);
203
+ const element = {};
204
+ for (let i = 0; i < properties.length; i++) {
205
+ if (properties[i].type === 'list') {
206
+ const list = [];
207
+ const n = parseASCIINumber(values.shift(), properties[i].countType);
208
+ for (let j = 0; j < n; j++) {
209
+ list.push(parseASCIINumber(values.shift(), properties[i].itemType));
210
+ }
211
+ element[properties[i].name] = list;
212
+ }
213
+ else {
214
+ element[properties[i].name] = parseASCIINumber(values.shift(), properties[i].type);
215
+ }
177
216
  }
178
- }
179
-
180
- return element;
217
+ return element;
181
218
  }
182
-
219
+ /**
220
+ * @param buffer
221
+ * @param elementName
222
+ * @param element
223
+ */
224
+ // HELPER FUNCTIONS
225
+ // eslint-disable-next-line complexity
183
226
  function handleElement(buffer, elementName, element = {}) {
184
- switch (elementName) {
185
- case 'vertex':
186
- buffer.vertices.push(element.x, element.y, element.z);
187
-
188
- if ('nx' in element && 'ny' in element && 'nz' in element) {
189
- buffer.normals.push(element.nx, element.ny, element.nz);
190
- }
191
-
192
- if ('s' in element && 't' in element) {
193
- buffer.uvs.push(element.s, element.t);
194
- }
195
-
196
- if ('red' in element && 'green' in element && 'blue' in element) {
197
- buffer.colors.push(element.red / 255.0, element.green / 255.0, element.blue / 255.0);
198
- }
199
-
200
- break;
201
-
202
- case 'face':
203
- const vertexIndices = element.vertex_indices || element.vertex_index;
204
-
205
- if (vertexIndices.length === 3) {
206
- buffer.indices.push(vertexIndices[0], vertexIndices[1], vertexIndices[2]);
207
- } else if (vertexIndices.length === 4) {
208
- buffer.indices.push(vertexIndices[0], vertexIndices[1], vertexIndices[3]);
209
- buffer.indices.push(vertexIndices[1], vertexIndices[2], vertexIndices[3]);
210
- }
211
-
212
- break;
213
-
214
- default:
215
- break;
216
- }
227
+ switch (elementName) {
228
+ case 'vertex':
229
+ buffer.vertices.push(element.x, element.y, element.z);
230
+ if ('nx' in element && 'ny' in element && 'nz' in element) {
231
+ buffer.normals.push(element.nx, element.ny, element.nz);
232
+ }
233
+ if ('s' in element && 't' in element) {
234
+ buffer.uvs.push(element.s, element.t);
235
+ }
236
+ if ('red' in element && 'green' in element && 'blue' in element) {
237
+ buffer.colors.push(element.red / 255.0, element.green / 255.0, element.blue / 255.0);
238
+ }
239
+ break;
240
+ case 'face':
241
+ const vertexIndices = element.vertex_indices || element.vertex_index; // issue #9338
242
+ if (vertexIndices.length === 3) {
243
+ buffer.indices.push(vertexIndices[0], vertexIndices[1], vertexIndices[2]);
244
+ }
245
+ else if (vertexIndices.length === 4) {
246
+ buffer.indices.push(vertexIndices[0], vertexIndices[1], vertexIndices[3]);
247
+ buffer.indices.push(vertexIndices[1], vertexIndices[2], vertexIndices[3]);
248
+ }
249
+ break;
250
+ default:
251
+ break;
252
+ }
217
253
  }
218
- //# sourceMappingURL=parse-ply-in-batches.js.map
@@ -0,0 +1,11 @@
1
+ import type { PLYMesh } from './ply-types';
2
+ export type ParsePLYOptions = {
3
+ propertyNameMapping?: Record<string, string>;
4
+ };
5
+ /**
6
+ * @param data
7
+ * @param options
8
+ * @returns
9
+ */
10
+ export declare function parsePLY(data: ArrayBuffer | string, options?: ParsePLYOptions): PLYMesh;
11
+ //# sourceMappingURL=parse-ply.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-ply.d.ts","sourceRoot":"","sources":["../../src/lib/parse-ply.ts"],"names":[],"mappings":"AAsBA,OAAO,KAAK,EACV,OAAO,EAMR,MAAM,aAAa,CAAC;AAGrB,MAAM,MAAM,eAAe,GAAG;IAC5B,mBAAmB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC9C,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,WAAW,GAAG,MAAM,EAAE,OAAO,GAAE,eAAoB,GAAG,OAAO,CAc3F"}