PCDLoader.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. import {
  2. BufferGeometry,
  3. Color,
  4. FileLoader,
  5. Float32BufferAttribute,
  6. Int32BufferAttribute,
  7. Loader,
  8. Points,
  9. PointsMaterial,
  10. SRGBColorSpace
  11. } from 'three';
  12. /**
  13. * A loader for the Point Cloud Data (PCD) format.
  14. *
  15. * PCDLoader supports ASCII and (compressed) binary files as well as the following PCD fields:
  16. * - x y z
  17. * - rgb
  18. * - normal_x normal_y normal_z
  19. * - intensity
  20. * - label
  21. *
  22. * ```js
  23. * const loader = new PCDLoader();
  24. *
  25. * const points = await loader.loadAsync( './models/pcd/binary/Zaghetto.pcd' );
  26. * points.geometry.center(); // optional
  27. * points.geometry.rotateX( Math.PI ); // optional
  28. * scene.add( points );
  29. * ```
  30. *
  31. * @augments Loader
  32. * @three_import import { PCDLoader } from 'three/addons/loaders/PCDLoader.js';
  33. */
  34. class PCDLoader extends Loader {
  35. /**
  36. * Constructs a new PCD loader.
  37. *
  38. * @param {LoadingManager} [manager] - The loading manager.
  39. */
  40. constructor( manager ) {
  41. super( manager );
  42. /**
  43. * Whether to use little Endian or not.
  44. *
  45. * @type {boolean}
  46. * @default true
  47. */
  48. this.littleEndian = true;
  49. }
  50. /**
  51. * Starts loading from the given URL and passes the loaded PCD asset
  52. * to the `onLoad()` callback.
  53. *
  54. * @param {string} url - The path/URL of the file to be loaded. This can also be a data URI.
  55. * @param {function(Points)} onLoad - Executed when the loading process has been finished.
  56. * @param {onProgressCallback} onProgress - Executed while the loading is in progress.
  57. * @param {onErrorCallback} onError - Executed when errors occur.
  58. */
  59. load( url, onLoad, onProgress, onError ) {
  60. const scope = this;
  61. const loader = new FileLoader( scope.manager );
  62. loader.setPath( scope.path );
  63. loader.setResponseType( 'arraybuffer' );
  64. loader.setRequestHeader( scope.requestHeader );
  65. loader.setWithCredentials( scope.withCredentials );
  66. loader.load( url, function ( data ) {
  67. try {
  68. onLoad( scope.parse( data ) );
  69. } catch ( e ) {
  70. if ( onError ) {
  71. onError( e );
  72. } else {
  73. console.error( e );
  74. }
  75. scope.manager.itemError( url );
  76. }
  77. }, onProgress, onError );
  78. }
  79. /**
  80. * Get dataview value by field type and size.
  81. *
  82. * @param {DataView} dataview - The DataView to read from.
  83. * @param {number} offset - The offset to start reading from.
  84. * @param {'F' | 'U' | 'I'} type - Field type.
  85. * @param {number} size - Field size.
  86. * @returns {number} Field value.
  87. */
  88. _getDataView( dataview, offset, type, size ) {
  89. switch ( type ) {
  90. case 'F': {
  91. if ( size === 8 ) {
  92. return dataview.getFloat64( offset, this.littleEndian );
  93. }
  94. return dataview.getFloat32( offset, this.littleEndian );
  95. }
  96. case 'I': {
  97. if ( size === 1 ) {
  98. return dataview.getInt8( offset );
  99. }
  100. if ( size === 2 ) {
  101. return dataview.getInt16( offset, this.littleEndian );
  102. }
  103. return dataview.getInt32( offset, this.littleEndian );
  104. }
  105. case 'U': {
  106. if ( size === 1 ) {
  107. return dataview.getUint8( offset );
  108. }
  109. if ( size === 2 ) {
  110. return dataview.getUint16( offset, this.littleEndian );
  111. }
  112. return dataview.getUint32( offset, this.littleEndian );
  113. }
  114. }
  115. }
  116. /**
  117. * Parses the given PCD data and returns a point cloud.
  118. *
  119. * @param {ArrayBuffer} data - The raw PCD data as an array buffer.
  120. * @return {Points} The parsed point cloud.
  121. */
  122. parse( data ) {
  123. // from https://gitlab.com/taketwo/three-pcd-loader/blob/master/decompress-lzf.js
  124. function decompressLZF( inData, outLength ) {
  125. const inLength = inData.length;
  126. const outData = new Uint8Array( outLength );
  127. let inPtr = 0;
  128. let outPtr = 0;
  129. let ctrl;
  130. let len;
  131. let ref;
  132. do {
  133. ctrl = inData[ inPtr ++ ];
  134. if ( ctrl < ( 1 << 5 ) ) {
  135. ctrl ++;
  136. if ( outPtr + ctrl > outLength ) throw new Error( 'Output buffer is not large enough' );
  137. if ( inPtr + ctrl > inLength ) throw new Error( 'Invalid compressed data' );
  138. do {
  139. outData[ outPtr ++ ] = inData[ inPtr ++ ];
  140. } while ( -- ctrl );
  141. } else {
  142. len = ctrl >> 5;
  143. ref = outPtr - ( ( ctrl & 0x1f ) << 8 ) - 1;
  144. if ( inPtr >= inLength ) throw new Error( 'Invalid compressed data' );
  145. if ( len === 7 ) {
  146. len += inData[ inPtr ++ ];
  147. if ( inPtr >= inLength ) throw new Error( 'Invalid compressed data' );
  148. }
  149. ref -= inData[ inPtr ++ ];
  150. if ( outPtr + len + 2 > outLength ) throw new Error( 'Output buffer is not large enough' );
  151. if ( ref < 0 ) throw new Error( 'Invalid compressed data' );
  152. if ( ref >= outPtr ) throw new Error( 'Invalid compressed data' );
  153. do {
  154. outData[ outPtr ++ ] = outData[ ref ++ ];
  155. } while ( -- len + 2 );
  156. }
  157. } while ( inPtr < inLength );
  158. return outData;
  159. }
  160. function parseHeader( binaryData ) {
  161. const PCDheader = {};
  162. const buffer = new Uint8Array( binaryData );
  163. let data = '', line = '', i = 0, end = false;
  164. const max = buffer.length;
  165. while ( i < max && end === false ) {
  166. const char = String.fromCharCode( buffer[ i ++ ] );
  167. if ( char === '\n' || char === '\r' ) {
  168. if ( line.trim().toLowerCase().startsWith( 'data' ) ) {
  169. end = true;
  170. }
  171. line = '';
  172. } else {
  173. line += char;
  174. }
  175. data += char;
  176. }
  177. const result1 = data.search( /[\r\n]DATA\s(\S*)\s/i );
  178. const result2 = /[\r\n]DATA\s(\S*)\s/i.exec( data.slice( result1 - 1 ) );
  179. PCDheader.data = result2[ 1 ];
  180. PCDheader.headerLen = result2[ 0 ].length + result1;
  181. PCDheader.str = data.slice( 0, PCDheader.headerLen );
  182. // remove comments
  183. PCDheader.str = PCDheader.str.replace( /#.*/gi, '' );
  184. // parse
  185. PCDheader.version = /^VERSION (.*)/im.exec( PCDheader.str );
  186. PCDheader.fields = /^FIELDS (.*)/im.exec( PCDheader.str );
  187. PCDheader.size = /^SIZE (.*)/im.exec( PCDheader.str );
  188. PCDheader.type = /^TYPE (.*)/im.exec( PCDheader.str );
  189. PCDheader.count = /^COUNT (.*)/im.exec( PCDheader.str );
  190. PCDheader.width = /^WIDTH (.*)/im.exec( PCDheader.str );
  191. PCDheader.height = /^HEIGHT (.*)/im.exec( PCDheader.str );
  192. PCDheader.viewpoint = /^VIEWPOINT (.*)/im.exec( PCDheader.str );
  193. PCDheader.points = /^POINTS (.*)/im.exec( PCDheader.str );
  194. // evaluate
  195. if ( PCDheader.version !== null )
  196. PCDheader.version = parseFloat( PCDheader.version[ 1 ] );
  197. PCDheader.fields = ( PCDheader.fields !== null ) ? PCDheader.fields[ 1 ].split( ' ' ) : [];
  198. if ( PCDheader.type !== null )
  199. PCDheader.type = PCDheader.type[ 1 ].split( ' ' );
  200. if ( PCDheader.width !== null )
  201. PCDheader.width = parseInt( PCDheader.width[ 1 ] );
  202. if ( PCDheader.height !== null )
  203. PCDheader.height = parseInt( PCDheader.height[ 1 ] );
  204. if ( PCDheader.viewpoint !== null )
  205. PCDheader.viewpoint = PCDheader.viewpoint[ 1 ];
  206. if ( PCDheader.points !== null )
  207. PCDheader.points = parseInt( PCDheader.points[ 1 ], 10 );
  208. if ( PCDheader.points === null )
  209. PCDheader.points = PCDheader.width * PCDheader.height;
  210. if ( PCDheader.size !== null ) {
  211. PCDheader.size = PCDheader.size[ 1 ].split( ' ' ).map( function ( x ) {
  212. return parseInt( x, 10 );
  213. } );
  214. }
  215. if ( PCDheader.count !== null ) {
  216. PCDheader.count = PCDheader.count[ 1 ].split( ' ' ).map( function ( x ) {
  217. return parseInt( x, 10 );
  218. } );
  219. } else {
  220. PCDheader.count = [];
  221. for ( let i = 0, l = PCDheader.fields.length; i < l; i ++ ) {
  222. PCDheader.count.push( 1 );
  223. }
  224. }
  225. PCDheader.offset = {};
  226. let sizeSum = 0;
  227. for ( let i = 0, l = PCDheader.fields.length; i < l; i ++ ) {
  228. if ( PCDheader.data === 'ascii' ) {
  229. PCDheader.offset[ PCDheader.fields[ i ] ] = i;
  230. } else {
  231. PCDheader.offset[ PCDheader.fields[ i ] ] = sizeSum;
  232. sizeSum += PCDheader.size[ i ] * PCDheader.count[ i ];
  233. }
  234. }
  235. // for binary only
  236. PCDheader.rowSize = sizeSum;
  237. return PCDheader;
  238. }
  239. // parse header
  240. const PCDheader = parseHeader( data );
  241. // parse data
  242. const position = [];
  243. const normal = [];
  244. const color = [];
  245. const intensity = [];
  246. const label = [];
  247. const c = new Color();
  248. // ascii
  249. if ( PCDheader.data === 'ascii' ) {
  250. const offset = PCDheader.offset;
  251. const textData = new TextDecoder().decode( data );
  252. const pcdData = textData.slice( PCDheader.headerLen );
  253. const lines = pcdData.split( '\n' );
  254. for ( let i = 0, l = lines.length; i < l; i ++ ) {
  255. if ( lines[ i ] === '' ) continue;
  256. const line = lines[ i ].split( ' ' );
  257. if ( offset.x !== undefined ) {
  258. position.push( parseFloat( line[ offset.x ] ) );
  259. position.push( parseFloat( line[ offset.y ] ) );
  260. position.push( parseFloat( line[ offset.z ] ) );
  261. }
  262. if ( offset.rgb !== undefined ) {
  263. const rgb_field_index = PCDheader.fields.findIndex( ( field ) => field === 'rgb' );
  264. const rgb_type = PCDheader.type[ rgb_field_index ];
  265. const float = parseFloat( line[ offset.rgb ] );
  266. let rgb = float;
  267. if ( rgb_type === 'F' ) {
  268. // treat float values as int
  269. // https://github.com/daavoo/pyntcloud/pull/204/commits/7b4205e64d5ed09abe708b2e91b615690c24d518
  270. const farr = new Float32Array( 1 );
  271. farr[ 0 ] = float;
  272. rgb = new Int32Array( farr.buffer )[ 0 ];
  273. }
  274. const r = ( ( rgb >> 16 ) & 0x0000ff ) / 255;
  275. const g = ( ( rgb >> 8 ) & 0x0000ff ) / 255;
  276. const b = ( ( rgb >> 0 ) & 0x0000ff ) / 255;
  277. c.setRGB( r, g, b, SRGBColorSpace );
  278. color.push( c.r, c.g, c.b );
  279. }
  280. if ( offset.normal_x !== undefined ) {
  281. normal.push( parseFloat( line[ offset.normal_x ] ) );
  282. normal.push( parseFloat( line[ offset.normal_y ] ) );
  283. normal.push( parseFloat( line[ offset.normal_z ] ) );
  284. }
  285. if ( offset.intensity !== undefined ) {
  286. intensity.push( parseFloat( line[ offset.intensity ] ) );
  287. }
  288. if ( offset.label !== undefined ) {
  289. label.push( parseInt( line[ offset.label ] ) );
  290. }
  291. }
  292. }
  293. // binary-compressed
  294. // normally data in PCD files are organized as array of structures: XYZRGBXYZRGB
  295. // binary compressed PCD files organize their data as structure of arrays: XXYYZZRGBRGB
  296. // that requires a totally different parsing approach compared to non-compressed data
  297. if ( PCDheader.data === 'binary_compressed' ) {
  298. const sizes = new Uint32Array( data.slice( PCDheader.headerLen, PCDheader.headerLen + 8 ) );
  299. const compressedSize = sizes[ 0 ];
  300. const decompressedSize = sizes[ 1 ];
  301. const decompressed = decompressLZF( new Uint8Array( data, PCDheader.headerLen + 8, compressedSize ), decompressedSize );
  302. const dataview = new DataView( decompressed.buffer );
  303. const offset = PCDheader.offset;
  304. for ( let i = 0; i < PCDheader.points; i ++ ) {
  305. if ( offset.x !== undefined ) {
  306. const xIndex = PCDheader.fields.indexOf( 'x' );
  307. const yIndex = PCDheader.fields.indexOf( 'y' );
  308. const zIndex = PCDheader.fields.indexOf( 'z' );
  309. position.push( this._getDataView( dataview, ( PCDheader.points * offset.x ) + PCDheader.size[ xIndex ] * i, PCDheader.type[ xIndex ], PCDheader.size[ xIndex ] ) );
  310. position.push( this._getDataView( dataview, ( PCDheader.points * offset.y ) + PCDheader.size[ yIndex ] * i, PCDheader.type[ yIndex ], PCDheader.size[ yIndex ] ) );
  311. position.push( this._getDataView( dataview, ( PCDheader.points * offset.z ) + PCDheader.size[ zIndex ] * i, PCDheader.type[ zIndex ], PCDheader.size[ zIndex ] ) );
  312. }
  313. if ( offset.rgb !== undefined ) {
  314. const rgbIndex = PCDheader.fields.indexOf( 'rgb' );
  315. const r = dataview.getUint8( ( PCDheader.points * offset.rgb ) + PCDheader.size[ rgbIndex ] * i + 2 ) / 255.0;
  316. const g = dataview.getUint8( ( PCDheader.points * offset.rgb ) + PCDheader.size[ rgbIndex ] * i + 1 ) / 255.0;
  317. const b = dataview.getUint8( ( PCDheader.points * offset.rgb ) + PCDheader.size[ rgbIndex ] * i + 0 ) / 255.0;
  318. c.setRGB( r, g, b, SRGBColorSpace );
  319. color.push( c.r, c.g, c.b );
  320. }
  321. if ( offset.normal_x !== undefined ) {
  322. const xIndex = PCDheader.fields.indexOf( 'normal_x' );
  323. const yIndex = PCDheader.fields.indexOf( 'normal_y' );
  324. const zIndex = PCDheader.fields.indexOf( 'normal_z' );
  325. normal.push( this._getDataView( dataview, ( PCDheader.points * offset.normal_x ) + PCDheader.size[ xIndex ] * i, PCDheader.type[ xIndex ], PCDheader.size[ xIndex ] ) );
  326. normal.push( this._getDataView( dataview, ( PCDheader.points * offset.normal_y ) + PCDheader.size[ yIndex ] * i, PCDheader.type[ yIndex ], PCDheader.size[ yIndex ] ) );
  327. normal.push( this._getDataView( dataview, ( PCDheader.points * offset.normal_z ) + PCDheader.size[ zIndex ] * i, PCDheader.type[ zIndex ], PCDheader.size[ zIndex ] ) );
  328. }
  329. if ( offset.intensity !== undefined ) {
  330. const intensityIndex = PCDheader.fields.indexOf( 'intensity' );
  331. intensity.push( this._getDataView( dataview, ( PCDheader.points * offset.intensity ) + PCDheader.size[ intensityIndex ] * i, PCDheader.type[ intensityIndex ], PCDheader.size[ intensityIndex ] ) );
  332. }
  333. if ( offset.label !== undefined ) {
  334. const labelIndex = PCDheader.fields.indexOf( 'label' );
  335. label.push( dataview.getInt32( ( PCDheader.points * offset.label ) + PCDheader.size[ labelIndex ] * i, this.littleEndian ) );
  336. }
  337. }
  338. }
  339. // binary
  340. if ( PCDheader.data === 'binary' ) {
  341. const dataview = new DataView( data, PCDheader.headerLen );
  342. const offset = PCDheader.offset;
  343. for ( let i = 0, row = 0; i < PCDheader.points; i ++, row += PCDheader.rowSize ) {
  344. if ( offset.x !== undefined ) {
  345. const xIndex = PCDheader.fields.indexOf( 'x' );
  346. const yIndex = PCDheader.fields.indexOf( 'y' );
  347. const zIndex = PCDheader.fields.indexOf( 'z' );
  348. position.push( this._getDataView( dataview, row + offset.x, PCDheader.type[ xIndex ], PCDheader.size[ xIndex ] ) );
  349. position.push( this._getDataView( dataview, row + offset.y, PCDheader.type[ yIndex ], PCDheader.size[ yIndex ] ) );
  350. position.push( this._getDataView( dataview, row + offset.z, PCDheader.type[ zIndex ], PCDheader.size[ zIndex ] ) );
  351. }
  352. if ( offset.rgb !== undefined ) {
  353. const r = dataview.getUint8( row + offset.rgb + 2 ) / 255.0;
  354. const g = dataview.getUint8( row + offset.rgb + 1 ) / 255.0;
  355. const b = dataview.getUint8( row + offset.rgb + 0 ) / 255.0;
  356. c.setRGB( r, g, b, SRGBColorSpace );
  357. color.push( c.r, c.g, c.b );
  358. }
  359. if ( offset.normal_x !== undefined ) {
  360. const xIndex = PCDheader.fields.indexOf( 'normal_x' );
  361. const yIndex = PCDheader.fields.indexOf( 'normal_y' );
  362. const zIndex = PCDheader.fields.indexOf( 'normal_z' );
  363. normal.push( this._getDataView( dataview, row + offset.normal_x, PCDheader.type[ xIndex ], PCDheader.size[ xIndex ] ) );
  364. normal.push( this._getDataView( dataview, row + offset.normal_y, PCDheader.type[ yIndex ], PCDheader.size[ yIndex ] ) );
  365. normal.push( this._getDataView( dataview, row + offset.normal_z, PCDheader.type[ zIndex ], PCDheader.size[ zIndex ] ) );
  366. }
  367. if ( offset.intensity !== undefined ) {
  368. const intensityIndex = PCDheader.fields.indexOf( 'intensity' );
  369. intensity.push( this._getDataView( dataview, row + offset.intensity, PCDheader.type[ intensityIndex ], PCDheader.size[ intensityIndex ] ) );
  370. }
  371. if ( offset.label !== undefined ) {
  372. label.push( dataview.getInt32( row + offset.label, this.littleEndian ) );
  373. }
  374. }
  375. }
  376. // build geometry
  377. const geometry = new BufferGeometry();
  378. if ( position.length > 0 ) geometry.setAttribute( 'position', new Float32BufferAttribute( position, 3 ) );
  379. if ( normal.length > 0 ) geometry.setAttribute( 'normal', new Float32BufferAttribute( normal, 3 ) );
  380. if ( color.length > 0 ) geometry.setAttribute( 'color', new Float32BufferAttribute( color, 3 ) );
  381. if ( intensity.length > 0 ) geometry.setAttribute( 'intensity', new Float32BufferAttribute( intensity, 1 ) );
  382. if ( label.length > 0 ) geometry.setAttribute( 'label', new Int32BufferAttribute( label, 1 ) );
  383. geometry.computeBoundingSphere();
  384. // build material
  385. const material = new PointsMaterial( { size: 0.005 } );
  386. if ( color.length > 0 ) {
  387. material.vertexColors = true;
  388. }
  389. // build point cloud
  390. return new Points( geometry, material );
  391. }
  392. }
  393. export { PCDLoader };