123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- /**
- * @author Sebastien Valette [email protected]
- */
- THREE.VTKLoader = function () {};
- THREE.VTKLoader.prototype = new THREE.Loader();
- THREE.VTKLoader.prototype.constructor = THREE.VTKLoader;
- THREE.VTKLoader.prototype.load = function ( url, callback ) {
- var that = this;
- var xhr = new XMLHttpRequest();
- xhr.onreadystatechange = function () {
- if ( xhr.readyState == 4 ) {
- if ( xhr.status == 200 || xhr.status == 0 ) {
- callback( that.parse( xhr.responseText ) );
- } else {
- console.error( 'THREE.VTKLoader: Couldn\'t load ' + url + ' (' + xhr.status + ')' );
- }
- }
- };
- xhr.open( "GET", url, true );
- xhr.send( null );
- };
- THREE.VTKLoader.prototype.parse = function ( data ) {
- var geometry = new THREE.Geometry();
- var lines = data.split("\n");
- function v( x, y, z ) {
- geometry.vertices.push( new THREE.Vector3( x, y, z ) );
- }
- function f3( a, b, c ) {
- geometry.faces.push( new THREE.Face3( a, b, c ) );
- }
- var lineIndex = 0;
- var line = lines[ 0 ].split( ' ' );
- var lineLength = line.length;
- var columnIndex = -1;
- function readNextString() {
- while ( 1 ) {
- var nextWord = line[ columnIndex ];
- columnIndex ++;
- if ( columnIndex == lineLength ) {
- lineIndex ++;
- columnIndex = 0;
- if ( lineIndex > lines.length ) {
- return '';
- }
- line = lines[ lineIndex ].split( ' ' );
- lineLength = line.length;
- }
- if ( nextWord != null ) {
- if ( nextWord.length > 0 ) {
- return nextWord;
- }
- }
- }
- }
- // read point data
- var found = false;
- while ( !found ) {
- var readString = readNextString();
- switch ( readString.toUpperCase() ) {
- case 'POINTS':
- found = true;
- break;
- case '':
- alert ( 'error while reading ' + url + ' : \n' );
- return;
- }
- }
- var newIndex;
- var new2old;
- var numberOfPoints = parseInt( readNextString() );
- if ( numberOfPoints > 5000000 ) {
- alert ( 'mesh is too big : ' + numberOfPoints + ' vertices' );
- return;
- }
- var coord = [ 0, 0, 0 ];
- var index2 = 0;
- var number;
- var coordIndex;
- for ( var j = 0; j != numberOfPoints; j ++ ) {
- for ( coordIndex = 0; coordIndex < 3; coordIndex ++ ) {
- do {
- number = parseFloat( line[ columnIndex ] );
- columnIndex ++;
- if ( columnIndex == lineLength ) {
- lineIndex ++;
- columnIndex = 0;
- if ( lineIndex > lines.length ) {
- alert ( 'error while reading ' + url + ' : \n' );
- return;
- }
- line = lines[ lineIndex ].split( ' ' );
- lineLength = line.length;
- }
- } while ( isNaN( number ) )
- coord[ coordIndex ] = number;
- }
- v( coord[ 0 ], coord[ 1 ], coord[ 2 ] );
- }
- found = false;
- while ( !found ) {
- var readString = readNextString();
- switch ( readString ) {
- case 'POLYGONS':
- found = true;
- break;
- case '':
- alert ( 'error while reading ' + url + ' : \n' );
- return;
- }
- }
- var numberOfPolygons = parseInt( readNextString() );
- var numberOfpolygonElements = parseInt( readNextString() );
- index2 = 0;
- var connectivity = [];
- for ( var p = 0; p != numberOfpolygonElements; p ++ ) {
- do {
- number = parseInt( line[ columnIndex ] );
- columnIndex ++;
- if ( columnIndex == lineLength ) {
- lineIndex ++;
- columnIndex = 0;
- if ( lineIndex > lines.length ) {
- alert ( 'error while reading ' + url + ' : \n' );
- return;
- }
- line = lines[ lineIndex ].split( ' ' );
- lineLength = line.length;
- }
- } while ( isNaN( number ) )
- connectivity[ index2 ] = number;
- index2 ++;
- if ( index2 == connectivity[ 0 ] + 1 ) {
- var triangle = [ 0, 0, 0 ];
- index2 = 0;
- var numberOfTrianglesInCell = connectivity[ 0 ] - 2;
- var vertex1 = triangle[ 0 ] = connectivity[ 1 ];
- for ( var i = 0; i < numberOfTrianglesInCell; i ++ ) {
- var vertex2 = connectivity[ i + 2 ];
- var vertex3 = triangle[ 2 ] = connectivity[ i + 3 ];
- f3( vertex1, vertex2, vertex3 );
- }
- }
- }
- geometry.computeCentroids();
- geometry.computeFaceNormals();
- geometry.computeVertexNormals();
- geometry.computeBoundingSphere();
- return geometry;
- }
|