1
0

PCDLoader.js 12 KB

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