From: Ian Jackson Date: Sat, 7 Apr 2018 20:00:14 +0000 (+0100) Subject: moebius: break out DiscreteBezier (nfc) X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=c30789ad362e23523d301a4858ca03c1ffde97bf;p=moebius3.git moebius: break out DiscreteBezier (nfc) Signed-off-by: Ian Jackson --- diff --git a/moebius.py b/moebius.py index 58d79f6..aadb42a 100644 --- a/moebius.py +++ b/moebius.py @@ -68,6 +68,15 @@ class Twirler(ParametricCircle): r = tw.radius(theta) return cos(zeta) * r + sin(zeta) * tw._axis +class DiscreteBezier(): + def __init__(b, cp, nt, bezier_constructor=DoubleCubicBezier): + b.nt = nt + b._t_vals = np.linspace(0, 1, b.nt+1) + b._continuous_bezier = bezier_constructor(cp) + def point_at_it(b, it): + t = b._t_vals[it] + return b._continuous_bezier.point_at_t(t) + class MoebiusHalf: def __init__(m, nu, nt): ''' @@ -81,9 +90,8 @@ class MoebiusHalf: m.nt = nt m._thetas = [ u * tau for u in np.linspace(0, 1, nu+1) ] m._cp2b = BezierSegment([ (c,) for c in [0.33,0.33, 1.50]]) - m._beziers = [ m._bezier(theta) for theta in m._thetas ] - m._t_vals = np.linspace(0, 1, m.nt+1) - def _bezier(m, theta, constructor=DoubleCubicBezier): + m._dbeziers = [ m._dbezier(theta) for theta in m._thetas ] + def _dbezier(m, theta, dconstructor=DiscreteBezier): cp = [None] * 4 cp[0] = m.edge .point(theta) cp[3] = m.midline.point(theta*2) @@ -95,7 +103,7 @@ class MoebiusHalf: # file=sys.stderr) cp[1] = cp[0] + cp1scale * m.edge .dirn (theta) cp[2] = cp[3] + cp2scale * m.midline.dirn (theta*2) - return constructor(cp) + return dconstructor(cp, m.nt) def point(m, iu, it): ''' 0 <= iu <= nu meaning 0 <= u <= 1 @@ -105,8 +113,7 @@ class MoebiusHalf: 0 <= it <= nt across the half-traverse 0 is the edge, 1 is the midline ''' - t = m._t_vals[it] - return np.array(m._beziers[iu].point_at_t(t)) + return np.array(m._dbeziers[iu].point_at_it(it)) def details(m, iu, t): '''