PCDLoader.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 binarryToStr( 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, il = data.byteLength; i < il; 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. var i, l;
  41. PCDheader.data = result2[ 1 ];
  42. PCDheader.headerLen = result2[ 0 ].length + result1;
  43. PCDheader.str = data.substr( 0, PCDheader.headerLen );
  44. // remove comments
  45. PCDheader.str = PCDheader.str.replace( /\#.*/gi, '' );
  46. // parse
  47. PCDheader.version = /VERSION (.*)/i.exec( PCDheader.str );
  48. PCDheader.fields = /FIELDS (.*)/i.exec( PCDheader.str );
  49. PCDheader.size = /SIZE (.*)/i.exec( PCDheader.str );
  50. PCDheader.type = /TYPE (.*)/i.exec( PCDheader.str );
  51. PCDheader.count = /COUNT (.*)/i.exec( PCDheader.str );
  52. PCDheader.width = /WIDTH (.*)/i.exec( PCDheader.str );
  53. PCDheader.height = /HEIGHT (.*)/i.exec( PCDheader.str );
  54. PCDheader.viewpoint = /VIEWPOINT (.*)/i.exec( PCDheader.str );
  55. PCDheader.points = /POINTS (.*)/i.exec( PCDheader.str );
  56. // evaluate
  57. if ( PCDheader.version !== null )
  58. PCDheader.version = parseFloat( PCDheader.version[ 1 ] );
  59. if ( PCDheader.fields !== null )
  60. PCDheader.fields = PCDheader.fields[ 1 ].split( ' ' );
  61. if ( PCDheader.type !== null )
  62. PCDheader.type = PCDheader.type[ 1 ].split( ' ' );
  63. if ( PCDheader.width !== null )
  64. PCDheader.width = parseInt( PCDheader.width[ 1 ] );
  65. if ( PCDheader.height !== null )
  66. PCDheader.height = parseInt( PCDheader.height[ 1 ] );
  67. if ( PCDheader.viewpoint !== null )
  68. PCDheader.viewpoint = PCDheader.viewpoint[ 1 ];
  69. if ( PCDheader.points !== null )
  70. PCDheader.points = parseInt( PCDheader.points[ 1 ], 10 );
  71. if ( PCDheader.points === null )
  72. PCDheader.points = PCDheader.width * PCDheader.height;
  73. if ( PCDheader.size !== null ) {
  74. PCDheader.size = PCDheader.size[ 1 ].split( ' ' ).map( function ( x ) {
  75. return parseInt( x, 10 );
  76. } );
  77. }
  78. if ( PCDheader.count !== null ) {
  79. PCDheader.count = PCDheader.count[ 1 ].split( ' ' ).map( function ( x ) {
  80. return parseInt( x, 10 );
  81. } );
  82. } else {
  83. PCDheader.count = [];
  84. for ( i = 0, l = PCDheader.fields.length; i < l; i ++ ) {
  85. PCDheader.count.push( 1 );
  86. }
  87. }
  88. PCDheader.offset = {};
  89. var sizeSum = 0;
  90. for ( i = 0, l = PCDheader.fields.length; i < l; i ++ ) {
  91. if ( PCDheader.data === 'ascii' ) {
  92. PCDheader.offset[ PCDheader.fields[ i ] ] = i;
  93. } else {
  94. PCDheader.offset[ PCDheader.fields[ i ] ] = sizeSum;
  95. sizeSum += PCDheader.size[ i ];
  96. }
  97. }
  98. // for binary only
  99. PCDheader.rowSize = sizeSum;
  100. return PCDheader;
  101. }
  102. var dataView, offset;
  103. var i, l;
  104. var textData = binarryToStr( data );
  105. // parse header (always ascii format)
  106. var PCDheader = parseHeader( textData );
  107. // parse data
  108. var position = [];
  109. var normal = [];
  110. var color = [];
  111. // ascii
  112. if ( PCDheader.data === 'ascii' ) {
  113. offset = PCDheader.offset;
  114. var pcdData = textData.substr( PCDheader.headerLen );
  115. var lines = pcdData.split( '\n' );
  116. for ( i = 0, l = lines.length; i < l; i ++ ) {
  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 c = new Float32Array( [ parseFloat( line[ offset.rgb ] ) ] );
  125. dataview = new DataView( c.buffer, 0 );
  126. color.push( dataview.getUint8( 0 ) / 255.0 );
  127. color.push( dataview.getUint8( 1 ) / 255.0 );
  128. color.push( dataview.getUint8( 2 ) / 255.0 );
  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. dataview = new DataView( data, PCDheader.headerLen );
  144. offset = PCDheader.offset;
  145. for ( 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 + 0 ) / 255.0 );
  153. color.push( dataview.getUint8( row + offset.rgb + 1 ) / 255.0 );
  154. color.push( dataview.getUint8( row + offset.rgb + 2 ) / 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 = true;
  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. };