Changeset 1024


Ignore:
Timestamp:
Mar 7, 2005, 3:15:02 PM (20 years ago)
Author:
ole
Message:

Added function outside_polygon and a test

Location:
inundation/ga/storm_surge/pyvolution
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • inundation/ga/storm_surge/pyvolution/test_util.py

    r1018 r1024  
    919919        assert allclose( res, [0,1,2] )
    920920
     921    def test_outside_polygon(self):
     922
     923        U = [[0,0], [1,0], [1,1], [0,1]] #Unit square   
     924        assert not outside_polygon( [0.5, 0.5], U )
     925        #evaluate to False as the point 0.5, 0.5 is inside the unit square
     926
     927        assert outside_polygon( [1.5, 0.5], U )
     928        #evaluate to True as the point 1.5, 0.5 is outside the unit square
     929       
     930        indices = outside_polygon( [[0.5, 0.5], [1, -0.5], [0.3, 0.2]], U )
     931        assert allclose( indices, [1] )
     932       
     933        #One more test of vector formulation returning indices
     934        polygon = [[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]]
     935        points = [ [0.5, 0.5], [1, -0.5], [1.5, 0], [0.5, 1.5], [0.5, -0.5]]
     936        res = outside_polygon( points, polygon )
     937
     938        assert allclose( res, [3,4] )
     939
     940
     941
     942        polygon = [[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]]
     943        points = [ [0.5, 1.4], [0.5, 0.5], [1, -0.5], [1.5, 0], [0.5, 1.5], [0.5, -0.5]]
     944        res = outside_polygon( points, polygon )
     945
     946        assert allclose( res, [0, 4, 5] )       
     947       
    921948
    922949    def test_populate_polygon(self):
  • inundation/ga/storm_surge/pyvolution/util.py

    r999 r1024  
    918918       
    919919    Examples:
    920        inside_polygon( [0.5, 0.5], [[0,0], [1,0], [1,1], [0,1]] )
     920       U = [[0,0], [1,0], [1,1], [0,1]] #Unit square
     921       inside_polygon( [0.5, 0.5], U)
    921922       will evaluate to True as the point 0.5, 0.5 is inside the unit square
    922923       
    923        inside_polygon( [[0.5, 0.5], [1, -0.5], [0.3, 0.2]] )
     924       inside_polygon( [[0.5, 0.5], [1, -0.5], [0.3, 0.2]], U)
    924925       will return the indices [0, 2] as only the first and the last point
    925926       is inside the unit square
     
    10751076        return indices[:count]
    10761077
     1078
     1079#def remove_from(A, B):
     1080#    """Assume that A
     1081#    """
     1082#    from Numeric import search_sorted##
     1083#
     1084#    ind = search_sorted(A, B)
     1085
     1086   
     1087
     1088def outside_polygon(point, polygon, closed = True, verbose = False):
     1089    """Determine whether points are outside a polygon
     1090   
     1091    Input:
     1092       point - Tuple of (x, y) coordinates, or list of tuples
     1093       polygon - list of vertices of polygon
     1094       closed - (optional) determine whether points on boundary should be
     1095       regarded as belonging to the polygon (closed = True)
     1096       or not (closed = False)
     1097   
     1098    Output:
     1099       If one point is considered, True or False is returned.
     1100       If multiple points are passed in, the function returns indices
     1101       of those points that fall outside the polygon     
     1102       
     1103    Examples:
     1104       U = [[0,0], [1,0], [1,1], [0,1]] #Unit square   
     1105       outside_polygon( [0.5, 0.5], U )
     1106       will evaluate to False as the point 0.5, 0.5 is inside the unit square
     1107
     1108       ouside_polygon( [1.5, 0.5], U )
     1109       will evaluate to True as the point 1.5, 0.5 is outside the unit square
     1110       
     1111       outside_polygon( [[0.5, 0.5], [1, -0.5], [0.3, 0.2]], U )
     1112       will return the indices [1] as only the first and the last point
     1113       is inside the unit square
     1114    """
     1115
     1116    res  = inside_polygon(point, polygon, closed, verbose)
     1117
     1118    if res is True or res is False:
     1119        return not res
     1120
     1121    #Now invert indices
     1122    from Numeric import arrayrange, compress
     1123    outside_indices = arrayrange(len(point))
     1124    for i in res:
     1125        outside_indices = compress(outside_indices != i, outside_indices)
     1126    return outside_indices
     1127
     1128
     1129
    10771130class Polygon_function:
    10781131    """Create callable object f: x,y -> z, where a,y,z are vectors and
Note: See TracChangeset for help on using the changeset viewer.