Skip to content

Commit

Permalink
Merge pull request #174 from stanfordnmbl/dev
Browse files Browse the repository at this point in the history
Speed up IK by removing patella constraints
  • Loading branch information
antoinefalisse authored Jul 3, 2024
2 parents 637af74 + 20fc16a commit 305eef5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
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

0 comments on commit 305eef5

Please sign in to comment.