bugfix flevobezier

This commit is contained in:
Mario Voigt 2024-01-23 10:39:36 +01:00
parent c4048045ed
commit dd81cbfb99

View File

@ -10,14 +10,14 @@ import inkex
import gettext import gettext
from inkex.paths import Path from inkex.paths import Path
import sys import sys
def pout(t): sys.exit((gettext.gettext(t)))
class FlevoBezier(inkex.EffectExtension): class FlevoBezier(inkex.EffectExtension):
def effect(self): def effect(self):
if len(self.svg.selected) == 0: pout("Please select at least one path.") if len(self.svg.selected) == 0: inkex.utils.debug("Please select at least one path.")
for obj in self.svg.selected: # The objects are the paths, which may be compound for element in self.svg.selected: # The objects are the paths, which may be compound
curr = self.svg.selected[obj] if element.tag == inkex.addNS('path','svg'):
curr = self.svg.selected[element]
raw = Path(curr.get("d")).to_arrays() raw = Path(curr.get("d")).to_arrays()
subpaths, prev = [], 0 subpaths, prev = [], 0
for i in range(len(raw)): # Breaks compound paths into simple paths for i in range(len(raw)): # Breaks compound paths into simple paths
@ -50,10 +50,13 @@ class FlevoBezier(inkex.EffectExtension):
nodes.append(node(simpath[i][1][-2:])) nodes.append(node(simpath[i][1][-2:]))
output += flevobezier(nodes, closed) output += flevobezier(nodes, closed)
curr.set("d", output) curr.set("d", output)
else:
inkex.utils.debug("element {} is not a path! Try to degroup / convert before!".format(element.get('id')))
# The main algorithm! Yay! # The main algorithm! Yay!
def flevobezier(points, z): def flevobezier(points, z):
if len(points) < 2: pout("A curve isn't a point, silly!") if len(points) < 2: inkex.utils.debug("A curve isn't a point, silly!")
res = [] res = []
prevtrail, trail, lead, window = 0, 0, 1, points[:2] # Start with first two points prevtrail, trail, lead, window = 0, 0, 1, points[:2] # Start with first two points
maybeover = False # Over by error followed by over by angle -> backup maybeover = False # Over by error followed by over by angle -> backup
@ -66,7 +69,7 @@ def flevobezier(points, z):
try: try:
dist(v) / dist(w) dist(v) / dist(w)
except ZeroDivisionError as e: except ZeroDivisionError as e:
pout("Division by zero. Check if your path contains duplicate handles.") inkex.utils.debug("Division by zero. Check if your path contains duplicate handles.")
if dotp(v, w) / dist(v) / dist(w) >= 0.5: # 60 degrees or less, over by angle if dotp(v, w) / dist(v) / dist(w) >= 0.5: # 60 degrees or less, over by angle
if maybeover: # backup if maybeover: # backup
newcurve = stress(points[prevtrail:lead])[0] newcurve = stress(points[prevtrail:lead])[0]
@ -124,7 +127,7 @@ def flevobezier(points, z):
res[t - 1] = res[t] + spin(v, sign * theta) res[t - 1] = res[t] + spin(v, sign * theta)
res[t + 1] = res[t] + spin(w, -sign * theta) res[t + 1] = res[t] + spin(w, -sign * theta)
except ZeroDivisionError: except ZeroDivisionError:
pout("Path has only one point left. Cannot continue") inkex.utils.debug("Path has only one point left. Cannot continue")
res.append(ouro) res.append(ouro)
# Formatting and final output # Formatting and final output
out = "M " + str(res[0]) out = "M " + str(res[0])
@ -191,7 +194,7 @@ def cubicfrom4(nodes, p = None, q = None):
x = nodes[1] - (1 - lm) ** 3 * nodes[0] - lm ** 3 * nodes[3] x = nodes[1] - (1 - lm) ** 3 * nodes[0] - lm ** 3 * nodes[3]
y = nodes[2] - (1 - mu) ** 3 * nodes[0] - mu ** 3 * nodes[3] y = nodes[2] - (1 - mu) ** 3 * nodes[0] - mu ** 3 * nodes[3]
det = a * d - b * c det = a * d - b * c
if not det: pout("Singular matrix!") if not det: inkex.utils.debug("Singular matrix!")
l, m = (d * x - b * y) / det, (a * y - c * x) / det l, m = (d * x - b * y) / det, (a * y - c * x) / det
return [nodes[0], l, m, nodes[3]] return [nodes[0], l, m, nodes[3]]