Commercial Territory (http://blog.csdn.net/omni360/)
This article follows "Attribution-non-commercial use-consistent" authoring public agreement
Reprint Please keep this sentence: Business Domain-this blog focuses on Agile development and mobile and IoT device research: Data visualization, Golang, HTML5, WEBGL, three. JS, Otherwise, from this blog article refused to reprint or reprint, thank you for your cooperation.
The following code is the gaze of the Math/quaternion.js file in the Three.js source code file.
Many other updates in: 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 the constructor of the Bhouston/http://exocortex.com *//*///euler object. The object that is used to create a Euler angle. The function function of the Euler object is implemented using the function prototype object defined by///.////// How to use: var Euler = new Euler (5,3,2, ' xyz ')////Create a rotation around an axis 5 degrees, rotate a degree around the y axis, rotate 2 degrees around an axis, and rotate in the order of ' XYZ '. With the rotation order ability to determine how many degrees each x, Y, Z axis rotates.// Note: The number of x, Y, z represents the rotation angle of the 3 axes, which axis rotates how many degrees, which need to be followed by the number of parameters (order) rotation order to determine.///note: The parameter (x,y,z,order) is an optional parameter, assuming that no parameters are specified (X,y,z,order), A eular (Euler angle) object with a coordinate of (0,0,0, ' XYZ ') is created.///NOTE: The default order (rotation order) is the ' XYZ ' value range is [' xyz ', ' yzx ', ' zxy ', ' XZY ', ' yxz ', ' ZYX ']// In layman's terms, Euler's angle is a method used to describe the direction of an object in three-dimensional space. For example, an object in three-dimensional space, the number of degrees around the x-axis, how many degrees of///y axis, the z-axis of how many degrees, to describe the object in the three-dimensional space in the direction.// A little bit similar to the Flying Tigers in Hong Kong movies, "Flying Eagle, flying Eagle, in front of you, 5 o'clock direction, find the target, ready to gather goals." *////<summary>euler</summary>///<param name = "x" type= "number" > rotate x degrees around an axis </param>///< param name = "y" type= "number" > rotates y degree around an axis </param>///<param name = "Z" type= "number" > Rotates z degree around an axis </param>/ <param name = "Order" type= "StringThe default order of >order (rotation order) is the ' XYZ ' value range is [' 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 '];//(rotation order) The default order is ' XYZ ' value range is [' xyz ', ' yzx ', ' zxy ', ' XZY ', ' Yxz ', ' ZYX ']three. Euler.defaultorder = ' xyz ';//The default rotation order is ' XYZ '/******************************************** The following are the function functions provided by the Euler object. ******* /three. Euler.prototype = {Constructor:three. Euler,//constructor _x:0, _y:0, _z:0, _order:three. euler.defaultorder,//(_x,_y,_z,_order) initializes x, Y, Z to 0, the rotation order is the default rotation order ' XYZ ', and the/*///get x method is used to obtain the Euler angle × value.///note:get x () is used by euler.prototype.x (euler.x), which can be used on browsers other than IE browsers. *////<summary>get x</summary>///< Returns type= "Number" > return euler angle _x value </returns>get x () {return this._x;//return Euler angle _x value},/*///set x method is used to re-set the X-value of the Euler angle. and returns the Euler angle of the new value.///note:set x () is used EULER.PROtotype.x=value (euler.x = value), which can be used on browsers other than IE browsers. *////<summary>set X</summary>///<param Name = "value" type= "number" > Angle value rotated around X axis </param>set x (value) {this._x = value;//set Euler angle _ X-value This.onchangecallback (); Call the callback function. The},/*///get y method is used to derive the Euler angle y value from the.///note:get y () is used Euler.prototype.y (EULER.Y), which is used on browsers other than IE browsers. * <summary>get y</summary>///<returns type= "number" > return euler angle _y value </returns>get y () {return this._y;//returns the Euler angle _y value},/*///set y method is used to set the Y-value of the Euler angle from the new. and returns the Euler angle of the value.///note:set y () is used by Euler.prototype.y=value ( Euler.y = value), which can be used on browsers other than IE browsers. *////<summary>set y</summary>///<param name = "Value" type= "Number" > the angular value of rotation around the y-axis </param>set y (value) {this._y = value;//sets the _y value of the Euler angle This.onchangecallback ();//Call the callback function.}, The/*///get Z method is used to obtain the Euler angle Z-value.///note:get Z () is used by Euler.prototype.z (EULER.Z), which can be used on browsers other than IE browsers .*////< Summary>get z</summary>///<returns type= "number" > return euler angle _z value</returns>get Z () {return this._z;//returns the Euler angle _z value},/*///set Z method is used to re-set the Z-value of the Euler angle. and returns the Euler angle of the new value.///note:set Z () is used by euler.prototype.z=value (Euler.z = value), which is used on browsers other than IE browsers. *////<summary>set z</summary >///<param name = "value" type= "number" > angle value rotated around the z axis </param>set Z (value) {this._z = value;//set Euler angle _ Z-value this.onchangecallback (); Call the callback function. The},/*///get order method is used to obtain the Euler angle order value.///note:get order () is used by Euler.prototype.order (Euler.order), This method can be used on browsers other than IE browsers. *////<summary>get order</summary>///<returns type= "number" > Return to Euler Corner _ Z-value </returns>get order () {return this._order;//return Euler angle _order value},/*///set order method is used to re-set the order value of the Euler angle. and returns the Euler angle of the new value.///note:set order () is used by euler.prototype.order=value (Euler.order = value), This method of use is available on browsers other than IE browsers.///note: The parameter (x,y,z,order) is an optional parameter, assuming that no parameters are specified (X,y,z,order), a eular (Euler angle) object with coordinates (0,0,0, ' XYZ ') is created .///NOTE: The default order of the parameters order (rotation order) is the ' XYZ ' value range is [' xyz ', ' yzx ', ' zxy ', ' XZY ', ' yxz ', ' ZYX ']*////<summary>set Order</summary>///<param name = "value" type= "number" > angle value rotated around the z-axis </param>set order (value) {This._ order = value; Sets the _order value of the Euler angle This.onchangecallback (); Call the callback function.},///todo: The Setx () method is missing here.///todo: The Sety () method is missing here.///todo: The Setz () method is missing here.///todo: The SETW () method is missing.///todo: The SetComponent () method is missing here.///todo: The Getcomponent () method is missing here. The/*///set method is used to set the X,y,z,order value of Euler (Euler's angle) from the new. and returns the Euler (Euler angle) of the newly-coordinate value. TODO: Change the Set method, compatible with X,y,z,order parameter omitting support polymorphism. *////<summary>set</summary>///<param name = "x" type= "number" > Rotate x degrees around an axis </param>///<param name = "Y" type= "number" > rotates the y degree around an axis </param>///<param name = "Z" type= " Number "> rotate the z degree around an axis </param>///<param name =" Order "type=" # ">order (rotation order) The default order is ' XYZ ' value range is [' xyz ', ' Yzx ', ' zxy ', ' XZY ', ' yxz ', ' ZYX ']</param>///<returns type= ' Euler (Euler angle) ' > Return to New Euler (Euler angle) </returns> Set:function (x, y, Z, order) {this._x = X;this._y = Y;this._z = Z;this._order = Order | | this._order;this.onchangecall Back ();//Call the callback function. Return this;//returns a newEuler (Euler angle)},/*///copy method is used to copy Euler (Euler angle) (X,y,z,order) values. and return the new Euler (Euler angle). *////<summary>copy</summary>/ <param name = "Euler (Euler angle)" type= "quaternion" >euler (Euler angle) </param>///<returns type= "Euler (Euler angle)" > Returns the new Euler (Euler angle) </returns>copy:function (Euler) {this._x = Euler._x;this._y = Euler._y;this._z = Euler._z;this._or Der = Euler._order;this.onchangecallback ();//Call the callback function. Return this;//returns a new Euler (Euler angle)},/*/// The Setfromrotationmatrix method uses a parameter m (rotation matrix) to achieve the purpose of rotational transformation.///NOTE:M is a rotation matrix, many other formulas: http://en.wikipedia.org/wiki/ transformation_matrix//////Example: This example is a z-axis rotated 30 degrees./----------------------------------------------------|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 = Degreesx = (M21-M12) = 0-0 =0y = (M02-M20) = 0-0 =0z = (m10-m01) = -0.5-0.5 = -1*////<summary>setfromrotationmatrix</summary>///<param name = "M" T Ype= "Matrix3" >3x3 Matrix (rotation matrix) </param>///<param name = "Order" type= "String" >order (rotation order) The default order is ' XYZ ' value range is [ ' XYZ ', ' yzx ', ' zxy ', ' XZY ', ' yxz ', ' ZYX ']</param>///<returns type= ' Euler (Euler angle) ' > Return to New Euler (Euler angle) </ Returns>setfromrotationmatrix:function (M, order) {var clamp = three. Math.clamp;//clamp is used to set the value range of values//assumes the upper 3x3 of M is a pure rotation matrix (i.e, unscaled)//Ensure that the reference m is a 3x3 rotation matrix. VA R 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[,];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 = Ma Th.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 = Ma Th.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 '//failure error in setting Euler angle object in accordance with the above-mentioned rotation order}this._order = order;// Set the rotation order again this.onchangecallback ();//Call the callback function. Return this;//returns a new Euler (Euler angle)},/*///setfromquaternion method to set Euler (with a four-dollar number) Euler's angle).///NOTE: The parameter (q) must be a unit vector by calling. Normalize () to get the unit vector.//NOTE: The parameter update is an optional number of parameters. Assuming that you don't set the default true.*////<summary> Setfromquaternion</summary>///<param name = "Q" type= "quaternion" > four $ </param>///<param name = " Order "type=" String "> Rotation order </param>///<param name =" Update "type=" bool ">update indicates whether to invoke the callback function bool type tag Value </ Param>///<returns type= "Euler (Euler angle)" > Return to the new Euler (Euler angle) </returns>setfromquaternion:function (q, Order, Update) {var clamp = three. Math.clamp;//clamp used to set the value range of the values//q is assumed to be normalized//make sure Q is a unit vector//The following URLs are a specific introduction to converting four to Euler angles. There are a lot of math algorithms in it.//HTTP ://www.mathworks.com/matlabcentral/fileexchange/20696- Function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/content/spincalc.mvar 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)); _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), (s QW + 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-s QX + 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 = Ma Th.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 + sq) X-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 = Ma Th.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 = Ma Th.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 ')//failure error in setting Euler angle object in accordance with the above-mentioned rotation order}this._order = order;//Set the rotation order again if (update!== FA LSE) This.onchangecallback ();//The callback function is inferred by the update parameter. Return this;//returns a new Euler (Euler angle)},/*///reorder method to set Euler (Euler angle) by four-dollar number ).//Note: The parameter (q) must be a unit vector by calling. Normalize () to get the unit vector.///The Warning:reorder method discards the rotation order information for Euler (Euler angle). *////<summary >reorder</summary>///<param name = "Order" type= "String" > Rotation order </param>///<returns type= " Euler (Euler angle) "> returns the new Euler (Euler angle) </returns>reorder:function () {//Warning:reorder method discards the rotation order information for Euler (Euler angle).// Warning:this discards revolution Information-bhoustonvar q = new three. Quaternion (); return function (Neworder) {Q.setfromeuler (this); This.setfromquaternion (q, Neworder);// Call the Setfromquaternion () method to return the Euler (Euler angle) of the new rotation order;} (), the/*equals method///equals method is equivalent to the comparison operator = = =, the current Euler (Euler angle) and the parameter Euler (X,y,z,order) values are compared to return the bool type value .*////<summary> Equals</summary>///<param name = "V" type= "Euler (Euler Angle)" >euler (Euler angle) (X,y,z,order) </param>///<returns type= "bool" > Return TRUE or False</returns>equals:function (Euler) {return (euler._x = = = This._x ) && (euler._y = = = this._y) && (euler._z = = = This._z) && (Euler._order = = = This._order);},/ The *fromarray method///fromarray method assigns an array of stored Euler (Euler angle) (X,y,z,order) values to the current Euler (Euler angle) object///NOTE: The parameter order is an optional number of parameters. Assuming that the default rotation order is not set . *////<summary>fromarray</summary>///<param name = "Array" type= "array" >euler (Euler angle) (X,y,z,order) The value array array[x,y,z,order]</param>///<returns type= "Euler (Euler angle)" > returns the new Euler (Euler angle) </returns>fromarray: Function (array) {this._x = array[0];this._y = array[1];this._z = array[2];if (array[3]!== undefined) this._o Rder = array[3];this.onchangecallback ();//Call the callback function. Return this;//returns a new Euler (Euler angle)},/*toarray method///toarray method converts the current Euler ( Euler angle) The properties of the object are assigned to the array array[5,3,2, ' XYZ ']. Returns an Array object. *////<summary>toarray</summary>///<returns type= "Array ">euler (Euler angle) (_x,_y,_z,_order) value array Array[x,y,z,order]</returns>toarraY:function () {return [this._x, this._y, this._z, This._order];//returns an array that includes X,y,z,order values,},/*onchange method// The OnChange method passes the callback function expression as a callback parameter to the Onchangecallback () method .*////<summary>onchange</summary>///< PARAM NAME = "callback" type= "function" > callback function </param>///<returns type= "Euler (Euler Angle)" >euler (Euler angle) </ Returns>onchange:function (callback) {This.onchangecallback = Callback;return This;},/*onchangecallback method// The Onchangecallback method clones an Euler (Euler angle) object. The///note:onchangecallback () method here is an empty function attribute, called Onchangecallback () in most of the above methods method, which is a very convenient method. *////<summary>onchangecallback</summary>onchangecallback:function () {},/*clone method// The Clone method clones an Euler (Euler angle) object. *////<summary>clone</summary>///<returns type= "Euler (Euler angle)" > Return Euler ( Euler angle) Object </returns>clone:function () {return new three. Euler (this._x, this._y, This._z, This._order);};
Commercial Territory (http://blog.csdn.net/omni360/)
This article follows "Attribution-non-commercial use-consistent" authoring public agreement
Reprint Please keep this sentence: Business Domain-this blog focuses on Agile development and mobile and IoT device research: Data visualization, Golang, HTML5, WEBGL, three. JS, Otherwise, from this blog article refused to reprint or reprint, thank you for your cooperation.
The following code is the gaze of the Math/quaternion.js file in the Three.js source code file.
Many other updates in: Https://github.com/omni360/three.js.sourcecode/blob/master/Three.js
Three.js Source code Gaze (vii) Math/euler.js