VTKLoader.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  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. {
  37. while (1)
  38. {
  39. var nextWord=line[columnIndex];
  40. columnIndex++;
  41. if (columnIndex==lineLength)
  42. {
  43. lineIndex++;
  44. columnIndex=0;
  45. if (lineIndex>lines.length)
  46. return ("");
  47. line=lines[lineIndex].split(" ");
  48. lineLength=line.length;
  49. }
  50. if (nextWord!=null)
  51. if (nextWord.length>0)
  52. return (nextWord);
  53. }
  54. }
  55. // read point data
  56. var found=false;
  57. while (!found)
  58. {
  59. var readString=readNextString();
  60. switch (readString.toUpperCase())
  61. {
  62. case "POINTS":
  63. found=true;
  64. break;
  65. case "":
  66. alert ("error while reading "+url+" : \n");
  67. return;
  68. default:
  69. }
  70. }
  71. var newIndex;
  72. var new2old;
  73. var numberOfPoints=parseInt(readNextString());
  74. if (numberOfPoints>5000000)
  75. {
  76. alert ("mesh is too big : "+numberOfPoints+" vertices");
  77. return;
  78. }
  79. var coord=[0,0,0];
  80. var index2=0;
  81. var number;
  82. var coordIndex;
  83. for (var j=0;j!=numberOfPoints;j++)
  84. {
  85. for (coordIndex=0;coordIndex<3;coordIndex++)
  86. {
  87. do
  88. {
  89. number=parseFloat(line[columnIndex]);
  90. columnIndex++;
  91. if (columnIndex==lineLength)
  92. {
  93. lineIndex++;
  94. columnIndex=0;
  95. if (lineIndex>lines.length)
  96. {
  97. alert ("error while reading "+url+" : \n");
  98. return;
  99. }
  100. line=lines[lineIndex].split(" ");
  101. lineLength=line.length;
  102. }
  103. } while (isNaN(number))
  104. coord[coordIndex]=number;
  105. }
  106. v(coord[0],coord[1],coord[2]);
  107. }
  108. found=false;
  109. while (!found)
  110. {
  111. var readString=readNextString();
  112. switch (readString)
  113. {
  114. case "POLYGONS":
  115. found=true;
  116. break;
  117. case "":
  118. alert ("error while reading "+url+" : \n");
  119. return;
  120. default:
  121. }
  122. }
  123. var numberOfPolygons=parseInt(readNextString());
  124. var numberOfpolygonElements=parseInt(readNextString());
  125. index2=0;
  126. var connectivity=[];
  127. for (var p=0;p!=numberOfpolygonElements;p++)
  128. {
  129. do
  130. {
  131. number=parseInt(line[columnIndex]);
  132. columnIndex++;
  133. if (columnIndex==lineLength)
  134. {
  135. lineIndex++;
  136. columnIndex=0;
  137. if (lineIndex>lines.length)
  138. {
  139. alert ("error while reading "+url+" : \n");
  140. return;
  141. }
  142. line=lines[lineIndex].split(" ");
  143. lineLength=line.length;
  144. }
  145. } while (isNaN(number))
  146. connectivity[index2]=number;
  147. index2++;
  148. if (index2==connectivity[0]+1)
  149. {
  150. var triangle=[0,0,0];
  151. index2=0;
  152. var numberOfTrianglesInCell=connectivity[0]-2;
  153. var vertex1=triangle[0]=connectivity[1];
  154. for (var i=0;i<numberOfTrianglesInCell;i++)
  155. {
  156. var vertex2=connectivity[i+2];
  157. var vertex3=triangle[2]=connectivity[i+3];
  158. f3(vertex1,vertex2,vertex3);
  159. }
  160. }
  161. }
  162. geometry.computeCentroids();
  163. geometry.computeFaceNormals();
  164. geometry.computeVertexNormals();
  165. geometry.computeBoundingSphere();
  166. return (geometry);
  167. }