Ignore:
Timestamp:
Jun 15, 2010, 12:06:46 PM (15 years ago)
Author:
James Hudson
Message:

Refactorings to allow tests to pass.

Location:
trunk/anuga_core/source/anuga/geometry
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/anuga_core/source/anuga/geometry/__init__.py

    r7711 r7841  
    22"""
    33
    4 pass
    54
    65#Add path of package to PYTHONPATH to allow C-extensions to be loaded
  • trunk/anuga_core/source/anuga/geometry/polygon.py

    r7778 r7841  
    55import numpy as num
    66
    7 from math import sqrt
    87from anuga.utilities.numerical_tools import ensure_numeric
    98from anuga.geospatial_data.geospatial_data import ensure_absolute, \
    109                                                    Geospatial_data
    11 from anuga.config import netcdf_float
    1210import anuga.utilities.log as log
    1311
     
    129127    line1 = ensure_numeric(line1, num.float)
    130128
    131     x0 = line0[0,0]; y0 = line0[0,1]
    132     x1 = line0[1,0]; y1 = line0[1,1]
    133 
    134     x2 = line1[0,0]; y2 = line1[0,1]
    135     x3 = line1[1,0]; y3 = line1[1,1]
     129    x0 = line0[0, 0]; y0 = line0[0, 1]
     130    x1 = line0[1, 0]; y1 = line0[1, 1]
     131
     132    x2 = line1[0, 0]; y2 = line1[0, 1]
     133    x3 = line1[1, 0]; y3 = line1[1, 1]
    136134
    137135    denom = (y3-y2)*(x1-x0) - (x3-x2)*(y1-y0)
     
    208206    line1 = ensure_numeric(line1, num.float)
    209207
    210     status, value = _intersection(line0[0,0], line0[0,1],
    211                                   line0[1,0], line0[1,1],
    212                                   line1[0,0], line1[0,1],
    213                                   line1[1,0], line1[1,1])
     208    status, value = _intersection(line0[0, 0], line0[0, 1],
     209                                  line0[1, 0], line0[1, 1],
     210                                  line1[0, 0], line1[0, 1],
     211                                  line1[1, 0], line1[1, 1])
    214212
    215213    return status, value
     
    219217                       rtol=1.0e-12,
    220218                       atol=1.0e-12,                     
    221                        check_inputs=True,
    222                        verbose=False):
     219                       check_inputs=True):
    223220    """Determine if one point is inside a triangle
    224221   
     
    278275   
    279276    # Quickly reject points that are clearly outside
    280     if point[0] < min(triangle[:, 0]): return False
    281     if point[0] > max(triangle[:, 0]): return False   
    282     if point[1] < min(triangle[:, 1]): return False
    283     if point[1] > max(triangle[:, 1]): return False       
     277    if point[0] < min(triangle[:, 0]):
     278        return False
     279    if point[0] > max(triangle[:, 0]):
     280        return False   
     281    if point[1] < min(triangle[:, 1]):
     282        return False
     283    if point[1] > max(triangle[:, 1]):
     284        return False       
    284285
    285286
     
    314315        # Check if point lies on one of the edges
    315316       
    316         for X, Y in [[A,B], [B,C], [C,A]]:
     317        for X, Y in [[A, B], [B, C], [C, A]]:
    317318            res = _point_on_line(point[0], point[1],
    318319                                 X[0], X[1],
     
    341342   
    342343    def segments_joined(seg0, seg1):
     344        """ See if there are identical segments in the 2 lists. """
    343345        for i in seg0:
    344346            for j in seg1:   
     
    381383    return False
    382384   
    383    
    384 def is_inside_polygon_quick(point, polygon, closed=True, verbose=False):
    385     """Determine if one point is inside a polygon
    386     Both point and polygon are assumed to be numeric arrays or lists and
    387     no georeferencing etc or other checks will take place.
    388    
    389     As such it is faster than is_inside_polygon
    390     """
    391 
    392     # FIXME(Ole): This function isn't being used
    393     polygon = ensure_numeric(polygon, num.float)
    394     points = ensure_numeric(point, num.float) # Convert point to array of points
    395     points = num.ascontiguousarray(points[num.newaxis, :])
    396     msg = ('is_inside_polygon() must be invoked with one point only.\n'
    397            'I got %s and converted to %s' % (str(point), str(points.shape)))
    398     assert points.shape[0] == 1 and points.shape[1] == 2, msg
    399    
    400     indices = num.zeros(1, num.int)
    401 
    402     count = _separate_points_by_polygon(points, polygon, indices,
    403                                         int(closed), int(verbose))
    404 
    405     return count > 0
    406 
    407385
    408386def is_inside_polygon(point, polygon, closed=True, verbose=False):
     
    420398    else:
    421399        msg = 'is_inside_polygon must be invoked with one point only'
    422         raise msg
     400        raise Exception(msg)
    423401
    424402##
     
    743721                  figname=None,
    744722                  label=None,
    745                   alpha=None,
    746                   verbose=False):
     723                  alpha=None):
    747724    """ Take list of polygons and plot.
    748725
     
    769746    """
    770747
    771     from pylab import ion, hold, plot, axis, figure, legend, savefig, xlabel, \
     748    from pylab import ion, hold, plot, savefig, xlabel, \
    772749                      ylabel, title, close, title, fill
    773750
     
    793770            alpha = None
    794771        else:
    795             if alpha < 0.0:
    796                 alpha = 0.0
    797             if alpha > 1.0:
    798                 alpha = 1.0
     772            alpha = max(0.0, min(1.0, alpha))
    799773
    800774    n = len(polygons_points)
     
    818792    for i, item in enumerate(polygons_points):
    819793        x, y = poly_xy(item)
    820         if min(x) < minx: minx = min(x)
    821         if max(x) > maxx: maxx = max(x)
    822         if min(y) < miny: miny = min(y)
    823         if max(y) > maxy: maxy = max(y)
    824         plot(x,y,colour[i])
     794        if min(x) < minx:
     795            minx = min(x)
     796        if max(x) > maxx:
     797            maxx = max(x)
     798        if min(y) < miny:
     799            miny = min(y)
     800        if max(y) > maxy:
     801            maxy = max(y)
     802        plot(x, y, colour[i])
    825803        if alpha:
    826804            fill(x, y, colour[i], alpha=alpha)
     
    840818
    841819
    842 def poly_xy(polygon, verbose=False):
     820def poly_xy(polygon):
    843821    """ this is used within plot_polygons so need to duplicate
    844822        the first point so can have closed polygon in plot
     
    858836        raise Exception, msg
    859837
    860     x = polygon[:,0]
    861     y = polygon[:,1]
    862     x = num.concatenate((x, [polygon[0,0]]), axis = 0)
    863     y = num.concatenate((y, [polygon[0,1]]), axis = 0)
     838    x = polygon[:, 0]
     839    y = polygon[:, 1]
     840    x = num.concatenate((x, [polygon[0, 0]]), axis = 0)
     841    y = num.concatenate((y, [polygon[0, 1]]), axis = 0)
    864842
    865843    return x, y
     
    960938        assert y.shape[0] == N
    961939
    962         points = num.ascontiguousarray(num.concatenate((x[:,num.newaxis],
    963                                                         y[:,num.newaxis]),
    964                                                        axis=1 ))
     940        points = num.ascontiguousarray(num.concatenate((x[:, num.newaxis],
     941                                                        y[:, num.newaxis]),
     942                                                       axis = 1 ))
    965943
    966944        if callable(self.default):
     
    10421020        fid.write('%f, %f\n' % point)
    10431021    fid.close()
    1044 
    1045 ##
    1046 # @brief Unimplemented.
    1047 def read_tagged_polygons(filename):
    1048     """
    1049     """
    1050     pass
    10511022
    10521023
     
    10971068            if exclude is not None:
    10981069                for ex_poly in exclude:
    1099                     if is_inside_polygon([x,y], ex_poly):
     1070                    if is_inside_polygon([x, y], ex_poly):
    11001071                        append = False
    11011072
    11021073        if append is True:
    1103             points.append([x,y])
     1074            points.append([x, y])
    11041075
    11051076    return points
     
    11221093    import exceptions
    11231094
    1124     class Found(exceptions.Exception): pass
     1095    class Found(exceptions.Exception):
     1096        pass
    11251097
    11261098    polygon = ensure_numeric(polygon)
     
    12321204    # Find outer extent of polygon
    12331205    num_polygon = ensure_numeric(polygon)
    1234     max_x = max(num_polygon[:,0])
    1235     max_y = max(num_polygon[:,1])
    1236     min_x = min(num_polygon[:,0])
    1237     min_y = min(num_polygon[:,1])
     1206    max_x = max(num_polygon[:, 0])
     1207    max_y = max(num_polygon[:, 1])
     1208    min_x = min(num_polygon[:, 0])
     1209    min_y = min(num_polygon[:, 1])
    12381210
    12391211    # Keep only some points making sure extrema are kept
     
    12571229                         interpolation_points=None,
    12581230                         rtol=1.0e-6,
    1259                          atol=1.0e-8,
    1260                          verbose=False):
     1231                         atol=1.0e-8):
    12611232    """Interpolate linearly between values data on polyline nodes
    12621233    of a polyline to list of interpolation points.
     
    12891260    gauge_neighbour_id = ensure_numeric(gauge_neighbour_id, num.int)
    12901261
    1291     n = polyline_nodes.shape[0]    # Number of nodes in polyline
     1262    num_nodes = polyline_nodes.shape[0]    # Number of nodes in polyline
    12921263
    12931264    # Input sanity check
    1294     msg = 'interpolation_points are not given (interpolate.py)'
    1295     assert interpolation_points is not None, msg
    1296 
    1297     msg = 'function value must be specified at every interpolation node'
    1298     assert data.shape[0] == polyline_nodes.shape[0], msg
    1299 
    1300     msg = 'Must define function value at one or more nodes'
    1301     assert data.shape[0] > 0, msg
    1302 
    1303     if n == 1:
    1304         msg = 'Polyline contained only one point. I need more. ' + str(data)
    1305         raise Exception, msg
    1306     elif n > 1:
     1265    assert_msg = 'interpolation_points are not given (interpolate.py)'
     1266    assert interpolation_points is not None, assert_msg
     1267
     1268    assert_msg = 'function value must be specified at every interpolation node'
     1269    assert data.shape[0] == polyline_nodes.shape[0], assert_msg
     1270
     1271    assert_msg = 'Must define function value at one or more nodes'
     1272    assert data.shape[0] > 0, assert_msg
     1273
     1274    if num_nodes == 1:
     1275        assert_msg = 'Polyline contained only one point. I need more. '
     1276        assert_msg += str(data)
     1277        raise Exception, assert_msg
     1278    elif num_nodes > 1:
    13071279        _interpolate_polyline(data,
    13081280                              polyline_nodes,
     
    13461318
    13471319else:
    1348     msg = 'C implementations could not be accessed by %s.\n ' %__file__
    1349     msg += 'Make sure compile_all.py has been run as described in '
    1350     msg += 'the ANUGA installation guide.'
    1351     raise Exception, msg
     1320    error_msg = 'C implementations could not be accessed by %s.\n ' %__file__
     1321    error_msg += 'Make sure compile_all.py has been run as described in '
     1322    error_msg += 'the ANUGA installation guide.'
     1323    raise Exception(error_msg)
    13521324
    13531325
  • trunk/anuga_core/source/anuga/geometry/test_polygon.py

    r7711 r7841  
    178178
    179179    def test_inside_polygon_main(self):
    180         """test_is_inside_polygon_quick
     180        """test_is_inside_polygon
    181181       
    182182        Test fast version of of is_inside_polygon
     
    186186        polygon = [[0,0], [1,0], [1,1], [0,1]]
    187187
    188         assert is_inside_polygon_quick( (0.5, 0.5), polygon )
    189         assert not is_inside_polygon_quick( (0.5, 1.5), polygon )
    190         assert not is_inside_polygon_quick( (0.5, -0.5), polygon )
    191         assert not is_inside_polygon_quick( (-0.5, 0.5), polygon )
    192         assert not is_inside_polygon_quick( (1.5, 0.5), polygon )
     188        assert is_inside_polygon( (0.5, 0.5), polygon )
     189        assert not is_inside_polygon( (0.5, 1.5), polygon )
     190        assert not is_inside_polygon( (0.5, -0.5), polygon )
     191        assert not is_inside_polygon( (-0.5, 0.5), polygon )
     192        assert not is_inside_polygon( (1.5, 0.5), polygon )
    193193
    194194        # Try point on borders
    195         assert is_inside_polygon_quick( (1., 0.5), polygon, closed=True)
    196         assert is_inside_polygon_quick( (0.5, 1), polygon, closed=True)
    197         assert is_inside_polygon_quick( (0., 0.5), polygon, closed=True)
    198         assert is_inside_polygon_quick( (0.5, 0.), polygon, closed=True)
    199 
    200         assert not is_inside_polygon_quick( (0.5, 1), polygon, closed=False)
    201         assert not is_inside_polygon_quick( (0., 0.5), polygon, closed=False)
    202         assert not is_inside_polygon_quick( (0.5, 0.), polygon, closed=False)
    203         assert not is_inside_polygon_quick( (1., 0.5), polygon, closed=False)
     195        assert is_inside_polygon( (1., 0.5), polygon, closed=True)
     196        assert is_inside_polygon( (0.5, 1), polygon, closed=True)
     197        assert is_inside_polygon( (0., 0.5), polygon, closed=True)
     198        assert is_inside_polygon( (0.5, 0.), polygon, closed=True)
     199
     200        assert not is_inside_polygon( (0.5, 1), polygon, closed=False)
     201        assert not is_inside_polygon( (0., 0.5), polygon, closed=False)
     202        assert not is_inside_polygon( (0.5, 0.), polygon, closed=False)
     203        assert not is_inside_polygon( (1., 0.5), polygon, closed=False)
    204204
    205205
     
    229229        assert not is_inside_polygon( (0.5, -0.5), polygon )
    230230
    231         assert is_inside_polygon_quick( (0.5, 0.5), polygon )
    232         assert is_inside_polygon_quick( (1, -0.5), polygon )
    233         assert is_inside_polygon_quick( (1.5, 0), polygon )
    234 
    235         assert not is_inside_polygon_quick( (0.5, 1.5), polygon )
    236         assert not is_inside_polygon_quick( (0.5, -0.5), polygon )
     231        assert is_inside_polygon( (0.5, 0.5), polygon )
     232        assert is_inside_polygon( (1, -0.5), polygon )
     233        assert is_inside_polygon( (1.5, 0), polygon )
     234
     235        assert not is_inside_polygon( (0.5, 1.5), polygon )
     236        assert not is_inside_polygon( (0.5, -0.5), polygon )
    237237
    238238        # Very convoluted polygon
     
    449449            assert is_inside_polygon(point, polygon)
    450450
    451             assert is_inside_polygon_quick(point, polygon)
     451            assert is_inside_polygon(point, polygon)
    452452
    453453
     
    458458        for point in points:
    459459            assert is_inside_polygon(point, polygon)
    460             assert is_inside_polygon_quick(point, polygon)
     460            assert is_inside_polygon(point, polygon)
    461461
    462462
     
    15981598        assert y[4] == 6
    15991599
    1600     # Disabled
    1601     def xtest_plot_polygons(self):
     1600
     1601    def test_plot_polygons(self):
    16021602        import os
    16031603
     
    16051605        polygon1 = [[0,0], [1,0], [1,1], [0,1]]
    16061606        polygon2 = [[1,1], [2,1], [3,2], [2,2]]
    1607         v = plot_polygons([polygon1, polygon2], 'test1')
     1607        v = plot_polygons([polygon1, polygon2], figname='test1')
    16081608        assert len(v) == 4
    16091609        assert v[0] == 0
     
    16141614        # Another case
    16151615        polygon3 = [[1,5], [10,1], [100,10], [50,10], [3,6]]
    1616         v = plot_polygons([polygon2,polygon3], 'test2')
     1616        v = plot_polygons([polygon2,polygon3], figname='test2')
    16171617        assert len(v) == 4
    16181618        assert v[0] == 1
Note: See TracChangeset for help on using the changeset viewer.