diff --git a/prjxray/overlay.py b/prjxray/overlay.py new file mode 100644 index 00000000..64c7cce6 --- /dev/null +++ b/prjxray/overlay.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# Copyright (C) 2017-2020 The Project X-Ray Authors. +# +# Use of this source code is governed by a ISC-style +# license that can be found in the LICENSE file or at +# https://opensource.org/licenses/ISC +# +# SPDX-License-Identifier: ISC +class Overlay(object): + """ Object that represents an overlay. + + Can be used to iterate over tiles and sites not inside a partition region. + + """ + + def __init__(self, region_dict): + self.region_dict = region_dict + + def tile_in_roi(self, grid_loc): + """ Returns true if grid_loc (GridLoc tuple) is within the overlay. """ + x = grid_loc.grid_x + y = grid_loc.grid_y + for _, bounds in self.region_dict.items(): + x1, x2, y1, y2 = bounds + if x1 <= x and x <= x2 and y1 <= y and y <= y2: + return False + return True diff --git a/tests/test_util.py b/tests/test_util.py index 8ea851ed..2187630c 100755 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -21,7 +21,9 @@ from unittest import TestCase, main environ['XRAY_DATABASE_ROOT'] = '.' environ['XRAY_PART'] = './' -from prjxray.util import get_roi +from prjxray.util import get_roi, get_db_root +from prjxray.overlay import Overlay +from prjxray.grid_types import GridLoc @contextmanager @@ -69,6 +71,17 @@ class TestUtil(TestCase): self.assertListEqual( list(get_roi().gen_sites()), [('ATILE', 'FOO', 'BAR')]) + def test_in_roi_overlay(self): + region_dict = {} + region_dict['pr1'] = (10, 58, 0, 51) + region_dict['pr2'] = (10, 58, 52, 103) + overlay = Overlay(region_dict) + self.assertFalse(overlay.tile_in_roi(GridLoc(18, 50))) + self.assertFalse(overlay.tile_in_roi(GridLoc(18, 84))) + self.assertTrue(overlay.tile_in_roi(GridLoc(8, 50))) + self.assertTrue(overlay.tile_in_roi(GridLoc(18, 112))) + self.assertTrue(overlay.tile_in_roi(GridLoc(80, 40))) + if __name__ == '__main__': main()