performance optimizations
This commit is contained in:
parent
22ec194560
commit
c989ab6b9f
1 changed files with 408 additions and 815 deletions
|
|
@ -27,9 +27,9 @@ class ComponentResult:
|
|||
Standard container for generated move geometry and state.
|
||||
"""
|
||||
__slots__ = (
|
||||
'geometry', 'dilated_geometry', 'proxy_geometry', 'actual_geometry',
|
||||
'geometry', 'dilated_geometry', 'proxy_geometry', 'actual_geometry', 'dilated_actual_geometry',
|
||||
'end_port', 'length', 'move_type', 'bounds', 'dilated_bounds',
|
||||
'total_bounds', 'total_dilated_bounds', 'total_bounds_box', 'total_dilated_bounds_box', '_t_cache'
|
||||
'total_bounds', 'total_dilated_bounds', '_t_cache', '_total_geom_list', '_offsets', '_coords_cache'
|
||||
)
|
||||
|
||||
def __init__(
|
||||
|
|
@ -40,42 +40,57 @@ class ComponentResult:
|
|||
dilated_geometry: list[Polygon] | None = None,
|
||||
proxy_geometry: list[Polygon] | None = None,
|
||||
actual_geometry: list[Polygon] | None = None,
|
||||
dilated_actual_geometry: list[Polygon] | None = None,
|
||||
skip_bounds: bool = False,
|
||||
move_type: str = 'Unknown'
|
||||
move_type: str = 'Unknown',
|
||||
_total_geom_list: list[Polygon] | None = None,
|
||||
_offsets: list[int] | None = None,
|
||||
_coords_cache: numpy.ndarray | None = None
|
||||
) -> None:
|
||||
self.geometry = geometry
|
||||
self.dilated_geometry = dilated_geometry
|
||||
self.proxy_geometry = proxy_geometry
|
||||
self.actual_geometry = actual_geometry
|
||||
self.dilated_actual_geometry = dilated_actual_geometry
|
||||
self.end_port = end_port
|
||||
self.length = length
|
||||
self.move_type = move_type
|
||||
self._t_cache = {}
|
||||
|
||||
if _total_geom_list is not None and _offsets is not None:
|
||||
self._total_geom_list = _total_geom_list
|
||||
self._offsets = _offsets
|
||||
self._coords_cache = _coords_cache
|
||||
else:
|
||||
# Flatten everything for fast vectorized translate
|
||||
gl = list(geometry)
|
||||
o = [len(geometry)]
|
||||
if dilated_geometry: gl.extend(dilated_geometry)
|
||||
o.append(len(gl))
|
||||
if proxy_geometry: gl.extend(proxy_geometry)
|
||||
o.append(len(gl))
|
||||
if actual_geometry: gl.extend(actual_geometry)
|
||||
o.append(len(gl))
|
||||
if dilated_actual_geometry: gl.extend(dilated_actual_geometry)
|
||||
self._total_geom_list = gl
|
||||
self._offsets = o
|
||||
self._coords_cache = shapely.get_coordinates(gl)
|
||||
|
||||
if not skip_bounds:
|
||||
# Vectorized bounds calculation
|
||||
self.bounds = shapely.bounds(geometry)
|
||||
# Total bounds across all polygons in the move
|
||||
self.total_bounds = numpy.array([
|
||||
numpy.min(self.bounds[:, 0]),
|
||||
numpy.min(self.bounds[:, 1]),
|
||||
numpy.max(self.bounds[:, 2]),
|
||||
numpy.max(self.bounds[:, 3])
|
||||
numpy.min(self.bounds[:, 0]), numpy.min(self.bounds[:, 1]),
|
||||
numpy.max(self.bounds[:, 2]), numpy.max(self.bounds[:, 3])
|
||||
])
|
||||
self.total_bounds_box = box(*self.total_bounds)
|
||||
|
||||
if dilated_geometry is not None:
|
||||
self.dilated_bounds = shapely.bounds(dilated_geometry)
|
||||
self.total_dilated_bounds = numpy.array([
|
||||
numpy.min(self.dilated_bounds[:, 0]),
|
||||
numpy.min(self.dilated_bounds[:, 1]),
|
||||
numpy.max(self.dilated_bounds[:, 2]),
|
||||
numpy.max(self.dilated_bounds[:, 3])
|
||||
numpy.min(self.dilated_bounds[:, 0]), numpy.min(self.dilated_bounds[:, 1]),
|
||||
numpy.max(self.dilated_bounds[:, 2]), numpy.max(self.dilated_bounds[:, 3])
|
||||
])
|
||||
self.total_dilated_bounds_box = box(*self.total_dilated_bounds)
|
||||
else:
|
||||
self.dilated_bounds = None
|
||||
self.total_dilated_bounds = None
|
||||
self.total_dilated_bounds_box = None
|
||||
|
||||
def translate(self, dx: float, dy: float) -> ComponentResult:
|
||||
"""
|
||||
|
|
@ -87,47 +102,44 @@ class ComponentResult:
|
|||
if (dxr, dyr) in self._t_cache:
|
||||
return self._t_cache[(dxr, dyr)]
|
||||
|
||||
# Vectorized translation
|
||||
geoms = list(self.geometry)
|
||||
num_geom = len(self.geometry)
|
||||
# FASTEST TRANSLATE
|
||||
new_coords = self._coords_cache + [dx, dy]
|
||||
new_total_arr = shapely.set_coordinates(list(self._total_geom_list), new_coords)
|
||||
new_total = new_total_arr.tolist()
|
||||
|
||||
offsets = [num_geom]
|
||||
if self.dilated_geometry is not None:
|
||||
geoms.extend(self.dilated_geometry)
|
||||
offsets.append(len(geoms))
|
||||
|
||||
if self.proxy_geometry is not None:
|
||||
geoms.extend(self.proxy_geometry)
|
||||
offsets.append(len(geoms))
|
||||
|
||||
if self.actual_geometry is not None:
|
||||
geoms.extend(self.actual_geometry)
|
||||
offsets.append(len(geoms))
|
||||
|
||||
import shapely
|
||||
coords = shapely.get_coordinates(geoms)
|
||||
translated = shapely.set_coordinates(geoms, coords + [dx, dy])
|
||||
|
||||
new_geom = list(translated[:offsets[0]])
|
||||
new_dil = list(translated[offsets[0]:offsets[1]]) if self.dilated_geometry is not None else None
|
||||
new_proxy = list(translated[offsets[1]:offsets[2]]) if self.proxy_geometry is not None else None
|
||||
new_actual = list(translated[offsets[2]:offsets[3]]) if self.actual_geometry is not None else None
|
||||
o = self._offsets
|
||||
new_geom = new_total[:o[0]]
|
||||
new_dil = new_total[o[0]:o[1]] if self.dilated_geometry is not None else None
|
||||
new_proxy = new_total[o[1]:o[2]] if self.proxy_geometry is not None else None
|
||||
new_actual = new_total[o[2]:o[3]] if self.actual_geometry is not None else None
|
||||
new_dil_actual = new_total[o[3]:] if self.dilated_actual_geometry is not None else None
|
||||
|
||||
new_port = Port(self.end_port.x + dx, self.end_port.y + dy, self.end_port.orientation)
|
||||
res = ComponentResult(new_geom, new_port, self.length, new_dil, new_proxy, new_actual, skip_bounds=True, move_type=self.move_type)
|
||||
|
||||
# Optimize: reuse and translate bounds
|
||||
res.bounds = self.bounds + [dx, dy, dx, dy]
|
||||
res.total_bounds = self.total_bounds + [dx, dy, dx, dy]
|
||||
res.total_bounds_box = box(*res.total_bounds)
|
||||
# Fast bypass of __init__
|
||||
res = self.__class__.__new__(self.__class__)
|
||||
res.geometry = new_geom
|
||||
res.dilated_geometry = new_dil
|
||||
res.proxy_geometry = new_proxy
|
||||
res.actual_geometry = new_actual
|
||||
res.dilated_actual_geometry = new_dil_actual
|
||||
res.end_port = new_port
|
||||
res.length = self.length
|
||||
res.move_type = self.move_type
|
||||
res._t_cache = {}
|
||||
res._total_geom_list = new_total
|
||||
res._offsets = o
|
||||
res._coords_cache = new_coords
|
||||
|
||||
db = [dx, dy, dx, dy]
|
||||
res.bounds = self.bounds + db
|
||||
res.total_bounds = self.total_bounds + db
|
||||
if self.dilated_bounds is not None:
|
||||
res.dilated_bounds = self.dilated_bounds + [dx, dy, dx, dy]
|
||||
res.total_dilated_bounds = self.total_dilated_bounds + [dx, dy, dx, dy]
|
||||
res.total_dilated_bounds_box = box(*res.total_dilated_bounds)
|
||||
res.dilated_bounds = self.dilated_bounds + db
|
||||
res.total_dilated_bounds = self.total_dilated_bounds + db
|
||||
else:
|
||||
res.dilated_bounds = None
|
||||
res.total_dilated_bounds = None
|
||||
res.total_dilated_bounds_box = None
|
||||
|
||||
self._t_cache[(dxr, dyr)] = res
|
||||
return res
|
||||
|
|
@ -193,7 +205,7 @@ class Straight:
|
|||
dilated_geom = [Polygon(poly_points_dil)]
|
||||
|
||||
# For straight segments, geom IS the actual geometry
|
||||
return ComponentResult(geometry=geom, end_port=end_port, length=actual_length, dilated_geometry=dilated_geom, actual_geometry=geom, move_type='Straight')
|
||||
return ComponentResult(geometry=geom, end_port=end_port, length=actual_length, dilated_geometry=dilated_geom, actual_geometry=geom, dilated_actual_geometry=dilated_geom, move_type='Straight')
|
||||
|
||||
|
||||
def _get_num_segments(radius: float, angle_deg: float, sagitta: float = 0.01) -> int:
|
||||
|
|
@ -267,21 +279,10 @@ def _clip_bbox(
|
|||
half_sweep = sweep / 2.0
|
||||
|
||||
# Define vertices in local space (center at 0,0, symmetry axis along +X)
|
||||
# 1. Start Inner
|
||||
# 2. Start Outer
|
||||
# 3. Peak Outer Start (tangent intersection approximation)
|
||||
# 4. Peak Outer End
|
||||
# 5. End Outer
|
||||
# 6. End Inner
|
||||
# 7. Peak Inner (ensures convexity and inner clipping)
|
||||
|
||||
# To clip the outer corner, we use two peak vertices that follow the arc tighter.
|
||||
cos_hs = numpy.cos(half_sweep)
|
||||
cos_hs2 = numpy.cos(half_sweep / 2.0)
|
||||
tan_hs2 = numpy.tan(half_sweep / 2.0)
|
||||
|
||||
# Distance to peak from center: r_out / cos(hs/2)
|
||||
# At angles +/- hs/2
|
||||
peak_r = r_out / cos_hs2
|
||||
|
||||
local_verts = [
|
||||
|
|
@ -415,9 +416,11 @@ class Bend90:
|
|||
)
|
||||
|
||||
dilated_geom = None
|
||||
dilated_actual_geom = None
|
||||
if dilation > 0:
|
||||
dilated_actual_geom = _get_arc_polygons(cx, cy, actual_radius, width, t_start, t_end, sagitta, dilation=dilation)
|
||||
if collision_type == "arc":
|
||||
dilated_geom = _get_arc_polygons(cx, cy, actual_radius, width, t_start, t_end, sagitta, dilation=dilation)
|
||||
dilated_geom = dilated_actual_geom
|
||||
else:
|
||||
dilated_geom = [p.buffer(dilation) for p in collision_polys]
|
||||
|
||||
|
|
@ -428,6 +431,7 @@ class Bend90:
|
|||
dilated_geometry=dilated_geom,
|
||||
proxy_geometry=proxy_geom,
|
||||
actual_geometry=arc_polys,
|
||||
dilated_actual_geometry=dilated_actual_geom,
|
||||
move_type='Bend90'
|
||||
)
|
||||
|
||||
|
|
@ -479,14 +483,10 @@ class SBend:
|
|||
theta = 2 * numpy.arctan2(abs(local_dy), local_dx)
|
||||
|
||||
if abs(theta) < 1e-9:
|
||||
# Practically straight, but offset implies we need a bend.
|
||||
# If offset is also tiny, return a straight?
|
||||
if abs(offset) < 1e-6:
|
||||
# Degenerate case: effectively straight
|
||||
return Straight.generate(start_port, numpy.sqrt(local_dx**2 + local_dy**2), width, snap_to_grid=False, dilation=dilation)
|
||||
raise ValueError("SBend calculation failed: theta close to zero")
|
||||
# De-generate to straight
|
||||
actual_len = numpy.sqrt(local_dx**2 + local_dy**2)
|
||||
return Straight.generate(start_port, actual_len, width, snap_to_grid=False, dilation=dilation)
|
||||
|
||||
# Avoid division by zero if theta is 0 (though unlikely due to offset check)
|
||||
denom = (2 * (1 - numpy.cos(theta)))
|
||||
if abs(denom) < 1e-9:
|
||||
raise ValueError("SBend calculation failed: radius denominator zero")
|
||||
|
|
@ -495,7 +495,8 @@ class SBend:
|
|||
|
||||
# Limit radius to prevent giant arcs
|
||||
if actual_radius > 100000.0:
|
||||
raise ValueError("SBend calculation failed: radius too large")
|
||||
actual_len = numpy.sqrt(local_dx**2 + local_dy**2)
|
||||
return Straight.generate(start_port, actual_len, width, snap_to_grid=False, dilation=dilation)
|
||||
|
||||
direction = 1 if local_dy > 0 else -1
|
||||
c1_angle = rad_start + direction * numpy.pi / 2
|
||||
|
|
@ -526,11 +527,14 @@ class SBend:
|
|||
proxy_geom = [p1, p2]
|
||||
|
||||
dilated_geom = None
|
||||
dilated_actual_geom = None
|
||||
if dilation > 0:
|
||||
d1 = _get_arc_polygons(cx1, cy1, actual_radius, width, ts1, te1, sagitta, dilation=dilation)[0]
|
||||
d2 = _get_arc_polygons(cx2, cy2, actual_radius, width, ts2, te2, sagitta, dilation=dilation)[0]
|
||||
dilated_actual_geom = [d1, d2]
|
||||
|
||||
if collision_type == "arc":
|
||||
d1 = _get_arc_polygons(cx1, cy1, actual_radius, width, ts1, te1, sagitta, dilation=dilation)[0]
|
||||
d2 = _get_arc_polygons(cx2, cy2, actual_radius, width, ts2, te2, sagitta, dilation=dilation)[0]
|
||||
dilated_geom = [d1, d2]
|
||||
dilated_geom = dilated_actual_geom
|
||||
else:
|
||||
dilated_geom = [p.buffer(dilation) for p in collision_polys]
|
||||
|
||||
|
|
@ -541,5 +545,6 @@ class SBend:
|
|||
dilated_geometry=dilated_geom,
|
||||
proxy_geometry=proxy_geom,
|
||||
actual_geometry=arc_polys,
|
||||
dilated_actual_geometry=dilated_actual_geom,
|
||||
move_type='SBend'
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in a new issue