Computer Assited Medical Intervention Tool Kit  version 3.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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-2014 UJF-Grenoble 1, 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/tree/GNode.h>
36 
37 // simplification of sofa interface
38 #define SofaVector sofa::helper::vector
39 
40 // Loads for Sofa
41 #include "TranslationConstraint.h"
42 
44 namespace std {
46 typedef pair<unsigned int, unsigned int> MechanicalObjectDOFIndex;
50 typedef map <unsigned int, MechanicalObjectDOFIndex> AtomDOFMap;
54 typedef map<unsigned int, unsigned int> MechanicalObjectAtomDOFMap;
58 typedef map<unsigned int, unsigned int> MechanicalObjectDOFAtomMap;
59 }
60 
69 
70 public:
72 
73  SofaSimulator(MonitoringManager* monitoringManager,const char* file);
74 
75  virtual ~SofaSimulator();
76 
79  void doMove(double dt);
80  void init();
81  void getPosition(int index, double position[3]);
82  void getForce(int index, double force[3]);
83  void end() {}
84 
85  void createPml(const char* inputFile,const char* pmlFile);
87 
88  std::string getScnFile();
89 
90 private:
92  sofa::simulation::Node *getGNode();
93 
95  std::MechanicalObjectAtomDOFMap & getMechanicalObjectAtomDOFMap(unsigned int mechObjectIndex);
96 
98  std::MechanicalObjectDOFAtomMap & getMechanicalObjectDOFAtomMap(unsigned int mechObjectIndex);
99 
101  sofa::defaulttype::Vec3Types::Coord getDOFPosition(unsigned int atomIndex);
102 
104  sofa::defaulttype::Vec3Types::Coord getDOFPosition(unsigned int mechObjectIndex, unsigned int dofIndex);
105 
107  sofa::defaulttype::Vec3Types::Deriv getDOFForce(unsigned int atomIndex);
108 
110  sofa::defaulttype::Vec3Types::Deriv getDOFForce(unsigned int mechObjectIndex, unsigned int dofIndex);
111 
113  unsigned int getAtomIndex(unsigned int mechObjectIndex, unsigned int dofIndex);
114 
116  sofa::component::container::MechanicalObject<sofa::defaulttype::Vec3Types> * getMechanicalObject(unsigned int mechObjectIndex);
117 
119  unsigned int getNumberOfMechanicalObjects();
120 
122  sofa::defaulttype::Vec3Types::VecCoord getMechanicalObjectDOFPosition(unsigned int mechObjectIndex);
123 
125  sofa::defaulttype::Vec3Types::VecDeriv getMechanicalObjectDOFForce(unsigned int mechObjectIndex);
126 
128  void build();
129 
131  void buildConstraints();
132 
134 #if defined(SOFA_1_0_RC1) || defined(SOFA_SVN)
135  sofa::simulation::Node::SPtr groot;
136 #endif
137 
138 #ifdef MML_SOFA_1_0_BETA4
139  sofa::simulation::Node* groot;
140 #endif
141 
143  SofaVector<BaseMechanicalState*> mechanicalObjects;
144 
146  std::AtomDOFMap atomsToDOF;
147 
149  std::vector<std::MechanicalObjectAtomDOFMap*> mechanicalObjectAtomDOFMap;
150 
152  std::vector<std::MechanicalObjectDOFAtomMap*> mechanicalObjectDOFAtomMap;
153 
155  std::vector<TranslationConstraint<sofa::defaulttype::Vec3Types> *> translations;
156 
158  std::string scnFile;
159 };
160 
161 // -------------------- getGNode --------------------
162 inline sofa::simulation::Node* SofaSimulator::getGNode() {
163  if (groot == NULL) {
164  //-- TODO build a sofaGRoot using the nodes
165  }
166 
167 #if defined(SOFA_1_0_RC1) || defined(SOFA_SVN)
168  return groot.get();
169 #endif
170 
171 #ifdef MML_SOFA_1_0_BETA4
172  return groot;
173 #endif
174 }
175 
176 // -------------------- getNumberOfMechanicalObjects --------------------
178  return mechanicalObjects.size();
179 }
180 
181 // -------------------- getMechanicalObjectAtomDOFMap --------------------
182 inline std::MechanicalObjectAtomDOFMap & SofaSimulator::getMechanicalObjectAtomDOFMap(unsigned int mechObjectIndex) {
183  return (*mechanicalObjectAtomDOFMap[mechObjectIndex]);
184 }
185 
186 // -------------------- getMechanicalObjectDOFAtomMap --------------------
187 inline std::MechanicalObjectDOFAtomMap & SofaSimulator::getMechanicalObjectDOFAtomMap(unsigned int mechObjectIndex) {
188  return (*mechanicalObjectDOFAtomMap[mechObjectIndex]);
189 }
190 
191 // -------------------- getMechanicalObjectDOFPosition --------------------
192 inline sofa::defaulttype::Vec3Types::VecCoord SofaSimulator::getMechanicalObjectDOFPosition(unsigned int mechObjectIndex) {
193  return (*getMechanicalObject(mechObjectIndex)->getX());
194 }
195 
196 // -------------------- getMechanicalObjectDOFForce --------------------
197 inline sofa::defaulttype::Vec3Types::VecDeriv SofaSimulator::getMechanicalObjectDOFForce(unsigned int mechObjectIndex) {
198  return (*getMechanicalObject(mechObjectIndex)->getF());
199 }
200 
201 // -------------------- getScnFile --------------------
202 inline std::string SofaSimulator::getScnFile() {
203  return scnFile;
204 }
205 
206 #endif // SIMULATOR_SIMULATORS_SOFA_SOFASIMULATOR_H
MonitoringManager * monitoringManager
monitoring manager
Definition: Simulator.h:84
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:391
std::vector< std::MechanicalObjectDOFAtomMap * > mechanicalObjectDOFAtomMap
list of all MechanicalObjectDOFAtomMap
Definition: SofaSimulator.h:152
std::vector< std::MechanicalObjectAtomDOFMap * > mechanicalObjectAtomDOFMap
list of all MechanicalObjectAtomDOFMap
Definition: SofaSimulator.h:149
std::vector< TranslationConstraint< sofa::defaulttype::Vec3Types > * > translations
the load constraints (i.e. Translation) for each mechanical Objects
Definition: SofaSimulator.h:155
sofa::defaulttype::Vec3Types::VecDeriv getMechanicalObjectDOFForce(unsigned int mechObjectIndex)
get the forces of all DOF for mechanical object mechObjectIndex
Definition: SofaSimulator.h:197
void createPml(const char *inputFile, const char *pmlFile)
Create a pml file from an imput file.
Definition: SofaSimulator.cpp:489
std::string getScnFile()
Definition: SofaSimulator.h:202
SofaSimulator(MonitoringManager *monitoringManager)
Definition: SofaSimulator.cpp:75
STL namespace.
void end()
end simultor
Definition: SofaSimulator.h:83
TODO Comment class here.
Definition: SofaSimulator.h:68
void buildConstraints()
translate loads into constraints
Definition: SofaSimulator.cpp:452
unsigned int getNumberOfMechanicalObjects()
get the number of mechanical objects
Definition: SofaSimulator.h:177
sofa::simulation::Node * getGNode()
get the sofa graph node root
Definition: SofaSimulator.h:162
SofaVector< BaseMechanicalState * > mechanicalObjects
the sofa graph node root
Definition: SofaSimulator.h:143
virtual ~SofaSimulator()
Definition: SofaSimulator.cpp:290
std::MechanicalObjectDOFAtomMap & getMechanicalObjectDOFAtomMap(unsigned int mechObjectIndex)
get the MechanicalObjectAtomDOFMap for a given mechancial object
Definition: SofaSimulator.h:187
std::MechanicalObjectAtomDOFMap & getMechanicalObjectAtomDOFMap(unsigned int mechObjectIndex)
get the MechanicalObjectAtomDOFMap for a given mechancial object
Definition: SofaSimulator.h:182
void getPosition(int index, double position[3])
get current position for one atom
Definition: SofaSimulator.cpp:417
sofa::defaulttype::Vec3Types::VecCoord getMechanicalObjectDOFPosition(unsigned int mechObjectIndex)
get the positions of all DOF for mechanical object mechObjectIndex
Definition: SofaSimulator.h:192
Manager of the benchmark tests.
Definition: MonitoringManager.h:50
void init()
initialize the simulator
Definition: SofaSimulator.cpp:444
sofa::component::container::MechanicalObject< sofa::defaulttype::Vec3Types > * getMechanicalObject(unsigned int mechObjectIndex)
get a mechanical object by its index
Definition: SofaSimulator.cpp:398
void doMove(double dt)
ask the simulator do to one step of the simulation
Definition: SofaSimulator.cpp:403
sofa::defaulttype::Vec3Types::Deriv getDOFForce(unsigned int atomIndex)
Return the force std::vector corresponding to the Atom number i.
Definition: SofaSimulator.cpp:381
unsigned int atomIndex
Definition: gensphere.cpp:41
sofa::defaulttype::Vec3Types::Coord getDOFPosition(unsigned int atomIndex)
Return the Coord std::vector corresponding to the Atom number i.
Definition: SofaSimulator.cpp:371
void build()
build structure
Definition: SofaSimulator.cpp:299
An interactive simulator is a simulator that we can control step by step (ex: Sofa) ...
Definition: InteractiveSimulator.h:38
string pmlFile
Definition: pml2ansys.cpp:36
std::string scnFile
path to .scn file
Definition: SofaSimulator.h:158
std::AtomDOFMap atomsToDOF
create a correspondance between the atoms and the DOFs (indexAtom<->indexMechObject[indexDOF]) ...
Definition: SofaSimulator.h:146
void getForce(int index, double force[3])
get current force for one atom
Definition: SofaSimulator.cpp:430