Frame.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 /* Francois Faure, INRIA-UJF, 2006
00028  */
00029 #include <sofa/defaulttype/Frame.h>
00030 #include <iostream>
00031 using std::cout;
00032 using std::endl;
00033 
00034 namespace sofa
00035 {
00036 
00037 namespace defaulttype
00038 {
00039 
00040 Frame::Frame (const Vec3 &origin, const Mat33& matrix )
00041                 : origin_(origin)
00042                 , basis_(matrix)
00043 {}
00044 Frame::Frame (const Vec3 &origin, const Quat &orientation, const Vec3& scale )
00045 {
00046         setTransform( origin, orientation, scale );
00047 }
00048 
00049 Frame::Frame (const Vec3 &origin )
00050                 : origin_(origin)
00051 {
00052         basis_.identity();
00053 }
00054 
00055 Frame::Frame ()
00056 {
00057         clear();
00058 }
00059 
00060 void Frame::clear()
00061 {
00062         basis_.identity();
00063         origin_.clear();
00064 }
00065 
00066 //void clear() { origin_.clear(); basis__.clear(); scale_=Vec3(1,1,1); }
00067 
00068 Frame::Vec3& Frame::getOrigin ()
00069 {
00070         return origin_;
00071 }
00072 const Frame::Vec3& Frame::getOrigin () const
00073 {
00074         return origin_;
00075 }
00076 void Frame::setOrigin( const Frame::Vec3& origin )
00077 {
00078         origin_=origin;
00079 }
00080 
00081 Frame::Mat33& Frame::getBasis ()
00082 {
00083         return basis_;
00084 }
00085 const Frame::Mat33& Frame::getBasis () const
00086 {
00087         return basis_;
00088 }
00089 void Frame::setBasis( const Frame::Mat33& m )
00090 {
00091         basis_=m;
00092 }
00093 
00094 void Frame::setTransform( const Frame::Vec3& origin, const Frame::Quat& orientation, const Frame::Vec3& scale )
00095 {
00096         setOrigin(origin);
00097         orientation.toMatrix(basis_);
00098         for( int i=0; i<3; i++ ) {
00099                 basis_[i][0] *= scale[0];
00100                 basis_[i][1] *= scale[1];
00101                 basis_[i][2] *= scale[2];
00102         }
00103 }
00104 
00105 
00106 
00107 Frame Frame::identity()
00108 {
00109         Frame c;
00110         c.clear();
00111         return c;
00112 }
00113 
00115 void Frame::multRight( const Frame& c )
00116 {
00117         origin_ += basis_ * c.getOrigin();
00118         basis_ = basis_ * c.getBasis();
00119 }
00120 
00122 Frame Frame::mult( const Frame& c ) const
00123 {
00124         Frame r = (*this);
00125         r.multRight(c);
00126         return r;
00127 }
00128 
00130 void Frame::writeOpenGlMatrix( float *m ) const
00131 {
00132         m[0] = (float) basis_[0][0];
00133         m[1] = (float) basis_[1][0];
00134         m[2] = (float) basis_[2][0];
00135         m[3] = (float) 0;
00136         m[4] = (float) basis_[0][1];
00137         m[5] = (float) basis_[1][1];
00138         m[6] = (float) basis_[2][1];
00139         m[7] = (float) 0;
00140         m[8] = (float) basis_[0][2];
00141         m[9] = (float) basis_[1][2];
00142         m[10] = (float) basis_[2][2];
00143         m[11] = (float) 0;
00144         m[12] = (float) origin_[0];
00145         m[13] = (float) origin_[1];
00146         m[14] = (float) origin_[2];
00147         m[15] = (float) 1;
00148 }
00149 
00151 void Frame::writeOpenGlMatrix( double *m ) const
00152 {
00153         m[0] = basis_[0][0];
00154         m[1] = basis_[1][0];
00155         m[2] = basis_[2][0];
00156         m[3] = 0;
00157         m[4] = basis_[0][1];
00158         m[5] = basis_[1][1];
00159         m[6] = basis_[2][1];
00160         m[7] = 0;
00161         m[8] = basis_[0][2];
00162         m[9] = basis_[1][2];
00163         m[10] = basis_[2][2];
00164         m[11] = 0;
00165         m[12] = origin_[0];
00166         m[13] = origin_[1];
00167         m[14] = origin_[2];
00168         m[15] = 1;
00169 }
00170 
00172 Frame Frame::inversed() const
00173 {
00174         Mat33 inv;
00175 #ifdef NDEBUG
00176         inv.invert( basis_ );
00177 #else
00178         bool invertible = inv.invert( basis_ );
00179         assert( invertible );
00180 #endif
00181         return Frame( -(inv*origin_) , inv );
00182 }
00183 
00184 std::ostream& operator << (std::ostream& out, const sofa::defaulttype::Frame& c )
00185 {
00186     out<<"origin = "<<c.getOrigin()<<", basis matrix="<<endl;
00187     for( int i=0; i<3; i++ ) {
00188         for( int j=0; j<3; j++ )
00189             out<<c.getBasis()[i][j]<<" ";
00190         out<<endl;
00191     }
00192     return out;
00193 }
00194 
00195 } // namespace defaulttype
00196 
00197 } // namespace sofa
00198 
00199 
00200 

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