Browse Source

VTKLoader clean up.
@dvenkatsagar do you mind trying it with your models to make sure I didn't break anything?

Mr.doob 9 years ago
parent
commit
f8e6b7542a
2 changed files with 475 additions and 268 deletions
  1. 441 241
      examples/js/loaders/VTKLoader.js
  2. 34 27
      examples/webgl_loader_vtk.html

+ 441 - 241
examples/js/loaders/VTKLoader.js

@@ -369,11 +369,11 @@ THREE.VTKLoader.prototype = {
 				state = findString ( buffer, index );
 				line = state.parsedString;
 
-				if ( line.indexOf ( "POINTS" ) === 0 ) {
+				if ( line.indexOf ( 'POINTS' ) === 0 ) {
 
 					vtk.push ( line );
 					// Add the points
-					numberOfPoints = parseInt ( line.split( " " )[ 1 ], 10 );
+					numberOfPoints = parseInt ( line.split( ' ' )[ 1 ], 10 );
 
 					// Each point is 3 4-byte floats
 					count = numberOfPoints * 4 * 3;
@@ -392,10 +392,10 @@ THREE.VTKLoader.prototype = {
 					// increment our next pointer
 					state.next = state.next + count + 1;
 
-				} else if ( line.indexOf ( "TRIANGLE_STRIPS" ) === 0 ) {
+				} else if ( line.indexOf ( 'TRIANGLE_STRIPS' ) === 0 ) {
 
-					var numberOfStrips = parseInt ( line.split( " " )[ 1 ], 10 );
-					var size = parseInt ( line.split ( " " )[ 2 ], 10 );
+					var numberOfStrips = parseInt ( line.split( ' ' )[ 1 ], 10 );
+					var size = parseInt ( line.split ( ' ' )[ 2 ], 10 );
 					// 4 byte integers
 					count = size * 4;
 
@@ -440,10 +440,10 @@ THREE.VTKLoader.prototype = {
 					// increment our next pointer
 					state.next = state.next + count + 1;
 
-				} else if ( line.indexOf ( "POLYGONS" ) === 0 ) {
+				} else if ( line.indexOf ( 'POLYGONS' ) === 0 ) {
 
-					var numberOfStrips = parseInt ( line.split( " " )[ 1 ], 10 );
-					var size = parseInt ( line.split ( " " )[ 2 ], 10 );
+					var numberOfStrips = parseInt ( line.split( ' ' )[ 1 ], 10 );
+					var size = parseInt ( line.split ( ' ' )[ 2 ], 10 );
 					// 4 byte integers
 					count = size * 4;
 
@@ -477,9 +477,9 @@ THREE.VTKLoader.prototype = {
 					// increment our next pointer
 					state.next = state.next + count + 1;
 
-				} else if ( line.indexOf ( "POINT_DATA" ) === 0 ) {
+				} else if ( line.indexOf ( 'POINT_DATA' ) === 0 ) {
 
-					numberOfPoints = parseInt ( line.split( " " )[ 1 ], 10 );
+					numberOfPoints = parseInt ( line.split( ' ' )[ 1 ], 10 );
 
 					// Grab the next line
 					state = findString ( buffer, state.next );
@@ -528,337 +528,526 @@ THREE.VTKLoader.prototype = {
 
 		}
 
-		function parseXML(stringFile){
+		function parseXML( stringFile ) {
+
 			// Changes XML to JSON, based on https://davidwalsh.name/convert-xml-json
-			var xmlToJson = function (xml) {
+
+			function xmlToJson( xml ) {
 
 				// Create the return object
 				var obj = {};
 
-				if (xml.nodeType == 1) { // element
+				if ( xml.nodeType == 1 ) { // element
+
 					// do attributes
-					if (xml.attributes) {
-						if (xml.attributes.length > 0) {
-						obj["attributes"] = {};
-							for (var j = 0; j < xml.attributes.length; j++) {
-								var attribute = xml.attributes.item(j);
-								obj["attributes"][attribute.nodeName] = attribute.nodeValue.trim();
+
+					if ( xml.attributes ) {
+
+						if ( xml.attributes.length > 0 ) {
+
+							obj[ 'attributes' ] = {};
+
+							for ( var j = 0; j < xml.attributes.length; j ++ ) {
+
+								var attribute = xml.attributes.item( j );
+								obj[ 'attributes' ][ attribute.nodeName ] = attribute.nodeValue.trim();
+
 							}
+
 						}
+
 					}
-				} else if (xml.nodeType == 3) { // text
+
+				} else if ( xml.nodeType == 3 ) { // text
+
 					obj = xml.nodeValue.trim();
+
 				}
 
 				// do children
-			 	if (xml.hasChildNodes()) {
-					for(var i = 0; i < xml.childNodes.length; i++) {
-						var item = xml.childNodes.item(i);
+				if ( xml.hasChildNodes() ) {
+
+					for ( var i = 0; i < xml.childNodes.length; i ++ ) {
+
+						var item = xml.childNodes.item( i );
 						var nodeName = item.nodeName;
-						if (typeof(obj[nodeName]) == "undefined") {
-							var tmp = xmlToJson(item);
-							if(tmp != ""){
-								obj[nodeName] = tmp;
-							}
+
+						if ( typeof( obj[ nodeName ] ) === 'undefined' ) {
+
+							var tmp = xmlToJson( item );
+
+							if ( tmp !== '' ) obj[ nodeName ] = tmp;
+
 						} else {
-							if (typeof(obj[nodeName].push) == "undefined") {
-								var old = obj[nodeName];
-								obj[nodeName] = [];
-								obj[nodeName].push(old);
-							}
-							var tmp = xmlToJson(item);
-							if(tmp != ""){
-								obj[nodeName].push(tmp);
+
+							if ( typeof( obj[ nodeName ].push ) === 'undefined' ) {
+
+								var old = obj[ nodeName ];
+								obj[ nodeName ] = [ old ];
+
 							}
+
+							var tmp = xmlToJson( item );
+
+							if ( tmp !== '' ) obj[ nodeName ].push( tmp );
+
 						}
+
 					}
+
 				}
+
 				return obj;
-			};
+
+			}
 
 			// Taken from Base64-js
-			var Base64toByteArray = function(b64) {
-					var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array;
-					var i;
-					var lookup = [];
-					var revLookup = [];
-				  var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
-				  var len = code.length;
-
-				  for (i = 0; i < len; i++) {
-				    lookup[i] = code[i];
-				  }
-
-				  for (i = 0; i < len; ++i) {
-				    revLookup[code.charCodeAt(i)] = i;
-				  }
-				  revLookup['-'.charCodeAt(0)] = 62;
-				  revLookup['_'.charCodeAt(0)] = 63;
-
-				  var j, l, tmp, placeHolders, arr;
-				  var len = b64.length;
-
-				  if (len % 4 > 0) {
-				    throw new Error('Invalid string. Length must be a multiple of 4');
-				  }
-
-				  placeHolders = b64[len - 2] === '=' ? 2 : b64[len - 1] === '=' ? 1 : 0;
-				  arr = new Arr(len * 3 / 4 - placeHolders);
-				  l = placeHolders > 0 ? len - 4 : len;
-
-					var L = 0;
-				  for (i = 0, j = 0; i < l; i += 4, j += 3) {
-				    tmp = (revLookup[b64.charCodeAt(i)] << 18) | (revLookup[b64.charCodeAt(i + 1)] << 12) | (revLookup[b64.charCodeAt(i + 2)] << 6) | revLookup[b64.charCodeAt(i + 3)];
-				    arr[L++] = (tmp & 0xFF0000) >> 16;
-				    arr[L++] = (tmp & 0xFF00) >> 8;
-				    arr[L++] = tmp & 0xFF;
-				  }
-
-				  if (placeHolders === 2) {
-				    tmp = (revLookup[b64.charCodeAt(i)] << 2) | (revLookup[b64.charCodeAt(i + 1)] >> 4);
-				    arr[L++] = tmp & 0xFF;
-				  } else if (placeHolders === 1) {
-				    tmp = (revLookup[b64.charCodeAt(i)] << 10) | (revLookup[b64.charCodeAt(i + 1)] << 4) | (revLookup[b64.charCodeAt(i + 2)] >> 2);
-				    arr[L++] = (tmp >> 8) & 0xFF;
-				    arr[L++] = tmp & 0xFF;
-				  }
-
-				  return arr;
+			function Base64toByteArray( b64 ) {
+
+				var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array;
+				var i;
+				var lookup = [];
+				var revLookup = [];
+				var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
+				var len = code.length;
+
+				for ( i = 0; i < len; i ++ ) {
+
+					lookup[ i ] = code[ i ];
+
 				}
 
-				var parseDataArray = function(ele, compressed){
-					// Check the format
-					if (ele.attributes.format == "binary"){
-						if(compressed){
-							// Split the blob_header and compressed Data
-							if(ele["#text"].indexOf('==') != -1){
-								var data = ele["#text"].split("==");
-								//console.log(data);
-								if (data.length == 2){
-									var blob = data.shift();
-									var content = data.shift();
-									if(content == ""){
-										content = blob + "==";
-									}
-								}else if(data.length > 2){
-									var blob = data.shift();
-									var content = data.shift();
-									content = content + "==";
-								}else if (data.length < 2){
-									var content = data.shift();
-									content = content + "==";
+				for ( i = 0; i < len; ++ i ) {
+
+					revLookup[ code.charCodeAt( i ) ] = i;
+
+				}
+
+				revLookup[ '-'.charCodeAt( 0 ) ] = 62;
+				revLookup[ '_'.charCodeAt( 0 ) ] = 63;
+
+				var j, l, tmp, placeHolders, arr;
+				var len = b64.length;
+
+				if ( len % 4 > 0 ) {
+
+					throw new Error( 'Invalid string. Length must be a multiple of 4' );
+
+				}
+
+				placeHolders = b64[ len - 2 ] === '=' ? 2 : b64[ len - 1 ] === '=' ? 1 : 0;
+				arr = new Arr( len * 3 / 4 - placeHolders );
+				l = placeHolders > 0 ? len - 4 : len;
+
+				var L = 0;
+
+				for ( i = 0, j = 0; i < l; i += 4, j += 3 ) {
+
+					tmp = ( revLookup[ b64.charCodeAt( i ) ] << 18 ) | ( revLookup[ b64.charCodeAt( i + 1 ) ] << 12 ) | ( revLookup[ b64.charCodeAt( i + 2 ) ] << 6 ) | revLookup[ b64.charCodeAt( i + 3 ) ];
+					arr[ L ++ ] = ( tmp & 0xFF0000 ) >> 16;
+					arr[ L ++ ] = ( tmp & 0xFF00 ) >> 8;
+					arr[ L ++ ] = tmp & 0xFF;
+
+				}
+
+				if ( placeHolders === 2 ) {
+
+					tmp = ( revLookup[ b64.charCodeAt( i ) ] << 2 ) | ( revLookup[ b64.charCodeAt( i + 1 ) ] >> 4 );
+					arr[ L ++ ] = tmp & 0xFF;
+
+				} else if ( placeHolders === 1 ) {
+
+					tmp = ( revLookup[ b64.charCodeAt( i ) ] << 10 ) | ( revLookup[ b64.charCodeAt( i + 1 ) ] << 4 ) | ( revLookup[ b64.charCodeAt( i + 2 ) ] >> 2 );
+					arr[ L ++ ] = ( tmp >> 8 ) & 0xFF;
+					arr[ L ++ ] = tmp & 0xFF;
+
+				}
+
+				return arr;
+
+			}
+
+			function parseDataArray( ele, compressed ) {
+
+				// Check the format
+
+				if ( ele.attributes.format == 'binary' ) {
+
+					if ( compressed ) {
+
+						// Split the blob_header and compressed Data
+						if ( ele[ '#text' ].indexOf( '==' ) != - 1 ) {
+
+							var data = ele[ '#text' ].split( '==' );
+
+							// console.log( data );
+
+							if ( data.length == 2 ) {
+
+								var blob = data.shift();
+								var content = data.shift();
+
+								if ( content === '' ) {
+
+									content = blob + '==';
+
 								}
 
-								// Convert to bytearray
-								var arr = Base64toByteArray(content);
-								// decompress
-								var inflate = new Zlib.Inflate(arr,{resize:true,verify:true});
-								var content = inflate.decompress();
-							}else{
-								var content = Base64toByteArray(ele["#text"]);
+							} else if ( data.length > 2 ) {
+
+								var blob = data.shift();
+								var content = data.shift();
+								content = content + '==';
+
+							} else if ( data.length < 2 ) {
+
+								var content = data.shift();
+								content = content + '==';
+
 							}
-						}else{
-							var content = Base64toByteArray(ele["#text"]);
-						}
-						var content = content.buffer;
-					}else{
-						if(ele["#text"]){
-							var content = ele["#text"].replace(/\n/g," ").split(" ").filter(function(el,idx,arr){
-								if(el != ""){
-									return el;
-								}
-							});
-						}else{
-							var content = new Int32Array(0).buffer;
+
+							// Convert to bytearray
+							var arr = Base64toByteArray( content );
+
+							// decompress
+							var inflate = new Zlib.Inflate( arr, { resize: true, verify: true } );
+							var content = inflate.decompress();
+
+						} else {
+
+							var content = Base64toByteArray( ele[ '#text' ] );
+
 						}
+
+					} else {
+
+						var content = Base64toByteArray( ele[ '#text' ] );
+
 					}
-					delete ele["#text"];
-					// Get the content and optimize it
-					if(ele.attributes.type == 'Float32'){
-						var txt = new Float32Array(content);
-						if(ele.attributes.format == "binary"){
-							if(!compressed){
-								txt = txt.filter(function(el,idx,arr){if(idx != 0){return true;}});
-							}
+
+					var content = content.buffer;
+
+				} else {
+
+					if ( ele[ '#text' ] ) {
+
+						var content = ele[ '#text' ].replace( /\n/g, ' ' ).split( ' ' ).filter( function ( el, idx, arr ) {
+
+							if ( el !== '' ) return el;
+
+						} );
+
+					} else {
+
+						var content = new Int32Array( 0 ).buffer;
+
+					}
+
+				}
+
+				delete ele[ '#text' ];
+
+				// Get the content and optimize it
+
+				if ( ele.attributes.type == 'Float32' ) {
+
+					var txt = new Float32Array( content );
+
+					if ( ele.attributes.format == 'binary' ) {
+
+						if ( ! compressed ) {
+
+							txt = txt.filter( function( el, idx, arr ) {
+
+								if ( idx !== 0 ) return true;
+
+							} );
+
 						}
-					}else if (ele.attributes.type == 'Int64'){
-						var txt = new Int32Array(content);
-						if(ele.attributes.format == "binary"){
-							if(!compressed){
-								txt = txt.filter(function(el,idx,arr){if(idx!=0){return true;}});
-							}
-							txt = txt.filter(function(el,idx,arr){if(idx%2!=1){return true;}});
+
+					}
+
+				} else if ( ele.attributes.type === 'Int64' ) {
+
+					var txt = new Int32Array( content );
+
+					if ( ele.attributes.format == 'binary' ) {
+
+						if ( ! compressed ) {
+
+							txt = txt.filter( function ( el, idx, arr ) {
+
+								if ( idx !== 0 ) return true;
+
+							} );
+
 						}
+
+						txt = txt.filter( function ( el, idx, arr ) {
+
+							if ( idx % 2 !== 1 ) return true;
+
+						} );
+
 					}
 
-					//console.log(txt);
-					return txt;
 				}
 
+				// console.log( txt );
+
+				return txt;
+
+			}
 
 			// Main part
 			// Get Dom
 			var dom = null;
-			if (window.DOMParser) {
+
+			if ( window.DOMParser ) {
+
 				try {
-						dom = (new DOMParser()).parseFromString(stringFile, "text/xml");
-				}catch (e) {
+
+					dom = ( new DOMParser() ).parseFromString( stringFile, 'text/xml' );
+
+				} catch ( e ) {
+
 					dom = null;
+
 				}
-			}else if (window.ActiveXObject) {
+
+			} else if ( window.ActiveXObject ) {
+
 				try {
-						dom = new ActiveXObject('Microsoft.XMLDOM');
-						dom.async = false;
-						if (!dom.loadXML(xml)){
-							throw new Error(dom.parseError.reason + dom.parseError.srcText);
-						}
-				}catch (e) {
+
+					dom = new ActiveXObject( 'Microsoft.XMLDOM' );
+					dom.async = false;
+
+					if ( ! dom.loadXML( xml ) ) {
+
+						throw new Error( dom.parseError.reason + dom.parseError.srcText );
+
+					}
+
+				} catch ( e ) {
+
 					dom = null;
+
 				}
-			}else{
-				 throw new Error("Cannot parse xml string!");
+
+			} else {
+
+				throw new Error( 'Cannot parse xml string!' );
+
 			}
 
 			// Get the doc
 			var doc = dom.documentElement;
 			// Convert to json
-			var json = xmlToJson(doc);
+			var json = xmlToJson( doc );
 			var points = [];
 			var normals = [];
 			var indices = [];
 
-			if(json.PolyData){
+			if ( json.PolyData ) {
+
 				var piece = json.PolyData.Piece;
-				var compressed = json.attributes.hasOwnProperty("compressor");
+				var compressed = json.attributes.hasOwnProperty( 'compressor' );
+
 				// Can be optimized
 				// Loop through the sections
-				var sections = ["PointData", "Points", "Strips", "Polys"];// +["CellData", "Verts", "Lines"];
+				var sections = [ 'PointData', 'Points', 'Strips', 'Polys' ];// +['CellData', 'Verts', 'Lines'];
 				var sectionIndex = 0, numberOfSections = sections.length;
-				while (sectionIndex < numberOfSections){
-					var section = piece[sections[sectionIndex]];
+
+				while ( sectionIndex < numberOfSections ) {
+
+					var section = piece[ sections[ sectionIndex ] ];
+
 					// If it has a DataArray in it
-					if(section.DataArray){
+
+					if ( section.DataArray ) {
+
 						// Depending on the number of DataArrays
-						if(Object.prototype.toString.call( section.DataArray ) === '[object Array]'){
+
+						if ( Object.prototype.toString.call( section.DataArray ) === '[object Array]' ) {
+
 							var arr = section.DataArray;
-						}else{
-							var arr = [section.DataArray];
+
+						} else {
+
+							var arr = [ section.DataArray ];
+
 						}
 
 						var dataArrayIndex = 0, numberOfDataArrays = arr.length;
-						while(dataArrayIndex < numberOfDataArrays){
+
+						while ( dataArrayIndex < numberOfDataArrays ) {
+
 							// Parse the DataArray
-							arr[dataArrayIndex].text = parseDataArray(arr[dataArrayIndex],compressed);
-							dataArrayIndex++;
+							arr[ dataArrayIndex ].text = parseDataArray( arr[ dataArrayIndex ], compressed );
+							dataArrayIndex ++;
+
 						}
 
-						switch(sections[sectionIndex]){
+						switch ( sections[ sectionIndex ] ) {
 
 							// if iti is point data
-							case "PointData" :
-								var numberOfPoints = parseInt(piece.attributes.NumberOfPoints);
+							case 'PointData':
+
+								var numberOfPoints = parseInt( piece.attributes.NumberOfPoints );
 								var normalsName = section.attributes.Normals;
-								if (numberOfPoints > 0){
-									for(var i = 0, len = arr.length; i < len; i++){
-										if(normalsName == arr[i].attributes.Name){
-											var components = arr[i].attributes.NumberOfComponents;
-											normals = new Float32Array(numberOfPoints * components);
-											normals.set(arr[i].text,0);
+
+								if ( numberOfPoints > 0 ) {
+
+									for ( var i = 0, len = arr.length; i < len; i ++ ) {
+
+										if ( normalsName == arr[ i ].attributes.Name ) {
+
+											var components = arr[ i ].attributes.NumberOfComponents;
+											normals = new Float32Array( numberOfPoints * components );
+											normals.set( arr[ i ].text, 0 );
+
 										}
+
 									}
+
 								}
-								//console.log("Normals", normals);
+
+								// console.log('Normals', normals);
+
 								break;
 
 							// if it is points
-							case "Points" :
-								var numberOfPoints = parseInt(piece.attributes.NumberOfPoints);
-								if (numberOfPoints > 0){
+							case 'Points':
+
+								var numberOfPoints = parseInt( piece.attributes.NumberOfPoints );
+
+								if ( numberOfPoints > 0 ) {
+
 									var components = section.DataArray.attributes.NumberOfComponents;
 									points = new Float32Array( numberOfPoints * components );
-									points.set(section.DataArray.text, 0);
+									points.set( section.DataArray.text, 0 );
+
 								}
-								//console.log("Points", points);
+
+								// console.log('Points', points);
+
 								break;
 
 							// if it is strips
-							case "Strips" :
-								var numberOfStrips = parseInt(piece.attributes.NumberOfStrips);
-								if(numberOfStrips > 0){
-									var connectivity = new Int32Array(section.DataArray[0].text.length);
-									var offset = new Int32Array(section.DataArray[1].text.length);
-									connectivity.set(section.DataArray[0].text, 0);
-									offset.set(section.DataArray[1].text, 0);
+							case 'Strips':
+
+								var numberOfStrips = parseInt( piece.attributes.NumberOfStrips );
+
+								if ( numberOfStrips > 0 ) {
+
+									var connectivity = new Int32Array( section.DataArray[ 0 ].text.length );
+									var offset = new Int32Array( section.DataArray[ 1 ].text.length );
+									connectivity.set( section.DataArray[ 0 ].text, 0 );
+									offset.set( section.DataArray[ 1 ].text, 0 );
 
 									var size = numberOfStrips + connectivity.length;
 									indices = new Uint32Array( 3 * size - 9 * numberOfStrips );
+
 									var indicesIndex = 0;
-									for (var i = 0,len = numberOfStrips; i < len; i++) {
+
+									for ( var i = 0,len = numberOfStrips; i < len; i ++ ) {
+
 										var strip = [];
-										for (var s = 0,len1 = offset[i], len0 = 0; s < len1 - len0; s++) {
-											strip.push (connectivity[s]);
-											if(i > 0){
-												len0 = offset[i-1];
-											}
+
+										for ( var s = 0, len1 = offset[ i ], len0 = 0; s < len1 - len0; s ++ ) {
+
+											strip.push ( connectivity[ s ] );
+
+											if ( i > 0 ) len0 = offset[ i - 1 ];
+
 										}
-										for (var j = 0,len1 = offset[i], len0 = 0; j < len1 - len0 - 2; j++) {
+
+										for ( var j = 0, len1 = offset[ i ], len0 = 0; j < len1 - len0 - 2; j ++ ) {
+
 											if ( j % 2 ) {
-												indices[ indicesIndex++ ] = strip[ j ];
-												indices[ indicesIndex++ ] = strip[ j + 2 ];
-												indices[ indicesIndex++ ] = strip[ j + 1 ];
-											}else{
-												indices[ indicesIndex++ ] = strip[ j ];
-												indices[ indicesIndex++ ] = strip[ j + 1 ];
-												indices[ indicesIndex++ ] = strip[ j + 2 ];
-											}
-											if(i > 0){
-												len0 = offset[i-1];
+
+												indices[ indicesIndex ++ ] = strip[ j ];
+												indices[ indicesIndex ++ ] = strip[ j + 2 ];
+												indices[ indicesIndex ++ ] = strip[ j + 1 ];
+
+											} else {
+
+												indices[ indicesIndex ++ ] = strip[ j ];
+												indices[ indicesIndex ++ ] = strip[ j + 1 ];
+												indices[ indicesIndex ++ ] = strip[ j + 2 ];
+
 											}
+
+											if ( i > 0 ) len0 = offset[ i - 1 ];
+
 										}
+
 									}
+
 								}
-								//console.log("Strips", indices);
+
+								//console.log('Strips', indices);
+
 								break;
 
 							// if it is polys
-							case "Polys" :
-								var numberOfPolys = parseInt(piece.attributes.NumberOfPolys);
-								if(numberOfPolys > 0){
-									var connectivity = new Int32Array(section.DataArray[0].text.length);
-									var offset = new Int32Array(section.DataArray[1].text.length);
-									connectivity.set(section.DataArray[0].text, 0);
-									offset.set(section.DataArray[1].text, 0);
+							case 'Polys':
+
+								var numberOfPolys = parseInt( piece.attributes.NumberOfPolys );
+
+								if ( numberOfPolys > 0 ) {
+
+									var connectivity = new Int32Array( section.DataArray[ 0 ].text.length );
+									var offset = new Int32Array( section.DataArray[ 1 ].text.length );
+									connectivity.set( section.DataArray[ 0 ].text, 0 );
+									offset.set( section.DataArray[ 1 ].text, 0 );
 
 									var size = numberOfPolys + connectivity.length;
 									indices = new Uint32Array( 3 * size - 9 * numberOfPolys );
 									var indicesIndex = 0, connectivityIndex = 0;
 									var i = 0,len = numberOfPolys, len0 = 0;
-									while(i < len){
+
+									while ( i < len ) {
+
 										var poly = [];
-										var s = 0, len1 = offset[i];
-										while(s < len1 - len0){
-											poly.push (connectivity[connectivityIndex++]);
-											s++;
+										var s = 0, len1 = offset[ i ];
+
+										while ( s < len1 - len0 ) {
+
+											poly.push( connectivity[ connectivityIndex ++ ] );
+											s ++;
+
 										}
+
 										var j = 1;
-										while(j < len1 - len0 - 1){
-											indices[ indicesIndex++ ] = poly[ 0 ];
-											indices[ indicesIndex++ ] = poly[ j ];
-											indices[ indicesIndex++ ] = poly[ j + 1 ];
-											j++;
+
+										while ( j < len1 - len0 - 1 ) {
+
+											indices[ indicesIndex ++ ] = poly[ 0 ];
+											indices[ indicesIndex ++ ] = poly[ j ];
+											indices[ indicesIndex ++ ] = poly[ j + 1 ];
+											j ++;
+
 										}
-										i++;
-										len0 = offset[i-1];
+
+										i ++;
+										len0 = offset[ i - 1 ];
+
 									}
+
 								}
-								//console.log("Polys", indices);
+								//console.log('Polys', indices);
 								break;
 
-							default :
+							default:
 								break;
+
 						}
+
 					}
-					sectionIndex++;
+
+					sectionIndex ++;
+
 				}
 
 				var geometry = new THREE.BufferGeometry();
@@ -870,35 +1059,46 @@ THREE.VTKLoader.prototype = {
 					geometry.addAttribute( 'normal', new THREE.BufferAttribute( normals, 3 ) );
 
 				}
-				//console.log(json);
+
+				// console.log( json );
+
 				return geometry;
-			}else{
-				/* TODO for vtu,vti,and other xml formats*/
+
+			} else {
+
+				// TODO for vtu,vti,and other xml formats
+
 			}
+
 		}
 
-		function getStringFile(data){
+		function getStringFile( data ) {
+
 			var stringFile = '';
 			var charArray = new Uint8Array( data );
 			var i = 0;
 			var len = charArray.length;
-			while(len--){
-				stringFile += String.fromCharCode( charArray[ i++ ] );
+
+			while ( len -- ) {
+
+				stringFile += String.fromCharCode( charArray[ i ++ ] );
+
 			}
+
 			return stringFile;
+
 		}
 
 		// get the 5 first lines of the files to check if there is the key word binary
 		var meta = String.fromCharCode.apply( null, new Uint8Array( data, 0, 250 ) ).split( '\n' );
-		if (meta[0].indexOf("xml") != -1){
 
-			var stringFile = getStringFile(data);
-			return parseXML(stringFile);
+		if ( meta[ 0 ].indexOf( 'xml' ) !== - 1 ) {
+
+			return parseXML( getStringFile( data ) );
 
-		}else if ( meta[ 2 ].includes( 'ASCII' ) ) {
+		} else if ( meta[ 2 ].includes( 'ASCII' ) ) {
 
-			var stringFile = getStringFile(data);
-			return parseASCII( stringFile );
+			return parseASCII( getStringFile( data ) );
 
 		} else {
 

+ 34 - 27
examples/webgl_loader_vtk.html

@@ -28,7 +28,7 @@
 	<body>
 		<div id="info">
 		<a href="http://threejs.org" target="_blank">three.js</a> -
-		vtk formats loader test -
+		vtk formats loader test<br />
 		Legacy vtk model from <a href="http://www.cc.gatech.edu/projects/large_models/" target="_blank">The GeorgiaTech Lagre Geometric Model Archive</a>,
 		</div>
 
@@ -84,15 +84,16 @@
 				camera.add( dirLight );
 				camera.add( dirLight.target );
 
-				var material = new THREE.MeshLambertMaterial( { color:0xffffff, side: THREE.DoubleSide } );
+				var material = new THREE.MeshLambertMaterial( { color: 0xffffff, side: THREE.DoubleSide } );
 
 				var loader = new THREE.VTKLoader();
 				loader.load( "models/vtk/bunny.vtk", function ( geometry ) {
+
 					geometry.center();
 					geometry.computeVertexNormals();
 
 					var mesh = new THREE.Mesh( geometry, material );
-					mesh.position.set( -0.075,0.005,0 );
+					mesh.position.set( - 0.075, 0.005, 0 );
 					mesh.scale.multiplyScalar( 0.2 );
 					scene.add( mesh );
 
@@ -100,48 +101,54 @@
 
 				var loader1 = new THREE.VTKLoader();
 				loader1.load( 'models/vtk/cube_ascii.vtp', function ( geometry ) {
-	        geometry.computeVertexNormals();
-	        geometry.center();
 
-	        var material = new THREE.MeshLambertMaterial( { color: 0x00ff00, side: THREE.DoubleSide } );
-	        var mesh = new THREE.Mesh( geometry, material );
+					geometry.computeVertexNormals();
+					geometry.center();
+
+					var material = new THREE.MeshLambertMaterial( { color: 0x00ff00, side: THREE.DoubleSide } );
+					var mesh = new THREE.Mesh( geometry, material );
 
-	        mesh.position.set(-0.025,0,0);
-	        mesh.scale.multiplyScalar( 0.01 );
+					mesh.position.set( - 0.025, 0, 0 );
+					mesh.scale.multiplyScalar( 0.01 );
 
 
-	        scene.add( mesh );
-	      });
+					scene.add( mesh );
+
+				} );
 
 				var loader2 = new THREE.VTKLoader();
 				loader2.load( 'models/vtk/cube_binary.vtp', function ( geometry ) {
-	        geometry.computeVertexNormals();
-	        geometry.center();
 
-	        var material = new THREE.MeshLambertMaterial( { color: 0x0000ff, side: THREE.DoubleSide } );
-	        var mesh = new THREE.Mesh( geometry, material );
+					geometry.computeVertexNormals();
+					geometry.center();
+
+					var material = new THREE.MeshLambertMaterial( { color: 0x0000ff, side: THREE.DoubleSide } );
+					var mesh = new THREE.Mesh( geometry, material );
+
+					mesh.position.set( 0.025, 0, 0 );
+					mesh.scale.multiplyScalar( 0.01 );
 
-	        mesh.position.set(0.025,0,0);
-	        mesh.scale.multiplyScalar( 0.01 );
 
+					scene.add( mesh );
 
-	        scene.add( mesh );
-	      });
+				} );
 
 				var loader3 = new THREE.VTKLoader();
 				loader3.load( 'models/vtk/cube_no_compression.vtp', function ( geometry ) {
-	        geometry.computeVertexNormals();
-	        geometry.center();
 
-	        var material = new THREE.MeshLambertMaterial( { color: 0xff0000, side: THREE.DoubleSide } );
-	        var mesh = new THREE.Mesh( geometry, material );
+					geometry.computeVertexNormals();
+					geometry.center();
 
-	        mesh.position.set(0.075,0,0);
-	        mesh.scale.multiplyScalar( 0.01 );
+					var material = new THREE.MeshLambertMaterial( { color: 0xff0000, side: THREE.DoubleSide } );
+					var mesh = new THREE.Mesh( geometry, material );
+
+					mesh.position.set( 0.075, 0, 0 );
+					mesh.scale.multiplyScalar( 0.01 );
 
 
-	        scene.add( mesh );
-	      });
+					scene.add( mesh );
+
+				} );
 
 				// renderer