def mouse_up(self, event): if not self.draw: return self.create = False self.set_key_mask(event) self.control_point2, self.control_point2_doc = self._calc_points(event) self.cursor = [] + self.control_point2 if self.path[0]: if self.alt_mask: self.point, self.point_doc = self._calc_points(event) self.add_point([] + self.point, [] + self.point_doc) self.control_point0 = [] + self.point self.cursor = event.get_point() self.curve_point = [] + self.point elif self.control_point2: self.point = [] + self.curve_point self.point_doc = [] + self.curve_point_doc self.control_point1 = contra_point(self.control_point2, self.curve_point) self.control_point1_doc = contra_point(self.control_point2_doc, self.curve_point_doc) node_type = sk2const.NODE_SYMMETRICAL if len(self.points): bp_doc = bezier_base_point(self.points[-1]) else: bp_doc = self.path[0] if self.control_point0_doc == bp_doc and \ self.control_point1_doc == self.curve_point_doc: node_type = sk2const.NODE_CUSP p0d = midpoint(bp_doc, self.curve_point_doc, 1.0 / 3.0) self.control_point0_doc = p0d p1d = midpoint(bp_doc, self.curve_point_doc, 2.0 / 3.0) self.control_point1_doc = p1d self.control_point0 = self.canvas.doc_to_win(p0d) self.control_point1 = self.canvas.doc_to_win(p1d) self.add_point([ self.control_point0, self.control_point1, self.curve_point, node_type ], [ self.control_point0_doc, self.control_point1_doc, self.curve_point_doc, node_type ]) self.control_point0 = [] + self.control_point2 self.control_point0_doc = [] + self.control_point2_doc snapped = self._calc_points(event) self.cursor = [] + snapped[0] self.curve_point, self.curve_point_doc = snapped else: self.point, self.point_doc = self._calc_points(event) self.add_point(self.point, self.point_doc) self.control_point0 = [] + self.point self.control_point0_doc = [] + self.point_doc self.on_timer()
def mouse_up(self, event): if self.draw: self.create = False self.ctrl_mask = False self.alt_mask = False p = event.get_point() self.control_point2, self.control_point2_doc = self.snap.snap_point(p)[1:] self.ctrl_mask = event.is_ctrl() self.alt_mask = event.is_alt() if self.path[0]: if self.alt_mask: p = event.get_point() self.point, self.point_doc = self.snap.snap_point(p)[1:] self.add_point([] + self.point, [] + self.point_doc) self.control_point0 = [] + self.point self.cursor = event.get_point() self.curve_point = [] + self.point elif self.control_point2: self.point = [] + self.curve_point self.point_doc = [] + self.curve_point_doc self.control_point1 = contra_point(self.control_point2, self.curve_point) self.control_point1_doc = contra_point(self.control_point2_doc, self.curve_point_doc) node_type = const.NODE_SYMMETRICAL if len(self.points): bp_doc = bezier_base_point(self.points[-1]) else: bp_doc = self.path[0] if self.control_point0_doc == bp_doc and \ self.control_point1_doc == self.curve_point_doc: node_type = const.NODE_CUSP self.control_point0_doc = midpoint(bp_doc, self.curve_point_doc, 1.0 / 3.0) self.control_point1_doc = midpoint(bp_doc, self.curve_point_doc, 2.0 / 3.0) self.control_point0 = self.canvas.doc_to_win(self.control_point0_doc) self.control_point1 = self.canvas.doc_to_win(self.control_point1_doc) self.add_point([self.control_point0, self.control_point1, self.curve_point, node_type], [self.control_point0_doc, self.control_point1_doc, self.curve_point_doc, node_type]) self.control_point0 = [] + self.control_point2 self.control_point0_doc = [] + self.control_point2_doc p = event.get_point() self.cursor = [] + p self.curve_point, self.curve_point_doc = self.snap.snap_point(p)[1:] else: p = event.get_point() self.point, self.point_doc = self.snap.snap_point(p)[1:] self.add_point(self.point, self.point_doc) self.control_point0 = [] + self.point self.control_point0_doc = [] + self.point_doc self.on_timer()
def insert_new_node_by_kbd(self): if self.new_node: self.insert_new_node() elif self.selected_nodes: new_nodes = [] new_selection = [] + self.selected_nodes for n0 in self.selected_nodes: path = n0.path if not path.get_point_index(n0): continue n1 = n0.get_point_before() if n0.is_curve(): x0, y0 = n0.point[2] cx00, cy00 = n0.point[0] cx01, cy01 = n0.point[1] x1, y1 = n1.get_base_point() x_new = ((x0 + x1) / 8.0) + ((cx00 + cx01) / 8.0 * 3.0) y_new = ((y0 + y1) / 8.0) + ((cy00 + cy01) / 8.0 * 3.0) else: x_new, y_new = libgeom.midpoint(n0.point, n1.get_base_point()) new_nodes.append(self.canvas.point_doc_to_win([x_new, y_new])) for node in new_nodes: self.set_new_node(node) new_selection.append(self.insert_new_node()) self.set_selected_nodes(new_selection)
def aprox2path(self, pts, closed): """approximated spline""" marker = sk2const.NODE_SMOOTH last = pts[0] cur = pts[1] start = libgeom.midpoint(last, cur) node = start[:] points = [] if closed: points.append(node) else: points.append(last) points.append(node) last = cur for cur in pts[2:]: c1 = libgeom.midpoint(node, last, F23) node = libgeom.midpoint(last, cur) c2 = libgeom.midpoint(node, last, F23) points.append([c1, c2, node, marker]) last = cur if closed: c1 = libgeom.midpoint(node, last, F23) c2 = libgeom.midpoint(start, last, F23) points.append([c1, c2, start, marker]) return points
def _to(self, x, y, cid, max_distance): end_point = (x, y) while True: current_point = (self.x, self.y) distance = libgeom.distance(current_point, end_point) print "##", distance if distance > 0: coef = min(distance, max_distance) / distance if coef != 1.0: x, y = libgeom.midpoint(current_point, end_point, coef) cmd = dst_model.DstStitch() cmd.cid = cid cmd.dx = int(x) - self.x cmd.dy = int(y) - self.y self.dst_mt.childs.append(cmd) self.move(cmd.dx, cmd.dy) if distance < max_distance: break
def get_point(self): p = libgeom.midpoint(self.start, self.stop, self.coef) return libgeom.apply_trafo_to_point(p, self.target.trafo)
def parse_svg_path_cmds(pathcmds): index = 0 last = None last_index = 0 cmds = [] pathcmds = re.sub(' *', ' ', pathcmds) for item in pathcmds: if item in 'MmZzLlHhVvCcSsQqTtAa': if last: coords = parse_svg_coords(pathcmds[last_index + 1:index]) cmds.append((last, coords)) last = item last_index = index index += 1 coords = parse_svg_coords(pathcmds[last_index + 1:index]) cmds.append([last, coords]) paths = [] path = [] cpoint = [] rel_flag = False last_cmd = 'M' last_quad = None for cmd in cmds: if cmd[0] in 'Mm': if path: paths.append(path) path = deepcopy(PATH_STUB) rel_flag = cmd[0] == 'm' points = [cmd[1][i:i + 2] for i in range(0, len(cmd[1]), 2)] for point in points: if cpoint and rel_flag: point = add_points(base_point(cpoint), point) if not path[0]: path[0] = point else: path[1].append(point) cpoint = point elif cmd[0] in 'Zz': p0 = [] + base_point(cpoint) p1 = [] + path[0] if not libgeom.is_equal_points(p0, p1, 8): path[1].append([] + path[0]) path[2] = sk2const.CURVE_CLOSED cpoint = [] + path[0] elif cmd[0] in 'Cc': rel_flag = cmd[0] == 'c' points = [cmd[1][i:i + 2] for i in range(0, len(cmd[1]), 2)] points = [points[i:i + 3] for i in range(0, len(points), 3)] for point in points: if rel_flag: point = [ add_points(base_point(cpoint), point[0]), add_points(base_point(cpoint), point[1]), add_points(base_point(cpoint), point[2]) ] qpoint = [] + point qpoint.append(sk2const.NODE_CUSP) path[1].append(qpoint) cpoint = point elif cmd[0] in 'Ll': rel_flag = cmd[0] == 'l' points = [cmd[1][i:i + 2] for i in range(0, len(cmd[1]), 2)] for point in points: if rel_flag: point = add_points(base_point(cpoint), point) path[1].append(point) cpoint = point elif cmd[0] in 'Hh': rel_flag = cmd[0] == 'h' for x in cmd[1]: dx, y = base_point(cpoint) if rel_flag: point = [x + dx, y] else: point = [x, y] path[1].append(point) cpoint = point elif cmd[0] in 'Vv': rel_flag = cmd[0] == 'v' for y in cmd[1]: x, dy = base_point(cpoint) if rel_flag: point = [x, y + dy] else: point = [x, y] path[1].append(point) cpoint = point elif cmd[0] in 'Ss': rel_flag = cmd[0] == 's' points = [cmd[1][i:i + 2] for i in range(0, len(cmd[1]), 2)] points = [points[i:i + 2] for i in range(0, len(points), 2)] for point in points: q = cpoint p = cpoint if len(cpoint) > 2: q = cpoint[1] p = cpoint[2] p1 = sub_points(add_points(p, p), q) if rel_flag: p2 = add_points(base_point(cpoint), point[0]) p3 = add_points(base_point(cpoint), point[1]) else: p2, p3 = point point = [p1, p2, p3] qpoint = [] + point qpoint.append(sk2const.NODE_CUSP) path[1].append(qpoint) cpoint = point elif cmd[0] in 'Qq': rel_flag = cmd[0] == 'q' groups = [cmd[1][i:i + 4] for i in range(0, len(cmd[1]), 4)] for vals in groups: p = base_point(cpoint) if rel_flag: q = add_points(p, [vals[0], vals[1]]) p3 = add_points(p, [vals[2], vals[3]]) else: q = [vals[0], vals[1]] p3 = [vals[2], vals[3]] p1 = add_points(mult_point(p, F13), mult_point(q, F23)) p2 = add_points(mult_point(p3, F13), mult_point(q, F23)) point = [p1, p2, p3] qpoint = [] + point qpoint.append(sk2const.NODE_CUSP) path[1].append(qpoint) cpoint = point last_quad = q elif cmd[0] in 'Tt': rel_flag = cmd[0] == 't' groups = [cmd[1][i:i + 2] for i in range(0, len(cmd[1]), 2)] if last_cmd not in 'QqTt' or last_quad is None: last_quad = base_point(cpoint) for vals in groups: p = base_point(cpoint) q = sub_points(mult_point(p, 2.0), last_quad) if rel_flag: p3 = add_points(p, [vals[0], vals[1]]) else: p3 = [vals[0], vals[1]] p1 = add_points(mult_point(p, F13), mult_point(q, F23)) p2 = add_points(mult_point(p3, F13), mult_point(q, F23)) point = [p1, p2, p3] qpoint = [] + point qpoint.append(sk2const.NODE_CUSP) path[1].append(qpoint) cpoint = point last_quad = q elif cmd[0] in 'Aa': rel_flag = cmd[0] == 'a' arcs = [cmd[1][i:i + 7] for i in range(0, len(cmd[1]), 7)] for arc in arcs: cpoint = base_point(cpoint) rev_flag = False rx, ry, xrot, large_arc_flag, sweep_flag, x, y = arc rx = abs(rx) ry = abs(ry) if rel_flag: x += cpoint[0] y += cpoint[1] if cpoint == [x, y]: continue if not rx or not ry: path[1].append([x, y]) continue vector = [[] + cpoint, [x, y]] if sweep_flag: vector = [[x, y], [] + cpoint] rev_flag = True cpoint = [x, y] dir_tr = libgeom.trafo_rotate_grad(-xrot) if rx > ry: tr = [1.0, 0.0, 0.0, rx / ry, 0.0, 0.0] r = rx else: tr = [ry / rx, 0.0, 0.0, 1.0, 0.0, 0.0] r = ry dir_tr = libgeom.multiply_trafo(dir_tr, tr) vector = libgeom.apply_trafo_to_points(vector, dir_tr) l = libgeom.distance(*vector) if l > 2.0 * r: r = l / 2.0 mp = libgeom.midpoint(*vector) tr0 = libgeom.trafo_rotate(math.pi / 2.0, mp[0], mp[1]) pvector = libgeom.apply_trafo_to_points(vector, tr0) k = math.sqrt(r * r - l * l / 4.0) if large_arc_flag: center = libgeom.midpoint(mp, pvector[1], 2.0 * k / l) else: center = libgeom.midpoint(mp, pvector[0], 2.0 * k / l) angle1 = libgeom.get_point_angle(vector[0], center) angle2 = libgeom.get_point_angle(vector[1], center) da = angle2 - angle1 start = angle1 end = angle2 if large_arc_flag: if -math.pi >= da or da <= math.pi: start = angle2 end = angle1 rev_flag = not rev_flag else: if -math.pi <= da or da >= math.pi: start = angle2 end = angle1 rev_flag = not rev_flag pth = libgeom.get_circle_paths(start, end, sk2const.ARC_ARC)[0] if rev_flag: pth = libgeom.reverse_path(pth) points = pth[1] for point in points: if len(point) == 3: point.append(sk2const.NODE_CUSP) tr0 = [1.0, 0.0, 0.0, 1.0, -0.5, -0.5] points = libgeom.apply_trafo_to_points(points, tr0) tr1 = [2.0 * r, 0.0, 0.0, 2.0 * r, 0.0, 0.0] points = libgeom.apply_trafo_to_points(points, tr1) tr2 = [1.0, 0.0, 0.0, 1.0, center[0], center[1]] points = libgeom.apply_trafo_to_points(points, tr2) tr3 = libgeom.invert_trafo(dir_tr) points = libgeom.apply_trafo_to_points(points, tr3) for point in points: path[1].append(point) last_cmd = cmd[0] if path: paths.append(path) return paths
def xspline2path(self, pts, cpts, closed=False): marker = sk2const.NODE_SMOOTH points = [] pts = pts[:] cpts = cpts[:] if closed: last = pts[-1] pts.append(pts[0]) cpts.append(cpts[0]) else: last = libgeom.contra_point(pts[1], pts[0]) ipt = libgeom.contra_point(pts[-2], pts[-1]) pts.append(ipt) cpts.append(0.0) for idx, cpt in enumerate(cpts[0:-1], 0): cur = pts[idx][:] foll = pts[idx + 1] if cpt == 0.0: # 'angular point' if not points: points.append(cur) else: points.append([c1, cur[:], cur[:], marker]) c1 = cur[:] elif cpt < 0.0: # 'interpolation point' coef = 0.5 * abs(cpt) c2, c1n = figlib.ctrl_points(last, cur, foll, coef) if not points: points.append(cur) else: points.append([c1, c2, cur, marker]) c1 = c1n else: # 'approximated point' coef = 1.0 - F13 + F13 * (1.0 - cpt) mp = libgeom.midpoint(last, foll) node = libgeom.midpoint(mp, cur, coef) if not points: points.append(node) else: c2 = libgeom.midpoint(last, cur, coef) points.append([c1, c2, node, marker]) c1 = libgeom.midpoint(foll, cur, coef) last = cur if closed: cpt = cpts[-1] cur = pts[-1] foll = pts[1] if cpt == 0.0: points.append([c1, cur[:], cur[:], marker]) elif cpt < 0.0: coef = 0.5 * abs(cpt) c2, c1n = figlib.ctrl_points(last, cur, foll, coef) points.append([c1, c2, cur, marker]) else: coef = 1.0 - F13 + F13 * (1.0 - cpt) mp = libgeom.midpoint(last, foll) node = libgeom.midpoint(mp, cur, coef) c2 = libgeom.midpoint(last, cur, coef) points.append([c1, c2, node, marker]) return points