quadratics, axi render command

This commit is contained in:
Michael Fogleman 2018-02-13 20:53:49 -05:00
parent 02048e18b9
commit 1385886ab1
3 changed files with 48 additions and 5 deletions

View File

@ -3,7 +3,8 @@ from __future__ import division
from math import sin, cos, radians from math import sin, cos, radians
from .paths import ( from .paths import (
simplify_paths, sort_paths, join_paths, crop_paths, convex_hull) simplify_paths, sort_paths, join_paths, crop_paths, convex_hull,
expand_quadratics)
try: try:
import cairo import cairo
@ -19,10 +20,11 @@ class Drawing(object):
def loads(cls, data): def loads(cls, data):
paths = [] paths = []
for line in data.split('\n'): for line in data.split('\n'):
points = line.strip().split() path = line.strip().split()
points = [map(float, x.split(',')) for x in points] path = [map(float, x.split(',')) for x in path]
if points: path = expand_quadratics(path)
paths.append(points) if path:
paths.append(path)
return cls(paths) return cls(paths)
@classmethod @classmethod

View File

@ -12,6 +12,15 @@ def main():
return return
command, args = args[0], args[1:] command, args = args[0], args[1:]
command = command.lower() command = command.lower()
if command == 'render':
d = axi.Drawing.load(args[0])
d = d.rotate_and_scale_to_fit(12, 8.5, step=90)
path = args[1] if len(args) > 1 else 'out.png'
im = d.render(
scale=109 * 1, line_width=0.25/25.4)
# show_axi_bounds=False, use_axi_bounds=False)
im.write_to_png(path)
return
device = axi.Device() device = axi.Device()
if command == 'zero': if command == 'zero':
device.zero_position() device.zero_position()

View File

@ -119,3 +119,35 @@ def convex_hull(points):
if isinstance(hull, geometry.Point): if isinstance(hull, geometry.Point):
return list(hull.coords) return list(hull.coords)
raise Exception('unhandled convex hull geometry') raise Exception('unhandled convex hull geometry')
def quadratic_path(x0, y0, x1, y1, x2, y2):
n = int(hypot(x1 - x0, y1 - y0) + hypot(x2 - x1, y2 - y1))
n = max(n, 4)
points = []
m = 1 / float(n - 1)
for i in range(n):
t = i * m
u = 1 - t
a = u * u
b = 2 * u * t
c = t * t
x = a * x0 + b * x1 + c * x2
y = a * y0 + b * y1 + c * y2
points.append((x, y))
return points
def expand_quadratics(path):
result = []
previous = (0, 0)
for point in path:
if len(point) == 2:
result.append(point)
previous = point
elif len(point) == 4:
x0, y0 = previous
x1, y1, x2, y2 = point
result.extend(quadratic_path(x0, y0, x1, y1, x2, y2))
previous = (x2, y2)
else:
raise Exception('invalid point: %r' % point)
return result