VTKLoader.js 13 KB

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