PCDLoader.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. THREE.PCDLoader = function ( manager ) {
  2. THREE.Loader.call( this, manager );
  3. this.littleEndian = true;
  4. };
  5. THREE.PCDLoader.prototype = Object.assign( Object.create( THREE.Loader.prototype ), {
  6. constructor: THREE.PCDLoader,
  7. load: function ( url, onLoad, onProgress, onError ) {
  8. var scope = this;
  9. var 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: function ( data, url ) {
  28. // from https://gitlab.com/taketwo/three-pcd-loader/blob/master/decompress-lzf.js
  29. function decompressLZF( inData, outLength ) {
  30. var inLength = inData.length;
  31. var outData = new Uint8Array( outLength );
  32. var inPtr = 0;
  33. var outPtr = 0;
  34. var ctrl;
  35. var len;
  36. var 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. var PCDheader = {};
  67. var result1 = data.search( /[\r\n]DATA\s(\S*)\s/i );
  68. var 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 );
  72. // remove comments
  73. PCDheader.str = PCDheader.str.replace( /\#.*/gi, '' );
  74. // parse
  75. PCDheader.version = /VERSION (.*)/i.exec( PCDheader.str );
  76. PCDheader.fields = /FIELDS (.*)/i.exec( PCDheader.str );
  77. PCDheader.size = /SIZE (.*)/i.exec( PCDheader.str );
  78. PCDheader.type = /TYPE (.*)/i.exec( PCDheader.str );
  79. PCDheader.count = /COUNT (.*)/i.exec( PCDheader.str );
  80. PCDheader.width = /WIDTH (.*)/i.exec( PCDheader.str );
  81. PCDheader.height = /HEIGHT (.*)/i.exec( PCDheader.str );
  82. PCDheader.viewpoint = /VIEWPOINT (.*)/i.exec( PCDheader.str );
  83. PCDheader.points = /POINTS (.*)/i.exec( PCDheader.str );
  84. // evaluate
  85. if ( PCDheader.version !== null )
  86. PCDheader.version = parseFloat( PCDheader.version[ 1 ] );
  87. if ( PCDheader.fields !== null )
  88. PCDheader.fields = PCDheader.fields[ 1 ].split( ' ' );
  89. if ( PCDheader.type !== null )
  90. PCDheader.type = PCDheader.type[ 1 ].split( ' ' );
  91. if ( PCDheader.width !== null )
  92. PCDheader.width = parseInt( PCDheader.width[ 1 ] );
  93. if ( PCDheader.height !== null )
  94. PCDheader.height = parseInt( PCDheader.height[ 1 ] );
  95. if ( PCDheader.viewpoint !== null )
  96. PCDheader.viewpoint = PCDheader.viewpoint[ 1 ];
  97. if ( PCDheader.points !== null )
  98. PCDheader.points = parseInt( PCDheader.points[ 1 ], 10 );
  99. if ( PCDheader.points === null )
  100. PCDheader.points = PCDheader.width * PCDheader.height;
  101. if ( PCDheader.size !== null ) {
  102. PCDheader.size = PCDheader.size[ 1 ].split( ' ' ).map( function ( x ) {
  103. return parseInt( x, 10 );
  104. } );
  105. }
  106. if ( PCDheader.count !== null ) {
  107. PCDheader.count = PCDheader.count[ 1 ].split( ' ' ).map( function ( x ) {
  108. return parseInt( x, 10 );
  109. } );
  110. } else {
  111. PCDheader.count = [];
  112. for ( var i = 0, l = PCDheader.fields.length; i < l; i ++ ) {
  113. PCDheader.count.push( 1 );
  114. }
  115. }
  116. PCDheader.offset = {};
  117. var sizeSum = 0;
  118. for ( var i = 0, l = PCDheader.fields.length; i < l; i ++ ) {
  119. if ( PCDheader.data === 'ascii' ) {
  120. PCDheader.offset[ PCDheader.fields[ i ] ] = i;
  121. } else {
  122. PCDheader.offset[ PCDheader.fields[ i ] ] = sizeSum;
  123. sizeSum += PCDheader.size[ i ] * PCDheader.count[ i ];
  124. }
  125. }
  126. // for binary only
  127. PCDheader.rowSize = sizeSum;
  128. return PCDheader;
  129. }
  130. var textData = THREE.LoaderUtils.decodeText( new Uint8Array( data ) );
  131. // parse header (always ascii format)
  132. var PCDheader = parseHeader( textData );
  133. // parse data
  134. var position = [];
  135. var normal = [];
  136. var color = [];
  137. // ascii
  138. if ( PCDheader.data === 'ascii' ) {
  139. var offset = PCDheader.offset;
  140. var pcdData = textData.substr( PCDheader.headerLen );
  141. var lines = pcdData.split( '\n' );
  142. for ( var i = 0, l = lines.length; i < l; i ++ ) {
  143. if ( lines[ i ] === '' ) continue;
  144. var line = lines[ i ].split( ' ' );
  145. if ( offset.x !== undefined ) {
  146. position.push( parseFloat( line[ offset.x ] ) );
  147. position.push( parseFloat( line[ offset.y ] ) );
  148. position.push( parseFloat( line[ offset.z ] ) );
  149. }
  150. if ( offset.rgb !== undefined ) {
  151. var rgb = parseFloat( line[ offset.rgb ] );
  152. var r = ( rgb >> 16 ) & 0x0000ff;
  153. var g = ( rgb >> 8 ) & 0x0000ff;
  154. var b = ( rgb >> 0 ) & 0x0000ff;
  155. color.push( r / 255, g / 255, b / 255 );
  156. }
  157. if ( offset.normal_x !== undefined ) {
  158. normal.push( parseFloat( line[ offset.normal_x ] ) );
  159. normal.push( parseFloat( line[ offset.normal_y ] ) );
  160. normal.push( parseFloat( line[ offset.normal_z ] ) );
  161. }
  162. }
  163. }
  164. // binary-compressed
  165. // normally data in PCD files are organized as array of structures: XYZRGBXYZRGB
  166. // binary compressed PCD files organize their data as structure of arrays: XXYYZZRGBRGB
  167. // that requires a totally different parsing approach compared to non-compressed data
  168. if ( PCDheader.data === 'binary_compressed' ) {
  169. var sizes = new Uint32Array( data.slice( PCDheader.headerLen, PCDheader.headerLen + 8 ) );
  170. var compressedSize = sizes[ 0 ];
  171. var decompressedSize = sizes[ 1 ];
  172. var decompressed = decompressLZF( new Uint8Array( data, PCDheader.headerLen + 8, compressedSize ), decompressedSize );
  173. var dataview = new DataView( decompressed.buffer );
  174. var offset = PCDheader.offset;
  175. for ( var i = 0; i < PCDheader.points; i ++ ) {
  176. if ( offset.x !== undefined ) {
  177. position.push( dataview.getFloat32( ( PCDheader.points * offset.x ) + PCDheader.size[ 0 ] * i, this.littleEndian ) );
  178. position.push( dataview.getFloat32( ( PCDheader.points * offset.y ) + PCDheader.size[ 1 ] * i, this.littleEndian ) );
  179. position.push( dataview.getFloat32( ( PCDheader.points * offset.z ) + PCDheader.size[ 2 ] * i, this.littleEndian ) );
  180. }
  181. if ( offset.rgb !== undefined ) {
  182. color.push( dataview.getUint8( ( PCDheader.points * offset.rgb ) + PCDheader.size[ 3 ] * i + 0 ) / 255.0 );
  183. color.push( dataview.getUint8( ( PCDheader.points * offset.rgb ) + PCDheader.size[ 3 ] * i + 1 ) / 255.0 );
  184. color.push( dataview.getUint8( ( PCDheader.points * offset.rgb ) + PCDheader.size[ 3 ] * i + 2 ) / 255.0 );
  185. }
  186. if ( offset.normal_x !== undefined ) {
  187. normal.push( dataview.getFloat32( ( PCDheader.points * offset.normal_x ) + PCDheader.size[ 4 ] * i, this.littleEndian ) );
  188. normal.push( dataview.getFloat32( ( PCDheader.points * offset.normal_y ) + PCDheader.size[ 5 ] * i, this.littleEndian ) );
  189. normal.push( dataview.getFloat32( ( PCDheader.points * offset.normal_z ) + PCDheader.size[ 6 ] * i, this.littleEndian ) );
  190. }
  191. }
  192. }
  193. // binary
  194. if ( PCDheader.data === 'binary' ) {
  195. var dataview = new DataView( data, PCDheader.headerLen );
  196. var offset = PCDheader.offset;
  197. for ( var i = 0, row = 0; i < PCDheader.points; i ++, row += PCDheader.rowSize ) {
  198. if ( offset.x !== undefined ) {
  199. position.push( dataview.getFloat32( row + offset.x, this.littleEndian ) );
  200. position.push( dataview.getFloat32( row + offset.y, this.littleEndian ) );
  201. position.push( dataview.getFloat32( row + offset.z, this.littleEndian ) );
  202. }
  203. if ( offset.rgb !== undefined ) {
  204. color.push( dataview.getUint8( row + offset.rgb + 2 ) / 255.0 );
  205. color.push( dataview.getUint8( row + offset.rgb + 1 ) / 255.0 );
  206. color.push( dataview.getUint8( row + offset.rgb + 0 ) / 255.0 );
  207. }
  208. if ( offset.normal_x !== undefined ) {
  209. normal.push( dataview.getFloat32( row + offset.normal_x, this.littleEndian ) );
  210. normal.push( dataview.getFloat32( row + offset.normal_y, this.littleEndian ) );
  211. normal.push( dataview.getFloat32( row + offset.normal_z, this.littleEndian ) );
  212. }
  213. }
  214. }
  215. // build geometry
  216. var geometry = new THREE.BufferGeometry();
  217. if ( position.length > 0 ) geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( position, 3 ) );
  218. if ( normal.length > 0 ) geometry.setAttribute( 'normal', new THREE.Float32BufferAttribute( normal, 3 ) );
  219. if ( color.length > 0 ) geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( color, 3 ) );
  220. geometry.computeBoundingSphere();
  221. // build material
  222. var material = new THREE.PointsMaterial( { size: 0.005 } );
  223. if ( color.length > 0 ) {
  224. material.vertexColors = true;
  225. } else {
  226. material.color.setHex( Math.random() * 0xffffff );
  227. }
  228. // build point cloud
  229. var mesh = new THREE.Points( geometry, material );
  230. var name = url.split( '' ).reverse().join( '' );
  231. name = /([^\/]*)/.exec( name );
  232. name = name[ 1 ].split( '' ).reverse().join( '' );
  233. mesh.name = name;
  234. return mesh;
  235. }
  236. } );