Compare commits

...

3 Commits

View File

@ -3,7 +3,7 @@ Tools are objects which dynamically generate simple single-use devices (e.g. wir
# TODO document all tools
"""
from typing import Literal, Any
from typing import Literal, Any, Self
from collections.abc import Sequence, Callable
from abc import ABCMeta # , abstractmethod # TODO any way to make Tool ok with implementing only one method?
from dataclasses import dataclass
@ -473,8 +473,8 @@ class AutoTool(Tool, metaclass=ABCMeta):
class Transition:
""" Description of a pre-rendered transition """
abstract: Abstract
our_port_name: str
their_port_name: str
our_port_name: str
@property
def our_port(self) -> Port:
@ -484,6 +484,9 @@ class AutoTool(Tool, metaclass=ABCMeta):
def their_port(self) -> Port:
return self.abstract.ports[self.their_port_name]
def reversed(self) -> Self:
return type(self)(self.abstract, self.their_port_name, self.our_port_name)
@dataclass(frozen=True, slots=True)
class LData:
""" Data for planL """
@ -507,6 +510,12 @@ class AutoTool(Tool, metaclass=ABCMeta):
default_out_ptype: str
""" Default value for out_ptype """
def add_complementary_transitions(self) -> Self:
for iioo in list(self.transitions.keys()):
ooii = (iioo[1], iioo[0])
self.transitions.setdefault(ooii, self.transitions[iioo].reversed())
return self
def path(
self,
ccw: SupportsBool | None,
@ -526,6 +535,8 @@ class AutoTool(Tool, metaclass=ABCMeta):
tree, pat = Library.mktree(SINGLE_USE_PREFIX + 'path')
pat.add_port_pair(names=port_names, ptype='unk' if in_ptype is None else in_ptype)
if data.in_transition:
pat.plug(data.in_transition.abstract, {port_names[1]: data.in_transition.their_port_name})
if not numpy.isclose(data.straight_length, 0):
straight_pat_or_tree = data.straight.fn(data.straight_length, **kwargs)
if isinstance(straight_pat_or_tree, Pattern):
@ -565,7 +576,7 @@ class AutoTool(Tool, metaclass=ABCMeta):
def _itransition2dxy(in_transition: Transition | None) -> NDArray[numpy.float64]:
if in_transition is None:
return numpy.zeros(2)
irot = in_transition.our_port.rotation
irot = in_transition.their_port.rotation
assert irot is not None
itrans_dxy = rotation_matrix_2d(-irot) @ (in_transition.our_port.offset - in_transition.their_port.offset)
return itrans_dxy
@ -606,8 +617,8 @@ class AutoTool(Tool, metaclass=ABCMeta):
otrans_dxy = self._otransition2dxy(out_transition, bend_angle)
b_transition = None
if ccw is not None and bend.out_port.ptype != straight.ptype:
b_transition = self.transitions.get((bend.out_port.ptype, straight.ptype), None)
if ccw is not None and bend.in_port.ptype != straight.ptype:
b_transition = self.transitions.get((bend.in_port.ptype, straight.ptype), None)
btrans_dxy = self._itransition2dxy(b_transition)
straight_length = length - bend_dxy[0] - itrans_dxy[0] - btrans_dxy[0] - otrans_dxy[0]