OpenWalnut
1.4.0
|
00001 //--------------------------------------------------------------------------- 00002 // 00003 // Project: OpenWalnut ( http://www.openwalnut.org ) 00004 // 00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS 00006 // For more information see http://www.openwalnut.org/copying 00007 // 00008 // This file is part of OpenWalnut. 00009 // 00010 // OpenWalnut is free software: you can redistribute it and/or modify 00011 // it under the terms of the GNU Lesser General Public License as published by 00012 // the Free Software Foundation, either version 3 of the License, or 00013 // (at your option) any later version. 00014 // 00015 // OpenWalnut is distributed in the hope that it will be useful, 00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 // GNU Lesser General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU Lesser General Public License 00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>. 00022 // 00023 //--------------------------------------------------------------------------- 00024 00025 #ifndef WMATRIXINITIALIZERS_H 00026 #define WMATRIXINITIALIZERS_H 00027 00028 #include "WMatrixFixed.h" 00029 #include "WVectorFixed.h" 00030 00031 // This file contains a lot of useful matrix initializers, especially useful for 4x4 matrix. 00032 00033 /** 00034 * Create a scaling matrix, scaling with a given factor along each axis. 00035 * 00036 * \tparam ValueT type of the scaling parameters 00037 * \param sx scaling in X direction 00038 * \param sy scaling in Y direction 00039 * \param sz scaling in Z direction 00040 * 00041 * \return created matrix. 00042 */ 00043 template< typename ValueT > 00044 WMatrix4d makeScale( ValueT sx, ValueT sy, ValueT sz ) 00045 { 00046 WMatrix4d m; 00047 m( 0, 0 ) = sx; 00048 m( 1, 1 ) = sy; 00049 m( 2, 2 ) = sz; 00050 m( 3, 3 ) = 1.0; 00051 return m; 00052 } 00053 00054 /** 00055 * Create a scaling matrix, scaling with a given factor along each axis. 00056 * 00057 * \tparam VectorT the vector types used 00058 * \param vec vector whose elements describe the scaling. 00059 * 00060 * \return the created matrix. 00061 */ 00062 template< typename VectorT > 00063 WMatrix4d makeScale( const VectorT& vec ) 00064 { 00065 return makeScale( vec[0], vec[1], vec[2] ); 00066 } 00067 00068 /** 00069 * Create a translation matrix, translating with a given factor along each axis. 00070 * 00071 * \tparam ValueT type of the translation parameters 00072 * \param tx translation in X direction 00073 * \param ty translation in Y direction 00074 * \param tz translation in Z direction 00075 * 00076 * \return created matrix. 00077 */ 00078 template< typename ValueT > 00079 WMatrix4d makeTranslate( ValueT tx, ValueT ty, ValueT tz ) 00080 { 00081 WMatrix4d m; 00082 m( 0, 3 ) = tx; 00083 m( 1, 3 ) = ty; 00084 m( 2, 3 ) = tz; 00085 m( 3, 3 ) = 1.0; 00086 return m; 00087 } 00088 00089 /** 00090 * Create a translation matrix, translating with a given factor along each axis. 00091 * 00092 * \tparam VectorT the vector types used 00093 * \param vec vector whose elements describe the scaling. 00094 * 00095 * \return the created matrix. 00096 */ 00097 template< typename VectorT > 00098 WMatrix4d makeTranslate( const VectorT& vec ) 00099 { 00100 return makeTranslate( vec[0], vec[1], vec[2] ); 00101 } 00102 00103 /** 00104 * Creates a rotation matrix. 00105 * 00106 * \tparam ValueT type of the parameters 00107 * \param angle the angle to rotate in degree 00108 * \param x rotation in x direction 00109 * \param y rotation in y direction 00110 * \param z rotation in z direction 00111 * 00112 * \return created matrix. 00113 */ 00114 template< typename ValueT > 00115 WMatrix4d makeRotate( ValueT angle, ValueT x, ValueT y, ValueT z ) 00116 { 00117 // This can be read in the OpenGL Red Book -- Appendix Homogeneous Coordinates and Transformation Matrices. 00118 00119 // First: create some vectors and matrices we need. 00120 // Normalize axis 00121 WVector3d u( normalize( WVector3d( x, y, z ) ) ); 00122 // The row vector of the axis 00123 WVectorRow3d uT( transpose( u ) ); 00124 WMatrix3d uuT = u * uT; 00125 WMatrix3d s; 00126 s( 0, 0 ) = 0.0; s( 0, 1 ) = -u[2]; s( 0, 2 ) = u[1]; // NOLINT - multiple commands on one line 00127 s( 1, 0 ) = u[2]; s( 1, 1 ) = 0.0; s( 1, 2 ) = -u[0]; // NOLINT - multiple commands on one line 00128 s( 2, 0 ) = -u[1]; s( 2, 1 ) = u[0]; s( 2, 2 ) = 0.0; // NOLINT - multiple commands on one line 00129 00130 // Now we can formulate the rotation matrix: 00131 return WMatrix4d::fromMatrices( 00132 WMatrix4d::identity(), 00133 uuT + cos( angle ) * ( WMatrix3d::identity() - uuT ) + sin( angle ) * s 00134 ); 00135 } 00136 00137 /** 00138 * Creates a rotation matrix using the specified vector, which describes the axe 00139 * 00140 * \tparam ValueT type of the parameters 00141 * \param angle the angle to rotate in degree 00142 * \param vec rotation axe 00143 * 00144 * \return created matrix. 00145 */ 00146 template< typename VectorT > 00147 WMatrix4d makeRotate( const VectorT& vec ) 00148 { 00149 return makeRotate( vec[0], vec[1], vec[2] ); 00150 } 00151 00152 #endif // WMATRIXINITIALIZERS_H 00153