PCDLoader.js 10 KB

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