From 2037979ac05b06a9061cc519ccd155192276b04b Mon Sep 17 00:00:00 2001 From: Michael Fogleman Date: Fri, 19 Jan 2018 14:26:35 -0500 Subject: [PATCH] pull from osm branch --- axi/drawing.py | 15 +++++++++++++-- axi/paths.py | 13 ++++++++----- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/axi/drawing.py b/axi/drawing.py index 6e322f2..79fff94 100644 --- a/axi/drawing.py +++ b/axi/drawing.py @@ -20,15 +20,25 @@ class Drawing(object): for line in data.split('\n'): points = line.strip().split() points = [map(float, x.split(',')) for x in points] - paths.append(points) + if points: + paths.append(points) return cls(paths) + @classmethod + def load(cls, filename): + with open(filename, 'r') as fp: + return cls.loads(fp.read()) + def dumps(self): lines = [] for path in self.paths: lines.append(' '.join('%f,%f' % (x, y) for x, y in path)) return '\n'.join(lines) + def dump(self, filename): + with open(filename, 'w') as fp: + fp.write(self.dumps()) + @property def bounds(self): if not self._bounds: @@ -148,11 +158,12 @@ class Drawing(object): return drawing.scale(scale, scale).center(width, height) def remove_paths_outside(self, width, height): + e = 1e-8 paths = [] for path in self.paths: ok = True for x, y in path: - if x < 0 or y < 0 or x > width or y > height: + if x < -e or y < -e or x > width + e or y > height + e: ok = False break if ok: diff --git a/axi/paths.py b/axi/paths.py index b3a6ec1..4c9c990 100644 --- a/axi/paths.py +++ b/axi/paths.py @@ -64,10 +64,12 @@ def join_paths(paths, tolerance): return result def crop_interpolate(x1, y1, x2, y2, ax, ay, bx, by): - t1 = (x1 - ax) / (bx - ax) - t2 = (y1 - ay) / (by - ay) - t3 = (x2 - ax) / (bx - ax) - t4 = (y2 - ay) / (by - ay) + dx = bx - ax + dy = by - ay + t1 = (x1 - ax) / dx if dx else -1 + t2 = (y1 - ay) / dy if dy else -1 + t3 = (x2 - ax) / dx if dx else -1 + t4 = (y2 - ay) / dy if dy else -1 ts = [t1, t2, t3, t4] ts = [t for t in ts if t >= 0 and t <= 1] t = min(ts) @@ -76,12 +78,13 @@ def crop_interpolate(x1, y1, x2, y2, ax, ay, bx, by): return (x, y) def crop_path(path, x1, y1, x2, y2): + e = 1e-9 result = [] buf = [] previous_point = None previous_inside = False for x, y in path: - inside = x >= x1 and y >= y1 and x <= x2 and y <= y2 + inside = x >= x1 - e and y >= y1 - e and x <= x2 + e and y <= y2 + e if inside: if not previous_inside and previous_point: px, py = previous_point