a b/tool_funcs/getBodyJoint.m
1
%-------------------------------------------------------------------------%
2
%    Copyright (c) 2021 Modenese L.                                       %
3
%    Author:   Luca Modenese,  2021                                       %
4
%    email:    l.modenese@imperial.ac.uk                                  %
5
% ----------------------------------------------------------------------- %
6
% Function replacing the old getJoint() method that we all loved in OpenSim
7
% 3.3 but currently non available in OpenSim 4.x.
8
% Returns the joint for which the specified body would be the child.
9
% ----------------------------------------------------------------------- %
10
function bodyJoint = getBodyJoint(osimModel, aBodyName, debug_printout)
11
12
import org.opensim.modeling.*;
13
14
if nargin<3; debug_printout=0; end
15
16
% default
17
bodyJoint = [];
18
19
if strcmp(aBodyName, 'ground')
20
    return
21
end
22
23
% check if body is included in the model
24
if osimModel.getBodySet().getIndex(aBodyName)<0
25
    error(['getBodyJoint.m The specified body ', aBodyName,' is not included in the OpenSim model'])
26
end
27
28
% get jointset
29
jointSet = osimModel.getJointSet();
30
31
% loop through jointset
32
nj = 1;
33
34
for n_joint = 0:jointSet.getSize()-1
35
    
36
    % get cur joint
37
    cur_joint = jointSet.get(n_joint);
38
    
39
    % child frame from joint
40
    child_frame = cur_joint.getChildFrame();
41
    
42
    % link back to base frame: this could be a body
43
    body_of_frame = child_frame.findBaseFrame();
44
    
45
    % get base frame name
46
    possible_body_name = char(body_of_frame.getName());
47
48
    % if body with that name exist than the joint is that body's joint
49
    if osimModel.getBodySet.getIndex(possible_body_name)>=0 && strcmp(aBodyName, possible_body_name)
50
        
51
        % save the joints with the specified body as Child 
52
        jointName(nj) = {char(cur_joint.getName())};
53
        
54
        if debug_printout
55
            disp([aBodyName, ' is parent frame on joint: ',jointName{nj}]);
56
        end
57
        % update counter
58
        nj = nj + 1;
59
        continue
60
    end
61
end
62
63
% return a string if there is only one body
64
if numel(jointName)==1
65
    jointName = jointName{1};
66
    bodyJoint = osimModel.getJointSet.get(jointName);
67
else
68
    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.'])
69
end
70
71
end