pull from osm branch

This commit is contained in:
Michael Fogleman 2018-01-19 14:26:35 -05:00
parent 422af94b37
commit 2037979ac0
2 changed files with 21 additions and 7 deletions

View File

@ -20,15 +20,25 @@ class Drawing(object):
for line in data.split('\n'): for line in data.split('\n'):
points = line.strip().split() points = line.strip().split()
points = [map(float, x.split(',')) for x in points] points = [map(float, x.split(',')) for x in points]
if points:
paths.append(points) paths.append(points)
return cls(paths) return cls(paths)
@classmethod
def load(cls, filename):
with open(filename, 'r') as fp:
return cls.loads(fp.read())
def dumps(self): def dumps(self):
lines = [] lines = []
for path in self.paths: for path in self.paths:
lines.append(' '.join('%f,%f' % (x, y) for x, y in path)) lines.append(' '.join('%f,%f' % (x, y) for x, y in path))
return '\n'.join(lines) return '\n'.join(lines)
def dump(self, filename):
with open(filename, 'w') as fp:
fp.write(self.dumps())
@property @property
def bounds(self): def bounds(self):
if not self._bounds: if not self._bounds:
@ -148,11 +158,12 @@ class Drawing(object):
return drawing.scale(scale, scale).center(width, height) return drawing.scale(scale, scale).center(width, height)
def remove_paths_outside(self, width, height): def remove_paths_outside(self, width, height):
e = 1e-8
paths = [] paths = []
for path in self.paths: for path in self.paths:
ok = True ok = True
for x, y in path: 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 ok = False
break break
if ok: if ok:

View File

@ -64,10 +64,12 @@ def join_paths(paths, tolerance):
return result return result
def crop_interpolate(x1, y1, x2, y2, ax, ay, bx, by): def crop_interpolate(x1, y1, x2, y2, ax, ay, bx, by):
t1 = (x1 - ax) / (bx - ax) dx = bx - ax
t2 = (y1 - ay) / (by - ay) dy = by - ay
t3 = (x2 - ax) / (bx - ax) t1 = (x1 - ax) / dx if dx else -1
t4 = (y2 - ay) / (by - ay) 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 = [t1, t2, t3, t4]
ts = [t for t in ts if t >= 0 and t <= 1] ts = [t for t in ts if t >= 0 and t <= 1]
t = min(ts) t = min(ts)
@ -76,12 +78,13 @@ def crop_interpolate(x1, y1, x2, y2, ax, ay, bx, by):
return (x, y) return (x, y)
def crop_path(path, x1, y1, x2, y2): def crop_path(path, x1, y1, x2, y2):
e = 1e-9
result = [] result = []
buf = [] buf = []
previous_point = None previous_point = None
previous_inside = False previous_inside = False
for x, y in path: 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 inside:
if not previous_inside and previous_point: if not previous_inside and previous_point:
px, py = previous_point px, py = previous_point