diff options
Diffstat (limited to 'tools.py')
-rw-r--r-- | tools.py | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/tools.py b/tools.py new file mode 100644 index 0000000..51ef2e0 --- /dev/null +++ b/tools.py @@ -0,0 +1,62 @@ +import numpy as np + +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) |