PCDLoader.js 7.0 KB

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