BarycentricContactMapper.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_COLLISION_BARYCENTRICCONTACTMAPPER_INL
00026 #define SOFA_COMPONENT_COLLISION_BARYCENTRICCONTACTMAPPER_INL
00027 
00028 #include <sofa/component/collision/BarycentricContactMapper.h>
00029 #include <sofa/simulation/tree/GNode.h>
00030 #include <sofa/simulation/tree/Simulation.h>
00031 #include <iostream>
00032 
00033 namespace sofa
00034 {
00035 
00036 namespace component
00037 {
00038 
00039 namespace collision
00040 {
00041 
00042 template < class TCollisionModel, class DataTypes >
00043 void BarycentricContactMapper<TCollisionModel,DataTypes>::cleanup()
00044 {
00045     if (mapping!=NULL)
00046     {
00047         simulation::tree::GNode* parent = dynamic_cast<simulation::tree::GNode*>(model->getContext());
00048         if (parent!=NULL)
00049         {
00050             simulation::tree::GNode* child = dynamic_cast<simulation::tree::GNode*>(mapping->getContext());
00051             child->removeObject(mapping->getTo());
00052             child->removeObject(mapping);
00053             parent->removeChild(child);
00054             delete mapping->getTo();
00055             delete mapping;
00056             delete child;
00057         mapping = NULL;
00058         }
00059     }
00060 }
00061 
00062 template < class TCollisionModel, class DataTypes >
00063 typename BarycentricContactMapper<TCollisionModel,DataTypes>::MMechanicalState* BarycentricContactMapper<TCollisionModel,DataTypes>::createMapping(const char* name)
00064 {
00065     if (model==NULL) return NULL;
00066     simulation::tree::GNode* parent = dynamic_cast<simulation::tree::GNode*>(model->getContext());
00067     if (parent==NULL)
00068     {
00069         std::cerr << "ERROR: BarycentricContactMapper only works for scenegraph scenes.\n";
00070         return NULL;
00071     }
00072     simulation::tree::GNode* child = new simulation::tree::GNode(name); parent->addChild(child); child->updateSimulationContext();
00073     MMechanicalState* mstate = new MMechanicalObject; child->addObject(mstate);
00074     //mapping = new MMapping(model->getMechanicalState(), mstate, model->getMeshTopology()); 
00075     //mapper = mapping->getMapper();
00076     mapper = new mapping::BarycentricMapperMeshTopology<InDataTypes, typename BarycentricContactMapper::DataTypes>(model->getMeshTopology());
00077     mapping = new MMapping(model->getMechanicalState(), mstate, mapper);
00078     child->addObject(mapping);
00079     return mstate;
00080 }
00081 
00082 template < class TCollisionModel, class DataTypes >
00083 void IdentityContactMapper<TCollisionModel,DataTypes>::cleanup()
00084 {
00085     if (mapping!=NULL)
00086     {
00087         simulation::tree::GNode* parent = dynamic_cast<simulation::tree::GNode*>(model->getContext());
00088         if (parent!=NULL)
00089         {
00090             simulation::tree::GNode* child = dynamic_cast<simulation::tree::GNode*>(mapping->getContext());
00091             child->removeObject(mapping->getTo());
00092             child->removeObject(mapping);
00093             parent->removeChild(child);
00094             delete mapping->getTo();
00095             delete mapping;
00096             delete child;
00097         mapping = NULL;
00098         }
00099     }
00100 }
00101 template < class TCollisionModel, class DataTypes >
00102 typename IdentityContactMapper<TCollisionModel,DataTypes>::MMechanicalState* IdentityContactMapper<TCollisionModel,DataTypes>::createMapping(const char* name)
00103 {
00104     if (model==NULL) return NULL;
00105     simulation::tree::GNode* parent = dynamic_cast<simulation::tree::GNode*>(model->getContext());
00106     if (parent==NULL)
00107     {
00108         std::cerr << "ERROR: IdentityContactMapper only works for scenegraph scenes.\n";
00109         return NULL;
00110     }
00111     simulation::tree::GNode* child = new simulation::tree::GNode(name); parent->addChild(child); child->updateSimulationContext();
00112     MMechanicalState* mstate = new MMechanicalObject; child->addObject(mstate);
00113     mapping = new MMapping(model->getMechanicalState(), mstate); child->addObject(mapping);
00114     return mstate;
00115 }
00116 
00117 template < class TCollisionModel, class DataTypes >
00118 void RigidContactMapper<TCollisionModel,DataTypes>::cleanup()
00119 {
00120     if (child!=NULL)
00121     {
00122         simulation::tree::getSimulation()->unload(child);
00123         child = NULL;
00124     }
00125 }
00126 
00127 template < class TCollisionModel, class DataTypes >
00128 typename RigidContactMapper<TCollisionModel,DataTypes>::MMechanicalState* RigidContactMapper<TCollisionModel,DataTypes>::createMapping(const char* name)
00129 {
00130     if (model==NULL) return NULL;
00131     InMechanicalState* instate = model->getMechanicalState();
00132     if (instate!=NULL)
00133     {
00134         simulation::tree::GNode* parent = dynamic_cast<simulation::tree::GNode*>(instate->getContext());
00135         if (parent==NULL)
00136         {
00137             std::cerr << "ERROR: RigidContactMapper only works for scenegraph scenes.\n";
00138             return NULL;
00139         }
00140         child = new simulation::tree::GNode(name); parent->addChild(child); child->updateSimulationContext();
00141         outmodel = new MMechanicalObject; child->addObject(outmodel);
00142         mapping = new MMapping(instate, outmodel); child->addObject(mapping);
00143     }
00144     else
00145     {
00146         simulation::tree::GNode* parent = dynamic_cast<simulation::tree::GNode*>(model->getContext());
00147         if (parent==NULL)
00148         {
00149             std::cerr << "ERROR: RigidContactMapper only works for scenegraph scenes.\n";
00150             return NULL;
00151         }
00152         child = new simulation::tree::GNode(name); parent->addChild(child); child->updateSimulationContext();
00153         outmodel = new MMechanicalObject; child->addObject(outmodel);
00154         mapping = NULL;
00155     }
00156     return outmodel;
00157 }
00158 
00159 template < class TCollisionModel, class DataTypes >
00160 void SubsetContactMapper<TCollisionModel,DataTypes>::cleanup()
00161 {
00162     if (child!=NULL)
00163     {
00164         simulation::tree::getSimulation()->unload(child);
00165     child = NULL;
00166     }
00167 }
00168     
00169 template < class TCollisionModel, class DataTypes >
00170 typename SubsetContactMapper<TCollisionModel,DataTypes>::MMechanicalState* SubsetContactMapper<TCollisionModel,DataTypes>::createMapping(const char* name)
00171 {
00172     if (model==NULL) return NULL;
00173     InMechanicalState* instate = model->getMechanicalState();
00174     if (instate!=NULL)
00175     {
00176         simulation::tree::GNode* parent = dynamic_cast<simulation::tree::GNode*>(instate->getContext());
00177         if (parent==NULL)
00178         {
00179             std::cerr << "ERROR: SubsetContactMapper only works for scenegraph scenes.\n";
00180             return NULL;
00181         }
00182         child = new simulation::tree::GNode(name); parent->addChild(child); child->updateSimulationContext();
00183         outmodel = new MMechanicalObject; child->addObject(outmodel);
00184         mapping = new MMapping(instate, outmodel); child->addObject(mapping);
00185     }
00186     else
00187     {
00188         simulation::tree::GNode* parent = dynamic_cast<simulation::tree::GNode*>(model->getContext());
00189         if (parent==NULL)
00190         {
00191             std::cerr << "ERROR: SubsetContactMapper only works for scenegraph scenes.\n";
00192             return NULL;
00193         }
00194         child = new simulation::tree::GNode(name); parent->addChild(child); child->updateSimulationContext();
00195         outmodel = new MMechanicalObject; child->addObject(outmodel);
00196         mapping = NULL;
00197     }
00198     return outmodel;
00199 }
00200 
00201 template <class DataTypes>
00202 typename ContactMapper<RigidDistanceGridCollisionModel,DataTypes>::MMechanicalState* ContactMapper<RigidDistanceGridCollisionModel,DataTypes>::createMapping(const char* name)
00203 {
00204     MMechanicalState* outmodel = Inherit::createMapping(name);
00205     if (this->child!=NULL && this->mapping==NULL)
00206     {
00207         // add velocity visualization
00208         sofa::component::visualmodel::DrawV* visu = new sofa::component::visualmodel::DrawV;
00209         this->child->addObject(visu);
00210         visu->useAlpha.setValue(true);
00211         visu->vscale.setValue(this->model->getContext()->getDt());
00212         sofa::component::mapping::IdentityMapping< core::Mapping< core::componentmodel::behavior::State<DataTypes>, core::componentmodel::behavior::MappedModel< ExtVectorTypes< Vec<3,GLfloat>, Vec<3,GLfloat> > > > >* map = new sofa::component::mapping::IdentityMapping< core::Mapping< core::componentmodel::behavior::State<DataTypes> , core::componentmodel::behavior::MappedModel< ExtVectorTypes< Vec<3,GLfloat>, Vec<3,GLfloat> > > > > ( outmodel, visu );
00213         this->child->addObject(map);
00214         visu->init();
00215         map->init();
00216     }
00217     return outmodel;
00218 }
00219 
00220 } // namespace collision
00221 
00222 } // namespace component
00223 
00224 } // namespace sofa
00225 
00226 #endif

Generated on Sun Nov 23 02:33:00 2008 for Sofa by  doxygen 1.5.7.1