VTKLoader.js 6.1 KB

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