Ignore:
Timestamp:
Mar 17, 2009, 5:44:57 PM (15 years ago)
Author:
ole
Message:

Added new function is_inside_triangle. This helps the fitting functions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/utilities/test_polygon.py

    r6215 r6534  
    195195
    196196
     197    def test_is_inside_polygon_quick(self):
     198        """test_is_inside_polygon_quick
     199       
     200        Test fast version of of is_inside_polygon
     201        """
     202
     203        # Simplest case: Polygon is the unit square
     204        polygon = [[0,0], [1,0], [1,1], [0,1]]
     205
     206        assert is_inside_polygon_quick( (0.5, 0.5), polygon )
     207        assert not is_inside_polygon_quick( (0.5, 1.5), polygon )
     208        assert not is_inside_polygon_quick( (0.5, -0.5), polygon )
     209        assert not is_inside_polygon_quick( (-0.5, 0.5), polygon )
     210        assert not is_inside_polygon_quick( (1.5, 0.5), polygon )
     211
     212        # Try point on borders
     213        assert is_inside_polygon_quick( (1., 0.5), polygon, closed=True)
     214        assert is_inside_polygon_quick( (0.5, 1), polygon, closed=True)
     215        assert is_inside_polygon_quick( (0., 0.5), polygon, closed=True)
     216        assert is_inside_polygon_quick( (0.5, 0.), polygon, closed=True)
     217
     218        assert not is_inside_polygon_quick( (0.5, 1), polygon, closed=False)
     219        assert not is_inside_polygon_quick( (0., 0.5), polygon, closed=False)
     220        assert not is_inside_polygon_quick( (0.5, 0.), polygon, closed=False)
     221        assert not is_inside_polygon_quick( (1., 0.5), polygon, closed=False)
     222
     223
     224       
     225       
    197226    def test_inside_polygon_main(self):
    198227
     
    223252        assert not is_inside_polygon( (0.5, -0.5), polygon )
    224253
     254       
     255        assert is_inside_polygon_quick( (0.5, 0.5), polygon )
     256        assert is_inside_polygon_quick( (1, -0.5), polygon )
     257        assert is_inside_polygon_quick( (1.5, 0), polygon )
     258       
     259        assert not is_inside_polygon_quick( (0.5, 1.5), polygon )
     260        assert not is_inside_polygon_quick( (0.5, -0.5), polygon )               
    225261
    226262        # Very convoluted polygon
     
    406442        assert num.allclose( res, [1,2,3,5,4,0] )       
    407443        assert count == 3
    408        
     444
     445       
     446       
     447    def test_is_inside_triangle(self):
     448
     449
     450        # Simplest case:
     451        triangle = [[0, 0], [1, 0], [0.5, 1]]       
     452
     453        assert is_inside_triangle((0.5, 0.5), triangle)
     454        assert is_inside_triangle((0.9, 0.1), triangle)       
     455        assert not is_inside_triangle((0.5, 1.5), triangle)
     456        assert not is_inside_triangle((0.5, -0.5), triangle)
     457        assert not is_inside_triangle((-0.5, 0.5), triangle)
     458        assert not is_inside_triangle((1.5, 0.5), triangle)
     459
     460        # Try point on borders
     461        assert is_inside_triangle((0.5, 0), triangle, closed=True)
     462        assert is_inside_triangle((1, 0), triangle, closed=True)
     463
     464        assert not is_inside_triangle((0.5, 0), triangle, closed=False)
     465        assert not is_inside_triangle((1, 0), triangle, closed=False)       
     466
     467        # Try vertices
     468        for P in triangle:
     469            assert is_inside_triangle(P, triangle, closed=True)           
     470            assert not is_inside_triangle(P, triangle, closed=False)                       
     471       
     472       
     473        # Slightly different
     474        triangle = [[0, 0.1], [1, -0.2], [0.5, 1]]       
     475        assert is_inside_triangle((0.5, 0.5), triangle)                       
     476        assert is_inside_triangle((0.4, 0.1), triangle)                               
     477        assert not is_inside_triangle((1, 1), triangle)                               
     478       
     479        # Try vertices
     480        for P in triangle:
     481            assert is_inside_triangle(P, triangle, closed=True)           
     482            assert not is_inside_triangle(P, triangle, closed=False)                               
     483           
     484           
     485           
    409486
    410487    def test_populate_polygon(self):
     
    416493        for point in points:
    417494            assert is_inside_polygon(point, polygon)
    418 
    419 
    420         #Very convoluted polygon
     495            assert is_inside_polygon_quick(point, polygon)           
     496
     497
     498        # Very convoluted polygon
    421499        polygon = [[0,0], [10,10], [15,5], [20, 10], [25,0], [30,10], [40,-10]]
    422500
     
    426504        for point in points:
    427505            assert is_inside_polygon(point, polygon)
     506            assert is_inside_polygon_quick(point, polygon)                       
    428507
    429508
     
    17821861#-------------------------------------------------------------
    17831862if __name__ == "__main__":
    1784     suite = unittest.makeSuite(Test_Polygon,'test')
     1863    suite = unittest.makeSuite(Test_Polygon, 'test')
    17851864    runner = unittest.TextTestRunner()
    17861865    runner.run(suite)
Note: See TracChangeset for help on using the changeset viewer.