VTKLoader.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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.setResponseType( 'arraybuffer' );
  15. loader.load( url, function( text ) {
  16. onLoad( scope.parse( text ) );
  17. },
  18. onProgress, onError );
  19. },
  20. parse: function( data ) {
  21. //get the 5 first lines of the files to check if there is the key word binary
  22. var meta = String.fromCharCode.apply( null, new Uint8Array( data, 0, 250 ) ).split( '\n' );
  23. console.log( meta );
  24. if ( meta[ 2 ] === 'ASCII' ) {
  25. return this.parseASCII( String.fromCharCode.apply( null, new Uint8Array( data ) ) );
  26. } else {
  27. return this.parseBinary( data );
  28. }
  29. },
  30. parseASCII: function( data ) {
  31. // connectivity of the triangles
  32. var indices = [];
  33. // triangles vertices
  34. var positions = [];
  35. // red, green, blue colors in the range 0 to 1
  36. var colors = [];
  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. // indicates start of triangle strips section
  50. var patTRIANGLE_STRIPS = /^TRIANGLE_STRIPS /;
  51. // POINT_DATA number_of_values
  52. var patPOINT_DATA = /^POINT_DATA[ ]+(\d+)/;
  53. // CELL_DATA number_of_polys
  54. var patCELL_DATA = /^CELL_DATA[ ]+(\d+)/;
  55. // Start of color section
  56. var patCOLOR_SCALARS = /^COLOR_SCALARS[ ]+(\w+)[ ]+3/;
  57. // NORMALS Normals float
  58. var patNORMALS = /^NORMALS[ ]+(\w+)[ ]+(\w+)/;
  59. var inPointsSection = false;
  60. var inPolygonsSection = false;
  61. var inTriangleStripSection = false;
  62. var inPointDataSection = false;
  63. var inCellDataSection = false;
  64. var inColorSection = 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 ( inTriangleStripSection ) {
  96. if ( ( result = patConnectivity.exec( line ) ) !== null ) {
  97. // numVertices i0 i1 i2 ...
  98. var numVertices = parseInt( result[ 1 ] );
  99. var inds = result[ 2 ].split( /\s+/ );
  100. if ( numVertices >= 3 ) {
  101. var i0, i1, i2;
  102. // split the polygon in numVertices - 2 triangles
  103. for ( var j = 0; j < numVertices - 2; j ++ ) {
  104. if ( j % 2 === 1 ) {
  105. i0 = parseInt( inds[ j ] );
  106. i1 = parseInt( inds[ j + 2 ] );
  107. i2 = parseInt( inds[ j + 1 ] );
  108. indices.push( i0, i1, i2 );
  109. } else {
  110. i0 = parseInt( inds[ j ] );
  111. i1 = parseInt( inds[ j + 1 ] );
  112. i2 = parseInt( inds[ j + 2 ] );
  113. indices.push( i0, i1, i2 );
  114. }
  115. }
  116. }
  117. }
  118. } else if ( inPointDataSection || inCellDataSection ) {
  119. if ( inColorSection ) {
  120. // Get the colors
  121. while ( ( result = pat3Floats.exec( line ) ) !== null ) {
  122. var r = parseFloat( result[ 1 ] );
  123. var g = parseFloat( result[ 2 ] );
  124. var b = parseFloat( result[ 3 ] );
  125. colors.push( r, g, b );
  126. }
  127. } else if ( inNormalsSection ) {
  128. // Get the normal vectors
  129. while ( ( result = pat3Floats.exec( line ) ) !== null ) {
  130. var nx = parseFloat( result[ 1 ] );
  131. var ny = parseFloat( result[ 2 ] );
  132. var nz = parseFloat( result[ 3 ] );
  133. normals.push( nx, ny, nz );
  134. }
  135. }
  136. }
  137. if ( patPOLYGONS.exec( line ) !== null ) {
  138. inPolygonsSection = true;
  139. inPointsSection = false;
  140. inTriangleStripSection = false;
  141. } else if ( patPOINTS.exec( line ) !== null ) {
  142. inPolygonsSection = false;
  143. inPointsSection = true;
  144. inTriangleStripSection = false;
  145. } else if ( patTRIANGLE_STRIPS.exec( line ) !== null ) {
  146. inPolygonsSection = false;
  147. inPointsSection = false;
  148. inTriangleStripSection = true;
  149. } else if ( patPOINT_DATA.exec( line ) !== null ) {
  150. inPointDataSection = true;
  151. inPointsSection = false;
  152. inPolygonsSection = false;
  153. inTriangleStripSection = false;
  154. } else if ( patCELL_DATA.exec( line ) !== null ) {
  155. inCellDataSection = true;
  156. inPointsSection = false;
  157. inPolygonsSection = false;
  158. inTriangleStripSection = false;
  159. } else if ( patCOLOR_SCALARS.exec( line ) !== null ) {
  160. inColorSection = true;
  161. inNormalsSection = false;
  162. inPointsSection = false;
  163. inPolygonsSection = false;
  164. inTriangleStripSection = false;
  165. } else if ( patNORMALS.exec( line ) !== null ) {
  166. inNormalsSection = true;
  167. inColorSection = false;
  168. inPointsSection = false;
  169. inPolygonsSection = false;
  170. inTriangleStripSection = false;
  171. }
  172. }
  173. var geometry;
  174. var stagger = 'point';
  175. if ( colors.length == indices.length ) {
  176. stagger = 'cell';
  177. }
  178. if ( stagger == 'point' ) {
  179. // Nodal. Use BufferGeometry
  180. geometry = new THREE.BufferGeometry();
  181. geometry.setIndex( new THREE.BufferAttribute( new ( indices.length > 65535 ? Uint32Array : Uint16Array )( indices ), 1 ) );
  182. geometry.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( positions ), 3 ) );
  183. if ( colors.length == positions.length ) {
  184. geometry.addAttribute( 'color', new THREE.BufferAttribute( new Float32Array( colors ), 3 ) );
  185. }
  186. if ( normals.length == positions.length ) {
  187. geometry.addAttribute( 'normal', new THREE.BufferAttribute( new Float32Array( normals ), 3 ) );
  188. }
  189. } else {
  190. // Cell centered colors. The only way to attach a solid color to each triangle
  191. // is to use Geometry, which is less efficient than BufferGeometry
  192. geometry = new THREE.Geometry();
  193. var numTriangles = indices.length / 3;
  194. var numPoints = positions.length / 3;
  195. var va, vb, vc;
  196. var face;
  197. var ia, ib, ic;
  198. var x, y, z;
  199. var r, g, b;
  200. for ( var j = 0; j < numPoints; ++ j ) {
  201. x = positions[ 3 * j + 0 ];
  202. y = positions[ 3 * j + 1 ];
  203. z = positions[ 3 * j + 2 ];
  204. geometry.vertices.push( new THREE.Vector3( x, y, z ) );
  205. }
  206. for ( var i = 0; i < numTriangles; ++ i ) {
  207. ia = indices[ 3 * i + 0 ];
  208. ib = indices[ 3 * i + 1 ];
  209. ic = indices[ 3 * i + 2 ];
  210. geometry.faces.push( new THREE.Face3( ia, ib, ic ) );
  211. }
  212. if ( colors.length == numTriangles * 3 ) {
  213. for ( var i = 0; i < numTriangles; ++ i ) {
  214. face = geometry.faces[ i ];
  215. r = colors[ 3 * i + 0 ];
  216. g = colors[ 3 * i + 1 ];
  217. b = colors[ 3 * i + 2 ];
  218. face.color = new THREE.Color().setRGB( r, g, b );
  219. }
  220. }
  221. }
  222. return geometry;
  223. },
  224. parseBinary: function( data ) {
  225. var count, pointIndex, i, numberOfPoints, pt, s;
  226. var buffer = new Uint8Array ( data );
  227. var dataView = new DataView ( data );
  228. // Points and normals, by default, are empty
  229. var points = [];
  230. var normals = [];
  231. var indices = [];
  232. // Going to make a big array of strings
  233. var vtk = [];
  234. var index = 0;
  235. var findString = function( buffer, start ) {
  236. var index = start;
  237. var c = buffer[ index ];
  238. var s = [];
  239. while ( c != 10 ) {
  240. s.push ( String.fromCharCode ( c ) );
  241. index ++;
  242. c = buffer[ index ];
  243. }
  244. return { start: start,
  245. end: index,
  246. next: index + 1,
  247. parsedString: s.join( '' ) };
  248. }
  249. var state, line;
  250. while ( true ) {
  251. // Get a string
  252. state = findString ( buffer, index );
  253. line = state.parsedString;
  254. if ( line.indexOf ( "POINTS" ) == 0 ) {
  255. vtk.push ( line );
  256. // Add the points
  257. numberOfPoints = parseInt ( line.split( " " )[ 1 ], 10 );
  258. // Each point is 3 4-byte floats
  259. count = numberOfPoints * 4 * 3;
  260. points = new Float32Array( numberOfPoints * 3 );
  261. pointIndex = state.next;
  262. for ( i = 0; i < numberOfPoints; i ++ ) {
  263. points[ 3 * i ] = dataView.getFloat32( pointIndex, false );
  264. points[ 3 * i + 1 ] = dataView.getFloat32( pointIndex + 4, false );
  265. points[ 3 * i + 2 ] = dataView.getFloat32( pointIndex + 8, false );
  266. pointIndex = pointIndex + 12;
  267. }
  268. // increment our next pointer
  269. state.next = state.next + count + 1;
  270. } else if ( line.indexOf ( "TRIANGLE_STRIPS" ) === 0 ) {
  271. var numberOfStrips = parseInt ( line.split( " " )[ 1 ], 10 );
  272. var size = parseInt ( line.split ( " " )[ 2 ], 10 );
  273. // 4 byte integers
  274. count = size * 4;
  275. indices = new Uint32Array( 3 * size - 9 * numberOfStrips );
  276. var indicesIndex = 0;
  277. pointIndex = state.next;
  278. for ( i = 0; i < numberOfStrips; i ++ ) {
  279. // For each strip, read the first value, then record that many more points
  280. var indexCount = dataView.getInt32( pointIndex, false );
  281. var strip = [];
  282. pointIndex += 4;
  283. for ( s = 0; s < indexCount; s ++ ) {
  284. strip.push ( dataView.getInt32( pointIndex, false ) );
  285. pointIndex += 4;
  286. }
  287. // retrieves the n-2 triangles from the triangle strip
  288. for ( var j = 0; j < indexCount - 2; j ++ ) {
  289. if ( j % 2 ) {
  290. indices[ indicesIndex ++ ] = strip[ j ];
  291. indices[ indicesIndex ++ ] = strip[ j + 2 ];
  292. indices[ indicesIndex ++ ] = strip[ j + 1 ];
  293. } else {
  294. indices[ indicesIndex ++ ] = strip[ j ];
  295. indices[ indicesIndex ++ ] = strip[ j + 1 ];
  296. indices[ indicesIndex ++ ] = strip[ j + 2 ];
  297. }
  298. }
  299. }
  300. // increment our next pointer
  301. state.next = state.next + count + 1;
  302. } else if ( line.indexOf ( "POLYGONS" ) === 0 ) {
  303. var numberOfStrips = parseInt ( line.split( " " )[ 1 ], 10 );
  304. var size = parseInt ( line.split ( " " )[ 2 ], 10 );
  305. // 4 byte integers
  306. count = size * 4;
  307. indices = new Uint32Array( 3 * size - 9 * numberOfStrips );
  308. var indicesIndex = 0;
  309. pointIndex = state.next;
  310. for ( i = 0; i < numberOfStrips; i ++ ) {
  311. // For each strip, read the first value, then record that many more points
  312. var indexCount = dataView.getInt32( pointIndex, false );
  313. var strip = [];
  314. pointIndex += 4;
  315. for ( s = 0; s < indexCount; s ++ ) {
  316. strip.push ( dataView.getInt32( pointIndex, false ) );
  317. pointIndex += 4;
  318. }
  319. var i0 = strip[ 0 ]
  320. // divide the polygon in n-2 triangle
  321. for ( var j = 1; j < indexCount - 1; j ++ ) {
  322. indices[ indicesIndex ++ ] = strip[ 0 ];
  323. indices[ indicesIndex ++ ] = strip[ j ];
  324. indices[ indicesIndex ++ ] = strip[ j + 1 ];
  325. }
  326. }
  327. // increment our next pointer
  328. state.next = state.next + count + 1;
  329. } else if ( line.indexOf ( "POINT_DATA" ) == 0 ) {
  330. numberOfPoints = parseInt ( line.split( " " )[ 1 ], 10 );
  331. // Grab the next line
  332. state = findString ( buffer, state.next );
  333. // Now grab the binary data
  334. count = numberOfPoints * 4 * 3;
  335. normals = new Float32Array( numberOfPoints * 3 );
  336. pointIndex = state.next;
  337. for ( i = 0; i < numberOfPoints; i ++ ) {
  338. normals[ 3 * i ] = dataView.getFloat32( pointIndex, false );
  339. normals[ 3 * i + 1 ] = dataView.getFloat32( pointIndex + 4, false );
  340. normals[ 3 * i + 2 ] = dataView.getFloat32( pointIndex + 8, false );
  341. pointIndex += 12;
  342. }
  343. // Increment past our data
  344. state.next = state.next + count;
  345. }
  346. // Increment index
  347. index = state.next;
  348. if ( index >= buffer.byteLength ) {
  349. break;
  350. }
  351. }
  352. var geometry = new THREE.BufferGeometry();
  353. geometry.setIndex( new THREE.BufferAttribute( indices, 1 ) );
  354. geometry.addAttribute( 'position', new THREE.BufferAttribute( points, 3 ) );
  355. if ( normals.length == points.length ) {
  356. geometry.addAttribute( 'normal', new THREE.BufferAttribute( normals, 3 ) );
  357. }
  358. return geometry;
  359. }
  360. };
  361. THREE.EventDispatcher.prototype.apply( THREE.VTKLoader.prototype );