Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up IK by removing patella constraints #174

Merged
merged 3 commits into from
Jul 3, 2024
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
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ def main(sessionName, trialName, trial_id, cameras_to_use=['all'],
# Run IK tool.
logging.info('Running Inverse Kinematics')
try:
pathOutputIK = runIKTool(
pathOutputIK, pathModelIK = runIKTool(
pathGenericSetupFile4IK, pathScaledModel,
pathTRCFile4IK, outputIKDir)
except Exception as e:
Expand All @@ -608,7 +608,7 @@ def main(sessionName, trialName, trial_id, cameras_to_use=['all'],
os.makedirs(outputJsonVisDir,exist_ok=True)
outputJsonVisPath = os.path.join(outputJsonVisDir,
trialName + '.json')
generateVisualizerJson(pathScaledModel, pathOutputIK,
generateVisualizerJson(pathModelIK, pathOutputIK,
outputJsonVisPath,
vertical_offset=vertical_offset)

Expand Down
39 changes: 36 additions & 3 deletions utilsOpenSim.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,44 @@ def runIKTool(pathGenericSetupFile, pathScaledModel, pathTRCFile,
pathOutputSetup = os.path.join(
pathOutputFolder, 'Setup_IK_' + IKFileName + '.xml')

# Setup IK tool.
# To make IK faster, we remove the patellas and their constraints from the
# model. Constraints make the IK problem more difficult, and the patellas
# are not used in the IK solution for this particular model. Since muscles
# are attached to the patellas, we also remove all muscles.
opensim.Logger.setLevelString('error')
model = opensim.Model(pathScaledModel)
# Remove all actuators.
forceSet = model.getForceSet()
forceSet.setSize(0)
# Remove patellofemoral constraints.
constraintSet = model.getConstraintSet()
patellofemoral_constraints = [
'patellofemoral_knee_angle_r_con', 'patellofemoral_knee_angle_l_con']
for patellofemoral_constraint in patellofemoral_constraints:
i = constraintSet.getIndex(patellofemoral_constraint, 0)
constraintSet.remove(i)
# Remove patella bodies.
bodySet = model.getBodySet()
patella_bodies = ['patella_r', 'patella_l']
for patella in patella_bodies:
i = bodySet.getIndex(patella, 0)
bodySet.remove(i)
# Remove patellofemoral joints.
jointSet = model.getJointSet()
patellofemoral_joints = ['patellofemoral_r', 'patellofemoral_l']
for patellofemoral in patellofemoral_joints:
i = jointSet.getIndex(patellofemoral, 0)
jointSet.remove(i)
# Print the model to a new file.
model.finalizeConnections
model.initSystem()
pathScaledModelWithoutPatella = pathScaledModel.replace('.osim', '_no_patella.osim')
model.printToXML(pathScaledModelWithoutPatella)

# Setup IK tool.
IKTool = opensim.InverseKinematicsTool(pathGenericSetupFile)
IKTool.setName(IKFileName)
IKTool.set_model_file(pathScaledModel)
IKTool.set_model_file(pathScaledModelWithoutPatella)
IKTool.set_marker_file(pathTRCFile)
if timeRange:
IKTool.set_time_range(0, timeRange[0])
Expand All @@ -180,7 +213,7 @@ def runIKTool(pathGenericSetupFile, pathScaledModel, pathTRCFile,
command = 'opensim-cmd -o error' + ' run-tool ' + pathOutputSetup
os.system(command)

return pathOutputMotion
return pathOutputMotion, pathScaledModelWithoutPatella


# %% This function will look for a time window, of a minimum duration specified
Expand Down
Loading