% This function is part of the NMSM Pipeline, see file for full license.
%
% This function returns a string array of the muscles in the model in the
% order they are listed in the model
%
% (Model) -> (Array of string)
% Returns the muscles in the model
function muscles = getMusclesFromCoordinates(model, ...
    coordinates)
if ~isa(model, 'org.opensim.modeling.Model')
    model = Model(model);
end
muscles = string([]);
for i=0:model.getForceSet().getMuscles().getSize() - 1
    if(isMuscleInCoordinates(model, coordinates, ...
            model.getForceSet().getMuscles().get(i)))
        muscles(end + 1) = ...
            model.getForceSet().getMuscles().get(i).getName().toCharArray()';
    end
end
end
function output = isMuscleInCoordinates(model, coordinates, muscle)
if muscle.get_appliesForce()
    path = muscle.get_GeometryPath().getPathPointSet();
    for j = 0 : path.getSize() - 1
        bodyCoordinates = getCoordinatesFromBodies(model, ...
            path.get(j).getBodyName().toCharArray()');
        for k = 1 : length(bodyCoordinates)
            if(any(strcmp(coordinates, bodyCoordinates(k))))
                output = true;
                return
            end
        end
    end
end
output = false;
end