PCDLoader.js 7.0 KB

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