QuadBendingSprings.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 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
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 {
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
00094
00095
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
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
00117 StiffSpringForceField<DataTypes>::init();
00118 }
00119
00120
00121 }
00122
00123 }
00124
00125 }
00126
00127 #endif