@loaders.gl/terrain 4.2.0-alpha.3 → 4.2.0-alpha.5

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.
@@ -1,231 +1,226 @@
1
- const QUANTIZED_MESH_HEADER = new Map([['centerX', Float64Array.BYTES_PER_ELEMENT], ['centerY', Float64Array.BYTES_PER_ELEMENT], ['centerZ', Float64Array.BYTES_PER_ELEMENT], ['minHeight', Float32Array.BYTES_PER_ELEMENT], ['maxHeight', Float32Array.BYTES_PER_ELEMENT], ['boundingSphereCenterX', Float64Array.BYTES_PER_ELEMENT], ['boundingSphereCenterY', Float64Array.BYTES_PER_ELEMENT], ['boundingSphereCenterZ', Float64Array.BYTES_PER_ELEMENT], ['boundingSphereRadius', Float64Array.BYTES_PER_ELEMENT], ['horizonOcclusionPointX', Float64Array.BYTES_PER_ELEMENT], ['horizonOcclusionPointY', Float64Array.BYTES_PER_ELEMENT], ['horizonOcclusionPointZ', Float64Array.BYTES_PER_ELEMENT]]);
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ // Copyright (C) 2018-2019 HERE Europe B.V.
5
+ //
6
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ // of this software and associated documentation files (the "Software"), to deal
8
+ // in the Software without restriction, including without limitation the rights
9
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ // copies of the Software, and to permit persons to whom the Software is
11
+ // furnished to do so, subject to the following conditions:
12
+ //
13
+ // The above copyright notice and this permission notice shall be included in
14
+ // all copies or substantial portions of the Software.
15
+ //
16
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ // SOFTWARE.
23
+ const QUANTIZED_MESH_HEADER = new Map([
24
+ ['centerX', Float64Array.BYTES_PER_ELEMENT],
25
+ ['centerY', Float64Array.BYTES_PER_ELEMENT],
26
+ ['centerZ', Float64Array.BYTES_PER_ELEMENT],
27
+ ['minHeight', Float32Array.BYTES_PER_ELEMENT],
28
+ ['maxHeight', Float32Array.BYTES_PER_ELEMENT],
29
+ ['boundingSphereCenterX', Float64Array.BYTES_PER_ELEMENT],
30
+ ['boundingSphereCenterY', Float64Array.BYTES_PER_ELEMENT],
31
+ ['boundingSphereCenterZ', Float64Array.BYTES_PER_ELEMENT],
32
+ ['boundingSphereRadius', Float64Array.BYTES_PER_ELEMENT],
33
+ ['horizonOcclusionPointX', Float64Array.BYTES_PER_ELEMENT],
34
+ ['horizonOcclusionPointY', Float64Array.BYTES_PER_ELEMENT],
35
+ ['horizonOcclusionPointZ', Float64Array.BYTES_PER_ELEMENT]
36
+ ]);
2
37
  function decodeZigZag(value) {
3
- return value >> 1 ^ -(value & 1);
38
+ return (value >> 1) ^ -(value & 1);
4
39
  }
5
40
  function decodeHeader(dataView) {
6
- let position = 0;
7
- const header = {};
8
- for (const [key, bytesCount] of QUANTIZED_MESH_HEADER) {
9
- const getter = bytesCount === 8 ? dataView.getFloat64 : dataView.getFloat32;
10
- header[key] = getter.call(dataView, position, true);
11
- position += bytesCount;
12
- }
13
- return {
14
- header,
15
- headerEndPosition: position
16
- };
41
+ let position = 0;
42
+ const header = {};
43
+ for (const [key, bytesCount] of QUANTIZED_MESH_HEADER) {
44
+ const getter = bytesCount === 8 ? dataView.getFloat64 : dataView.getFloat32;
45
+ header[key] = getter.call(dataView, position, true);
46
+ position += bytesCount;
47
+ }
48
+ return { header, headerEndPosition: position };
17
49
  }
18
50
  function decodeVertexData(dataView, headerEndPosition) {
19
- let position = headerEndPosition;
20
- const elementsPerVertex = 3;
21
- const vertexCount = dataView.getUint32(position, true);
22
- const vertexData = new Uint16Array(vertexCount * elementsPerVertex);
23
- position += Uint32Array.BYTES_PER_ELEMENT;
24
- const bytesPerArrayElement = Uint16Array.BYTES_PER_ELEMENT;
25
- const elementArrayLength = vertexCount * bytesPerArrayElement;
26
- const uArrayStartPosition = position;
27
- const vArrayStartPosition = uArrayStartPosition + elementArrayLength;
28
- const heightArrayStartPosition = vArrayStartPosition + elementArrayLength;
29
- let u = 0;
30
- let v = 0;
31
- let height = 0;
32
- for (let i = 0; i < vertexCount; i++) {
33
- u += decodeZigZag(dataView.getUint16(uArrayStartPosition + bytesPerArrayElement * i, true));
34
- v += decodeZigZag(dataView.getUint16(vArrayStartPosition + bytesPerArrayElement * i, true));
35
- height += decodeZigZag(dataView.getUint16(heightArrayStartPosition + bytesPerArrayElement * i, true));
36
- vertexData[i] = u;
37
- vertexData[i + vertexCount] = v;
38
- vertexData[i + vertexCount * 2] = height;
39
- }
40
- position += elementArrayLength * 3;
41
- return {
42
- vertexData,
43
- vertexDataEndPosition: position
44
- };
51
+ let position = headerEndPosition;
52
+ const elementsPerVertex = 3;
53
+ const vertexCount = dataView.getUint32(position, true);
54
+ const vertexData = new Uint16Array(vertexCount * elementsPerVertex);
55
+ position += Uint32Array.BYTES_PER_ELEMENT;
56
+ const bytesPerArrayElement = Uint16Array.BYTES_PER_ELEMENT;
57
+ const elementArrayLength = vertexCount * bytesPerArrayElement;
58
+ const uArrayStartPosition = position;
59
+ const vArrayStartPosition = uArrayStartPosition + elementArrayLength;
60
+ const heightArrayStartPosition = vArrayStartPosition + elementArrayLength;
61
+ let u = 0;
62
+ let v = 0;
63
+ let height = 0;
64
+ for (let i = 0; i < vertexCount; i++) {
65
+ u += decodeZigZag(dataView.getUint16(uArrayStartPosition + bytesPerArrayElement * i, true));
66
+ v += decodeZigZag(dataView.getUint16(vArrayStartPosition + bytesPerArrayElement * i, true));
67
+ height += decodeZigZag(dataView.getUint16(heightArrayStartPosition + bytesPerArrayElement * i, true));
68
+ vertexData[i] = u;
69
+ vertexData[i + vertexCount] = v;
70
+ vertexData[i + vertexCount * 2] = height;
71
+ }
72
+ position += elementArrayLength * 3;
73
+ return { vertexData, vertexDataEndPosition: position };
45
74
  }
46
- function decodeIndex(buffer, position, indicesCount, bytesPerIndex) {
47
- let encoded = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : true;
48
- let indices;
49
- if (bytesPerIndex === 2) {
50
- indices = new Uint16Array(buffer, position, indicesCount);
51
- } else {
52
- indices = new Uint32Array(buffer, position, indicesCount);
53
- }
54
- if (!encoded) {
55
- return indices;
56
- }
57
- let highest = 0;
58
- for (let i = 0; i < indices.length; ++i) {
59
- const code = indices[i];
60
- indices[i] = highest - code;
61
- if (code === 0) {
62
- ++highest;
75
+ function decodeIndex(buffer, position, indicesCount, bytesPerIndex, encoded = true) {
76
+ let indices;
77
+ if (bytesPerIndex === 2) {
78
+ indices = new Uint16Array(buffer, position, indicesCount);
79
+ }
80
+ else {
81
+ indices = new Uint32Array(buffer, position, indicesCount);
63
82
  }
64
- }
65
- return indices;
83
+ if (!encoded) {
84
+ return indices;
85
+ }
86
+ let highest = 0;
87
+ for (let i = 0; i < indices.length; ++i) {
88
+ const code = indices[i];
89
+ indices[i] = highest - code;
90
+ if (code === 0) {
91
+ ++highest;
92
+ }
93
+ }
94
+ return indices;
66
95
  }
67
96
  function decodeTriangleIndices(dataView, vertexData, vertexDataEndPosition) {
68
- let position = vertexDataEndPosition;
69
- const elementsPerVertex = 3;
70
- const vertexCount = vertexData.length / elementsPerVertex;
71
- const bytesPerIndex = vertexCount > 65536 ? Uint32Array.BYTES_PER_ELEMENT : Uint16Array.BYTES_PER_ELEMENT;
72
- if (position % bytesPerIndex !== 0) {
73
- position += bytesPerIndex - position % bytesPerIndex;
74
- }
75
- const triangleCount = dataView.getUint32(position, true);
76
- position += Uint32Array.BYTES_PER_ELEMENT;
77
- const triangleIndicesCount = triangleCount * 3;
78
- const triangleIndices = decodeIndex(dataView.buffer, position, triangleIndicesCount, bytesPerIndex);
79
- position += triangleIndicesCount * bytesPerIndex;
80
- return {
81
- triangleIndicesEndPosition: position,
82
- triangleIndices
83
- };
97
+ let position = vertexDataEndPosition;
98
+ const elementsPerVertex = 3;
99
+ const vertexCount = vertexData.length / elementsPerVertex;
100
+ const bytesPerIndex = vertexCount > 65536 ? Uint32Array.BYTES_PER_ELEMENT : Uint16Array.BYTES_PER_ELEMENT;
101
+ if (position % bytesPerIndex !== 0) {
102
+ position += bytesPerIndex - (position % bytesPerIndex);
103
+ }
104
+ const triangleCount = dataView.getUint32(position, true);
105
+ position += Uint32Array.BYTES_PER_ELEMENT;
106
+ const triangleIndicesCount = triangleCount * 3;
107
+ const triangleIndices = decodeIndex(dataView.buffer, position, triangleIndicesCount, bytesPerIndex);
108
+ position += triangleIndicesCount * bytesPerIndex;
109
+ return {
110
+ triangleIndicesEndPosition: position,
111
+ triangleIndices
112
+ };
84
113
  }
85
114
  function decodeEdgeIndices(dataView, vertexData, triangleIndicesEndPosition) {
86
- let position = triangleIndicesEndPosition;
87
- const elementsPerVertex = 3;
88
- const vertexCount = vertexData.length / elementsPerVertex;
89
- const bytesPerIndex = vertexCount > 65536 ? Uint32Array.BYTES_PER_ELEMENT : Uint16Array.BYTES_PER_ELEMENT;
90
- const westVertexCount = dataView.getUint32(position, true);
91
- position += Uint32Array.BYTES_PER_ELEMENT;
92
- const westIndices = decodeIndex(dataView.buffer, position, westVertexCount, bytesPerIndex, false);
93
- position += westVertexCount * bytesPerIndex;
94
- const southVertexCount = dataView.getUint32(position, true);
95
- position += Uint32Array.BYTES_PER_ELEMENT;
96
- const southIndices = decodeIndex(dataView.buffer, position, southVertexCount, bytesPerIndex, false);
97
- position += southVertexCount * bytesPerIndex;
98
- const eastVertexCount = dataView.getUint32(position, true);
99
- position += Uint32Array.BYTES_PER_ELEMENT;
100
- const eastIndices = decodeIndex(dataView.buffer, position, eastVertexCount, bytesPerIndex, false);
101
- position += eastVertexCount * bytesPerIndex;
102
- const northVertexCount = dataView.getUint32(position, true);
103
- position += Uint32Array.BYTES_PER_ELEMENT;
104
- const northIndices = decodeIndex(dataView.buffer, position, northVertexCount, bytesPerIndex, false);
105
- position += northVertexCount * bytesPerIndex;
106
- return {
107
- edgeIndicesEndPosition: position,
108
- westIndices,
109
- southIndices,
110
- eastIndices,
111
- northIndices
112
- };
115
+ let position = triangleIndicesEndPosition;
116
+ const elementsPerVertex = 3;
117
+ const vertexCount = vertexData.length / elementsPerVertex;
118
+ const bytesPerIndex = vertexCount > 65536 ? Uint32Array.BYTES_PER_ELEMENT : Uint16Array.BYTES_PER_ELEMENT;
119
+ const westVertexCount = dataView.getUint32(position, true);
120
+ position += Uint32Array.BYTES_PER_ELEMENT;
121
+ const westIndices = decodeIndex(dataView.buffer, position, westVertexCount, bytesPerIndex, false);
122
+ position += westVertexCount * bytesPerIndex;
123
+ const southVertexCount = dataView.getUint32(position, true);
124
+ position += Uint32Array.BYTES_PER_ELEMENT;
125
+ const southIndices = decodeIndex(dataView.buffer, position, southVertexCount, bytesPerIndex, false);
126
+ position += southVertexCount * bytesPerIndex;
127
+ const eastVertexCount = dataView.getUint32(position, true);
128
+ position += Uint32Array.BYTES_PER_ELEMENT;
129
+ const eastIndices = decodeIndex(dataView.buffer, position, eastVertexCount, bytesPerIndex, false);
130
+ position += eastVertexCount * bytesPerIndex;
131
+ const northVertexCount = dataView.getUint32(position, true);
132
+ position += Uint32Array.BYTES_PER_ELEMENT;
133
+ const northIndices = decodeIndex(dataView.buffer, position, northVertexCount, bytesPerIndex, false);
134
+ position += northVertexCount * bytesPerIndex;
135
+ return {
136
+ edgeIndicesEndPosition: position,
137
+ westIndices,
138
+ southIndices,
139
+ eastIndices,
140
+ northIndices
141
+ };
113
142
  }
114
143
  function decodeVertexNormalsExtension(extensionDataView) {
115
- return new Uint8Array(extensionDataView.buffer, extensionDataView.byteOffset, extensionDataView.byteLength);
144
+ return new Uint8Array(extensionDataView.buffer, extensionDataView.byteOffset, extensionDataView.byteLength);
116
145
  }
117
146
  function decodeWaterMaskExtension(extensionDataView) {
118
- return extensionDataView.buffer.slice(extensionDataView.byteOffset, extensionDataView.byteOffset + extensionDataView.byteLength);
147
+ return extensionDataView.buffer.slice(extensionDataView.byteOffset, extensionDataView.byteOffset + extensionDataView.byteLength);
119
148
  }
120
149
  function decodeExtensions(dataView, indicesEndPosition) {
121
- const extensions = {};
122
- if (dataView.byteLength <= indicesEndPosition) {
123
- return {
124
- extensions,
125
- extensionsEndPosition: indicesEndPosition
126
- };
127
- }
128
- let position = indicesEndPosition;
129
- while (position < dataView.byteLength) {
130
- const extensionId = dataView.getUint8(position, true);
131
- position += Uint8Array.BYTES_PER_ELEMENT;
132
- const extensionLength = dataView.getUint32(position, true);
133
- position += Uint32Array.BYTES_PER_ELEMENT;
134
- const extensionView = new DataView(dataView.buffer, position, extensionLength);
135
- switch (extensionId) {
136
- case 1:
137
- {
138
- extensions.vertexNormals = decodeVertexNormalsExtension(extensionView);
139
- break;
140
- }
141
- case 2:
142
- {
143
- extensions.waterMask = decodeWaterMaskExtension(extensionView);
144
- break;
150
+ const extensions = {};
151
+ if (dataView.byteLength <= indicesEndPosition) {
152
+ return { extensions, extensionsEndPosition: indicesEndPosition };
153
+ }
154
+ let position = indicesEndPosition;
155
+ while (position < dataView.byteLength) {
156
+ const extensionId = dataView.getUint8(position, true);
157
+ position += Uint8Array.BYTES_PER_ELEMENT;
158
+ const extensionLength = dataView.getUint32(position, true);
159
+ position += Uint32Array.BYTES_PER_ELEMENT;
160
+ const extensionView = new DataView(dataView.buffer, position, extensionLength);
161
+ switch (extensionId) {
162
+ case 1: {
163
+ extensions.vertexNormals = decodeVertexNormalsExtension(extensionView);
164
+ break;
165
+ }
166
+ case 2: {
167
+ extensions.waterMask = decodeWaterMaskExtension(extensionView);
168
+ break;
169
+ }
170
+ default: {
171
+ // console.warn(`Unknown extension with id ${extensionId}`)
172
+ }
145
173
  }
146
- default:
147
- {}
174
+ position += extensionLength;
148
175
  }
149
- position += extensionLength;
150
- }
151
- return {
152
- extensions,
153
- extensionsEndPosition: position
154
- };
176
+ return { extensions, extensionsEndPosition: position };
155
177
  }
156
178
  export const DECODING_STEPS = {
157
- header: 0,
158
- vertices: 1,
159
- triangleIndices: 2,
160
- edgeIndices: 3,
161
- extensions: 4
179
+ header: 0,
180
+ vertices: 1,
181
+ triangleIndices: 2,
182
+ edgeIndices: 3,
183
+ extensions: 4
162
184
  };
163
185
  const DEFAULT_OPTIONS = {
164
- maxDecodingStep: DECODING_STEPS.extensions
186
+ maxDecodingStep: DECODING_STEPS.extensions
165
187
  };
166
188
  export default function decode(data, userOptions) {
167
- const options = Object.assign({}, DEFAULT_OPTIONS, userOptions);
168
- const view = new DataView(data);
169
- const {
170
- header,
171
- headerEndPosition
172
- } = decodeHeader(view);
173
- if (options.maxDecodingStep < DECODING_STEPS.vertices) {
174
- return {
175
- header
176
- };
177
- }
178
- const {
179
- vertexData,
180
- vertexDataEndPosition
181
- } = decodeVertexData(view, headerEndPosition);
182
- if (options.maxDecodingStep < DECODING_STEPS.triangleIndices) {
183
- return {
184
- header,
185
- vertexData
186
- };
187
- }
188
- const {
189
- triangleIndices,
190
- triangleIndicesEndPosition
191
- } = decodeTriangleIndices(view, vertexData, vertexDataEndPosition);
192
- if (options.maxDecodingStep < DECODING_STEPS.edgeIndices) {
193
- return {
194
- header,
195
- vertexData,
196
- triangleIndices
197
- };
198
- }
199
- const {
200
- westIndices,
201
- southIndices,
202
- eastIndices,
203
- northIndices,
204
- edgeIndicesEndPosition
205
- } = decodeEdgeIndices(view, vertexData, triangleIndicesEndPosition);
206
- if (options.maxDecodingStep < DECODING_STEPS.extensions) {
189
+ const options = Object.assign({}, DEFAULT_OPTIONS, userOptions);
190
+ const view = new DataView(data);
191
+ const { header, headerEndPosition } = decodeHeader(view);
192
+ if (options.maxDecodingStep < DECODING_STEPS.vertices) {
193
+ return { header };
194
+ }
195
+ const { vertexData, vertexDataEndPosition } = decodeVertexData(view, headerEndPosition);
196
+ if (options.maxDecodingStep < DECODING_STEPS.triangleIndices) {
197
+ return { header, vertexData };
198
+ }
199
+ const { triangleIndices, triangleIndicesEndPosition } = decodeTriangleIndices(view, vertexData, vertexDataEndPosition);
200
+ if (options.maxDecodingStep < DECODING_STEPS.edgeIndices) {
201
+ return { header, vertexData, triangleIndices };
202
+ }
203
+ const { westIndices, southIndices, eastIndices, northIndices, edgeIndicesEndPosition } = decodeEdgeIndices(view, vertexData, triangleIndicesEndPosition);
204
+ if (options.maxDecodingStep < DECODING_STEPS.extensions) {
205
+ return {
206
+ header,
207
+ vertexData,
208
+ triangleIndices,
209
+ westIndices,
210
+ northIndices,
211
+ eastIndices,
212
+ southIndices
213
+ };
214
+ }
215
+ const { extensions } = decodeExtensions(view, edgeIndicesEndPosition);
207
216
  return {
208
- header,
209
- vertexData,
210
- triangleIndices,
211
- westIndices,
212
- northIndices,
213
- eastIndices,
214
- southIndices
217
+ header,
218
+ vertexData,
219
+ triangleIndices,
220
+ westIndices,
221
+ northIndices,
222
+ eastIndices,
223
+ southIndices,
224
+ extensions
215
225
  };
216
- }
217
- const {
218
- extensions
219
- } = decodeExtensions(view, edgeIndicesEndPosition);
220
- return {
221
- header,
222
- vertexData,
223
- triangleIndices,
224
- westIndices,
225
- northIndices,
226
- eastIndices,
227
- southIndices,
228
- extensions
229
- };
230
226
  }
231
- //# sourceMappingURL=decode-quantized-mesh.js.map