Ignore:
Timestamp:
Mar 19, 2009, 1:43:34 PM (16 years ago)
Author:
rwilson
Message:

Merged trunk into numpy, all tests and validations work.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/numpy/anuga/utilities/test_polygon.py

    r6441 r6553  
    181181        polygon = [[0,0], [1,0], [1,1], [0,1]]
    182182
     183        assert is_inside_polygon_quick( (0.5, 0.5), polygon )
     184        assert not is_inside_polygon_quick( (0.5, 1.5), polygon )
     185        assert not is_inside_polygon_quick( (0.5, -0.5), polygon )
     186        assert not is_inside_polygon_quick( (-0.5, 0.5), polygon )
     187        assert not is_inside_polygon_quick( (1.5, 0.5), polygon )
     188
     189        # Try point on borders
     190        assert is_inside_polygon_quick( (1., 0.5), polygon, closed=True)
     191        assert is_inside_polygon_quick( (0.5, 1), polygon, closed=True)
     192        assert is_inside_polygon_quick( (0., 0.5), polygon, closed=True)
     193        assert is_inside_polygon_quick( (0.5, 0.), polygon, closed=True)
     194
     195        assert not is_inside_polygon_quick( (0.5, 1), polygon, closed=False)
     196        assert not is_inside_polygon_quick( (0., 0.5), polygon, closed=False)
     197        assert not is_inside_polygon_quick( (0.5, 0.), polygon, closed=False)
     198        assert not is_inside_polygon_quick( (1., 0.5), polygon, closed=False)
     199
     200
     201    def test_inside_polygon_main(self):
     202        # Simplest case: Polygon is the unit square
     203        polygon = [[0,0], [1,0], [1,1], [0,1]]
     204
    183205        # From real example (that failed)
    184206        polygon = [[20,20], [40,20], [40,40], [20,40]]
     
    195217        # More convoluted and non convex polygon
    196218        polygon = [[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]]
    197         assert is_inside_polygon((0.5, 0.5), polygon)
    198         assert is_inside_polygon((1, -0.5), polygon)
    199         assert is_inside_polygon((1.5, 0), polygon)
    200 
    201         assert not is_inside_polygon((0.5, 1.5), polygon)
    202         assert not is_inside_polygon((0.5, -0.5), polygon)
     219        assert is_inside_polygon( (0.5, 0.5), polygon )
     220        assert is_inside_polygon( (1, -0.5), polygon )
     221        assert is_inside_polygon( (1.5, 0), polygon )
     222
     223        assert not is_inside_polygon( (0.5, 1.5), polygon )
     224        assert not is_inside_polygon( (0.5, -0.5), polygon )
     225
     226        assert is_inside_polygon_quick( (0.5, 0.5), polygon )
     227        assert is_inside_polygon_quick( (1, -0.5), polygon )
     228        assert is_inside_polygon_quick( (1.5, 0), polygon )
     229
     230        assert not is_inside_polygon_quick( (0.5, 1.5), polygon )
     231        assert not is_inside_polygon_quick( (0.5, -0.5), polygon )
    203232
    204233        # Very convoluted polygon
     
    365394
    366395        polygon = [[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]]
    367         points = [[0.5, 1.4], [0.5, 0.5], [1, -0.5], [1.5, 0],
    368                   [0.5, 1.5], [0.5, -0.5]]
    369         res, count = separate_points_by_polygon(points, polygon)
    370 
    371         assert num.allclose( res, [1, 2, 3, 5, 4, 0] )
     396        points = [ [0.5, 1.4], [0.5, 0.5], [1, -0.5], [1.5, 0], [0.5, 1.5], [0.5, -0.5]]
     397        res, count = separate_points_by_polygon( points, polygon )
     398
     399        assert num.allclose( res, [1,2,3,5,4,0] )
    372400        assert count == 3
     401
     402
     403    def test_is_inside_triangle(self):
     404        # Simplest case:
     405        triangle = [[0, 0], [1, 0], [0.5, 1]]
     406
     407        assert is_inside_triangle((0.5, 0.5), triangle)
     408        assert is_inside_triangle((0.9, 0.1), triangle)
     409        assert not is_inside_triangle((0.5, 1.5), triangle)
     410        assert not is_inside_triangle((0.5, -0.5), triangle)
     411        assert not is_inside_triangle((-0.5, 0.5), triangle)
     412        assert not is_inside_triangle((1.5, 0.5), triangle)
     413
     414        # Try point on borders
     415        assert is_inside_triangle((0.5, 0), triangle, closed=True)
     416        assert is_inside_triangle((1, 0), triangle, closed=True)
     417
     418        assert not is_inside_triangle((0.5, 0), triangle, closed=False)
     419        assert not is_inside_triangle((1, 0), triangle, closed=False)
     420
     421        # Try vertices
     422        for P in triangle:
     423            assert is_inside_triangle(P, triangle, closed=True)
     424            assert not is_inside_triangle(P, triangle, closed=False)
     425
     426        # Slightly different
     427        triangle = [[0, 0.1], [1, -0.2], [0.5, 1]]
     428        assert is_inside_triangle((0.5, 0.5), triangle)
     429        assert is_inside_triangle((0.4, 0.1), triangle)
     430        assert not is_inside_triangle((1, 1), triangle)
     431
     432        # Try vertices
     433        for P in triangle:
     434            assert is_inside_triangle(P, triangle, closed=True)
     435            assert not is_inside_triangle(P, triangle, closed=False)
     436
    373437
    374438    def test_populate_polygon(self):
     
    379443        for point in points:
    380444            assert is_inside_polygon(point, polygon)
     445
     446            assert is_inside_polygon_quick(point, polygon)
     447
    381448
    382449        # Very convoluted polygon
     
    386453        for point in points:
    387454            assert is_inside_polygon(point, polygon)
     455            assert is_inside_polygon_quick(point, polygon)
     456
    388457
    389458    def test_populate_polygon_with_exclude(self):
     
    16861755        assert num.allclose(z, z_ref)
    16871756
    1688     def test_inside_polygon_badtest_1(self):
    1689         '''Test failure found in a larger test in shallow_water.
    1690         (test_get_maximum_inundation in test_data_manager.py (line 10574))
    1691        
    1692         Data below, when fed into:
    1693             indices = inside_polygon(points, polygon)
    1694         returns indices == [], and it shouldn't.
    1695 
    1696         However, it works fine here!?!?
    1697         '''
    1698 
    1699         polygon = [[50, 1], [99, 1], [99, 49], [50, 49]]
    1700 
    1701         points = [[  0.,  0.], [  0., 10.], [  0., 20.], [  0., 30.],
    1702                   [  0., 40.], [  0., 50.], [  5.,  0.], [  5., 10.],
    1703                   [  5., 20.], [  5., 30.], [  5., 40.], [  5., 50.],
    1704                   [ 10.,  0.], [ 10., 10.], [ 10., 20.], [ 10., 30.],
    1705                   [ 10., 40.], [ 10., 50.], [ 15.,  0.], [ 15., 10.],
    1706                   [ 15., 20.], [ 15., 30.], [ 15., 40.], [ 15., 50.],
    1707                   [ 20.,  0.], [ 20., 10.], [ 20., 20.], [ 20., 30.],
    1708                   [ 20., 40.], [ 20., 50.], [ 25.,  0.], [ 25., 10.],
    1709                   [ 25., 20.], [ 25., 30.], [ 25., 40.], [ 25., 50.],
    1710                   [ 30.,  0.], [ 30., 10.], [ 30., 20.], [ 30., 30.],
    1711                   [ 30., 40.], [ 30., 50.], [ 35.,  0.], [ 35., 10.],
    1712                   [ 35., 20.], [ 35., 30.], [ 35., 40.], [ 35., 50.],
    1713                   [ 40.,  0.], [ 40., 10.], [ 40., 20.], [ 40., 30.],
    1714                   [ 40., 40.], [ 40., 50.], [ 45.,  0.], [ 45., 10.],
    1715                   [ 45., 20.], [ 45., 30.], [ 45., 40.], [ 45., 50.],
    1716                   [ 50.,  0.], [ 50., 10.], [ 50., 20.], [ 50., 30.],
    1717                   [ 50., 40.], [ 50., 50.], [ 55.,  0.], [ 55., 10.],
    1718                   [ 55., 20.], [ 55., 30.], [ 55., 40.], [ 55., 50.],
    1719                   [ 60.,  0.], [ 60., 10.], [ 60., 20.], [ 60., 30.],
    1720                   [ 60., 40.], [ 60., 50.], [ 65.,  0.], [ 65., 10.],
    1721                   [ 65., 20.], [ 65., 30.], [ 65., 40.], [ 65., 50.],
    1722                   [ 70.,  0.], [ 70., 10.], [ 70., 20.], [ 70., 30.],
    1723                   [ 70., 40.], [ 70., 50.], [ 75.,  0.], [ 75., 10.],
    1724                   [ 75., 20.], [ 75., 30.], [ 75., 40.], [ 75., 50.],
    1725                   [ 80.,  0.], [ 80., 10.], [ 80., 20.], [ 80., 30.],
    1726                   [ 80., 40.], [ 80., 50.], [ 85.,  0.], [ 85., 10.],
    1727                   [ 85., 20.], [ 85., 30.], [ 85., 40.], [ 85., 50.],
    1728                   [ 90.,  0.], [ 90., 10.], [ 90., 20.], [ 90., 30.],
    1729                   [ 90., 40.], [ 90., 50.], [ 95.,  0.], [ 95., 10.],
    1730                   [ 95., 20.], [ 95., 30.], [ 95., 40.], [ 95., 50.],
    1731                   [100.,  0.], [100., 10.], [100., 20.], [100., 30.],
    1732                   [100., 40.], [100., 50.]]
    1733 
    1734         points = num.array(points)
    1735 
    1736         indices = inside_polygon(points, polygon)
    1737 
     1757
     1758    def test_is_inside_triangle_more(self):
     1759
     1760        res = is_inside_triangle([0.5, 0.5], [[ 0.5,  0. ],
     1761                                              [ 0.5,  0.5],
     1762                                              [ 0.,   0. ]])
     1763        assert res is True
     1764
     1765        res = is_inside_triangle([0.59999999999999998, 0.29999999999999999],
     1766                                 [[ 0.5,  0. ], [ 0.5, 0.5], [0., 0.]])
     1767        assert res is False
     1768
     1769        res = is_inside_triangle([0.59999999999999998, 0.29999999999999999],
     1770                                 [[1., 0.], [1., 0.5], [0.5, 0.]])
     1771        assert res is False
     1772
     1773
     1774        res = is_inside_triangle([0.59999999999999998, 0.29999999999999999],
     1775                                 [[0.5, 0.5], [0.5, 0.], [1., 0.5]])
     1776        assert res is True
     1777
     1778
     1779        res = is_inside_triangle([0.10000000000000001, 0.20000000000000001],
     1780                                 [[0.5, 0.], [0.5, 0.5], [0., 0.]])
     1781        assert res is False
     1782
     1783
     1784        res = is_inside_triangle([0.10000000000000001, 0.20000000000000001],
     1785                                 [[0., 0.5], [0., 0.], [0.5, 0.5]])
     1786        assert res is True
     1787
     1788        res = is_inside_triangle([0.69999999999999996, 0.69999999999999996],
     1789                                 [[0.5, 0.], [0.5, 0.5], [0., 0.]])
     1790        assert res is False
     1791
     1792        res = is_inside_triangle([0.59999999999999998, 0.29999999999999999],
     1793                                 [[0.25, 0.5], [0.25, 0.25], [0.5, 0.5]])
     1794        assert res is False
     1795
     1796
     1797        res = is_inside_triangle([10, 3],
     1798                                 [[0.1, 0.],
     1799                                  [0.1, 0.08333333],
     1800                                  [0., 0.]])
     1801        assert res is False
    17381802
    17391803################################################################################
Note: See TracChangeset for help on using the changeset viewer.