Jollywatt / typst-fletcher

Typst package for drawing diagrams with arrows, built on top of CeTZ.
MIT License
265 stars 6 forks source link

Division by Zero when edges have 180deg corners #43

Closed wlievens closed 1 day ago

wlievens commented 6 days ago

My diagram sometimes triggers division-by-zero errors when I'm tuning certain dimensioning parameters, such as diagram spacing.

It appears to me the sign(x) function should simply for x=0, and right now it does not, as it is implemented as sign(x)=x/|x|.

https://github.com/Jollywatt/typst-fletcher/blob/master/src/utils.typ#L80

My diagram code

#let default-edge-corner-radius = 3pt
#set text(size: 9pt)
#diagram(
  spacing: (26pt, 25pt),
  mark-scale: 80%,
  node-stroke: 1pt,
  edge-stroke: 1pt,
  node-outset: 2pt,
  node((4, 3), [Idle], fill: green.lighten(50%), corner-radius: 4pt, extrude: (0pt, 3pt), outset: 5pt),
  edge("-|>"),
  node((2, 3), [_Start \ Probing_], fill: yellow.lighten(60%), shape: fletcher.shapes.parallelogram.with(angle: 20deg)),
  edge("-|>"),
  node((1, 3), [Start \ Wafer Lot]),
  edge("-|>"),
  node((0, 3), [Start Wafer]),
  edge("-|>"),
  node((0, 4), [Start Die]),
  edge("-|>"),
  node((0, 5), [Touch \ Needles]),
  edge("-|>"),
  node((0, 6), [Detect Dust]], fill: purple.lighten(70%)),
  edge("l,dd", "-|>"),
  edge("-|>"),
  node((0, 7), [Run Test], fill: purple.lighten(70%)),
  edge("rrrrr", "-|>", dash: (3pt, 3pt)),
  edge("d", "-|>"),
  edge("l,d", "-|>"),
  node((-1, 8), text(fill: white)[Error], fill: orange.darken(15%)),
  edge("d", "-|>"),
  node((5, 7), [Save \ HDF5], fill: blue.lighten(80%), shape: fletcher.shapes.rect),
  edge("-|>", dash: (3pt, 3pt)),
  node((5, 9), [Upload \ HDF5], fill: blue.lighten(80%), shape: fletcher.shapes.rect),
  node((0, 8), [Check \ Minichar], fill: red.lighten(80%), shape: fletcher.shapes.diamond),
  edge("dl", "-|>", label: "No"),
  edge("r", "-|>", label: "Yes"),
  node((1, 8), [Run Minichar], fill: purple.lighten(70%)),
  edge("d,ll", "-|>", corner-radius: default-edge-corner-radius),
  node((-1, 9), [Retract \ Needles]),
  edge("-|>"),
  node((-1, 10), [Finish Die]),
  edge("-|>"),
  node((0, 10), [Next Die]),
  edge("rr,uuuuu,l,l", "-|>", corner-radius: default-edge-corner-radius),
  edge("-|>"),
  node((0, 11), [Finish \ Wafer], shape: fletcher.shapes.rect),
  edge("-|>"),
  node((0, 12), [Next \ Wafer], shape: fletcher.shapes.rect),
  edge("-|>"),
  edge("r,u,rr,uuuuuuu,lll", "-|>", corner-radius: default-edge-corner-radius),
  node((2, 12), [Finish \ Wafer Lot]),
  edge("rr,uuuuuuuuu", "-|>", corner-radius: default-edge-corner-radius),
  node(enclose: ((5, 1), (5, 12)), stroke: blue, snap: false, inset: 15pt, align(center + top)[_Separate \ Threads_]),
)
Jollywatt commented 5 days ago

It seems like this is caused by the edge with vertices "rr,uuuuu,l,l". If you do "rr,uuuuu,ll" instead, it seems to work ok. My guess is that "l,l" causes fletcher to try to render a rounded "corner" with an angle of 180deg, which explodes sometimes. If so, these kinds of edge cases should be handled better...

wlievens commented 5 days ago

Thanks for the tip. That seems to have been an artifact from playing with the manual edge routing a lot.

Jollywatt commented 1 day ago

This should be working now!