Computer Assisted Medical Intervention Tool Kit version 6.0
 
Loading...
Searching...
No Matches
TranslationConstraint.inl
Go to the documentation of this file.
1/*****************************************************************************
2 * $CAMITK_LICENCE_BEGIN$
3 *
4 * CamiTK - Computer Assisted Medical Intervention ToolKit
5 * (c) 2001-2025 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#ifndef TRANSLATION_CONSTRAINT_INL
27#define TRANSLATION_CONSTRAINT_INL
28
30#include <sofa/helper/system/config.h>
31#include <sofa/helper/gl/template.h>
32
33using namespace std;
34using namespace sofa::core::behavior;
35
36template<class DataTypes>
37TranslationConstraint<DataTypes>::TranslationConstraint(Loads* loadsList, const map<unsigned int, unsigned int> &atomIndexToDOFIndex, MechanicalState<DataTypes> *mm) : ProjectiveConstraintSet<DataTypes>(mm) {
38 atomToDOFIndexes = atomIndexToDOFIndex;
39 mmodel = mm;
40
41 SReal dirX, dirY, dirZ;
42 this->setName("TranslationConstraint");
43 initTime=0;
44 //for each load, we search which ones are translations applied on the body nodes
45 for (unsigned int i = 0 ; i < loadsList->numberOfLoads() ; i++) {
46 Load * load = loadsList->getLoad(i);
47 if (load->getType() == "Translation") {
48 if (load->getDirection().isToward()) {
49 std::map<unsigned int, unsigned int>::const_iterator titi = atomIndexToDOFIndex.find(load->getDirection().getToward());
50 if (titi != atomIndexToDOFIndex.end()){
51 unsigned int dofInd = titi->second;
52 dirX = (*mm->getX())[dofInd].x();
53 dirY = (*mm->getX())[dofInd].y();
54 dirZ = (*mm->getX())[dofInd].z();
55 }
56 }
57 else
58 load->getDirection(dirX, dirY, dirZ);
59 unsigned int cpt = 0;
60 for (unsigned int j = 0 ; j < load->numberOfTargets(); j++) {
61 std::map<unsigned int, unsigned int>::const_iterator result = atomIndexToDOFIndex.find(load->getTarget(j));
62 if (result != atomIndexToDOFIndex.end()) {
63 cpt++;
64 if (load->getDirection().isToward())
65 addConstraint(result->second, Deriv(dirX - (*mm->getX())[result->second].x(), dirY - (*mm->getX())[result->second].y(), dirZ - (*mm->getX())[result->second].z()));
66 else
67 addConstraint(result->second, Deriv(dirX, dirY, dirZ));
68 // fix targets on the X axe
69 if (load->getDirection().isXNull() && load->getValue(0) != 0)
70 fixDOF(result->second, 0);
71 if (load->getDirection().isYNull() && load->getValue(0) != 0) // fix targets on the Y axe
72 fixDOF(result->second, 1);
73 if (load->getDirection().isZNull() && load->getValue(0) != 0) // fix targets on the Z axe
74 fixDOF(result->second, 2);
75 }
76 }
77 if (cpt > 0)
78 loads.push_back(load);
79 }
80 }
81}
83
84template<class DataTypes>
86{
87 this->targets.push_back(index);
88 trans.normalize();
89 this->translations.push_back(trans);
90 this->directionsNULLs.push_back(Deriv(1, 1, 1));
91 this->initPos.push_back(Deriv(0, 0, 0));
92 return this;
93}
94
95template<class DataTypes>
97{
98 std::vector<unsigned int>::iterator it1 = targets.begin();
99 VecDerivIterator it2 = translations.begin();
100 VecDerivIterator it3 = directionsNULLs.begin();
101 VecDerivIterator it4 = initPos.begin();
102 while (it1 != targets.end() && *it1 != (unsigned)index) {
103 ++it1;
104 ++it2;
105 ++it3;
106 ++it4;
107 }
108
109 targets.erase(it1);
110 translations.erase(it2);
111 directionsNULLs.erase(it3);
112 initPos.erase(it4);
113
114 return this;
115}
116
117
118template<class DataTypes>
119void TranslationConstraint<DataTypes>::fixDOF(int index, int axe)
120{
121 //set the value to 1 on the corrects vector component
122 std::vector<unsigned int>::iterator it1 = targets.begin();
123 VecDerivIterator it2 = directionsNULLs.begin();
124 while (it1 != targets.end() && *it1 != (unsigned)index) {
125 ++it1;
126 ++it2;
127 }
128
129 (*it2)[axe] = 0;
130}
131
132
133template<class DataTypes>
135
136 SReal time = this->getContext()->getTime()-initTime;
137 //SReal prevTime = time - this->getContext()->getDt();
138
139 std::vector<unsigned int>::iterator it1 = targets.begin();
140 VecDerivIterator it2 = translations.begin();
141 VecDerivIterator it3 = directionsNULLs.begin();
142 Load * load;
143 SReal valTime; //, prevValTime;
144
145 for (unsigned int i = 0 ; i < loads.size() ; i++) {
146 load = loads[i];
147 // the current value
148 valTime = load->getValue(time);
149 // the value at last time we updated the load
150 //prevValTime = (prevTime < 0.0) ? 0.0 : load->getValue(prevTime);
151 for (unsigned int j = 0 ; j < load->numberOfTargets();j++) {
152 if (atomToDOFIndexes.find(load->getTarget(j)) != atomToDOFIndexes.end())
153 {
154 // nullified all other acceleration
155 if ((load->getDirection().isXNull() || load->getDirection().isXSpecified()) && valTime != 0)
156 dx[*it1][0] = 0; // fix targets on the X axis
157 if ((load->getDirection().isYNull() || load->getDirection().isYSpecified()) && valTime != 0)
158 dx[*it1][1] = 0; // fix targets on the Y axis
159 if ((load->getDirection().isZNull() || load->getDirection().isZSpecified()) && valTime != 0)
160 dx[*it1][2] = 0; // fix targets on the Z axis
161
162 ++it1;
163 ++it2;
164 ++it3;
165 }
166 }
167 }
168}
169
170template <class DataTypes>
172 for (std::vector<unsigned int>::iterator it1 = targets.begin();it1 != targets.end();++it1) {
173 v[*it1][0] = 0.0;
174 v[*it1][1] = 0.0;
175 v[*it1][2] = 0.0;
176 }
177}
178
179template<class DataTypes>
181
182 SReal time = this->getContext()->getTime()-initTime;
183 SReal prevTime = time - this->getContext()->getDt();
184
185 std::vector<unsigned int>::iterator it1 = targets.begin();
186 VecDerivIterator it2 = translations.begin();
187 VecDerivIterator it3 = initPos.begin();
188 Load * load;
189
190 for (unsigned int i = 0 ; i < loads.size() ; i++)
191 {
192 load = loads[i];
193 for (unsigned int j = 0 ; j < load->numberOfTargets();j++)
194 {
195 if (atomToDOFIndexes.find(load->getTarget(j)) != atomToDOFIndexes.end()) {
196
197 //initPos is the current position
198 *it3 = x[*it1];
199
200 if (load->getValue(time) != 0.0){
201 if (load->getDirection().isToward()) {
202 std::map<unsigned int, unsigned int>::const_iterator titi = atomToDOFIndexes.find(load->getDirection().getToward());
203 if (titi != atomToDOFIndexes.end()){
204 (*it2) = (*mmodel->getX())[titi->second] - (*mmodel->getX())[*it1];
205 it2->normalize();
206 }
207 }
208 // the last time we updated the load
209 double lastValue = (prevTime < 0.0) ? 0.0 : load->getValue(prevTime);
210 // multiply by the difference between the current value and the last time value,
211 // so that when t is equal to the date of the current value event, the total
212 // of displacement will sum up to value.
213 x[*it1] = (*it3) + (*it2) * (load->getValue(time) - lastValue);
214 }
215
216 ++it1;
217 ++it2;
218 ++it3;
219 }
220 }
221 }
222}
223
224
225
226// -- VisualModel interface
227template<class DataTypes>
229{
230 /*
231 // TODO update this to show constraints if this class is integrated as a Sofa Plugin
232
233 if (!this->getContext()->getShowBehaviorModels()) return;
234
235 VecCoord x = *mmodel->getX();
236 glDisable(GL_LIGHTING);
237 glColor4f(1, 0.5, 0.5, 1);
238
239 glPointSize(10);
240
241 //for Fixed points, display a big red point
242 glBegin(GL_POINTS);
243 VecDerivIterator it2 = directionsNULLs.begin();
244 for (std::vector<unsigned int>::const_iterator it = this->targets.begin(); it != this->targets.end(); ++it)
245 {
246 if ((*it2)[0] == 0 && (*it2)[1] == 0 && (*it2)[2] == 0)
247 sofa::helper::gl::glVertexT(x[*it]);
248
249 it2++;
250 }
251 glEnd();
252
253 //for translated points, display a little red segment with translation direction
254 glPointSize(1);
255 glBegin(GL_LINES);
256 VecDerivIterator it3 = translations.begin();
257 it2 = directionsNULLs.begin();
258 for (std::vector<unsigned int>::const_iterator it = this->targets.begin(); it != this->targets.end(); ++it)
259 {
260 if ((*it2)[0] == 1 || (*it2)[1] == 1 || (*it2)[2] == 1) {
261 sofa::helper::gl::glVertexT(x[*it]);
262 sofa::helper::gl::glVertexT(x[*it] + *it3);
263 }
264 it3++;
265 it2++;
266 }
267 glEnd();
268*/
269
270
271}
272
273template<class DataTypes>
275 initTime=time;
276}
277
278#endif //TRANSLATION_CONSTRAINT_INL
Class that describes a load to be used in the simulation.
Definition Load.h:53
double getValue(const double d=0.0)
The current value at date d (default: d = 0.0).
Definition Load.cpp:112
void getDirection(double &x, double &y, double &z) const
Get the direction.
Definition Load.cpp:235
unsigned int numberOfTargets() const
get the number of target
Definition Load.cpp:224
int getTarget(const unsigned int target) const
Get a target by index.
Definition Load.cpp:229
std::string getType() const
get the type string, has to be impleted in subclasses
Definition Load.cpp:283
This class makes it possible to manage a list of "Load".
Definition Loads.h:71
Load * getLoad(const unsigned int i) const
get a load by its index in the list
Definition Loads.cpp:249
unsigned int numberOfLoads() const
get the number of "Load" stored in the list
Definition Loads.cpp:259
TODO Comment class here.
Definition TranslationConstraint.h:48
DataTypes::VecDeriv::iterator VecDerivIterator
Definition TranslationConstraint.h:54
TranslationConstraint< DataTypes > * addConstraint(unsigned int index, Deriv trans)
fix or translate a point
Definition TranslationConstraint.inl:85
TranslationConstraint(Loads *loadsList, const std::map< unsigned int, unsigned int > &atomIndexToDOFIndex, sofa::core::behavior::MechanicalState< DataTypes > *mm)
constructor
Definition TranslationConstraint.inl:37
DataTypes::VecDeriv VecDeriv
Definition TranslationConstraint.h:52
TranslationConstraint< DataTypes > * removeConstraint(int index)
Definition TranslationConstraint.inl:96
virtual void projectPosition(VecCoord &x)
project x to constrained space (x models a position)
Definition TranslationConstraint.inl:180
void setInitTime(double time)
set initial time (context->getTime() remind the same when simulation is rewind)
Definition TranslationConstraint.inl:274
DataTypes::VecCoord VecCoord
template types
Definition TranslationConstraint.h:51
void projectResponse(VecDeriv &dx)
Constraint inherits.
Definition TranslationConstraint.inl:134
void draw()
– VisualModel interface
Definition TranslationConstraint.inl:228
virtual void projectVelocity(VecDeriv &v)
project dx to constrained space (dx models a velocity)
Definition TranslationConstraint.inl:171
DataTypes::Deriv Deriv
Definition TranslationConstraint.h:56
STL namespace.