three.jsソースコード凝視(7)Math/Euler.js

17933 ワード

商域無疆(http://blog.csdn.net/omni360/)
本論文は「署名-非商業用途-一貫性を保つ」に従って共通プロトコルを作成する.
転載はこの文を保留してください。商域は疆がありません。 このブログは 機敏な開発と移動とモノ聯設備の研究:データの可視化、GOLANG、Html 5、WEBGL、THREE.JS、さもなくば、本ブログからの文章は転載または再転載を拒否します。ご協力ありがとうございます。
下記のコードはTHREE.JSソースファイルのMath/Qual ternion.jsファイルの凝視です.
多くの他の更新は以下の通りです.  https://github.com/omni360/three.js.sourcecode/blob/master/Three.js
// File:src/math/Euler.js

/**
 * @author mrdoob / http://mrdoob.com/
 * @author WestLangley / http://github.com/WestLangley
 * @author bhouston / http://exocortex.com
 */
/*
///Euler       .            .Euler         
///              .
///
///	    : var euler = new Euler(5,3,2,'XYZ')
///	         5 , y     ,     2 ,     'XYZ'.             x,y,z        .
///	NOTE:   x,y,z  3       ,          ,       (order)       .
///	NOTE:   (x,y,z,order)     ,       (x,y,z,order),        (0,0,0,'XYZ') Eular(   )  .
/// NOTE:   order(    )      'XYZ'      ['XYZ', 'YZX', 'ZXY', 'XZY', 'YXZ', 'ZYX' ]
///
///     ,                                 .    ,          , x      ,
///	y      ,z      ,                .
///                  ,"  ,  ,      ,5    ,    ,      ."
///
*/
///<summary>Euler</summary>
///<param name ="x" type="number">     x </param>
///<param name ="y" type="number">     y </param>
///<param name ="z" type="number">     z </param>
///<param name ="order" type="String">order(    )      'XYZ'      ['XYZ', 'YZX', 'ZXY', 'XZY', 'YXZ', 'ZYX' ]</param>

THREE.Euler = function ( x, y, z, order ) {

	this._x = x || 0;
	this._y = y || 0;
	this._z = z || 0;
	this._order = order || THREE.Euler.DefaultOrder;

};

THREE.Euler.RotationOrders = [ 'XYZ', 'YZX', 'ZXY', 'XZY', 'YXZ', 'ZYX' ];	//(    )      'XYZ'      ['XYZ', 'YZX', 'ZXY', 'XZY', 'YXZ', 'ZYX' ]

THREE.Euler.DefaultOrder = 'XYZ';	//        'XYZ'

/****************************************
****   Euler         .
****************************************/

THREE.Euler.prototype = {

	constructor: THREE.Euler, //   

	_x: 0, _y: 0, _z: 0, _order: THREE.Euler.DefaultOrder,	// (_x,_y,_z,_order)   x,y,z 0,           'XYZ';

	/*
	///get x       euler x .
	///NOTE: get x()      Euler.prototype.x(Euler.x),         ie              .
	*/
	///<summary>get x</summary>
	///<returns type="number">  euler _x </returns>
	get x () {

		return this._x;	//  euler _x 

	},

	/*
	///set x         Euler  x .       Euler .
	///NOTE: set x()      Euler.prototype.x=value(Euler.x = value),         ie              .
	*/
	///<summary>set x</summary>
	///<param name ="value" type="number"> x       </param>
	set x ( value ) {

		this._x = value;	//  Euler  _x 
		this.onChangeCallback(); //      .

	},

	/*
	///get y        euler y .
	///NOTE: get y()      Euler.prototype.y(Euler.y),         ie              .
	*/
	///<summary>get y</summary>
	///<returns type="number">  euler _y </returns>
	get y () {

		return this._y;	//  euler _y 

	},

	/*
	///set y         Euler  y .       Euler .
	///NOTE: set y()      Euler.prototype.y=value(Euler.y = value),         ie              .
	*/
	///<summary>set y</summary>
	///<param name ="value" type="number"> y       </param>
	set y ( value ) {

		this._y = value;	//  Euler  _y 
		this.onChangeCallback(); //      .

	},

	/*
	///get z       euler z .
	///NOTE: get z()      Euler.prototype.z(Euler.z),         ie              .
	*/
	///<summary>get z</summary>
	///<returns type="number">  euler _z </returns>
	get z () {

		return this._z;	//  euler _z 

	},

	/*
	///set z         Euler  z .       Euler .
	///NOTE: set z()      Euler.prototype.z=value(Euler.z = value),         ie              .
	*/
	///<summary>set z</summary>
	///<param name ="value" type="number"> z       </param>
	set z ( value ) {

		this._z = value;	//  Euler  _z 
		this.onChangeCallback(); //      .

	},

	/*
	///get order       euler order .
	///NOTE: get order()      Euler.prototype.order(Euler.order),         ie              .
	*/
	///<summary>get order</summary>
	///<returns type="number">  euler _z </returns>
	get order () {

		return this._order;	//  euler _order 

	},

	/*
	///set order         Euler  order .       Euler .
	///NOTE: set order()      Euler.prototype.order=value(Euler.order = value),         ie              .
	///	NOTE:   (x,y,z,order)     ,       (x,y,z,order),        (0,0,0,'XYZ') Eular(   )  .
	/// NOTE:   order(    )      'XYZ'      ['XYZ', 'YZX', 'ZXY', 'XZY', 'YXZ', 'ZYX' ]
	*/
	///<summary>set order</summary>
	///<param name ="value" type="number"> z       </param>
	set order ( value ) {

		this._order = value;	  	//  Euler  _order 
		this.onChangeCallback();	 //      .

	},

	///TODO:    setX()  .
	///TODO:    setY()  .
	///TODO:    setZ()  .
	///TODO:    setW()  .
	///TODO:    setComponent()  .
	///TODO:    getComponent()  .

	/*
	///set        Euler(   ) x,y,z,order .         Euler(   ).
	/// TODO:  set  ,  x,y,z,order        .
	*/
	///<summary>set</summary>
	///<param name ="x" type="number">     x </param>
	///<param name ="y" type="number">     y </param>
	///<param name ="z" type="number">     z </param>
	///<param name ="order" type="number">order(    )      'XYZ'      ['XYZ', 'YZX', 'ZXY', 'XZY', 'YXZ', 'ZYX' ]</param>
	///<returns type="Euler(   )">    Euler(   )</returns>
	set: function ( x, y, z, order ) {

		this._x = x;
		this._y = y;
		this._z = z;
		this._order = order || this._order;

		this.onChangeCallback();	//      .

		return this;	//    Euler(   )

	},

	/*
	///copy      Euler(   ) (x,y,z,order) .     Euler(   ).
	*/
	///<summary>copy</summary>
	///<param name ="euler(   )" type="Quaternion">Euler(   )</param>
	///<returns type="Euler(   )">    Euler(   )</returns>	
	copy: function ( euler ) {

		this._x = euler._x;
		this._y = euler._y;
		this._z = euler._z;
		this._order = euler._order;

		this.onChangeCallback();	//      .

		return this;	//    Euler(   )

	},

	/*
	///setFromRotationMatrix        m(    ),          .
	/// NOTE:m       ,      :http://en.wikipedia.org/wiki/Transformation_matrix
	///	
	///   :

		     z   30 .

					/----------------------------------------------------\
					|cos(heading) = 0.866	| sin(heading) = 0.5   | 0	 |
					|-----------------------|----------------------------|
		matrix =	|-sin(heading) = -0.5   |cos(heading) = 0.866  | 0   |
					|-----------------------|----------------------|-----|
					|     0                 |     0                | 1   |
					\----------------------------------------------------/

		angle = acos ( ( m00 + m11 + m22 - 1)/2)

		angle = acos ( ( 0.866 + 0.866 + 1 - 1)/2)

		angle = acos ( 0.866 )

		angle = 30 degrees

		x = (m21 - m12) = 0 - 0 =0
		y = (m02 - m20) = 0 - 0 =0
		z = (m10 - m01) = -0.5 - 0.5 = -1

	*/
	///<summary>setFromRotationMatrix</summary>
	///<param name ="m" type="Matrix3">3x3  (    )</param>
	///<param name ="order" type="String">order(    )      'XYZ'      ['XYZ', 'YZX', 'ZXY', 'XZY', 'YXZ', 'ZYX' ]</param>
	///<returns type="Euler(   )">    Euler(   )</returns>
	setFromRotationMatrix: function ( m, order ) {

		var clamp = THREE.Math.clamp;	//clamp           

		// assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
		//     m   3x3     .

		var te = m.elements;
		var m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ];
		var m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ];
		var m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ];

		order = order || this._order;

		if ( order === 'XYZ' ) {

			this._y = Math.asin( clamp( m13, - 1, 1 ) );

			if ( Math.abs( m13 ) < 0.99999 ) {

				this._x = Math.atan2( - m23, m33 );
				this._z = Math.atan2( - m12, m11 );

			} else {

				this._x = Math.atan2( m32, m22 );
				this._z = 0;

			}

		} else if ( order === 'YXZ' ) {

			this._x = Math.asin( - clamp( m23, - 1, 1 ) );

			if ( Math.abs( m23 ) < 0.99999 ) {

				this._y = Math.atan2( m13, m33 );
				this._z = Math.atan2( m21, m22 );

			} else {

				this._y = Math.atan2( - m31, m11 );
				this._z = 0;

			}

		} else if ( order === 'ZXY' ) {

			this._x = Math.asin( clamp( m32, - 1, 1 ) );

			if ( Math.abs( m32 ) < 0.99999 ) {

				this._y = Math.atan2( - m31, m33 );
				this._z = Math.atan2( - m12, m22 );

			} else {

				this._y = 0;
				this._z = Math.atan2( m21, m11 );

			}

		} else if ( order === 'ZYX' ) {

			this._y = Math.asin( - clamp( m31, - 1, 1 ) );

			if ( Math.abs( m31 ) < 0.99999 ) {

				this._x = Math.atan2( m32, m33 );
				this._z = Math.atan2( m21, m11 );

			} else {

				this._x = 0;
				this._z = Math.atan2( - m12, m22 );

			}

		} else if ( order === 'YZX' ) {

			this._z = Math.asin( clamp( m21, - 1, 1 ) );

			if ( Math.abs( m21 ) < 0.99999 ) {

				this._x = Math.atan2( - m23, m22 );
				this._y = Math.atan2( - m31, m11 );

			} else {

				this._x = 0;
				this._y = Math.atan2( m13, m33 );

			}

		} else if ( order === 'XZY' ) {

			this._z = Math.asin( - clamp( m12, - 1, 1 ) );

			if ( Math.abs( m12 ) < 0.99999 ) {

				this._x = Math.atan2( m32, m22 );
				this._y = Math.atan2( m13, m11 );

			} else {

				this._x = Math.atan2( - m23, m33 );
				this._y = 0;

			}

		} else {

			console.warn( 'THREE.Euler: .setFromRotationMatrix() given unsupported order: ' + order )	//                       

		}

		this._order = order;	//         

		this.onChangeCallback();	//      .

		return this;	//    Euler(   )

	},

	/*
	///setFromQuaternion         Euler(   ).
	/// NOTE:  (q)       ,    .normalize()      .
	/// NOTE:  update       .        true.
	*/
	///<summary>setFromQuaternion</summary>
	///<param name ="q" type="Quaternion">   </param>
	///<param name ="order" type="String">    </param>
	///<param name ="update" type="bool">update          bool    </param>
	///<returns type="Euler(   )">    Euler(   )</returns>
	setFromQuaternion: function ( q, order, update ) {

		var clamp = THREE.Math.clamp;	//clamp           

		// q is assumed to be normalized
		//   q       

		//                        .          .
		// http://www.mathworks.com/matlabcentral/fileexchange/20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/content/SpinCalc.m

		var sqx = q.x * q.x;
		var sqy = q.y * q.y;
		var sqz = q.z * q.z;
		var sqw = q.w * q.w;

		order = order || this._order;

		if ( order === 'XYZ' ) {

			this._x = Math.atan2( 2 * ( q.x * q.w - q.y * q.z ), ( sqw - sqx - sqy + sqz ) );
			this._y = Math.asin(  clamp( 2 * ( q.x * q.z + q.y * q.w ), - 1, 1 ) );
			this._z = Math.atan2( 2 * ( q.z * q.w - q.x * q.y ), ( sqw + sqx - sqy - sqz ) );

		} else if ( order ===  'YXZ' ) {

			this._x = Math.asin(  clamp( 2 * ( q.x * q.w - q.y * q.z ), - 1, 1 ) );
			this._y = Math.atan2( 2 * ( q.x * q.z + q.y * q.w ), ( sqw - sqx - sqy + sqz ) );
			this._z = Math.atan2( 2 * ( q.x * q.y + q.z * q.w ), ( sqw - sqx + sqy - sqz ) );

		} else if ( order === 'ZXY' ) {

			this._x = Math.asin(  clamp( 2 * ( q.x * q.w + q.y * q.z ), - 1, 1 ) );
			this._y = Math.atan2( 2 * ( q.y * q.w - q.z * q.x ), ( sqw - sqx - sqy + sqz ) );
			this._z = Math.atan2( 2 * ( q.z * q.w - q.x * q.y ), ( sqw - sqx + sqy - sqz ) );

		} else if ( order === 'ZYX' ) {

			this._x = Math.atan2( 2 * ( q.x * q.w + q.z * q.y ), ( sqw - sqx - sqy + sqz ) );
			this._y = Math.asin(  clamp( 2 * ( q.y * q.w - q.x * q.z ), - 1, 1 ) );
			this._z = Math.atan2( 2 * ( q.x * q.y + q.z * q.w ), ( sqw + sqx - sqy - sqz ) );

		} else if ( order === 'YZX' ) {

			this._x = Math.atan2( 2 * ( q.x * q.w - q.z * q.y ), ( sqw - sqx + sqy - sqz ) );
			this._y = Math.atan2( 2 * ( q.y * q.w - q.x * q.z ), ( sqw + sqx - sqy - sqz ) );
			this._z = Math.asin(  clamp( 2 * ( q.x * q.y + q.z * q.w ), - 1, 1 ) );

		} else if ( order === 'XZY' ) {

			this._x = Math.atan2( 2 * ( q.x * q.w + q.y * q.z ), ( sqw - sqx + sqy - sqz ) );
			this._y = Math.atan2( 2 * ( q.x * q.z + q.y * q.w ), ( sqw + sqx - sqy - sqz ) );
			this._z = Math.asin(  clamp( 2 * ( q.z * q.w - q.x * q.y ), - 1, 1 ) );

		} else {

			console.warn( 'THREE.Euler: .setFromQuaternion() given unsupported order: ' + order )	//                       

		}

		this._order = order;		//         

		if ( update !== false ) this.onChangeCallback();	//  update            .

		return this;	//    Euler(   )

	},

	/*
	///reorder         Euler(   )     .
	/// NOTE:  (q)       ,    .normalize()      .
	/// WARNING: reorder      Euler(   )       .
	*/
	///<summary>reorder</summary>
	///<param name ="order" type="String">    </param>
	///<returns type="Euler(   )">    Euler(   )</returns>
	reorder: function () {
		// WARNING: reorder      Euler(   )       .
		// WARNING: this discards revolution information -bhouston

		var q = new THREE.Quaternion();

		return function ( newOrder ) {

			q.setFromEuler( this );
			this.setFromQuaternion( q, newOrder );	//  setFromQuaternion()  ,        Euler(   )

		};


	}(),

	/*equals  
	///equals          ===,   Euler(   )   euler  (x,y,z,order)     ,  bool  .
	*/
	///<summary>equals</summary>
	///<param name ="v" type="Euler(   )">Euler(   )(x,y,z,order)</param>
	///<returns type="bool">  true or false</returns>	
	equals: function ( euler ) {

		return ( euler._x === this._x ) && ( euler._y === this._y ) && ( euler._z === this._z ) && ( euler._order === this._order );

	},

	/*fromArray  
	///fromArray     Euler(   )(x,y,z,order)         Euler(   )  
	/// NOTE:  order       .              .
	*/
	///<summary>fromArray</summary>
	///<param name ="array" type="Array">Euler(   )(x,y,z,order)   array[x,y,z,order]</param>
	///<returns type="Euler(   )">    Euler(   )</returns>	
	fromArray: function ( array ) {

		this._x = array[ 0 ];
		this._y = array[ 1 ];
		this._z = array[ 2 ];
		if ( array[ 3 ] !== undefined ) this._order = array[ 3 ];

		this.onChangeCallback();	//      .

		return this;	//    Euler(   )

	},

	/*toArray  
	///toArray     Euler(   )          array[5,3,2,'XYZ'].        .
	*/
	///<summary>toArray</summary>
	///<returns type="Array">Euler(   )(_x,_y,_z,_order)   array[x,y,z,order]</returns>	
	toArray: function () {

		return [ this._x, this._y, this._z, this._order ];	//      x,y,z,order     ,

	},

	/*onChange  
	///onChange             callback     onChangeCallback()  .
	*/
	///<summary>onChange</summary>
	///<param name ="callback" type="function">    </param>
	///<returns type="Euler(   )">Euler(   )</returns>	
	onChange: function ( callback ) {

		this.onChangeCallback = callback;

		return this;

	},

	/*onChangeCallback  
	///onChangeCallback      Euler(   )  .
	///NOTE:onChangeCallback()               ,              onChangeCallback()  ,           .
	*/
	///<summary>onChangeCallback</summary>
	onChangeCallback: function () {},

	/*clone  
	///clone      Euler(   )  .
	*/
	///<summary>clone</summary>
	///<returns type="Euler(   )">  Euler(   )  </returns>	
	clone: function () {

		return new THREE.Euler( this._x, this._y, this._z, this._order );

	}

};
商域無疆(http://blog.csdn.net/omni360/)
本論文は「署名-非商業用途-一貫性を保つ」に従って共通プロトコルを作成する.
転載はこの文を保留してください。商域は疆がありません。 このブログは 機敏な開発と移動とモノ聯設備の研究:データの可視化、GOLANG、Html 5、WEBGL、THREE.JS、さもなくば、本ブログからの文章は転載または再転載を拒否します。ご協力ありがとうございます。
下記のコードはTHREE.JSソースファイルのMath/Qual ternion.jsファイルの凝視です.
多くの他の更新は以下の通りです.  https://github.com/omni360/three.js.sourcecode/blob/master/Three.js