VTKLoader.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**
  2. * @author Sebastien Valette [email protected]
  3. */
  4. THREE.VTKLoader = function () {};
  5. THREE.VTKLoader.prototype = new THREE.Loader();
  6. THREE.VTKLoader.prototype.constructor = THREE.VTKLoader;
  7. THREE.VTKLoader.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.VTKLoader.prototype.parse = function ( data ) {
  23. var geometry = new THREE.Geometry();
  24. var lines = data.split("\n");
  25. function v( x, y, z ) {
  26. geometry.vertices.push( new THREE.Vector3( x, y, z ) );
  27. }
  28. function f3( a, b, c ) {
  29. geometry.faces.push( new THREE.Face3( a, b, c ) );
  30. }
  31. var lineIndex = 0;
  32. var line = lines[ 0 ].split( ' ' );
  33. var lineLength = line.length;
  34. var columnIndex = -1;
  35. function readNextString() {
  36. while ( 1 ) {
  37. var nextWord = line[ columnIndex ];
  38. columnIndex ++;
  39. if ( columnIndex == lineLength ) {
  40. lineIndex ++;
  41. columnIndex = 0;
  42. if ( lineIndex > lines.length ) {
  43. return '';
  44. }
  45. line = lines[ lineIndex ].split( ' ' );
  46. lineLength = line.length;
  47. }
  48. if ( nextWord != null ) {
  49. if ( nextWord.length > 0 ) {
  50. return nextWord;
  51. }
  52. }
  53. }
  54. }
  55. // read point data
  56. var found = false;
  57. while ( !found ) {
  58. var readString = readNextString();
  59. switch ( readString.toUpperCase() ) {
  60. case 'POINTS':
  61. found = true;
  62. break;
  63. case '':
  64. alert ( 'error while reading ' + url + ' : \n' );
  65. return;
  66. }
  67. }
  68. var newIndex;
  69. var new2old;
  70. var numberOfPoints = parseInt( readNextString() );
  71. if ( numberOfPoints > 5000000 ) {
  72. alert ( 'mesh is too big : ' + numberOfPoints + ' vertices' );
  73. return;
  74. }
  75. var coord = [ 0, 0, 0 ];
  76. var index2 = 0;
  77. var number;
  78. var coordIndex;
  79. for ( var j = 0; j != numberOfPoints; j ++ ) {
  80. for ( coordIndex = 0; coordIndex < 3; coordIndex ++ ) {
  81. do {
  82. number = parseFloat( line[ columnIndex ] );
  83. columnIndex ++;
  84. if ( columnIndex == lineLength ) {
  85. lineIndex ++;
  86. columnIndex = 0;
  87. if ( lineIndex > lines.length ) {
  88. alert ( 'error while reading ' + url + ' : \n' );
  89. return;
  90. }
  91. line = lines[ lineIndex ].split( ' ' );
  92. lineLength = line.length;
  93. }
  94. } while ( isNaN( number ) )
  95. coord[ coordIndex ] = number;
  96. }
  97. v( coord[ 0 ], coord[ 1 ], coord[ 2 ] );
  98. }
  99. found = false;
  100. while ( !found ) {
  101. var readString = readNextString();
  102. switch ( readString ) {
  103. case 'POLYGONS':
  104. found = true;
  105. break;
  106. case '':
  107. alert ( 'error while reading ' + url + ' : \n' );
  108. return;
  109. }
  110. }
  111. var numberOfPolygons = parseInt( readNextString() );
  112. var numberOfpolygonElements = parseInt( readNextString() );
  113. index2 = 0;
  114. var connectivity = [];
  115. for ( var p = 0; p != numberOfpolygonElements; p ++ ) {
  116. do {
  117. number = parseInt( line[ columnIndex ] );
  118. columnIndex ++;
  119. if ( columnIndex == lineLength ) {
  120. lineIndex ++;
  121. columnIndex = 0;
  122. if ( lineIndex > lines.length ) {
  123. alert ( 'error while reading ' + url + ' : \n' );
  124. return;
  125. }
  126. line = lines[ lineIndex ].split( ' ' );
  127. lineLength = line.length;
  128. }
  129. } while ( isNaN( number ) )
  130. connectivity[ index2 ] = number;
  131. index2 ++;
  132. if ( index2 == connectivity[ 0 ] + 1 ) {
  133. var triangle = [ 0, 0, 0 ];
  134. index2 = 0;
  135. var numberOfTrianglesInCell = connectivity[ 0 ] - 2;
  136. var vertex1 = triangle[ 0 ] = connectivity[ 1 ];
  137. for ( var i = 0; i < numberOfTrianglesInCell; i ++ ) {
  138. var vertex2 = connectivity[ i + 2 ];
  139. var vertex3 = triangle[ 2 ] = connectivity[ i + 3 ];
  140. f3( vertex1, vertex2, vertex3 );
  141. }
  142. }
  143. }
  144. geometry.computeCentroids();
  145. geometry.computeFaceNormals();
  146. geometry.computeVertexNormals();
  147. geometry.computeBoundingSphere();
  148. return geometry;
  149. }