itowns 2.43.2-next.9 → 2.44.0

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 (62) hide show
  1. package/CONTRIBUTORS.md +1 -0
  2. package/changelog.md +107 -0
  3. package/dist/455.js +2 -0
  4. package/dist/455.js.map +1 -0
  5. package/dist/debug.js +1 -1
  6. package/dist/debug.js.LICENSE.txt +2 -2
  7. package/dist/debug.js.map +1 -1
  8. package/dist/itowns.js +1 -1
  9. package/dist/itowns.js.LICENSE.txt +1 -22
  10. package/dist/itowns.js.map +1 -1
  11. package/dist/itowns_lasparser.js +2 -0
  12. package/dist/itowns_lasparser.js.map +1 -0
  13. package/dist/itowns_lasworker.js +2 -0
  14. package/dist/itowns_lasworker.js.map +1 -0
  15. package/dist/itowns_potree2worker.js +2 -0
  16. package/dist/itowns_potree2worker.js.map +1 -0
  17. package/dist/itowns_widgets.js +1 -1
  18. package/dist/itowns_widgets.js.map +1 -1
  19. package/examples/3dtiles_loader.html +150 -0
  20. package/examples/config.json +5 -0
  21. package/examples/jsm/.eslintrc.cjs +38 -0
  22. package/examples/jsm/OGC3DTilesHelper.js +105 -0
  23. package/examples/misc_instancing.html +2 -1
  24. package/examples/potree2_25d_map.html +127 -0
  25. package/lib/Core/3DTiles/C3DTFeature.js +6 -6
  26. package/lib/Core/Potree2Node.js +206 -0
  27. package/lib/Core/Potree2PointAttributes.js +139 -0
  28. package/lib/Core/Scheduler/Scheduler.js +1 -1
  29. package/lib/Core/Style.js +52 -49
  30. package/lib/Core/View.js +3 -0
  31. package/lib/Layer/C3DTilesLayer.js +6 -6
  32. package/lib/Layer/OGC3DTilesLayer.js +386 -0
  33. package/lib/Layer/PointCloudLayer.js +1 -1
  34. package/lib/Layer/Potree2Layer.js +165 -0
  35. package/lib/Layer/ReferencingLayerProperties.js +5 -0
  36. package/lib/Layer/TiledGeometryLayer.js +4 -1
  37. package/lib/Loader/Potree2BrotliLoader.js +261 -0
  38. package/lib/Loader/Potree2Loader.js +207 -0
  39. package/lib/Main.js +9 -2
  40. package/lib/Parser/B3dmParser.js +11 -22
  41. package/lib/Parser/LASParser.js +48 -16
  42. package/lib/Parser/Potree2BinParser.js +92 -0
  43. package/lib/Parser/ShapefileParser.js +2 -2
  44. package/lib/Parser/deprecated/LegacyGLTFLoader.js +25 -5
  45. package/lib/Parser/iGLTFLoader.js +169 -0
  46. package/lib/Process/3dTilesProcessing.js +2 -1
  47. package/lib/Provider/3dTilesProvider.js +6 -4
  48. package/lib/Renderer/PointsMaterial.js +128 -94
  49. package/lib/Source/FileSource.js +1 -1
  50. package/lib/Source/OGC3DTilesGoogleSource.js +32 -0
  51. package/lib/Source/OGC3DTilesIonSource.js +37 -0
  52. package/lib/Source/OGC3DTilesSource.js +24 -0
  53. package/lib/Source/Potree2Source.js +172 -0
  54. package/lib/ThreeExtended/loaders/DRACOLoader.js +5 -3
  55. package/lib/ThreeExtended/loaders/GLTFLoader.js +38 -2
  56. package/lib/ThreeExtended/loaders/KTX2Loader.js +17 -10
  57. package/lib/ThreeExtended/utils/BufferGeometryUtils.js +32 -30
  58. package/lib/Worker/LASLoaderWorker.js +19 -0
  59. package/lib/Worker/Potree2Worker.js +21 -0
  60. package/package.json +33 -31
  61. package/lib/Parser/GLTFParser.js +0 -88
  62. /package/lib/{Parser → Loader}/LASLoader.js +0 -0
@@ -0,0 +1,165 @@
1
+ /*
2
+ ============
3
+ == POTREE ==
4
+ ============
5
+
6
+ http://potree.org
7
+
8
+ Copyright (c) 2011-2020, Markus Schütz
9
+ All rights reserved.
10
+
11
+ Redistribution and use in source and binary forms, with or without
12
+ modification, are permitted provided that the following conditions are met:
13
+
14
+ 1. Redistributions of source code must retain the above copyright notice, this
15
+ list of conditions and the following disclaimer.
16
+ 2. Redistributions in binary form must reproduce the above copyright notice,
17
+ this list of conditions and the following disclaimer in the documentation
18
+ and/or other materials provided with the distribution.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
24
+ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+
31
+ The views and conclusions contained in the software and documentation are those
32
+ of the authors and should not be interpreted as representing official policies,
33
+ either expressed or implied, of the FreeBSD Project.
34
+ */
35
+
36
+ import * as THREE from 'three';
37
+ import PointCloudLayer from "./PointCloudLayer.js";
38
+ import Potree2Node from "../Core/Potree2Node.js";
39
+ import Extent from "../Core/Geographic/Extent.js";
40
+ import { PointAttribute, Potree2PointAttributes, PointAttributeTypes } from "../Core/Potree2PointAttributes.js";
41
+ const bboxMesh = new THREE.Mesh();
42
+ const box3 = new THREE.Box3();
43
+ bboxMesh.geometry.boundingBox = box3;
44
+ const typeNameAttributeMap = {
45
+ double: PointAttributeTypes.DATA_TYPE_DOUBLE,
46
+ float: PointAttributeTypes.DATA_TYPE_FLOAT,
47
+ int8: PointAttributeTypes.DATA_TYPE_INT8,
48
+ uint8: PointAttributeTypes.DATA_TYPE_UINT8,
49
+ int16: PointAttributeTypes.DATA_TYPE_INT16,
50
+ uint16: PointAttributeTypes.DATA_TYPE_UINT16,
51
+ int32: PointAttributeTypes.DATA_TYPE_INT32,
52
+ uint32: PointAttributeTypes.DATA_TYPE_UINT32,
53
+ int64: PointAttributeTypes.DATA_TYPE_INT64,
54
+ uint64: PointAttributeTypes.DATA_TYPE_UINT64
55
+ };
56
+ function parseAttributes(jsonAttributes) {
57
+ const attributes = new Potree2PointAttributes();
58
+ const replacements = {
59
+ rgb: 'rgba'
60
+ };
61
+ for (const jsonAttribute of jsonAttributes) {
62
+ const {
63
+ name,
64
+ numElements,
65
+ min,
66
+ max
67
+ } = jsonAttribute;
68
+ const type = typeNameAttributeMap[jsonAttribute.type];
69
+ const potreeAttributeName = replacements[name] ? replacements[name] : name;
70
+ const attribute = new PointAttribute(potreeAttributeName, type, numElements);
71
+ if (numElements === 1) {
72
+ attribute.range = [min[0], max[0]];
73
+ } else {
74
+ attribute.range = [min, max];
75
+ }
76
+ if (name === 'gps-time') {
77
+ // HACK: Guard against bad gpsTime range in metadata, see potree/potree#909
78
+ if (attribute.range[0] === attribute.range[1]) {
79
+ attribute.range[1] += 1;
80
+ }
81
+ }
82
+ attribute.initialRange = attribute.range;
83
+ attributes.add(attribute);
84
+ }
85
+ {
86
+ // check if it has normals
87
+ const hasNormals = attributes.attributes.find(a => a.name === 'NormalX') !== undefined && attributes.attributes.find(a => a.name === 'NormalY') !== undefined && attributes.attributes.find(a => a.name === 'NormalZ') !== undefined;
88
+ if (hasNormals) {
89
+ attributes.addVector({
90
+ name: 'NORMAL',
91
+ attributes: ['NormalX', 'NormalY', 'NormalZ']
92
+ });
93
+ }
94
+ }
95
+ return attributes;
96
+ }
97
+
98
+ /**
99
+ * @property {boolean} isPotreeLayer - Used to checkout whether this layer
100
+ * is a Potree2Layer. Default is `true`. You should not change this, as it is
101
+ * used internally for optimisation.
102
+ */
103
+ class Potree2Layer extends PointCloudLayer {
104
+ /**
105
+ * Constructs a new instance of Potree2 layer.
106
+ *
107
+ * @constructor
108
+ * @extends PointCloudLayer
109
+ *
110
+ * @example
111
+ * // Create a new point cloud layer
112
+ * const points = new Potree2Layer('points',
113
+ * {
114
+ * source: new Potree2Source({
115
+ * url: 'https://pointsClouds/',
116
+ * file: 'metadata.json',
117
+ * }
118
+ * });
119
+ *
120
+ * View.prototype.addLayer.call(view, points);
121
+ *
122
+ * @param {string} id - The id of the layer, that should be unique. It is
123
+ * not mandatory, but an error will be emitted if this layer is added a
124
+ * {@link View} that already has a layer going by that id.
125
+ * @param {Object} config - Configuration, all elements in it
126
+ * will be merged as is in the layer. For example, if the configuration
127
+ * contains three elements `name, protocol, extent`, these elements will be
128
+ * available using `layer.name` or something else depending on the property
129
+ * name. See the list of properties to know which one can be specified.
130
+ * @param {string} [config.crs=ESPG:4326] - The CRS of the {@link View} this
131
+ * layer will be attached to. This is used to determine the extent of this
132
+ * layer. Default to `EPSG:4326`.
133
+ */
134
+ constructor(id, config) {
135
+ super(id, config);
136
+ this.isPotreeLayer = true;
137
+ const resolve = this.addInitializationStep();
138
+ this.source.whenReady.then(metadata => {
139
+ this.scale = new THREE.Vector3(1, 1, 1);
140
+ this.metadata = metadata;
141
+ this.pointAttributes = parseAttributes(metadata.attributes);
142
+ this.spacing = metadata.spacing;
143
+ const normal = Array.isArray(this.pointAttributes.attributes) && this.pointAttributes.attributes.find(elem => elem.name.startsWith('NORMAL'));
144
+ if (normal) {
145
+ this.material.defines[normal.name] = 1;
146
+ }
147
+ const min = new THREE.Vector3(...metadata.boundingBox.min);
148
+ const max = new THREE.Vector3(...metadata.boundingBox.max);
149
+ const boundingBox = new THREE.Box3(min, max);
150
+ const root = new Potree2Node(0, 0, this);
151
+ root.bbox = boundingBox;
152
+ root.boundingSphere = boundingBox.getBoundingSphere(new THREE.Sphere());
153
+ root.id = 'r';
154
+ root.depth = 0;
155
+ root.nodeType = 2;
156
+ root.hierarchyByteOffset = 0n;
157
+ root.hierarchyByteSize = BigInt(metadata.hierarchy.firstChunkSize);
158
+ root.byteOffset = 0;
159
+ this.root = root;
160
+ this.extent = Extent.fromBox3(this.source.crs || 'EPSG:4326', boundingBox);
161
+ return this.root.loadOctree().then(resolve);
162
+ });
163
+ }
164
+ }
165
+ export default Potree2Layer;
@@ -38,6 +38,11 @@ function ReferLayerProperties(material, layer) {
38
38
  get: () => material.layer.pntsMaxAttenuatedSize
39
39
  });
40
40
  }
41
+ if (material.uniforms && material.uniforms.scale != undefined) {
42
+ Object.defineProperty(material.uniforms.scale, 'value', {
43
+ get: () => material.layer.scale
44
+ });
45
+ }
41
46
  Object.defineProperty(material, 'wireframe', {
42
47
  get: () => material.layer.wireframe
43
48
  });
@@ -67,7 +67,7 @@ class TiledGeometryLayer extends GeometryLayer {
67
67
  // TODO : this should be add in a preprocess method specific to GeoidLayer.
68
68
  this.object3d.geoidHeight = 0;
69
69
  this.protocol = 'tile';
70
- this._hideSkirt = false;
70
+ this._hideSkirt = !!config.hideSkirt;
71
71
  this.sseSubdivisionThreshold = this.sseSubdivisionThreshold || 1.0;
72
72
  this.schemeTile = schemeTile;
73
73
  this.builder = builder;
@@ -94,6 +94,9 @@ class TiledGeometryLayer extends GeometryLayer {
94
94
  return this._hideSkirt;
95
95
  }
96
96
  set hideSkirt(value) {
97
+ if (!this.level0Nodes) {
98
+ return;
99
+ }
97
100
  this._hideSkirt = value;
98
101
  for (const node of this.level0Nodes) {
99
102
  node.traverse(obj => {
@@ -0,0 +1,261 @@
1
+ /*
2
+ ============
3
+ == POTREE ==
4
+ ============
5
+
6
+ http://potree.org
7
+
8
+ Copyright (c) 2011-2020, Markus Schütz
9
+ All rights reserved.
10
+
11
+ Redistribution and use in source and binary forms, with or without
12
+ modification, are permitted provided that the following conditions are met:
13
+
14
+ 1. Redistributions of source code must retain the above copyright notice, this
15
+ list of conditions and the following disclaimer.
16
+ 2. Redistributions in binary form must reproduce the above copyright notice,
17
+ this list of conditions and the following disclaimer in the documentation
18
+ and/or other materials provided with the distribution.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
24
+ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+
31
+ The views and conclusions contained in the software and documentation are those
32
+ of the authors and should not be interpreted as representing official policies,
33
+ either expressed or implied, of the FreeBSD Project.
34
+ */
35
+
36
+ import { PointAttribute, PointAttributeTypes } from "../Core/Potree2PointAttributes.js";
37
+ import { decompress } from 'brotli-compress/js.mjs';
38
+ const typedArrayMapping = {
39
+ int8: Int8Array,
40
+ int16: Int16Array,
41
+ int32: Int32Array,
42
+ int64: Float64Array,
43
+ uint8: Uint8Array,
44
+ uint16: Uint16Array,
45
+ uint32: Uint32Array,
46
+ uint64: Float64Array,
47
+ float: Float32Array,
48
+ double: Float64Array
49
+ };
50
+ function dealign24b(mortoncode) {
51
+ // see https://stackoverflow.com/questions/45694690/how-i-can-remove-all-odds-bits-in-c
52
+
53
+ // input alignment of desired bits
54
+ // ..a..b..c..d..e..f..g..h..i..j..k..l..m..n..o..p
55
+ let x = mortoncode;
56
+
57
+ // ..a..b..c..d..e..f..g..h..i..j..k..l..m..n..o..p ..a..b..c..d..e..f..g..h..i..j..k..l..m..n..o..p
58
+ // ..a.....c.....e.....g.....i.....k.....m.....o... .....b.....d.....f.....h.....j.....l.....n.....p
59
+ // ....a.....c.....e.....g.....i.....k.....m.....o. .....b.....d.....f.....h.....j.....l.....n.....p
60
+ x = (x & 0b001000001000001000001000) >> 2 | (x & 0b000001000001000001000001) >> 0;
61
+ // ....ab....cd....ef....gh....ij....kl....mn....op ....ab....cd....ef....gh....ij....kl....mn....op
62
+ // ....ab..........ef..........ij..........mn...... ..........cd..........gh..........kl..........op
63
+ // ........ab..........ef..........ij..........mn.. ..........cd..........gh..........kl..........op
64
+ x = (x & 0b000011000000000011000000) >> 4 | (x & 0b000000000011000000000011) >> 0;
65
+ // ........abcd........efgh........ijkl........mnop ........abcd........efgh........ijkl........mnop
66
+ // ........abcd....................ijkl............ ....................efgh....................mnop
67
+ // ................abcd....................ijkl.... ....................efgh....................mnop
68
+ x = (x & 0b000000001111000000000000) >> 8 | (x & 0b000000000000000000001111) >> 0;
69
+ // ................abcdefgh................ijklmnop ................abcdefgh................ijklmnop
70
+ // ................abcdefgh........................ ........................................ijklmnop
71
+ // ................................abcdefgh........ ........................................ijklmnop
72
+ x = (x & 0b000000000000000000000000) >> 16 | (x & 0b000000000000000011111111) >> 0;
73
+
74
+ // sucessfully realigned!
75
+ // ................................abcdefghijklmnop
76
+
77
+ return x;
78
+ }
79
+ export default async function load(buffer, options) {
80
+ const {
81
+ pointAttributes,
82
+ scale,
83
+ min,
84
+ size,
85
+ offset,
86
+ numPoints
87
+ } = options;
88
+ let bytes;
89
+ if (numPoints === 0) {
90
+ bytes = {
91
+ buffer: new ArrayBuffer(0)
92
+ };
93
+ } else {
94
+ try {
95
+ bytes = await decompress(new Int8Array(buffer));
96
+ } catch (e) {
97
+ bytes = {
98
+ buffer: new ArrayBuffer(numPoints * (pointAttributes.byteSize + 12))
99
+ };
100
+ console.error(`problem with node ${name}: `, e);
101
+ }
102
+ }
103
+ const view = new DataView(bytes.buffer);
104
+ const attributeBuffers = {};
105
+ const gridSize = 32;
106
+ const grid = new Uint32Array(gridSize ** 3);
107
+ const toIndex = (x, y, z) => {
108
+ // min is already subtracted
109
+ const dx = gridSize * x / size.x;
110
+ const dy = gridSize * y / size.y;
111
+ const dz = gridSize * z / size.z;
112
+ const ix = Math.min(parseInt(dx, 10), gridSize - 1);
113
+ const iy = Math.min(parseInt(dy, 10), gridSize - 1);
114
+ const iz = Math.min(parseInt(dz, 10), gridSize - 1);
115
+ return ix + iy * gridSize + iz * gridSize * gridSize;
116
+ };
117
+ let numOccupiedCells = 0;
118
+ let byteOffset = 0;
119
+ for (const pointAttribute of pointAttributes.attributes) {
120
+ if (['POSITION_CARTESIAN', 'position'].includes(pointAttribute.name)) {
121
+ const buff = new ArrayBuffer(numPoints * 4 * 3);
122
+ const positions = new Float32Array(buff);
123
+ for (let j = 0; j < numPoints; j++) {
124
+ const mc_0 = view.getUint32(byteOffset + 4, true);
125
+ const mc_1 = view.getUint32(byteOffset + 0, true);
126
+ const mc_2 = view.getUint32(byteOffset + 12, true);
127
+ const mc_3 = view.getUint32(byteOffset + 8, true);
128
+ byteOffset += 16;
129
+ let X = dealign24b((mc_3 & 0x00FFFFFF) >>> 0) | dealign24b((mc_3 >>> 24 | mc_2 << 8) >>> 0) << 8;
130
+ let Y = dealign24b((mc_3 & 0x00FFFFFF) >>> 1) | dealign24b((mc_3 >>> 24 | mc_2 << 8) >>> 1) << 8;
131
+ let Z = dealign24b((mc_3 & 0x00FFFFFF) >>> 2) | dealign24b((mc_3 >>> 24 | mc_2 << 8) >>> 2) << 8;
132
+ if (mc_1 != 0 || mc_2 != 0) {
133
+ X = X | dealign24b((mc_1 & 0x00FFFFFF) >>> 0) << 16 | dealign24b((mc_1 >>> 24 | mc_0 << 8) >>> 0) << 24;
134
+ Y = Y | dealign24b((mc_1 & 0x00FFFFFF) >>> 1) << 16 | dealign24b((mc_1 >>> 24 | mc_0 << 8) >>> 1) << 24;
135
+ Z = Z | dealign24b((mc_1 & 0x00FFFFFF) >>> 2) << 16 | dealign24b((mc_1 >>> 24 | mc_0 << 8) >>> 2) << 24;
136
+ }
137
+ const x = parseInt(X, 10) * scale[0] + offset[0] - min.x;
138
+ const y = parseInt(Y, 10) * scale[1] + offset[1] - min.y;
139
+ const z = parseInt(Z, 10) * scale[2] + offset[2] - min.z;
140
+ const index = toIndex(x, y, z);
141
+ const count = grid[index]++;
142
+ if (count === 0) {
143
+ numOccupiedCells++;
144
+ }
145
+ positions[3 * j + 0] = x;
146
+ positions[3 * j + 1] = y;
147
+ positions[3 * j + 2] = z;
148
+ }
149
+ attributeBuffers[pointAttribute.name] = {
150
+ buffer: buff,
151
+ attribute: pointAttribute
152
+ };
153
+ } else if (['RGBA', 'rgba'].includes(pointAttribute.name)) {
154
+ const buff = new ArrayBuffer(numPoints * 4);
155
+ const colors = new Uint8Array(buff);
156
+ for (let j = 0; j < numPoints; j++) {
157
+ const mc_0 = view.getUint32(byteOffset + 4, true);
158
+ const mc_1 = view.getUint32(byteOffset + 0, true);
159
+ byteOffset += 8;
160
+ const r = dealign24b((mc_1 & 0x00FFFFFF) >>> 0) | dealign24b((mc_1 >>> 24 | mc_0 << 8) >>> 0) << 8;
161
+ const g = dealign24b((mc_1 & 0x00FFFFFF) >>> 1) | dealign24b((mc_1 >>> 24 | mc_0 << 8) >>> 1) << 8;
162
+ const b = dealign24b((mc_1 & 0x00FFFFFF) >>> 2) | dealign24b((mc_1 >>> 24 | mc_0 << 8) >>> 2) << 8;
163
+ colors[4 * j + 0] = r > 255 ? r / 256 : r;
164
+ colors[4 * j + 1] = g > 255 ? g / 256 : g;
165
+ colors[4 * j + 2] = b > 255 ? b / 256 : b;
166
+ }
167
+ attributeBuffers[pointAttribute.name] = {
168
+ buffer: buff,
169
+ attribute: pointAttribute
170
+ };
171
+ } else {
172
+ const buff = new ArrayBuffer(numPoints * 4);
173
+ const f32 = new Float32Array(buff);
174
+ const TypedArray = typedArrayMapping[pointAttribute.type.name];
175
+ const preciseBuffer = new TypedArray(numPoints);
176
+ let [offset, scale] = [0, 1];
177
+ const getterMap = {
178
+ int8: view.getInt8,
179
+ int16: view.getInt16,
180
+ int32: view.getInt32,
181
+ uint8: view.getUint8,
182
+ uint16: view.getUint16,
183
+ uint32: view.getUint32,
184
+ float: view.getFloat32,
185
+ double: view.getFloat64
186
+ };
187
+ const getter = getterMap[pointAttribute.type.name].bind(view);
188
+
189
+ // compute offset and scale to pack larger types into 32 bit floats
190
+ if (pointAttribute.type.size > 4) {
191
+ const [amin, amax] = pointAttribute.range;
192
+ offset = amin;
193
+ scale = 1 / (amax - amin);
194
+ }
195
+ for (let j = 0; j < numPoints; j++) {
196
+ const value = getter(byteOffset, true);
197
+ byteOffset += pointAttribute.byteSize;
198
+ f32[j] = (value - offset) * scale;
199
+ preciseBuffer[j] = value;
200
+ }
201
+ attributeBuffers[pointAttribute.name] = {
202
+ buffer: buff,
203
+ preciseBuffer,
204
+ attribute: pointAttribute,
205
+ offset,
206
+ scale
207
+ };
208
+ }
209
+ }
210
+ const occupancy = parseInt(numPoints / numOccupiedCells, 10);
211
+ {
212
+ // add indices
213
+ const buff = new ArrayBuffer(numPoints * 4);
214
+ const indices = new Uint32Array(buff);
215
+ for (let i = 0; i < numPoints; i++) {
216
+ indices[i] = i;
217
+ }
218
+ attributeBuffers.INDICES = {
219
+ buffer: buff,
220
+ attribute: PointAttribute.INDICES
221
+ };
222
+ }
223
+ {
224
+ // handle attribute vectors
225
+ const vectors = pointAttributes.vectors;
226
+ for (const vector of vectors) {
227
+ const {
228
+ name,
229
+ attributes
230
+ } = vector;
231
+ const numVectorElements = attributes.length;
232
+ const buffer = new ArrayBuffer(numVectorElements * numPoints * 4);
233
+ const f32 = new Float32Array(buffer);
234
+ let iElement = 0;
235
+ for (const sourceName of attributes) {
236
+ const sourceBuffer = attributeBuffers[sourceName];
237
+ const {
238
+ offset,
239
+ scale
240
+ } = sourceBuffer;
241
+ const view = new DataView(sourceBuffer.buffer);
242
+ const getter = view.getFloat32.bind(view);
243
+ for (let j = 0; j < numPoints; j++) {
244
+ const value = getter(j * 4, true);
245
+ f32[j * numVectorElements + iElement] = value / scale + offset;
246
+ }
247
+ iElement++;
248
+ }
249
+ const vecAttribute = new PointAttribute(name, PointAttributeTypes.DATA_TYPE_FLOAT, 3);
250
+ attributeBuffers[name] = {
251
+ buffer,
252
+ attribute: vecAttribute
253
+ };
254
+ }
255
+ }
256
+ return {
257
+ buffer,
258
+ attributeBuffers,
259
+ density: occupancy
260
+ };
261
+ }
@@ -0,0 +1,207 @@
1
+ /*
2
+ ============
3
+ == POTREE ==
4
+ ============
5
+
6
+ http://potree.org
7
+
8
+ Copyright (c) 2011-2020, Markus Schütz
9
+ All rights reserved.
10
+
11
+ Redistribution and use in source and binary forms, with or without
12
+ modification, are permitted provided that the following conditions are met:
13
+
14
+ 1. Redistributions of source code must retain the above copyright notice, this
15
+ list of conditions and the following disclaimer.
16
+ 2. Redistributions in binary form must reproduce the above copyright notice,
17
+ this list of conditions and the following disclaimer in the documentation
18
+ and/or other materials provided with the distribution.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
24
+ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+
31
+ The views and conclusions contained in the software and documentation are those
32
+ of the authors and should not be interpreted as representing official policies,
33
+ either expressed or implied, of the FreeBSD Project.
34
+ */
35
+
36
+ import { PointAttribute, PointAttributeTypes } from "../Core/Potree2PointAttributes.js";
37
+ const typedArrayMapping = {
38
+ int8: Int8Array,
39
+ int16: Int16Array,
40
+ int32: Int32Array,
41
+ int64: Float64Array,
42
+ uint8: Uint8Array,
43
+ uint16: Uint16Array,
44
+ uint32: Uint32Array,
45
+ uint64: Float64Array,
46
+ float: Float32Array,
47
+ double: Float64Array
48
+ };
49
+ export default function load(buffer, options) {
50
+ const {
51
+ pointAttributes,
52
+ scale,
53
+ min,
54
+ size,
55
+ offset,
56
+ numPoints
57
+ } = options;
58
+ const view = new DataView(buffer);
59
+ const attributeBuffers = {};
60
+ let attributeOffset = 0;
61
+ let bytesPerPoint = 0;
62
+ for (const pointAttribute of pointAttributes.attributes) {
63
+ bytesPerPoint += pointAttribute.byteSize;
64
+ }
65
+ const gridSize = 32;
66
+ const grid = new Uint32Array(gridSize ** 3);
67
+ const toIndex = (x, y, z) => {
68
+ // min is already subtracted
69
+ const dx = gridSize * x / size.x;
70
+ const dy = gridSize * y / size.y;
71
+ const dz = gridSize * z / size.z;
72
+ const ix = Math.min(parseInt(dx, 10), gridSize - 1);
73
+ const iy = Math.min(parseInt(dy, 10), gridSize - 1);
74
+ const iz = Math.min(parseInt(dz, 10), gridSize - 1);
75
+ return ix + iy * gridSize + iz * gridSize * gridSize;
76
+ };
77
+ let numOccupiedCells = 0;
78
+ for (const pointAttribute of pointAttributes.attributes) {
79
+ if (['POSITION_CARTESIAN', 'position'].includes(pointAttribute.name)) {
80
+ const buff = new ArrayBuffer(numPoints * 4 * 3);
81
+ const positions = new Float32Array(buff);
82
+ for (let j = 0; j < numPoints; j++) {
83
+ const pointOffset = j * bytesPerPoint;
84
+ const x = view.getInt32(pointOffset + attributeOffset + 0, true) * scale[0] + offset[0] - min.x;
85
+ const y = view.getInt32(pointOffset + attributeOffset + 4, true) * scale[1] + offset[1] - min.y;
86
+ const z = view.getInt32(pointOffset + attributeOffset + 8, true) * scale[2] + offset[2] - min.z;
87
+ const index = toIndex(x, y, z);
88
+ const count = grid[index]++;
89
+ if (count === 0) {
90
+ numOccupiedCells++;
91
+ }
92
+ positions[3 * j + 0] = x;
93
+ positions[3 * j + 1] = y;
94
+ positions[3 * j + 2] = z;
95
+ }
96
+ attributeBuffers[pointAttribute.name] = {
97
+ buffer: buff,
98
+ attribute: pointAttribute
99
+ };
100
+ } else if (['RGBA', 'rgba'].includes(pointAttribute.name)) {
101
+ const buff = new ArrayBuffer(numPoints * 4);
102
+ const colors = new Uint8Array(buff);
103
+ for (let j = 0; j < numPoints; j++) {
104
+ const pointOffset = j * bytesPerPoint;
105
+ const r = view.getUint16(pointOffset + attributeOffset + 0, true);
106
+ const g = view.getUint16(pointOffset + attributeOffset + 2, true);
107
+ const b = view.getUint16(pointOffset + attributeOffset + 4, true);
108
+ colors[4 * j + 0] = r > 255 ? r / 256 : r;
109
+ colors[4 * j + 1] = g > 255 ? g / 256 : g;
110
+ colors[4 * j + 2] = b > 255 ? b / 256 : b;
111
+ }
112
+ attributeBuffers[pointAttribute.name] = {
113
+ buffer: buff,
114
+ attribute: pointAttribute
115
+ };
116
+ } else {
117
+ const buff = new ArrayBuffer(numPoints * 4);
118
+ const f32 = new Float32Array(buff);
119
+ const TypedArray = typedArrayMapping[pointAttribute.type.name];
120
+ const preciseBuffer = new TypedArray(numPoints);
121
+ let [offset, scale] = [0, 1];
122
+ const getterMap = {
123
+ int8: view.getInt8,
124
+ int16: view.getInt16,
125
+ int32: view.getInt32,
126
+ uint8: view.getUint8,
127
+ uint16: view.getUint16,
128
+ uint32: view.getUint32,
129
+ float: view.getFloat32,
130
+ double: view.getFloat64
131
+ };
132
+ const getter = getterMap[pointAttribute.type.name].bind(view);
133
+
134
+ // compute offset and scale to pack larger types into 32 bit floats
135
+ if (pointAttribute.type.size > 4) {
136
+ const [amin, amax] = pointAttribute.range;
137
+ offset = amin;
138
+ scale = 1 / (amax - amin);
139
+ }
140
+ for (let j = 0; j < numPoints; j++) {
141
+ const pointOffset = j * bytesPerPoint;
142
+ const value = getter(pointOffset + attributeOffset, true);
143
+ f32[j] = (value - offset) * scale;
144
+ preciseBuffer[j] = value;
145
+ }
146
+ attributeBuffers[pointAttribute.name] = {
147
+ buffer: buff,
148
+ preciseBuffer,
149
+ attribute: pointAttribute,
150
+ offset,
151
+ scale
152
+ };
153
+ }
154
+ attributeOffset += pointAttribute.byteSize;
155
+ }
156
+ const occupancy = parseInt(numPoints / numOccupiedCells, 10);
157
+ {
158
+ // add indices
159
+ const buff = new ArrayBuffer(numPoints * 4);
160
+ const indices = new Uint32Array(buff);
161
+ for (let i = 0; i < numPoints; i++) {
162
+ indices[i] = i;
163
+ }
164
+ attributeBuffers.INDICES = {
165
+ buffer: buff,
166
+ attribute: PointAttribute.INDICES
167
+ };
168
+ }
169
+ {
170
+ // handle attribute vectors
171
+ const vectors = pointAttributes.vectors;
172
+ for (const vector of vectors) {
173
+ const {
174
+ name,
175
+ attributes
176
+ } = vector;
177
+ const numVectorElements = attributes.length;
178
+ const buffer = new ArrayBuffer(numVectorElements * numPoints * 4);
179
+ const f32 = new Float32Array(buffer);
180
+ let iElement = 0;
181
+ for (const sourceName of attributes) {
182
+ const sourceBuffer = attributeBuffers[sourceName];
183
+ const {
184
+ offset,
185
+ scale
186
+ } = sourceBuffer;
187
+ const view = new DataView(sourceBuffer.buffer);
188
+ const getter = view.getFloat32.bind(view);
189
+ for (let j = 0; j < numPoints; j++) {
190
+ const value = getter(j * 4, true);
191
+ f32[j * numVectorElements + iElement] = value / scale + offset;
192
+ }
193
+ iElement++;
194
+ }
195
+ const vecAttribute = new PointAttribute(name, PointAttributeTypes.DATA_TYPE_FLOAT, 3);
196
+ attributeBuffers[name] = {
197
+ buffer,
198
+ attribute: vecAttribute
199
+ };
200
+ }
201
+ }
202
+ return {
203
+ buffer,
204
+ attributeBuffers,
205
+ density: occupancy
206
+ };
207
+ }