Compare commits

...

4 Commits

2 changed files with 43 additions and 29 deletions

View File

@ -345,6 +345,17 @@ class RenderPather(PortList, PatherMixin):
return self return self
def plugged(
self,
connections: dict[str, str],
) -> Self:
for aa, bb in connections.items():
porta = self.ports[aa]
portb = self.ports[bb]
self.paths[aa].append(RenderStep('P', None, porta.copy(), porta.copy(), None))
self.paths[bb].append(RenderStep('P', None, portb.copy(), portb.copy(), None))
PortList.plugged(self, connections)
return self
def path( def path(
self, self,

View File

@ -340,7 +340,7 @@ class BasicTool(Tool, metaclass=ABCMeta):
bend_angle *= -1 bend_angle *= -1
else: else:
bend_dxy = numpy.zeros(2) bend_dxy = numpy.zeros(2)
bend_angle = 0 bend_angle = pi
in_transition = self.transitions.get('unk' if in_ptype is None else in_ptype, None) in_transition = self.transitions.get('unk' if in_ptype is None else in_ptype, None)
if in_transition is not None: if in_transition is not None:
@ -401,15 +401,15 @@ class BasicTool(Tool, metaclass=ABCMeta):
gen_straight, sport_in, _sport_out = self.straight gen_straight, sport_in, _sport_out = self.straight
for step in batch: for step in batch:
straight_length, ccw, in_transition, out_transition = step.data data = step.data
assert step.tool == self assert step.tool == self
if step.opcode == 'L': if step.opcode == 'L':
if in_transition: if data.in_transition:
ipat, iport_theirs, _iport_ours = in_transition ipat, iport_theirs, _iport_ours = data.in_transition
pat.plug(ipat, {port_names[1]: iport_theirs}) pat.plug(ipat, {port_names[1]: iport_theirs})
if not numpy.isclose(straight_length, 0): if not numpy.isclose(data.straight_length, 0):
straight_pat_or_tree = gen_straight(straight_length, **kwargs) straight_pat_or_tree = gen_straight(data.straight_length, **kwargs)
pmap = {port_names[1]: sport_in} pmap = {port_names[1]: sport_in}
if isinstance(straight_pat_or_tree, Pattern): if isinstance(straight_pat_or_tree, Pattern):
straight_pat = straight_pat_or_tree straight_pat = straight_pat_or_tree
@ -427,11 +427,11 @@ class BasicTool(Tool, metaclass=ABCMeta):
else: else:
straight = tree <= straight_pat_or_tree straight = tree <= straight_pat_or_tree
pat.plug(straight, pmap) pat.plug(straight, pmap)
if ccw is not None: if data.ccw is not None:
bend, bport_in, bport_out = self.bend bend, bport_in, bport_out = self.bend
pat.plug(bend, {port_names[1]: bport_in}, mirrored=bool(ccw)) pat.plug(bend, {port_names[1]: bport_in}, mirrored=bool(data.ccw))
if out_transition: if data.out_transition:
opat, oport_theirs, oport_ours = out_transition opat, oport_theirs, oport_ours = data.out_transition
pat.plug(opat, {port_names[1]: oport_ours}) pat.plug(opat, {port_names[1]: oport_ours})
return tree return tree
@ -443,7 +443,8 @@ class AutoTool(Tool, metaclass=ABCMeta):
for generating straight paths, and a table of pre-rendered `transitions` for converting for generating straight paths, and a table of pre-rendered `transitions` for converting
from non-native ptypes. from non-native ptypes.
""" """
straights: list[tuple[str, Callable[[float], Pattern] | Callable[[float], Library], str, str]] straights: list[tuple[str, Callable[[float], Pattern] | Callable[[float], Library], str, str, tuple[float, float]]]
# TODO add min length?
bends: list[abstract_tuple_t] # Assumed to be clockwise bends: list[abstract_tuple_t] # Assumed to be clockwise
""" `clockwise_bend_abstract, in_port_name, out_port_name` """ """ `clockwise_bend_abstract, in_port_name, out_port_name` """
@ -508,7 +509,7 @@ class AutoTool(Tool, metaclass=ABCMeta):
@staticmethod @staticmethod
def _bend2dxy(bend_tuple: abstract_tuple_t, ccw: SupportsBool | None) -> tuple[NDArray[numpy.float64], float]: def _bend2dxy(bend_tuple: abstract_tuple_t, ccw: SupportsBool | None) -> tuple[NDArray[numpy.float64], float]:
if ccw is None: if ccw is None:
return numpy.zeros(2), 0.0 return numpy.zeros(2), pi
bend, bport_in, bport_out = bend_tuple bend, bport_in, bport_out = bend_tuple
angle_in = bend.ports[bport_in].rotation angle_in = bend.ports[bport_in].rotation
@ -566,9 +567,10 @@ class AutoTool(Tool, metaclass=ABCMeta):
) -> tuple[Port, LData]: ) -> tuple[Port, LData]:
# TODO check all the math for L-shaped bends # TODO check all the math for L-shaped bends
success = False
for straight_tuple in self.straights: for straight_tuple in self.straights:
stype = straight_tuple[0] stype = straight_tuple[0]
straight_bounds = straight_tuple[-1]
for bend_tuple in self.bends: for bend_tuple in self.bends:
bend_dxy, bend_angle = self._bend2dxy(bend_tuple, ccw) bend_dxy, bend_angle = self._bend2dxy(bend_tuple, ccw)
btypei = bend_tuple[0][bend_tuple[1]].ptype btypei = bend_tuple[0][bend_tuple[1]].ptype
@ -578,7 +580,7 @@ class AutoTool(Tool, metaclass=ABCMeta):
itrans_dxy = self._itransition2dxy(in_transition) itrans_dxy = self._itransition2dxy(in_transition)
out_transition = self.transitions.get(('unk' if out_ptype is None else out_ptype, stype if ccw is None else btypeo), None) out_transition = self.transitions.get(('unk' if out_ptype is None else out_ptype, stype if ccw is None else btypeo), None)
otrans_dxy = self._otransition2dxy(in_transition, bend_angle) otrans_dxy = self._otransition2dxy(out_transition, bend_angle)
b_transition = None b_transition = None
if ccw is not None and btypei != stype: if ccw is not None and btypei != stype:
@ -587,9 +589,10 @@ class AutoTool(Tool, metaclass=ABCMeta):
straight_length = length - bend_dxy[0] - itrans_dxy[0] - btrans_dxy[0] - otrans_dxy[0] straight_length = length - bend_dxy[0] - itrans_dxy[0] - btrans_dxy[0] - otrans_dxy[0]
bend_run = bend_dxy[1] + itrans_dxy[1] + btrans_dxy[1] + otrans_dxy[1] bend_run = bend_dxy[1] + itrans_dxy[1] + btrans_dxy[1] + otrans_dxy[1]
if straight_length >= 0: success = straight_bounds[0] <= straight_length < straight_bounds[1]
if success:
break break
if straight_length >= 0: if success:
break break
else: else:
# Failed to break # Failed to break
@ -625,15 +628,16 @@ class AutoTool(Tool, metaclass=ABCMeta):
pat.add_port_pair(names=(port_names[0], port_names[1])) pat.add_port_pair(names=(port_names[0], port_names[1]))
for step in batch: for step in batch:
_stype, gen_straight, sport_in, _sport_out = step.data.straight_tuple data = step.data
_stype, gen_straight, sport_in, _sport_out, _length_bounds = data.straight_tuple
assert step.tool == self assert step.tool == self
if step.opcode == 'L': if step.opcode == 'L':
if step.data.in_transition: if data.in_transition:
ipat, iport_theirs, _iport_ours = step.data.in_transition ipat, iport_theirs, _iport_ours = data.in_transition
pat.plug(ipat, {port_names[1]: iport_theirs}) pat.plug(ipat, {port_names[1]: iport_theirs})
if not numpy.isclose(step.data.straight_length, 0): if not numpy.isclose(data.straight_length, 0):
straight_pat_or_tree = gen_straight(step.data.straight_length, **kwargs) straight_pat_or_tree = gen_straight(data.straight_length, **kwargs)
pmap = {port_names[1]: sport_in} pmap = {port_names[1]: sport_in}
if isinstance(straight_pat_or_tree, Pattern): if isinstance(straight_pat_or_tree, Pattern):
straight_pat = straight_pat_or_tree straight_pat = straight_pat_or_tree
@ -651,19 +655,18 @@ class AutoTool(Tool, metaclass=ABCMeta):
else: else:
straight = tree <= straight_pat_or_tree straight = tree <= straight_pat_or_tree
pat.plug(straight, pmap) pat.plug(straight, pmap)
if step.data.b_transition: if data.b_transition:
btpat, btport_bend, btport_straight = step.data.b_transition btpat, btport_bend, btport_straight = data.b_transition
pat.plug(btpat, {port_names[1]: btport_straight}) pat.plug(btpat, {port_names[1]: btport_straight})
if step.data.ccw is not None: if data.ccw is not None:
bend, bport_in, bport_out = step.data.bend_tuple bend, bport_in, bport_out = data.bend_tuple
pat.plug(bend, {port_names[1]: bport_in}, mirrored=bool(step.data.ccw)) pat.plug(bend, {port_names[1]: bport_in}, mirrored=bool(data.ccw))
if step.data.out_transition: if data.out_transition:
opat, oport_theirs, oport_ours = step.data.out_transition opat, oport_theirs, oport_ours = data.out_transition
pat.plug(opat, {port_names[1]: oport_ours}) pat.plug(opat, {port_names[1]: oport_ours})
return tree return tree
@dataclass @dataclass
class PathTool(Tool, metaclass=ABCMeta): class PathTool(Tool, metaclass=ABCMeta):
""" """