PCDLoader.js 7.0 KB

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