浏览代码

Merge pull request #11657 from Mugen87/pcd

PCDLoader: Clean up
Mr.doob 8 年之前
父节点
当前提交
8ab70b6ab0
共有 1 个文件被更改,包括 140 次插入91 次删除
  1. 140 91
      examples/js/loaders/PCDLoader.js

+ 140 - 91
examples/js/loaders/PCDLoader.js

@@ -43,7 +43,7 @@ THREE.PCDLoader.prototype = {
 
 
 		}
 		}
 
 
-		var text = "";
+		var text = '';
 		for ( var i = 0, il = data.byteLength; i < il; i ++ ) {
 		for ( var i = 0, il = data.byteLength; i < il; i ++ ) {
 
 
 			text += String.fromCharCode( charArray[ i ] );
 			text += String.fromCharCode( charArray[ i ] );
@@ -58,62 +58,92 @@ THREE.PCDLoader.prototype = {
 		var PCDheader = {};
 		var PCDheader = {};
 		var result1 = data.search( /[\r\n]DATA\s(\S*)\s/i );
 		var result1 = data.search( /[\r\n]DATA\s(\S*)\s/i );
 		var result2 = /[\r\n]DATA\s(\S*)\s/i.exec( data.substr( result1 - 1 ) );
 		var result2 = /[\r\n]DATA\s(\S*)\s/i.exec( data.substr( result1 - 1 ) );
+
+		var i, l;
+
 		PCDheader.data = result2[ 1 ];
 		PCDheader.data = result2[ 1 ];
 		PCDheader.headerLen = result2[ 0 ].length + result1;
 		PCDheader.headerLen = result2[ 0 ].length + result1;
 		PCDheader.str = data.substr( 0, PCDheader.headerLen );
 		PCDheader.str = data.substr( 0, PCDheader.headerLen );
-		// Remove comments
-		PCDheader.str = PCDheader.str.replace( /\#.*/gi, "" );
+
+		// remove comments
+
+		PCDheader.str = PCDheader.str.replace( /\#.*/gi, '' );
+
+		// parse
+
 		PCDheader.version = /VERSION (.*)/i.exec( PCDheader.str );
 		PCDheader.version = /VERSION (.*)/i.exec( PCDheader.str );
-		if ( PCDheader.version != null )
-			PCDheader.version = parseFloat( PCDheader.version[ 1 ] );
 		PCDheader.fields = /FIELDS (.*)/i.exec( PCDheader.str );
 		PCDheader.fields = /FIELDS (.*)/i.exec( PCDheader.str );
-		if ( PCDheader.fields != null )
-			PCDheader.fields = PCDheader.fields[ 1 ].split( " " );
 		PCDheader.size = /SIZE (.*)/i.exec( PCDheader.str );
 		PCDheader.size = /SIZE (.*)/i.exec( PCDheader.str );
-		if ( PCDheader.size != null )
-			PCDheader.size = PCDheader.size[ 1 ].split( " " ).map( function ( x ) {
-
-				return parseInt( x, 10 );
-
-			} );
 		PCDheader.type = /TYPE (.*)/i.exec( PCDheader.str );
 		PCDheader.type = /TYPE (.*)/i.exec( PCDheader.str );
-		if ( PCDheader.type != null )
-			PCDheader.type = PCDheader.type[ 1 ].split( " " );
 		PCDheader.count = /COUNT (.*)/i.exec( PCDheader.str );
 		PCDheader.count = /COUNT (.*)/i.exec( PCDheader.str );
-		if ( PCDheader.count != null )
-			PCDheader.count = PCDheader.count[ 1 ].split( " " ).map( function ( x ) {
+		PCDheader.width = /WIDTH (.*)/i.exec( PCDheader.str );
+		PCDheader.height = /HEIGHT (.*)/i.exec( PCDheader.str );
+		PCDheader.viewpoint = /VIEWPOINT (.*)/i.exec( PCDheader.str );
+		PCDheader.points = /POINTS (.*)/i.exec( PCDheader.str );
 
 
-				return parseInt( x, 10 );
+		// evaluate
 
 
-			} );
-		PCDheader.width = /WIDTH (.*)/i.exec( PCDheader.str );
-		if ( PCDheader.width != null )
+		if ( PCDheader.version !== null )
+			PCDheader.version = parseFloat( PCDheader.version[ 1 ] );
+
+		if ( PCDheader.fields !== null )
+			PCDheader.fields = PCDheader.fields[ 1 ].split( ' ' );
+
+		if ( PCDheader.type !== null )
+			PCDheader.type = PCDheader.type[ 1 ].split( ' ' );
+
+		if ( PCDheader.width !== null )
 			PCDheader.width = parseInt( PCDheader.width[ 1 ] );
 			PCDheader.width = parseInt( PCDheader.width[ 1 ] );
-		PCDheader.height = /HEIGHT (.*)/i.exec( PCDheader.str );
-		if ( PCDheader.height != null )
+
+		if ( PCDheader.height !== null )
 			PCDheader.height = parseInt( PCDheader.height[ 1 ] );
 			PCDheader.height = parseInt( PCDheader.height[ 1 ] );
-		PCDheader.viewpoint = /VIEWPOINT (.*)/i.exec( PCDheader.str );
-		if ( PCDheader.viewpoint != null )
+
+		if ( PCDheader.viewpoint !== null )
 			PCDheader.viewpoint = PCDheader.viewpoint[ 1 ];
 			PCDheader.viewpoint = PCDheader.viewpoint[ 1 ];
-		PCDheader.points = /POINTS (.*)/i.exec( PCDheader.str );
-		if ( PCDheader.points != null )
+
+		if ( PCDheader.points !== null )
 			PCDheader.points = parseInt( PCDheader.points[ 1 ], 10 );
 			PCDheader.points = parseInt( PCDheader.points[ 1 ], 10 );
-		if ( PCDheader.points == null )
+
+		if ( PCDheader.points === null )
 			PCDheader.points = PCDheader.width * PCDheader.height;
 			PCDheader.points = PCDheader.width * PCDheader.height;
 
 
-		if ( PCDheader.count == null ) {
+		if ( PCDheader.size !== null ) {
+
+			PCDheader.size = PCDheader.size[ 1 ].split( ' ' ).map( function ( x ) {
+
+				return parseInt( x, 10 );
+
+			} );
+
+		}
+
+		if ( PCDheader.count !== null ) {
+
+			PCDheader.count = PCDheader.count[ 1 ].split( ' ' ).map( function ( x ) {
+
+				return parseInt( x, 10 );
+
+			} );
+
+		} else {
 
 
 			PCDheader.count = [];
 			PCDheader.count = [];
-			for ( var i = 0; i < PCDheader.fields; i ++ )
+
+			for ( i = 0, l = PCDheader.fields.length; i < l; i ++ ) {
+
 				PCDheader.count.push( 1 );
 				PCDheader.count.push( 1 );
 
 
+			}
+
 		}
 		}
 
 
 		PCDheader.offset = {};
 		PCDheader.offset = {};
+
 		var sizeSum = 0;
 		var sizeSum = 0;
-		for ( var i = 0; i < PCDheader.fields.length; i ++ ) {
 
 
-			if ( PCDheader.data == "ascii" ) {
+		for ( i = 0, l = PCDheader.fields.length; i < l; i ++ ) {
+
+			if ( PCDheader.data === 'ascii' ) {
 
 
 				PCDheader.offset[ PCDheader.fields[ i ] ] = i;
 				PCDheader.offset[ PCDheader.fields[ i ] ] = i;
 
 
@@ -125,7 +155,9 @@ THREE.PCDLoader.prototype = {
 			}
 			}
 
 
 		}
 		}
-		// For binary only
+
+		// for binary only
+
 		PCDheader.rowSize = sizeSum;
 		PCDheader.rowSize = sizeSum;
 
 
 		return PCDheader;
 		return PCDheader;
@@ -134,53 +166,56 @@ THREE.PCDLoader.prototype = {
 
 
 	parse: function ( data, url ) {
 	parse: function ( data, url ) {
 
 
+		var dataView, offset;
+		var i, l;
+
 		var textData = this.binarryToStr( data );
 		var textData = this.binarryToStr( data );
 
 
-		// Parse the header
-		// Header is always ascii format
+		// parse header (always ascii format)
+
 		var PCDheader = this.parseHeader( textData );
 		var PCDheader = this.parseHeader( textData );
 
 
-		// Parse the data
-		var position = false;
-		if ( PCDheader.offset.x != undefined )
-			position = new Float32Array( PCDheader.points * 3 );
-		var color = false;
-		if ( PCDheader.offset.rgb != undefined )
-			color = new Float32Array( PCDheader.points * 3 );
-		var normal = false;
-		if ( PCDheader.offset.normal_x != undefined )
-			normal = new Float32Array( PCDheader.points * 3 );
+		// parse data
+
+		var position = [];
+		var normal = [];
+		var color = [];
+
+		// ascii
 
 
-		if ( PCDheader.data == "ascii" ) {
+		if ( PCDheader.data === 'ascii' ) {
 
 
-			var offset = PCDheader.offset;
+			offset = PCDheader.offset;
 			var pcdData = textData.substr( PCDheader.headerLen );
 			var pcdData = textData.substr( PCDheader.headerLen );
 			var lines = pcdData.split( '\n' );
 			var lines = pcdData.split( '\n' );
-			var i3 = 0;
-			for ( var i = 0; i < lines.length; i ++, i3 += 3 ) {
 
 
-				var line = lines[ i ].split( " " );
-				if ( offset.x != undefined ) {
+			for ( i = 0, l = lines.length; i < l; i ++ ) {
 
 
-					position[ i3 + 0 ] = parseFloat( line[ offset.x ] );
-					position[ i3 + 1 ] = parseFloat( line[ offset.y ] );
-					position[ i3 + 2 ] = parseFloat( line[ offset.z ] );
+				var line = lines[ i ].split( ' ' );
+
+				if ( offset.x !== undefined ) {
+
+					position.push( parseFloat( line[ offset.x ] ) );
+					position.push( parseFloat( line[ offset.y ] ) );
+					position.push( parseFloat( line[ offset.z ] ) );
 
 
 				}
 				}
-				if ( offset.rgb != undefined ) {
+
+				if ( offset.rgb !== undefined ) {
 
 
 					var c = new Float32Array( [ parseFloat( line[ offset.rgb ] ) ] );
 					var c = new Float32Array( [ parseFloat( line[ offset.rgb ] ) ] );
-					var dataview = new DataView( c.buffer, 0 );
-					color[ i3 + 0 ] = dataview.getUint8( 0 ) / 255.0;
-					color[ i3 + 1 ] = dataview.getUint8( 1 ) / 255.0;
-					color[ i3 + 2 ] = dataview.getUint8( 2 ) / 255.0;
+					dataview = new DataView( c.buffer, 0 );
+					color.push( dataview.getUint8( 0 ) / 255.0 );
+					color.push( dataview.getUint8( 1 ) / 255.0 );
+					color.push( dataview.getUint8( 2 ) / 255.0 );
 
 
 				}
 				}
-				if ( offset.normal_x != undefined ) {
 
 
-					normal[ i3 + 0 ] = parseFloat( line[ offset.normal_x ] );
-					normal[ i3 + 1 ] = parseFloat( line[ offset.normal_y ] );
-					normal[ i3 + 2 ] = parseFloat( line[ offset.normal_z ] );
+				if ( offset.normal_x !== undefined ) {
+
+					normal.push( parseFloat( line[ offset.normal_x ] ) );
+					normal.push( parseFloat( line[ offset.normal_y ] ) );
+					normal.push( parseFloat( line[ offset.normal_z ] ) );
 
 
 				}
 				}
 
 
@@ -188,40 +223,43 @@ THREE.PCDLoader.prototype = {
 
 
 		}
 		}
 
 
-		if ( PCDheader.data == "binary_compressed" ) {
+		// binary
+
+		if ( PCDheader.data === 'binary_compressed' ) {
 
 
 			console.error( 'THREE.PCDLoader: binary_compressed files are not supported' );
 			console.error( 'THREE.PCDLoader: binary_compressed files are not supported' );
 			return;
 			return;
 
 
 		}
 		}
 
 
-		if ( PCDheader.data == "binary" ) {
+		if ( PCDheader.data === 'binary' ) {
 
 
-			var row = 0;
-			var dataview = new DataView( data, PCDheader.headerLen );
-			var i = 0;
-			var offset = PCDheader.offset;
-			for ( var i3 = 0; i < PCDheader.points; i3 += 3, row += PCDheader.rowSize, i ++ ) {
+			dataview = new DataView( data, PCDheader.headerLen );
+			offset = PCDheader.offset;
 
 
-				if ( offset.x != undefined ) {
+			for ( i = 0, row = 0; i < PCDheader.points; i ++, row += PCDheader.rowSize ) {
 
 
-					position[ i3 + 0 ] = dataview.getFloat32( row + offset.x, this.littleEndian );
-					position[ i3 + 1 ] = dataview.getFloat32( row + offset.y, this.littleEndian );
-					position[ i3 + 2 ] = dataview.getFloat32( row + offset.z, this.littleEndian );
+				if ( offset.x !== undefined ) {
+
+					position.push( dataview.getFloat32( row + offset.x, this.littleEndian ) );
+					position.push( dataview.getFloat32( row + offset.y, this.littleEndian ) );
+					position.push( dataview.getFloat32( row + offset.z, this.littleEndian ) );
 
 
 				}
 				}
-				if ( offset.rgb != undefined ) {
 
 
-					color[ i3 + 0 ] = dataview.getUint8( row + offset.rgb + 0 ) / 255.0;
-					color[ i3 + 1 ] = dataview.getUint8( row + offset.rgb + 1 ) / 255.0;
-					color[ i3 + 2 ] = dataview.getUint8( row + offset.rgb + 2 ) / 255.0;
+				if ( offset.rgb !== undefined ) {
+
+					color.push( dataview.getUint8( row + offset.rgb + 0 ) / 255.0 );
+					color.push( dataview.getUint8( row + offset.rgb + 1 ) / 255.0 );
+					color.push( dataview.getUint8( row + offset.rgb + 2 ) / 255.0 );
 
 
 				}
 				}
-				if ( offset.normal_x != undefined ) {
 
 
-					normal[ i3 + 0 ] = dataview.getFloat32( row + offset.normal_x, this.littleEndian );
-					normal[ i3 + 1 ] = dataview.getFloat32( row + offset.normal_y, this.littleEndian );
-					normal[ i3 + 2 ] = dataview.getFloat32( row + offset.normal_z, this.littleEndian );
+				if ( offset.normal_x !== undefined ) {
+
+					normal.push( dataview.getFloat32( row + offset.normal_x, this.littleEndian ) );
+					normal.push( dataview.getFloat32( row + offset.normal_y, this.littleEndian ) );
+					normal.push( dataview.getFloat32( row + offset.normal_z, this.littleEndian ) );
 
 
 				}
 				}
 
 
@@ -229,26 +267,37 @@ THREE.PCDLoader.prototype = {
 
 
 		}
 		}
 
 
+		// build geometry
+
 		var geometry = new THREE.BufferGeometry();
 		var geometry = new THREE.BufferGeometry();
-		if ( position != false )
-			geometry.addAttribute( 'position', new THREE.BufferAttribute( position, 3 ) );
-		if ( color != false )
-			geometry.addAttribute( 'color', new THREE.BufferAttribute( color, 3 ) );
-		if ( normal != false )
-			geometry.addAttribute( 'normal', new THREE.BufferAttribute( normal, 3 ) );
+
+		if ( position.length > 0 ) geometry.addAttribute( 'position', new THREE.Float32BufferAttribute( position, 3 ) );
+		if ( normal.length > 0 ) geometry.addAttribute( 'normal', new THREE.Float32BufferAttribute( normal, 3 ) );
+		if ( color.length > 0 ) geometry.addAttribute( 'color', new THREE.Float32BufferAttribute( color, 3 ) );
 
 
 		geometry.computeBoundingSphere();
 		geometry.computeBoundingSphere();
 
 
-		var material = new THREE.PointsMaterial( { size: 0.005, vertexColors: ! ( color == false ) } );
-		if ( color == false )
+		// build material
+
+		var material = new THREE.PointsMaterial( { size: 0.005 } );
+
+		if ( color.length > 0 ) {
+
+			material.vertexColors = true;
+
+		} else {
+
 			material.color.setHex( Math.random() * 0xffffff );
 			material.color.setHex( Math.random() * 0xffffff );
 
 
+		}
+
+		// build mesh
+
 		var mesh = new THREE.Points( geometry, material );
 		var mesh = new THREE.Points( geometry, material );
 		var name = url.split( '' ).reverse().join( '' );
 		var name = url.split( '' ).reverse().join( '' );
 		name = /([^\/]*)/.exec( name );
 		name = /([^\/]*)/.exec( name );
 		name = name[ 1 ].split( '' ).reverse().join( '' );
 		name = name[ 1 ].split( '' ).reverse().join( '' );
 		mesh.name = name;
 		mesh.name = name;
-		mesh.PCDheader = PCDheader;
 
 
 		return mesh;
 		return mesh;