FixedConstraint.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_CONSTRAINT_FIXEDCONSTRAINT_INL
00026 #define SOFA_COMPONENT_CONSTRAINT_FIXEDCONSTRAINT_INL
00027 
00028 #include <sofa/core/componentmodel/topology/BaseMeshTopology.h>
00029 #include <sofa/core/componentmodel/behavior/Constraint.inl>
00030 #include <sofa/component/constraint/FixedConstraint.h>
00031 #include <sofa/component/topology/PointSubset.h>
00032 
00033 #include <sofa/helper/gl/template.h>
00034 #include <sofa/defaulttype/RigidTypes.h>
00035 #include <iostream>
00036 
00037 
00038 using std::cerr;
00039 using std::endl;
00040 
00041 namespace sofa
00042 {
00043 
00044 namespace component
00045 {
00046 
00047 namespace constraint
00048 {
00049 
00050 using namespace core::componentmodel::topology;
00051 
00052 using namespace sofa::defaulttype;
00053 using namespace sofa::helper;
00054 using namespace sofa::core::componentmodel::behavior;
00055 
00056 
00057 // Define TestNewPointFunction
00058 template< class DataTypes>
00059 bool FixedConstraint<DataTypes>::FCTestNewPointFunction(int /*nbPoints*/, void* param, const sofa::helper::vector< unsigned int > &, const sofa::helper::vector< double >& )
00060 {
00061     FixedConstraint<DataTypes> *fc= (FixedConstraint<DataTypes> *)param;
00062     if (fc) {
00063         return true;
00064     }else{
00065         return false;
00066     }
00067 }
00068 
00069 // Define RemovalFunction
00070 template< class DataTypes>
00071 void FixedConstraint<DataTypes>::FCRemovalFunction(int pointIndex, void* param)
00072 {
00073     FixedConstraint<DataTypes> *fc= (FixedConstraint<DataTypes> *)param;
00074     if (fc) {
00075         fc->removeConstraint((unsigned int) pointIndex);
00076     }
00077     return;
00078 } 
00079 
00080 template <class DataTypes>
00081 FixedConstraint<DataTypes>::FixedConstraint()
00082 : core::componentmodel::behavior::Constraint<DataTypes>(NULL)
00083 , f_indices( initData(&f_indices,"indices","Indices of the fixed points") )
00084 , f_fixAll( initData(&f_fixAll,false,"fixAll","filter all the DOF to implement a fixed object") )
00085 {
00086     // default to indice 0
00087     f_indices.beginEdit()->push_back(0);
00088     f_indices.endEdit();
00089 }
00090 
00091 
00092 // Handle topological changes
00093 template <class DataTypes> void FixedConstraint<DataTypes>::handleTopologyChange()
00094 {   
00095     std::list<const TopologyChange *>::const_iterator itBegin=topology->firstChange();
00096     std::list<const TopologyChange *>::const_iterator itEnd=topology->lastChange();
00097 
00098     f_indices.beginEdit()->handleTopologyEvents(itBegin,itEnd,this->getMState()->getSize());
00099 
00100 }
00101 
00102 template <class DataTypes>
00103 FixedConstraint<DataTypes>::~FixedConstraint()
00104 {
00105 }
00106 
00107 template <class DataTypes>
00108 void FixedConstraint<DataTypes>::clearConstraints()
00109 {
00110     f_indices.beginEdit()->clear();
00111     f_indices.endEdit();
00112 }
00113 
00114 template <class DataTypes>
00115 void FixedConstraint<DataTypes>::addConstraint(unsigned int index)
00116 {
00117     f_indices.beginEdit()->push_back(index);
00118     f_indices.endEdit();
00119 }
00120 
00121 template <class DataTypes>
00122 void FixedConstraint<DataTypes>::removeConstraint(unsigned int index)
00123 {
00124     removeValue(*f_indices.beginEdit(),index);
00125     f_indices.endEdit();
00126 }
00127 
00128 // -- Constraint interface
00129 
00130 
00131 template <class DataTypes>
00132 void FixedConstraint<DataTypes>::init()
00133 {
00134     this->core::componentmodel::behavior::Constraint<DataTypes>::init();
00135 
00136     topology = getContext()->getMeshTopology();
00137 
00138     // Initialize functions and parameters
00139     topology::PointSubset my_subset = f_indices.getValue();
00140 
00141     my_subset.setTestFunction(FCTestNewPointFunction);
00142     my_subset.setRemovalFunction(FCRemovalFunction);
00143 
00144     my_subset.setTestParameter( (void *) this );
00145     my_subset.setRemovalParameter( (void *) this );
00146 
00147 }
00148 
00149 template <class DataTypes>
00150 void FixedConstraint<DataTypes>::projectResponse(VecDeriv& res)
00151 {
00152     const SetIndexArray & indices = f_indices.getValue().getArray();
00153   //std::cerr<<"FixedConstraint<DataTypes>::projectResponse, res.size()="<<res.size()<<endl;
00154     if( f_fixAll.getValue()==true ) {  // fix everyting
00155         for( unsigned i=0; i<res.size(); i++ )
00156             res[i] = Deriv();
00157     }
00158     else {
00159     for (SetIndexArray::const_iterator it = indices.begin();
00160         it != indices.end();
00161         ++it)
00162     {
00163         res[*it] = Deriv();
00164     }
00165     }
00166 }
00167 
00168 // Matrix Integration interface
00169 template <class DataTypes>
00170 void FixedConstraint<DataTypes>::applyConstraint(defaulttype::BaseMatrix *mat, unsigned int &offset)
00171 {
00172     //std::cout << "applyConstraint in Matrix with offset = " << offset << std::endl;
00173     const unsigned int N = Deriv::size();
00174     const SetIndexArray & indices = f_indices.getValue().getArray();
00175 
00176     for (SetIndexArray::const_iterator it = indices.begin(); it != indices.end(); ++it)
00177     {
00178         // Reset Fixed Row and Col
00179         for (unsigned int c=0;c<N;++c)
00180             mat->clearRowCol(offset + N * (*it) + c);
00181         // Set Fixed Vertex
00182         for (unsigned int c=0;c<N;++c)
00183             mat->set(offset + N * (*it) + c, offset + N * (*it) + c, 1.0);
00184     }
00185 }
00186 
00187 template <class DataTypes>
00188 void FixedConstraint<DataTypes>::applyConstraint(defaulttype::BaseVector *vect, unsigned int &offset)
00189 {
00190     //std::cout << "applyConstraint in Vector with offset = " << offset << std::endl;
00191     const unsigned int N = Deriv::size();
00192 
00193     const SetIndexArray & indices = f_indices.getValue().getArray();
00194     for (SetIndexArray::const_iterator it = indices.begin(); it != indices.end(); ++it)
00195     {
00196         for (unsigned int c=0;c<N;++c)
00197             vect->clear(offset + N * (*it) + c);
00198     }
00199 }
00200 
00201 
00202 template <class DataTypes>
00203 void FixedConstraint<DataTypes>::draw()
00204 {
00205     if (!getContext()->
00206             getShowBehaviorModels()) return;
00207     const VecCoord& x = *this->mstate->getX();
00208     //std::cerr<<"FixedConstraint<DataTypes>::draw(), x.size() = "<<x.size()<<endl;
00209     glDisable (GL_LIGHTING);
00210     glPointSize(10);
00211     glColor4f (1,0.5,0.5,1);
00212     glBegin (GL_POINTS);
00213     const SetIndexArray & indices = f_indices.getValue().getArray();
00214     //std::cerr<<"FixedConstraint<DataTypes>::draw(), indices = "<<indices<<endl;
00215     if( f_fixAll.getValue()==true ) for (unsigned i=0; i<x.size(); i++ )
00216         {
00217         gl::glVertexT(x[i]);
00218         }
00219     else for (SetIndexArray::const_iterator it = indices.begin();
00220         it != indices.end();
00221         ++it)
00222         {
00223         gl::glVertexT(x[*it]);
00224         }
00225     glEnd();
00226 }
00227 
00228 // Specialization for rigids
00229 #ifndef SOFA_FLOAT
00230 template <>
00231     void FixedConstraint<Rigid3dTypes >::draw();
00232 template <>
00233     void FixedConstraint<Rigid2dTypes >::draw();
00234 #endif
00235 #ifndef SOFA_DOUBLE
00236 template <>
00237     void FixedConstraint<Rigid3fTypes >::draw();
00238 template <>
00239     void FixedConstraint<Rigid2fTypes >::draw();
00240 #endif
00241 
00242 
00243 
00244 } // namespace constraint
00245 
00246 } // namespace component
00247 
00248 } // namespace sofa
00249 
00250 #endif
00251 
00252 

Generated on Sun Nov 23 02:40:01 2008 for Sofa by  doxygen 1.5.7.1