import numpy as np import time import datetime def timeit(func): def f(*args, **kwargs): t0 = time.time() retval = func(*args, **kwargs) elapsed = time.time() - t0 print '%s elapsed in %s().' % (datetime.timedelta(seconds=elapsed), func.__name__) return retval return f def read_csv(filename): """Return an array of comma-separated values from `filename`.""" f = open(filename) points = [] for line in f: try: points.append([float(s) for s in line.split(',')]) except ValueError: pass f.close() return np.array(points) def offset(points, x): """ Return the set of points obtained by offsetting the edges of the profile created by `points` by an amount `x`. Args: - points: array Array of points which define the 2-D profile to be offset. - x: float Distance to offset the profile; a positive `x` value will offset the profile in the direction of the profile path rotated 90 degrees clockwise. """ points = np.asarray(points) points = np.array([points[0] - (points[1] - points[0])] + list(points) + [points[-1] - (points[-2] - points[-1])]) offset_points = [] for i in range(1,len(points)-1): v1 = np.cross(points[i]-points[i-1], (0,0,1))[:2] v1 /= np.linalg.norm(v1) v1 *= x a = points[i-1] + v1 b = points[i] + v1 v2 = np.cross(points[i+1]-points[i], (0,0,1))[:2] v2 /= np.linalg.norm(v2) v2 *= x c = points[i] + v2 d = points[i+1] + v2 m = np.empty((2,2)) m[:,0] = b-a m[:,1] = c-d try: j = np.linalg.solve(m, c-a)[0] except np.linalg.linalg.LinAlgError as e: offset_points.append(b) continue offset_points.append((a + j*(b-a))) return np.array(offset_points)