PCDLoader.js 7.1 KB

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