VTKLoader2.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. var pattern, result;
  31. // float float float
  32. pattern = /([\d|\.|\+|\-|e]+) ([\d|\.|\+|\-|e]+) ([\d|\.|\+|\-|e]+)/g;
  33. while ( ( result = pattern.exec( data ) ) != null ) {
  34. // ["1.0 2.0 3.0", "1.0", "2.0", "3.0"]
  35. vertex( parseFloat( result[ 1 ] ), parseFloat( result[ 2 ] ), parseFloat( result[ 3 ] ) );
  36. }
  37. // 3 int int int
  38. pattern = /3 ([\d]+) ([\d]+) ([\d]+) /g;
  39. while ( ( result = pattern.exec( data ) ) != null ) {
  40. // ["3 1 2 3", "1", "2", "3"]
  41. face3( parseInt( result[ 1 ] ), parseInt( result[ 2 ] ), parseInt( result[ 3 ] ) );
  42. }
  43. geometry.computeCentroids();
  44. geometry.computeFaceNormals();
  45. geometry.computeVertexNormals();
  46. geometry.computeBoundingSphere();
  47. return geometry;
  48. }