Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions matlab_tool_v4.0/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.log
*.asv
Binary file not shown.
116 changes: 116 additions & 0 deletions matlab_tool_v4.0/TEST_plugin.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
%-------------------------------------------------------------------------%
% Copyright (c) 2019 Modenese L. %
% Author: Luca Modenese %
% email: l.modenese@imperial.ac.uk %
% ----------------------------------------------------------------------- %
% Load Library
import org.opensim.modeling.*;

% verification
osimModel_name = '../_test_data/MFD_tests/testModel.osim';
IK_mot_file = '../_test_data/MFD_tests/testKinematics_file.mot';
results_directory = '../_test_data/MFD_tests';
bodyOfInterest_name = 'MovingBody';
bodyExpressResultsIn_name = [];
effective_attachm = 'false';
print_attachm = 'true';
vis_on = 'true';
N_frame_test = 5;

% validated_results_folder = '../_test_data/MFD_tests/validated_res';

res_anatAttach_local = getMuscleForceDirection(osimModel_name,...
IK_mot_file,...
results_directory,...
bodyOfInterest_name,...
bodyExpressResultsIn_name,...
effective_attachm,...
print_attachm,...
vis_on,...
N_frame_test);

% res_effectAttach_local = getMuscleForceDirection(osimModel_name,...
% IK_mot_file,...
% results_directory,...
% bodyOfInterest_name,...
% bodyExpressResultsIn_name,...
% 'true',...
% print_attachm,...
% vis_on,...
% []);
%
% res_effectAttach_ground = getMuscleForceDirection(osimModel_name,...
% IK_mot_file,...
% results_directory,...
% bodyOfInterest_name,...
% 'ground',...
% 'true',...
% print_attachm,...
% vis_on,...
% []);
%
% res_anatAttach_ground = getMuscleForceDirection(osimModel_name,...
% IK_mot_file,...
% results_directory,...
% bodyOfInterest_name,...
% 'ground',...
% 'false',...
% print_attachm,...
% vis_on,...
% []);


% res_anatAttach_local
% res_anatAttach_ground
% res_effectAttach_ground
% res_effectAttach_local
%
% res_anatAttach_local_val = sto2Mat(fullfile(validated_results_folder,'LOCAL_ANATOM_MuscleForceDirection_vectors.sto'));
%
% res_anatAttach_ground.rowheaders
% res_effectAttach_ground
% res_effectAttach_local
%--------------------------------------------------------------------------
% %% test 1: simple arm26 model
% osimModel_name = '../_test_data/Arm26/arm26.osim';
% IK_mot_file = '../_test_data/Arm26/elbow_flexion.mot';
% results_directory = '../_test_data/Arm26';
% bodyOfInterest_name = 'r_humerus';
% bodyExpressResultsIn_name = [];
% effective_attachm = 'true';
% print_attachm = 'true';
% vis_on = 'true';
% N_frame_test = 5;
%
% muscleLinesOfActionStruct = getMuscleForceDirection(osimModel_name,...
% IK_mot_file,...
% results_directory,...
% bodyOfInterest_name,...
% bodyExpressResultsIn_name,...
% effective_attachm,...
% print_attachm,...
% vis_on,...
% N_frame_test);
%
%
%--------------------------------------------------------------------------
%% test 2: gait2392 model
osimModel_name = '../_test_data/gait2392/subject01.osim';
IK_mot_file = '../_test_data/gait2392/subject01_walk1_ik.mot';
results_directory = '../_test_data/gait2392';
bodyOfInterest_name = 'femur_r';
bodyExpressResultsIn_name = [];
effective_attachm = 'true';
print_attachm = 'true';
vis_on = 'true';
N_frame_test = 5;

muscleLinesOfActionStruct = getMuscleForceDirection(osimModel_name,...
IK_mot_file,...
results_directory,...
bodyOfInterest_name,...
bodyExpressResultsIn_name,...
effective_attachm,...
print_attachm,...
vis_on,...
N_frame_test);
65 changes: 65 additions & 0 deletions matlab_tool_v4.0/_dev_deal_withMuscleWrap.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
%-------------------------------------------------------------------------%
% Copyright (c) 2019 Modenese L. %
% Author: Luca Modenese %
% email: l.modenese@imperial.ac.uk %
% ----------------------------------------------------------------------- %

clear;clc
import org.opensim.modeling.*

% TO DO: add the possibility of following states
%%%%%%%%%%%%% SET UP %%%%%%%%%%%%%%%%%%%%%%%%%%%%
% General model
ModelFile = 'arm26.osim';
% read model
osimModel = Model(ModelFile);
muscles = osimModel.getMuscles();
aOsimMuscleNameSet = 'all';%{'BRA'};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% setting which muscles will be followed
muscleNames = ArrayStr;
if strcmp(aOsimMuscleNameSet,'all')
% getmuscles
muscles.getNames(muscleNames);
else
% assign given muscle names to the set to be analyzed
for n_mn = 1:length( aOsimMuscleNameSet )
muscleNames.append(aOsimMuscleNameSet{n_mn})
end
end

for n_mus = 0:muscleNames.getSize()-1

% current muscle
curr_muscle = muscles.get(muscleNames.getitem(n_mus));

% EXTRACTING THE WRAPPING POINTS
si = osimModel.initSystem();

% get the geometry path for the current state
PathpointArray = curr_muscle.getGeometryPath().getCurrentPath(si);

% normal pathpoints attached to bodies
ModelPathpoint = curr_muscle.getGeometryPath().getPathPointSet();

% check if there are wrapping points
NrOfWrappingPoints = PathpointArray.getSize()-ModelPathpoint.getSize();

% header
display(['Muscle ',char(curr_muscle.getName()), ' (',num2str(NrOfWrappingPoints),' wrapping points)']);

for n_p = 0:PathpointArray.getSize()-1

% get the coordinates of the current pathpoint
curr_point_loc_OS = Vec3(PathpointArray.getitem(n_p).getLocation);
curr_point_loc = [curr_point_loc_OS.get(0),curr_point_loc_OS.get(1),curr_point_loc_OS.get(2)];

% body where the body is attached to
display(['Point ',num2str(n_p+1), ' attached to body ',char(PathpointArray.getitem(n_p).getBodyName())]);
display([num2str(curr_point_loc_OS.get(0)),' ',num2str(curr_point_loc_OS.get(1)),' ',num2str(curr_point_loc_OS.get(2))])

end
display('-----------------------------------');
end
33 changes: 33 additions & 0 deletions matlab_tool_v4.0/getBodyorGround.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function Body = getBodyorGround(osimModel,BodyName)
%-------------------------------------------------------------------------%
% getBodyorGround gets a Body or Ground by name from an OpenSim model %
% (version 4.0 and up). %
% %
% INPUTS: %
% - osimModel: the OpenSim model object. %
% - BodyName: name pf body to get, a character string. %
% %
% OUTPUTS: %
% - Body: target PhysicalFrame object, either a Body or Ground. %
% %
% code edition log: %
% 18 Sep 2019: created for OpenSim 4.0 model Body/Ground format (KS). %
% ----------------------------------------------------------------------- %
% Author: Ke Song (Washington University in St. Louis) %
% E-mail: ksong23@wustl.edu %
% ----------------------------------------------------------------------- %

% load Library
import org.opensim.modeling.*;

% get name of Ground in model
osimGroundName = char(osimModel.getGround.getName());

% get Ground or Body based on name
if strcmp(BodyName,osimGroundName)
Body = osimModel.getGround(); %Ground
else
Body = osimModel.getBodySet.get(BodyName);
end

end
76 changes: 76 additions & 0 deletions matlab_tool_v4.0/getCurrentMusclePathAsMat.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
%-------------------------------------------------------------------------%
% Copyright (c) 2019 Modenese L. %
% %
% 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. %
% %
% Author: Luca Modenese, 2017 %
% email: l.modenese@imperial.ac.uk %
% ----------------------------------------------------------------------- %
% Given a state, this script collects info about the path of a muscle.
% Bodies of muscle attachments and point coordinates are made available as
% a list and a matrix respectively.
%
% INPUTS:
% - osimMuscle: an OpenSim model (API object).
% - s: state, used to update the current muscle path and get
% active conditional viapoints.
%
% OUTPUTS:
% - mus_bodyset_list: matrix where each row is a muscle point and
% columns are its [X Y Z] coordinates in the reference system of
% the body they belong to (as in .osim file).
% - mus_pointset_mat: cell array of body names, same length as
% the PathPointSet, with the name of the Body for each point.
%
%
% last modified:
% 2017: created.
% 26 Jun 2018: comments (LM)
% 29 Nov 2018: renamed, recommented, modified, added to MFD Matlab version.
% 18 Sep 2019: using getCurrentPath to recognize wrap points (KS).
% ----------------------------------------------------------------------- %
function [mus_bodyset_list, mus_pointset_mat] = getCurrentMusclePathAsMat(osimMuscle, s)

% load libraries
import org.opensim.modeling.*;

% get geometry path point array (including wrap points) for current state
curr_PathPointSet = osimMuscle.getGeometryPath().getCurrentPath(s);

n_pp = 1;
for n_p = 0:curr_PathPointSet.getSize-1

% point under examination
curr_point = curr_PathPointSet.get(n_p);
attachBody = curr_point.getBodyName();

% if pathpoint is conditional, then check if it is active
if strcmp(char(curr_point.getConcreteClassName), 'ConditionalPathPoint')
cond_viapoint = ConditionalPathPoint.safeDownCast(curr_point);
% is it active?
if cond_viapoint.isActive(s)
% if yes add it to point set
mus_pointset_mat(n_pp, 1:3) = [curr_point.getLocation(s).get(0), curr_point.getLocation(s).get(1), curr_point.getLocation(s).get(2)]; %#ok<*AGROW>
mus_bodyset_list(n_pp) = {char(attachBody)};
n_pp = n_pp+1;
else
continue
end
else
% if not conditional add it
mus_pointset_mat(n_pp, 1:3) = [curr_point.getLocation(s).get(0), curr_point.getLocation(s).get(1), curr_point.getLocation(s).get(2)];
mus_bodyset_list(n_pp) = {char(attachBody)};
n_pp = n_pp+1;
end
end

end
53 changes: 53 additions & 0 deletions matlab_tool_v4.0/getMatStructColumn.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
%-------------------------------------------------------------------------%
% Copyright (c) 2019 Modenese L. %
% %
% 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. %
% %
% Author: Luca Modenese %
% email: l.modenese@imperial.ac.uk %
% ----------------------------------------------------------------------- %
%
% Function that allows to retrieve the value of a specified variable whose
% name is specified in var_name.
%
% INPUTS
% struct: is a structure with fields 'colheaders', the headers, and 'data'
% that is a matrix of data.
% var_name: the name of the variable to extract.
%
% OUTPUTS
% var_value: the column of the matrix correspondent to the header specified
% in input as var_name.
%
%
% written: Luca Modenese, July 2014
% last modified:
% Header (February 2018, LM)
% 29 Nov 2018: renamed, recommented, added to MFD Matlab version (LM).
% ----------------------------------------------------------------------- %
function var_value = getMatStructColumn(struct, var_name)

% gets the index of the desired variable name in the colheaders of the
% structure from where it will be extracted
var_index = strncmp(struct.colheaders, var_name, length(var_name));

if sum(var_index) == 0
%error(['getValueColumnForHeader.m','. No header in structure is matching the name ''',var_name,'''.'])
display(['getValueColumnForHeader.m','. No header in structure is matching the name ''',var_name,''''])
var_value = [];
return
end

% uses the index to retrieve the column of values for that variable.
var_value = struct.data(:,var_index);

end
Loading