PCDLoader.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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, url ) );
  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, url ) {
  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.substr( result1 - 1 ) );
  69. PCDheader.data = result2[ 1 ];
  70. PCDheader.headerLen = result2[ 0 ].length + result1;
  71. PCDheader.str = data.substr( 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. if ( PCDheader.fields !== null ) PCDheader.fields = 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 = []; // ascii
  123. if ( PCDheader.data === 'ascii' ) {
  124. const offset = PCDheader.offset;
  125. const pcdData = textData.substr( PCDheader.headerLen );
  126. const lines = pcdData.split( '\n' );
  127. for ( let i = 0, l = lines.length; i < l; i ++ ) {
  128. if ( lines[ i ] === '' ) continue;
  129. const line = lines[ i ].split( ' ' );
  130. if ( offset.x !== undefined ) {
  131. position.push( parseFloat( line[ offset.x ] ) );
  132. position.push( parseFloat( line[ offset.y ] ) );
  133. position.push( parseFloat( line[ offset.z ] ) );
  134. }
  135. if ( offset.rgb !== undefined ) {
  136. const rgb = parseFloat( line[ offset.rgb ] );
  137. const r = rgb >> 16 & 0x0000ff;
  138. const g = rgb >> 8 & 0x0000ff;
  139. const b = rgb >> 0 & 0x0000ff;
  140. color.push( r / 255, g / 255, b / 255 );
  141. }
  142. if ( offset.normal_x !== undefined ) {
  143. normal.push( parseFloat( line[ offset.normal_x ] ) );
  144. normal.push( parseFloat( line[ offset.normal_y ] ) );
  145. normal.push( parseFloat( line[ offset.normal_z ] ) );
  146. }
  147. }
  148. } // binary-compressed
  149. // normally data in PCD files are organized as array of structures: XYZRGBXYZRGB
  150. // binary compressed PCD files organize their data as structure of arrays: XXYYZZRGBRGB
  151. // that requires a totally different parsing approach compared to non-compressed data
  152. if ( PCDheader.data === 'binary_compressed' ) {
  153. const sizes = new Uint32Array( data.slice( PCDheader.headerLen, PCDheader.headerLen + 8 ) );
  154. const compressedSize = sizes[ 0 ];
  155. const decompressedSize = sizes[ 1 ];
  156. const decompressed = decompressLZF( new Uint8Array( data, PCDheader.headerLen + 8, compressedSize ), decompressedSize );
  157. const dataview = new DataView( decompressed.buffer );
  158. const offset = PCDheader.offset;
  159. for ( let i = 0; i < PCDheader.points; i ++ ) {
  160. if ( offset.x !== undefined ) {
  161. position.push( dataview.getFloat32( PCDheader.points * offset.x + PCDheader.size[ 0 ] * i, this.littleEndian ) );
  162. position.push( dataview.getFloat32( PCDheader.points * offset.y + PCDheader.size[ 1 ] * i, this.littleEndian ) );
  163. position.push( dataview.getFloat32( PCDheader.points * offset.z + PCDheader.size[ 2 ] * i, this.littleEndian ) );
  164. }
  165. if ( offset.rgb !== undefined ) {
  166. color.push( dataview.getUint8( PCDheader.points * offset.rgb + PCDheader.size[ 3 ] * i + 0 ) / 255.0 );
  167. color.push( dataview.getUint8( PCDheader.points * offset.rgb + PCDheader.size[ 3 ] * i + 1 ) / 255.0 );
  168. color.push( dataview.getUint8( PCDheader.points * offset.rgb + PCDheader.size[ 3 ] * i + 2 ) / 255.0 );
  169. }
  170. if ( offset.normal_x !== undefined ) {
  171. normal.push( dataview.getFloat32( PCDheader.points * offset.normal_x + PCDheader.size[ 4 ] * i, this.littleEndian ) );
  172. normal.push( dataview.getFloat32( PCDheader.points * offset.normal_y + PCDheader.size[ 5 ] * i, this.littleEndian ) );
  173. normal.push( dataview.getFloat32( PCDheader.points * offset.normal_z + PCDheader.size[ 6 ] * i, this.littleEndian ) );
  174. }
  175. }
  176. } // binary
  177. if ( PCDheader.data === 'binary' ) {
  178. const dataview = new DataView( data, PCDheader.headerLen );
  179. const offset = PCDheader.offset;
  180. for ( let i = 0, row = 0; i < PCDheader.points; i ++, row += PCDheader.rowSize ) {
  181. if ( offset.x !== undefined ) {
  182. position.push( dataview.getFloat32( row + offset.x, this.littleEndian ) );
  183. position.push( dataview.getFloat32( row + offset.y, this.littleEndian ) );
  184. position.push( dataview.getFloat32( row + offset.z, this.littleEndian ) );
  185. }
  186. if ( offset.rgb !== undefined ) {
  187. color.push( dataview.getUint8( row + offset.rgb + 2 ) / 255.0 );
  188. color.push( dataview.getUint8( row + offset.rgb + 1 ) / 255.0 );
  189. color.push( dataview.getUint8( row + offset.rgb + 0 ) / 255.0 );
  190. }
  191. if ( offset.normal_x !== undefined ) {
  192. normal.push( dataview.getFloat32( row + offset.normal_x, this.littleEndian ) );
  193. normal.push( dataview.getFloat32( row + offset.normal_y, this.littleEndian ) );
  194. normal.push( dataview.getFloat32( row + offset.normal_z, this.littleEndian ) );
  195. }
  196. }
  197. } // build geometry
  198. const geometry = new THREE.BufferGeometry();
  199. if ( position.length > 0 ) geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( position, 3 ) );
  200. if ( normal.length > 0 ) geometry.setAttribute( 'normal', new THREE.Float32BufferAttribute( normal, 3 ) );
  201. if ( color.length > 0 ) geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( color, 3 ) );
  202. geometry.computeBoundingSphere(); // build material
  203. const material = new THREE.PointsMaterial( {
  204. size: 0.005
  205. } );
  206. if ( color.length > 0 ) {
  207. material.vertexColors = true;
  208. } else {
  209. material.color.setHex( Math.random() * 0xffffff );
  210. } // build point cloud
  211. const mesh = new THREE.Points( geometry, material );
  212. let name = url.split( '' ).reverse().join( '' );
  213. name = /([^\/]*)/.exec( name );
  214. name = name[ 1 ].split( '' ).reverse().join( '' );
  215. mesh.name = name;
  216. return mesh;
  217. }
  218. }
  219. THREE.PCDLoader = PCDLoader;
  220. } )();