PCDLoader.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. import {
  2. BufferGeometry,
  3. FileLoader,
  4. Float32BufferAttribute,
  5. Loader,
  6. LoaderUtils,
  7. Points,
  8. PointsMaterial
  9. } from '../../../build/three.module.js';
  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, url ) );
  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, url ) {
  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.substr( result1 - 1 ) );
  77. PCDheader.data = result2[ 1 ];
  78. PCDheader.headerLen = result2[ 0 ].length + result1;
  79. PCDheader.str = data.substr( 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. if ( PCDheader.fields !== null )
  96. PCDheader.fields = 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. // ascii
  146. if ( PCDheader.data === 'ascii' ) {
  147. const offset = PCDheader.offset;
  148. const pcdData = textData.substr( PCDheader.headerLen );
  149. const lines = pcdData.split( '\n' );
  150. for ( let i = 0, l = lines.length; i < l; i ++ ) {
  151. if ( lines[ i ] === '' ) continue;
  152. const line = lines[ i ].split( ' ' );
  153. if ( offset.x !== undefined ) {
  154. position.push( parseFloat( line[ offset.x ] ) );
  155. position.push( parseFloat( line[ offset.y ] ) );
  156. position.push( parseFloat( line[ offset.z ] ) );
  157. }
  158. if ( offset.rgb !== undefined ) {
  159. const rgb = parseFloat( line[ offset.rgb ] );
  160. const r = ( rgb >> 16 ) & 0x0000ff;
  161. const g = ( rgb >> 8 ) & 0x0000ff;
  162. const b = ( rgb >> 0 ) & 0x0000ff;
  163. color.push( r / 255, g / 255, b / 255 );
  164. }
  165. if ( offset.normal_x !== undefined ) {
  166. normal.push( parseFloat( line[ offset.normal_x ] ) );
  167. normal.push( parseFloat( line[ offset.normal_y ] ) );
  168. normal.push( parseFloat( line[ offset.normal_z ] ) );
  169. }
  170. }
  171. }
  172. // binary-compressed
  173. // normally data in PCD files are organized as array of structures: XYZRGBXYZRGB
  174. // binary compressed PCD files organize their data as structure of arrays: XXYYZZRGBRGB
  175. // that requires a totally different parsing approach compared to non-compressed data
  176. if ( PCDheader.data === 'binary_compressed' ) {
  177. const sizes = new Uint32Array( data.slice( PCDheader.headerLen, PCDheader.headerLen + 8 ) );
  178. const compressedSize = sizes[ 0 ];
  179. const decompressedSize = sizes[ 1 ];
  180. const decompressed = decompressLZF( new Uint8Array( data, PCDheader.headerLen + 8, compressedSize ), decompressedSize );
  181. const dataview = new DataView( decompressed.buffer );
  182. const offset = PCDheader.offset;
  183. for ( let i = 0; i < PCDheader.points; i ++ ) {
  184. if ( offset.x !== undefined ) {
  185. position.push( dataview.getFloat32( ( PCDheader.points * offset.x ) + PCDheader.size[ 0 ] * i, this.littleEndian ) );
  186. position.push( dataview.getFloat32( ( PCDheader.points * offset.y ) + PCDheader.size[ 1 ] * i, this.littleEndian ) );
  187. position.push( dataview.getFloat32( ( PCDheader.points * offset.z ) + PCDheader.size[ 2 ] * i, this.littleEndian ) );
  188. }
  189. if ( offset.rgb !== undefined ) {
  190. color.push( dataview.getUint8( ( PCDheader.points * offset.rgb ) + PCDheader.size[ 3 ] * i + 0 ) / 255.0 );
  191. color.push( dataview.getUint8( ( PCDheader.points * offset.rgb ) + PCDheader.size[ 3 ] * i + 1 ) / 255.0 );
  192. color.push( dataview.getUint8( ( PCDheader.points * offset.rgb ) + PCDheader.size[ 3 ] * i + 2 ) / 255.0 );
  193. }
  194. if ( offset.normal_x !== undefined ) {
  195. normal.push( dataview.getFloat32( ( PCDheader.points * offset.normal_x ) + PCDheader.size[ 4 ] * i, this.littleEndian ) );
  196. normal.push( dataview.getFloat32( ( PCDheader.points * offset.normal_y ) + PCDheader.size[ 5 ] * i, this.littleEndian ) );
  197. normal.push( dataview.getFloat32( ( PCDheader.points * offset.normal_z ) + PCDheader.size[ 6 ] * i, this.littleEndian ) );
  198. }
  199. }
  200. }
  201. // binary
  202. if ( PCDheader.data === 'binary' ) {
  203. const dataview = new DataView( data, PCDheader.headerLen );
  204. const offset = PCDheader.offset;
  205. for ( let i = 0, row = 0; i < PCDheader.points; i ++, row += PCDheader.rowSize ) {
  206. if ( offset.x !== undefined ) {
  207. position.push( dataview.getFloat32( row + offset.x, this.littleEndian ) );
  208. position.push( dataview.getFloat32( row + offset.y, this.littleEndian ) );
  209. position.push( dataview.getFloat32( row + offset.z, this.littleEndian ) );
  210. }
  211. if ( offset.rgb !== undefined ) {
  212. color.push( dataview.getUint8( row + offset.rgb + 2 ) / 255.0 );
  213. color.push( dataview.getUint8( row + offset.rgb + 1 ) / 255.0 );
  214. color.push( dataview.getUint8( row + offset.rgb + 0 ) / 255.0 );
  215. }
  216. if ( offset.normal_x !== undefined ) {
  217. normal.push( dataview.getFloat32( row + offset.normal_x, this.littleEndian ) );
  218. normal.push( dataview.getFloat32( row + offset.normal_y, this.littleEndian ) );
  219. normal.push( dataview.getFloat32( row + offset.normal_z, this.littleEndian ) );
  220. }
  221. }
  222. }
  223. // build geometry
  224. const geometry = new BufferGeometry();
  225. if ( position.length > 0 ) geometry.setAttribute( 'position', new Float32BufferAttribute( position, 3 ) );
  226. if ( normal.length > 0 ) geometry.setAttribute( 'normal', new Float32BufferAttribute( normal, 3 ) );
  227. if ( color.length > 0 ) geometry.setAttribute( 'color', new Float32BufferAttribute( color, 3 ) );
  228. geometry.computeBoundingSphere();
  229. // build material
  230. const material = new PointsMaterial( { size: 0.005 } );
  231. if ( color.length > 0 ) {
  232. material.vertexColors = true;
  233. } else {
  234. material.color.setHex( Math.random() * 0xffffff );
  235. }
  236. // build point cloud
  237. const mesh = new Points( geometry, material );
  238. let name = url.split( '' ).reverse().join( '' );
  239. name = /([^\/]*)/.exec( name );
  240. name = name[ 1 ].split( '' ).reverse().join( '' );
  241. mesh.name = name;
  242. return mesh;
  243. }
  244. }
  245. export { PCDLoader };