FixedPlaneConstraint.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_FIXEDPLANECONSTRAINT_INL
00026 #define SOFA_COMPONENT_CONSTRAINT_FIXEDPLANECONSTRAINT_INL
00027
00028 #include <sofa/core/componentmodel/behavior/Constraint.inl>
00029 #include <sofa/component/constraint/FixedPlaneConstraint.h>
00030 #include <sofa/core/componentmodel/topology/BaseMeshTopology.h>
00031 #include <sofa/helper/gl/template.h>
00032 #include <sofa/defaulttype/RigidTypes.h>
00033
00034 namespace sofa
00035 {
00036
00037 namespace component
00038 {
00039
00040 namespace constraint
00041 {
00042 using namespace core::componentmodel::topology;
00043 using namespace sofa::defaulttype;
00044
00045 template <class DataTypes>
00046 FixedPlaneConstraint<DataTypes>::FixedPlaneConstraint()
00047 : indices( initData(&indices,"indices","Indices of the fixed points"))
00048 , direction( initData(&direction,"direction","normal direction of the plane"))
00049 , dmin( initData(&dmin,(Real)0,"dmin","Minimum plane distance from the origin"))
00050 , dmax( initData(&dmax,(Real)0,"dmax","Maximum plane distance from the origin") )
00051 {
00052 selectVerticesFromPlanes=false;
00053 }
00054
00055 template <class DataTypes>
00056 FixedPlaneConstraint<DataTypes>::~FixedPlaneConstraint()
00057 {
00058 }
00059
00060 template< class DataTypes>
00061 bool FixedPlaneConstraint<DataTypes>::FPCTestNewPointFunction(int , void* param, const helper::vector< unsigned int > &, const helper::vector< double >& )
00062 {
00063 FixedPlaneConstraint<DataTypes> *fc= (FixedPlaneConstraint<DataTypes> *)param;
00064 if (fc) {
00065 return true;
00066 }else{
00067 return false;
00068 }
00069 }
00070
00071
00072 template< class DataTypes>
00073 void FixedPlaneConstraint<DataTypes>::FPCRemovalFunction(int pointIndex, void* param)
00074 {
00075 FixedPlaneConstraint<DataTypes> *fc= (FixedPlaneConstraint<DataTypes> *)param;
00076 if (fc) {
00077 fc->removeConstraint((unsigned int) pointIndex);
00078 }
00079 return;
00080 }
00081 template <class DataTypes>
00082 FixedPlaneConstraint<DataTypes>* FixedPlaneConstraint<DataTypes>::addConstraint(int index)
00083 {
00084 indices.beginEdit()->push_back(index);
00085 return this;
00086 }
00087
00088 template <class DataTypes>
00089 FixedPlaneConstraint<DataTypes>* FixedPlaneConstraint<DataTypes>::removeConstraint(int )
00090 {
00091 return this;
00092 }
00093
00094
00095 template <class DataTypes>
00096 void FixedPlaneConstraint<DataTypes>::projectResponse(VecDeriv& res)
00097 {
00098 Coord dir=direction.getValue();
00099
00100 for (helper::vector< unsigned int >::const_iterator it = this->indices.getValue().begin(); it != this->indices.getValue().end(); ++it)
00101 {
00103 res[*it]-= dir*dot(res[*it],dir);
00104 }
00105
00106 }
00107 template <class DataTypes>
00108 void FixedPlaneConstraint<DataTypes>::setDirection(Coord dir)
00109 {
00110 if (dir.norm2()>0) {
00111 direction.setValue(dir);
00112 }
00113 }
00114
00115 template <class DataTypes>
00116 void FixedPlaneConstraint<DataTypes>::selectVerticesAlongPlane()
00117 {
00118 VecCoord& x = *this->mstate->getX();
00119 unsigned int i;
00120 for(i=0; i<x.size();++i) {
00121 if (isPointInPlane(x[i]))
00122 addConstraint(i);
00123 }
00124
00125 }
00126 template <class DataTypes>
00127 void FixedPlaneConstraint<DataTypes>::init()
00128 {
00129 this->core::componentmodel::behavior::Constraint<DataTypes>::init();
00130
00131 topology = getContext()->getMeshTopology();
00132
00134 if (dmin.getValue()!=dmax.getValue())
00135 selectVerticesFromPlanes=true;
00136
00137 if (selectVerticesFromPlanes)
00138 selectVerticesAlongPlane();
00139
00140 topology::PointSubset my_subset = indices.getValue();
00141
00142
00143 my_subset.setTestFunction(FPCTestNewPointFunction);
00144 my_subset.setRemovalFunction(FPCRemovalFunction);
00145
00146 my_subset.setTestParameter( (void *) this );
00147 my_subset.setRemovalParameter( (void *) this );
00148
00149 }
00150
00151
00152 template <class DataTypes> void FixedPlaneConstraint<DataTypes>::handleTopologyChange()
00153 {
00154 std::list<const TopologyChange *>::const_iterator itBegin=topology->firstChange();
00155 std::list<const TopologyChange *>::const_iterator itEnd=topology->lastChange();
00156
00157 indices.beginEdit()->handleTopologyEvents(itBegin,itEnd,this->getMState()->getSize());
00158
00159 }
00160
00161 template <class DataTypes>
00162 void FixedPlaneConstraint<DataTypes>::draw()
00163 {
00164 if (!getContext()->getShowBehaviorModels()) return;
00165 const VecCoord& x = *this->mstate->getX();
00166 glDisable (GL_LIGHTING);
00167 glPointSize(10);
00168 glColor4f (1,1.0,0.5,1);
00169 glBegin (GL_POINTS);
00170 for (helper::vector< unsigned int >::const_iterator it = this->indices.getValue().begin(); it != this->indices.getValue().end(); ++it)
00171 {
00172 helper::gl::glVertexT(x[*it]);
00173 }
00174 glEnd();
00175 }
00176
00177
00178 }
00179
00180 }
00181
00182 }
00183
00184 #endif