def curveto(self, points, relative=False): if relative: ox, oy = self.current else: ox, oy = 0, 0 vertices = self.vertices[-1] x0, y0 = self.current for i in range(0, len(points), 6): x1, y1 = points[i + 0], points[i + 1] x2, y2 = points[i + 2], points[i + 3] self.last_control4 = x2, y2 x3, y3 = points[i + 4], points[i + 5] V = curves.curve4_bezier((x0 + ox, y0 + oy), (x1 + ox, y1 + oy), (x2 + ox, y2 + oy), (x3 + ox, y3 + oy)) vertices.extend(V[1:].tolist()) x0, y0 = vertices[-1] self.current = vertices[-1]
def curveto(self, points, relative=False): if relative: ox,oy = self.current else: ox,oy = 0,0 vertices = self.vertices[-1] x0, y0 = self.current for i in range(0,len(points),6): x1,y1 = points[i+0], points[i+1] x2,y2 = points[i+2], points[i+3] self.last_control4 = x2,y2 x3,y3 = points[i+4], points[i+5] V = curves.curve4_bezier((x0+ox,y0+oy), (x1+ox,y1+oy), (x2+ox,y2+oy), (x3+ox,y3+oy)) vertices.extend(V[1:].tolist()) x0,y0 = vertices[-1] self.current = vertices[-1]
def flatten_recursive(self,flatness=0.125, angle=15): return curve4_bezier(self.p0, self.p1, self.p2, self.p3, flatness, angle)
V_curr[..., 1] = P[:, np.newaxis, 1] V_curr[..., 2] = 1, -1 L = np.cumsum(np.sqrt( ((P[1:] - P[:-1])**2).sum(axis=-1))).reshape(n - 1, 1) V_curr[1:, :, 3] = L if closed: V[0], V[-1] = V[-3], V[2] else: V[0], V[-1] = V[1], V[-2] return V_prev, V_curr, V_next, L[-1] d = 64 # P = curve4_bezier((d, 3*d), (512+d, 512+d), (512+d, -d), (d, 512-3*d)) # P = curve4_bezier((d, d), (0,512) , (512,0), (512-d,512-d)) P = curve4_bezier((d, d), (d, 512), (512 - d, 512), (512 - d, d)) print(len(P)) V_prev, V_curr, V_next, length = bake(P) curve = gloo.Program(line_vertex, line_fragment) curve["prev"], curve["curr"], curve["next"] = V_prev, V_curr, V_next curve["antialias"] = 1.5 curve["linelength"] = length points = gloo.Program(point_vertex, point_fragment, count=len(P)) points["center"] = P points["radius"] = 3.5 points["radius"][0] = 5 points["radius"][-1] = 5 app.run()
def on_key(event=None): if event and event.key == 'escape': sys.exit() plt.cla() plt.ion() axes.set_xlim(0,size[0]) axes.set_ylim(0,size[1]) # P = [275, 591, 143, 383, 348, 578, 560, 285] # P = [291,612, 533,579, 482,476, 332,175] # P = [332,175,482,476,533,579,291,612] # P = [145,207,293,238,565,453,564,103] # P = [551,198,637,684,417,500,566,573] # P = [351,529,291,642,460,333,324,353] # P = [573,157,699,546,101,594,157,589] # P = [200,400, 600,600, 200,600, 600, 400] P = np.random.randint(100,700,8) C = CubicBezier(*P) # P = C.flatten_brute_iterative(25) P = C.flatten_forward_iterative(25) mean,std = measure(C,P) # print "Error forward = %.2f +/- %.3f (%d points)" % (mean,std,len(P)) P = C.flatten_iterative(flatness=.125) mean,std = measure(C,P) print "Error iterative = %.2f +/- %.3f (%d points)" % (mean,std,len(P)) C.plot() P = np.array(P) line_plot(P, lw=150, edgecolor='k', alpha=.1) #plt.plot(P[:,0], P[:,1], lw=150, color='k', alpha=.1) plt.scatter(P[:,0], P[:,1], s=5, color='r', zorder=40) # P = C.flatten_behdad_segment(flatness=.125) # mean,std = measure(C,P) # print "Error behdad = %.2f +/- %.3f (%d points)" % (mean,std,len(P)) # A = C.flatten_behdad_arc(flatness=.125) # for arc in A: # center,radius,angle0,angle1,negative = arc # angle0 = 180*angle0/math.pi # angle1 = 180*angle1/math.pi # if negative: # angle0, angle1 = angle1, angle0 # arc = Arc((center.x,center.y), 2*radius, 2*radius, 0, # angle0, angle1, linewidth = 150, alpha=.25) # fig.gca().add_artist(arc) # mean,std = measure_arc(C,A) # print "Error behdad arc= %.2f +/- %.3f (%d points)" % (mean,std,len(A)) P = curve4_bezier(C.p0, C.p1, C.p2, C.p3) mean,std = measure(C,P) print "Error recursive = %.2f +/- %.3f (%d points)" % (mean,std,len(P)) #print "Error recursive = %.2f (%d points)" % (measure(C,P), len(P)) print "C = [%d,%d,%d,%d,%d,%d,%d,%d]" % (C.x0,C.y0,C.x1,C.y1,C.x2,C.y2,C.x3,C.y3) print plt.xticks([]),plt.yticks([]) plt.ioff()
def flatten_recursive(self, flatness=0.125, angle=15): return curve4_bezier(self.p0, self.p1, self.p2, self.p3, flatness, angle)