MechanicalStateController.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 //
00026 // C++ Models: MechanicalStateController
00027 //
00028 // Description:
00029 //
00030 //
00031 // Author: Pierre-Jean Bensoussan, Digital Trainers (2008)
00032 //
00033 // Copyright: See COPYING file that comes with this distribution
00034 //
00035 //
00036 
00037 #ifndef SOFA_COMPONENT_CONTROLLER_MECHANICALSTATECONTROLLER_INL
00038 #define SOFA_COMPONENT_CONTROLLER_MECHANICALSTATECONTROLLER_INL
00039 
00040 #include <sofa/component/controller/MechanicalStateController.h>
00041 #include <sofa/core/componentmodel/behavior/MechanicalState.h>
00042 #include <sofa/core/objectmodel/MouseEvent.h>
00043 #include <sofa/core/objectmodel/OmniEvent.h>
00044 #include <sofa/defaulttype/VecTypes.h>
00045 #include <sofa/defaulttype/Quat.h>
00046 #include <sofa/simulation/tree/GNode.h>
00047 #include <sofa/simulation/common/MechanicalVisitor.h>
00048 #include <sofa/simulation/common/UpdateMappingVisitor.h>
00049 
00050 
00051 
00052 namespace sofa
00053 {
00054 
00055 namespace component
00056 {
00057 
00058 namespace controller
00059 {
00060 
00061 template <class DataTypes>
00062 MechanicalStateController<DataTypes>::MechanicalStateController()
00063     : index( initData(&index, (unsigned int)0, "index", "Index of the controlled DOF") )
00064     , mainDirection(sofa::defaulttype::Vec<3,Real>((Real)0.0, (Real)0.0, (Real)-1.0))
00065     , mainDirectionPtr( initDataPtr(&mainDirectionPtr, &mainDirection, "mainDirection", "Main direction and orientation of the controlled DOF") )
00066 {
00067     mainDirection.normalize();
00068 }
00069 
00070 template <class DataTypes>
00071 void MechanicalStateController<DataTypes>::init()
00072 {
00073     using sofa::simulation::tree::GNode;
00074     using core::componentmodel::behavior::MechanicalState;
00075     mainDirectionPtr.beginEdit();
00076     mState = dynamic_cast<MechanicalState<DataTypes> *> (this->getContext()->getMechanicalState());
00077     if (!mState)
00078         std::cerr << "WARNING - MechanicalStateController has no binding MechanicalState\n";
00079     omni = false;
00080 }
00081 
00082 
00083 
00084 template <class DataTypes>
00085 void MechanicalStateController<DataTypes>::onMouseEvent(core::objectmodel::MouseEvent *mev)
00086 {
00087     eventX = mev->getPosX();
00088     eventY = mev->getPosY();
00089 
00090     switch (mev->getState())
00091     {
00092         case sofa::core::objectmodel::MouseEvent::LeftPressed :
00093             mouseMode = BtLeft;
00094             mouseSavedPosX = eventX;
00095             mouseSavedPosY = eventY;
00096             break;
00097 
00098         case sofa::core::objectmodel::MouseEvent::LeftReleased :
00099             mouseMode = None;
00100             break;
00101 
00102         case sofa::core::objectmodel::MouseEvent::RightPressed :
00103             mouseMode = BtRight;
00104             mouseSavedPosX = eventX;
00105             mouseSavedPosY = eventY;
00106             break;
00107 
00108         case sofa::core::objectmodel::MouseEvent::RightReleased :
00109             mouseMode = None;
00110             break;
00111 
00112         case sofa::core::objectmodel::MouseEvent::Move :
00113             if (handleEventTriggersUpdate.getValue())
00114                 applyController();
00115             break;
00116 
00117         default :
00118             break;
00119     }
00120 }
00121 
00122 template <class DataTypes>
00123 void MechanicalStateController<DataTypes>::onOmniEvent(core::objectmodel::OmniEvent *oev)
00124 {
00125     omni = true;
00126     omniX = oev->getPosX();
00127     omniY = oev->getPosY();
00128     omniZ = oev->getPosZ();
00129     position = oev->getPosition();
00130     orientation = oev->getOrientation();
00131     applyController();
00132     omni = false;
00133 }
00134 
00135 
00136 
00137 template <class DataTypes>
00138 void MechanicalStateController<DataTypes>::onBeginAnimationStep()
00139 {
00140     applyController();
00141 }
00142 
00143 
00144 
00145 template <class DataTypes>
00146 void MechanicalStateController<DataTypes>::applyController()
00147 {
00148     using sofa::defaulttype::Quat;
00149     using sofa::defaulttype::Vec;
00150 
00151     if(omni)
00152     {
00153         if(mState)
00154         {
00155 
00156                 //cout << "youyou " << mState << endl;
00157                 //if(mState->getXfree())
00158                 {
00159                     //cout << "yoyo" << endl;
00160 //                  (*mState->getXfree())[0].getCenter()[0] = (Real)omniX;
00161 //                  //(*mState->getX())[0].getCenter()[0] = omniX;
00162 //                  (*mState->getXfree())[0].getCenter()[1] = (Real)omniY;
00163 //                  //(*mSt ate->getX())[0].getCenter()[1] = omniY;
00164 //                  (*mState->getXfree())[0].getCenter()[2] = (Real)omniZ;
00165 //                  //(*mState->getX())[0].getCenter()[2] = omniZ;
00166 
00167                     (*mState->getXfree())[0].getCenter() = position;
00168                     (*mState->getX())[0].getCenter() = position;
00169 
00170 //                  (*mState->getXfree())[0].getOrientation()[0] = 0.0;
00171 //                  (*mState->getXfree())[0].getOrientation()[1] = 0.0;
00172 //                  (*mState->getXfree())[0].getOrientation()[2] = 0.0;
00173 //                  (*mState->getXfree())[0].getOrientation()[3] = 1.0;
00174 
00175                     (*mState->getXfree())[0].getOrientation() = orientation;
00176 
00177 //                  (*mState->getX())[0].getOrientation()[0] = 0.0;
00178 //                  (*mState->getX())[0].getOrientation()[1] = 0.0;
00179 //                  (*mState->getX())[0].getOrientation()[2] = 0.0;
00180 //                  (*mState->getX())[0].getOrientation()[3] = 1.0;
00181 
00182                     (*mState->getX())[0].getOrientation() = orientation;
00183 
00184                     sofa::simulation::tree::GNode *node = static_cast<sofa::simulation::tree::GNode*> (this->getContext());
00185                     sofa::simulation::MechanicalPropagatePositionAndVelocityVisitor mechaVisitor; mechaVisitor.execute(node);
00186                     sofa::simulation::UpdateMappingVisitor updateVisitor; updateVisitor.execute(node);
00187                 }
00188         }
00189         omni = false;
00190     }
00191     if ((mouseMode==BtLeft) || (mouseMode==BtRight))
00192     {
00193         int dx = eventX - mouseSavedPosX;
00194         int dy = eventY - mouseSavedPosY;
00195         mouseSavedPosX = eventX;
00196         mouseSavedPosY = eventY;
00197 
00198         if (mState)
00199         {
00200             unsigned int i = index.getValue();
00201 
00202             Vec<3,Real> x(1,0,0);
00203             Vec<3,Real> y(0,1,0);
00204             Vec<3,Real> z(0,0,1);
00205 
00206             if (mouseMode==BtLeft)
00207             {
00208                 (*mState->getXfree())[i].getOrientation() = (*mState->getX())[i].getOrientation() * Quat(y, dx * (Real)0.001) * Quat(z, dy * (Real)0.001);
00209                 (*mState->getX())[i].getOrientation() = (*mState->getX())[i].getOrientation() * Quat(y, dx * (Real)0.001) * Quat(z, dy * (Real)0.001);
00210             }
00211             else
00212             {
00213                 sofa::helper::Quater<Real>& quatrot = (*mState->getX())[i].getOrientation();
00214                 sofa::defaulttype::Vec<3,Real> vectrans(dy * mainDirection[0] * (Real)0.05, dy * mainDirection[1] * (Real)0.05, dy * mainDirection[2] * (Real)0.05);
00215                 vectrans = quatrot.rotate(vectrans);
00216                 (*mState->getXfree())[i].getCenter() += vectrans;
00217                 (*mState->getX())[i].getCenter() += vectrans;
00218 
00219                 (*mState->getXfree())[i].getOrientation() = (*mState->getX())[i].getOrientation() * Quat(x, dx * (Real)0.001);
00220                 (*mState->getX())[i].getOrientation() = (*mState->getX())[i].getOrientation() * Quat(x, dx * (Real)0.001);
00221             }
00222         }
00223         sofa::simulation::tree::GNode *node = static_cast<sofa::simulation::tree::GNode*> (this->getContext());
00224         sofa::simulation::MechanicalPropagatePositionAndVelocityVisitor mechaVisitor; mechaVisitor.execute(node);
00225         sofa::simulation::UpdateMappingVisitor updateVisitor; updateVisitor.execute(node);
00226     }
00227 }
00228 
00229 
00230 
00231 template <class DataTypes>
00232 core::componentmodel::behavior::MechanicalState<DataTypes> *MechanicalStateController<DataTypes>::getMechanicalState() const
00233 {
00234     return mState;
00235 }
00236 
00237 
00238 
00239 template <class DataTypes>
00240 void MechanicalStateController<DataTypes>::setMechanicalState(core::componentmodel::behavior::MechanicalState<DataTypes> *_mState)
00241 {
00242     mState = _mState;
00243 }
00244 
00245 
00246 
00247 template <class DataTypes>
00248 unsigned int MechanicalStateController<DataTypes>::getIndex() const
00249 {
00250     return index.getValue();
00251 }
00252 
00253 
00254 
00255 template <class DataTypes>
00256 void MechanicalStateController<DataTypes>::setIndex(const unsigned int _index)
00257 {
00258     index.setValue(_index);
00259 }
00260 
00261 
00262 
00263 template <class DataTypes>
00264 const sofa::defaulttype::Vec<3, typename MechanicalStateController<DataTypes>::Real > &MechanicalStateController<DataTypes>::getMainDirection() const
00265 {
00266     return mainDirection;
00267 }
00268 
00269 
00270 
00271 template <class DataTypes>
00272 void MechanicalStateController<DataTypes>::setMainDirection(const sofa::defaulttype::Vec<3,Real> _mainDirection)
00273 {
00274     mainDirection = _mainDirection;
00275 }
00276 
00277 
00278 } // namespace controller
00279 
00280 } // namespace component
00281 
00282 } // namespace sofa
00283 
00284 #endif // SOFA_COMPONENT_CONTROLLER_MECHANICALSTATECONTROLLER_H

Generated on Sun Nov 23 02:50:35 2008 for Sofa by  doxygen 1.5.7.1