MechanicalStateController.inl
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
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
00157
00158 {
00159
00160
00161
00162
00163
00164
00165
00166
00167 (*mState->getXfree())[0].getCenter() = position;
00168 (*mState->getX())[0].getCenter() = position;
00169
00170
00171
00172
00173
00174
00175 (*mState->getXfree())[0].getOrientation() = orientation;
00176
00177
00178
00179
00180
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 }
00279
00280 }
00281
00282 }
00283
00284 #endif // SOFA_COMPONENT_CONTROLLER_MECHANICALSTATECONTROLLER_H