Computer Assited Medical Intervention Tool Kit  version 5.0
SofaSimulator.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * $CAMITK_LICENCE_BEGIN$
3  *
4  * CamiTK - Computer Assisted Medical Intervention ToolKit
5  * (c) 2001-2021 Univ. Grenoble Alpes, CNRS, Grenoble INP, TIMC, 38000 Grenoble, France
6  *
7  * Visit http://camitk.imag.fr for more information
8  *
9  * This file is part of CamiTK.
10  *
11  * CamiTK is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * CamiTK is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License version 3 for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * version 3 along with CamiTK. If not, see <http://www.gnu.org/licenses/>.
22  *
23  * $CAMITK_LICENCE_END$
24  ****************************************************************************/
25 
26 
27 #ifndef SIMULATOR_SIMULATORS_SOFA_SOFASIMULATOR_H
28 #define SIMULATOR_SIMULATORS_SOFA_SOFASIMULATOR_H
29 
30 #include "InteractiveSimulator.h"
31 
32 #include <sofa/simulation/tree/TreeSimulation.h>
33 #include <sofa/component/container/MechanicalObject.h>
34 #include <sofa/defaulttype/VecTypes.h>
35 #include <sofa/simulation/common/Node.h>
36 #include <sofa/simulation/tree/GNode.h>
37 
38 // simplification of sofa interface
39 #define SofaVector sofa::helper::vector
40 
41 // Loads for Sofa
42 #include "TranslationConstraint.h"
43 
45 namespace std {
47 typedef pair<unsigned int, unsigned int> MechanicalObjectDOFIndex;
51 typedef map <unsigned int, MechanicalObjectDOFIndex> AtomDOFMap;
55 typedef map<unsigned int, unsigned int> MechanicalObjectAtomDOFMap;
59 typedef map<unsigned int, unsigned int> MechanicalObjectDOFAtomMap;
60 }
61 
70 
71 public:
73 
75 
76  virtual ~SofaSimulator();
77 
80  void doMove(double dt);
81  void init();
82  void getPosition(int index, double position[3]);
83  void getForce(int index, double force[3]);
84  void end() {}
85 
86  void createPml(const char* inputFile, const char* pmlFile);
88 
89  std::string getScnFile();
90 
91 private:
93  sofa::simulation::Node* getGNode();
94 
97 
100 
102  sofa::defaulttype::Vec3Types::Coord getDOFPosition(unsigned int atomIndex);
103 
105  sofa::defaulttype::Vec3Types::Coord getDOFPosition(unsigned int mechObjectIndex, unsigned int dofIndex);
106 
108  sofa::defaulttype::Vec3Types::Deriv getDOFForce(unsigned int atomIndex);
109 
111  sofa::defaulttype::Vec3Types::Deriv getDOFForce(unsigned int mechObjectIndex, unsigned int dofIndex);
112 
114  unsigned int getAtomIndex(unsigned int mechObjectIndex, unsigned int dofIndex);
115 
117  sofa::component::container::MechanicalObject<sofa::defaulttype::Vec3Types>* getMechanicalObject(unsigned int mechObjectIndex);
118 
120  unsigned int getNumberOfMechanicalObjects();
121 
123  sofa::defaulttype::Vec3Types::VecCoord getMechanicalObjectDOFPosition(unsigned int mechObjectIndex);
124 
126  sofa::defaulttype::Vec3Types::VecDeriv getMechanicalObjectDOFForce(unsigned int mechObjectIndex);
127 
129  void build();
130 
132  void buildConstraints();
133 
135 #if defined(SOFA_1_0_RC1) || defined(SOFA_SVN) || defined(SOFA_STABLE)
136  sofa::simulation::Node::SPtr groot;
137 #endif
138 
139 #ifdef MML_SOFA_1_0_BETA4
140  sofa::simulation::Node* groot;
141 #endif
142 
144  SofaVector<BaseMechanicalState*> mechanicalObjects;
145 
148 
150  std::vector<std::MechanicalObjectAtomDOFMap*> mechanicalObjectAtomDOFMap;
151 
153  std::vector<std::MechanicalObjectDOFAtomMap*> mechanicalObjectDOFAtomMap;
154 
156  std::vector<TranslationConstraint<sofa::defaulttype::Vec3Types> *> translations;
157 
159  std::string scnFile;
160 };
161 
162 // -------------------- getGNode --------------------
163 inline sofa::simulation::Node* SofaSimulator::getGNode() {
164  if (groot == nullptr) {
165  //-- TODO build a sofaGRoot using the nodes
166  }
167 
168 #if defined(SOFA_1_0_RC1) || defined(SOFA_SVN) || defined(SOFA_STABLE)
169  return groot.get();
170 #endif
171 
172 #ifdef MML_SOFA_1_0_BETA4
173  return groot;
174 #endif
175 }
176 
177 // -------------------- getNumberOfMechanicalObjects --------------------
179  return mechanicalObjects.size();
180 }
181 
182 // -------------------- getMechanicalObjectAtomDOFMap --------------------
184  return (*mechanicalObjectAtomDOFMap[mechObjectIndex]);
185 }
186 
187 // -------------------- getMechanicalObjectDOFAtomMap --------------------
189  return (*mechanicalObjectDOFAtomMap[mechObjectIndex]);
190 }
191 
192 // -------------------- getMechanicalObjectDOFPosition --------------------
193 inline sofa::defaulttype::Vec3Types::VecCoord SofaSimulator::getMechanicalObjectDOFPosition(unsigned int mechObjectIndex) {
194  return (*getMechanicalObject(mechObjectIndex)->getX());
195 }
196 
197 // -------------------- getMechanicalObjectDOFForce --------------------
198 inline sofa::defaulttype::Vec3Types::VecDeriv SofaSimulator::getMechanicalObjectDOFForce(unsigned int mechObjectIndex) {
199  return (*getMechanicalObject(mechObjectIndex)->getF());
200 }
201 
202 // -------------------- getScnFile --------------------
203 inline std::string SofaSimulator::getScnFile() {
204  return scnFile;
205 }
206 
207 #endif // SIMULATOR_SIMULATORS_SOFA_SOFASIMULATOR_H
std::MechanicalObjectDOFIndex
pair< unsigned int, unsigned int > MechanicalObjectDOFIndex
definition of a couple (=STL pair) [unsigned int mecObjectIndex, unsigned int dofIndex]
Definition: SofaSimulator.h:47
SimulatorFactory
A factory to create Simulator.
Definition: SimulatorFactory.h:40
SofaSimulator::createPml
void createPml(const char *inputFile, const char *pmlFile)
Create a pml file from an imput file.
Definition: SofaSimulator.cpp:534
SofaSimulator.h
sofaRegistered
bool sofaRegistered
Definition: SofaSimulator.cpp:74
PhysicalModel::setExclusiveComponents
void setExclusiveComponents(MultiComponent *)
set the exclusive multi component. Becareful: the physical model takes control of this MultiComponent
Definition: PhysicalModel.cpp:676
StructureProperties::TETRAHEDRON
@ TETRAHEDRON
the structure is a tetrahedron, it must be a cell and have sub-structures that are atoms
Definition: StructureProperties.h:159
RenderingMode::WIREFRAME_AND_SURFACE
@ WIREFRAME_AND_SURFACE
Definition: RenderingMode.h:91
StructuralComponent::getNumberOfStructures
unsigned int getNumberOfStructures() const
get the number of structures
Definition: StructuralComponent.h:252
SofaSimulator::getMechanicalObjectAtomDOFMap
std::MechanicalObjectAtomDOFMap & getMechanicalObjectAtomDOFMap(unsigned int mechObjectIndex)
get the MechanicalObjectAtomDOFMap for a given mechancial object
Definition: SofaSimulator.h:183
Structure::getType
StructureProperties::GeometricType getType() const
get the type of index
Definition: Structure.cpp:40
Atom
An atom has an unique index in the physical model object, a 3D position, and different basic properti...
Definition: Atom.h:49
SurfaceExtractor::init
void init()
initialize the extractor
Definition: SurfaceExtractor.cpp:42
SofaSimulator::getMechanicalObjectDOFAtomMap
std::MechanicalObjectDOFAtomMap & getMechanicalObjectDOFAtomMap(unsigned int mechObjectIndex)
get the MechanicalObjectAtomDOFMap for a given mechancial object
Definition: SofaSimulator.h:188
SofaSimulator::getNumberOfMechanicalObjects
unsigned int getNumberOfMechanicalObjects()
get the number of mechanical objects
Definition: SofaSimulator.h:178
SofaSimulator::getDOFForce
sofa::defaulttype::Vec3Types::Deriv getDOFForce(unsigned int atomIndex)
Return the force std::vector corresponding to the Atom number i.
Definition: SofaSimulator.cpp:418
SofaSimulator::translations
std::vector< TranslationConstraint< sofa::defaulttype::Vec3Types > * > translations
the load constraints (i.e. Translation) for each mechanical Objects
Definition: SofaSimulator.h:156
SofaSimulator::scnFile
std::string scnFile
path to .scn file
Definition: SofaSimulator.h:159
a
#define a
MonitoringManager::setDt
void setDt(double dt)
change dt and save modification into mmlIn
Definition: MonitoringManager.cpp:393
SofaSimulator::getMechanicalObjectDOFForce
sofa::defaulttype::Vec3Types::VecDeriv getMechanicalObjectDOFForce(unsigned int mechObjectIndex)
get the forces of all DOF for mechanical object mechObjectIndex
Definition: SofaSimulator.h:198
PhysicalModel::getNumberOfAtoms
unsigned int getNumberOfAtoms() const
get the number of atoms
Definition: PhysicalModel.cpp:716
SofaSimulator::getMechanicalObject
sofa::component::container::MechanicalObject< sofa::defaulttype::Vec3Types > * getMechanicalObject(unsigned int mechObjectIndex)
get a mechanical object by its index
Definition: SofaSimulator.cpp:435
TranslationConstraint.h
PhysicalModel::setName
void setName(const std::string)
set the name of the physical model
Definition: PhysicalModel.h:362
SurfaceExtractor::generateExternalSurface
void generateExternalSurface(StructuralComponent &surface, StructuralComponent &surfacePoints)
generate the external surface
Definition: SurfaceExtractor.cpp:209
PhysicalModel::getAtom
Atom * getAtom(const unsigned int id)
Get the atom that has the global index given in parameters.
Definition: PhysicalModel.h:385
Structure::getIndex
unsigned int getIndex() const
get the structure unique index (stored in its property)
Definition: Structure.cpp:30
MML_DECL_CLASS
#define MML_DECL_CLASS(name)
needed in files to register into factories (because of static lib and windows linking problems)
Definition: Macros.h:31
Component::setExclusive
void setExclusive(const bool)
set the exclusive flag
Definition: modeling/libraries/pml/Component.h:141
StructuralComponent
A structural component is composed either by cell or by atoms.
Definition: StructuralComponent.h:52
StructuralComponent::addStructure
void addStructure(Structure *s, bool check=true)
Add a Structure in the list (and tells the structure to add this structural component in its list).
Definition: StructuralComponent.h:212
Atom.h
MonitoringManager::isLmlPresent
bool isLmlPresent()
check if a lml is present
Definition: MonitoringManager.cpp:486
SofaSimulator::getPosition
void getPosition(int index, double position[3])
get current position for one atom
Definition: SofaSimulator.cpp:459
SofaSimulator::init
void init()
initialize the simulator
Definition: SofaSimulator.cpp:486
PhysicalModel
This is the main class of this project. Following a nice concept, a physical model is able to represe...
Definition: PhysicalModel.h:86
StructuralComponent::getAtoms
StructuralComponent * getAtoms()
Return a StructuralComponent with all the atoms of this structural component.
Definition: StructuralComponent.cpp:261
SofaSimulator::getForce
void getForce(int index, double force[3])
get current force for one atom
Definition: SofaSimulator.cpp:472
Component::getNumberOfCells
virtual unsigned int getNumberOfCells() const =0
get the total nr of cell of the component
SofaSimulator::getGNode
sofa::simulation::Node * getGNode()
get the sofa graph node root
Definition: SofaSimulator.h:163
SofaSimulator::atomsToDOF
std::AtomDOFMap atomsToDOF
create a correspondance between the atoms and the DOFs (indexAtom<->indexMechObject[indexDOF])
Definition: SofaSimulator.h:147
MonitoringManager::getLml
Loads * getLml()
Definition: MonitoringManager.cpp:274
Cell
A cell has an unique index in the physical model object, is composed by atoms, and different basic pr...
Definition: Cell.h:46
SofaSimulator::doMove
void doMove(double dt)
ask the simulator do to one step of the simulation
Definition: SofaSimulator.cpp:440
Component::getCell
virtual Cell * getCell(unsigned int) const =0
conveniant method to get cell by order number (not cell index)
PhysicalModel::xmlPrint
void xmlPrint(std::ostream &o, bool opt=false)
print the physical model to an output stream in a XML format (see physicalmodel.xsd for detail about ...
Definition: PhysicalModel.cpp:258
Simulator::monitoringManager
MonitoringManager * monitoringManager
monitoring manager
Definition: Simulator.h:107
SofaSimulator::end
void end()
end simultor
Definition: SofaSimulator.h:84
SofaSimulator::mechanicalObjectAtomDOFMap
std::vector< std::MechanicalObjectAtomDOFMap * > mechanicalObjectAtomDOFMap
list of all MechanicalObjectAtomDOFMap
Definition: SofaSimulator.h:150
StructuralComponent.h
SofaSimulator::mechanicalObjectDOFAtomMap
std::vector< std::MechanicalObjectDOFAtomMap * > mechanicalObjectDOFAtomMap
list of all MechanicalObjectDOFAtomMap
Definition: SofaSimulator.h:153
PhysicalModel::getComponentByName
Component * getComponentByName(const std::string n)
get a structural or multi component by its name.
Definition: PhysicalModel.cpp:553
StructuralComponent::setMode
void setMode(const RenderingMode::Mode)
set the rendering mode
Definition: StructuralComponent.cpp:134
SofaSimulator::build
void build()
build structure
Definition: SofaSimulator.cpp:292
SurfaceExtractor::AddCell
void AddCell(Cell *c)
try to add a cell in the surface extractor and do it if this cell is not in
Definition: SurfaceExtractor.cpp:50
SofaSimulator::mechanicalObjects
SofaVector< BaseMechanicalState * > mechanicalObjects
the sofa graph node root
Definition: SofaSimulator.h:144
SimulatorFactory.h
SofaSimulator::SofaSimulator
SofaSimulator(MonitoringManager *monitoringManager)
Definition: SofaSimulator.cpp:76
SofaSimulator::~SofaSimulator
virtual ~SofaSimulator()
Definition: SofaSimulator.cpp:282
SofaSimulator::getAtomIndex
unsigned int getAtomIndex(unsigned int mechObjectIndex, unsigned int dofIndex)
get the atom id corresponding to DOF of index dofIndex in the MechanicalObject of index mechObjectInd...
Definition: SofaSimulator.cpp:428
SofaWidget
TODO Comment class here.
Definition: SofaWidget.h:44
MultiComponent::addSubComponent
void addSubComponent(Component *)
add a component in the list of subcomponents (and set the isExclusive flag accordingly to the state o...
Definition: MultiComponent.h:122
InteractiveSimulator
An interactive simulator is a simulator that we can control step by step (ex: Sofa)
Definition: InteractiveSimulator.h:38
StructureProperties::HEXAHEDRON
@ HEXAHEDRON
the structure is a hexahedron, it must be a cell and have sub-structures that are atoms
Definition: StructureProperties.h:162
MonitoringManager
Manager of the benchmark tests. Do simulation loop and tests.
Definition: MonitoringManager.h:50
StructureProperties::TRIANGLE
@ TRIANGLE
the structure is a triangle, i.e it must be a cell composed of 3 atoms
Definition: StructureProperties.h:157
SofaSimulator::getMechanicalObjectDOFPosition
sofa::defaulttype::Vec3Types::VecCoord getMechanicalObjectDOFPosition(unsigned int mechObjectIndex)
get the positions of all DOF for mechanical object mechObjectIndex
Definition: SofaSimulator.h:193
StructuralComponent::getStructure
Structure * getStructure(const unsigned int) const
get a structure by its index (fisrt structure is at index 0)
Definition: StructuralComponent.h:220
InteractiveSimulator.h
Component
A component is something that composed something and could also be a part of something.
Definition: modeling/libraries/pml/Component.h:48
SofaSimulator::buildConstraints
void buildConstraints()
translate loads into constraints
Definition: SofaSimulator.cpp:495
TranslationConstraint
TODO Comment class here.
Definition: TranslationConstraint.h:48
PhysicalModel::getAtoms
StructuralComponent * getAtoms() const
get all the atoms
Definition: PhysicalModel.h:380
TranslationConstraint::setInitTime
void setInitTime(double time)
set initial time (context->getTime() remind the same when simulation is rewind)
MultiComponent.h
PhysicalModel::setInformativeComponents
void setInformativeComponents(MultiComponent *)
set the exclusive multi component. Becareful: the physical model takes control of this MultiComponent
Definition: PhysicalModel.cpp:686
StructureProperties::QUAD
@ QUAD
the structure is a quad, i.e it must be a cell composed of 4 atoms
Definition: StructureProperties.h:158
StructuralComponent::getNumberOfCells
unsigned int getNumberOfCells() const override
get the total nr of cell of the component
Definition: StructuralComponent.cpp:183
std::MechanicalObjectDOFAtomMap
map< unsigned int, unsigned int > MechanicalObjectDOFAtomMap
there is one MechanicalObjectDOFAtomMap per Mechanical Object: this is a map where first is the DOF i...
Definition: SofaSimulator.h:59
std::AtomDOFMap
map< unsigned int, MechanicalObjectDOFIndex > AtomDOFMap
definition of the association set (=map in STL) AtomDOFMap AtomDOFMap associates an atom index with t...
Definition: SofaSimulator.h:51
TranslationConstraint::getTargets
std::vector< unsigned int > getTargets()
return the targets list
Definition: TranslationConstraint.h:89
SofaSimulator::getScnFile
std::string getScnFile()
Definition: SofaSimulator.h:203
std::MechanicalObjectAtomDOFMap
map< unsigned int, unsigned int > MechanicalObjectAtomDOFMap
there is one MechanicalObjectAtomDOFMap per Mechanical Object: this is a map where first is the atom ...
Definition: SofaSimulator.h:55
MultiComponent
A multi-component stores other components, hence providing a way to have an tree representation of co...
Definition: MultiComponent.h:44
SurfaceExtractor.h
SofaSimulator::getDOFPosition
sofa::defaulttype::Vec3Types::Coord getDOFPosition(unsigned int atomIndex)
Return the Coord std::vector corresponding to the Atom number i.
Definition: SofaSimulator.cpp:408
SofaSimulator
TODO Comment class here.
Definition: SofaSimulator.h:69
SurfaceExtractor
A class to generate the external surface of a pml to use it just add the cells among which you want t...
Definition: SurfaceExtractor.h:44
SofaWidget.h
PhysicalModel::setAtoms
void setAtoms(StructuralComponent *, bool deleteOld=true)
set the atom structural component.
Definition: PhysicalModel.cpp:572