فهرست منبع

Promoting VTKLoader2 into VTKLoader (VTKLoader2 loads more samples than VTKLoader and is easier to maintain).

Mr.doob 13 سال پیش
والد
کامیت
919b9044b9
3فایلهای تغییر یافته به همراه21 افزوده شده و 391 حذف شده
  1. 21 167
      examples/js/loaders/VTKLoader.js
  2. 0 105
      examples/js/loaders/VTKLoader2.js
  3. 0 119
      examples/webgl_loader_vtk2.html

+ 21 - 167
examples/js/loaders/VTKLoader.js

@@ -1,5 +1,5 @@
 /**
- * @author Sebastien Valette [email protected]
+ * @author mrdoob / http://mrdoob.com/
  */
 
 THREE.VTKLoader = function () {};
@@ -38,206 +38,60 @@ THREE.VTKLoader.prototype.load = function ( url, callback ) {
 THREE.VTKLoader.prototype.parse = function ( data ) {
 
 	var geometry = new THREE.Geometry();
-	var lines = data.split("\n");
 
-	function v( x, y, z ) {
+	function vertex( x, y, z ) {
 
 		geometry.vertices.push( new THREE.Vector3( x, y, z ) );
 
 	}
 
-	function f3( a, b, c ) {
+	function face3( a, b, c ) {
 
 		geometry.faces.push( new THREE.Face3( a, b, c ) );
 
 	}
 
-	var lineIndex = 0;
+	function face4( a, b, c, d ) {
 
-	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;
-
-				}
-
-			}
-
-		}
+		geometry.faces.push( new THREE.Face4( a, b, c, d ) );
 
 	}
 
-	// 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 pattern, result;
 
-	}
+	// float float float
 
-	var newIndex;
-	var new2old;
+	pattern = /([\d|\.|\+|\-|e]+)[ ]+([\d|\.|\+|\-|e]+)[ ]+([\d|\.|\+|\-|e]+)/g;
 
-	var numberOfPoints = parseInt( readNextString() );
+	while ( ( result = pattern.exec( data ) ) != null ) {
 
-	if ( numberOfPoints > 5000000 ) {
+		// ["1.0 2.0 3.0", "1.0", "2.0", "3.0"]
 
-		alert ( 'mesh is too big : ' + numberOfPoints + ' vertices' );
-		return;
+		vertex( parseFloat( result[ 1 ] ), parseFloat( result[ 2 ] ), parseFloat( result[ 3 ] ) );
 
 	}
 
-	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 ) {
+	// 3 int int int
 
-					lineIndex ++;
-					columnIndex = 0;
+	pattern = /3[ ]+([\d]+)[ ]+([\d]+)[ ]+([\d]+)/g;
 
-					if ( lineIndex > lines.length ) {
+	while ( ( result = pattern.exec( data ) ) != null ) {
 
-						alert ( 'error while reading ' + url + ' : \n' );
-						return;
-					}
+		// ["3 1 2 3", "1", "2", "3"]
 
-					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;
-
-		}
+		face3( parseInt( result[ 1 ] ), parseInt( result[ 2 ] ), parseInt( result[ 3 ] ) );
 
 	}
 
-	var numberOfPolygons = parseInt( readNextString() );
-	var numberOfpolygonElements = parseInt( readNextString() );
-
-	index2 = 0;
-	var connectivity = [];
-
-	for ( var p = 0; p != numberOfpolygonElements; p ++ ) {
-
-		do {
+	// 4 int int int int
 
-			number = parseInt( line[ columnIndex ] );
+	pattern = /4[ ]+([\d]+)[ ]+([\d]+)[ ]+([\d]+)[ ]+([\d]+)/g;
 
-			columnIndex ++;
+	while ( ( result = pattern.exec( data ) ) != null ) {
 
-			if ( columnIndex == lineLength ) {
+		// ["4 1 2 3 4", "1", "2", "3", "4"]
 
-				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 );
-
-			}
-
-		}
+		face4( parseInt( result[ 1 ] ), parseInt( result[ 2 ] ), parseInt( result[ 3 ] ), parseInt( result[ 4 ] ) );
 
 	}
 

+ 0 - 105
examples/js/loaders/VTKLoader2.js

@@ -1,105 +0,0 @@
-/**
- * @author mrdoob / http://mrdoob.com/
- */
-
-THREE.VTKLoader2 = function () {};
-
-THREE.VTKLoader2.prototype = new THREE.Loader();
-THREE.VTKLoader2.prototype.constructor = THREE.VTKLoader2;
-
-THREE.VTKLoader2.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.VTKLoader2.prototype.parse = function ( data ) {
-
-	var geometry = new THREE.Geometry();
-
-	function vertex( x, y, z ) {
-
-		geometry.vertices.push( new THREE.Vector3( x, y, z ) );
-
-	}
-
-	function face3( a, b, c ) {
-
-		geometry.faces.push( new THREE.Face3( a, b, c ) );
-
-	}
-
-	function face4( a, b, c, d ) {
-
-		geometry.faces.push( new THREE.Face4( a, b, c, d ) );
-
-	}
-
-	var pattern, result;
-
-	// float float float
-
-	pattern = /([\d|\.|\+|\-|e]+)[ ]+([\d|\.|\+|\-|e]+)[ ]+([\d|\.|\+|\-|e]+)/g;
-
-	while ( ( result = pattern.exec( data ) ) != null ) {
-
-		// ["1.0 2.0 3.0", "1.0", "2.0", "3.0"]
-
-		vertex( parseFloat( result[ 1 ] ), parseFloat( result[ 2 ] ), parseFloat( result[ 3 ] ) );
-
-	}
-
-	// 3 int int int
-
-	pattern = /3[ ]+([\d]+)[ ]+([\d]+)[ ]+([\d]+)/g;
-
-	while ( ( result = pattern.exec( data ) ) != null ) {
-
-		// ["3 1 2 3", "1", "2", "3"]
-
-		face3( parseInt( result[ 1 ] ), parseInt( result[ 2 ] ), parseInt( result[ 3 ] ) );
-
-	}
-
-	// 4 int int int int
-
-	pattern = /4[ ]+([\d]+)[ ]+([\d]+)[ ]+([\d]+)[ ]+([\d]+)/g;
-
-	while ( ( result = pattern.exec( data ) ) != null ) {
-
-		// ["4 1 2 3 4", "1", "2", "3", "4"]
-
-		face4( parseInt( result[ 1 ] ), parseInt( result[ 2 ] ), parseInt( result[ 3 ] ), parseInt( result[ 4 ] ) );
-
-	}
-
-	geometry.computeCentroids();
-	geometry.computeFaceNormals();
-	geometry.computeVertexNormals();
-	geometry.computeBoundingSphere();
-
-	return geometry;
-
-}

+ 0 - 119
examples/webgl_loader_vtk2.html

@@ -1,119 +0,0 @@
-<!doctype html>
-<html lang="en">
-	<head>
-		<title>three.js webgl - loaders - vtk loader (experimental)</title>
-		<meta charset="utf-8">
-		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
-		<style>
-			body {
-				font-family: Monospace;
-				background-color: #000;
-				color: #fff;
-				margin: 0px;
-				overflow: hidden;
-			}
-			#info {
-				color: #fff;
-				position: absolute;
-				top: 10px;
-				width: 100%;
-				text-align: center;
-				z-index: 100;
-				display:block;
-			}
-			#info a, .button { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
-		</style>
-	</head>
-
-	<body>
-		<div id="info">
-		<a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> -
-		vtk format loader test -
-		model from <a href="http://www.cc.gatech.edu/projects/large_models/" target="_blank">The GeorgiaTech Lagre Geometric Model Archive</a>,
-		</div>
-
-		<script src="../build/Three.js"></script>
-		<script src="js/loaders/VTKLoader2.js"></script>
-
-		<script src="js/Detector.js"></script>
-		<script src="js/Stats.js"></script>
-
-		<script>
-
-			if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
-
-			var container, stats;
-
-			var camera, controls, scene, renderer;
-
-			var cross;
-
-
-			// scene and camera
-
-			scene = new THREE.Scene();
-
-			camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.01, 1e10 );
-			camera.position.z = 0.2;
-
-
-			scene.add( camera );
-
-			controls = new THREE.TrackballControls( camera );
-
-			controls.rotateSpeed = 5.0;
-			controls.zoomSpeed = 5;
-			controls.panSpeed = 2;
-
-			controls.noZoom = false;
-			controls.noPan = false;
-
-			controls.staticMoving = true;
-			controls.dynamicDampingFactor = 0.3;
-
-			// light
-
-			var dirLight = new THREE.DirectionalLight( 0xffffff );
-			dirLight.position.set( 200, 200, 1000 ).normalize();
-			camera.add( dirLight );
-			camera.add( dirLight.target );
-
-			// renderer
-
-			renderer = new THREE.WebGLRenderer( { antialias: false } );
-			renderer.setClearColorHex( 0x000000, 1 );
-			renderer.setSize( window.innerWidth, window.innerHeight );
-
-			container = document.createElement( 'div' );
-			document.body.appendChild( container );
-			container.appendChild( renderer.domElement );
-
-			stats = new Stats();
-			stats.domElement.style.position = 'absolute';
-			stats.domElement.style.top = '0px';
-			container.appendChild( stats.domElement );
-
-
-			var material =  new THREE.MeshLambertMaterial( { color:0xffffff} );
-
-			var loader=new THREE.VTKLoader2();
-			loader.load ("models/vtk/bunny.vtk", function(geom){
-				var mesh = new THREE.Mesh(geom, material );
-				mesh.doubleSided=true;
-				mesh.position.setY(-0.09);
-				scene.add( mesh );
-				animate();
-				});
-
-			function animate() {
-
-				requestAnimationFrame( animate );
-				controls.update();
-				renderer.render( scene, camera );
-				stats.update();
-
-			}
-		</script>
-
-	</body>
-</html>