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 #ifndef SOFA_COMPONENT_MAPPING_ARTICULATEDSYSTEMMAPPING_INL
00027 #define SOFA_COMPONENT_MAPPING_ARTICULATEDSYSTEMMAPPING_INL
00028
00029 #include <sofa/component/mapping/ArticulatedSystemMapping.h>
00030 #include <sofa/core/objectmodel/BaseContext.h>
00031 #include <sofa/helper/gl/template.h>
00032
00033 namespace sofa
00034 {
00035
00036 namespace component
00037 {
00038
00039 namespace mapping
00040 {
00041
00042 template <class BasicMapping>
00043 void ArticulatedSystemMapping<BasicMapping>::init()
00044 {
00045 std::cout<<"INIT"<<std::endl;
00046 GNode* context = dynamic_cast<GNode*>(this->fromModel->getContext());
00047 context->getNodeObject(ahc);
00048 articulationCenters = ahc->getArticulationCenters();
00049
00050 OutVecCoord& xto = *this->toModel->getX();
00051 InVecCoord& xfrom = *this->fromModel->getX();
00052
00053 ArticulationPos.clear();
00054 ArticulationAxis.clear();
00055 ArticulationPos.resize(xfrom.size());
00056 ArticulationAxis.resize(xfrom.size());
00057
00058 context->parent->getNodeObject(rootModel);
00059
00060 CoordinateBuf.clear();
00061 CoordinateBuf.resize(xfrom.size());
00062 for (unsigned int c=0; c<xfrom.size(); c++)
00063 {
00064 CoordinateBuf[c].x() = 0.0;
00065 }
00066
00067 vector<ArticulatedHierarchyContainer::ArticulationCenter*>::const_iterator ac = articulationCenters.begin();
00068 vector<ArticulatedHierarchyContainer::ArticulationCenter*>::const_iterator acEnd = articulationCenters.end();
00069
00070 for (; ac != acEnd; ac++)
00071 {
00072 (*ac)->OrientationArticulationCenter.clear();
00073 (*ac)->DisplacementArticulationCenter.clear();
00074 (*ac)->Disp_Rotation.clear();
00075
00076 std::cout<<"- (*ac)->OrientationArticulationCenter"<< (*ac)->OrientationArticulationCenter;
00077
00078
00079 }
00080
00081 apply(xto, xfrom);
00082
00083 }
00084
00085 template <class BasicMapping>
00086 void ArticulatedSystemMapping<BasicMapping>::reset()
00087 {
00088 init();
00089 }
00090
00091
00092
00093
00094 template <class BasicMapping>
00095 void ArticulatedSystemMapping<BasicMapping>::apply( typename Out::VecCoord& out, const typename In::VecCoord& in )
00096 {
00097
00098
00099
00100
00101
00102
00103
00104
00105 if (rootModel)
00106 {
00107
00108 out[0] = (*rootModel->getX())[rootModel->getSize()-1];
00109 }
00110
00111 vector<ArticulatedHierarchyContainer::ArticulationCenter*>::const_iterator ac = articulationCenters.begin();
00112 vector<ArticulatedHierarchyContainer::ArticulationCenter*>::const_iterator acEnd = articulationCenters.end();
00113
00114 for (; ac != acEnd; ac++)
00115 {
00116 int parent = (*ac)->parentIndex.getValue();
00117 int child = (*ac)->childIndex.getValue();
00118
00119
00120
00121
00122 Quat quat_child_buf = out[child].getOrientation();
00123
00124
00125
00126
00127
00128 (*ac)->globalPosition.setValue(out[parent].getCenter() +
00129 out[parent].getOrientation().rotate((*ac)->posOnParent.getValue()));
00130
00131 vector<ArticulatedHierarchyContainer::ArticulationCenter::Articulation*> articulations = (*ac)->getArticulations();
00132 vector<ArticulatedHierarchyContainer::ArticulationCenter::Articulation*>::const_iterator a = articulations.begin();
00133 vector<ArticulatedHierarchyContainer::ArticulationCenter::Articulation*>::const_iterator aEnd = articulations.end();
00134
00135 int process = (*ac)->articulationProcess.getValue();
00136
00137 switch(process) {
00138 case 0:
00139
00140 {
00141
00142 out[child].getOrientation() = out[parent].getOrientation();
00143 out[child].getCenter() = out[parent].getCenter() + (*ac)->initTranslateChild(out[parent].getOrientation());
00144
00145 Vec<3,OutReal> APos;
00146 APos = (*ac)->globalPosition.getValue();
00147 for (; a != aEnd; a++)
00148 {
00149 int ind = (*a)->articulationIndex.getValue();
00150 InCoord value = in[ind];
00151 Vec<3,Real> axis = out[child].getOrientation().rotate((*a)->axis.getValue());
00152 ArticulationAxis[ind] = axis;
00153
00154 if ((*a)->rotation.getValue())
00155 {
00156 Quat dq;
00157 dq.axisToQuat(axis, value.x());
00158 out[child].getCenter() += (*ac)->translateChild(dq, out[child].getOrientation());
00159 out[child].getOrientation() += dq;
00160
00161 }
00162 if ((*a)->translation.getValue())
00163 {
00164 out[child].getCenter() += axis*value.x();
00165 APos += axis*value.x();
00166 }
00167
00168 ArticulationPos[ind]= APos;
00169 }
00170 break;
00171 }
00172 case 1:
00173 {
00174
00175
00176
00177 for (; a != aEnd; a++)
00178 {
00179 int ind = (*a)->articulationIndex.getValue();
00180 InCoord value = in[ind];
00181 InCoord prev_value = CoordinateBuf[ind];
00182 Vec<3,Real> axis = out[parent].getOrientation().rotate((*a)->axis.getValue());
00183 ArticulationAxis[ind]=axis;
00184
00185
00186 if ((*a)->rotation.getValue() )
00187 {
00188 Quat r;
00189 r.axisToQuat(axis, value.x() - prev_value.x());
00190
00191 (*ac)->OrientationArticulationCenter+=r;
00192
00193 }
00194 if ((*a)->translation.getValue())
00195 {
00196 (*ac)->DisplacementArticulationCenter+=axis*(value.x() - prev_value.x());
00197 }
00198
00199 }
00200
00202
00203
00204
00205
00206 (*ac)->globalPosition.setValue(out[parent].getCenter() + out[parent].getOrientation().rotate((*ac)->posOnParent.getValue()) + (*ac)->DisplacementArticulationCenter);
00207 vector<ArticulatedHierarchyContainer::ArticulationCenter::Articulation*>::const_iterator a = articulations.begin();
00208
00209 for (; a != aEnd; a++)
00210 {
00211 Vec<3,OutReal> APos;
00212 APos = (*ac)->globalPosition.getValue();
00213 ArticulationPos[(*a)->articulationIndex.getValue()]=APos;
00214 }
00215
00216
00217 out[child].getOrientation() = out[parent].getOrientation() + (*ac)->OrientationArticulationCenter;
00218 out[child].getCenter() = (*ac)->globalPosition.getValue() - out[child].getOrientation().rotate( (*ac)->posOnChild.getValue() );
00219
00220 break;
00221
00222 }
00223
00224
00225 case 2:
00226 {
00227
00228
00229
00230 Vec<3,Real> disp(0,0,0);
00231
00232
00233 for (; a != aEnd; a++)
00234 {
00235 int ind = (*a)->articulationIndex.getValue();
00236 InCoord value = in[ind];
00237 InCoord prev_value = CoordinateBuf[ind];
00238 Vec<3,Real> axis = quat_child_buf.rotate((*a)->axis.getValue());
00239 ArticulationAxis[ind]=axis;
00240
00241
00242
00243
00244
00245
00246 if ((*a)->rotation.getValue() )
00247 {
00248 Quat r;
00249 r.axisToQuat(axis, value.x() - prev_value.x());
00250
00251 (*ac)->OrientationArticulationCenter+=r;
00252 }
00253 if ((*a)->translation.getValue())
00254 {
00255 disp += axis*(value.x()) ;
00256
00257 }
00258
00261 Vec<3,OutReal> APos;
00262 APos = (*ac)->globalPosition.getValue();
00263 ArticulationPos[(*a)->articulationIndex.getValue()]=APos;
00264
00265 }
00266 (*ac)->DisplacementArticulationCenter=disp;
00267
00268 out[child].getOrientation() = out[parent].getOrientation() + (*ac)->OrientationArticulationCenter;
00269 out[child].getCenter() = (*ac)->globalPosition.getValue() - out[child].getOrientation().rotate((*ac)->posOnChild.getValue());
00270 out[child].getCenter() += (*ac)->DisplacementArticulationCenter;
00271
00272 break;
00273
00274 }
00275
00276 }
00277 }
00278
00280
00281 CoordinateBuf.clear();
00282 CoordinateBuf.resize(in.size());
00283 for (unsigned int c=0; c<in.size(); c++)
00284 {
00285 CoordinateBuf[c].x() = in[c].x();
00286 }
00287
00288
00289 }
00290
00291
00292
00293 template <class BasicMapping>
00294 void ArticulatedSystemMapping<BasicMapping>::applyJ( typename Out::VecDeriv& out, const typename In::VecDeriv& in, const typename InRoot::VecDeriv* inroot )
00295 {
00296
00297
00298 OutVecCoord& xto = *this->toModel->getX();
00299
00300 out.clear();
00301 out.resize(xto.size());
00302
00303
00304 if (inroot)
00305 {
00306
00307 out[0] = (*inroot)[inroot->size()-1];
00308 }
00309 else
00310 out[0] = OutDeriv();
00311
00312
00313
00314 vector<ArticulatedHierarchyContainer::ArticulationCenter*>::const_iterator ac = articulationCenters.begin();
00315 vector<ArticulatedHierarchyContainer::ArticulationCenter*>::const_iterator acEnd = articulationCenters.end();
00316
00317 int i = 0;
00318
00319 for (; ac != acEnd; ac++)
00320 {
00321 int parent = (*ac)->parentIndex.getValue();
00322 int child = (*ac)->childIndex.getValue();
00323
00324 out[child].getVOrientation() += out[parent].getVOrientation();
00325 Vec<3,OutReal> P = xto[parent].getCenter();
00326 Vec<3,OutReal> C = xto[child].getCenter();
00327 out[child].getVCenter() = out[parent].getVCenter() + cross(P-C, out[parent].getVOrientation());
00328
00329
00330 vector<ArticulatedHierarchyContainer::ArticulationCenter::Articulation*> articulations = (*ac)->getArticulations();
00331 vector<ArticulatedHierarchyContainer::ArticulationCenter::Articulation*>::const_iterator a = articulations.begin();
00332 vector<ArticulatedHierarchyContainer::ArticulationCenter::Articulation*>::const_iterator aEnd = articulations.end();
00333
00334 for (; a != aEnd; a++)
00335 {
00336 int ind = (*a)->articulationIndex.getValue();
00337 InCoord value = in[ind];
00338 Vec<3,OutReal> axis = ArticulationAxis[ind];
00339 Vec<3,OutReal> A = ArticulationPos[ind];
00340
00341
00342 if ((*a)->rotation.getValue())
00343 {
00344 out[child].getVCenter() += cross(A-C, axis*value.x());
00345 out[child].getVOrientation() += axis*value.x();
00346 }
00347 if ((*a)->translation.getValue())
00348 {
00349 out[child].getVCenter() += axis*value.x();
00350 }
00351 i++;
00352
00353 }
00354 }
00355 }
00356
00357 template <class BasicMapping>
00358 void ArticulatedSystemMapping<BasicMapping>::applyJT( typename In::VecDeriv& out, const typename Out::VecDeriv& in, typename InRoot::VecDeriv* outroot )
00359 {
00360
00361
00362 OutVecCoord& xto = *this->toModel->getX();
00363
00364
00365
00366
00367
00368 OutVecDeriv fObjects6DBuf = in;
00369
00370 vector<ArticulatedHierarchyContainer::ArticulationCenter*>::const_iterator ac = articulationCenters.end();
00371 vector<ArticulatedHierarchyContainer::ArticulationCenter*>::const_iterator acBegin = articulationCenters.begin();
00372
00373 int i=ArticulationAxis.size();
00374 while (ac != acBegin)
00375 {
00376 ac--;
00377 int parent = (*ac)->parentIndex.getValue();
00378 int child = (*ac)->childIndex.getValue();
00379
00380 fObjects6DBuf[parent].getVCenter() += fObjects6DBuf[child].getVCenter();
00381 Vec<3,OutReal> P = xto[parent].getCenter();
00382 Vec<3,OutReal> C = xto[child].getCenter();
00383 fObjects6DBuf[parent].getVOrientation() += fObjects6DBuf[child].getVOrientation() + cross(C-P, fObjects6DBuf[child].getVCenter());
00384
00385
00386 vector<ArticulatedHierarchyContainer::ArticulationCenter::Articulation*> articulations = (*ac)->getArticulations();
00387
00388 vector<ArticulatedHierarchyContainer::ArticulationCenter::Articulation*>::const_iterator a = articulations.end();
00389 vector<ArticulatedHierarchyContainer::ArticulationCenter::Articulation*>::const_iterator aBegin = articulations.begin();
00390
00391 while (a != aBegin)
00392 {
00393 a--;
00394 i--;
00395 int ind = (*a)->articulationIndex.getValue();
00396 Vec<3,OutReal> axis = ArticulationAxis[ind];
00397 Vec<3,Real> A = ArticulationPos[ind] ;
00398 OutDeriv T;
00399 T.getVCenter() = fObjects6DBuf[child].getVCenter();
00400 T.getVOrientation() = fObjects6DBuf[child].getVOrientation() + cross(C-A, fObjects6DBuf[child].getVCenter());
00401
00402
00403
00404 if ((*a)->rotation.getValue())
00405 {
00406 out[ind].x() += (Real)dot(axis, T.getVOrientation());
00407 }
00408 if ((*a)->translation.getValue())
00409 {
00410 out[ind].x() += (Real)dot(axis, T.getVCenter());
00411 }
00412 }
00413 }
00414
00415 if (outroot)
00416 {
00417 (*outroot)[outroot->size()-1] += fObjects6DBuf[0];
00418 }
00419
00420
00421
00422 }
00423
00424 template <class BasicMapping>
00425 void ArticulatedSystemMapping<BasicMapping>::applyJT( typename In::VecConst& out, const typename Out::VecConst& in, typename InRoot::VecConst* outRoot )
00426 {
00427 std::cout<<" ApplyJT const - size in="<< in.size() <<std::endl;
00428
00429 OutVecCoord& xto = *this->toModel->getX();
00430
00431 out.resize(in.size());
00432
00433 if (rootModel!=NULL)
00434 outRoot->resize(in.size());
00435
00436
00437 for(unsigned int i=0; i<in.size(); i++)
00438 {
00439 for (unsigned int j=0;j<in[i].size();j++)
00440 {
00441 const OutSparseDeriv cIn = in[i][j];
00442 int childIndex = cIn.index;
00443 const OutDeriv valueConst = (OutDeriv) cIn.data;
00444 Vec<3,OutReal> C = xto[childIndex].getCenter();
00445 vector<ArticulatedHierarchyContainer::ArticulationCenter*> ACList = ahc->getAcendantList(childIndex);
00446
00447 vector<ArticulatedHierarchyContainer::ArticulationCenter*>::const_iterator ac = ACList.begin();
00448 vector<ArticulatedHierarchyContainer::ArticulationCenter*>::const_iterator acEnd = ACList.end();
00449
00450
00451 int ii=0;
00452
00453 for (; ac != acEnd; ac++)
00454 {
00455
00456 vector<ArticulatedHierarchyContainer::ArticulationCenter::Articulation*> articulations = (*ac)->getArticulations();
00457 vector<ArticulatedHierarchyContainer::ArticulationCenter::Articulation*>::const_iterator a = articulations.begin();
00458 vector<ArticulatedHierarchyContainer::ArticulationCenter::Articulation*>::const_iterator aEnd = articulations.end();
00459
00460
00461 for (; a != aEnd; a++)
00462 {
00463 int ind= (*a)->articulationIndex.getValue();
00464 InSparseDeriv constArt;
00465 constArt.index =ind ;
00466
00467
00468 Vec<3,OutReal> axis = ArticulationAxis[ind];
00469 Vec<3,Real> A = ArticulationPos[ind] ;
00470 OutDeriv T;
00471 T.getVCenter() = valueConst.getVCenter();
00472 T.getVOrientation() = valueConst.getVOrientation() + cross(C - A, valueConst.getVCenter());
00473
00474
00475
00476 if ((*a)->rotation.getValue())
00477 {
00478 constArt.data = (Real)dot(axis, T.getVOrientation());
00479 }
00480 if ((*a)->translation.getValue())
00481 {
00482 constArt.data = (Real)dot(axis, T.getVCenter());
00483
00484 }
00485 out[i].push_back(constArt);
00486 ii++;
00487 }
00488 }
00489
00490 if (rootModel!=NULL)
00491 {
00492 Vec<3,OutReal> posRoot = xto[0].getCenter();
00493 OutDeriv T;
00494 T.getVCenter() = valueConst.getVCenter();
00495 T.getVOrientation() = valueConst.getVOrientation() + cross(C - posRoot, valueConst.getVCenter());
00496 unsigned int indexT = 7;
00497 OutSparseDeriv constraintT(indexT, T);
00498
00499 (*outRoot)[i].push_back(constraintT);
00500 std::cout<< "constraintT = data : "<< T << "index : "<< indexT<<std::endl;
00501 }
00502
00503
00504
00505 }
00506 }
00507
00508
00509 std::cout<<"End ApplyJT const"<<std::endl;
00510
00511 }
00512
00513
00514
00515 template <class BasicMapping>
00516 void ArticulatedSystemMapping<BasicMapping>::propagateV()
00517 {
00518 if (this->fromModel!=NULL && this->toModel->getV()!=NULL && this->fromModel->getV()!=NULL)
00519 applyJ(*this->toModel->getV(), *this->fromModel->getV(), (rootModel==NULL ? NULL : rootModel->getV()));
00520 }
00521
00522
00523
00524 template <class BasicMapping>
00525 void ArticulatedSystemMapping<BasicMapping>::propagateDx()
00526 {
00527 if (this->fromModel!=NULL && this->toModel->getDx()!=NULL && this->fromModel->getDx()!=NULL)
00528 applyJ(*this->toModel->getDx(), *this->fromModel->getDx(), (rootModel==NULL ? NULL : rootModel->getDx()));
00529 }
00530
00531
00532
00533 template <class BasicMapping>
00534 void ArticulatedSystemMapping<BasicMapping>::accumulateForce()
00535 {
00536 if (this->fromModel!=NULL && this->toModel->getF()!=NULL && this->fromModel->getF()!=NULL)
00537 applyJT(*this->fromModel->getF(), *this->toModel->getF(), (rootModel==NULL ? NULL : rootModel->getF()));
00538 }
00539
00540
00541
00542 template <class BasicMapping>
00543 void ArticulatedSystemMapping<BasicMapping>::accumulateDf()
00544 {
00545 if (this->fromModel!=NULL && this->toModel->getF()!=NULL && this->fromModel->getF()!=NULL)
00546 applyJT(*this->fromModel->getF(), *this->toModel->getF(), (rootModel==NULL ? NULL : rootModel->getF()));
00547 }
00548
00549
00550
00551 template <class BasicMapping>
00552 void ArticulatedSystemMapping<BasicMapping>::accumulateConstraint()
00553 {
00554 if (this->fromModel!=NULL && this->toModel->getC()!=NULL && this->fromModel->getC()!=NULL)
00555 {
00556 applyJT(*this->fromModel->getC(), *this->toModel->getC(), (rootModel==NULL ? NULL : rootModel->getC()));
00557
00558
00559 std::vector<unsigned int>::iterator it = this->toModel->getConstraintId().begin();
00560 std::vector<unsigned int>::iterator itEnd = this->toModel->getConstraintId().end();
00561
00562 while (it != itEnd)
00563 {
00564 this->fromModel->setConstraintId(*it);
00565
00566
00567 if (rootModel!=NULL)
00568 rootModel->setConstraintId(*it);
00569 it++;
00570 }
00571 }
00572 }
00573
00574
00575
00576 template <class BasicMapping>
00577 void ArticulatedSystemMapping<BasicMapping>::draw()
00578 {
00579
00580 if (!this->getShow()) return;
00581 glDisable (GL_LIGHTING);
00582 glPointSize(10);
00583 vector<ArticulatedHierarchyContainer::ArticulationCenter*>::const_iterator ac = articulationCenters.begin();
00584 vector<ArticulatedHierarchyContainer::ArticulationCenter*>::const_iterator acEnd = articulationCenters.end();
00585 unsigned int i=0;
00586 for (; ac != acEnd; ac++)
00587 {
00588
00589
00590 vector<ArticulatedHierarchyContainer::ArticulationCenter::Articulation*> articulations = (*ac)->getArticulations();
00591 vector<ArticulatedHierarchyContainer::ArticulationCenter::Articulation*>::const_iterator a = articulations.begin();
00592 vector<ArticulatedHierarchyContainer::ArticulationCenter::Articulation*>::const_iterator aEnd = articulations.end();
00593 for (; a != aEnd; a++)
00594 {
00595 glBegin (GL_POINTS);
00596 glColor4f (1,0.5,0.5,1);
00597
00598 int ind= (*a)->articulationIndex.getValue();
00599 helper::gl::glVertexT(ArticulationPos[ind]);
00600 glEnd();
00601 glBegin(GL_LINES);
00602 glColor4f(0,0,1,1);
00603 helper::gl::glVertexT(ArticulationPos[ind]);
00604 Vec<3,OutReal> Pos_axis = ArticulationPos[ind] + ArticulationAxis[ind];
00605 helper::gl::glVertexT(Pos_axis);
00606
00607 glEnd();
00608
00609 i++;
00610 }
00611 }
00612 glPointSize(1);
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622 }
00623
00624 }
00625
00626 }
00627
00628 }
00629
00630 #endif