TriangleBendingSprings.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 //
00026 // C++ Implementation: TriangleBendingSprings
00027 //
00028 // Description:
00029 //
00030 //
00031 // Author: The SOFA team </www.sofa-framework.org>, (C) 2007
00032 //
00033 // Copyright: See COPYING file that comes with this distribution
00034 //
00035 //
00036 #ifndef SOFA_COMPONENT_FORCEFIELD_TRIANGLEBENDINGSPRINGS_INL
00037 #define SOFA_COMPONENT_FORCEFIELD_TRIANGLEBENDINGSPRINGS_INL
00038 
00039 #include <sofa/component/forcefield/TriangleBendingSprings.h>
00040 #include <sofa/core/componentmodel/topology/BaseMeshTopology.h>
00041 #include <iostream>
00042 
00043 namespace sofa
00044 {
00045 
00046 namespace component
00047 {
00048 
00049 namespace forcefield
00050 {
00051 
00052 using namespace core::componentmodel::behavior;
00053 
00054 template<class DataTypes>
00055 TriangleBendingSprings<DataTypes>::TriangleBendingSprings()
00056 : dof(NULL)
00057 {
00058   //std::cerr<<"TriangleBendingSprings<DataTypes>::TriangleBendingSprings"<<std::endl;
00059 }
00060 
00061 
00062 template<class DataTypes>
00063 TriangleBendingSprings<DataTypes>::~TriangleBendingSprings()
00064 {}
00065 
00066 template<class DataTypes>
00067 void TriangleBendingSprings<DataTypes>::addSpring( unsigned a, unsigned b )
00068 {
00069     const VecCoord& x = *dof->getX();
00070     Real s = (Real)this->ks.getValue();
00071     Real d = (Real)this->kd.getValue();
00072     Real l = (x[a]-x[b]).norm();
00073     this->SpringForceField<DataTypes>::addSpring(a,b, s, d, l );
00074     //std::cout<<"=================================TriangleBendingSprings<DataTypes>::addSpring "<<a<<", "<<b<<std::endl;
00075 }
00076 
00077 template<class DataTypes>
00078     void TriangleBendingSprings<DataTypes>::registerTriangle( unsigned a, unsigned b, unsigned c, std::map<IndexPair, unsigned>& edgeMap)
00079 {
00080   //std::cout<<"=================================TriangleBendingSprings<DataTypes>::registerTriangle "<<a<<", "<<b<<", "<<c<<std::endl;
00081   using namespace std;
00082   {
00083         IndexPair edge(a<b ? a : b,a<b ? b : a);
00084         unsigned opposite = c;
00085         if( edgeMap.find( edge ) == edgeMap.end() )
00086         {
00087             edgeMap[edge] = opposite;
00088         }
00089         else
00090         { // create a spring between the opposite
00091             this->addSpring(opposite,edgeMap[edge]);
00092         }
00093     }
00094 
00095     {
00096         IndexPair edge(b<c ? b : c,b<c ? c : b);
00097         unsigned opposite = a;
00098         if( edgeMap.find( edge ) == edgeMap.end() )
00099         {
00100             edgeMap[edge] = opposite;
00101         }
00102         else
00103         { // create a spring between the opposite
00104             this->addSpring(opposite,edgeMap[edge]);
00105         }
00106     }
00107 
00108     {
00109         IndexPair edge(c<a ? c : a,c<a ? a : c);
00110         unsigned  opposite = b;
00111         if( edgeMap.find( edge ) == edgeMap.end() )
00112         {
00113             edgeMap[edge] = opposite;
00114         }
00115         else
00116         { // create a spring between the opposite
00117             this->addSpring(opposite,edgeMap[edge]);
00118         }
00119     }
00120 
00121 }
00122 
00123 
00124 
00125 template<class DataTypes>
00126 void TriangleBendingSprings<DataTypes>::init()
00127 {
00128     dof = dynamic_cast<MechanicalObject<DataTypes>*>( this->getContext()->getMechanicalState() );
00129     assert(dof);
00130     this->mstate1 = this->mstate2 = dof;
00131     StiffSpringForceField<DataTypes>::clear();
00132 
00133     // Set the bending springs
00134 
00135     std::map< IndexPair, unsigned > edgeMap;
00136     sofa::core::componentmodel::topology::BaseMeshTopology* topology = this->getContext()->getMeshTopology();
00137     assert( topology );
00138 
00139     const sofa::core::componentmodel::topology::BaseMeshTopology::SeqTriangles& triangles = topology->getTriangles();
00140     //std::cout<<"==================================TriangleBendingSprings<DataTypes>::init(), triangles size = "<<triangles.size()<<std::endl;
00141     for( unsigned i= 0; i<triangles.size(); ++i )
00142     {
00143         const sofa::core::componentmodel::topology::BaseMeshTopology::Triangle& face = triangles[i];
00144         {
00145             registerTriangle( face[0], face[1], face[2], edgeMap );
00146         }
00147 
00148     }
00149 
00150     const sofa::core::componentmodel::topology::BaseMeshTopology::SeqQuads& quads = topology->getQuads();
00151     //std::cout<<"==================================TriangleBendingSprings<DataTypes>::init(), quad size = "<<topology->getQuads().size()<<std::endl;
00152     for( unsigned i= 0; i<quads.size(); ++i )
00153     {
00154       const sofa::core::componentmodel::topology::BaseMeshTopology::Quad& face = quads[i];
00155       {
00156         registerTriangle( face[0], face[1], face[2], edgeMap );
00157         registerTriangle( face[0], face[2], face[3], edgeMap );
00158       }
00159 
00160     }
00161 
00162     // init the parent class
00163     StiffSpringForceField<DataTypes>::init();
00164 
00165 }
00166 
00167 
00168 } // namespace forcefield
00169 
00170 } // namespace component
00171 
00172 } // namespace sofa
00173 
00174 #endif

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