VTKLoader.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. parse: function ( data ) {
  18. var indices = [];
  19. var positions = [];
  20. var pattern, result;
  21. // float float float
  22. pattern = /([\+|\-]?[\d]+[\.][\d|\-|e]+)[ ]+([\+|\-]?[\d]+[\.][\d|\-|e]+)[ ]+([\+|\-]?[\d]+[\.][\d|\-|e]+)/g;
  23. while ( ( result = pattern.exec( data ) ) !== null ) {
  24. // ["1.0 2.0 3.0", "1.0", "2.0", "3.0"]
  25. positions.push( parseFloat( result[ 1 ] ), parseFloat( result[ 2 ] ), parseFloat( result[ 3 ] ) );
  26. }
  27. // 3 int int int
  28. pattern = /3[ ]+([\d]+)[ ]+([\d]+)[ ]+([\d]+)/g;
  29. while ( ( result = pattern.exec( data ) ) !== null ) {
  30. // ["3 1 2 3", "1", "2", "3"]
  31. indices.push( parseInt( result[ 1 ] ), parseInt( result[ 2 ] ), parseInt( result[ 3 ] ) );
  32. }
  33. // 4 int int int int
  34. pattern = /4[ ]+([\d]+)[ ]+([\d]+)[ ]+([\d]+)[ ]+([\d]+)/g;
  35. while ( ( result = pattern.exec( data ) ) !== null ) {
  36. // ["4 1 2 3 4", "1", "2", "3", "4"]
  37. indices.push( parseInt( result[ 1 ] ), parseInt( result[ 2 ] ), parseInt( result[ 4 ] ) );
  38. indices.push( parseInt( result[ 2 ] ), parseInt( result[ 3 ] ), parseInt( result[ 4 ] ) );
  39. }
  40. var geometry = new THREE.BufferGeometry();
  41. geometry.addAttribute( 'index', new THREE.BufferAttribute( new ( indices.length > 65535 ? Uint32Array : Uint16Array )( indices ), 1 ) );
  42. geometry.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( positions ), 3 ) );
  43. return geometry;
  44. }
  45. };
  46. THREE.EventDispatcher.prototype.apply( THREE.VTKLoader.prototype );