Three.js Source Note (vii) Math/euler.js

Source: Internet
Author: User
Tags acos asin cos sin unsupported

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 a comment for the math/quaternion.js file in the three.js source file.

More 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 functions of the Euler object are implemented using the function prototype object defined by///.//////Usage: var Euler = new Euler (5,3,2, ' xyz ') creates a rotation around an axis 5 degrees, rotates a degree around the y-axis, rotates 2 degrees around an axis, and rotates in the order of ' XYZ '. There is a rotation order to determine how many degrees each x, Y, Z axis rotates.///note: Parameter x, The y,z represents the rotation angle of the 3 axes, and the number of axes that are rotated depends on the order of the parameters (Order).///note: Parameter (x,y,z,order) is an optional parameter, if you do not specify a parameter (x,y,z,order), a coordinate is created ( 0,0,0, ' xyz ') eular (Euler angle) object.//Note: The default order of the parameter order (rotation order) is the ' XYZ ' value range is [' xyz ', ' yzx ', ' zxy ', ' XZY ', ' yxz ', ' ZYX ']//////popular speaking, Euler's angle is a common 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, the///y axis of how many degrees, the z-axis of how many degrees, to describe the object in the three-dimensional space in the direction of. "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= "String" >ordeThe default order of R (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 '/******************************************** below is the function function 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 () The usage is 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. The usage of///note:set x () is Euler.prototype.x=value (euler.x = value), this usage is available on browsers other than IE browsers. *////<summary>set x</summary>///<param name = "Value" type= " Number > The angle value </param>set x (value) {this._x = value;//sets the _x value of the Euler angle This.onchangecallback ();//Invokes a callback function.},/ The *///get y method is used to derive the Euler angle y value from. The use of///note:get y () is euler.prototype.y (EULER.Y), which can be 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 re-set the Y value of the Euler angle. and returns the Euler angle of the new value. The use of///note:set y () is euler.prototype.y=value (euler.y = value) , this usage is available on browsers other than IE browsers. *////<summary>set y</summary>///<param name = "value" type= "number" > The angle value rotated around the y axis </param>set y (value) {this._y = value;//sets the _y value of the Euler angle This.onchangecallback ();//Invokes a callback function.},/*///get Z method to obtain the Z-value of the Euler angle. The use of///note:get Z () is 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. The use of///note:set Z () is Euler.prototype.z=value ( Euler.z = value), this usage is available on browsers other than IE browsers. *////<summary>set z</summary>///<param name = "Value" type= " Number > The angle value </param>set Z (value) {this._z = value;//sets the _z value of the Euler angle This.onchangecallback ();//Invokes a callback function.},/ The *///get order method is used to obtain the Euler angle order value. The use of///note:get order () is Euler.prototype.order (Euler.order), which can be used on browsers other than IE browsers . *////<summary>get order</summary>///<returns type= "number" > return euler angle _z value </returns>get Order () {return this._order;//returns the Euler angle _order value},/*///set The order method used to re-set the Euler angle's order value. and returns the Euler angle of the new value.///note:set Order The usage of () is euler.prototype.order=value (Euler.order = value), which can be used on browsers other than IE browsers.///note: Parameter (x,y,z,order) is an optional parameter, If you do not specify a parameter (x,y,z,order), a eular (Euler angle) object with coordinates (0,0,0, ' XYZ ') is created.//NOTE: The default order of the parameter order (rotation order) is the ' XYZ ' value range is [' xyz ', ' yzx ', ' zxy ' , ' XZY ', ' yxz ', ' ZYX ']*////<summary>set order</summary>///<param name = ' value 'Type= "Number" > the angle value of rotation 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: Modify the Set method, which is 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 the new Euler (Euler angle)},/*///copy method used to copy Euler (Euler angle) (X,y,z,ordeR) value. and returns the new Euler (Euler angle). *////<summary>copy</summary>///<param name = "Euler (Euler angle)" type= "quaternion" > Euler (Euler angle) </param>///<returns type= "Euler (Euler angle)" > Return new Euler (Euler angle) </returns>copy:function (Euler) { this._x = euler._x;this._y = Euler._y;this._z = Euler._z;this._order = Euler._order;this.onchangecallback ();// Call the callback function. Return this;//returns the new Euler (Euler angle)},/*///setfromrotationmatrix method using a parameter m (rotation matrix) to achieve the purpose of rotation transformation.///NOTE:M is a rotation matrix, more formulas : http://en.wikipedia.org/wiki/Transformation_matrix//////Example: This sample is rotated 30 degrees on the z axis./------------------------------------ ----------------|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*////<s Ummary>setfromrotationmatrix</summary>///<param name = "M" type= "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 ']</p Aram>///<returns type= "Euler (Euler angle)" > Return to the 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 parameter 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 '//failed to set Euler angle object by following several rotation sequences}this._order = order;// Reset the Rotation order this.onchangecallback ();//Call the callback function. Return this;//returns a new Euler (Euler angle)},/*///setfromquaternion method to set Euler (Euler angle) by a four-dollar number ).///NOTE: The parameter (q) must be a unit vector by calling. Normalize () to get the unit vector.//Note: Parameter update is an optional parameter. If you do not set the default to 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 value//q is assumednormalized//Ensure that q is a unit vector//The following URL is a detailed 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 '//by pressingSet Euler angle object in the above several rotation order failure error}this._order = order;//Reset the rotation order if (update!== false) This.onchangecallback ();// The update parameter is used to determine whether the callback function is called. Return this;//Returns a new Euler (Euler angle)},/*///reorder method to set the rotation order of Euler (Euler's angle) by a four-tuple.//NOTE: The parameter (q) must be a unit vector, The unit vector is obtained by calling. Normalize ().///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)" > Return new Euler (Euler angle) </ The Returns>reorder:function () {///Warning:reorder method discards the rotation order information for Euler (Euler angle).//Warning:this discards revolution Informat Ion-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 equals the comparison operator = = =, compares the current Euler (Euler angle) with the (X,y,z,order) value in the parameter Euler and returns 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" > returns TRUE or false</returns>equals:function (Euler) {return (euler._x = = = this._x) && (euler._y = = = this._y) && (E Uler._z = = = This._z) && (Euler._order = = = This._order);},/*fromarray method///fromarray method will store Euler (Euler angle) (x, Y, Z, Order) value array to the current Euler (Euler angle) object///NOTE: The parameter order is an optional parameter. If you do not set the default rotation order by using the .*////<summary>fromarray</summary> <param name = "Array" type= "array" >euler (Euler angle) (x,y,z,order) value array array[x,y,z,order]</param>///< Returns type= "Euler (Euler angle)" > Return new Euler (Euler angle) </returns>fromarray:function (array) {this._x = array[0];this._y = a rray[1];this._z = array[2];if (array[3]!== undefined) This._order = array[3];this.onchangecallback ();//Call callback function. R Eturn this;//returns the new Euler (Euler angle)},/*toarray method///toarray method assigns the properties of the current Euler (Euler angle) object 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 containing X,y,z,order values, the},/*onchange method///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///onchangecallback method clones an Euler (Euler angle) object.///note:onchangecallback () The method here is an empty function attribute, the Onchangecallback () method is called in most of the above methods, which is a convenient method. *////<summary>onchangecallback</summary >onchangecallback:function () {},/*clone method///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 a comment for the math/quaternion.js file in the three.js source file.

More updates in: https://github.com/omni360/three.js.sourcecode/blob/master/Three.js

Three.js Source Note (vii) Math/euler.js

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.