<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/env python
'''
-------------------
Copyright (c) 2016 Computational Biomechanics (CoBi) Core, Department of
Biomedical Engineering, Cleveland Clinic

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the
following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
-------------------

post_process.py

Written by Craig Bennetts.
Updated and customized by Ahmet Erdemir.

Contact: 
 Ahmet Erdemir, PhD
 Computational Biomodeling (CoBi) Core
 Department of Biomedical Engineering
 Lerner Research Institute
 Cleveland Clinic
 Cleveland, OH 44195, USA
 erdemira@ccf.org

'''

import sys
import numpy as np

def usage(argv):
	print
	print 'USAGE: ' + argv[0]
	print


def main(argv):

	# Check for appropriate usage
	if len(argv) != 1:
		usage(argv)
		sys.exit(1)

	kinetics_filename = 'femur_kinetics.txt'
	t_kinetics, f, m = read_FEBio_kinetics_file(kinetics_filename)

	# ACL force
	f_ACL = np.linalg.norm(f, axis=1)

	kinematics_filename = 'femur_kinematics.txt'
	t_kinematics, u, q = read_FEBio_kinematics_file(kinematics_filename)

	post_process_filename = 'post_process.txt'

	fout = open(post_process_filename,'w')

	fout.write('Simulation Time, AP (mm), ML (mm), DC (mm), FE (deg), IE (deg), VV (deg), ACL Force (N)\n')

	for ti,ui,qi,fi in zip(t_kinematics, u, q, f_ACL):

		# transform for femur pose wrt. origin
		Ttf = pose_2_transform(ui,qi)
		Tft = np.linalg.inv(Ttf)
		clinical_dof = clinicalDecomposition(Tft)

		fout.write( '%8.3f, %8.3f, %8.3f, %8.3f, %8.3f, %8.3f, %8.3f, %8.3f\n' % (ti, clinical_dof['AP'], clinical_dof['ML'], clinical_dof['DC'], clinical_dof['FE'], clinical_dof['IE'], clinical_dof['VV'], fi))

	fout.close()

# END OF main()


def read_FEBio_kinematics_file(filename):

	finput = open(filename, 'r')
	lines = finput.readlines()
	finput.close()
	data_list = []
	for line in lines:
		if 'Time' in line:
			time  = line.strip('\n').split('=')[-1]
		elif not ('*' in line):
			values = line.strip('\n').split(' ')
			values[0] = time
			data_list.append(values)
	data = np.array(data_list, dtype=float)

	return data[:,0], data[:,1:4], data[:,4:]

def read_FEBio_kinetics_file(filename):

	finput = open(filename, 'r')
	lines = finput.readlines()
	finput.close()
	data_list = []
	for line in lines:
		if 'Time' in line:
			time  = line.strip('\n').split('=')[-1]
		elif not ('*' in line):
			values = line.strip('\n').split(' ')
			values[0] = time
			data_list.append(values)
	data = np.array(data_list, dtype=float)

	return data[:,0], data[:,1:4], data[:,4:]

def quaternion_2_RotMatrix(q):

	# Normalize the quaternion
	q_mag = np.sqrt( q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3] ) 

	qx = q[0]/q_mag
	qy = q[1]/q_mag
	qz = q[2]/q_mag
	qw = q[3]/q_mag

	R = np.zeros((3,3))

	R[0,0] = 1.0 - 2.0*qy*qy - 2.0*qz*qz
	R[0,1] = 2.0*qx*qy - 2.0*qz*qw
	R[0,2] = 2.0*qx*qz + 2.0*qy*qw
	R[1,0] = 2.0*qx*qy + 2.0*qz*qw
	R[1,1] = 1.0 - 2.0*qx*qx - 2.0*qz*qz
	R[1,2] = 2.0*qy*qz - 2.0*qx*qw
	R[2,0] = 2.0*qx*qz - 2.0*qy*qw
	R[2,1] = 2.0*qy*qz + 2.0*qx*qw
	R[2,2] = 1.0 - 2.0*qx*qx - 2.0*qy*qy

	return R

# FUNCTION TO CONVERT TRANSLATION AND QUATERNION TO 3D TRANFORM MATRIX
# INPUT:
# u = translation = list with 3 values [x,y,z]
# q = quaternion  = list with 4 values [qx,qy,qz,qw]
# OUTPUT:
# T = 3D transform, 4x4 array
def pose_2_transform(u,q):

	R = quaternion_2_RotMatrix(q)

	T = np.zeros((4,4))
	
	T[0:3,0:3] = R
	T[0:3,3] = u
	T[3,3] = 1.0

	return T

def clinicalDecomposition(Tft):
# motion of tibia relative to femur
# based on clinical dof convention and signs
# based on of grood &amp; suntay convention and signs
# based on model coordinate systems

	clinical_dof = {}
	alpha = np.arctan2(-Tft[1,2],Tft[2,2])
	clinical_dof['FE'] = -np.rad2deg(alpha)
	gamma = np.arctan2(-Tft[0,1],Tft[0,0])
	clinical_dof['IE'] =  np.rad2deg(gamma)
	beta = np.arctan2(Tft[0,2],np.sqrt(Tft[1,2]*Tft[1,2] + Tft[2,2]*Tft[2,2]))
	clinical_dof['VV'] =  np.rad2deg(beta)
	clinical_dof['AP'] = Tft[1,3]*np.cos(alpha) + Tft[2,3]*np.sin(alpha)
	clinical_dof['ML'] = -Tft[0,3]
	Ttf = np.linalg.inv(Tft)
	clinical_dof['DC'] = Ttf[2,3]

	return clinical_dof

if __name__ == "__main__":
    main(sys.argv)
</pre></body></html>