PCDLoader.js 12 KB

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