PCDLoader.js 10 KB

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