Coverage for klayout_pex/rcx25/c/polygon_utils.py: 92%

24 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-03-31 19:36 +0000

1#! /usr/bin/env python3 

2# 

3# -------------------------------------------------------------------------------- 

4# SPDX-FileCopyrightText: 2024 Martin Jan Köhler and Harald Pretl 

5# Johannes Kepler University, Institute for Integrated Circuits. 

6# 

7# This file is part of KPEX 

8# (see https://github.com/martinjankoehler/klayout-pex). 

9# 

10# This program is free software: you can redistribute it and/or modify 

11# it under the terms of the GNU General Public License as published by 

12# the Free Software Foundation, either version 3 of the License, or 

13# (at your option) any later version. 

14# 

15# This program is distributed in the hope that it will be useful, 

16# but WITHOUT ANY WARRANTY; without even the implied warranty of 

17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

18# GNU General Public License for more details. 

19# 

20# You should have received a copy of the GNU General Public License 

21# along with this program. If not, see <http://www.gnu.org/licenses/>. 

22# SPDX-License-Identifier: GPL-3.0-or-later 

23# -------------------------------------------------------------------------------- 

24# 

25 

26from typing import * 

27import math 

28 

29import klayout.db as kdb 

30from klayout.dbcore import EdgeWithProperties 

31 

32from klayout_pex.rcx25.types import EdgeDistance 

33from klayout_pex.log import warning 

34 

35 

36# NOTE: first lateral nearby shape blocks everything beyond (like sidewall situation) up to halo 

37def nearest_edge_distance(polygon: kdb.Polygon) -> float: 

38 bbox: kdb.Box = polygon.bbox() 

39 

40 if not polygon.is_box(): 

41 warning(f"Side overlap, outside polygon {polygon} is not a box. " 

42 f"Currently, only boxes are supported, will be using bounding box {bbox}") 

43 ## distance_near = (bbox.p1.y + bbox.p2.y) / 2.0 

44 distance_near = min(bbox.p1.y, bbox.p2.y) 

45 if distance_near < 0: 

46 distance_near = 0 

47 return distance_near 

48 

49 

50def find_polygon_with_nearest_edge(polygons_on_same_layer: List[kdb.PolygonWithProperties]) -> Tuple[EdgeDistance, kdb.PolygonWithProperties]: 

51 nearest_lateral_shape = (math.inf, polygons_on_same_layer[0]) 

52 

53 for p in polygons_on_same_layer: 

54 dnear = nearest_edge_distance(p) 

55 if dnear < nearest_lateral_shape[0]: 

56 nearest_lateral_shape = (dnear, p) 

57 

58 return nearest_lateral_shape 

59 

60 

61def nearest_edge(polygon: kdb.PolygonWithProperties) -> EdgeWithProperties: 

62 edge = [e for e in polygon.each_edge() if e.d().x < 0][-1] 

63 return kdb.EdgeWithProperties(edge, properties={'net': polygon.property('net')})