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