Exemplo n.º 1
0
def Newton_bezier(t, b, ivar, val, itmax, tol):
    d = lbez.diff(b)
    for it in range(itmax):
        f = lbez.eval_bezier_curve(b, t) - val
        if abs(f[ivar]) < tol:
            return t
        df = lbez.eval_bezier_curve(d, t)
        if abs(df[ivar]) < 1e-15:
            return None
        dt = -f[ivar] / df[ivar]
        t += dt
    return None
Exemplo n.º 2
0
def bezier_extremal_values(b, ivar):
    d = lbez.diff(b)
    A = d[2][ivar] - 2 * d[1][ivar] + d[0][ivar]
    if abs(A) < 1e-15:
        return [0.5 * d[0][ivar] / (d[0][ivar] - d[1][ivar])]
    else:
        B = d[1][ivar] - d[0][ivar]
        C = d[0][ivar]
        sqrtdelta = numpy.sqrt(B**2 - A * C)
        t = []
        for sign in [-1, 1]:
            ts = (sign * sqrtdelta - B) / A
            if ts >= 0 and ts <= 1: t.append(ts)
        if len(t) < 1:
            fig, ax = plt.subplots()
            u = numpy.linspace(0, 1, 100)
            xy = lbez.eval_bezier_curve(d, u)
            ax.plot(u, xy[:, ivar])
            ax.plot([0, 1], [0, 0], 'k--')
            ax.set_aspect('equal')
            plt.show()
        return t
Exemplo n.º 3
0
def Newton_intersection(curves, t, itmax=30, tol=1e-6, verbose=False):
    deriv = [lbez.diff(b) for b in curves]
    for it in range(itmax):
        if verbose: print 'it. #%d, t = (%s, %s)' % (it, t[0], t[1])
        xy = [lbez.eval_bezier_curve(curves[i], t[i]) for i in range(2)]
        if verbose:
            for i in range(2):
                print '   xy_%d = (%s, %s)' % (i, xy[i][0], xy[i][1])
        res = xy[1] - xy[0]
        if verbose: print '   |res| = %s' % (numpy.hypot(res[0], res[1]))
        #
        if sum(res**2) < tol**2:
            return t
        #
        dxy = [lbez.eval_bezier_curve(deriv[i], t[i]) for i in range(2)]
        dxy[1] = -dxy[1]
        det = dxy[0][0] * dxy[1][1] - dxy[0][1] * dxy[1][0]
        if verbose: print '   |det| = %s\n' % (abs(det))
        if abs(det) < 1e-15:
            return None
        t[0] += (res[0] * dxy[1][1] - res[1] * dxy[1][0]) / det
        t[1] += (dxy[0][0] * res[1] - dxy[0][1] * res[0]) / det
    return None
 def update(self):
     self.xt = lbez.diff(self.x)
     self.xtt = lbez.diff(self.xt)
     return
Exemplo n.º 5
0
polys = []
for path in paths:
    poly = numpy.empty((0, 2))
    bpoly = numpy.empty((0, 2))
    for i, segm in enumerate(path):
        xy = lbez.eval_bezier_curve(segm, t)
        poly = numpy.vstack([poly, xy])
        bpoly = numpy.vstack([bpoly, segm])
    polys.append(poly)
    bpolys.append(bpoly)
##############################

##############################
# EDIT
# body (right)
d = lbez.diff(paths[0][3])
t0 = 0.5 * d[0][1] / (d[0][1] - d[1][1])

paths[0][3] = lbez.reparameterize_bezier_curve(paths[0][3], end=t0)
xy0 = paths[0][3][3]

t1 = Newton_bezier(0, paths[0][4], 1, xy0[1], 20, 1e-6)
paths[0][4] = lbez.reparameterize_bezier_curve(paths[0][4], start=t1)

paths[0] = insert_joint(paths[0], after=3, tension=0)

# body (left)
paths[0] = paths[0][:7]
for i in range(7):
    bsym = paths[0][6 - i][-1::-1].copy()
    bsym[:, 0] = -bsym[:, 0]
Exemplo n.º 6
0
import numpy
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import sys
sys.path.append('/d/bandrieu/GitHub/Code/Python')
import lib_bezier as lbez

b = numpy.array([[0, 0], [0.3, 1], [1, 1], [1, 0], [1, -1], [1.5, -0.5],
                 [2, 0]])
#b = b[0:4]
"""
b = numpy.array([(0,0), (2,1)])
"""
b = b + 0.4 * (2 * numpy.random.rand(b.shape[0], b.shape[1]) - 1)

db = lbez.diff(b)

n = 100
t = numpy.linspace(0, 1, n)
"""
p = numpy.zeros((n,2))
for i in range(n):
    f, bl, br = lbez.de_casteljau(b, t[i])
    for j in range(2):
        p[i,j] = f[j]
"""

p = lbez.eval_bezier_curve(b, t)
dp = lbez.eval_bezier_curve(db, t)
#print dp