11import * as THREE from 'three' ;
2- import LASLoader from 'Loader/LASLoader ' ;
2+ import { spawn , Thread , Transfer } from 'threads ' ;
33
4- const lasLoader = new LASLoader ( ) ;
4+ let _lazPerf ;
5+ let _thread ;
6+
7+ function workerInstance ( ) {
8+ return new Worker (
9+ /* webpackChunkName: "itowns_lasparser" */
10+ new URL ( '../Worker/LASLoaderWorker.js' , import . meta. url ) ,
11+ { type : 'module' } ,
12+ ) ;
13+ }
14+
15+ async function loader ( ) {
16+ if ( _thread ) { return _thread ; }
17+ _thread = await spawn ( workerInstance ( ) ) ;
18+ if ( _lazPerf ) { _thread . lazPerf ( _lazPerf ) ; }
19+ return _thread ;
20+ }
521
622function buildBufferGeometry ( attributes ) {
723 const geometry = new THREE . BufferGeometry ( ) ;
@@ -53,9 +69,18 @@ export default {
5369 if ( ! path ) {
5470 throw new Error ( 'Path to laz-perf is mandatory' ) ;
5571 }
56- lasLoader . lazPerf = path ;
72+ _lazPerf = path ;
5773 } ,
5874
75+ /**
76+ * Terminate all worker instances.
77+ * @returns {Promise<void> }
78+ */
79+ terminate ( ) {
80+ const currentThread = _thread ;
81+ _thread = undefined ;
82+ return Thread . terminate ( currentThread ) ;
83+ } ,
5984
6085 /**
6186 * Parses a chunk of a LAS or LAZ (LASZip) and returns the corresponding
@@ -79,12 +104,18 @@ export default {
79104 * @return {Promise<THREE.BufferGeometry> } A promise resolving with a
80105 * `THREE.BufferGeometry`.
81106 */
82- parseChunk ( data , options = { } ) {
83- return lasLoader . parseChunk ( data , options . in ) . then ( ( parsedData ) => {
84- const geometry = buildBufferGeometry ( parsedData . attributes ) ;
85- geometry . computeBoundingBox ( ) ;
86- return geometry ;
107+ async parseChunk ( data , options = { } ) {
108+ const lasLoader = await loader ( ) ;
109+ const parsedData = await lasLoader . parseChunk ( Transfer ( data ) , {
110+ pointCount : options . in . pointCount ,
111+ header : options . in . header ,
112+ eb : options . eb ,
113+ colorDepth : options . in . colorDepth ,
87114 } ) ;
115+
116+ const geometry = buildBufferGeometry ( parsedData . attributes ) ;
117+ geometry . computeBoundingBox ( ) ;
118+ return geometry ;
88119 } ,
89120
90121 /**
@@ -101,17 +132,21 @@ export default {
101132 * @return {Promise } A promise resolving with a `THREE.BufferGeometry`. The
102133 * header of the file is contained in `userData`.
103134 */
104- parse ( data , options = { } ) {
135+ async parse ( data , options = { } ) {
105136 if ( options . out ?. skip ) {
106137 console . warn ( "Warning: options 'skip' not supported anymore" ) ;
107138 }
108- return lasLoader . parseFile ( data , {
109- colorDepth : options . in ?. colorDepth ,
110- } ) . then ( ( parsedData ) => {
111- const geometry = buildBufferGeometry ( parsedData . attributes ) ;
112- geometry . userData . header = parsedData . header ;
113- geometry . computeBoundingBox ( ) ;
114- return geometry ;
139+
140+ const input = options . in ;
141+
142+ const lasLoader = await loader ( ) ;
143+ const parsedData = await lasLoader . parseFile ( Transfer ( data ) , {
144+ colorDepth : input ?. colorDepth ,
115145 } ) ;
146+
147+ const geometry = buildBufferGeometry ( parsedData . attributes ) ;
148+ geometry . userData . header = parsedData . header ;
149+ geometry . computeBoundingBox ( ) ;
150+ return geometry ;
116151 } ,
117152} ;
0 commit comments