Computer Assisted Medical Intervention Tool Kit  version 5.2
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-2024 Univ. Grenoble Alpes, CNRS, Grenoble INP - UGA, 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 
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 
147  std::AtomDOFMap atomsToDOF;
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 --------------------
178 inline unsigned int SofaSimulator::getNumberOfMechanicalObjects() {
179  return mechanicalObjects.size();
180 }
181 
182 // -------------------- getMechanicalObjectAtomDOFMap --------------------
183 inline std::MechanicalObjectAtomDOFMap& SofaSimulator::getMechanicalObjectAtomDOFMap(unsigned int mechObjectIndex) {
184  return (*mechanicalObjectAtomDOFMap[mechObjectIndex]);
185 }
186 
187 // -------------------- getMechanicalObjectDOFAtomMap --------------------
188 inline std::MechanicalObjectDOFAtomMap& SofaSimulator::getMechanicalObjectDOFAtomMap(unsigned int mechObjectIndex) {
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
pair< unsigned int, unsigned int > MechanicalObjectDOFIndex
definition of a couple (=STL pair) [unsigned int mecObjectIndex, unsigned int dofIndex]
Definition: SofaSimulator.h:47
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
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
An interactive simulator is a simulator that we can control step by step (ex: Sofa)
Definition: InteractiveSimulator.h:38
Manager of the benchmark tests.
Definition: MonitoringManager.h:50
MonitoringManager * monitoringManager
monitoring manager
Definition: Simulator.h:84
TODO Comment class here.
Definition: SofaSimulator.h:69
SofaSimulator(MonitoringManager *monitoringManager)
Definition: SofaSimulator.cpp:76
void createPml(const char *inputFile, const char *pmlFile)
Create a pml file from an imput file.
Definition: SofaSimulator.cpp:534
std::string getScnFile()
Definition: SofaSimulator.h:203
void getForce(int index, double force[3])
get current force for one atom
Definition: SofaSimulator.cpp:472
void end()
end simultor
Definition: SofaSimulator.h:84
virtual ~SofaSimulator()
Definition: SofaSimulator.cpp:282
void doMove(double dt)
ask the simulator do to one step of the simulation
Definition: SofaSimulator.cpp:440
void getPosition(int index, double position[3])
get current position for one atom
Definition: SofaSimulator.cpp:459
void init()
initialize the simulator
Definition: SofaSimulator.cpp:486