EdgeSetController.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_EDGESETCONTROLLER_INL
00038 #define SOFA_COMPONENT_CONTROLLER_EDGESETCONTROLLER_INL
00039
00040 #include <sofa/component/controller/EdgeSetController.h>
00041 #include <sofa/core/componentmodel/behavior/MechanicalState.h>
00042 #include <sofa/core/objectmodel/MouseEvent.h>
00043 #include <sofa/core/objectmodel/KeypressedEvent.h>
00044 #include <sofa/defaulttype/VecTypes.h>
00045 #include <sofa/defaulttype/Quat.h>
00046 #include <sofa/helper/system/gl.h>
00047 #include <sofa/helper/gl/template.h>
00048 #include <sofa/simulation/tree/GNode.h>
00049 #include <sofa/simulation/common/MechanicalVisitor.h>
00050 #include <sofa/simulation/common/UpdateMappingVisitor.h>
00051 #include <sofa/component/topology/EdgeSetGeometryAlgorithms.h>
00052
00053 namespace sofa
00054 {
00055
00056 namespace component
00057 {
00058
00059 namespace controller
00060 {
00061
00062 template <class DataTypes>
00063 EdgeSetController<DataTypes>::EdgeSetController()
00064 :step(0.0)
00065 {
00066
00067 }
00068
00069
00070
00071 template <class DataTypes>
00072 void EdgeSetController<DataTypes>::init()
00073 {
00074 _topology = this->getContext()->getMeshTopology();
00075 this->getContext()->get(edgeGeo);
00076 this->getContext()->get(edgeMod);
00077
00078 if (edgeGeo == NULL)
00079 std::cerr << "WARNING. EdgeSetController has no binding EdgeSetGeometryAlgorithms\n";
00080
00081 if (edgeMod == NULL)
00082 std::cerr << "WARNING. EdgeSetController has no binding EdgeSetTopologyModifier\n";
00083
00084 Inherit::init();
00085
00086 if (_topology->getNbEdges()>0)
00087 {
00088 edge0RestedLength = edgeGeo->computeRestEdgeLength(0);
00089 }
00090 }
00091
00092
00093
00094 template <class DataTypes>
00095 void EdgeSetController<DataTypes>::onMouseEvent(core::objectmodel::MouseEvent *mev)
00096 {
00097 switch (mev->getState())
00098 {
00099 case sofa::core::objectmodel::MouseEvent::Wheel :
00100 this->mouseMode = Inherit::Wheel;
00101 step = 2 * (Real)(abs(mev->getWheelDelta()) / mev->getWheelDelta());
00102 break;
00103
00104 default:
00105 break;
00106 }
00107 }
00108
00109 template <class DataTypes>
00110 void EdgeSetController<DataTypes>::onKeyPressedEvent(core::objectmodel::KeypressedEvent *kev)
00111 {
00112 switch(kev->getKey())
00113 {
00114 case '+':
00115 this->mouseMode = Inherit::Wheel;
00116 step = 4;
00117 break;
00118 case '-':
00119 this->mouseMode = Inherit::Wheel;
00120 step = -4;
00121 break;
00122 }
00123 }
00124
00125 template <class DataTypes>
00126 void EdgeSetController<DataTypes>::onBeginAnimationStep()
00127 {
00128 applyController();
00129 }
00130
00131
00132
00133 template <class DataTypes>
00134 void EdgeSetController<DataTypes>::applyController()
00135 {
00136 using sofa::defaulttype::Quat;
00137 using sofa::defaulttype::Vec;
00138
00139 if (this->mouseMode == Inherit::Wheel)
00140 {
00141 this->mouseMode = Inherit::None;
00142
00143 if (this->mState)
00144 {
00145 sofa::helper::Quater<Real>& quatrot = (*this->mState->getX0())[0].getOrientation();
00146 sofa::defaulttype::Vec<3,Real> vectrans(step * this->mainDirection[0] * (Real)0.05, step * this->mainDirection[1] * (Real)0.05, step * this->mainDirection[2] * (Real)0.05);
00147 vectrans = quatrot.rotate(vectrans);
00148
00149 (*this->mState->getX0())[0].getCenter() += vectrans;
00150
00151 }
00152
00153 sofa::simulation::tree::GNode *node = static_cast<sofa::simulation::tree::GNode*> (this->getContext());
00154 sofa::simulation::MechanicalPropagatePositionAndVelocityVisitor mechaVisitor; mechaVisitor.execute(node);
00155 sofa::simulation::UpdateMappingVisitor updateVisitor; updateVisitor.execute(node);
00156
00157 modifyTopology();
00158 }
00159 }
00160
00161
00162
00163 template <class DataTypes>
00164 void EdgeSetController<DataTypes>::modifyTopology(void)
00165 {
00166 assert(edgeGeo != 0);
00167
00168 if (step >= 0)
00169 {
00170 sofa::helper::vector< unsigned int > baseEdge(0);
00171 baseEdge = _topology->getEdgeVertexShell(0);
00172
00173 if (baseEdge.size() == 1)
00174 {
00175 if (edgeGeo->computeRestEdgeLength(baseEdge[0]) > ( 2 * edge0RestedLength ))
00176 {
00177
00178 sofa::helper::vector<unsigned int> indices(0);
00179 indices.push_back(baseEdge[0]);
00180
00181 edgeMod->splitEdges(indices);
00182
00183
00184
00185 unsigned int numPoints = _topology->getNbPoints();
00186
00187 sofa::helper::vector<unsigned int> permutations(numPoints);
00188 permutations[0] = 0;
00189 permutations[numPoints - 1] = 1;
00190
00191 for (unsigned int i = 1; i < numPoints - 1; i++)
00192 permutations[i] = i + 1;
00193
00194
00195
00196
00197
00198
00199
00200
00201 sofa::helper::vector<unsigned int> inverse_permutations(numPoints);
00202 for (unsigned int i = 0; i < numPoints; i++)
00203 inverse_permutations[permutations[i]] = i;
00204
00205
00206
00207
00208
00209
00210
00211
00212 edgeMod->renumberPoints((const sofa::helper::vector<unsigned int> &) inverse_permutations, (const sofa::helper::vector<unsigned int> &) permutations);
00213
00214 }
00215 }
00216 }
00217 else
00218 {
00219 sofa::helper::vector< unsigned int > baseEdge;
00220 baseEdge = _topology->getEdgeVertexShell(1);
00221
00222 if (baseEdge.size() == 2)
00223 {
00224
00225 if ((edgeGeo->computeRestEdgeLength(baseEdge[0]) < ( 0.5 * edge0RestedLength ))
00226 ||(edgeGeo->computeRestEdgeLength(baseEdge[1]) < ( 0.5 * edge0RestedLength )))
00227
00228 {
00229
00230
00231 sofa::helper::vector< sofa::helper::vector<unsigned int> > edges_fuse(0);
00232 sofa::helper::vector<unsigned int> v(0);
00233 v.push_back(baseEdge[0]);
00234 v.push_back(baseEdge[1]);
00235 edges_fuse.push_back(v);
00236 edgeMod->fuseEdges(edges_fuse, true);
00237
00238
00239
00240 unsigned int numPoints = _topology->getNbPoints();
00241
00242 sofa::helper::vector<unsigned int> permutations(numPoints);
00243 permutations[0] = 0;
00244 permutations[1] = numPoints - 1;
00245 for (unsigned int i = 2; i < numPoints; i++)
00246 permutations[i] = i-1;
00247
00248
00249
00250
00251
00252
00253
00254
00255 sofa::helper::vector<unsigned int> inverse_permutations(numPoints);
00256 for (unsigned int i = 0; i < numPoints; i++)
00257 inverse_permutations[permutations[i]] = i;
00258
00259
00260
00261
00262
00263
00264
00265
00266 edgeMod->renumberPoints((const sofa::helper::vector<unsigned int> &) inverse_permutations, (const sofa::helper::vector<unsigned int> &) permutations);
00267
00268 }
00269 }
00270 }
00271 }
00272
00273
00274 template <class DataTypes>
00275 void EdgeSetController<DataTypes>::draw()
00276 {
00277 glDisable(GL_LIGHTING);
00278
00279 if (edgeGeo)
00280 {
00281 glBegin(GL_LINES);
00282 for (int i=0; i<_topology->getNbEdges(); i++)
00283 {
00284 glColor4f(1.0,1.0,0.0,1.0);
00285 helper::gl::glVertexT((*this->mState->getX())[_topology->getEdge(i)[0]]);
00286 helper::gl::glVertexT((*this->mState->getX())[_topology->getEdge(i)[1]]);
00287 }
00288 glEnd();
00289
00290 glPointSize(10);
00291 glBegin(GL_POINTS);
00292 for (int i=0; i<_topology->getNbEdges(); i++)
00293 {
00294 glColor4f(1.0,0.0,0.0,1.0);
00295 helper::gl::glVertexT((*this->mState->getX())[_topology->getEdge(i)[0]]);
00296 helper::gl::glVertexT((*this->mState->getX())[_topology->getEdge(i)[1]]);
00297 }
00298 glEnd();
00299 glPointSize(1);
00300 }
00301 }
00302
00303
00304 }
00305
00306 }
00307
00308 }
00309
00310 #endif // SOFA_COMPONENT_CONTROLLER_EDGESETCONTROLLER_INL