-
Notifications
You must be signed in to change notification settings - Fork 4
/
ikGroupContainer.cpp
278 lines (254 loc) · 9.58 KB
/
ikGroupContainer.cpp
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "ikGroupContainer.h"
#include "environment.h"
CIkGroupContainer::CIkGroupContainer()
{
}
CIkGroupContainer::~CIkGroupContainer()
{
removeAllIkGroups();
}
CIkGroupContainer* CIkGroupContainer::copyYourself() const
{
CIkGroupContainer* duplicate=new CIkGroupContainer();
for (size_t i=0;i<ikGroups.size();i++)
duplicate->ikGroups.push_back(ikGroups[i]->copyYourself());
return(duplicate);
}
CikGroup* CIkGroupContainer::getIkGroup(int groupHandle) const
{
for (size_t i=0;i<ikGroups.size();i++)
{
if (ikGroups[i]->getObjectHandle()==groupHandle)
return(ikGroups[i]);
}
return(nullptr);
}
CikGroup* CIkGroupContainer::getIkGroup(std::string groupName) const
{
for (size_t i=0;i<ikGroups.size();i++)
{
if (ikGroups[i]->getObjectName().compare(groupName)==0)
return(ikGroups[i]);
}
return(nullptr);
}
void CIkGroupContainer::removeIkGroup(int ikGroupHandle)
{
CEnvironment::currentEnvironment->objectContainer->announceIkGroupWillBeErased(ikGroupHandle);
for (size_t i=0;i<ikGroups.size();i++)
{
if (ikGroups[i]->getObjectHandle()==ikGroupHandle)
{
delete ikGroups[i];
ikGroups.erase(ikGroups.begin()+i);
return;
}
}
}
void CIkGroupContainer::removeAllIkGroups()
{
while (ikGroups.size()!=0)
removeIkGroup(ikGroups[0]->getObjectHandle());
}
void CIkGroupContainer::announceSceneObjectWillBeErased(int objectHandle)
{
size_t i=0;
while (i<ikGroups.size())
{
if (ikGroups[i]->announceSceneObjectWillBeErased(objectHandle))
{ // This ik group has to be erased:
removeIkGroup(ikGroups[i]->getObjectHandle()); // This will call announceIkGroupWillBeErased!
i=0; // order may have changed!
}
else
i++;
}
}
void CIkGroupContainer::announceIkGroupWillBeErased(int ikGroupHandle)
{ // Never called from copy buffer!
size_t i=0;
while (i<ikGroups.size())
{
if (ikGroups[i]->announceIkGroupWillBeErased(ikGroupHandle))
{ // This ik group has to be erased (normally never happens)
removeIkGroup(ikGroups[i]->getObjectHandle()); // This will call announceIkGroupWillBeErased!
i=0; // ordering may have changed!
}
else
i++;
}
}
int CIkGroupContainer::computeAllIkGroups(bool exceptExplicitHandling)
{
int performedCount=0;
{
for (size_t i=0;i<ikGroups.size();i++)
{
if ((!exceptExplicitHandling)||(!ikGroups[i]->getExplicitHandling_old()))
{
std::vector<int> gr;
gr.push_back(ikGroups[i]->getObjectHandle());
if ( (computeIk(gr,nullptr,nullptr)&ik_calc_notperformed)==0 )
performedCount++;
}
}
}
return(performedCount);
}
void CIkGroupContainer::addIkGroup(CikGroup* anIkGroup,bool keepCurrentHandle)
{ // Be careful! We don't check if the group is valid!!
if (!keepCurrentHandle)
{
int newHandle=2030003;
while (getIkGroup(newHandle)!=nullptr)
newHandle++;
anIkGroup->setObjectHandle(newHandle);
}
ikGroups.push_back(anIkGroup);
}
int CIkGroupContainer::computeIk(int groupHandle,double precision[2],int(*cb)(const int*,double*,const int*,const int*,const int*,const int*,double*,double*,double*,int,int))
{ // Return value is one ik_resultinfo...-value
std::vector<int> gr;
gr.push_back(groupHandle);
return(computeIk(gr,precision,cb));
}
int CIkGroupContainer::computeIk(const std::vector<int>& groupHandles,double precision[2],int(*cb)(const int*,double*,const int*,const int*,const int*,const int*,double*,double*,double*,int,int))
{ // Return value is one ik_resultinfo...-value
if (precision!=nullptr)
{
precision[0]=0.0;
precision[1]=0.0;
}
std::vector<CikGroup*> ikGroups;
std::vector<std::vector<CikElement*>> ikElements;
for (size_t i=0;i<groupHandles.size();i++)
{
CikGroup* ikGroup=getIkGroup(groupHandles[i]);
if ( (ikGroup!=nullptr)&&((ikGroup->getOptions()&ik_group_enabled)!=0) )
{
ikGroup->clearJointLimitHits();
ikGroups.push_back(ikGroup);
std::vector<CikElement*> els;
if (!ikGroup->getValidElements(els))
return(ik_calc_notperformed|ik_calc_notwithintolerance);
ikElements.push_back(els);
}
}
if (ikGroups.size()==0)
return(ik_calc_notperformed|ik_calc_notwithintolerance);
CikGroup* firstIkGroup=ikGroups[0];
std::vector<int> memorizedConf_handles;
std::vector<double> memorizedConf_vals;
CEnvironment::currentEnvironment->objectContainer->memorizeJointConfig(memorizedConf_handles,memorizedConf_vals);
// Here we have the main iteration loop:
double interpolFact=1.0; // We first try to solve in one step
int cumulResultInfo=0;
bool withinPosition,withinOrientation;
for (int iterationNb=0;iterationNb<ikGroups[0]->getMaxIterations();iterationNb++)
{
std::vector<CJoint*> allJoints;
std::vector<int> allJointDofIndices;
for (size_t i=0;i<ikGroups.size();i++)
{
CikGroup* ikGroup=ikGroups[i];
ikGroup->prepareRawJacobians(ikElements[i],interpolFact);
ikGroup->selectJoints(&ikElements[i],&allJoints,&allJointDofIndices);
}
for (size_t i=0;i<ikGroups.size();i++)
{
CikGroup* ikGroup=ikGroups[i];
if (ikGroups.size()>1)
ikGroup->selectJoints(&ikElements[i],&allJoints,&allJointDofIndices); // needs to be called a second time
int r=ikGroup->computeDq(&ikElements[i],false,iterationNb,cb);
if ( (r==ik_calc_cannotinvert)||(r==ik_calc_invalidcallbackdata) )
{
cumulResultInfo=cumulResultInfo|r|ik_calc_notwithintolerance;
break;
}
}
if ((cumulResultInfo&(ik_calc_cannotinvert|ik_calc_invalidcallbackdata))!=0)
break;
// "Assemble" result here:
// ***************************
CMatrix tdQ=ikGroups[ikGroups.size()-1]->getDq();
for (int i=int(ikGroups.size())-2;i>=0;i--)
{
CikGroup* ikGroup=ikGroups[size_t(i)];
const CMatrix dQ=ikGroup->getDq();
const CMatrix J=ikGroup->getJacobian();
const CMatrix Jinv=ikGroup->getInvJacobian();
CMatrix I(allJoints.size(),allJoints.size());
I.setIdentity();
CMatrix NP=I-Jinv*J;
tdQ=dQ+NP*tdQ;
}
// ***************************
double maxStepFact=0.0;
int resultInfo=firstIkGroup->checkDq(&tdQ,&maxStepFact);
cumulResultInfo=cumulResultInfo|resultInfo;
if ( ((firstIkGroup->getOptions()&ik_group_ignoremaxsteps)==0)&&((resultInfo&ik_calc_stepstoobig)!=0) )
{ // Joint variations not within tolerance. Retry by interpolating:
interpolFact=interpolFact/(maxStepFact*1.1); // 10% tolerance
iterationNb--; // redo this pass
}
else
{
firstIkGroup->applyDq(&tdQ);
if ( (maxStepFact<0.8)&&(interpolFact<1.0) )
{ // Joint variations are not too large: try to return to a non-interpolated target:
interpolFact=interpolFact/(maxStepFact*1.1); // 10% tolerance
if (interpolFact>1.0)
interpolFact=1.0;
}
}
// We check if all IK elements are under the required precision
withinPosition=true;
withinOrientation=true;;
for (size_t i=0;i<ikElements.size();i++)
{
for (size_t j=0;j<ikElements[i].size();j++)
{
CikElement* element=ikElements[i][j];
double lp,ap;
element->getTipTargetDistance(lp,ap);
double pr[2];
element->getPrecisions(pr);
if (lp>pr[0])
withinPosition=false;
if (ap>pr[1])
withinOrientation=false;
}
}
if ( (!withinPosition)||(!withinOrientation) )
cumulResultInfo=cumulResultInfo|ik_calc_notwithintolerance;
if ( ((cumulResultInfo&ik_calc_limithit)!=0)&&((firstIkGroup->getOptions()&ik_group_stoponlimithit)!=0) )
break;
if (withinPosition&&withinOrientation)
{
cumulResultInfo=cumulResultInfo&(~ik_calc_notwithintolerance);
break;
}
}
if (precision!=nullptr)
{
for (size_t i=0;i<ikElements.size();i++)
{
for (size_t j=0;j<ikElements[i].size();j++)
{
CikElement* element=ikElements[i][j];
double lp,ap;
element->getTipTargetDistance(lp,ap);
if (lp>precision[0])
precision[0]=lp;
if (ap>precision[1])
precision[1]=ap;
}
}
}
bool restoreConfig=(cumulResultInfo&ik_calc_cannotinvert)!=0;
if ( (cumulResultInfo&ik_calc_notwithintolerance)!=0 ) // notwithintolerance cannot always be considered as an error (e.g. when we want slow convergence, etc.) and results are applied by default
restoreConfig=( (((firstIkGroup->getOptions()&ik_group_restoreonbadlintol)!=0)&&(!withinPosition))||(((firstIkGroup->getOptions()&ik_group_restoreonbadangtol)!=0)&&(!withinOrientation)) );
if (restoreConfig)
CEnvironment::currentEnvironment->objectContainer->restoreJointConfig(memorizedConf_handles,memorizedConf_vals);
return(cumulResultInfo);
}