VTKLoader.js 10 KB

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