Is there a way to quickly clip the Region?

I need to extract multiple clip regions from a target Region, just like the function Layout#multi_clip() does, except that the execution object is replaced with Region. But it seems that there is no direct function to achieve this process, so I try to achieve it through the AND operation of two region:

# method 1
clip_region_list = []
for  polygon in clip_area_polygon_list:   #clip_area_polygon_list is a list of clip areas defined by polygon 
        clip_boundary= db.Region(polygon)
        clip_region_list.append(clip_boundary & target_region)    # target_region is the region to be clipped

However, the AND operation of clip_boundary and target_region in cycle will take a lot of time, because the target_region is usually very large. So I used method 2 to reduce the runtime.

# method 2
clip_region_list = []
clips_region = target_region & db.Region(clip_area_polygon_list)
for  polygon in clip_area_polygon_list:
        clip_boundary= db.Region(polygon)
        clip_region_list.append(clip_boundary & clips_region)

My question is whether there is a faster way, or whether this function can be added to the new version of klayout if it is easy to do. Because the clip operation on a single-layer region is a very common operation.


  • @garry The clip function is fundamentally different from anything the Region can offer. It will perform a clip hierarchically - i.e. generate clip variants of cells and use those if a cell is partially cut. It will maintain sub-hierarchies unless a variant needs to be created. This makes the clip feature much more efficient than the AND approach.

    In contrast, the Region object is a flat representation and this will degrade performance significantly. So for top performance you should really think about your design - the Region isn't the best choice here.

    Apart from that, the Region already uses the clip approach for AND if:

    • "strict_handling" is set to "false" (i.e. region.strict_handling = False)
    • The other operator is a single rectangle

    So this code (disclaimer: not tested):

    r = ... # the region to clip
    clip_rects = ... # an array of pya.Box objects for the clip rectangles
    r.strict_handling = False
    result = pya.Region()
    for box in clip_rects:
      result += r & pya.Region(box)

    should actually deliver the multi-clip feature already. An integrated solution would not be much different.


  • Thank you very much @Matthias , I will test the operation with the code you provided

Sign In or Register to comment.