PCDLoader.js 6.9 KB

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