PCDLoader.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /**
  2. * @author Filipe Caixeta / http://filipecaixeta.com.br
  3. * @author Mugen87 / https://github.com/Mugen87
  4. *
  5. * Description: A THREE loader for PCD ascii and binary files.
  6. *
  7. * Limitations: Compressed binary files are not supported.
  8. *
  9. */
  10. THREE.PCDLoader = function ( manager ) {
  11. THREE.Loader.call( this, manager );
  12. this.littleEndian = true;
  13. };
  14. THREE.PCDLoader.prototype = Object.assign( Object.create( THREE.Loader.prototype ), {
  15. constructor: THREE.PCDLoader,
  16. load: function ( url, onLoad, onProgress, onError ) {
  17. var scope = this;
  18. var loader = new THREE.FileLoader( scope.manager );
  19. loader.setPath( scope.path );
  20. loader.setResponseType( 'arraybuffer' );
  21. loader.load( url, function ( data ) {
  22. try {
  23. onLoad( scope.parse( data, url ) );
  24. } catch ( e ) {
  25. if ( onError ) {
  26. onError( e );
  27. } else {
  28. throw e;
  29. }
  30. }
  31. }, onProgress, onError );
  32. },
  33. parse: function ( data, url ) {
  34. function parseHeader( data ) {
  35. var PCDheader = {};
  36. var result1 = data.search( /[\r\n]DATA\s(\S*)\s/i );
  37. var result2 = /[\r\n]DATA\s(\S*)\s/i.exec( data.substr( result1 - 1 ) );
  38. PCDheader.data = result2[ 1 ];
  39. PCDheader.headerLen = result2[ 0 ].length + result1;
  40. PCDheader.str = data.substr( 0, PCDheader.headerLen );
  41. // remove comments
  42. PCDheader.str = PCDheader.str.replace( /\#.*/gi, '' );
  43. // parse
  44. PCDheader.version = /VERSION (.*)/i.exec( PCDheader.str );
  45. PCDheader.fields = /FIELDS (.*)/i.exec( PCDheader.str );
  46. PCDheader.size = /SIZE (.*)/i.exec( PCDheader.str );
  47. PCDheader.type = /TYPE (.*)/i.exec( PCDheader.str );
  48. PCDheader.count = /COUNT (.*)/i.exec( PCDheader.str );
  49. PCDheader.width = /WIDTH (.*)/i.exec( PCDheader.str );
  50. PCDheader.height = /HEIGHT (.*)/i.exec( PCDheader.str );
  51. PCDheader.viewpoint = /VIEWPOINT (.*)/i.exec( PCDheader.str );
  52. PCDheader.points = /POINTS (.*)/i.exec( PCDheader.str );
  53. // evaluate
  54. if ( PCDheader.version !== null )
  55. PCDheader.version = parseFloat( PCDheader.version[ 1 ] );
  56. if ( PCDheader.fields !== null )
  57. PCDheader.fields = PCDheader.fields[ 1 ].split( ' ' );
  58. if ( PCDheader.type !== null )
  59. PCDheader.type = PCDheader.type[ 1 ].split( ' ' );
  60. if ( PCDheader.width !== null )
  61. PCDheader.width = parseInt( PCDheader.width[ 1 ] );
  62. if ( PCDheader.height !== null )
  63. PCDheader.height = parseInt( PCDheader.height[ 1 ] );
  64. if ( PCDheader.viewpoint !== null )
  65. PCDheader.viewpoint = PCDheader.viewpoint[ 1 ];
  66. if ( PCDheader.points !== null )
  67. PCDheader.points = parseInt( PCDheader.points[ 1 ], 10 );
  68. if ( PCDheader.points === null )
  69. PCDheader.points = PCDheader.width * PCDheader.height;
  70. if ( PCDheader.size !== null ) {
  71. PCDheader.size = PCDheader.size[ 1 ].split( ' ' ).map( function ( x ) {
  72. return parseInt( x, 10 );
  73. } );
  74. }
  75. if ( PCDheader.count !== null ) {
  76. PCDheader.count = PCDheader.count[ 1 ].split( ' ' ).map( function ( x ) {
  77. return parseInt( x, 10 );
  78. } );
  79. } else {
  80. PCDheader.count = [];
  81. for ( var i = 0, l = PCDheader.fields.length; i < l; i ++ ) {
  82. PCDheader.count.push( 1 );
  83. }
  84. }
  85. PCDheader.offset = {};
  86. var sizeSum = 0;
  87. for ( var i = 0, l = PCDheader.fields.length; i < l; i ++ ) {
  88. if ( PCDheader.data === 'ascii' ) {
  89. PCDheader.offset[ PCDheader.fields[ i ] ] = i;
  90. } else {
  91. PCDheader.offset[ PCDheader.fields[ i ] ] = sizeSum;
  92. sizeSum += PCDheader.size[ i ];
  93. }
  94. }
  95. // for binary only
  96. PCDheader.rowSize = sizeSum;
  97. return PCDheader;
  98. }
  99. var textData = THREE.LoaderUtils.decodeText( new Uint8Array( data ) );
  100. // parse header (always ascii format)
  101. var PCDheader = parseHeader( textData );
  102. // parse data
  103. var position = [];
  104. var normal = [];
  105. var color = [];
  106. // ascii
  107. if ( PCDheader.data === 'ascii' ) {
  108. var offset = PCDheader.offset;
  109. var pcdData = textData.substr( PCDheader.headerLen );
  110. var lines = pcdData.split( '\n' );
  111. for ( var i = 0, l = lines.length; i < l; i ++ ) {
  112. if ( lines[ i ] === '' ) continue;
  113. var line = lines[ i ].split( ' ' );
  114. if ( offset.x !== undefined ) {
  115. position.push( parseFloat( line[ offset.x ] ) );
  116. position.push( parseFloat( line[ offset.y ] ) );
  117. position.push( parseFloat( line[ offset.z ] ) );
  118. }
  119. if ( offset.rgb !== undefined ) {
  120. var rgb = parseFloat( line[ offset.rgb ] );
  121. var r = ( rgb >> 16 ) & 0x0000ff;
  122. var g = ( rgb >> 8 ) & 0x0000ff;
  123. var b = ( rgb >> 0 ) & 0x0000ff;
  124. color.push( r / 255, g / 255, b / 255 );
  125. }
  126. if ( offset.normal_x !== undefined ) {
  127. normal.push( parseFloat( line[ offset.normal_x ] ) );
  128. normal.push( parseFloat( line[ offset.normal_y ] ) );
  129. normal.push( parseFloat( line[ offset.normal_z ] ) );
  130. }
  131. }
  132. }
  133. // binary
  134. if ( PCDheader.data === 'binary_compressed' ) {
  135. console.error( 'THREE.PCDLoader: binary_compressed files are not supported' );
  136. return;
  137. }
  138. if ( PCDheader.data === 'binary' ) {
  139. var dataview = new DataView( data, PCDheader.headerLen );
  140. var offset = PCDheader.offset;
  141. for ( var i = 0, row = 0; i < PCDheader.points; i ++, row += PCDheader.rowSize ) {
  142. if ( offset.x !== undefined ) {
  143. position.push( dataview.getFloat32( row + offset.x, this.littleEndian ) );
  144. position.push( dataview.getFloat32( row + offset.y, this.littleEndian ) );
  145. position.push( dataview.getFloat32( row + offset.z, this.littleEndian ) );
  146. }
  147. if ( offset.rgb !== undefined ) {
  148. color.push( dataview.getUint8( row + offset.rgb + 2 ) / 255.0 );
  149. color.push( dataview.getUint8( row + offset.rgb + 1 ) / 255.0 );
  150. color.push( dataview.getUint8( row + offset.rgb + 0 ) / 255.0 );
  151. }
  152. if ( offset.normal_x !== undefined ) {
  153. normal.push( dataview.getFloat32( row + offset.normal_x, this.littleEndian ) );
  154. normal.push( dataview.getFloat32( row + offset.normal_y, this.littleEndian ) );
  155. normal.push( dataview.getFloat32( row + offset.normal_z, this.littleEndian ) );
  156. }
  157. }
  158. }
  159. // build geometry
  160. var geometry = new THREE.BufferGeometry();
  161. if ( position.length > 0 ) geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( position, 3 ) );
  162. if ( normal.length > 0 ) geometry.setAttribute( 'normal', new THREE.Float32BufferAttribute( normal, 3 ) );
  163. if ( color.length > 0 ) geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( color, 3 ) );
  164. geometry.computeBoundingSphere();
  165. // build material
  166. var material = new THREE.PointsMaterial( { size: 0.005 } );
  167. if ( color.length > 0 ) {
  168. material.vertexColors = THREE.VertexColors;
  169. } else {
  170. material.color.setHex( Math.random() * 0xffffff );
  171. }
  172. // build mesh
  173. var mesh = new THREE.Points( geometry, material );
  174. var name = url.split( '' ).reverse().join( '' );
  175. name = /([^\/]*)/.exec( name );
  176. name = name[ 1 ].split( '' ).reverse().join( '' );
  177. mesh.name = name;
  178. return mesh;
  179. }
  180. } );