PCDLoader.js 10 KB

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