PCDLoader.js 11 KB

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