PCDLoader.js 6.9 KB

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