IBSimu  1.0.4
transformation.hpp
Go to the documentation of this file.
00001 
00005 /* Copyright (c) 2010 Taneli Kalvas. All rights reserved.
00006  *
00007  * You can redistribute this software and/or modify it under the terms
00008  * of the GNU General Public License as published by the Free Software
00009  * Foundation; either version 2 of the License, or (at your option)
00010  * any later version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00015  * General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with this library (file "COPYING" included in the package);
00019  * if not, write to the Free Software Foundation, Inc., 51 Franklin
00020  * Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  * 
00022  * If you have questions about your rights to use or distribute this
00023  * software, please contact Berkeley Lab's Technology Transfer
00024  * Department at TTD@lbl.gov. Other questions, comments and bug
00025  * reports should be sent directly to the author via email at
00026  * taneli.kalvas@jyu.fi.
00027  * 
00028  * NOTICE. This software was developed under partial funding from the
00029  * U.S.  Department of Energy.  As such, the U.S. Government has been
00030  * granted for itself and others acting on its behalf a paid-up,
00031  * nonexclusive, irrevocable, worldwide license in the Software to
00032  * reproduce, prepare derivative works, and perform publicly and
00033  * display publicly.  Beginning five (5) years after the date
00034  * permission to assert copyright is obtained from the U.S. Department
00035  * of Energy, and subject to any subsequent five (5) year renewals,
00036  * the U.S. Government is granted for itself and others acting on its
00037  * behalf a paid-up, nonexclusive, irrevocable, worldwide license in
00038  * the Software to reproduce, prepare derivative works, distribute
00039  * copies to the public, perform publicly and display publicly, and to
00040  * permit others to do so.
00041  */
00042 
00043 #ifndef TRANSFORMATION_HPP
00044 #define TRANSFORMATION_HPP 1
00045 
00046 
00047 #include <string.h>
00048 #include "vec3d.hpp"
00049 #include "vec4d.hpp"
00050 
00051 
00059 class Transformation
00060 {
00061 
00062     double x[16]; /* Matrix data in row first order:
00063                    *   0  1  2  3
00064                    *   4  5  6  7
00065                    *   8  9 10 11
00066                    *  12 13 14 15
00067                    */
00068 
00069 public:
00070 
00073     Transformation() {
00074         x[0] = x[5] = x[10] = x[15] = 1.0; 
00075         x[1] = x[2] = x[3] = x[4] = x[6] = x[7] 
00076             = x[8] = x[9] = x[11] = x[12] = x[13]
00077             = x[14] = 0.0;
00078     }
00079 
00082     Transformation( double x11, double x12, double x13, double x14,
00083                     double x21, double x22, double x23, double x24,
00084                     double x31, double x32, double x33, double x34,
00085                     double x41, double x42, double x43, double x44 ) { 
00086         x[0]  = x11;
00087         x[1]  = x12;
00088         x[2]  = x13;
00089         x[3]  = x14;
00090         x[4]  = x21;
00091         x[5]  = x22;
00092         x[6]  = x23;
00093         x[7]  = x24;
00094         x[8]  = x31;
00095         x[9]  = x32;
00096         x[10] = x33;
00097         x[11] = x34;
00098         x[12] = x41;
00099         x[13] = x42;
00100         x[14] = x43;
00101         x[15] = x44;
00102     }
00103 
00106     Transformation( const Transformation &m ) { 
00107         memcpy( x, m.x, 16*sizeof(double) );
00108     }
00109 
00112     ~Transformation() {}
00113 
00114 
00115 
00116 
00117 
00120     double &operator[]( int i ) {
00121         return( x[i] );
00122     }
00123 
00126     const double &operator[]( int i ) const {
00127         return( x[i] );
00128     }
00129     
00132     double determinant( void ) const;
00133 
00136     Transformation inverse( void ) const;
00137 
00140     const Transformation &operator*=( double s );
00141 
00150     Transformation operator*( const Transformation &m ) const;
00151 
00156     Vec4D operator*( const Vec4D &v ) const;
00157 
00163     Vec4D operator%( const Vec4D &v ) const;
00164 
00165 
00166 
00167 
00168 
00171     Vec4D transform( const Vec4D &xin ) const;
00172 
00173 
00174 
00175 
00181     Vec3D transform_point( const Vec3D &xin ) const;
00182 
00192     Vec3D inv_transform_point( const Vec3D &xin ) const;
00193 
00194 
00195 
00196 
00202     Vec3D transform_vector( const Vec3D &xin ) const;
00203 
00213     Vec3D inv_transform_vector( const Vec3D &xin ) const;
00214 
00215 
00216 
00217 
00223     void translate( const Vec3D &d ) {
00224         Transformation t1 = translation( d );
00225         Transformation t2 = *this * t1;
00226         *this = t2;
00227     }
00228 
00234     void scale( const Vec3D &s ) {
00235         Transformation t1 = scaling( s );
00236         Transformation t2 = *this * t1;
00237         *this = t2;
00238     }
00239 
00245     void rotate_x( double a ) {
00246         Transformation t1 = rotation_x( a );
00247         Transformation t2 = *this * t1;
00248         *this = t2;
00249     }
00250 
00256     void rotate_y( double a ) {
00257         Transformation t1 = rotation_y( a );
00258         Transformation t2 = *this * t1;
00259         *this = t2;
00260     }
00261 
00267     void rotate_z( double a ) {
00268         Transformation t1 = rotation_z( a );
00269         Transformation t2 = *this * t1;
00270         *this = t2;
00271     }
00272 
00273 
00274 
00277     static Transformation translation( const Vec3D &d ) {
00278         return( Transformation(  1,  0,  0, d[0],
00279                                  0,  1,  0, d[1],
00280                                  0,  0,  1, d[2],
00281                                  0,  0,  0,    1 ) );
00282     }
00283 
00286     static Transformation scaling( const Vec3D &s ) {
00287         return( Transformation( s[0],    0,    0,  0,
00288                                    0, s[1],    0,  0,
00289                                    0,    0, s[2],  0,
00290                                    0,    0,    0,  1 ) );
00291     }
00292  
00295     static Transformation rotation_x( double a ) {
00296         return( Transformation(  1,      0,       0,  0,
00297                                  0, cos(a), -sin(a),  0,
00298                                  0, sin(a),  cos(a),  0,
00299                                  0,      0,       0,  1 ) );
00300     }
00301 
00304     static Transformation rotation_y( double a ) {
00305         return( Transformation(  cos(a),  0, sin(a),  0,
00306                                       0,  1,      0,  0,
00307                                 -sin(a),  0, cos(a),  0,
00308                                       0,  0,      0,  1 ) );
00309     }
00310 
00313     static Transformation rotation_z( double a ) {
00314         return( Transformation( cos(a), -sin(a),  0,  0,
00315                                 sin(a),  cos(a),  0,  0,
00316                                      0,       0,  1,  0,
00317                                      0,       0,  0,  1 ) );
00318     }
00319 
00322     friend std::ostream &operator<<( std::ostream &os, const Transformation &t );
00323 };
00324 
00325 
00326 
00327 
00328 #endif
00329