From e8896fe18e3f052f6ae4c7a7a62ac3f40ec7c091 Mon Sep 17 00:00:00 2001 From: Kaalleen <36401965+kaalleen@users.noreply.github.com> Date: Tue, 13 Aug 2024 16:59:12 +0200 Subject: add remove duplicated points extension (#3117) --- lib/extensions/remove_duplicated_points.py | 55 ++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 lib/extensions/remove_duplicated_points.py (limited to 'lib/extensions/remove_duplicated_points.py') diff --git a/lib/extensions/remove_duplicated_points.py b/lib/extensions/remove_duplicated_points.py new file mode 100644 index 00000000..7d54a807 --- /dev/null +++ b/lib/extensions/remove_duplicated_points.py @@ -0,0 +1,55 @@ +# Authors: see git history +# +# Copyright (c) 2024 Authors +# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. + +from collections import deque + +from inkex import Path, Transform, errormsg +from shapely import MultiPoint, Point + +from ..elements import EmbroideryElement +from ..i18n import _ +from ..svg import get_correction_transform +from .base import InkstitchExtension + + +class RemoveDuplicatedPoints(InkstitchExtension): + ''' + This extension will remove duplicated points within the given range + ''' + def __init__(self, *args, **kwargs): + InkstitchExtension.__init__(self, *args, **kwargs) + self.arg_parser.add_argument("--notebook") + self.arg_parser.add_argument("-n", "--num_points", type=int, default=10, dest="num_points") + self.arg_parser.add_argument("-d", "--distance", type=float, default=0.01, dest="distance") + + def effect(self): + if not self.get_elements(): + return + + if not self.svg.selection: + errormsg(_("Please select one or more strokes.")) + return + + visited_nodes = [] + for element in self.elements: + if element.node.get_id() in visited_nodes: + continue + visited_nodes.append(element.node.get_id()) + if element.node.TAG != 'path': + # convert objects into paths + node = element.node.to_path_element() + element.node.getparent().replace(element.node, node) + element = EmbroideryElement(node) + new_paths = [] + for path in element.paths: + new_path = [] + for point in path: + # do compare with more than 10 points + points = deque(new_path, maxlen=self.options.num_points) + if not points or Point(point).distance(MultiPoint(points)) > self.options.distance: + new_path.append(point) + new_paths.append(new_path) + transform = -element.node.transform @ Transform(get_correction_transform(element.node)) + element.node.set('d', str(Path(new_paths[0]).transform(transform))) -- cgit v1.2.3