File: SofaSimulator.h

package info (click to toggle)
camitk 4.1.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 427,532 kB
  • sloc: cpp: 79,494; xml: 1,176; sh: 1,137; ansic: 142; makefile: 107; perl: 84; sed: 20
file content (207 lines) | stat: -rw-r--r-- 7,889 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*****************************************************************************
 * $CAMITK_LICENCE_BEGIN$
 *
 * CamiTK - Computer Assisted Medical Intervention ToolKit
 * (c) 2001-2018 Univ. Grenoble Alpes, CNRS, TIMC-IMAG UMR 5525 (GMCAO)
 *
 * Visit http://camitk.imag.fr for more information
 *
 * This file is part of CamiTK.
 *
 * CamiTK is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * CamiTK is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with CamiTK.  If not, see <http://www.gnu.org/licenses/>.
 *
 * $CAMITK_LICENCE_END$
 ****************************************************************************/


#ifndef SIMULATOR_SIMULATORS_SOFA_SOFASIMULATOR_H
#define SIMULATOR_SIMULATORS_SOFA_SOFASIMULATOR_H

#include "InteractiveSimulator.h"

#include <sofa/simulation/tree/TreeSimulation.h>
#include <sofa/component/container/MechanicalObject.h>
#include <sofa/defaulttype/VecTypes.h>
#include <sofa/simulation/common/Node.h>
#include <sofa/simulation/tree/GNode.h>

// simplification of sofa interface
#define SofaVector sofa::helper::vector

// Loads for Sofa
#include "TranslationConstraint.h"

/// correspondance between an atom index and index of the DOF in a mechanical object
namespace std {
/// definition of a couple (=STL pair) [unsigned int mecObjectIndex, unsigned int dofIndex]
typedef pair<unsigned int, unsigned int> MechanicalObjectDOFIndex;
/** definition of the association set (=map in STL) AtomDOFMap
  * AtomDOFMap associates an atom index with the the MechanicalObjectDofIndex
  */
typedef map <unsigned int, MechanicalObjectDOFIndex> AtomDOFMap;
/** there is one MechanicalObjectAtomDOFMap per Mechanical Object:
  * this is a map where first is the atom index and second is the DOF index for this mechanical object
  */
typedef map<unsigned int, unsigned int> MechanicalObjectAtomDOFMap;
/** there is one MechanicalObjectDOFAtomMap per Mechanical Object:
  * this is a map where first is the DOF index for this mechanical object and second is the atom index
  */
typedef map<unsigned int, unsigned int> MechanicalObjectDOFAtomMap;
}

/**
 *
 * @ingroup group_cepmodeling_libraries_mml
 *
 * @brief
 * TODO Comment class here.
 */
class SofaSimulator : public InteractiveSimulator {

public:
    SofaSimulator(MonitoringManager* monitoringManager);

    SofaSimulator(MonitoringManager* monitoringManager, const char* file);

    virtual ~SofaSimulator();

    ///@name Simulator inherited
    ///@{
    void doMove(double dt);
    void init();
    void getPosition(int index, double position[3]);
    void getForce(int index, double force[3]);
    void end() {}

    void createPml(const char* inputFile, const char* pmlFile);
    ///@}

    std::string getScnFile();

private:
    /// get the sofa graph node root
    sofa::simulation::Node* getGNode();

    /// get the MechanicalObjectAtomDOFMap for a given mechancial object
    std::MechanicalObjectAtomDOFMap& getMechanicalObjectAtomDOFMap(unsigned int mechObjectIndex);

    /// get the MechanicalObjectAtomDOFMap for a given mechancial object
    std::MechanicalObjectDOFAtomMap& getMechanicalObjectDOFAtomMap(unsigned int mechObjectIndex);

    ///Return the Coord std::vector corresponding to the Atom number i
    sofa::defaulttype::Vec3Types::Coord getDOFPosition(unsigned int atomIndex);

    /// get the position of the DOF of index dofIndex in the MechanicalObject of index mechObjectIndex
    sofa::defaulttype::Vec3Types::Coord getDOFPosition(unsigned int mechObjectIndex, unsigned int dofIndex);

    ///Return the force std::vector corresponding to the Atom number i
    sofa::defaulttype::Vec3Types::Deriv getDOFForce(unsigned int atomIndex);

    /// get the force of the DOF of index dofIndex in the MechanicalObject of index mechObjectIndex
    sofa::defaulttype::Vec3Types::Deriv getDOFForce(unsigned int mechObjectIndex, unsigned int dofIndex);

    /// get the atom id corresponding to DOF of index dofIndex in the MechanicalObject of index mechObjectIndex
    unsigned int getAtomIndex(unsigned int mechObjectIndex, unsigned int dofIndex);

    /// get a mechanical object by its index
    sofa::component::container::MechanicalObject<sofa::defaulttype::Vec3Types>* getMechanicalObject(unsigned int mechObjectIndex);

    /// get the number of mechanical objects
    unsigned int getNumberOfMechanicalObjects();

    /// get the positions of all DOF for mechanical object mechObjectIndex
    sofa::defaulttype::Vec3Types::VecCoord getMechanicalObjectDOFPosition(unsigned int mechObjectIndex);

    /// get the forces of all DOF for mechanical object mechObjectIndex
    sofa::defaulttype::Vec3Types::VecDeriv getMechanicalObjectDOFForce(unsigned int mechObjectIndex);

    /// build structure
    void build();

    /// translate loads into constraints
    void buildConstraints();

    /// the sofa graph node root
#if defined(SOFA_1_0_RC1) || defined(SOFA_SVN) || defined(SOFA_STABLE)
    sofa::simulation::Node::SPtr groot;
#endif

#ifdef MML_SOFA_1_0_BETA4
    sofa::simulation::Node* groot;
#endif

    /// all the mechanical objects
    SofaVector<BaseMechanicalState*> mechanicalObjects;

    ///create a correspondance between the atoms and the DOFs (indexAtom<->indexMechObject[indexDOF])
    std::AtomDOFMap atomsToDOF;

    /// list of all MechanicalObjectAtomDOFMap
    std::vector<std::MechanicalObjectAtomDOFMap*> mechanicalObjectAtomDOFMap;

    /// list of all MechanicalObjectDOFAtomMap
    std::vector<std::MechanicalObjectDOFAtomMap*> mechanicalObjectDOFAtomMap;

    /// the load constraints (i.e. Translation) for each mechanical Objects
    std::vector<TranslationConstraint<sofa::defaulttype::Vec3Types> *> translations;

    /// path to .scn file
    std::string scnFile;
};

// -------------------- getGNode --------------------
inline sofa::simulation::Node* SofaSimulator::getGNode() {
    if (groot == NULL) {
        //-- TODO build a sofaGRoot using the nodes
    }

#if defined(SOFA_1_0_RC1) || defined(SOFA_SVN) || defined(SOFA_STABLE)
    return groot.get();
#endif

#ifdef MML_SOFA_1_0_BETA4
    return groot;
#endif
}

// -------------------- getNumberOfMechanicalObjects --------------------
inline unsigned int SofaSimulator::getNumberOfMechanicalObjects() {
    return mechanicalObjects.size();
}

// -------------------- getMechanicalObjectAtomDOFMap --------------------
inline std::MechanicalObjectAtomDOFMap& SofaSimulator::getMechanicalObjectAtomDOFMap(unsigned int mechObjectIndex) {
    return (*mechanicalObjectAtomDOFMap[mechObjectIndex]);
}

// -------------------- getMechanicalObjectDOFAtomMap --------------------
inline std::MechanicalObjectDOFAtomMap& SofaSimulator::getMechanicalObjectDOFAtomMap(unsigned int mechObjectIndex) {
    return (*mechanicalObjectDOFAtomMap[mechObjectIndex]);
}

// -------------------- getMechanicalObjectDOFPosition --------------------
inline sofa::defaulttype::Vec3Types::VecCoord SofaSimulator::getMechanicalObjectDOFPosition(unsigned int mechObjectIndex) {
    return (*getMechanicalObject(mechObjectIndex)->getX());
}

// -------------------- getMechanicalObjectDOFForce --------------------
inline sofa::defaulttype::Vec3Types::VecDeriv SofaSimulator::getMechanicalObjectDOFForce(unsigned int mechObjectIndex) {
    return (*getMechanicalObject(mechObjectIndex)->getF());
}

// -------------------- getScnFile --------------------
inline std::string SofaSimulator::getScnFile() {
    return scnFile;
}

#endif // SIMULATOR_SIMULATORS_SOFA_SOFASIMULATOR_H