QuadBendingSprings.inl

Go to the documentation of this file.
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_FORCEFIELD_QUADBENDINGSPRINGS_INL
00026 #define SOFA_COMPONENT_FORCEFIELD_QUADBENDINGSPRINGS_INL
00027 
00028 #include <sofa/component/forcefield/QuadBendingSprings.h>
00029 #include <sofa/core/componentmodel/topology/BaseMeshTopology.h>
00030 #include <iostream>
00031 
00032 namespace sofa
00033 {
00034 
00035 namespace component
00036 {
00037 
00038 namespace forcefield
00039 {
00040 
00041 using namespace core::componentmodel::behavior;
00042 
00043 template<class DataTypes>
00044 QuadBendingSprings<DataTypes>::QuadBendingSprings()
00045 : dof(NULL)
00046 {
00047 }
00048 
00049 
00050 template<class DataTypes>
00051 QuadBendingSprings<DataTypes>::~QuadBendingSprings()
00052 {}
00053 
00054 template<class DataTypes>
00055 void QuadBendingSprings<DataTypes>::addSpring( unsigned a, unsigned b, std::set<IndexPair>& springSet )
00056 {
00057     IndexPair ab(a<b?a:b, a<b?b:a);
00058     if (springSet.find(ab) != springSet.end()) return;
00059     springSet.insert(ab);
00060     const VecCoord& x = *dof->getX();
00061     Real s = (Real)this->ks.getValue();
00062     Real d = (Real)this->kd.getValue();
00063     Real l = (x[a]-x[b]).norm();
00064     this->SpringForceField<DataTypes>::addSpring(a,b, s, d, l );
00065     //std::cout<<"=================================QuadBendingSprings<DataTypes>::addSpring "<<a<<", "<<b<<std::endl;
00066 }
00067 
00068 template<class DataTypes>
00069     void QuadBendingSprings<DataTypes>::registerEdge( IndexPair ab, IndexPair cd, std::map<IndexPair, IndexPair>& edgeMap, std::set<IndexPair>& springSet)
00070 {
00071     if (ab.first > ab.second)
00072     {
00073         ab = std::make_pair(ab.second,ab.first);
00074         cd = std::make_pair(cd.second,cd.first);
00075     }
00076     if (edgeMap.find(ab) == edgeMap.end())
00077     {
00078         edgeMap[ab] = cd;
00079     }
00080     else
00081     { // create a spring between the opposite
00082         IndexPair ef = edgeMap[ab];
00083         this->addSpring(cd.first, ef.first, springSet);
00084         this->addSpring(cd.second, ef.second, springSet);
00085     }
00086 }
00087 
00088 template<class DataTypes>
00089 void QuadBendingSprings<DataTypes>::init()
00090 {
00091     dof = dynamic_cast<MechanicalObject<DataTypes>*>( this->getContext()->getMechanicalState() );
00092     assert(dof);
00093     //std::cout<<"==================================QuadBendingSprings<DataTypes>::init(), dof size = "<<dof->getX()->size()<<std::endl;
00094 
00095     // Set the bending springs
00096 
00097     std::map< IndexPair, IndexPair > edgeMap;
00098     std::set< IndexPair > springSet;
00099 
00100     sofa::core::componentmodel::topology::BaseMeshTopology* topology = this->getContext()->getMeshTopology();
00101     assert( topology );
00102 
00103     const sofa::core::componentmodel::topology::BaseMeshTopology::SeqQuads& quads = topology->getQuads();
00104     //std::cout<<"==================================QuadBendingSprings<DataTypes>::init(), quads size = "<<quads.size()<<std::endl;
00105     for( unsigned i= 0; i<quads.size(); ++i )
00106     {
00107         const sofa::core::componentmodel::topology::BaseMeshTopology::Quad& face = quads[i];
00108         {
00109             registerEdge( std::make_pair(face[0], face[1]), std::make_pair(face[3], face[2]), edgeMap, springSet );
00110             registerEdge( std::make_pair(face[1], face[2]), std::make_pair(face[0], face[3]), edgeMap, springSet );
00111             registerEdge( std::make_pair(face[2], face[3]), std::make_pair(face[1], face[0]), edgeMap, springSet );
00112             registerEdge( std::make_pair(face[3], face[0]), std::make_pair(face[2], face[1]), edgeMap, springSet );
00113         }
00114     }
00115 
00116     // init the parent class
00117     StiffSpringForceField<DataTypes>::init();
00118 }
00119 
00120 
00121 } // namespace forcefield
00122 
00123 } // namespace component
00124 
00125 } // namespace sofa
00126 
00127 #endif

Generated on Sun Nov 23 03:03:54 2008 for Sofa by  doxygen 1.5.7.1