PCDLoader.js 6.7 KB

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