1. 程式人生 > >three.js 原始碼註釋(十四)Math/Sphere.js

three.js 原始碼註釋(十四)Math/Sphere.js


俺也是剛開始學,好多地兒肯定不對還請見諒.

以下程式碼是THREE.JS 原始碼檔案中Math/Sphere.js檔案的註釋.


// File:src/math/Sphere.js

/**
 * @author bhouston / http://exocortex.com
 * @author mrdoob / http://mrdoob.com/
 */
/*
///Sphere物件的建構函式.用來在三維空間內建立一個球體物件.Sphere物件的功能函式採用
///定義構造的函式原型物件來實現.
///
///	用法: var center = new Vector3(0,0,0),radius = 5; var sphere = new Sphere(center,radius);
///建立一個圓心是0,0,0半徑是5的球體.
*/
///<summary>Sphere</summary>
///<param name ="center" type="Vector3">中心點座標值</param>
///<param name ="radius" type="Number">Number球體半徑</param>
THREE.Sphere = function ( center, radius ) {

	this.center = ( center !== undefined ) ? center : new THREE.Vector3();	//賦值或者初始化center
	this.radius = ( radius !== undefined ) ? radius : 0;	//賦值或者初始化radius

};

/****************************************
****下面是Sphere物件提供的功能函式.
****************************************/
THREE.Sphere.prototype = {

	constructor: THREE.Sphere,	//構造器,返回對建立此物件的Sphere函式的引用

	/*
	///set方法用來從新設定球體的起始點,結束點,center,radius座標值.並返回新半徑,座標值的球體.
	*/
	///<summary>set</summary>
	///<param name ="center" type="Vector3">中心點座標值</param>
	///<param name ="radius" type="Number">Number球體半徑</param>
	///<returns type="Sphere">返回新半徑,座標值的球體</returns>
	set: function ( center, radius ) {

		this.center.copy( center );
		this.radius = radius;

		return this;		//返回新半徑,座標值的球體
	},

	/*
	///setFromPoints方法通過獲得Vector3物件組成的points陣列中的到圓心距離最大的值重新設定球體的半徑,通過可選引數optionalCenter用來設定球體的圓心.並返回新半徑,座標值的球體.
	/// NOTE:注意如果給setFromPoints()方法設定了optionalCenter引數,points陣列中數值到圓心的距離將會改變.
	*/
	///<summary>setFromPoints</summary>
	///<param name ="points" type="Vector3Array">Vector3物件組成的points陣列</param>
	///<param name ="optionalCenter" type="Vector3">可選引數,接收返回結果,球體的中心點</param>
	///<returns type="Sphere">返回新半徑,座標值的球體</returns>
	setFromPoints: function () {

		var box = new THREE.Box3();

		return function ( points, optionalCenter )  {

			var center = this.center;

			if ( optionalCenter !== undefined ) {

				center.copy( optionalCenter );

			} else {

				box.setFromPoints( points ).center( center );

			}

			var maxRadiusSq = 0;

			for ( var i = 0, il = points.length; i < il; i ++ ) {

				maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( points[ i ] ) );	//求points陣列中到圓心的最大值並賦值給maxRadiusSq

			}

			this.radius = Math.sqrt( maxRadiusSq );	

			return this;	//返回新半徑,座標值的球體

 		};

	}(),

	/*
	///copy方法用來複制球體的圓心,半徑,center,radius值.返回新半徑,座標值的球體
	*/
	///<summary>copy</summary>
	///<param name ="sphere" type="Sphere">球體</param>
	///<returns type="Sphere">返回新半徑,座標值的球體</returns>
	copy: function ( sphere ) {

		this.center.copy( sphere.center );
		this.radius = sphere.radius;

		return this;	//返回新半徑,座標值的球體

	},

	/*
	///empty方法用來判斷球體的半徑是否小於等於0,用來判斷空間中半徑是0,或者小於0的球體.
	*/
	///<summary>empty</summary>
	///<returns type="Boolean">返回true 或者 false</returns>
	empty: function () {

		return ( this.radius <= 0 );	//返回true 或者 false

	},

	/*
	///containsPoint方法用來獲得引數point(一個Vector3的三維點座標)是否在當前球體內.
	*/
	///<summary>containsPoint</summary>
	///<param name ="point" type="Vector3">一個Vector3的三維點座標</param>
	///<returns type="Boolean">返回true 或者 false</returns>
	containsPoint: function ( point ) {

		return ( point.distanceToSquared( this.center ) <= ( this.radius * this.radius ) );	//返回true 或者 false

	},

	/*
	///distanceToPoint方法用來獲得三維空間內一點到Sphere球體物件表面的最小長度.
	*/
	///<summary>distanceToPoint</summary>
	///<param name ="point" type="Vector3">一個三維空間內的Vector3的三維點座標</param>
	///<returns type="Number">返回三維空間內一點到Sphere球體物件表面的最小長度.</returns>
	distanceToPoint: function ( point ) {

		return ( point.distanceTo( this.center ) - this.radius );	//返回三維空間內一點到Sphere球體物件表面的最小長度.

	},

	/*
	///intersectsSphere方法獲取當前球體是否與引數sphere球體物件相交,返回true 或者 false
	*/
	///<summary>intersectsSphere</summary>
	///<param name ="sphere" type="Sphere">一個Sphere的球體</param>
	///<returns type="Boolean">返回true 或者 false</returns>
	intersectsSphere: function ( sphere ) {

		var radiusSum = this.radius + sphere.radius;

		return sphere.center.distanceToSquared( this.center ) <= ( radiusSum * radiusSum );	//返回true 或者 false

	},

	/*
	///clampPoint方法用來通過引數point收縮球體.如果point在球體外,強制將point設定到球體表面,如果point在球體內,重新設定球體半徑為point到當前球體半徑的距離.
	*/
	///<summary>clampPoint</summary>
	///<param name ="point" type="Vector3">一個Vector3的三維點座標</param>
	///<param name ="optionalTarget" type="Vector3">可選引數,接收返回結果,返回剪裁過的邊界點</param>
	///<returns type="Vector3">返回剪裁過的邊界點.</returns>
	clampPoint: function ( point, optionalTarget ) {

		var deltaLengthSq = this.center.distanceToSquared( point );

		var result = optionalTarget || new THREE.Vector3();
		result.copy( point );

		if ( deltaLengthSq > ( this.radius * this.radius ) ) {

			result.sub( this.center ).normalize();
			result.multiplyScalar( this.radius ).add( this.center );

		}

		return result;	// 返回剪裁過的邊界點

	},

	/*
	///getBoundingBox方法返回當前球體的Box3立方體邊界(這裡應該外切於球體的一個立方體)
	/// 與Box3類中的getBoundingSphere()方法對應.
	*/
	///<summary>getBoundingBox</summary>
	///<param name ="optionalTarget" type="THREE.Box3()">可選引數,THREE.Box3()立方體物件,用來接收返回值</param>
	///<returns type="THREE.Sphere()">返回當前球體的Box3立方體邊界(這裡應該外切於球體的一個立方體)</returns>
	getBoundingBox: function ( optionalTarget ) {

		var box = optionalTarget || new THREE.Box3();

		box.set( this.center, this.center );
		box.expandByScalar( this.radius );

		return box;			//返回當前球體的Box3立方體邊界(這裡應該外切於球體的一個立方體)

	},

	/*
	///applyMatrix4方法通過傳遞matrix(旋轉,縮放,移動等變換矩陣)對當前Sphere球體物件的圓心和半徑,應用變換.
	*/
	///<summary>applyMatrix4</summary>
	///<param name ="matrix" type="Matrix4">(旋轉,縮放,移動等變換矩陣</param>
	///<returns type="Boolean">返回變換後的球體.</returns>
	applyMatrix4: function ( matrix ) {

		this.center.applyMatrix4( matrix );
		this.radius = this.radius * matrix.getMaxScaleOnAxis();

		return this;		//返回變換後的球體.

	},

	/*
	///translate方法用來通過引數offset,移動當前球體的位置.
	*/
	///<summary>translate</summary>
	///<param name ="offset" type="Vector3">偏移量</param>
	///<returns type="Boolean">返回新半徑,座標值的球體</returns>
	translate: function ( offset ) {

		this.center.add( offset );

		return this;	//返回新半徑,座標值的球體

	},

	/*
	///equals方法用來獲得引數sphere(一個Sphere的球體)是否與當前球體完全相等,即圓心和半徑相等.
	*/
	///<summary>equals</summary>
	///<param name ="sphere" type="Sphere">一個Sphere的球體</param>
	///<returns type="Boolean">返回true 或者 false</returns>
	equals: function ( sphere ) {

		return sphere.center.equals( this.center ) && ( sphere.radius === this.radius );	//返回true 或者 false

	},

	/*clone方法
	///clone方法克隆一個球體物件.
	*/
	///<summary>clone</summary>
	///<returns type="Sphere">返回球體物件</returns>	
	clone: function () {

		return new THREE.Sphere().copy( this );		//返回球體物件

	}

};



以下程式碼是THREE.JS 原始碼檔案中Math/Line3.js檔案的註釋.