Computer Assited Medical Intervention Tool Kit  version 4.1
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-2018 Univ. Grenoble Alpes, CNRS, TIMC-IMAG UMR 5525 (GMCAO)
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:
72  SofaSimulator(MonitoringManager* monitoringManager);
73 
74  SofaSimulator(MonitoringManager* monitoringManager, const char* file);
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 
96  std::MechanicalObjectAtomDOFMap& getMechanicalObjectAtomDOFMap(unsigned int mechObjectIndex);
97 
99  std::MechanicalObjectDOFAtomMap& getMechanicalObjectDOFAtomMap(unsigned int mechObjectIndex);
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 == NULL) {
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::vector< std::MechanicalObjectDOFAtomMap * > mechanicalObjectDOFAtomMap
list of all MechanicalObjectDOFAtomMap
Definition: SofaSimulator.h:153
std::vector< std::MechanicalObjectAtomDOFMap * > mechanicalObjectAtomDOFMap
list of all MechanicalObjectAtomDOFMap
Definition: SofaSimulator.h:150
std::vector< TranslationConstraint< sofa::defaulttype::Vec3Types > * > translations
the load constraints (i.e. Translation) for each mechanical Objects
Definition: SofaSimulator.h:156
sofa::defaulttype::Vec3Types::VecDeriv getMechanicalObjectDOFForce(unsigned int mechObjectIndex)
get the forces of all DOF for mechanical object mechObjectIndex
Definition: SofaSimulator.h:198
std::string getScnFile()
Definition: SofaSimulator.h:203
STL namespace.
void end()
end simultor
Definition: SofaSimulator.h:84
TODO Comment class here.
Definition: SofaSimulator.h:69
unsigned int getNumberOfMechanicalObjects()
get the number of mechanical objects
Definition: SofaSimulator.h:178
sofa::simulation::Node * getGNode()
get the sofa graph node root
Definition: SofaSimulator.h:163
SofaVector< BaseMechanicalState * > mechanicalObjects
the sofa graph node root
Definition: SofaSimulator.h:144
std::MechanicalObjectDOFAtomMap & getMechanicalObjectDOFAtomMap(unsigned int mechObjectIndex)
get the MechanicalObjectAtomDOFMap for a given mechancial object
Definition: SofaSimulator.h:188
std::MechanicalObjectAtomDOFMap & getMechanicalObjectAtomDOFMap(unsigned int mechObjectIndex)
get the MechanicalObjectAtomDOFMap for a given mechancial object
Definition: SofaSimulator.h:183
sofa::defaulttype::Vec3Types::VecCoord getMechanicalObjectDOFPosition(unsigned int mechObjectIndex)
get the positions of all DOF for mechanical object mechObjectIndex
Definition: SofaSimulator.h:193
Manager of the benchmark tests.
Definition: MonitoringManager.h:50
An interactive simulator is a simulator that we can control step by step (ex: Sofa) ...
Definition: InteractiveSimulator.h:38
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
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
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
pair< unsigned int, unsigned int > MechanicalObjectDOFIndex
definition of a couple (=STL pair) [unsigned int mecObjectIndex, unsigned int dofIndex] ...
Definition: SofaSimulator.h:47
std::string scnFile
path to .scn file
Definition: SofaSimulator.h:159
std::AtomDOFMap atomsToDOF
create a correspondance between the atoms and the DOFs (indexAtom<->indexMechObject[indexDOF]) ...
Definition: SofaSimulator.h:147