VTKLoader.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 result;
  24. // float float float
  25. var pat3Floats = /([\-]?[\d]+[\.]?[\d|\-|e]*)[ ]+([\-]?[\d]+[\.]?[\d|\-|e]*)[ ]+([\-]?[\d]+[\.]?[\d|\-|e]*)/g;
  26. var patTriangle = /^3[ ]+([\d]+)[ ]+([\d]+)[ ]+([\d]+)/;
  27. var patQuad = /^4[ ]+([\d]+)[ ]+([\d]+)[ ]+([\d]+)[ ]+([\d]+)/;
  28. var patPOINTS = /^POINTS /;
  29. var patPOLYGONS = /^POLYGONS /;
  30. var inPointsSection = false;
  31. var inPolygonsSection = false;
  32. var lines = data.split('\n');
  33. for ( var i = 0; i < lines.length; ++i ) {
  34. line = lines[i];
  35. if ( inPointsSection ) {
  36. // get the vertices
  37. while ( ( result = pat3Floats.exec( line ) ) !== null ) {
  38. positions.push( parseFloat( result[ 1 ] ), parseFloat( result[ 2 ] ), parseFloat( result[ 3 ] ) );
  39. }
  40. }
  41. else if ( inPolygonsSection ) {
  42. result = patTriangle.exec(line);
  43. if ( result !== null ) {
  44. // 3 int int int
  45. // triangle
  46. indices.push( parseInt( result[ 1 ] ), parseInt( result[ 2 ] ), parseInt( result[ 3 ] ) );
  47. }
  48. else {
  49. result = patQuad.exec(line);
  50. if ( result !== null ) {
  51. // 4 int int int int
  52. // break quad into two triangles
  53. indices.push( parseInt( result[ 1 ] ), parseInt( result[ 2 ] ), parseInt( result[ 4 ] ) );
  54. indices.push( parseInt( result[ 2 ] ), parseInt( result[ 3 ] ), parseInt( result[ 4 ] ) );
  55. }
  56. }
  57. }
  58. if ( patPOLYGONS.exec(line) !== null ) {
  59. inPointsSection = false;
  60. inPolygonsSection = true;
  61. }
  62. if ( patPOINTS.exec(line) !== null ) {
  63. inPolygonsSection = false;
  64. inPointsSection = true;
  65. }
  66. }
  67. var geometry = new THREE.BufferGeometry();
  68. geometry.addAttribute( 'index', new THREE.BufferAttribute( new ( indices.length > 65535 ? Uint32Array : Uint16Array )( indices ), 1 ) );
  69. geometry.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( positions ), 3 ) );
  70. return geometry;
  71. }
  72. };
  73. THREE.EventDispatcher.prototype.apply( THREE.VTKLoader.prototype );