LMLConstraint.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 #ifndef LMLCONSTRAINT_INL
00026 #define LMLCONSTRAINT_INL
00027
00028 #include "sofa/core/componentmodel/behavior/Constraint.inl"
00029 #include "LMLConstraint.h"
00030 #include "sofa/helper/gl/template.h"
00031
00032 namespace sofa
00033 {
00034
00035 namespace filemanager
00036 {
00037
00038 namespace pml
00039 {
00040
00041
00042 template<class DataTypes>
00043 LMLConstraint<DataTypes>::LMLConstraint(Loads* loadsList, const map<unsigned int, unsigned int> &atomIndexToDOFIndex, MechanicalState<DataTypes> *mm)
00044 : Constraint<DataTypes>(mm), atomToDOFIndexes(atomIndexToDOFIndex)
00045 {
00046 mmodel = mm;
00047 loads = new Loads();
00048 Load * load;
00049 SReal dirX, dirY, dirZ;
00050 this->setName("loads");
00051
00052
00053 for (unsigned int i=0 ; i<loadsList->numberOfLoads() ; i++) {
00054 load = loadsList->getLoad(i);
00055 if (load->getType() == "Translation") {
00056 if (load->getDirection().isToward()) {
00057 std::map<unsigned int, unsigned int>::const_iterator titi = atomIndexToDOFIndex.find(load->getDirection().getToward());
00058 if (titi != atomIndexToDOFIndex.end()){
00059 unsigned int dofInd = titi->second;
00060 dirX = (*mm->getX())[dofInd].x();
00061 dirY = (*mm->getX())[dofInd].y();
00062 dirZ = (*mm->getX())[dofInd].z();
00063 }
00064 }
00065 else
00066 load->getDirection(dirX, dirY, dirZ);
00067 unsigned int cpt=0;
00068 for (unsigned int j=0 ; j<load->numberOfTargets(); j++) {
00069 std::map<unsigned int, unsigned int>::const_iterator result = atomIndexToDOFIndex.find(load->getTarget(j));
00070 if (result != atomIndexToDOFIndex.end()) {
00071 cpt++;
00072 if (load->getDirection().isToward())
00073 addConstraint(result->second, Deriv(dirX-(*mm->getX())[result->second].x(),dirY-(*mm->getX())[result->second].y(),dirZ-(*mm->getX())[result->second].z()) );
00074 else
00075 addConstraint(result->second, Deriv(dirX,dirY,dirZ) );
00076
00077 if (load->getDirection().isXNull() && load->getValue(0) != 0)
00078 fixDOF(result->second, 0);
00079 if (load->getDirection().isYNull() && load->getValue(0) != 0)
00080 fixDOF(result->second, 1);
00081 if (load->getDirection().isZNull() && load->getValue(0) != 0)
00082 fixDOF(result->second, 2);
00083 }
00084 }
00085 if (cpt > 0)
00086 loads->addLoad(load);
00087 }
00088 }
00089 }
00090
00091
00092 template<class DataTypes>
00093 LMLConstraint<DataTypes>* LMLConstraint<DataTypes>::addConstraint(unsigned int index, Deriv trans)
00094 {
00095 this->targets.push_back(index);
00096 trans.normalize();
00097 this->translations.push_back(trans);
00098 this->directionsNULLs.push_back(Deriv(1,1,1));
00099 this->initPos.push_back(Deriv(0,0,0));
00100 return this;
00101 }
00102
00103 template<class DataTypes>
00104 LMLConstraint<DataTypes>* LMLConstraint<DataTypes>::removeConstraint(int index)
00105 {
00106 std::vector<unsigned int>::iterator it1=targets.begin();
00107 VecDerivIterator it2=translations.begin();
00108 VecDerivIterator it3=directionsNULLs.begin();
00109 while(it1 != targets.end() && *it1!=(unsigned)index) {
00110 it1++;
00111 it2++;
00112 it3++;
00113 }
00114
00115 targets.erase(it1);
00116 translations.erase(it2);
00117 directionsNULLs.erase(it3);
00118
00119 return this;
00120 }
00121
00122
00123 template<class DataTypes>
00124 void LMLConstraint<DataTypes>::fixDOF(int index, int axe)
00125 {
00126
00127 std::vector<unsigned int>::iterator it1=targets.begin();
00128 VecDerivIterator it2 = directionsNULLs.begin();
00129 while(it1 != targets.end() && *it1!=(unsigned)index) {
00130 it1++;
00131 it2++;
00132 }
00133
00134 (*it2)[axe] = 0;
00135 }
00136
00137
00138 template<class DataTypes>
00139 void LMLConstraint<DataTypes>::projectResponse(VecDeriv& dx)
00140 {
00141
00142
00143 SReal time = this->getContext()->getTime();
00144 SReal prevTime = time - this->getContext()->getDt();
00145
00146 std::vector<unsigned int>::iterator it1=targets.begin();
00147 VecDerivIterator it2=translations.begin();
00148 VecDerivIterator it3=directionsNULLs.begin();
00149 Load * load;
00150 SReal valTime, prevValTime;
00151
00152 for (unsigned int i=0 ; i<loads->numberOfLoads() ; i++) {
00153 load = loads->getLoad(i);
00154 valTime = load->getValue(time);
00155 prevValTime = load->getValue(prevTime);
00156 for(unsigned int j=0 ; j<load->numberOfTargets();j++) {
00157 if ( atomToDOFIndexes.find(load->getTarget(j)) != atomToDOFIndexes.end() )
00158 {
00159
00160 if (load->getDirection().isXNull() && valTime != 0)
00161 (*it3)[0]=0;
00162 if (load->getDirection().isYNull() && valTime != 0)
00163 (*it3)[1]=0;
00164 if (load->getDirection().isZNull() && valTime != 0)
00165 (*it3)[2]=0;
00166
00167 if (valTime == 0)
00168 (*it3) = Deriv(1,1,1);
00169 else{
00170 if (load->getDirection().isToward()) {
00171 std::map<unsigned int, unsigned int>::const_iterator titi = atomToDOFIndexes.find(load->getDirection().getToward());
00172 if (titi != atomToDOFIndexes.end()){
00173 (*it2) = (*mmodel->getX())[titi->second] - (*mmodel->getX())[*it1];
00174 it2->normalize();
00175 }
00176 }
00177
00178
00179 dx[*it1][0] = ((*it2)[0]*valTime)-((*it2)[0]*prevValTime)*(*it3)[0];
00180 dx[*it1][1] = ((*it2)[1]*valTime)-((*it2)[1]*prevValTime)*(*it3)[1];
00181 dx[*it1][2] = ((*it2)[2]*valTime)-((*it2)[2]*prevValTime)*(*it3)[2];
00182 }
00183
00184 it1++;
00185 it2++;
00186 it3++;
00187 }
00188 }
00189 }
00190 }
00191
00192 template<class DataTypes>
00193 void LMLConstraint<DataTypes>::projectPosition(VecCoord& x)
00194 {
00195 SReal time = getContext()->getTime();
00196
00197 std::vector<unsigned int>::iterator it1=targets.begin();
00198 VecDerivIterator it2=translations.begin();
00199 VecDerivIterator it3=initPos.begin();
00200 Load * load;
00201
00202 for (unsigned int i=0 ; i<loads->numberOfLoads() ; i++)
00203 {
00204 load = loads->getLoad(i);
00205 for(unsigned int j=0 ; j<load->numberOfTargets();j++)
00206 {
00207 if ( atomToDOFIndexes.find(load->getTarget(j)) != atomToDOFIndexes.end() ) {
00208 if ( (*it3)[0]==0.0 && (*it3)[1]==0.0 && (*it3)[2]==0.0)
00209 *it3 = x[*it1];
00210 if( load->getDirection().isXNull() && load->getDirection().isYNull() && load->getDirection().isZNull() )
00211 *it3 = x[*it1];
00212
00213 if (load->getValue(time) != 0.0){
00214 if (load->getDirection().isToward()) {
00215 std::map<unsigned int, unsigned int>::const_iterator titi = atomToDOFIndexes.find(load->getDirection().getToward());
00216 if (titi != atomToDOFIndexes.end()){
00217 (*it2) = (*mmodel->getX())[titi->second] - (*mmodel->getX())[*it1];
00218 it2->normalize();
00219 }
00220 }
00221 x[*it1] = (*it3) + (*it2)*load->getValue(time);
00222 }
00223
00224 it1++;
00225 it2++;
00226 it3++;
00227 }
00228 }
00229 }
00230 }
00231
00232
00233
00234
00235 template<class DataTypes>
00236 void LMLConstraint<DataTypes>::draw()
00237 {
00238
00239 if (!getContext()->getShowBehaviorModels()) return;
00240
00241 VecCoord& x = *mmodel->getX();
00242 glDisable (GL_LIGHTING);
00243 glColor4f (1,0.5,0.5,1);
00244
00245 glPointSize(10);
00246
00247
00248 glBegin (GL_POINTS);
00249 VecDerivIterator it2 = directionsNULLs.begin();
00250 for (std::vector<unsigned int>::const_iterator it = this->targets.begin(); it != this->targets.end(); ++it)
00251 {
00252 if ((*it2)[0]==0 && (*it2)[1]==0 && (*it2)[2]==0 )
00253 helper::gl::glVertexT(x[*it]);
00254 it2++;
00255 }
00256 glEnd();
00257
00258
00259 glPointSize(1);
00260 glBegin( GL_LINES );
00261 VecDerivIterator it3 = translations.begin();
00262 it2 = directionsNULLs.begin();
00263 for (std::vector<unsigned int>::const_iterator it = this->targets.begin(); it != this->targets.end(); ++it)
00264 {
00265 if ((*it2)[0]==1 || (*it2)[1]==1 || (*it2)[2]==1 ) {
00266 helper::gl::glVertexT(x[*it]);
00267 helper::gl::glVertexT(x[*it]+*it3);
00268 }
00269 it3++;
00270 it2++;
00271 }
00272 glEnd();
00273
00274
00275 }
00276
00277 }
00278 }
00279 }
00280
00281 #endif //LMLCONSTRAINT_INL