Skip to content

Commit

Permalink
Merge pull request #478 from gdsfactory/bbox-poly-extract-path-fix
Browse files Browse the repository at this point in the history
Fix path-length extraction of bbox-like polygons
  • Loading branch information
nikosavola authored Aug 26, 2024
2 parents e4e0aa6 + 3900ea9 commit adcfd5d
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions gplugins/path_length_analysis/path_length_analysis_from_gds.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import numpy as np
import shapely as sh
import shapely.ops as ops
from gdsfactory import logger
from gdsfactory.typings import List, Optional, Tuple
from klayout.db import DPoint, Polygon
from scipy.signal import savgol_filter
Expand Down Expand Up @@ -89,8 +90,8 @@ def centerline_single_poly_2_ports(poly, under_sampling, port_list) -> np.ndarra
mid_point_found = _check_midpoint_found(inner_points, outer_points, port_list)

# ==== This is for debugging, keep until this is stable ====
# print(len(outer_points))
# print(len(inner_points))
# logger.debug(len(outer_points))
# logger.debug(len(inner_points))
# input()

# Relatively simple check to make sure that the first half is the outer curve and the
Expand Down Expand Up @@ -133,7 +134,7 @@ def centerline_single_poly_2_ports(poly, under_sampling, port_list) -> np.ndarra
if n_rolls > points.shape[0] and n_fixes_tried < 10 and not mid_point_found:
# Sometimes it is enough if we make the inner point be +-n elements longer
n_fixes_tried += 1
# print(f"Trying fix {n_fixes_tried}")
# logger.debug(f"Trying fix {n_fixes_tried}")
n_rolls = 0

outer_points = points[: (mid_index + fix_values[n_fixes_tried]), :]
Expand All @@ -154,7 +155,7 @@ def centerline_single_poly_2_ports(poly, under_sampling, port_list) -> np.ndarra

elif n_rolls > points.shape[0] and not mid_point_found:
# We could not find the right inner and outer points
print("We could not find the center line correctly")
logger.error(f"We could not find the center line correctly for {port_list}")
mid_point_found = True
outer_points = points[:mid_index]
inner_points = points[mid_index:]
Expand Down Expand Up @@ -188,7 +189,7 @@ def centerline_single_poly_2_ports(poly, under_sampling, port_list) -> np.ndarra
# There is a chance that the length of inner and outer is different
# Interpolate if that's the case
if inner_points.shape[0] != outer_points.shape[0]:
# print('interpolating')
# logger.debug('interpolating')
if inner_points.shape[0] > outer_points.shape[0]:
# More points in inner
outer_pts_x = outer_points[:, 0]
Expand Down Expand Up @@ -329,9 +330,13 @@ def extract_paths(

if n_ports == 2:
# This is the simplest case - a straight or a bend
centerline = centerline_single_poly_2_ports(
poly, under_sampling, consider_ports
)

if poly[0].is_box(): # only 4 points, no undersampling
centerline = centerline_single_poly_2_ports(poly, 1, consider_ports)
else:
centerline = centerline_single_poly_2_ports(
poly, under_sampling, consider_ports
)
if filter_function is not None:
centerline = filter_function(centerline)
p = gf.Path(centerline)
Expand Down

0 comments on commit adcfd5d

Please sign in to comment.