VTKLoader.js 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  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. Object.assign( THREE.VTKLoader.prototype, THREE.EventDispatcher.prototype, {
  9. load: function ( url, onLoad, onProgress, onError ) {
  10. var scope = this;
  11. var loader = new THREE.FileLoader( scope.manager );
  12. loader.setResponseType( 'arraybuffer' );
  13. loader.load( url, function( text ) {
  14. onLoad( scope.parse( text ) );
  15. }, onProgress, onError );
  16. },
  17. parse: function ( data ) {
  18. function parseASCII( data ) {
  19. // connectivity of the triangles
  20. var indices = [];
  21. // triangles vertices
  22. var positions = [];
  23. // red, green, blue colors in the range 0 to 1
  24. var colors = [];
  25. // normal vector, one per vertex
  26. var normals = [];
  27. var result;
  28. // pattern for reading vertices, 3 floats or integers
  29. var pat3Floats = /(\-?\d+\.?[\d\-\+e]*)\s+(\-?\d+\.?[\d\-\+e]*)\s+(\-?\d+\.?[\d\-\+e]*)/g;
  30. // pattern for connectivity, an integer followed by any number of ints
  31. // the first integer is the number of polygon nodes
  32. var patConnectivity = /^(\d+)\s+([\s\d]*)/;
  33. // indicates start of vertex data section
  34. var patPOINTS = /^POINTS /;
  35. // indicates start of polygon connectivity section
  36. var patPOLYGONS = /^POLYGONS /;
  37. // indicates start of triangle strips section
  38. var patTRIANGLE_STRIPS = /^TRIANGLE_STRIPS /;
  39. // POINT_DATA number_of_values
  40. var patPOINT_DATA = /^POINT_DATA[ ]+(\d+)/;
  41. // CELL_DATA number_of_polys
  42. var patCELL_DATA = /^CELL_DATA[ ]+(\d+)/;
  43. // Start of color section
  44. var patCOLOR_SCALARS = /^COLOR_SCALARS[ ]+(\w+)[ ]+3/;
  45. // NORMALS Normals float
  46. var patNORMALS = /^NORMALS[ ]+(\w+)[ ]+(\w+)/;
  47. var inPointsSection = false;
  48. var inPolygonsSection = false;
  49. var inTriangleStripSection = false;
  50. var inPointDataSection = false;
  51. var inCellDataSection = false;
  52. var inColorSection = false;
  53. var inNormalsSection = false;
  54. var lines = data.split( '\n' );
  55. for ( var i in lines ) {
  56. var line = lines[ i ];
  57. if ( inPointsSection ) {
  58. // get the vertices
  59. while ( ( result = pat3Floats.exec( line ) ) !== null ) {
  60. var x = parseFloat( result[ 1 ] );
  61. var y = parseFloat( result[ 2 ] );
  62. var z = parseFloat( result[ 3 ] );
  63. positions.push( x, y, z );
  64. }
  65. } else if ( inPolygonsSection ) {
  66. if ( ( result = patConnectivity.exec( line ) ) !== null ) {
  67. // numVertices i0 i1 i2 ...
  68. var numVertices = parseInt( result[ 1 ] );
  69. var inds = result[ 2 ].split( /\s+/ );
  70. if ( numVertices >= 3 ) {
  71. var i0 = parseInt( inds[ 0 ] );
  72. var i1, i2;
  73. var k = 1;
  74. // split the polygon in numVertices - 2 triangles
  75. for ( var j = 0; j < numVertices - 2; ++ j ) {
  76. i1 = parseInt( inds[ k ] );
  77. i2 = parseInt( inds[ k + 1 ] );
  78. indices.push( i0, i1, i2 );
  79. k ++;
  80. }
  81. }
  82. }
  83. } else if ( inTriangleStripSection ) {
  84. if ( ( result = patConnectivity.exec( line ) ) !== null ) {
  85. // numVertices i0 i1 i2 ...
  86. var numVertices = parseInt( result[ 1 ] );
  87. var inds = result[ 2 ].split( /\s+/ );
  88. if ( numVertices >= 3 ) {
  89. var i0, i1, i2;
  90. // split the polygon in numVertices - 2 triangles
  91. for ( var j = 0; j < numVertices - 2; j ++ ) {
  92. if ( j % 2 === 1 ) {
  93. i0 = parseInt( inds[ j ] );
  94. i1 = parseInt( inds[ j + 2 ] );
  95. i2 = parseInt( inds[ j + 1 ] );
  96. indices.push( i0, i1, i2 );
  97. } else {
  98. i0 = parseInt( inds[ j ] );
  99. i1 = parseInt( inds[ j + 1 ] );
  100. i2 = parseInt( inds[ j + 2 ] );
  101. indices.push( i0, i1, i2 );
  102. }
  103. }
  104. }
  105. }
  106. } else if ( inPointDataSection || inCellDataSection ) {
  107. if ( inColorSection ) {
  108. // Get the colors
  109. while ( ( result = pat3Floats.exec( line ) ) !== null ) {
  110. var r = parseFloat( result[ 1 ] );
  111. var g = parseFloat( result[ 2 ] );
  112. var b = parseFloat( result[ 3 ] );
  113. colors.push( r, g, b );
  114. }
  115. } else if ( inNormalsSection ) {
  116. // Get the normal vectors
  117. while ( ( result = pat3Floats.exec( line ) ) !== null ) {
  118. var nx = parseFloat( result[ 1 ] );
  119. var ny = parseFloat( result[ 2 ] );
  120. var nz = parseFloat( result[ 3 ] );
  121. normals.push( nx, ny, nz );
  122. }
  123. }
  124. }
  125. if ( patPOLYGONS.exec( line ) !== null ) {
  126. inPolygonsSection = true;
  127. inPointsSection = false;
  128. inTriangleStripSection = false;
  129. } else if ( patPOINTS.exec( line ) !== null ) {
  130. inPolygonsSection = false;
  131. inPointsSection = true;
  132. inTriangleStripSection = false;
  133. } else if ( patTRIANGLE_STRIPS.exec( line ) !== null ) {
  134. inPolygonsSection = false;
  135. inPointsSection = false;
  136. inTriangleStripSection = true;
  137. } else if ( patPOINT_DATA.exec( line ) !== null ) {
  138. inPointDataSection = true;
  139. inPointsSection = false;
  140. inPolygonsSection = false;
  141. inTriangleStripSection = false;
  142. } else if ( patCELL_DATA.exec( line ) !== null ) {
  143. inCellDataSection = true;
  144. inPointsSection = false;
  145. inPolygonsSection = false;
  146. inTriangleStripSection = false;
  147. } else if ( patCOLOR_SCALARS.exec( line ) !== null ) {
  148. inColorSection = true;
  149. inNormalsSection = false;
  150. inPointsSection = false;
  151. inPolygonsSection = false;
  152. inTriangleStripSection = false;
  153. } else if ( patNORMALS.exec( line ) !== null ) {
  154. inNormalsSection = true;
  155. inColorSection = false;
  156. inPointsSection = false;
  157. inPolygonsSection = false;
  158. inTriangleStripSection = false;
  159. }
  160. }
  161. var geometry;
  162. var stagger = 'point';
  163. if ( colors.length == indices.length ) {
  164. stagger = 'cell';
  165. }
  166. if ( stagger == 'point' ) {
  167. // Nodal. Use BufferGeometry
  168. geometry = new THREE.BufferGeometry();
  169. geometry.setIndex( new THREE.BufferAttribute( new Uint32Array( indices ), 1 ) );
  170. geometry.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( positions ), 3 ) );
  171. if ( colors.length == positions.length ) {
  172. geometry.addAttribute( 'color', new THREE.BufferAttribute( new Float32Array( colors ), 3 ) );
  173. }
  174. if ( normals.length == positions.length ) {
  175. geometry.addAttribute( 'normal', new THREE.BufferAttribute( new Float32Array( normals ), 3 ) );
  176. }
  177. } else {
  178. // Cell centered colors. The only way to attach a solid color to each triangle
  179. // is to use Geometry, which is less efficient than BufferGeometry
  180. geometry = new THREE.Geometry();
  181. var numTriangles = indices.length / 3;
  182. var numPoints = positions.length / 3;
  183. var va, vb, vc;
  184. var face;
  185. var ia, ib, ic;
  186. var x, y, z;
  187. var r, g, b;
  188. for ( var j = 0; j < numPoints; ++ j ) {
  189. x = positions[ 3 * j + 0 ];
  190. y = positions[ 3 * j + 1 ];
  191. z = positions[ 3 * j + 2 ];
  192. geometry.vertices.push( new THREE.Vector3( x, y, z ) );
  193. }
  194. for ( var i = 0; i < numTriangles; ++ i ) {
  195. ia = indices[ 3 * i + 0 ];
  196. ib = indices[ 3 * i + 1 ];
  197. ic = indices[ 3 * i + 2 ];
  198. geometry.faces.push( new THREE.Face3( ia, ib, ic ) );
  199. }
  200. if ( colors.length == numTriangles * 3 ) {
  201. for ( var i = 0; i < numTriangles; ++ i ) {
  202. face = geometry.faces[ i ];
  203. r = colors[ 3 * i + 0 ];
  204. g = colors[ 3 * i + 1 ];
  205. b = colors[ 3 * i + 2 ];
  206. face.color = new THREE.Color().setRGB( r, g, b );
  207. }
  208. }
  209. }
  210. return geometry;
  211. }
  212. function parseBinary( data ) {
  213. var count, pointIndex, i, numberOfPoints, pt, s;
  214. var buffer = new Uint8Array ( data );
  215. var dataView = new DataView ( data );
  216. // Points and normals, by default, are empty
  217. var points = [];
  218. var normals = [];
  219. var indices = [];
  220. // Going to make a big array of strings
  221. var vtk = [];
  222. var index = 0;
  223. function findString( buffer, start ) {
  224. var index = start;
  225. var c = buffer[ index ];
  226. var s = [];
  227. while ( c != 10 ) {
  228. s.push ( String.fromCharCode ( c ) );
  229. index ++;
  230. c = buffer[ index ];
  231. }
  232. return { start: start,
  233. end: index,
  234. next: index + 1,
  235. parsedString: s.join( '' ) };
  236. }
  237. var state, line;
  238. while ( true ) {
  239. // Get a string
  240. state = findString ( buffer, index );
  241. line = state.parsedString;
  242. if ( line.indexOf ( 'POINTS' ) === 0 ) {
  243. vtk.push ( line );
  244. // Add the points
  245. numberOfPoints = parseInt ( line.split( ' ' )[ 1 ], 10 );
  246. // Each point is 3 4-byte floats
  247. count = numberOfPoints * 4 * 3;
  248. points = new Float32Array( numberOfPoints * 3 );
  249. pointIndex = state.next;
  250. for ( i = 0; i < numberOfPoints; i ++ ) {
  251. points[ 3 * i ] = dataView.getFloat32( pointIndex, false );
  252. points[ 3 * i + 1 ] = dataView.getFloat32( pointIndex + 4, false );
  253. points[ 3 * i + 2 ] = dataView.getFloat32( pointIndex + 8, false );
  254. pointIndex = pointIndex + 12;
  255. }
  256. // increment our next pointer
  257. state.next = state.next + count + 1;
  258. } else if ( line.indexOf ( 'TRIANGLE_STRIPS' ) === 0 ) {
  259. var numberOfStrips = parseInt ( line.split( ' ' )[ 1 ], 10 );
  260. var size = parseInt ( line.split ( ' ' )[ 2 ], 10 );
  261. // 4 byte integers
  262. count = size * 4;
  263. indices = new Uint32Array( 3 * size - 9 * numberOfStrips );
  264. var indicesIndex = 0;
  265. pointIndex = state.next;
  266. for ( i = 0; i < numberOfStrips; i ++ ) {
  267. // For each strip, read the first value, then record that many more points
  268. var indexCount = dataView.getInt32( pointIndex, false );
  269. var strip = [];
  270. pointIndex += 4;
  271. for ( s = 0; s < indexCount; s ++ ) {
  272. strip.push ( dataView.getInt32( pointIndex, false ) );
  273. pointIndex += 4;
  274. }
  275. // retrieves the n-2 triangles from the triangle strip
  276. for ( var j = 0; j < indexCount - 2; j ++ ) {
  277. if ( j % 2 ) {
  278. indices[ indicesIndex ++ ] = strip[ j ];
  279. indices[ indicesIndex ++ ] = strip[ j + 2 ];
  280. indices[ indicesIndex ++ ] = strip[ j + 1 ];
  281. } else {
  282. indices[ indicesIndex ++ ] = strip[ j ];
  283. indices[ indicesIndex ++ ] = strip[ j + 1 ];
  284. indices[ indicesIndex ++ ] = strip[ j + 2 ];
  285. }
  286. }
  287. }
  288. // increment our next pointer
  289. state.next = state.next + count + 1;
  290. } else if ( line.indexOf ( 'POLYGONS' ) === 0 ) {
  291. var numberOfStrips = parseInt ( line.split( ' ' )[ 1 ], 10 );
  292. var size = parseInt ( line.split ( ' ' )[ 2 ], 10 );
  293. // 4 byte integers
  294. count = size * 4;
  295. indices = new Uint32Array( 3 * size - 9 * numberOfStrips );
  296. var indicesIndex = 0;
  297. pointIndex = state.next;
  298. for ( i = 0; i < numberOfStrips; i ++ ) {
  299. // For each strip, read the first value, then record that many more points
  300. var indexCount = dataView.getInt32( pointIndex, false );
  301. var strip = [];
  302. pointIndex += 4;
  303. for ( s = 0; s < indexCount; s ++ ) {
  304. strip.push ( dataView.getInt32( pointIndex, false ) );
  305. pointIndex += 4;
  306. }
  307. var i0 = strip[ 0 ];
  308. // divide the polygon in n-2 triangle
  309. for ( var j = 1; j < indexCount - 1; j ++ ) {
  310. indices[ indicesIndex ++ ] = strip[ 0 ];
  311. indices[ indicesIndex ++ ] = strip[ j ];
  312. indices[ indicesIndex ++ ] = strip[ j + 1 ];
  313. }
  314. }
  315. // increment our next pointer
  316. state.next = state.next + count + 1;
  317. } else if ( line.indexOf ( 'POINT_DATA' ) === 0 ) {
  318. numberOfPoints = parseInt ( line.split( ' ' )[ 1 ], 10 );
  319. // Grab the next line
  320. state = findString ( buffer, state.next );
  321. // Now grab the binary data
  322. count = numberOfPoints * 4 * 3;
  323. normals = new Float32Array( numberOfPoints * 3 );
  324. pointIndex = state.next;
  325. for ( i = 0; i < numberOfPoints; i ++ ) {
  326. normals[ 3 * i ] = dataView.getFloat32( pointIndex, false );
  327. normals[ 3 * i + 1 ] = dataView.getFloat32( pointIndex + 4, false );
  328. normals[ 3 * i + 2 ] = dataView.getFloat32( pointIndex + 8, false );
  329. pointIndex += 12;
  330. }
  331. // Increment past our data
  332. state.next = state.next + count;
  333. }
  334. // Increment index
  335. index = state.next;
  336. if ( index >= buffer.byteLength ) {
  337. break;
  338. }
  339. }
  340. var geometry = new THREE.BufferGeometry();
  341. geometry.setIndex( new THREE.BufferAttribute( indices, 1 ) );
  342. geometry.addAttribute( 'position', new THREE.BufferAttribute( points, 3 ) );
  343. if ( normals.length == points.length ) {
  344. geometry.addAttribute( 'normal', new THREE.BufferAttribute( normals, 3 ) );
  345. }
  346. return geometry;
  347. }
  348. function parseXML( stringFile ) {
  349. // Changes XML to JSON, based on https://davidwalsh.name/convert-xml-json
  350. function xmlToJson( xml ) {
  351. // Create the return object
  352. var obj = {};
  353. if ( xml.nodeType == 1 ) { // element
  354. // do attributes
  355. if ( xml.attributes ) {
  356. if ( xml.attributes.length > 0 ) {
  357. obj[ 'attributes' ] = {};
  358. for ( var j = 0; j < xml.attributes.length; j ++ ) {
  359. var attribute = xml.attributes.item( j );
  360. obj[ 'attributes' ][ attribute.nodeName ] = attribute.nodeValue.trim();
  361. }
  362. }
  363. }
  364. } else if ( xml.nodeType == 3 ) { // text
  365. obj = xml.nodeValue.trim();
  366. }
  367. // do children
  368. if ( xml.hasChildNodes() ) {
  369. for ( var i = 0; i < xml.childNodes.length; i ++ ) {
  370. var item = xml.childNodes.item( i );
  371. var nodeName = item.nodeName;
  372. if ( typeof( obj[ nodeName ] ) === 'undefined' ) {
  373. var tmp = xmlToJson( item );
  374. if ( tmp !== '' ) obj[ nodeName ] = tmp;
  375. } else {
  376. if ( typeof( obj[ nodeName ].push ) === 'undefined' ) {
  377. var old = obj[ nodeName ];
  378. obj[ nodeName ] = [ old ];
  379. }
  380. var tmp = xmlToJson( item );
  381. if ( tmp !== '' ) obj[ nodeName ].push( tmp );
  382. }
  383. }
  384. }
  385. return obj;
  386. }
  387. // Taken from Base64-js
  388. function Base64toByteArray( b64 ) {
  389. var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array;
  390. var i;
  391. var lookup = [];
  392. var revLookup = [];
  393. var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  394. var len = code.length;
  395. for ( i = 0; i < len; i ++ ) {
  396. lookup[ i ] = code[ i ];
  397. }
  398. for ( i = 0; i < len; ++ i ) {
  399. revLookup[ code.charCodeAt( i ) ] = i;
  400. }
  401. revLookup[ '-'.charCodeAt( 0 ) ] = 62;
  402. revLookup[ '_'.charCodeAt( 0 ) ] = 63;
  403. var j, l, tmp, placeHolders, arr;
  404. var len = b64.length;
  405. if ( len % 4 > 0 ) {
  406. throw new Error( 'Invalid string. Length must be a multiple of 4' );
  407. }
  408. placeHolders = b64[ len - 2 ] === '=' ? 2 : b64[ len - 1 ] === '=' ? 1 : 0;
  409. arr = new Arr( len * 3 / 4 - placeHolders );
  410. l = placeHolders > 0 ? len - 4 : len;
  411. var L = 0;
  412. for ( i = 0, j = 0; i < l; i += 4, j += 3 ) {
  413. tmp = ( revLookup[ b64.charCodeAt( i ) ] << 18 ) | ( revLookup[ b64.charCodeAt( i + 1 ) ] << 12 ) | ( revLookup[ b64.charCodeAt( i + 2 ) ] << 6 ) | revLookup[ b64.charCodeAt( i + 3 ) ];
  414. arr[ L ++ ] = ( tmp & 0xFF0000 ) >> 16;
  415. arr[ L ++ ] = ( tmp & 0xFF00 ) >> 8;
  416. arr[ L ++ ] = tmp & 0xFF;
  417. }
  418. if ( placeHolders === 2 ) {
  419. tmp = ( revLookup[ b64.charCodeAt( i ) ] << 2 ) | ( revLookup[ b64.charCodeAt( i + 1 ) ] >> 4 );
  420. arr[ L ++ ] = tmp & 0xFF;
  421. } else if ( placeHolders === 1 ) {
  422. tmp = ( revLookup[ b64.charCodeAt( i ) ] << 10 ) | ( revLookup[ b64.charCodeAt( i + 1 ) ] << 4 ) | ( revLookup[ b64.charCodeAt( i + 2 ) ] >> 2 );
  423. arr[ L ++ ] = ( tmp >> 8 ) & 0xFF;
  424. arr[ L ++ ] = tmp & 0xFF;
  425. }
  426. return arr;
  427. }
  428. function parseDataArray( ele, compressed ) {
  429. // Check the format
  430. if ( ele.attributes.format == 'binary' ) {
  431. if ( compressed ) {
  432. // Split the blob_header and compressed Data
  433. if ( ele[ '#text' ].indexOf( '==' ) != - 1 ) {
  434. var data = ele[ '#text' ].split( '==' );
  435. // console.log( data );
  436. if ( data.length == 2 ) {
  437. var blob = data.shift();
  438. var content = data.shift();
  439. if ( content === '' ) {
  440. content = blob + '==';
  441. }
  442. } else if ( data.length > 2 ) {
  443. var blob = data.shift();
  444. var content = data.shift();
  445. content = content + '==';
  446. } else if ( data.length < 2 ) {
  447. var content = data.shift();
  448. content = content + '==';
  449. }
  450. // Convert to bytearray
  451. var arr = Base64toByteArray( content );
  452. // decompress
  453. var inflate = new Zlib.Inflate( arr, { resize: true, verify: true } );
  454. var content = inflate.decompress();
  455. } else {
  456. var content = Base64toByteArray( ele[ '#text' ] );
  457. }
  458. } else {
  459. var content = Base64toByteArray( ele[ '#text' ] );
  460. }
  461. var content = content.buffer;
  462. } else {
  463. if ( ele[ '#text' ] ) {
  464. var content = ele[ '#text' ].replace( /\n/g, ' ' ).split( ' ' ).filter( function ( el, idx, arr ) {
  465. if ( el !== '' ) return el;
  466. } );
  467. } else {
  468. var content = new Int32Array( 0 ).buffer;
  469. }
  470. }
  471. delete ele[ '#text' ];
  472. // Get the content and optimize it
  473. if ( ele.attributes.type == 'Float32' ) {
  474. var txt = new Float32Array( content );
  475. if ( ele.attributes.format == 'binary' ) {
  476. if ( ! compressed ) {
  477. txt = txt.filter( function( el, idx, arr ) {
  478. if ( idx !== 0 ) return true;
  479. } );
  480. }
  481. }
  482. } else if ( ele.attributes.type === 'Int64' ) {
  483. var txt = new Int32Array( content );
  484. if ( ele.attributes.format == 'binary' ) {
  485. if ( ! compressed ) {
  486. txt = txt.filter( function ( el, idx, arr ) {
  487. if ( idx !== 0 ) return true;
  488. } );
  489. }
  490. txt = txt.filter( function ( el, idx, arr ) {
  491. if ( idx % 2 !== 1 ) return true;
  492. } );
  493. }
  494. }
  495. // console.log( txt );
  496. return txt;
  497. }
  498. // Main part
  499. // Get Dom
  500. var dom = null;
  501. if ( window.DOMParser ) {
  502. try {
  503. dom = ( new DOMParser() ).parseFromString( stringFile, 'text/xml' );
  504. } catch ( e ) {
  505. dom = null;
  506. }
  507. } else if ( window.ActiveXObject ) {
  508. try {
  509. dom = new ActiveXObject( 'Microsoft.XMLDOM' );
  510. dom.async = false;
  511. if ( ! dom.loadXML( xml ) ) {
  512. throw new Error( dom.parseError.reason + dom.parseError.srcText );
  513. }
  514. } catch ( e ) {
  515. dom = null;
  516. }
  517. } else {
  518. throw new Error( 'Cannot parse xml string!' );
  519. }
  520. // Get the doc
  521. var doc = dom.documentElement;
  522. // Convert to json
  523. var json = xmlToJson( doc );
  524. var points = [];
  525. var normals = [];
  526. var indices = [];
  527. if ( json.PolyData ) {
  528. var piece = json.PolyData.Piece;
  529. var compressed = json.attributes.hasOwnProperty( 'compressor' );
  530. // Can be optimized
  531. // Loop through the sections
  532. var sections = [ 'PointData', 'Points', 'Strips', 'Polys' ];// +['CellData', 'Verts', 'Lines'];
  533. var sectionIndex = 0, numberOfSections = sections.length;
  534. while ( sectionIndex < numberOfSections ) {
  535. var section = piece[ sections[ sectionIndex ] ];
  536. // If it has a DataArray in it
  537. if ( section.DataArray ) {
  538. // Depending on the number of DataArrays
  539. if ( Object.prototype.toString.call( section.DataArray ) === '[object Array]' ) {
  540. var arr = section.DataArray;
  541. } else {
  542. var arr = [ section.DataArray ];
  543. }
  544. var dataArrayIndex = 0, numberOfDataArrays = arr.length;
  545. while ( dataArrayIndex < numberOfDataArrays ) {
  546. // Parse the DataArray
  547. arr[ dataArrayIndex ].text = parseDataArray( arr[ dataArrayIndex ], compressed );
  548. dataArrayIndex ++;
  549. }
  550. switch ( sections[ sectionIndex ] ) {
  551. // if iti is point data
  552. case 'PointData':
  553. var numberOfPoints = parseInt( piece.attributes.NumberOfPoints );
  554. var normalsName = section.attributes.Normals;
  555. if ( numberOfPoints > 0 ) {
  556. for ( var i = 0, len = arr.length; i < len; i ++ ) {
  557. if ( normalsName == arr[ i ].attributes.Name ) {
  558. var components = arr[ i ].attributes.NumberOfComponents;
  559. normals = new Float32Array( numberOfPoints * components );
  560. normals.set( arr[ i ].text, 0 );
  561. }
  562. }
  563. }
  564. // console.log('Normals', normals);
  565. break;
  566. // if it is points
  567. case 'Points':
  568. var numberOfPoints = parseInt( piece.attributes.NumberOfPoints );
  569. if ( numberOfPoints > 0 ) {
  570. var components = section.DataArray.attributes.NumberOfComponents;
  571. points = new Float32Array( numberOfPoints * components );
  572. points.set( section.DataArray.text, 0 );
  573. }
  574. // console.log('Points', points);
  575. break;
  576. // if it is strips
  577. case 'Strips':
  578. var numberOfStrips = parseInt( piece.attributes.NumberOfStrips );
  579. if ( numberOfStrips > 0 ) {
  580. var connectivity = new Int32Array( section.DataArray[ 0 ].text.length );
  581. var offset = new Int32Array( section.DataArray[ 1 ].text.length );
  582. connectivity.set( section.DataArray[ 0 ].text, 0 );
  583. offset.set( section.DataArray[ 1 ].text, 0 );
  584. var size = numberOfStrips + connectivity.length;
  585. indices = new Uint32Array( 3 * size - 9 * numberOfStrips );
  586. var indicesIndex = 0;
  587. for ( var i = 0,len = numberOfStrips; i < len; i ++ ) {
  588. var strip = [];
  589. for ( var s = 0, len1 = offset[ i ], len0 = 0; s < len1 - len0; s ++ ) {
  590. strip.push ( connectivity[ s ] );
  591. if ( i > 0 ) len0 = offset[ i - 1 ];
  592. }
  593. for ( var j = 0, len1 = offset[ i ], len0 = 0; j < len1 - len0 - 2; j ++ ) {
  594. if ( j % 2 ) {
  595. indices[ indicesIndex ++ ] = strip[ j ];
  596. indices[ indicesIndex ++ ] = strip[ j + 2 ];
  597. indices[ indicesIndex ++ ] = strip[ j + 1 ];
  598. } else {
  599. indices[ indicesIndex ++ ] = strip[ j ];
  600. indices[ indicesIndex ++ ] = strip[ j + 1 ];
  601. indices[ indicesIndex ++ ] = strip[ j + 2 ];
  602. }
  603. if ( i > 0 ) len0 = offset[ i - 1 ];
  604. }
  605. }
  606. }
  607. //console.log('Strips', indices);
  608. break;
  609. // if it is polys
  610. case 'Polys':
  611. var numberOfPolys = parseInt( piece.attributes.NumberOfPolys );
  612. if ( numberOfPolys > 0 ) {
  613. var connectivity = new Int32Array( section.DataArray[ 0 ].text.length );
  614. var offset = new Int32Array( section.DataArray[ 1 ].text.length );
  615. connectivity.set( section.DataArray[ 0 ].text, 0 );
  616. offset.set( section.DataArray[ 1 ].text, 0 );
  617. var size = numberOfPolys + connectivity.length;
  618. indices = new Uint32Array( 3 * size - 9 * numberOfPolys );
  619. var indicesIndex = 0, connectivityIndex = 0;
  620. var i = 0,len = numberOfPolys, len0 = 0;
  621. while ( i < len ) {
  622. var poly = [];
  623. var s = 0, len1 = offset[ i ];
  624. while ( s < len1 - len0 ) {
  625. poly.push( connectivity[ connectivityIndex ++ ] );
  626. s ++;
  627. }
  628. var j = 1;
  629. while ( j < len1 - len0 - 1 ) {
  630. indices[ indicesIndex ++ ] = poly[ 0 ];
  631. indices[ indicesIndex ++ ] = poly[ j ];
  632. indices[ indicesIndex ++ ] = poly[ j + 1 ];
  633. j ++;
  634. }
  635. i ++;
  636. len0 = offset[ i - 1 ];
  637. }
  638. }
  639. //console.log('Polys', indices);
  640. break;
  641. default:
  642. break;
  643. }
  644. }
  645. sectionIndex ++;
  646. }
  647. var geometry = new THREE.BufferGeometry();
  648. geometry.setIndex( new THREE.BufferAttribute( indices, 1 ) );
  649. geometry.addAttribute( 'position', new THREE.BufferAttribute( points, 3 ) );
  650. if ( normals.length == points.length ) {
  651. geometry.addAttribute( 'normal', new THREE.BufferAttribute( normals, 3 ) );
  652. }
  653. // console.log( json );
  654. return geometry;
  655. } else {
  656. // TODO for vtu,vti,and other xml formats
  657. }
  658. }
  659. function getStringFile( data ) {
  660. var stringFile = '';
  661. var charArray = new Uint8Array( data );
  662. var i = 0;
  663. var len = charArray.length;
  664. while ( len -- ) {
  665. stringFile += String.fromCharCode( charArray[ i ++ ] );
  666. }
  667. return stringFile;
  668. }
  669. // get the 5 first lines of the files to check if there is the key word binary
  670. var meta = String.fromCharCode.apply( null, new Uint8Array( data, 0, 250 ) ).split( '\n' );
  671. if ( meta[ 0 ].indexOf( 'xml' ) !== - 1 ) {
  672. return parseXML( getStringFile( data ) );
  673. } else if ( meta[ 2 ].includes( 'ASCII' ) ) {
  674. return parseASCII( getStringFile( data ) );
  675. } else {
  676. return parseBinary( data );
  677. }
  678. }
  679. } );