Ignore:
Timestamp:
Dec 2, 2004, 3:28:33 PM (20 years ago)
Author:
ole
Message:

Added arbitrary functions to Polygon_function

File:
1 edited

Legend:

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

    r659 r664  
    5757    """
    5858   
    59     #FIXME: Could do with some optimisation         
     59    #FIXME: Could do with some C-optimisation         
    6060       
    6161    from Numeric import array, dot, allclose
     
    9494       point - Tuple of (x, y) coordinates, or list of tuples
    9595       polygon - list of vertices of polygon
     96       closed - (optional) determine whether points on boundary should be
     97       regarded as belonging to the polygon (closed = True)
     98       or not (closed = False)
    9699   
    97100    Output:
     
    109112       
    110113    Remarks:
    111        <Copy from source Franklins code >   
     114       The vertices may be listed clockwise or counterclockwise and
     115       the first point may optionally be repeated.   
     116       Polygons do not need to be convex.
     117       Polygons can have holes in them and points inside a hole is
     118       regarded as being outside the polygon.         
    112119               
    113                
    114        points at a vertex are considered to be inside         
    115120       
    116     Algorithm due to Darel Finley, http://www.alienryderflex.com/polygon/
    117    
    118    
    119     FIXME: More comments about algoritm
    120     FIXME: Deal with case where point on border of polygon (closed)
    121        
     121    Algorithm is based on work by Darel Finley,
     122    http://www.alienryderflex.com/polygon/
     123   
    122124    """   
    123125
     
    166168        y = points[k, 1]       
    167169
    168        
    169170        inside = False
    170171        for i in range(N):
     
    187188        if inside: indices.append(k)
    188189
    189         #print x, y, polygon, inside
    190 
    191    
    192190    if one_point:
    193191        return inside
     
    222220    Note: If two polygons overlap, the one last in the list takes precedence
    223221
    224     Note: default can only be a constant.   
    225222    """
    226223   
     
    253250        x = array(x).astype(Float)
    254251        y = array(y).astype(Float)     
    255         x = reshape(x, (len(x), 1))
    256         y = reshape(y, (len(y), 1))     
    257                
    258         points = concatenate( (x,y), axis=1 )
    259        
    260         z = ones(x.shape[0], Float) * self.default
     252       
     253        N = len(x)
     254        assert len(y) == N
     255       
     256        points = concatenate( (reshape(x, (N, 1)),
     257                               reshape(y, (N, 1))), axis=1 )
     258
     259        if callable(self.default):
     260            z = self.default(x,y)
     261        else:                   
     262            z = ones(N, Float) * self.default
     263           
    261264        for polygon, value in self.regions:
    262265            indices = inside_polygon(points, polygon)
    263             for i in indices:
    264                 z[i] = value
    265 
    266         #from sys import exit; exit()   
     266           
     267            #FIXME: This needs to be vectorised
     268            if callable(value):
     269                for i in indices:
     270                    xx = array([x[i]])
     271                    yy = array([y[i]])
     272                    z[i] = value(xx, yy)[0]
     273            else:   
     274                for i in indices:
     275                    z[i] = value
     276
    267277        return z                 
    268278
Note: See TracChangeset for help on using the changeset viewer.