PCDLoader.js 6.8 KB

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