PCDLoader.js 10.0 KB

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