VTKLoader.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.VTKLoader = function ( manager ) {
  5. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  6. };
  7. THREE.VTKLoader.prototype = {
  8. constructor: THREE.VTKLoader,
  9. load: function ( url, onLoad, onProgress, onError ) {
  10. var scope = this;
  11. var loader = new THREE.XHRLoader( scope.manager );
  12. loader.setCrossOrigin( this.crossOrigin );
  13. loader.load( url, function ( text ) {
  14. onLoad( scope.parse( text ) );
  15. }, onProgress, onError );
  16. },
  17. setCrossOrigin: function ( value ) {
  18. this.crossOrigin = value;
  19. },
  20. parse: function ( data ) {
  21. var indices = [];
  22. var positions = [];
  23. var pattern, result;
  24. // float float float
  25. pattern = /([\+|\-]?[\d]+[\.][\d|\-|e]+)[ ]+([\+|\-]?[\d]+[\.][\d|\-|e]+)[ ]+([\+|\-]?[\d]+[\.][\d|\-|e]+)/g;
  26. while ( ( result = pattern.exec( data ) ) !== null ) {
  27. // ["1.0 2.0 3.0", "1.0", "2.0", "3.0"]
  28. positions.push( parseFloat( result[ 1 ] ), parseFloat( result[ 2 ] ), parseFloat( result[ 3 ] ) );
  29. }
  30. // 3 int int int
  31. pattern = /3[ ]+([\d]+)[ ]+([\d]+)[ ]+([\d]+)/g;
  32. while ( ( result = pattern.exec( data ) ) !== null ) {
  33. // ["3 1 2 3", "1", "2", "3"]
  34. indices.push( parseInt( result[ 1 ] ), parseInt( result[ 2 ] ), parseInt( result[ 3 ] ) );
  35. }
  36. // 4 int int int int
  37. pattern = /4[ ]+([\d]+)[ ]+([\d]+)[ ]+([\d]+)[ ]+([\d]+)/g;
  38. while ( ( result = pattern.exec( data ) ) !== null ) {
  39. // ["4 1 2 3 4", "1", "2", "3", "4"]
  40. indices.push( parseInt( result[ 1 ] ), parseInt( result[ 2 ] ), parseInt( result[ 4 ] ) );
  41. indices.push( parseInt( result[ 2 ] ), parseInt( result[ 3 ] ), parseInt( result[ 4 ] ) );
  42. }
  43. var geometry = new THREE.BufferGeometry();
  44. geometry.addAttribute( 'index', new THREE.BufferAttribute( new ( indices.length > 65535 ? Uint32Array : Uint16Array )( indices ), 1 ) );
  45. geometry.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( positions ), 3 ) );
  46. return geometry;
  47. }
  48. };
  49. THREE.EventDispatcher.prototype.apply( THREE.VTKLoader.prototype );