Transformation.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This library is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU Lesser General Public License as published by    *
00007 * the Free Software Foundation; either version 2.1 of the License, or (at     *
00008 * your option) any later version.                                             *
00009 *                                                                             *
00010 * This library is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
00013 * for more details.                                                           *
00014 *                                                                             *
00015 * You should have received a copy of the GNU Lesser General Public License    *
00016 * along with this library; if not, write to the Free Software Foundation,     *
00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
00018 *******************************************************************************
00019 *                              SOFA :: Framework                              *
00020 *                                                                             *
00021 * Authors: M. Adam, J. Allard, B. Andre, P-J. Bensoussan, S. Cotin, C. Duriez,*
00022 * H. Delingette, F. Falipou, F. Faure, S. Fonteneau, L. Heigeas, C. Mendoza,  *
00023 * M. Nesme, P. Neumann, J-P. de la Plata Alcade, F. Poyer and F. Roy          *
00024 *                                                                             *
00025 * Contact information: contact@sofa-framework.org                             *
00026 ******************************************************************************/
00027 #include <sofa/helper/gl/Transformation.h>
00028 #include <sofa/helper/system/gl.h>
00029 #include <sofa/helper/gl/template.h>
00030 
00031 namespace sofa
00032 {
00033 
00034 namespace helper
00035 {
00036 
00037 namespace gl
00038 {
00039 
00040 // --------------------------------------------------------------------------------------
00041 // --- Constructor
00042 // --------------------------------------------------------------------------------------
00043 Transformation::Transformation()
00044 {
00045     register int    i, j;
00046 
00047     for (i = 0; i < 3; i++)
00048     {
00049         translation[i] = 0.0;
00050         scale[i] = 1.0;
00051         objectCenter[i] = 0.0;
00052     }
00053 
00054     for (i = 0; i < 4; i++)
00055     {
00056         for (j = 0; j < 4; j++)
00057         {
00058             rotation[i][j] = 0.0;
00059         }
00060         rotation[i][i] = 1.0;
00061     }
00062 }
00063 
00064 
00065 // --------------------------------------------------------------------------------------
00066 // --- Destructor
00067 // --------------------------------------------------------------------------------------
00068 Transformation::~Transformation()
00069 {
00070 }
00071 
00072 
00073 // --------------------------------------------------------------------------------------
00074 // --- Operator =
00075 // --------------------------------------------------------------------------------------
00076 Transformation& Transformation::operator=(const Transformation& transform)
00077 {
00078     register int i, j;
00079 
00080     for (i = 0; i < 3; i++)
00081     {
00082         translation[i] = transform.translation[i];
00083         scale[i] = transform.scale[i];
00084         objectCenter[i] = transform.objectCenter[i];
00085     }
00086 
00087     for (i = 0; i < 4; i++)
00088     {
00089         for (j = 0; j < 4; j++)
00090         {
00091             rotation[i][j] = transform.rotation[i][j];
00092         }
00093     }
00094 
00095     return *this;
00096 }
00097 
00098 
00099 // --------------------------------------------------------------------------------------
00100 // --- Apply the transformation
00101 // --------------------------------------------------------------------------------------
00102 void Transformation::Apply()
00103 {
00104     helper::gl::glTranslate(translation[0], translation[1], translation[2]);
00105     helper::gl::glMultMatrix((SReal *)rotation);
00106     helper::gl::glScale(scale[0], scale[1], scale[2]);
00107 }
00108 
00109 
00110 // --------------------------------------------------------------------------------------
00111 // --- First center the object, then apply the transformation (to align with the corresponding texture)
00112 // --------------------------------------------------------------------------------------
00113 void Transformation::ApplyWithCentring()
00114 {
00115     Apply();
00116     
00117     helper::gl::glTranslate(-objectCenter[0], -objectCenter[1], -objectCenter[2]);
00118 }
00119 
00120 
00121 // --------------------------------------------------------------------------------------
00122 // --- Apply the inverse transformation
00123 // --------------------------------------------------------------------------------------
00124 void Transformation::ApplyInverse()
00125 {
00126     SReal   iRotation[4][4];
00127 
00128     InvertTransRotMatrix(rotation, iRotation);
00129 
00130     helper::gl::glScale((SReal)1.0 / scale[0], (SReal)1.0 / scale[1], (SReal)1.0 / scale[2]);
00131     helper::gl::glMultMatrix((SReal *)rotation);
00132     helper::gl::glTranslate(-translation[0], -translation[1], -translation[2]);
00133     
00134 
00135 }
00136 
00137 
00138 //----------------------------------------------------------------------------
00139 //--- Inversion for 4x4 matrix only containing rotations and translations
00140 //--- Transpose rotation matrix and mutiple by -1 translation row
00141 //----------------------------------------------------------------------------
00142 void Transformation::InvertTransRotMatrix(SReal matrix[4][4])
00143 {
00144     SReal   tmp;
00145 
00146     tmp = matrix[0][1];
00147     matrix[0][1] = matrix[1][0];
00148     matrix[1][0] = tmp;
00149 
00150     tmp = matrix[0][2];
00151     matrix[0][2] = matrix[2][0];
00152     matrix[2][0] = tmp;
00153 
00154     tmp = matrix[1][2];
00155     matrix[1][2] = matrix[2][1];
00156     matrix[2][1] = tmp;
00157 
00158     matrix[3][0] = -matrix[3][0];
00159     matrix[3][1] = -matrix[3][1];
00160     matrix[3][2] = -matrix[3][2];
00161 }
00162 
00163 
00164 //----------------------------------------------------------------------------
00165 //--- Invert the source matrix sMatrix and put the result in the destination matrix dMatrix
00166 //--- Inversion for 4x4 matrix only containing rotations and translations
00167 //--- Transpose rotation matrix and mutiple by -1 translation row
00168 //----------------------------------------------------------------------------
00169 void Transformation::InvertTransRotMatrix(SReal sMatrix[4][4],
00170                                           SReal dMatrix[4][4])
00171 {
00172     register int    i, j;
00173 
00174     for (i = 0; i < 4; i++)
00175     {
00176         for (j = 0; j < 4; j++)
00177         {
00178             dMatrix[i][j] = sMatrix[i][j];
00179         }
00180     }
00181     InvertTransRotMatrix(dMatrix);
00182 }
00183 
00184 } // namespace gl
00185 
00186 } // namespace helper
00187 
00188 } // namespace sofa
00189 

Generated on Sun Nov 23 02:29:30 2008 for Sofa by  doxygen 1.5.7.1