PCDLoader.js 10.0 KB

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