VTKLoader.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /**
  2. * Origin: https://github.com/mrdoob/three.js/blob/af21991fc7c4e1d35d6a93031707273d937af0f9/examples/js/loaders/VTKLoader.js
  3. * @author mrdoob / http://mrdoob.com/ and Alex Pletzer
  4. */
  5. THREE.VTKLoader = function ( manager ) {
  6. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  7. };
  8. THREE.VTKLoader.prototype = {
  9. constructor: THREE.VTKLoader,
  10. load: function ( url, onLoad, onProgress, onError ) {
  11. // Will we bump into trouble reading the whole file into memory?
  12. var scope = this;
  13. var loader = new THREE.XHRLoader( scope.manager );
  14. loader.setCrossOrigin( this.crossOrigin );
  15. loader.load( url, function ( text ) {
  16. onLoad( scope.parse( text ) );
  17. },
  18. onProgress, onError );
  19. },
  20. setCrossOrigin: function ( value ) {
  21. this.crossOrigin = value;
  22. },
  23. parse: function ( data ) {
  24. // connectivity of the triangles
  25. var indices = [];
  26. // triangles vertices
  27. var positions = [];
  28. // red, green, blue colors in the range 0 to 1
  29. var colors = [];
  30. // normal vector, one per vertex
  31. var normals = [];
  32. var result;
  33. // pattern for reading vertices, 3 floats or integers
  34. var pat3Floats = /(\-?\d+\.?[\d\-\+e]*)\s+(\-?\d+\.?[\d\-\+e]*)\s+(\-?\d+\.?[\d\-\+e]*)/g;
  35. // pattern for connectivity, an integer followed by any number of ints
  36. // the first integer is the number of polygon nodes
  37. var patConnectivity = /^(\d+)\s+([\s\d]*)/;
  38. // indicates start of vertex data section
  39. var patPOINTS = /^POINTS /;
  40. // indicates start of polygon connectivity section
  41. var patPOLYGONS = /^POLYGONS /;
  42. // POINT_DATA number_of_values
  43. var patPOINT_DATA = /^POINT_DATA[ ]+(\d+)/;
  44. // CELL_DATA number_of_polys
  45. var patCELL_DATA = /^CELL_DATA[ ]+(\d+)/;
  46. // Start of color section
  47. var patCOLOR_SCALARS = /^COLOR_SCALARS[ ]+(\w+)[ ]+3/;
  48. // NORMALS Normals float
  49. var patNORMALS = /^NORMALS[ ]+(\w+)[ ]+(\w+)/;
  50. var inPointsSection = false;
  51. var inPolygonsSection = false;
  52. var inPointDataSection = false;
  53. var inCellDataSection = false;
  54. var inColorSection = false;
  55. var inNormalsSection = false;
  56. var lines = data.split('\n');
  57. for ( var i in lines ) {
  58. var line = lines[ i ];
  59. if ( inPointsSection ) {
  60. // get the vertices
  61. while ( ( result = pat3Floats.exec( line ) ) !== null ) {
  62. var x = parseFloat( result[ 1 ] );
  63. var y = parseFloat( result[ 2 ] );
  64. var z = parseFloat( result[ 3 ] );
  65. positions.push( x, y, z );
  66. }
  67. } else if ( inPolygonsSection ) {
  68. if ( ( result = patConnectivity.exec( line ) ) !== null ) {
  69. // numVertices i0 i1 i2 ...
  70. var numVertices = parseInt( result[ 1 ] );
  71. var inds = result[ 2 ].split(/\s+/);
  72. if ( numVertices >= 3 ) {
  73. var i0 = parseInt( inds[ 0 ] );
  74. var i1, i2;
  75. var k = 1;
  76. // split the polygon in numVertices - 2 triangles
  77. for ( var j = 0; j < numVertices - 2; ++j ) {
  78. i1 = parseInt( inds[ k ] );
  79. i2 = parseInt( inds[ k + 1 ] );
  80. indices.push( i0, i1, i2 );
  81. k++;
  82. }
  83. }
  84. }
  85. } else if ( inPointDataSection || inCellDataSection ) {
  86. if ( inColorSection ) {
  87. // Get the colors
  88. while ( ( result = pat3Floats.exec( line ) ) !== null ) {
  89. var r = parseFloat( result[ 1 ] );
  90. var g = parseFloat( result[ 2 ] );
  91. var b = parseFloat( result[ 3 ] );
  92. colors.push( r, g, b );
  93. }
  94. } else if ( inNormalsSection ) {
  95. // Get the normal vectors
  96. while ( ( result = pat3Floats.exec( line ) ) !== null ) {
  97. var nx = parseFloat( result[ 1 ] );
  98. var ny = parseFloat( result[ 2 ] );
  99. var nz = parseFloat( result[ 3 ] );
  100. normals.push( nx, ny, nz );
  101. }
  102. }
  103. }
  104. if ( patPOLYGONS.exec( line ) !== null ) {
  105. inPolygonsSection = true;
  106. inPointsSection = false;
  107. } else if ( patPOINTS.exec( line ) !== null ) {
  108. inPolygonsSection = false;
  109. inPointsSection = true;
  110. } else if ( patPOINT_DATA.exec( line ) !== null ) {
  111. inPointDataSection = true;
  112. inPointsSection = false;
  113. inPolygonsSection = false;
  114. } else if ( patCELL_DATA.exec( line ) !== null ) {
  115. inCellDataSection = true;
  116. inPointsSection = false;
  117. inPolygonsSection = false;
  118. } else if ( patCOLOR_SCALARS.exec( line ) !== null ) {
  119. inColorSection = true;
  120. inNormalsSection = false;
  121. inPointsSection = false;
  122. inPolygonsSection = false;
  123. } else if ( patNORMALS.exec( line ) !== null ) {
  124. inNormalsSection = true;
  125. inColorSection = false;
  126. inPointsSection = false;
  127. inPolygonsSection = false;
  128. }
  129. }
  130. var geometry;
  131. var stagger = 'point';
  132. if ( colors.length == indices.length ) {
  133. stagger = 'cell';
  134. }
  135. if ( stagger == 'point' ) {
  136. // Nodal. Use BufferGeometry
  137. geometry = new THREE.BufferGeometry();
  138. geometry.addAttribute( 'index', new THREE.BufferAttribute( new ( indices.length > 65535 ? Uint32Array : Uint16Array )( indices ), 1 ) );
  139. geometry.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( positions ), 3 ) );
  140. if ( colors.length == positions.length ) {
  141. geometry.addAttribute( 'color', new THREE.BufferAttribute( new Float32Array( colors ), 3 ) );
  142. }
  143. if ( normals.length == positions.length ) {
  144. geometry.addAttribute( 'normal', new THREE.BufferAttribute( new Float32Array( normals ), 3 ) );
  145. }
  146. } else {
  147. // Cell centered colors. The only way to attach a solid color to each triangle
  148. // is to use Geometry, which is less efficient than BufferGeometry
  149. geometry = new THREE.Geometry();
  150. var numTriangles = indices.length / 3;
  151. var numPoints = positions.length / 3;
  152. var va, vb, vc;
  153. var face;
  154. var ia, ib, ic;
  155. var x, y, z;
  156. var r, g, b;
  157. for ( var j = 0; j < numPoints; ++j ) {
  158. x = positions[ 3*j + 0 ];
  159. y = positions[ 3*j + 1 ];
  160. z = positions[ 3*j + 2 ];
  161. geometry.vertices.push( new THREE.Vector3( x, y, z ) );
  162. }
  163. for ( var i = 0; i < numTriangles; ++i ) {
  164. ia = indices[ 3*i + 0 ];
  165. ib = indices[ 3*i + 1 ];
  166. ic = indices[ 3*i + 2 ];
  167. geometry.faces.push( new THREE.Face3( ia, ib, ic ) );
  168. }
  169. if ( colors.length == numTriangles * 3 ) {
  170. for ( var i = 0; i < numTriangles; ++i ) {
  171. face = geometry.faces[i];
  172. r = colors[ 3*i + 0 ];
  173. g = colors[ 3*i + 1 ];
  174. b = colors[ 3*i + 2 ];
  175. face.color = new THREE.Color().setRGB( r, g, b );
  176. }
  177. }
  178. }
  179. return geometry;
  180. }
  181. };
  182. THREE.EventDispatcher.prototype.apply( THREE.VTKLoader.prototype );