from simplification.cutil import simplify_coords_vw_idx
import matplotlib.pyplot as plt

with open('U_full.txt', 'r') as f:
	u_full = [float(i) for i in f.read().splitlines() if i]
with open('F_full.txt', 'r') as f:
	f_full = [float(i) for i in f.read().splitlines() if i]
N1 = len(u_full)
print('Input number of points is {}'.format(N1))
plt.plot(u_full, f_full, '-k')
plt.show()

print('Cutting curve to 11 mm...')
N2 = N1
for i in range(N1):
	if abs(u_full[i]) >= 11.0:
		N2 = i+1
		break
u_full = u_full[0:N2]
f_full = f_full[0:N2]
print('Input number of points is {}'.format(N2))
plt.plot(u_full, f_full, '-k')
plt.show()

coords = [[ix,iy] for ix,iy in zip(range(N2), u_full)]
simplified = simplify_coords_vw_idx(coords, 1.0)
N3 = len(simplified)
print('Reduced number of points is {}'.format(N3))
u_reduced = [u_full[i] for i in simplified]
f_reduced = [f_full[i] for i in simplified]
plt.plot(u_full, f_full, '--k')
plt.plot(u_reduced, f_reduced, '-r')
plt.show()

with open('U_reduced.txt', 'w+') as f:
	f.write('{}\n'.format(0.0))
	for i in u_reduced:
		f.write('{}\n'.format(i))
with open('F_reduced.txt', 'w+') as f:
	f.write('{}\n'.format(0.0))
	for i in f_reduced:
		f.write('{}\n'.format(i))