-
Notifications
You must be signed in to change notification settings - Fork 310
/
InverseKinematicsSolver.cpp
266 lines (228 loc) · 12 KB
/
InverseKinematicsSolver.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
/* -------------------------------------------------------------------------- *
* OpenSim: InverseKinematicsSolver.cpp *
* -------------------------------------------------------------------------- *
* The OpenSim API is a toolkit for musculoskeletal modeling and simulation. *
* See http://opensim.stanford.edu and the NOTICE file for more information. *
* OpenSim is developed at Stanford University and supported by the US *
* National Institutes of Health (U54 GM072970, R24 HD065690) and by DARPA *
* through the Warrior Web program. *
* *
* Copyright (c) 2005-2017 Stanford University and the Authors *
* Author(s): Ajay Seth *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); you may *
* not use this file except in compliance with the License. You may obtain a *
* copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* -------------------------------------------------------------------------- */
#include "InverseKinematicsSolver.h"
#include "MarkersReference.h"
#include "Model/Model.h"
#include "Model/MarkerSet.h"
#include "simbody/internal/AssemblyCondition_Markers.h"
using namespace std;
using namespace SimTK;
namespace OpenSim {
//______________________________________________________________________________
/*
* An implementation of the InverseKinematicsSolver
*
* @param model to assemble
*/
InverseKinematicsSolver::InverseKinematicsSolver(const Model &model, MarkersReference &markersReference,
SimTK::Array_<CoordinateReference> &coordinateReferences,
double constraintWeight) : AssemblySolver(model, coordinateReferences, constraintWeight),
_markersReference(markersReference)
{
setAuthors("Ajay Seth");
// Do some consistency checking for markers
const MarkerSet &modelMarkerSet = getModel().getMarkerSet();
if(modelMarkerSet.getSize() < 1){
std::cout << "InverseKinematicsSolver: Model has no markers!" << std::endl;
throw Exception("InverseKinematicsSolver: Model has no markers!");
}
const SimTK::Array_<std::string> &markerNames = _markersReference.getNames(); // size and content as in trc file
if(markerNames.size() < 1){
std::cout << "InverseKinematicsSolver: No markers available from data provided." << std::endl;
throw Exception("InverseKinematicsSolver: No markers available from data provided.");
}
int index=0, cnt=0;
for(unsigned int i=0; i < markerNames.size(); i++) {
// Check if we have this marker in the model, else ignore it
index = modelMarkerSet.getIndex(markerNames[i], index);
if(index >= 0) //found corresponding model
cnt++;
}
if(cnt < 1){
std::cout <<"InverseKinematicsSolver: Marker data does not correspond to any model markers." << std::endl;
throw Exception("InverseKinematicsSolver: Marker data does not correspond to any model markers.");
}
if(cnt < 4)
cout << "WARNING: InverseKinematicsSolver found only " << cnt << " markers to track." << endl;
}
int InverseKinematicsSolver::getNumMarkersInUse() const
{
return _markerAssemblyCondition->getNumMarkers();
}
/* Change the weighting of a marker to take effect when assemble or track is called next.
Update a marker's weight by name. */
void InverseKinematicsSolver::updateMarkerWeight(const std::string &markerName, double value)
{
const Array_<std::string> &names = _markersReference.getNames();
SimTK::Array_<const std::string>::iterator p = std::find(names.begin(), names.end(), markerName);
int index = (int)std::distance(names.begin(), p);
updateMarkerWeight(index, value);
}
/* Update a marker's weight by its index. */
void InverseKinematicsSolver::updateMarkerWeight(int markerIndex, double value)
{
if(markerIndex >=0 && markerIndex < _markersReference.updMarkerWeightSet().getSize()){
_markersReference.updMarkerWeightSet()[markerIndex].setWeight(value);
_markerAssemblyCondition->changeMarkerWeight(SimTK::Markers::MarkerIx(markerIndex), value);
}
else
throw Exception("InverseKinematicsSolver::updateMarkerWeight: invalid markerIndex.");
}
/* Update all markers weights by order in the markersReference passed in to
construct the solver. */
void InverseKinematicsSolver::updateMarkerWeights(const SimTK::Array_<double> &weights)
{
if(static_cast<unsigned>(_markersReference.updMarkerWeightSet().getSize())
== weights.size()){
for(unsigned int i=0; i<weights.size(); i++){
_markersReference.updMarkerWeightSet()[i].setWeight(weights[i]);
_markerAssemblyCondition->changeMarkerWeight(SimTK::Markers::MarkerIx(i), weights[i]);
}
}
else
throw Exception("InverseKinematicsSolver::updateMarkerWeights: invalid size of weights.");
}
/* Compute and return the spatial location of a marker in ground. */
SimTK::Vec3 InverseKinematicsSolver::computeCurrentMarkerLocation(const std::string &markerName)
{
const Array_<std::string> &names = _markersReference.getNames();
SimTK::Array_<const std::string>::iterator p = std::find(names.begin(), names.end(), markerName);
int index = (int)std::distance(names.begin(), p);
return computeCurrentMarkerLocation(index);
}
SimTK::Vec3 InverseKinematicsSolver::computeCurrentMarkerLocation(int markerIndex)
{
if(markerIndex >=0 && markerIndex < _markerAssemblyCondition->getNumMarkers()){
return _markerAssemblyCondition->findCurrentMarkerLocation(SimTK::Markers::MarkerIx(markerIndex));
}
else
throw Exception("InverseKinematicsSolver::computeCurrentMarkerLocation: invalid markerIndex.");
}
/* Compute and return the spatial locations of all markers in ground. */
void InverseKinematicsSolver::computeCurrentMarkerLocations(SimTK::Array_<SimTK::Vec3> &markerLocations)
{
markerLocations.resize(_markerAssemblyCondition->getNumMarkers());
for(unsigned int i=0; i<markerLocations.size(); i++)
markerLocations[i] = _markerAssemblyCondition->findCurrentMarkerLocation(SimTK::Markers::MarkerIx(i));
}
/* Compute and return the distance error between model marker and observation. */
double InverseKinematicsSolver::computeCurrentMarkerError(const std::string &markerName)
{
const Array_<std::string> &names = _markersReference.getNames();
SimTK::Array_<const std::string>::iterator p = std::find(names.begin(), names.end(), markerName);
int index = (int)std::distance(names.begin(), p);
return computeCurrentMarkerError(index);
}
double InverseKinematicsSolver::computeCurrentMarkerError(int markerIndex)
{
if(markerIndex >=0 && markerIndex < _markerAssemblyCondition->getNumMarkers()){
return _markerAssemblyCondition->findCurrentMarkerError(SimTK::Markers::MarkerIx(markerIndex));
}
else
throw Exception("InverseKinematicsSolver::computeCurrentMarkerError: invalid markerIndex.");
}
/* Compute and return the distance errors between all model markers and their observations. */
void InverseKinematicsSolver::computeCurrentMarkerErrors(SimTK::Array_<double> &markerErrors)
{
markerErrors.resize(_markerAssemblyCondition->getNumMarkers());
for(unsigned int i=0; i<markerErrors.size(); i++)
markerErrors[i] = _markerAssemblyCondition->findCurrentMarkerError(SimTK::Markers::MarkerIx(i));
}
/* Compute and return the squared-distance error between model marker and observation. */
double InverseKinematicsSolver::computeCurrentSquaredMarkerError(const std::string &markerName)
{
const Array_<std::string> &names = _markersReference.getNames();
SimTK::Array_<const std::string>::iterator p = std::find(names.begin(), names.end(), markerName);
int index = (int)std::distance(names.begin(), p);
return computeCurrentSquaredMarkerError(index);
}
double InverseKinematicsSolver::computeCurrentSquaredMarkerError(int markerIndex)
{
if(markerIndex >=0 && markerIndex < _markerAssemblyCondition->getNumMarkers()){
return _markerAssemblyCondition->findCurrentMarkerErrorSquared(SimTK::Markers::MarkerIx(markerIndex));
}
else
throw Exception("InverseKinematicsSolver::computeCurrentMarkerSquaredError: invalid markerIndex.");
}
/* Compute and return the distance errors between all model marker and observations. */
void InverseKinematicsSolver::computeCurrentSquaredMarkerErrors(SimTK::Array_<double> &markerErrors)
{
markerErrors.resize(_markerAssemblyCondition->getNumMarkers());
for(unsigned int i=0; i<markerErrors.size(); i++)
markerErrors[i] = _markerAssemblyCondition->findCurrentMarkerErrorSquared(SimTK::Markers::MarkerIx(i));
}
/* Marker errors are reported in order different from tasks file or model, find name corresponding to passed in index */
std::string InverseKinematicsSolver::getMarkerNameForIndex(int markerIndex) const
{
return _markerAssemblyCondition->getMarkerName(SimTK::Markers::MarkerIx(markerIndex));
}
/* Internal method to convert the MarkerReferences into additional goals of the
of the base assembly solver, that is going to do the assembly. */
void InverseKinematicsSolver::setupGoals(SimTK::State &s)
{
// Setup coordinates performed by the base class
AssemblySolver::setupGoals(s);
std::unique_ptr<SimTK::Markers> condOwner(new SimTK::Markers());
_markerAssemblyCondition.reset(condOwner.get());
// Setup markers goals
// Get lists of all markers by names and corresponding weights from the MarkersReference
const SimTK::Array_<SimTK::String> &markerNames = _markersReference.getNames();
SimTK::Array_<double> markerWeights;
_markersReference.getWeights(s, markerWeights);
// get markers defined by the model
const MarkerSet &modelMarkerSet = getModel().getMarkerSet();
int index = -1;
SimTK::Transform X_BF;
//Loop through all markers in the reference
for(unsigned int i=0; i < markerNames.size(); ++i){
// Check if we have this marker in the model, else ignore it
index = modelMarkerSet.getIndex(markerNames[i], index);
if(index >= 0) {
Marker &marker = modelMarkerSet[index];
const SimTK::MobilizedBody& mobod =
marker.getParentFrame().getMobilizedBody();
X_BF = marker.getParentFrame().findTransformInBaseFrame();
_markerAssemblyCondition->
addMarker(marker.getName(), mobod, X_BF*marker.get_location(),
markerWeights[i]);
}
}
// Add marker goal to the ik objective and transfer ownership of the
// goal (AssemblyCondition) to Assembler
updAssembler().adoptAssemblyGoal(condOwner.release());
// lock-in the order that the observations (markers) are in and this cannot change from frame to frame
// and we can use an array of just the data for updating
_markerAssemblyCondition->defineObservationOrder(markerNames);
updateGoals(s);
}
/* Internal method to update the time, reference values and/or their weights based
on the state */
void InverseKinematicsSolver::updateGoals(const SimTK::State &s)
{
// update coordinates performed by the base class
AssemblySolver::updateGoals(s);
// specify the (initial) observations to be matched
_markersReference.getValues(s, _markerValues);
_markerAssemblyCondition->moveAllObservations(_markerValues);
}
} // end of namespace OpenSim