OpenWalnut  1.5.0dev
WMatrixInitializers.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WMATRIXINITIALIZERS_H
26 #define WMATRIXINITIALIZERS_H
27 
28 #include "WMatrixFixed.h"
29 #include "WVectorFixed.h"
30 
31 // This file contains a lot of useful matrix initializers, especially useful for 4x4 matrix.
32 
33 /**
34  * Create a scaling matrix, scaling with a given factor along each axis.
35  *
36  * \tparam ValueT type of the scaling parameters
37  * \param sx scaling in X direction
38  * \param sy scaling in Y direction
39  * \param sz scaling in Z direction
40  *
41  * \return created matrix.
42  */
43 template< typename ValueT >
44 WMatrix4d makeScale( ValueT sx, ValueT sy, ValueT sz )
45 {
46  WMatrix4d m;
47  m( 0, 0 ) = sx;
48  m( 1, 1 ) = sy;
49  m( 2, 2 ) = sz;
50  m( 3, 3 ) = 1.0;
51  return m;
52 }
53 
54 /**
55  * Create a scaling matrix, scaling with a given factor along each axis.
56  *
57  * \tparam VectorT the vector types used
58  * \param vec vector whose elements describe the scaling.
59  *
60  * \return the created matrix.
61  */
62 template< typename VectorT >
63 WMatrix4d makeScale( const VectorT& vec )
64 {
65  return makeScale( vec[0], vec[1], vec[2] );
66 }
67 
68 /**
69  * Create a translation matrix, translating with a given factor along each axis.
70  *
71  * \tparam ValueT type of the translation parameters
72  * \param tx translation in X direction
73  * \param ty translation in Y direction
74  * \param tz translation in Z direction
75  *
76  * \return created matrix.
77  */
78 template< typename ValueT >
79 WMatrix4d makeTranslate( ValueT tx, ValueT ty, ValueT tz )
80 {
81  WMatrix4d m;
82  m( 0, 3 ) = tx;
83  m( 1, 3 ) = ty;
84  m( 2, 3 ) = tz;
85  m( 3, 3 ) = 1.0;
86  return m;
87 }
88 
89 /**
90  * Create a translation matrix, translating with a given factor along each axis.
91  *
92  * \tparam VectorT the vector types used
93  * \param vec vector whose elements describe the scaling.
94  *
95  * \return the created matrix.
96  */
97 template< typename VectorT >
98 WMatrix4d makeTranslate( const VectorT& vec )
99 {
100  return makeTranslate( vec[0], vec[1], vec[2] );
101 }
102 
103 /**
104  * Creates a rotation matrix.
105  *
106  * \tparam ValueT type of the parameters
107  * \param angle the angle to rotate in degree
108  * \param x rotation in x direction
109  * \param y rotation in y direction
110  * \param z rotation in z direction
111  *
112  * \return created matrix.
113  */
114 template< typename ValueT >
115 WMatrix4d makeRotate( ValueT angle, ValueT x, ValueT y, ValueT z )
116 {
117  // This can be read in the OpenGL Red Book -- Appendix Homogeneous Coordinates and Transformation Matrices.
118 
119  // First: create some vectors and matrices we need.
120  // Normalize axis
121  WVector3d u( normalize( WVector3d( x, y, z ) ) );
122  // The row vector of the axis
123  WVectorRow3d uT( transpose( u ) );
124  WMatrix3d uuT = u * uT;
125  WMatrix3d s;
126  s( 0, 0 ) = 0.0; s( 0, 1 ) = -u[2]; s( 0, 2 ) = u[1]; // NOLINT - multiple commands on one line
127  s( 1, 0 ) = u[2]; s( 1, 1 ) = 0.0; s( 1, 2 ) = -u[0]; // NOLINT - multiple commands on one line
128  s( 2, 0 ) = -u[1]; s( 2, 1 ) = u[0]; s( 2, 2 ) = 0.0; // NOLINT - multiple commands on one line
129 
130  // Now we can formulate the rotation matrix:
133  uuT + cos( angle ) * ( WMatrix3d::identity() - uuT ) + sin( angle ) * s
134  );
135 }
136 
137 /**
138  * Creates a rotation matrix using the specified vector, which describes the axe
139  *
140  * \tparam ValueT type of the parameters
141  * \param angle the angle to rotate in degree
142  * \param vec rotation axe
143  *
144  * \return created matrix.
145  */
146 template< typename VectorT >
147 WMatrix4d makeRotate( const VectorT& vec )
148 {
149  return makeRotate( vec[0], vec[1], vec[2] );
150 }
151 
152 #endif // WMATRIXINITIALIZERS_H
153 
static MatrixType identity()
Returns an identity matrix.
Definition: WMatrixFixed.h:310
static MatrixType fromMatrices(const MatrixType &m, const WMatrixFixed< RHSValueT, RHSRows, RHSCols, RHSValueStoreT > &src, size_t rowOffset=0, size_t colOffset=0)
Copy construction allowing the creation of a WMatrixFixed by another matrix of different size.
Definition: WMatrixFixed.h:380