FixedConstraint.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_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
00058 template< class DataTypes>
00059 bool FixedConstraint<DataTypes>::FCTestNewPointFunction(int , 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
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
00087 f_indices.beginEdit()->push_back(0);
00088 f_indices.endEdit();
00089 }
00090
00091
00092
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
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
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
00154 if( f_fixAll.getValue()==true ) {
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
00169 template <class DataTypes>
00170 void FixedConstraint<DataTypes>::applyConstraint(defaulttype::BaseMatrix *mat, unsigned int &offset)
00171 {
00172
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
00179 for (unsigned int c=0;c<N;++c)
00180 mat->clearRowCol(offset + N * (*it) + c);
00181
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
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
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
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
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 }
00245
00246 }
00247
00248 }
00249
00250 #endif
00251
00252