VTKLoader2.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.VTKLoader2 = function () {};
  5. THREE.VTKLoader2.prototype = new THREE.Loader();
  6. THREE.VTKLoader2.prototype.constructor = THREE.VTKLoader2;
  7. THREE.VTKLoader2.prototype.load = function ( url, callback ) {
  8. var that = this;
  9. var xhr = new XMLHttpRequest();
  10. xhr.onreadystatechange = function () {
  11. if ( xhr.readyState == 4 ) {
  12. if ( xhr.status == 200 || xhr.status == 0 ) {
  13. callback( that.parse( xhr.responseText ) );
  14. } else {
  15. console.error( 'THREE.VTKLoader: Couldn\'t load ' + url + ' (' + xhr.status + ')' );
  16. }
  17. }
  18. };
  19. xhr.open( "GET", url, true );
  20. xhr.send( null );
  21. };
  22. THREE.VTKLoader2.prototype.parse = function ( data ) {
  23. var geometry = new THREE.Geometry();
  24. function vertex( x, y, z ) {
  25. geometry.vertices.push( new THREE.Vector3( x, y, z ) );
  26. }
  27. function face3( a, b, c ) {
  28. geometry.faces.push( new THREE.Face3( a, b, c ) );
  29. }
  30. function face4( a, b, c, d ) {
  31. geometry.faces.push( new THREE.Face4( a, b, c, d ) );
  32. }
  33. var pattern, result;
  34. // float float float
  35. pattern = /([\d|\.|\+|\-|e]+)[ ]+([\d|\.|\+|\-|e]+)[ ]+([\d|\.|\+|\-|e]+)/g;
  36. while ( ( result = pattern.exec( data ) ) != null ) {
  37. // ["1.0 2.0 3.0", "1.0", "2.0", "3.0"]
  38. vertex( parseFloat( result[ 1 ] ), parseFloat( result[ 2 ] ), parseFloat( result[ 3 ] ) );
  39. }
  40. // 3 int int int
  41. pattern = /3[ ]+([\d]+)[ ]+([\d]+)[ ]+([\d]+)/g;
  42. while ( ( result = pattern.exec( data ) ) != null ) {
  43. // ["3 1 2 3", "1", "2", "3"]
  44. face3( parseInt( result[ 1 ] ), parseInt( result[ 2 ] ), parseInt( result[ 3 ] ) );
  45. }
  46. // 4 int int int int
  47. pattern = /4[ ]+([\d]+)[ ]+([\d]+)[ ]+([\d]+)[ ]+([\d]+)/g;
  48. while ( ( result = pattern.exec( data ) ) != null ) {
  49. // ["4 1 2 3 4", "1", "2", "3", "4"]
  50. face4( parseInt( result[ 1 ] ), parseInt( result[ 2 ] ), parseInt( result[ 3 ] ), parseInt( result[ 4 ] ) );
  51. }
  52. geometry.computeCentroids();
  53. geometry.computeFaceNormals();
  54. geometry.computeVertexNormals();
  55. geometry.computeBoundingSphere();
  56. return geometry;
  57. }