PCDLoader.js 10 KB

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