PCDLoader.js 10 KB

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