OscillatorConstraint.inl

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 :: Modules                               *
00020 *                                                                             *
00021 * Authors: The SOFA Team and external contributors (see Authors.txt)          *
00022 *                                                                             *
00023 * Contact information: contact@sofa-framework.org                             *
00024 ******************************************************************************/
00025 #ifndef SOFA_COMPONENT_CONSTRAINT_OSCILLATORCONSTRAINT_INL
00026 #define SOFA_COMPONENT_CONSTRAINT_OSCILLATORCONSTRAINT_INL
00027 
00028 #include <sofa/component/constraint/FixedConstraint.inl>
00029 #include <sofa/component/constraint/OscillatorConstraint.h>
00030 #include <math.h>
00031 
00032 namespace sofa
00033 {
00034 
00035 namespace component
00036 {
00037 
00038 namespace constraint
00039 {
00040 
00041 using namespace sofa::defaulttype;
00042 
00043 template <class DataTypes>
00044 OscillatorConstraint<DataTypes>::OscillatorConstraint()
00045   : core::componentmodel::behavior::Constraint<DataTypes>(NULL)
00046   , constraints(initData(&constraints,"oscillators","Define a sequence of oscillating particules: \n[index, mean, amplitude, pulsation, phase]"))
00047 {
00048 }
00049 
00050 
00051 template <class DataTypes>
00052 OscillatorConstraint<DataTypes>::OscillatorConstraint(core::componentmodel::behavior::MechanicalState<DataTypes>* mstate)
00053 : core::componentmodel::behavior::Constraint<DataTypes>(mstate)
00054   , constraints(initData(&constraints,"oscillators","Define a sequence of oscillating particules: \n[index, Mean(x,y,z), amplitude(x,y,z), pulsation, phase]"))
00055 {
00056 }
00057 
00058 template <class DataTypes>
00059 OscillatorConstraint<DataTypes>::~OscillatorConstraint()
00060 {
00061 }
00062 
00063 template <class DataTypes>
00064 OscillatorConstraint<DataTypes>*  OscillatorConstraint<DataTypes>::addConstraint(unsigned index, const Coord& mean, const Deriv& amplitude, Real pulsation, Real phase)
00065 {
00066   this->constraints.beginEdit()->push_back( Oscillator(index,mean,amplitude,pulsation,phase) );
00067     return this;
00068 }
00069 
00070 
00071 template <class DataTypes>
00072 void OscillatorConstraint<DataTypes>::projectResponse(VecDeriv& res)
00073 {
00074         const helper::vector< Oscillator > &oscillators = constraints.getValue();
00075     //Real t = (Real) getContext()->getTime();
00076     for( unsigned i=0; i<oscillators.size(); ++i )
00077     {
00078           const unsigned& index = oscillators[i].index;
00079         //const Deriv& a = constraints[i].second.amplitude;
00080         //const Real& w = constraints[i].second.pulsation;
00081         //const Real& p = constraints[i].second.phase;
00082         
00083         //res[index] = a*(-w)*w*sin(w*t+p);
00084         res[index] = Deriv();
00085     }
00086 }
00087 
00088 template <class DataTypes>
00089 void OscillatorConstraint<DataTypes>::projectVelocity(VecDeriv& res)
00090 {
00091         const helper::vector< Oscillator > &oscillators = constraints.getValue();
00092     Real t = (Real) getContext()->getTime();
00093         for( unsigned i=0; i<oscillators.size(); ++i )
00094     {
00095           const unsigned& index = oscillators[i].index;
00096           const Deriv& a = oscillators[i].amplitude;
00097           const Real& w = oscillators[i].pulsation;
00098           const Real& p = oscillators[i].phase;
00099         
00100       res[index] = a*w*cos(w*t+p);
00101     }
00102 }
00103 
00104 template <class DataTypes>
00105 void OscillatorConstraint<DataTypes>::projectPosition(VecCoord& res)
00106 {
00107         const helper::vector< Oscillator > &oscillators = constraints.getValue();
00108     Real t = (Real) getContext()->getTime();
00109     //std::cerr<<"OscillatorConstraint<DataTypes>::projectPosition, t = "<<t<<endl;
00110     for( unsigned i=0; i<oscillators.size(); ++i )
00111     {
00112         const unsigned& index = oscillators[i].index;
00113         const Coord& m = oscillators[i].mean;
00114         const Deriv& a = oscillators[i].amplitude;
00115         const Real& w = oscillators[i].pulsation;
00116         const Real& p = oscillators[i].phase;
00117         
00118         res[index] = m + a*sin(w*t+p);
00119     }
00120 }
00121 
00122 
00124 //template <>
00125 //      void OscillatorConstraint<RigidTypes >::draw();
00126 //template <>
00127 //      void OscillatorConstraint<RigidTypes >::projectResponse(VecDeriv& dx);
00128 
00129 } // namespace constraint
00130 
00131 } // namespace component
00132 
00133 } // namespace sofa
00134 
00135 #endif

Generated on Sun Nov 23 02:43:59 2008 for Sofa by  doxygen 1.5.7.1