Loading...

How to implement an animation model different from the visual model ?

Screenshot : the liver - visual model - immersing in one tetrahedron - the animation model.
Screenshot : the liver - visual model - immersing in one tetrahedron - the animation model.

The code

The code relative to this example is located in : sofa/applications/tutorials/oneTetrahedron

There is just a Main.cpp with less than 100 lines. Open and observe it.


The scene graph

The scene graph used for this example
The scene graph used for this example
  • See OneParticule tutorial to know how to get this graph in SOFA


More comments

  • The graph root node:
    sofa::simulation::tree::GNode* groot = new sofa::simulation::tree::GNode;
    ...
    
  • One solver for all the graph
    sofa::component::odesolver::CGImplicitSolver* solver =
        new sofa::component::odesolver::CGImplicitSolver;
    ...
    
  • Set gravity for all the graph
    sofa::component::context::Gravity* gravity =
        new sofa::component::context::Gravity;
    ...
    
  • Tetrahedron degrees of freedom: set the geometric values of the 4 vertices
    sofa::component::MechanicalObject<MyTypes>* DOF =
        new sofa::component::MechanicalObject<MyTypes>;
    ...
    
  • Tetrahedron uniform mass: set an uniform mass onto the tetrahedron
    sofa::component::mass::UniformMass<MyTypes,double>* mass =
        new sofa::component::mass::UniformMass<MyTypes,double>;
    ...
    
  • Tetrahedron topology: the tetrahedron itself
    sofa::component::topology::MeshTopology* topology =
        new sofa::component::topology::MeshTopology;
    ...
    
  • Tetrahedron constraints: fixe the tetrahedron on the top
    sofa::component::constraint::FixedConstraint<MyTypes>*
    constraints =
        new sofa::component::constraint::FixedConstraint<MyTypes>;
    ...
    
  • Tetrahedron force field, the animation model : simulate the tetrahedron as a deformable FEM element
    sofa::component::forcefield::TetrahedronFEMForceField<MyTypes>* fem =
        new sofa::component::forcefield::TetrahedronFEMForceField<MyTypes>;
    ...
    
  • Tetrahedron skin: a children node containing the liver
    sofa::simulation::tree::GNode* skin = new sofa::simulation::tree::GNode;
    ...
    
  • The visual model: the liver
    sofa::component::visualmodel::OglModel* visual
        = new sofa::component::visualmodelOglModel;
    visual->load("../../../scenes/VisualModels/liver-smooth.obj", "", "");
    
  • The mapping between the tetrahedron (DOF) and the liver (visual) in order that the liver follow the tetrahedron animation
    MyMapping* mapping = new MyMapping(DOF, visual);
    ...
    


The mapping type between the visual and animation models

Here a BarycentricMapping is used to map the visual model to the animation model. This type is templated and you can find some examples of uses at the end of the file sofa/modules/sofa/component/mapping/BarycentricMapping.cpp

By analogy, we have built MyMapping type :

typedef sofa::component::mapping::BarycentricMapping<
    sofa::core::Mapping<
        sofa::core::componentmodel::behavior::MechanicalModel<MyTypes>,
        sofa::core::componentmodel::behavior::MappedModel< 
            sofa::defaulttype::ExtVectorTypes< 
                sofa::defaulttype::Vec<3,GLfloat>,
                sofa::defaulttype::Vec<3,GLfloat>
            >
        >
    >
> MyMapping;