VTKLoader.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. // LUT variables required for coloring
  30. var colorMap = 'rainbow';
  31. var numberOfColors = 512;
  32. var lut = new THREE.Lut( colorMap, numberOfColors );
  33. lut.setMax( 2000 );
  34. lut.setMin( 0 );
  35. // Float values defined for the LUT
  36. var color_scalars = [];
  37. // normal vector, one per vertex
  38. var normals = [];
  39. var result;
  40. // pattern for reading vertices, 3 floats or integers
  41. var pat3Floats = /(\-?\d+\.?[\d\-\+e]*)\s+(\-?\d+\.?[\d\-\+e]*)\s+(\-?\d+\.?[\d\-\+e]*)/g;
  42. // pattern for connectivity, an integer followed by any number of ints
  43. // the first integer is the number of polygon nodes
  44. var patConnectivity = /^(\d+)\s+([\s\d]*)/;
  45. // indicates start of vertex data section
  46. var patPOINTS = /^POINTS /;
  47. // indicates start of polygon connectivity section
  48. var patPOLYGONS = /^POLYGONS /;
  49. // POINT_DATA number_of_values
  50. var patPOINT_DATA = /^POINT_DATA[ ]+(\d+)/;
  51. // CELL_DATA number_of_polys
  52. var patCELL_DATA = /^CELL_DATA[ ]+(\d+)/;
  53. // Start of color section
  54. var patCOLOR_SCALARS = /^COLOR_SCALARS[ ]+(\w+)[ ]+3/;
  55. // Start of LUT section
  56. var patLOOKUP_TABLE = /^LOOKUP_TABLE[ ]+(\w)/;
  57. // NORMALS Normals float
  58. var patNORMALS = /^NORMALS[ ]+(\w+)[ ]+(\w+)/;
  59. var inPointsSection = false;
  60. var inPolygonsSection = false;
  61. var inPointDataSection = false;
  62. var inCellDataSection = false;
  63. var inColorSection = false;
  64. var inLookupTableSection = false;
  65. var inNormalsSection = false;
  66. var lines = data.split('\n');
  67. for ( var i in lines ) {
  68. var line = lines[ i ];
  69. if ( inPointsSection ) {
  70. // get the vertices
  71. while ( ( result = pat3Floats.exec( line ) ) !== null ) {
  72. var x = parseFloat( result[ 1 ] );
  73. var y = parseFloat( result[ 2 ] );
  74. var z = parseFloat( result[ 3 ] );
  75. positions.push( x, y, z );
  76. }
  77. } else if ( inPolygonsSection ) {
  78. if ( ( result = patConnectivity.exec( line ) ) !== null ) {
  79. // numVertices i0 i1 i2 ...
  80. var numVertices = parseInt( result[ 1 ] );
  81. var inds = result[ 2 ].split(/\s+/);
  82. if ( numVertices >= 3 ) {
  83. var i0 = parseInt( inds[ 0 ] );
  84. var i1, i2;
  85. var k = 1;
  86. // split the polygon in numVertices - 2 triangles
  87. for ( var j = 0; j < numVertices - 2; ++j ) {
  88. i1 = parseInt( inds[ k ] );
  89. i2 = parseInt( inds[ k + 1 ] );
  90. indices.push( i0, i1, i2 );
  91. k++;
  92. }
  93. }
  94. }
  95. } else if ( inPointDataSection ) {
  96. if ( inColorSection ) {
  97. // get the colors
  98. while ( ( result = pat3Floats.exec( line ) ) !== null ) {
  99. var r = parseFloat( result[ 1 ] );
  100. var g = parseFloat( result[ 2 ] );
  101. var b = parseFloat( result[ 3 ] );
  102. colors.push( r, g, b );
  103. }
  104. } else if ( inLookupTableSection ) {
  105. // get the color scalars
  106. var items = line.split( /(\s+)/ );
  107. for ( var item_index = 0; item_index < items.length; item_index++ ) {
  108. var scalar = parseFloat( items[ item_index ] ).toFixed( 12 );
  109. if ( ! isNaN( scalar ) ) {
  110. color_scalars.push( scalar );
  111. }
  112. }
  113. } else if ( inNormalsSection ) {
  114. // get the normal vectors
  115. while ( ( result = pat3Floats.exec( line ) ) !== null ) {
  116. var nx = parseFloat( result[ 1 ] );
  117. var ny = parseFloat( result[ 2 ] );
  118. var nz = parseFloat( result[ 3 ] );
  119. normals.push( nx, ny, nz );
  120. }
  121. }
  122. } else if ( inCellDataSection ) {
  123. if ( inColorSection ) {
  124. // get the colors
  125. while ( ( result = pat3Floats.exec( line ) ) !== null ) {
  126. var r = parseFloat( result[ 1 ] );
  127. var g = parseFloat( result[ 2 ] );
  128. var b = parseFloat( result[ 3 ] );
  129. colors.push( r, g, b );
  130. }
  131. } else if ( inLookupTableSection ) {
  132. // get the color scalars
  133. var items = line.split( /(\s+)/ );
  134. for ( var item_index = 0; item_index < items.length; item_index++ ) {
  135. var scalar = parseFloat( items[ item_index ] ).toFixed( 12 );
  136. if ( ! isNaN( scalar ) ) {
  137. color_scalars.push( scalar );
  138. }
  139. }
  140. } else if ( inNormalsSection ) {
  141. // get the normal vectors
  142. while ( ( result = pat3Floats.exec( line ) ) !== null ) {
  143. var nx = parseFloat( result[ 1 ] );
  144. var ny = parseFloat( result[ 2 ] );
  145. var nz = parseFloat( result[ 3 ] );
  146. normals.push( nx, ny, nz );
  147. }
  148. }
  149. }
  150. if ( patPOLYGONS.exec( line ) !== null ) {
  151. inPolygonsSection = true;
  152. inPointsSection = false;
  153. } else if ( patPOINTS.exec( line ) !== null ) {
  154. inPolygonsSection = false;
  155. inPointsSection = true;
  156. } else if ( patPOINT_DATA.exec( line ) !== null ) {
  157. inPointDataSection = true;
  158. inPointsSection = false;
  159. inPolygonsSection = false;
  160. } else if ( patCELL_DATA.exec( line ) !== null ) {
  161. inCellDataSection = true;
  162. inPointsSection = false;
  163. inPolygonsSection = false;
  164. } else if ( patCOLOR_SCALARS.exec( line ) !== null ) {
  165. inColorSection = true;
  166. inLookupTableSection = false;
  167. inNormalsSection = false;
  168. inPointsSection = false;
  169. inPolygonsSection = false;
  170. } else if ( patLOOKUP_TABLE.exec( line ) !== null ) {
  171. inLookupTableSection = true;
  172. inColorSection = false;
  173. inNormalsSection = false;
  174. inPointsSection = false;
  175. inPolygonsSection = false;
  176. } else if ( patNORMALS.exec( line ) !== null ) {
  177. inNormalsSection = true;
  178. inLookupTableSection = false;
  179. inColorSection = false;
  180. inPointsSection = false;
  181. inPolygonsSection = false;
  182. }
  183. }
  184. var geometry;
  185. var stagger = 'point';
  186. if ( colors.length == indices.length ) {
  187. stagger = 'cell';
  188. }
  189. if ( stagger == 'point' ) {
  190. // nodal. Use BufferGeometry
  191. geometry = new THREE.BufferGeometry();
  192. geometry.addAttribute( 'index', new THREE.BufferAttribute( new ( indices.length > 65535 ? Uint32Array : Uint16Array )( indices ), 1 ) );
  193. geometry.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( positions ), 3 ) );
  194. if ( colors.length == positions.length ) {
  195. geometry.addAttribute( 'color', new THREE.BufferAttribute( new Float32Array( colors ), 3 ) );
  196. } else if ( color_scalars.length > 0 ) {
  197. // Use LUT for coloring.
  198. var lutColors = [];
  199. for ( var i = 0; i < color_scalars.length; i++ ) {
  200. var colorValue = color_scalars[ i ];
  201. color = lut.getColor( colorValue );
  202. if ( color == undefined ) {
  203. console.log( "ERROR: " + colorValue );
  204. } else {
  205. lutColors[ 3 * i ] = color.r;
  206. lutColors[ 3 * i + 1 ] = color.g;
  207. lutColors[ 3 * i + 2 ] = color.b;
  208. }
  209. }
  210. geometry.addAttribute( 'color', new THREE.BufferAttribute( new Float32Array( lutColors ), 3 ) );
  211. }
  212. if ( normals.length == positions.length ) {
  213. geometry.addAttribute( 'normal', new THREE.BufferAttribute( new Float32Array( normals ), 3 ) );
  214. }
  215. } else {
  216. // cell centered colors. The only way to attach a solid color to each triangle
  217. // is to use Geometry
  218. geometry = new THREE.Geometry();
  219. var numTriangles = indices.length / 3;
  220. var numPoints = positions.length / 3;
  221. var va, vb, vc;
  222. var face;
  223. var colorA, colorB, colorC;
  224. var ia, ib, ic;
  225. var x, y, z;
  226. var r, g, b;
  227. for ( var j = 0; j < numPoints; ++j ) {
  228. x = positions[ 3*j + 0 ];
  229. y = positions[ 3*j + 1 ];
  230. z = positions[ 3*j + 2 ];
  231. geometry.vertices.push( new THREE.Vector3( x, y, z ) );
  232. }
  233. for ( var i = 0; i < numTriangles; ++i ) {
  234. ia = indices[ 3*i + 0 ];
  235. ib = indices[ 3*i + 1 ];
  236. ic = indices[ 3*i + 2 ];
  237. geometry.faces.push( new THREE.Face3( ia, ib, ic ) );
  238. }
  239. if ( colors.length == numTriangles * 3 ) {
  240. for ( var i = 0; i < numTriangles; ++i ) {
  241. face = geometry.faces[i];
  242. r = colors[ 3*i + 0 ];
  243. g = colors[ 3*i + 1 ];
  244. b = colors[ 3*i + 2 ];
  245. face.color = new THREE.Color().setRGB( r, g, b );
  246. }
  247. } else if ( color_scalars.length == numTriangles ) {
  248. // Use LUT for coloring.
  249. for ( var i = 0; i < numTriangles; ++i ) {
  250. face = geometry.faces[i];
  251. colorValue = color_scalars[ i ];
  252. color = lut.getColor( colorValue );
  253. if ( color == undefined ) {
  254. console.log( "ERROR: " + colorValue );
  255. } else {
  256. color = new THREE.Color();
  257. color.setRGB( color.r, color.g, color.b );
  258. face.color = new THREE.Color().setRGB( r, g, b );
  259. }
  260. }
  261. }
  262. }
  263. return geometry;
  264. }
  265. };
  266. THREE.EventDispatcher.prototype.apply( THREE.VTKLoader.prototype );