PCDLoader.js 10 KB

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