VTKLoader.js 1.8 KB

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