PCDLoader.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. ( function () {
  2. class PCDLoader extends THREE.Loader {
  3. constructor( manager ) {
  4. super( manager );
  5. this.littleEndian = true;
  6. }
  7. load( url, onLoad, onProgress, onError ) {
  8. const scope = this;
  9. const loader = new THREE.FileLoader( scope.manager );
  10. loader.setPath( scope.path );
  11. loader.setResponseType( 'arraybuffer' );
  12. loader.setRequestHeader( scope.requestHeader );
  13. loader.setWithCredentials( scope.withCredentials );
  14. loader.load( url, function ( data ) {
  15. try {
  16. onLoad( scope.parse( data ) );
  17. } catch ( e ) {
  18. if ( onError ) {
  19. onError( e );
  20. } else {
  21. console.error( e );
  22. }
  23. scope.manager.itemError( url );
  24. }
  25. }, onProgress, onError );
  26. }
  27. parse( data ) {
  28. // from https://gitlab.com/taketwo/three-pcd-loader/blob/master/decompress-lzf.js
  29. function decompressLZF( inData, outLength ) {
  30. const inLength = inData.length;
  31. const outData = new Uint8Array( outLength );
  32. let inPtr = 0;
  33. let outPtr = 0;
  34. let ctrl;
  35. let len;
  36. let ref;
  37. do {
  38. ctrl = inData[ inPtr ++ ];
  39. if ( ctrl < 1 << 5 ) {
  40. ctrl ++;
  41. if ( outPtr + ctrl > outLength ) throw new Error( 'Output buffer is not large enough' );
  42. if ( inPtr + ctrl > inLength ) throw new Error( 'Invalid compressed data' );
  43. do {
  44. outData[ outPtr ++ ] = inData[ inPtr ++ ];
  45. } while ( -- ctrl );
  46. } else {
  47. len = ctrl >> 5;
  48. ref = outPtr - ( ( ctrl & 0x1f ) << 8 ) - 1;
  49. if ( inPtr >= inLength ) throw new Error( 'Invalid compressed data' );
  50. if ( len === 7 ) {
  51. len += inData[ inPtr ++ ];
  52. if ( inPtr >= inLength ) throw new Error( 'Invalid compressed data' );
  53. }
  54. ref -= inData[ inPtr ++ ];
  55. if ( outPtr + len + 2 > outLength ) throw new Error( 'Output buffer is not large enough' );
  56. if ( ref < 0 ) throw new Error( 'Invalid compressed data' );
  57. if ( ref >= outPtr ) throw new Error( 'Invalid compressed data' );
  58. do {
  59. outData[ outPtr ++ ] = outData[ ref ++ ];
  60. } while ( -- len + 2 );
  61. }
  62. } while ( inPtr < inLength );
  63. return outData;
  64. }
  65. function parseHeader( data ) {
  66. const PCDheader = {};
  67. const result1 = data.search( /[\r\n]DATA\s(\S*)\s/i );
  68. const result2 = /[\r\n]DATA\s(\S*)\s/i.exec( data.slice( result1 - 1 ) );
  69. PCDheader.data = result2[ 1 ];
  70. PCDheader.headerLen = result2[ 0 ].length + result1;
  71. PCDheader.str = data.slice( 0, PCDheader.headerLen ); // remove comments
  72. PCDheader.str = PCDheader.str.replace( /\#.*/gi, '' ); // parse
  73. PCDheader.version = /VERSION (.*)/i.exec( PCDheader.str );
  74. PCDheader.fields = /FIELDS (.*)/i.exec( PCDheader.str );
  75. PCDheader.size = /SIZE (.*)/i.exec( PCDheader.str );
  76. PCDheader.type = /TYPE (.*)/i.exec( PCDheader.str );
  77. PCDheader.count = /COUNT (.*)/i.exec( PCDheader.str );
  78. PCDheader.width = /WIDTH (.*)/i.exec( PCDheader.str );
  79. PCDheader.height = /HEIGHT (.*)/i.exec( PCDheader.str );
  80. PCDheader.viewpoint = /VIEWPOINT (.*)/i.exec( PCDheader.str );
  81. PCDheader.points = /POINTS (.*)/i.exec( PCDheader.str ); // evaluate
  82. if ( PCDheader.version !== null ) PCDheader.version = parseFloat( PCDheader.version[ 1 ] );
  83. PCDheader.fields = PCDheader.fields !== null ? PCDheader.fields[ 1 ].split( ' ' ) : [];
  84. if ( PCDheader.type !== null ) PCDheader.type = PCDheader.type[ 1 ].split( ' ' );
  85. if ( PCDheader.width !== null ) PCDheader.width = parseInt( PCDheader.width[ 1 ] );
  86. if ( PCDheader.height !== null ) PCDheader.height = parseInt( PCDheader.height[ 1 ] );
  87. if ( PCDheader.viewpoint !== null ) PCDheader.viewpoint = PCDheader.viewpoint[ 1 ];
  88. if ( PCDheader.points !== null ) PCDheader.points = parseInt( PCDheader.points[ 1 ], 10 );
  89. if ( PCDheader.points === null ) PCDheader.points = PCDheader.width * PCDheader.height;
  90. if ( PCDheader.size !== null ) {
  91. PCDheader.size = PCDheader.size[ 1 ].split( ' ' ).map( function ( x ) {
  92. return parseInt( x, 10 );
  93. } );
  94. }
  95. if ( PCDheader.count !== null ) {
  96. PCDheader.count = PCDheader.count[ 1 ].split( ' ' ).map( function ( x ) {
  97. return parseInt( x, 10 );
  98. } );
  99. } else {
  100. PCDheader.count = [];
  101. for ( let i = 0, l = PCDheader.fields.length; i < l; i ++ ) {
  102. PCDheader.count.push( 1 );
  103. }
  104. }
  105. PCDheader.offset = {};
  106. let sizeSum = 0;
  107. for ( let i = 0, l = PCDheader.fields.length; i < l; i ++ ) {
  108. if ( PCDheader.data === 'ascii' ) {
  109. PCDheader.offset[ PCDheader.fields[ i ] ] = i;
  110. } else {
  111. PCDheader.offset[ PCDheader.fields[ i ] ] = sizeSum;
  112. sizeSum += PCDheader.size[ i ] * PCDheader.count[ i ];
  113. }
  114. } // for binary only
  115. PCDheader.rowSize = sizeSum;
  116. return PCDheader;
  117. }
  118. const textData = THREE.LoaderUtils.decodeText( new Uint8Array( data ) ); // parse header (always ascii format)
  119. const PCDheader = parseHeader( textData ); // parse data
  120. const position = [];
  121. const normal = [];
  122. const color = [];
  123. const intensity = [];
  124. const label = []; // ascii
  125. if ( PCDheader.data === 'ascii' ) {
  126. const offset = PCDheader.offset;
  127. const pcdData = textData.slice( PCDheader.headerLen );
  128. const lines = pcdData.split( '\n' );
  129. for ( let i = 0, l = lines.length; i < l; i ++ ) {
  130. if ( lines[ i ] === '' ) continue;
  131. const line = lines[ i ].split( ' ' );
  132. if ( offset.x !== undefined ) {
  133. position.push( parseFloat( line[ offset.x ] ) );
  134. position.push( parseFloat( line[ offset.y ] ) );
  135. position.push( parseFloat( line[ offset.z ] ) );
  136. }
  137. if ( offset.rgb !== undefined ) {
  138. const rgb_field_index = PCDheader.fields.findIndex( field => field === 'rgb' );
  139. const rgb_type = PCDheader.type[ rgb_field_index ];
  140. const float = parseFloat( line[ offset.rgb ] );
  141. let rgb = float;
  142. if ( rgb_type === 'F' ) {
  143. // treat float values as int
  144. // https://github.com/daavoo/pyntcloud/pull/204/commits/7b4205e64d5ed09abe708b2e91b615690c24d518
  145. const farr = new Float32Array( 1 );
  146. farr[ 0 ] = float;
  147. rgb = new Int32Array( farr.buffer )[ 0 ];
  148. }
  149. const r = rgb >> 16 & 0x0000ff;
  150. const g = rgb >> 8 & 0x0000ff;
  151. const b = rgb >> 0 & 0x0000ff;
  152. color.push( r / 255, g / 255, b / 255 );
  153. }
  154. if ( offset.normal_x !== undefined ) {
  155. normal.push( parseFloat( line[ offset.normal_x ] ) );
  156. normal.push( parseFloat( line[ offset.normal_y ] ) );
  157. normal.push( parseFloat( line[ offset.normal_z ] ) );
  158. }
  159. if ( offset.intensity !== undefined ) {
  160. intensity.push( parseFloat( line[ offset.intensity ] ) );
  161. }
  162. if ( offset.label !== undefined ) {
  163. label.push( parseInt( line[ offset.label ] ) );
  164. }
  165. }
  166. } // binary-compressed
  167. // normally data in PCD files are organized as array of structures: XYZRGBXYZRGB
  168. // binary compressed PCD files organize their data as structure of arrays: XXYYZZRGBRGB
  169. // that requires a totally different parsing approach compared to non-compressed data
  170. if ( PCDheader.data === 'binary_compressed' ) {
  171. const sizes = new Uint32Array( data.slice( PCDheader.headerLen, PCDheader.headerLen + 8 ) );
  172. const compressedSize = sizes[ 0 ];
  173. const decompressedSize = sizes[ 1 ];
  174. const decompressed = decompressLZF( new Uint8Array( data, PCDheader.headerLen + 8, compressedSize ), decompressedSize );
  175. const dataview = new DataView( decompressed.buffer );
  176. const offset = PCDheader.offset;
  177. for ( let i = 0; i < PCDheader.points; i ++ ) {
  178. if ( offset.x !== undefined ) {
  179. const xIndex = PCDheader.fields.indexOf( 'x' );
  180. const yIndex = PCDheader.fields.indexOf( 'y' );
  181. const zIndex = PCDheader.fields.indexOf( 'z' );
  182. position.push( dataview.getFloat32( PCDheader.points * offset.x + PCDheader.size[ xIndex ] * i, this.littleEndian ) );
  183. position.push( dataview.getFloat32( PCDheader.points * offset.y + PCDheader.size[ yIndex ] * i, this.littleEndian ) );
  184. position.push( dataview.getFloat32( PCDheader.points * offset.z + PCDheader.size[ zIndex ] * i, this.littleEndian ) );
  185. }
  186. if ( offset.rgb !== undefined ) {
  187. const rgbIndex = PCDheader.fields.indexOf( 'rgb' );
  188. color.push( dataview.getUint8( PCDheader.points * offset.rgb + PCDheader.size[ rgbIndex ] * i + 2 ) / 255.0 );
  189. color.push( dataview.getUint8( PCDheader.points * offset.rgb + PCDheader.size[ rgbIndex ] * i + 1 ) / 255.0 );
  190. color.push( dataview.getUint8( PCDheader.points * offset.rgb + PCDheader.size[ rgbIndex ] * i + 0 ) / 255.0 );
  191. }
  192. if ( offset.normal_x !== undefined ) {
  193. const xIndex = PCDheader.fields.indexOf( 'normal_x' );
  194. const yIndex = PCDheader.fields.indexOf( 'normal_y' );
  195. const zIndex = PCDheader.fields.indexOf( 'normal_z' );
  196. normal.push( dataview.getFloat32( PCDheader.points * offset.normal_x + PCDheader.size[ xIndex ] * i, this.littleEndian ) );
  197. normal.push( dataview.getFloat32( PCDheader.points * offset.normal_y + PCDheader.size[ yIndex ] * i, this.littleEndian ) );
  198. normal.push( dataview.getFloat32( PCDheader.points * offset.normal_z + PCDheader.size[ zIndex ] * i, this.littleEndian ) );
  199. }
  200. if ( offset.intensity !== undefined ) {
  201. const intensityIndex = PCDheader.fields.indexOf( 'intensity' );
  202. intensity.push( dataview.getFloat32( PCDheader.points * offset.intensity + PCDheader.size[ intensityIndex ] * i, this.littleEndian ) );
  203. }
  204. if ( offset.label !== undefined ) {
  205. const labelIndex = PCDheader.fields.indexOf( 'label' );
  206. label.push( dataview.getInt32( PCDheader.points * offset.label + PCDheader.size[ labelIndex ] * i, this.littleEndian ) );
  207. }
  208. }
  209. } // binary
  210. if ( PCDheader.data === 'binary' ) {
  211. const dataview = new DataView( data, PCDheader.headerLen );
  212. const offset = PCDheader.offset;
  213. for ( let i = 0, row = 0; i < PCDheader.points; i ++, row += PCDheader.rowSize ) {
  214. if ( offset.x !== undefined ) {
  215. position.push( dataview.getFloat32( row + offset.x, this.littleEndian ) );
  216. position.push( dataview.getFloat32( row + offset.y, this.littleEndian ) );
  217. position.push( dataview.getFloat32( row + offset.z, this.littleEndian ) );
  218. }
  219. if ( offset.rgb !== undefined ) {
  220. color.push( dataview.getUint8( row + offset.rgb + 2 ) / 255.0 );
  221. color.push( dataview.getUint8( row + offset.rgb + 1 ) / 255.0 );
  222. color.push( dataview.getUint8( row + offset.rgb + 0 ) / 255.0 );
  223. }
  224. if ( offset.normal_x !== undefined ) {
  225. normal.push( dataview.getFloat32( row + offset.normal_x, this.littleEndian ) );
  226. normal.push( dataview.getFloat32( row + offset.normal_y, this.littleEndian ) );
  227. normal.push( dataview.getFloat32( row + offset.normal_z, this.littleEndian ) );
  228. }
  229. if ( offset.intensity !== undefined ) {
  230. intensity.push( dataview.getFloat32( row + offset.intensity, this.littleEndian ) );
  231. }
  232. if ( offset.label !== undefined ) {
  233. label.push( dataview.getInt32( row + offset.label, this.littleEndian ) );
  234. }
  235. }
  236. } // build geometry
  237. const geometry = new THREE.BufferGeometry();
  238. if ( position.length > 0 ) geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( position, 3 ) );
  239. if ( normal.length > 0 ) geometry.setAttribute( 'normal', new THREE.Float32BufferAttribute( normal, 3 ) );
  240. if ( color.length > 0 ) geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( color, 3 ) );
  241. if ( intensity.length > 0 ) geometry.setAttribute( 'intensity', new THREE.Float32BufferAttribute( intensity, 1 ) );
  242. if ( label.length > 0 ) geometry.setAttribute( 'label', new THREE.Int32BufferAttribute( label, 1 ) );
  243. geometry.computeBoundingSphere(); // build material
  244. const material = new THREE.PointsMaterial( {
  245. size: 0.005
  246. } );
  247. if ( color.length > 0 ) {
  248. material.vertexColors = true;
  249. } // build point cloud
  250. return new THREE.Points( geometry, material );
  251. }
  252. }
  253. THREE.PCDLoader = PCDLoader;
  254. } )();