[76e9f4]: / tool_funcs / getBodyJoint.m

Download this file

72 lines (54 with data), 2.3 kB

 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
%-------------------------------------------------------------------------%
% Copyright (c) 2021 Modenese L. %
% Author: Luca Modenese, 2021 %
% email: l.modenese@imperial.ac.uk %
% ----------------------------------------------------------------------- %
% Function replacing the old getJoint() method that we all loved in OpenSim
% 3.3 but currently non available in OpenSim 4.x.
% Returns the joint for which the specified body would be the child.
% ----------------------------------------------------------------------- %
function bodyJoint = getBodyJoint(osimModel, aBodyName, debug_printout)
import org.opensim.modeling.*;
if nargin<3; debug_printout=0; end
% default
bodyJoint = [];
if strcmp(aBodyName, 'ground')
return
end
% check if body is included in the model
if osimModel.getBodySet().getIndex(aBodyName)<0
error(['getBodyJoint.m The specified body ', aBodyName,' is not included in the OpenSim model'])
end
% get jointset
jointSet = osimModel.getJointSet();
% loop through jointset
nj = 1;
for n_joint = 0:jointSet.getSize()-1
% get cur joint
cur_joint = jointSet.get(n_joint);
% child frame from joint
child_frame = cur_joint.getChildFrame();
% link back to base frame: this could be a body
body_of_frame = child_frame.findBaseFrame();
% get base frame name
possible_body_name = char(body_of_frame.getName());
% if body with that name exist than the joint is that body's joint
if osimModel.getBodySet.getIndex(possible_body_name)>=0 && strcmp(aBodyName, possible_body_name)
% save the joints with the specified body as Child
jointName(nj) = {char(cur_joint.getName())};
if debug_printout
disp([aBodyName, ' is parent frame on joint: ',jointName{nj}]);
end
% update counter
nj = nj + 1;
continue
end
end
% return a string if there is only one body
if numel(jointName)==1
jointName = jointName{1};
bodyJoint = osimModel.getJointSet.get(jointName);
else
error(['getBodyJoint.m More than one joint connected to body of interest', aBodyName,'. This function is design to work as getJoint() in OpenSim 3.3.'])
end
end