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 #ifndef SOFA_COMPONENT_MAPPING_CENTEROFMASSMAPPING_INL 00026 #define SOFA_COMPONENT_MAPPING_CENTEROFMASSMAPPING_INL 00027 00028 #include <sofa/component/mapping/CenterOfMassMapping.h> 00029 #include <sofa/defaulttype/VecTypes.h> 00030 #include <sofa/defaulttype/RigidTypes.h> 00031 #include <sofa/helper/gl/template.h> 00032 #include <sofa/core/componentmodel/behavior/MechanicalMapping.inl> 00033 #include <sofa/core/componentmodel/behavior/MechanicalState.h> 00034 #include <string> 00035 #include <iostream> 00036 00037 using std::cerr; 00038 using std::endl; 00039 00040 00041 namespace sofa 00042 { 00043 00044 namespace component 00045 { 00046 00047 namespace mapping 00048 { 00049 00050 using namespace sofa::defaulttype; 00051 00052 00053 00054 00055 template <class BasicMapping> 00056 void CenterOfMassMapping<BasicMapping>::init() 00057 { 00058 masses = dynamic_cast<BaseMass*> (this->fromModel->getContext()->getMass()); 00059 if(!masses) 00060 return; 00061 00062 totalMass = 0.0; 00063 00064 for (unsigned int i=0 ; i<this->fromModel->getX()->size() ; i++) 00065 totalMass += masses->getElementMass(i); 00066 00067 } 00068 00069 00070 template <class BasicMapping> 00071 void CenterOfMassMapping<BasicMapping>::apply ( typename Out::VecCoord& childPositions, const typename In::VecCoord& parentPositions ) 00072 { 00073 if(!masses || totalMass==0.0){ 00074 cerr<<"Error in CenterOfMassMapping : no mass found corresponding to the DOFs"<<endl; 00075 return; 00076 } 00077 00078 OutCoord outX; 00079 00080 for (unsigned int i=0 ; i<parentPositions.size() ; i++){ 00081 outX += parentPositions[i].getCenter() * masses->getElementMass(i); 00082 } 00083 00084 childPositions[0] = outX / totalMass; 00085 } 00086 00087 00088 template <class BasicMapping> 00089 void CenterOfMassMapping<BasicMapping>::applyJ ( typename Out::VecDeriv& childForces, const typename In::VecDeriv& parentForces ) 00090 { 00091 if(!masses || totalMass==0.0){ 00092 cerr<<"Error in CenterOfMassMapping : no mass found corresponding to the DOFs"<<endl; 00093 return; 00094 } 00095 00096 OutDeriv outF; 00097 00098 for (unsigned int i=0 ; i<parentForces.size() ; i++){ 00099 outF += parentForces[i].getVCenter() * masses->getElementMass(i); 00100 } 00101 00102 childForces[0] = outF / totalMass; 00103 00104 } 00105 00106 00107 template <class BasicMapping> 00108 void CenterOfMassMapping<BasicMapping>::applyJT ( typename In::VecDeriv& parentForces, const typename Out::VecDeriv& childForces ) 00109 { 00110 if(!masses || totalMass==0.0){ 00111 cerr<<"Error in CenterOfMassMapping : no mass found corresponding to the DOFs"<<endl; 00112 return; 00113 } 00114 00115 for (unsigned int i=0 ; i<parentForces.size() ; i++) 00116 parentForces[i].getVCenter() += childForces[0] * (masses->getElementMass(i) / totalMass); 00117 } 00118 00119 00120 00121 template <class BasicMapping> 00122 void CenterOfMassMapping<BasicMapping>::draw() 00123 { 00124 const typename Out::VecCoord &X = *this->toModel->getX(); 00125 glBegin (GL_LINES); 00126 glColor4f (1,1,0,1); 00127 for(unsigned int i=0 ; i<OutCoord::static_size ; i++){ 00128 OutCoord v; 00129 v[i] = (Real)0.1; 00130 helper::gl::glVertexT(X[0] -v); 00131 helper::gl::glVertexT(X[0] +v); 00132 } 00133 glEnd(); 00134 } 00135 00136 00137 } // namespace mapping 00138 00139 } // namespace component 00140 00141 } // namespace sofa 00142 00143 #endif
1.5.7.1