Skip to content

Commit

Permalink
Rework outline reconstruciton to be reliable
Browse files Browse the repository at this point in the history
- This is a backport of a7088f5
  • Loading branch information
set-soft committed Oct 13, 2022
1 parent 6e3c44c commit 5199320
Showing 1 changed file with 35 additions and 15 deletions.
50 changes: 35 additions & 15 deletions pcbdraw/pcbdraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ def unique_prefix():
def matrix(data):
return np.array(data, dtype=np.float32)

def pseudo_distance(a, b):
return (a[0] - b[0])**2 + (a[1] - b[1])**2

def get_closest(reference, elems):
distances = [pseudo_distance(reference, x) for x in elems]
return np.argmin(distances)

def extract_arg(args, index, default=None):
"""
Return n-th element of array or default if out of range
Expand Down Expand Up @@ -412,23 +419,36 @@ def get_board_polygon(svg_elements):
elements = elements[1:]
size = 0
# Append new segments to the ends of outline until there is none to append.
while size != len(outline):
while size != len(outline) and len(elements) > 0:
size = len(outline)
for i, e in enumerate(elements):
if SvgPathItem.is_same(outline[0].start, e.end):
outline.insert(0, e)
elif SvgPathItem.is_same(outline[0].start, e.start):
e.flip()
outline.insert(0, e)
elif SvgPathItem.is_same(outline[-1].end, e.start):
outline.append(e)
elif SvgPathItem.is_same(outline[-1].end, e.end):
e.flip()
outline.append(e)
else:
continue

i = get_closest(outline[0].start, [x.end for x in elements])
if SvgPathItem.is_same(outline[0].start, elements[i].end):
outline.insert(0, elements[i])
del elements[i]
continue

i = get_closest(outline[0].start, [x.start for x in elements])
if SvgPathItem.is_same(outline[0].start, elements[i].start):
e = elements[i]
e.flip()
outline.insert(0, e)
del elements[i]
break
continue

i = get_closest(outline[-1].end, [x.start for x in elements])
if SvgPathItem.is_same(outline[-1].end, elements[i].start):
outline.insert(0, elements[i])
del elements[i]
continue

i = get_closest(outline[-1].end, [x.end for x in elements])
if SvgPathItem.is_same(outline[-1].end, elements[i].end):
e = elements[i]
e.flip()
outline.insert(0, e)
del elements[i]
continue
# ...then, append it to path.
first = True
for x in outline:
Expand Down

0 comments on commit 5199320

Please sign in to comment.