PCDLoader.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. import {
  2. BufferGeometry,
  3. FileLoader,
  4. Float32BufferAttribute,
  5. Int32BufferAttribute,
  6. Loader,
  7. LoaderUtils,
  8. Points,
  9. PointsMaterial
  10. } from 'three';
  11. class PCDLoader extends Loader {
  12. constructor( manager ) {
  13. super( manager );
  14. this.littleEndian = true;
  15. }
  16. load( url, onLoad, onProgress, onError ) {
  17. const scope = this;
  18. const 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 ) );
  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( data ) {
  37. // from https://gitlab.com/taketwo/three-pcd-loader/blob/master/decompress-lzf.js
  38. function decompressLZF( inData, outLength ) {
  39. const inLength = inData.length;
  40. const outData = new Uint8Array( outLength );
  41. let inPtr = 0;
  42. let outPtr = 0;
  43. let ctrl;
  44. let len;
  45. let 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. const PCDheader = {};
  76. const result1 = data.search( /[\r\n]DATA\s(\S*)\s/i );
  77. const result2 = /[\r\n]DATA\s(\S*)\s/i.exec( data.slice( result1 - 1 ) );
  78. PCDheader.data = result2[ 1 ];
  79. PCDheader.headerLen = result2[ 0 ].length + result1;
  80. PCDheader.str = data.slice( 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. PCDheader.fields = ( PCDheader.fields !== null ) ? PCDheader.fields[ 1 ].split( ' ' ) : [];
  97. if ( PCDheader.type !== null )
  98. PCDheader.type = PCDheader.type[ 1 ].split( ' ' );
  99. if ( PCDheader.width !== null )
  100. PCDheader.width = parseInt( PCDheader.width[ 1 ] );
  101. if ( PCDheader.height !== null )
  102. PCDheader.height = parseInt( PCDheader.height[ 1 ] );
  103. if ( PCDheader.viewpoint !== null )
  104. PCDheader.viewpoint = PCDheader.viewpoint[ 1 ];
  105. if ( PCDheader.points !== null )
  106. PCDheader.points = parseInt( PCDheader.points[ 1 ], 10 );
  107. if ( PCDheader.points === null )
  108. PCDheader.points = PCDheader.width * PCDheader.height;
  109. if ( PCDheader.size !== null ) {
  110. PCDheader.size = PCDheader.size[ 1 ].split( ' ' ).map( function ( x ) {
  111. return parseInt( x, 10 );
  112. } );
  113. }
  114. if ( PCDheader.count !== null ) {
  115. PCDheader.count = PCDheader.count[ 1 ].split( ' ' ).map( function ( x ) {
  116. return parseInt( x, 10 );
  117. } );
  118. } else {
  119. PCDheader.count = [];
  120. for ( let i = 0, l = PCDheader.fields.length; i < l; i ++ ) {
  121. PCDheader.count.push( 1 );
  122. }
  123. }
  124. PCDheader.offset = {};
  125. let sizeSum = 0;
  126. for ( let i = 0, l = PCDheader.fields.length; i < l; i ++ ) {
  127. if ( PCDheader.data === 'ascii' ) {
  128. PCDheader.offset[ PCDheader.fields[ i ] ] = i;
  129. } else {
  130. PCDheader.offset[ PCDheader.fields[ i ] ] = sizeSum;
  131. sizeSum += PCDheader.size[ i ] * PCDheader.count[ i ];
  132. }
  133. }
  134. // for binary only
  135. PCDheader.rowSize = sizeSum;
  136. return PCDheader;
  137. }
  138. const textData = LoaderUtils.decodeText( new Uint8Array( data ) );
  139. // parse header (always ascii format)
  140. const PCDheader = parseHeader( textData );
  141. // parse data
  142. const position = [];
  143. const normal = [];
  144. const color = [];
  145. const intensity = [];
  146. const label = [];
  147. // ascii
  148. if ( PCDheader.data === 'ascii' ) {
  149. const offset = PCDheader.offset;
  150. const pcdData = textData.slice( PCDheader.headerLen );
  151. const lines = pcdData.split( '\n' );
  152. for ( let i = 0, l = lines.length; i < l; i ++ ) {
  153. if ( lines[ i ] === '' ) continue;
  154. const line = lines[ i ].split( ' ' );
  155. if ( offset.x !== undefined ) {
  156. position.push( parseFloat( line[ offset.x ] ) );
  157. position.push( parseFloat( line[ offset.y ] ) );
  158. position.push( parseFloat( line[ offset.z ] ) );
  159. }
  160. if ( offset.rgb !== undefined ) {
  161. const rgb_field_index = PCDheader.fields.findIndex( ( field ) => field === 'rgb' );
  162. const rgb_type = PCDheader.type[ rgb_field_index ];
  163. const float = parseFloat( line[ offset.rgb ] );
  164. let rgb = float;
  165. if ( rgb_type === 'F' ) {
  166. // treat float values as int
  167. // https://github.com/daavoo/pyntcloud/pull/204/commits/7b4205e64d5ed09abe708b2e91b615690c24d518
  168. const farr = new Float32Array( 1 );
  169. farr[ 0 ] = float;
  170. rgb = new Int32Array( farr.buffer )[ 0 ];
  171. }
  172. const r = ( rgb >> 16 ) & 0x0000ff;
  173. const g = ( rgb >> 8 ) & 0x0000ff;
  174. const b = ( rgb >> 0 ) & 0x0000ff;
  175. color.push( r / 255, g / 255, b / 255 );
  176. }
  177. if ( offset.normal_x !== undefined ) {
  178. normal.push( parseFloat( line[ offset.normal_x ] ) );
  179. normal.push( parseFloat( line[ offset.normal_y ] ) );
  180. normal.push( parseFloat( line[ offset.normal_z ] ) );
  181. }
  182. if ( offset.intensity !== undefined ) {
  183. intensity.push( parseFloat( line[ offset.intensity ] ) );
  184. }
  185. if ( offset.label !== undefined ) {
  186. label.push( parseInt( line[ offset.label ] ) );
  187. }
  188. }
  189. }
  190. // binary-compressed
  191. // normally data in PCD files are organized as array of structures: XYZRGBXYZRGB
  192. // binary compressed PCD files organize their data as structure of arrays: XXYYZZRGBRGB
  193. // that requires a totally different parsing approach compared to non-compressed data
  194. if ( PCDheader.data === 'binary_compressed' ) {
  195. const sizes = new Uint32Array( data.slice( PCDheader.headerLen, PCDheader.headerLen + 8 ) );
  196. const compressedSize = sizes[ 0 ];
  197. const decompressedSize = sizes[ 1 ];
  198. const decompressed = decompressLZF( new Uint8Array( data, PCDheader.headerLen + 8, compressedSize ), decompressedSize );
  199. const dataview = new DataView( decompressed.buffer );
  200. const offset = PCDheader.offset;
  201. for ( let i = 0; i < PCDheader.points; i ++ ) {
  202. if ( offset.x !== undefined ) {
  203. const xIndex = PCDheader.fields.indexOf( 'x' );
  204. const yIndex = PCDheader.fields.indexOf( 'y' );
  205. const zIndex = PCDheader.fields.indexOf( 'z' );
  206. position.push( dataview.getFloat32( ( PCDheader.points * offset.x ) + PCDheader.size[ xIndex ] * i, this.littleEndian ) );
  207. position.push( dataview.getFloat32( ( PCDheader.points * offset.y ) + PCDheader.size[ yIndex ] * i, this.littleEndian ) );
  208. position.push( dataview.getFloat32( ( PCDheader.points * offset.z ) + PCDheader.size[ zIndex ] * i, this.littleEndian ) );
  209. }
  210. if ( offset.rgb !== undefined ) {
  211. const rgbIndex = PCDheader.fields.indexOf( 'rgb' );
  212. color.push( dataview.getUint8( ( PCDheader.points * offset.rgb ) + PCDheader.size[ rgbIndex ] * i + 2 ) / 255.0 );
  213. color.push( dataview.getUint8( ( PCDheader.points * offset.rgb ) + PCDheader.size[ rgbIndex ] * i + 1 ) / 255.0 );
  214. color.push( dataview.getUint8( ( PCDheader.points * offset.rgb ) + PCDheader.size[ rgbIndex ] * i + 0 ) / 255.0 );
  215. }
  216. if ( offset.normal_x !== undefined ) {
  217. const xIndex = PCDheader.fields.indexOf( 'normal_x' );
  218. const yIndex = PCDheader.fields.indexOf( 'normal_y' );
  219. const zIndex = PCDheader.fields.indexOf( 'normal_z' );
  220. normal.push( dataview.getFloat32( ( PCDheader.points * offset.normal_x ) + PCDheader.size[ xIndex ] * i, this.littleEndian ) );
  221. normal.push( dataview.getFloat32( ( PCDheader.points * offset.normal_y ) + PCDheader.size[ yIndex ] * i, this.littleEndian ) );
  222. normal.push( dataview.getFloat32( ( PCDheader.points * offset.normal_z ) + PCDheader.size[ zIndex ] * i, this.littleEndian ) );
  223. }
  224. if ( offset.intensity !== undefined ) {
  225. const intensityIndex = PCDheader.fields.indexOf( 'intensity' );
  226. intensity.push( dataview.getFloat32( ( PCDheader.points * offset.intensity ) + PCDheader.size[ intensityIndex ] * i, this.littleEndian ) );
  227. }
  228. if ( offset.label !== undefined ) {
  229. const labelIndex = PCDheader.fields.indexOf( 'label' );
  230. label.push( dataview.getInt32( ( PCDheader.points * offset.label ) + PCDheader.size[ labelIndex ] * i, this.littleEndian ) );
  231. }
  232. }
  233. }
  234. // binary
  235. if ( PCDheader.data === 'binary' ) {
  236. const dataview = new DataView( data, PCDheader.headerLen );
  237. const offset = PCDheader.offset;
  238. for ( let i = 0, row = 0; i < PCDheader.points; i ++, row += PCDheader.rowSize ) {
  239. if ( offset.x !== undefined ) {
  240. position.push( dataview.getFloat32( row + offset.x, this.littleEndian ) );
  241. position.push( dataview.getFloat32( row + offset.y, this.littleEndian ) );
  242. position.push( dataview.getFloat32( row + offset.z, this.littleEndian ) );
  243. }
  244. if ( offset.rgb !== undefined ) {
  245. color.push( dataview.getUint8( row + offset.rgb + 2 ) / 255.0 );
  246. color.push( dataview.getUint8( row + offset.rgb + 1 ) / 255.0 );
  247. color.push( dataview.getUint8( row + offset.rgb + 0 ) / 255.0 );
  248. }
  249. if ( offset.normal_x !== undefined ) {
  250. normal.push( dataview.getFloat32( row + offset.normal_x, this.littleEndian ) );
  251. normal.push( dataview.getFloat32( row + offset.normal_y, this.littleEndian ) );
  252. normal.push( dataview.getFloat32( row + offset.normal_z, this.littleEndian ) );
  253. }
  254. if ( offset.intensity !== undefined ) {
  255. intensity.push( dataview.getFloat32( row + offset.intensity, this.littleEndian ) );
  256. }
  257. if ( offset.label !== undefined ) {
  258. label.push( dataview.getInt32( row + offset.label, this.littleEndian ) );
  259. }
  260. }
  261. }
  262. // build geometry
  263. const geometry = new BufferGeometry();
  264. if ( position.length > 0 ) geometry.setAttribute( 'position', new Float32BufferAttribute( position, 3 ) );
  265. if ( normal.length > 0 ) geometry.setAttribute( 'normal', new Float32BufferAttribute( normal, 3 ) );
  266. if ( color.length > 0 ) geometry.setAttribute( 'color', new Float32BufferAttribute( color, 3 ) );
  267. if ( intensity.length > 0 ) geometry.setAttribute( 'intensity', new Float32BufferAttribute( intensity, 1 ) );
  268. if ( label.length > 0 ) geometry.setAttribute( 'label', new Int32BufferAttribute( label, 1 ) );
  269. geometry.computeBoundingSphere();
  270. // build material
  271. const material = new PointsMaterial( { size: 0.005 } );
  272. if ( color.length > 0 ) {
  273. material.vertexColors = true;
  274. }
  275. // build point cloud
  276. return new Points( geometry, material );
  277. }
  278. }
  279. export { PCDLoader };