This is a matrix type template I have prepared in the game 3D graphics processing, which can generate an integer, floating point, and custom type, and define some common three-dimensional vector calculation function template, hope. Everyone gives a revised comment. It is recommended that everyone only looks at the function of the function, don't see the specific implementation, because the format is not right after it is passed, because the file is relatively long, I divide them into two posts
Matrix Template
// ******************************************************** *************************** /// The declaration: * /// * /// This version is purely in personal hobby, There may be many errors. Mainly in order to make your hobby * // people exchange together with each other. Therefore, you can modify the code that copies them, and you can apply * /// on any occasion, but the resulting problem does not have any responsibility. If you have any suggestions, please feel free to contact * /// I will discuss together. * / / / My mailbox is: swnuwangyun@21cn.com * /// *************************************** *************************************************** / / / * //// Matrix.h * /// This file defines the basic matrix template, which can result in various types of various types of types such as INT, FLOAT, DOUBLE, etc.. The basic matrix operator is overloaded. In order to meet the vector processing * // requirements, a series of coordinate transformation functions are defined * //// /// * //// ****************************** *********************************************************** ***** /// Version Upgrade Description: The original version of the matrix can meet the general needs, but when there is no default constructor in the structure * ///, it is very Inconvenient and unable to define a variety of * /// common special vectors. The return is due to the parameters of only one matrix data type in the original template. In the * // / this release, the template parameters are extended to 3, and there are two rows and columns of the matrix, and the initial INT reference * /// number when the configuration is canceled, and the unified initialization is 0. This matrix has the default constructor, and there is enough information in the template parameter * // // effective to solve the above problems.
* /// ************************************************************** ***************************** /// Writer: Wang Yun * /// Modifier: Wang Yun // Start Time : 2002.10.18 * / // End Time: 2002.10.19 * /// ****************************************** ************************************************************* / # include #include #include template class matrix; // ******************* *********************************************************** ***** /// two-dimensional vector and point * //// ******************************** *************************************************************** / TYPEDEF MATRIX Vector2i; Typedef Matrix Vertex2i; typedef matrix vector2f; typedef matrix vertex2f; typedef matrix vector2d; typedef Matrix vertex2d; // ************************************************** *********** ************************* /// 3D vector and point * /// ************ *********************************************************** ************ / TYPEDEF Matrix Vector3i; TypeDef Matrix Vertex3i; TypeDef Matrix vector3f; typedef matrix Vertex3f; typedef matrix vector3d; typedef matrix vertex3d;
// ******************************************************** ************************** /// 4D quarter coordinates and vector * /// ********** *********************************************************** *************** / TYPEDEF Matrix vector4i; typef matrix Vertex4i; typedef matrix vector4f; typedef Matrix vertex4f; type, 1, 4> vector4d; typedef matrix vertex4d; // *************** *********************************************************** ********* /// Common Vector Operation Function Template Definition * /// *************************** ***************************************************************************** // * Template // Vector component on various local coordinate axes int VectorProjection (Matrix & Vector, Matrix * PVectorx, Matrix < ELEMTYPE, R, C> * PVectory, Matrix * PVectorz); * / Template & Vector); Template // Vector Unit Matrix VectorUnit (Matrix & Vector); Template // Rotate Matrix around X-axis (Matrix & Vector, Float Beta); Template // Wheel Rotate Matrix Rotateoy (Matrix & Vector, Float Beta); Template // Rotate Matrix Rotateoz (Matrix & Vector, Float Beta; Template <
TypeName ElemType, int R, INT C> // Rotate Matrix Rotateon (Matrix & Reel, MATRIX & vector, Float Beta) Template // Rotate Matrix VECTORPRODUCT (Matrix & VB) ; // Transform to the quantity Template // int coordinateTransfer (Matrix & Origin, Matrix & XDirect, Matrix & Ydirect, Matrix & zdirect, Matrix * & PVERTEX, INT NVERTEX); Template // Two vector Code INT VECTORANGLE (Matrix & B, Matrix & A, Float * sin, float * cos); // *********** *********************************************************** ************* /// Matrix Template definition * /// * /// * /// ************************************************************** ********************************************************** / TEMPLATE Class Matrix {Private: int Row; / / Line int coL; // column ELMTYPE * pelemType; // Store the type array of matrix data
Public: Matrix (); Matrix (Const Matrix & A); // Depth Stack copy function must define before all of the calculation functions, otherwise the return to the parameter transmission // will be adopted The default copy constructor is made to get the result of the wrong // mistake ~ matrix (); public: inline int achieve () {return row;} inline int getcol () {Return Col;
Inline ELEMTYPE & OPERATOR () (Const Int & I, Const Int & J) {Return PelemType [(I-1) * Row J-1];} Matrix & Operator = (Const Matrix & a);
Friend Matrix Operator (Const Matrix & A, Const Matrix & b); Friend Matrix Operator - (Const Matrix & A, Const Matrix & b); Friend Matrix Operator * (Const Matrix & A, Const Matrix & b);
// The following three functions are defined for the multiplication of constants and matrices. Obviously, the redundancy is very large, and the future goal is // internally fixed a function template, from the Matches Matter Matrix Operator * (Const Double & Scale, R, C> & b); Friend Matrix Operator * (Const Float & Scale, Const Matrix & B Friend Matrix Operator * (Const Int & Scale, Const Matrix & b); // = Operator to return to a constant reference to A, // Yes In order to be able to use the elevational, ie c = a = D E, the return value cannot be used as the left value Friend Const Matrix & Operator = (Matrix & A, Const Matrix & b); Friend Const Matrix & Operator - = (Matrix & A, Const Matrix & B); Friend Const Matrix & Operator * = (Matrix & A, Const Matrix & b); // The following two operations symbol The overload is mainly for the use and testing of the character boundary //. Friend istream & Operator >> (ISTREAM & A); Friend Ostream & Operator << (Ostream & Out, Matrix & a);
}
// ******************************************************** ********************************* The commonly used vector operation function template implements code * /// ******** *********************************************************** ************************* / TEMPLATE ElemType Vectormo (Matrix & Vector) {ELMTYPE A = Vector (1 , 1), B = VECTOR (1, 2), c = vector (1, 3); RETURN SQRT (A * a B * B C * c);} Template // Vector Unit Matrix VectorUnit (Matrix & Vector) {/ * Matrix Result; EleMType Mo = 1 / VectorMo (Vector); Result = Mo * vector; return result; * / return 1 / vectormo (vector) * Vector;} Template Matrix Rotateox (Matrix & Vector, Float Beta) {Matrix Result; // In order to reduce the number of times the number of calls, increase the individual components of the spatial temporary //, ELMTYPE X = VECTOR (1, 1); ELMTYPE Y = vector (1, 2) ELMTYPE Z = VECTOR (1, 3); Result (1, 1) = x; Result (1, 2) = Y * COS (Beta) -z * sin (beta); Result (1, 3) = Y * SIN (Beta) Z * COS (Beta);
Return Result;} Template Matrix Rotateoy (Matrix & Vector, Float Beta) {Matrix Result; / / In order to reduce the number of times the following call function, increase the various components of the spatial temporary // time memory spinning axis ELMTYPE X = VECTOR (1, 1); ELMTYPE Y = Vector (1, 2); ELMTYPE Z = VECTOR (1, 3 );
Result (1,1) = x * Cos (beta) z * sin (beta); result (1, 2) = Y; Result (1, 3) = - x * sin (beta) z * COS (Beta ); returnrate;} template Matrix Rotateoz (Matrix & Vector, Float Beta) {Matrix Result; / / In order to reduce the number of times the function below, increase the various components of the space temporary // time memory spinning axis ELMTYPE X = VECTOR (1, 1); ELMTYPE Y = VECTOR (1, 2); ELMTYPE Z = VECTOR (1 , 3);
Result (1,1) = x * cos (beta) -y * sin (beta); Result (1, 2) = x * sin (beta) y * cos (beta); Result (1, 3) = z ;
Return Result;} Template Matrix Rotateon (Matrix & Reel, Matrix & Vector, Float Beta) {Matrix Result; // In order to reduce the number of times the following call function, increase the number of space temporary // time memory spinning axis ELMTYPE REELX = Reel (1, 1); ELMTYPE Rely = Reel (1, 2); ELMTYPE reelz = reel (1, 3); // In order to reduce frequently large amounts of operation, increase space temporary // storage of the rotation shaft of the origin and the angle of the X, Y, Z axis ELEMTYPE S = SQRT (Reelx * Reelx Rely * Rely Reelz * Reelz); ELMTYPE N1 = ReelX / S; ELMTYPE N2 = Rely / S; ELMTYPE N3 = ReelZ / S; / / To reduce the number of calculations, temporarily store common data ELEMTYPE N1N1 = N1 * N1, N2N2 = N2 * N2, N3N3 = N3 * N3, N1N3 = N1 * N3, N1N2 = N1 * N2, N2N3 = N2 * N3; // In order to reduce the number of calculations, temporary Store // sinusoidal value and cosine value ELEMTYPE COSBETA = COS (Beta), sinbeta = sin (beta); // core, calculate the respective components after rotation Result (1) = reelx * (N1N1 (1- N1N1) * COSBETA) rely * (N1N2 * (1-cosbeta) -N3 * sinbeta) reelz * (N1N3 * (1-cosbeta) N2 * sinbeta); Result (1, 2) = reelx * (N1N2 * (1-COSBETA) N3 * SINBETA) Rely * (N2N2 (1-N2N2) * Cosbeta) Reelz * (N2N3 * (1-cosbeta) -n1 * sinbeta); Result (1, 3) = reelx * ( N1N3 * (1-cosbeta) -N2 * sinbeta) rely * (N2N3 * (1-cosbeta) n1 * sinbeta) reelz * (N3N3
(1-N3N3) * Cosbeta); Return Result;} Template Matrix VectorProduct (Matrix & Va, Matrix & VB) / / quantity {Matrix Result (1, 3, 0); Result (1, 1) = VA (1, 2) * VB (1, 3) -VA (1, 3) * VB) * VB (1, 2); Result (1, 2) = VA (1, 3) * VB (1, 1) -VA (1, 1) * VB (1, 3) Result (1, 3) = VA (1, 1) * VB (1, 2) -VA (1, 2) * VB (1, 1); returnrate;} template Int CoordinateTransfer (Matrix & Origin, Matrix & XDirect, Matrix & Ydirect, Matrix & Zdirect, Matrix < ELMTYPE, R, C> * & PVERTEX, INT NVERTEX) // Coordinate Transformation {Return 0;} Template Int Vector (Matrix & B, Matrix & A, FLOAT * SIN, FLOAT * COS) {// Strove of two vector angles ELEMTYPE AX = A (1, 1), AY = A (1, 2), AZ = A (1, 3), BX = B (1, 1), BY = B (1, 2), BZ = B (1, 3); * COS = (AX * BX AY * BY AZ * BZ) / SQRT (AX * AX AY * AY AZ * AZ) / SQRT (bx * bx by * by bz * bz); * sin = sqrt (1 - (* cos * * cos)); return 1;}
// ******************************************************** **************************************************************************************** *********************************************************** ************** / TEMPLATE Matrix :: Matrix () {this-> row = r; this-> COL = C; while (! (this-> pelemtype = new elemtype [row * col])); // To construct a matrix, it will be able to success for for (int i = 0; i Matrix :: Matrix (Const Matrix & A) // Depth Stack Copy Construction Function {ROW = A.Row; col = a.col; while (! "); For (int i = 0; i
Template Matrix :: ~ Matrix () {if (pelemType) delete [] pelemType;
Template Matrix & Matrix :: Operator = (Const Matrix & a) {// Because Here is the statement of the matrix row check, with row = a.row; col = a.col; // while (! (")); // To improve efficiency, you can consider using memory District copy technology for (int i = 0; i
Template Matrix Operator (Const Matrix & A, Const Matrix & b) {Matrix Result = a; // Note, here use const types to construct the front // of the object is the parameters of the stack copy function must also be const // type, otherwise an error for errors (INT i = 0; I Matrix Operator - (Const Matrix & A, Const Matrix & b) {Matrix < ELEMTYPE, R, C> Result = a; for (int i = 0; i
Template Matrix Operator * (Const Matrix & A, Const Matrix & b) {// for matrix Multiplication, here should be inserted into two operations // 作 行 行 行 行 m 行 行; (INT i = 0; i Matrix Operator * ( Const Double & Scale, Const Matrix & b) {Matrix Result = B; for (int i = 0; i
Template Matrix Operator * (Const Float & Scale, Const Matrix & b) {Matrix Result = B For (int i = 0; i
Template Matrix Operator * (Const Int & Scale, Const Matrix & b) {Matrix Result = B For (int i = 0; i Const Matrix & Operator = (Matrix & A, Const Matrix & b) {a = a b; Return A;}
Template Const Matrix & Operator - = (Matrix & A, Const Matrix & b) {a = Ab; returnon
Template Const Matrix & Operator * = (Matrix & A, Const Matrix & b) {a = A * b; return a;} Template ISTREAM & OPERATOR >> (ISTREAM & IN, MATRIX & a) {cout << "please input the massix:" << endl; for (int i = 0; i > A.PELEMTYPE [i * row j]; return in } Template Ostream & Operator << (Ostream & Out, Matrix & a) {for (int i = 0; i
// ******************************************************** *************************** /// test function * /// ************* *********************************************************** *********** / VOID Main () {Vector3F V; V (1, 1) = 4; V (1, 2) = 2; Vector3F R; R (1, 1) = 1; Cout << Rotateox (V, 3); COUT << Rotateoy (V, 3);
COUT << V; Matrix Matrix; Matrix (4, 4) = 235; Matrix (2, 1) = 3; v = V V; cout << v; cout << Matrix Matrix COUT << Matrix * Matrix;
Cout << v; cout << vectormo (v); cout << vectorunit (v);
}