Skip to content

Instantly share code, notes, and snippets.

Created November 5, 2016 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/5ea7c3535d4abca6794c11b150527c3c to your computer and use it in GitHub Desktop.
Save anonymous/5ea7c3535d4abca6794c11b150527c3c to your computer and use it in GitHub Desktop.
import os
def read_targets(tf):
hd = []
tv = {}
for line in tf:
if line[0].isdigit():
n, x, y, z = line.split()
tv[int(n)] = (float(x), float(y), float(z))
else:
hd.append(line)
return tv, hd
def fstr(f):
if f == 0.0:
return '0'
s = ('%.3f' % f).rstrip('0')
if s[-1] == '0': print(s)
if f > 0.0 and f < 1.0:
return s[1:]
if f < 0.0 and f > -1.0:
return '-' + s[2:]
return s
def write_targets(tv, hd, dir, file):
if not os.path.exists(dir):
os.mkdir(dir)
tf = open(dir + '/' + file, 'w')
for line in hd:
tf.write(line)
for n, v in tv.items():
tf.write('%d %s %s %s\n' % (n, fstr(v[0]), fstr(v[1]), fstr(v[2])))
rknee = (4610, 4629, 6405, 6406)
lknee = (11228, 11247, 13001, 13002)
rjoint = (14278, 14285)
ljoint = (13838, 13845)
def have_knee(tv, knee, joint):
for n in knee:
if n not in tv:
return False;
for n in range(joint[0], joint[1] + 1):
if n not in tv:
return False
return True
def fix_knee(tv, knee, joint):
x, y, z = 0.0, 0.0, 0.0
for n in knee:
v = tv[n]
x = x + v[0]
y = y + v[1]
z = z + v[2]
x, y, z = x * 0.25, y * 0.25, z * 0.25
for n in range(joint[0], joint[1] + 1):
tv[n] = (x, y, z)
targetdir = 'data/targets/macrodetails/height'
targetname = 'height.target'
for filename in os.listdir(targetdir):
if filename.endswith(targetname):
tf = open(targetdir + '/' + filename)
tv, hd = read_targets(tf)
if have_knee(tv, rknee, rjoint) and have_knee(tv, lknee, ljoint):
fix_knee(tv, rknee, rjoint)
fix_knee(tv, lknee, ljoint)
write_targets(tv, hd, targetdir + '_', filename)
else:
print(filename, 'skipped.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment