📄 matrix3x3.as
字号:
/** * project3D Engine * @author John Sword * @version 2 - AS3 * * Conversion to AS2 from Brandom Williams 3x3 Matrix source * also bits and pieces from JME (Java 3d Engine) * constructor for a 3x3 Matrix datatype * */package engine.math{ import engine.geom.Vector; import engine.geom.Vertex; public class Matrix3x3 { //var e:Array; public var aa:Number; public var ab:Number; public var ac:Number; public var ba:Number; public var bb:Number; public var bc:Number; public var ca:Number; public var cb:Number; public var cc:Number; private static const matrix:Matrix3x3 = new Matrix3x3(); private static const sin:Function = Math.sin; private static const cos:Function = Math.cos; public function Matrix3x3 () { // initialize the elements to the identity matrix load_identity (); } // returns the matrix made from multiplying this matrix with the scalar 's' public function scalar_multiplication ( s:Number ) : Matrix3x3 { // matrix that will be returned //var product:Matrix3x3 = new Matrix3x3 (); //matrix; // calculate the elements of the product matrix matrix.aa *= s; matrix.ab *= s; matrix.ac *= s; matrix.ba *= s; matrix.bb *= s; matrix.bc *= s; matrix.ca *= s; matrix.cb *= s; matrix.cc *= s; // return the scaled matrix return matrix; } // returns the vector made from multiplying this matrix with V public function vector_multiplication ( V:Vector ) : Vector { var x:Number = V.x, y:Number = V.y, z:Number = V.z; // return the product vector return new Vector ( x * aa + y * ab + z * ac, x * ba + y * bb + z * bc, x * ca + y * cb + z * cc ); } // returns the Vertex made from multiplying this matrix with V public function vertex_multiplication ( V:Vertex ) : Vertex { // vertex that will be returned var product:Vertex = new Vertex (); var x:Number = V.x, y:Number = V.y, z:Number = V.z; // calculate the components of the vertex product.x = x * aa + y * ab + z * ac; product.y = x * ba + y * bb + z * bc; product.z = x * ca + y * cb + z * cc; // return the product vertex return ( product ); } // returns the matrix made from multiplying this matrix with M public function matrix_multiplication (M:Matrix3x3) : Matrix3x3 { // matrix that will be returned //var product:Matrix3x3 = new Matrix3x3 (); //matrix; // calculate the elements of the product matrix matrix.aa = M.aa * aa + M.ba * ab + M.ca * ac; matrix.ab = M.ab * aa + M.bb * ab + M.cb * ac; matrix.ac = M.ac * aa + M.bc * ab + M.cc * ac; matrix.ba = M.aa * ba + M.ba * bb + M.ca * bc; matrix.bb = M.ab * ba + M.bb * bb + M.cb * bc; matrix.bc = M.ac * ba + M.bc * bb + M.cc * bc; matrix.ca = M.aa * ca + M.ba * cb + M.ca * cc; matrix.cb = M.ab * ca + M.bb * cb + M.cb * cc; matrix.cc = M.ac * ca + M.bc * cb + M.cc * cc; // return the product matrix return (matrix); } // returns the transpose matrix of this matrix public function transpose () : Matrix3x3 { // transposed matrix that will be returned //var transpose:Matrix3x3 = new Matrix3x3 (); //matrix; // set the elements of the transposed matrix matrix.aa = aa; matrix.ab = ba; matrix.ac = ca; matrix.ba = ab; matrix.bb = bb; matrix.bc = cb; matrix.ca = ac; matrix.cb = bc; matrix.cc = cc; // return the transposed matrix return (matrix); } // loads the identity matrix into this matrix public function load_identity () : void { // set the elements of the matrix for an identity matrix aa = 1.0; ab = 0.0; ac = 0.0; ba = 0.0; bb = 1.0; bc = 0.0; ca = 0.0; cb = 0.0; cc = 1.0; } // loads a rotation matrix for a rotation around the x-axis given the sine and cosine of the rotation angle public function load_rotation_x (sine:Number, cosine:Number) : void { // set the elements of the rotation matrix aa = 1.0; ab = 0.0; ac = 0.0; ba = 0.0; bb = cosine; bc = -sine; ca = 0.0; cb = sine; cc = cosine; } // loads a rotation matrix for a rotation around the y-axis given the sine and cosine of the rotation angle public function load_rotation_y (sine:Number, cosine:Number) : void { // set the elements of the rotation matrix aa = cosine; ab = 0.0; ac = sine; ba = 0.0; bb = 1.0; bc = 0.0; ca = -sine; cb = 0.0; cc = cosine; } // loads a rotation matrix for a rotation around the z-axis given the sine and cosine of the rotation angle public function load_rotation_z (sine:Number, cosine:Number) : void { // set the elements of the rotation matrix aa = cosine; ab = -sine; ac = 0.0; ba = sine; bb = cosine; bc = 0.0; ca = 0.0; cb = 0.0; cc = 1.0; } // loads a rotation matrix about the xyz-axes given the sine and cosine of three rotation angles public function load_rotation_xyz (sin_x:Number, cos_x:Number, sin_y:Number, cos_y:Number, sin_z:Number, cos_z:Number) : void { // set the elements of the rotation matrix aa = cos_y * cos_z; ab = -cos_y * sin_z; ac = sin_y; ba = sin_x * sin_y * cos_z + cos_x * sin_z bb = -sin_x * sin_y * sin_z + cos_x * cos_z; bc = -sin_x * cos_y; ca = -cos_x * sin_y * cos_z + sin_x * sin_z; cb = cos_x * sin_y * sin_z + sin_x * cos_z; cc = cos_x * cos_y; } /** * <code>angleRotation</code> sets this matrix to that of a rotation about * three axes (x, y, z). Where each axis has a specified rotation in * degrees. These rotations are expressed in a single <code>Vector</code> * object. * * @param angles * the angles to rotate. */ public function angleRotation ( angles:Vector ) : void { var angle:Number, sr:Number, sp:Number, sy:Number, cr:Number, cp:Number, cy:Number; angle = (angles.z * Math.PI / 180); sy = sin(angle); cy = cos(angle); angle = (angles.y * Math.PI / 180); sp = sin(angle); cp = cos(angle); angle = (angles.x * Math.PI / 180); sr = sin(angle); cr = cos(angle); // matrix = (Z * Y) * X aa = cp * cy; ba = cp * sy; ca = -sp; ab = sr * sp * cy + cr * -sy; bb = sr * sp * sy + cr * cy; cb = sr * cp; ac = (cr * sp * cy + -sr * -sy); bc = (cr * sp * sy + -sr * cy); cc = cr * cp; } // loads a rotation matrix about a unit vector given the sine and cosine of the rotation angle public function load_rotation_axis (A:Vector, sine:Number, cosine:Number) : void { var t:Number = 1 - cosine, x:Number = A.x, y:Number = A.y, z:Number = A.z; // set the elements of the rotation matrix aa = t * x * x + cosine; ab = t * x * y - sine * z; ac = t * x * z + sine * y; ba = t * x * y + sine * z; bb = t * y * y + cosine; bc = t * y * z - sine * x; ca = t * x * z - sine * y; cb = t * y * z + sine * x; cc = t * z * z + cosine; } // rotate a vector by this matrix public function rotateVect ( vec:Vector ) : void { var vx:Number = vec.x, vy:Number = vec.y, vz:Number = vec.z; vec.x = vx * aa + vy * ab + vz * ac; vec.y = vx * ba + vy * bb + vz * bc; vec.z = vx * ca + vy * cb + vz * cc; } public function fromAngleAxis ( angle:Number, axis:Vector ) : void { var normAxis:Vector = axis.normalize(); fromAngleNormalAxis(angle, normAxis); } public function fromAngleNormalAxis ( angle:Number, axis:Vector ) : void { zero(); var fCos:Number = cos(angle); var fSin:Number = sin(angle); var fOneMinusCos:Number = 1-fCos; var fX2:Number = axis.x*axis.x; var fY2:Number = axis.y*axis.y; var fZ2:Number = axis.z*axis.z; var fXYM:Number = axis.x*axis.y*fOneMinusCos; var fXZM:Number = axis.x*axis.z*fOneMinusCos; var fYZM:Number = axis.y*axis.z*fOneMinusCos; var fXSin:Number = axis.x*fSin; var fYSin:Number = axis.y*fSin; var fZSin:Number = axis.z*fSin; aa = fX2*fOneMinusCos+fCos; ab = fXYM-fZSin; ac = fXZM+fYSin; ba = fXYM+fZSin; bb = fY2*fOneMinusCos+fCos; bc = fYZM-fXSin; ca = fXZM-fYSin; cb = fYZM+fXSin; cc = fZ2*fOneMinusCos+fCos; } /** * generates the determinate of this matrix. * @return the determinate */ public function determinant() : Number { var fCo00:Number = bb*cc - bc*cb; var fCo10:Number = bc*ca - ba*cc; var fCo20:Number = ba*cb - bb*ca; var fDet:Number = aa*fCo00 + ab*fCo10 + ac*fCo20; return fDet; } /** * Inverts this matrix and stores it in the given store. * * @return The store */ public function invert ( store:Matrix3x3 ) : Matrix3x3 { if (store == null) store = new Matrix3x3(); var d:Number = determinant(); if ( Math.abs(d) < 0.001 ) return store.zero(); store.aa = bb * cc - bc * cb; store.ab = ac * cb - ab * cc; store.ac = ab * bc - ac * bb; store.ba = bc * ca - ba * cc; store.bb = aa * cc - ac * ca; store.bc = ac * ba - aa * bc; store.ca = ba * cb - bb * ca; store.cb = ab * ca - aa * cb; store.cc = aa * bb - ab * ba; store.multLocal(1/d); return store; } /** * <code>multLocal</code> multiplies this matrix internally by * a given scale factor. * * @param scale * the value to scale by. * @return this Matrix3x3 */ public function multLocal ( scale:Number ) : Matrix3x3 { aa *= scale; ab *= scale; ac *= scale; ba *= scale; bb *= scale; bc *= scale; ca *= scale; cb *= scale; cc *= scale; return this; } public function zero () : Matrix3x3 { aa = ab = ac = ba = bb = bc = ca = cb = cc = 0; return this; } public function clone () : Matrix3x3 { var m:Matrix3x3 = new Matrix3x3(); m.aa = aa; m.ab = ab; m.ac = ac; m.ba = ba; m.bb = bb; m.bc = bc; m.ca = ca; m.cb = cb; m.cc = cc; return m; } // make matrix trace-able public function toString () : String { return ("Matrix3x3 : " + '\n' + "[ " + aa + " , " + ab + " , " + ac + " ]" + '\n' + "[ " + ba + " , " + bb + " , " + bc + " ]" + '\n' + "[ " + ca + " , " + cb + " , " + cc + " ]" ); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -