PCDLoader.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. import {
  2. BufferGeometry,
  3. Color,
  4. FileLoader,
  5. Float32BufferAttribute,
  6. Int32BufferAttribute,
  7. Loader,
  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 = new TextDecoder().decode( 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. const c = new Color();
  148. // ascii
  149. if ( PCDheader.data === 'ascii' ) {
  150. const offset = PCDheader.offset;
  151. const pcdData = textData.slice( PCDheader.headerLen );
  152. const lines = pcdData.split( '\n' );
  153. for ( let i = 0, l = lines.length; i < l; i ++ ) {
  154. if ( lines[ i ] === '' ) continue;
  155. const line = lines[ i ].split( ' ' );
  156. if ( offset.x !== undefined ) {
  157. position.push( parseFloat( line[ offset.x ] ) );
  158. position.push( parseFloat( line[ offset.y ] ) );
  159. position.push( parseFloat( line[ offset.z ] ) );
  160. }
  161. if ( offset.rgb !== undefined ) {
  162. const rgb_field_index = PCDheader.fields.findIndex( ( field ) => field === 'rgb' );
  163. const rgb_type = PCDheader.type[ rgb_field_index ];
  164. const float = parseFloat( line[ offset.rgb ] );
  165. let rgb = float;
  166. if ( rgb_type === 'F' ) {
  167. // treat float values as int
  168. // https://github.com/daavoo/pyntcloud/pull/204/commits/7b4205e64d5ed09abe708b2e91b615690c24d518
  169. const farr = new Float32Array( 1 );
  170. farr[ 0 ] = float;
  171. rgb = new Int32Array( farr.buffer )[ 0 ];
  172. }
  173. const r = ( ( rgb >> 16 ) & 0x0000ff ) / 255;
  174. const g = ( ( rgb >> 8 ) & 0x0000ff ) / 255;
  175. const b = ( ( rgb >> 0 ) & 0x0000ff ) / 255;
  176. c.set( r, g, b ).convertSRGBToLinear();
  177. color.push( c.r, c.g, c.b );
  178. }
  179. if ( offset.normal_x !== undefined ) {
  180. normal.push( parseFloat( line[ offset.normal_x ] ) );
  181. normal.push( parseFloat( line[ offset.normal_y ] ) );
  182. normal.push( parseFloat( line[ offset.normal_z ] ) );
  183. }
  184. if ( offset.intensity !== undefined ) {
  185. intensity.push( parseFloat( line[ offset.intensity ] ) );
  186. }
  187. if ( offset.label !== undefined ) {
  188. label.push( parseInt( line[ offset.label ] ) );
  189. }
  190. }
  191. }
  192. // binary-compressed
  193. // normally data in PCD files are organized as array of structures: XYZRGBXYZRGB
  194. // binary compressed PCD files organize their data as structure of arrays: XXYYZZRGBRGB
  195. // that requires a totally different parsing approach compared to non-compressed data
  196. if ( PCDheader.data === 'binary_compressed' ) {
  197. const sizes = new Uint32Array( data.slice( PCDheader.headerLen, PCDheader.headerLen + 8 ) );
  198. const compressedSize = sizes[ 0 ];
  199. const decompressedSize = sizes[ 1 ];
  200. const decompressed = decompressLZF( new Uint8Array( data, PCDheader.headerLen + 8, compressedSize ), decompressedSize );
  201. const dataview = new DataView( decompressed.buffer );
  202. const offset = PCDheader.offset;
  203. for ( let i = 0; i < PCDheader.points; i ++ ) {
  204. if ( offset.x !== undefined ) {
  205. const xIndex = PCDheader.fields.indexOf( 'x' );
  206. const yIndex = PCDheader.fields.indexOf( 'y' );
  207. const zIndex = PCDheader.fields.indexOf( 'z' );
  208. position.push( dataview.getFloat32( ( PCDheader.points * offset.x ) + PCDheader.size[ xIndex ] * i, this.littleEndian ) );
  209. position.push( dataview.getFloat32( ( PCDheader.points * offset.y ) + PCDheader.size[ yIndex ] * i, this.littleEndian ) );
  210. position.push( dataview.getFloat32( ( PCDheader.points * offset.z ) + PCDheader.size[ zIndex ] * i, this.littleEndian ) );
  211. }
  212. if ( offset.rgb !== undefined ) {
  213. const rgbIndex = PCDheader.fields.indexOf( 'rgb' );
  214. const r = dataview.getUint8( ( PCDheader.points * offset.rgb ) + PCDheader.size[ rgbIndex ] * i + 2 ) / 255.0;
  215. const g = dataview.getUint8( ( PCDheader.points * offset.rgb ) + PCDheader.size[ rgbIndex ] * i + 1 ) / 255.0;
  216. const b = dataview.getUint8( ( PCDheader.points * offset.rgb ) + PCDheader.size[ rgbIndex ] * i + 0 ) / 255.0;
  217. c.set( r, g, b ).convertSRGBToLinear();
  218. color.push( c.r, c.g, c.b );
  219. }
  220. if ( offset.normal_x !== undefined ) {
  221. const xIndex = PCDheader.fields.indexOf( 'normal_x' );
  222. const yIndex = PCDheader.fields.indexOf( 'normal_y' );
  223. const zIndex = PCDheader.fields.indexOf( 'normal_z' );
  224. normal.push( dataview.getFloat32( ( PCDheader.points * offset.normal_x ) + PCDheader.size[ xIndex ] * i, this.littleEndian ) );
  225. normal.push( dataview.getFloat32( ( PCDheader.points * offset.normal_y ) + PCDheader.size[ yIndex ] * i, this.littleEndian ) );
  226. normal.push( dataview.getFloat32( ( PCDheader.points * offset.normal_z ) + PCDheader.size[ zIndex ] * i, this.littleEndian ) );
  227. }
  228. if ( offset.intensity !== undefined ) {
  229. const intensityIndex = PCDheader.fields.indexOf( 'intensity' );
  230. intensity.push( dataview.getFloat32( ( PCDheader.points * offset.intensity ) + PCDheader.size[ intensityIndex ] * i, this.littleEndian ) );
  231. }
  232. if ( offset.label !== undefined ) {
  233. const labelIndex = PCDheader.fields.indexOf( 'label' );
  234. label.push( dataview.getInt32( ( PCDheader.points * offset.label ) + PCDheader.size[ labelIndex ] * i, this.littleEndian ) );
  235. }
  236. }
  237. }
  238. // binary
  239. if ( PCDheader.data === 'binary' ) {
  240. const dataview = new DataView( data, PCDheader.headerLen );
  241. const offset = PCDheader.offset;
  242. for ( let i = 0, row = 0; i < PCDheader.points; i ++, row += PCDheader.rowSize ) {
  243. if ( offset.x !== undefined ) {
  244. position.push( dataview.getFloat32( row + offset.x, this.littleEndian ) );
  245. position.push( dataview.getFloat32( row + offset.y, this.littleEndian ) );
  246. position.push( dataview.getFloat32( row + offset.z, this.littleEndian ) );
  247. }
  248. if ( offset.rgb !== undefined ) {
  249. const r = dataview.getUint8( row + offset.rgb + 2 ) / 255.0;
  250. const g = dataview.getUint8( row + offset.rgb + 1 ) / 255.0;
  251. const b = dataview.getUint8( row + offset.rgb + 0 ) / 255.0;
  252. c.set( r, g, b ).convertSRGBToLinear();
  253. color.push( c.r, c.g, c.b );
  254. }
  255. if ( offset.normal_x !== undefined ) {
  256. normal.push( dataview.getFloat32( row + offset.normal_x, this.littleEndian ) );
  257. normal.push( dataview.getFloat32( row + offset.normal_y, this.littleEndian ) );
  258. normal.push( dataview.getFloat32( row + offset.normal_z, this.littleEndian ) );
  259. }
  260. if ( offset.intensity !== undefined ) {
  261. intensity.push( dataview.getFloat32( row + offset.intensity, this.littleEndian ) );
  262. }
  263. if ( offset.label !== undefined ) {
  264. label.push( dataview.getInt32( row + offset.label, this.littleEndian ) );
  265. }
  266. }
  267. }
  268. // build geometry
  269. const geometry = new BufferGeometry();
  270. if ( position.length > 0 ) geometry.setAttribute( 'position', new Float32BufferAttribute( position, 3 ) );
  271. if ( normal.length > 0 ) geometry.setAttribute( 'normal', new Float32BufferAttribute( normal, 3 ) );
  272. if ( color.length > 0 ) geometry.setAttribute( 'color', new Float32BufferAttribute( color, 3 ) );
  273. if ( intensity.length > 0 ) geometry.setAttribute( 'intensity', new Float32BufferAttribute( intensity, 1 ) );
  274. if ( label.length > 0 ) geometry.setAttribute( 'label', new Int32BufferAttribute( label, 1 ) );
  275. geometry.computeBoundingSphere();
  276. // build material
  277. const material = new PointsMaterial( { size: 0.005 } );
  278. if ( color.length > 0 ) {
  279. material.vertexColors = true;
  280. }
  281. // build point cloud
  282. return new Points( geometry, material );
  283. }
  284. }
  285. export { PCDLoader };