PCDLoader.js 7.0 KB

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