Changeset 5891 for anuga_core/source/anuga/utilities/test_polygon.py
- Timestamp:
- Nov 5, 2008, 4:19:05 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source/anuga/utilities/test_polygon.py
r5403 r5891 3 3 4 4 import unittest 5 from Numeric import zeros, array, allclose 5 import numpy 6 6 from math import sqrt, pi 7 7 from anuga.utilities.numerical_tools import ensure_numeric … … 43 43 p2 = [[0,0], [10,10], [15,5], [20, 10], [25,0], [30,10], [40,-10]] 44 44 45 f = Polygon_function( [(p1, 1.0)] )45 f = Polygon_function( [(p1, 1.0)], verbose=False ) 46 46 z = f([5, 5, 27, 35], [5, 9, 8, -5]) #Two first inside p1 47 assert allclose(z, [1,1,0,0])47 assert numpy.allclose(z, [1,1,0,0]) 48 48 49 49 50 50 f = Polygon_function( [(p2, 2.0)] ) 51 51 z = f([5, 5, 27, 35], [5, 9, 8, -5]) # First and last inside p2 52 assert allclose(z, [2,0,0,2])52 assert numpy.allclose(z, [2,0,0,2]) 53 53 54 54 … … 56 56 f = Polygon_function( [(p1, 1.0), (p2, 2.0)] ) 57 57 z = f([5, 5, 27, 35], [5, 9, 8, -5]) 58 assert allclose(z, [2,1,0,2]) 58 assert numpy.allclose(z, [2,1,0,2]) 59 60 61 def test_polygon_function_constants_tuple(self): 62 polygon = [[0,0], [10,0], [10,10], [0,10]] 63 points = ((5, 5), (5, 9), (27, 8), (35, -5)) 64 65 (indices, count) = separate_points_by_polygon(points, polygon, verbose=False) 59 66 60 67 def test_polygon_function_csvfile(self): … … 72 79 z = f([430000,480000], [490000,7720000]) # first outside, second inside 73 80 74 assert allclose(z, [0,10])81 assert numpy.allclose(z, [0,10]) 75 82 76 83 def test_polygon_function_georef(self): … … 90 97 z = f([5, 5, 27, 35], [5, 9, 8, -5]) #Two first inside p1 91 98 92 assert allclose(z, [1,1,0,0])99 assert numpy.allclose(z, [1,1,0,0]) 93 100 94 101 95 102 f = Polygon_function( [(p2, 2.0)], geo_reference=geo) 96 103 z = f([5, 5, 27, 35], [5, 9, 8, -5]) #First and last inside p2 97 assert allclose(z, [2,0,0,2])104 assert numpy.allclose(z, [2,0,0,2]) 98 105 99 106 … … 101 108 f = Polygon_function( [(p1, 1.0), (p2, 2.0)], geo_reference=geo) 102 109 z = f([5, 5, 27, 35], [5, 9, 8, -5]) 103 assert allclose(z, [2,1,0,2])110 assert numpy.allclose(z, [2,1,0,2]) 104 111 105 112 … … 107 114 f = Polygon_function( [(p1, 1.0), (p2, 2.0)]) 108 115 z = f([5, 5, 27, 35], [5, 9, 8, -5]) 109 assert not allclose(z, [2,1,0,2])116 assert not numpy.allclose(z, [2,1,0,2]) 110 117 111 118 … … 120 127 f = Polygon_function( [(p1, test_function)] ) 121 128 z = f([5, 5, 27, 35], [5, 9, 8, -5]) #Two first inside p1 122 assert allclose(z, [10,14,0,0])129 assert numpy.allclose(z, [10,14,0,0]) 123 130 124 131 # Combined 125 132 f = Polygon_function( [(p1, test_function), (p2, 2.0)] ) 126 133 z = f([5, 5, 27, 35], [5, 9, 8, -5]) 127 assert allclose(z, [2,14,0,2])134 assert numpy.allclose(z, [2,14,0,2]) 128 135 129 136 … … 131 138 f = Polygon_function( [(p1, test_function), (p2, 2.0)], default = 3.14) 132 139 z = f([5, 5, 27, 35], [5, 9, 8, -5]) 133 assert allclose(z, [2,14,3.14,2])140 assert numpy.allclose(z, [2,14,3.14,2]) 134 141 135 142 … … 138 145 default = test_function) 139 146 z = f([5, 5, 27, 35], [5, 9, 8, -5]) 140 assert allclose(z, [2,14,35,2])147 assert numpy.allclose(z, [2,14,35,2]) 141 148 142 149 … … 210 217 res = inside_polygon(points, polygon) 211 218 assert len(res) == 2 212 assert allclose(res, [0,1])219 assert numpy.allclose(res, [0,1]) ## alltrue? 213 220 214 221 … … 305 312 res = inside_polygon( points, polygon, verbose=False ) 306 313 307 assert allclose( res, [0,1,2] )314 assert numpy.allclose( res, [0,1,2] ) 308 315 309 316 def test_outside_polygon(self): … … 317 324 318 325 indices = outside_polygon( [[0.5, 0.5], [1, -0.5], [0.3, 0.2]], U ) 319 assert allclose( indices, [1] )326 assert numpy.allclose( indices, [1] ) 320 327 321 328 # One more test of vector formulation returning indices … … 324 331 res = outside_polygon( points, polygon ) 325 332 326 assert allclose( res, [3, 4] )333 assert numpy.allclose( res, [3, 4] ) 327 334 328 335 … … 332 339 res = outside_polygon( points, polygon ) 333 340 334 assert allclose( res, [0, 4, 5] )341 assert numpy.allclose( res, [0, 4, 5] ) 335 342 336 343 def test_outside_polygon2(self): … … 355 362 #print indices, count 356 363 assert count == 0 #None inside 357 assert allclose(indices, [3,2,1,0])364 assert numpy.allclose(indices, [3,2,1,0]) 358 365 359 366 indices = outside_polygon(points, U, closed = True) 360 assert allclose(indices, [0,1,2,3])367 assert numpy.allclose(indices, [0,1,2,3]) 361 368 362 369 indices = inside_polygon(points, U, closed = True) 363 assert allclose(indices, [])370 assert numpy.allclose(indices, []) 364 371 365 372 … … 375 382 indices, count = separate_points_by_polygon(points, U) 376 383 assert count == 3 #All inside 377 assert allclose(indices, [0,1,2])384 assert numpy.allclose(indices, [0,1,2]) 378 385 379 386 indices = outside_polygon(points, U, closed = True) 380 assert allclose(indices, [])387 assert numpy.allclose(indices, []) 381 388 382 389 indices = inside_polygon(points, U, closed = True) 383 assert allclose(indices, [0,1,2])390 assert numpy.allclose(indices, [0,1,2]) 384 391 385 392 … … 388 395 389 396 indices, count = separate_points_by_polygon( [[0.5, 0.5], [1, -0.5], [0.3, 0.2]], U ) 390 assert allclose( indices, [0,2,1] )397 assert numpy.allclose( indices, [0,2,1] ) 391 398 assert count == 2 392 399 … … 396 403 res, count = separate_points_by_polygon( points, polygon ) 397 404 398 assert allclose( res, [0,1,2,4,3] )405 assert numpy.allclose( res, [0,1,2,4,3] ) 399 406 assert count == 3 400 407 … … 404 411 res, count = separate_points_by_polygon( points, polygon ) 405 412 406 assert allclose( res, [1,2,3,5,4,0] )413 assert numpy.allclose( res, [1,2,3,5,4,0] ) 407 414 assert count == 3 408 415 … … 588 595 status, value = intersection(line0, line1) 589 596 assert status == 1 590 assert allclose(value, [0.0, 0.0])597 assert numpy.allclose(value, [0.0, 0.0]) 591 598 592 599 def test_intersection2(self): … … 596 603 status, value = intersection(line0, line1) 597 604 assert status == 1 598 assert allclose(value, [12.0, 6.0])605 assert numpy.allclose(value, [12.0, 6.0]) 599 606 600 607 # Swap direction of one line … … 603 610 status, value = intersection(line0, line1) 604 611 assert status == 1 605 assert allclose(value, [12.0, 6.0])612 assert numpy.allclose(value, [12.0, 6.0]) 606 613 607 614 # Swap order of lines 608 615 status, value = intersection(line1, line0) 609 616 assert status == 1 610 assert allclose(value, [12.0, 6.0])617 assert numpy.allclose(value, [12.0, 6.0]) 611 618 612 619 def test_intersection3(self): … … 616 623 status, value = intersection(line0, line1) 617 624 assert status == 1 618 assert allclose(value, [14.068965517, 7.0344827586])625 assert numpy.allclose(value, [14.068965517, 7.0344827586]) 619 626 620 627 # Swap direction of one line … … 623 630 status, value = intersection(line0, line1) 624 631 assert status == 1 625 assert allclose(value, [14.068965517, 7.0344827586])632 assert numpy.allclose(value, [14.068965517, 7.0344827586]) 626 633 627 634 # Swap order of lines 628 635 status, value = intersection(line1, line0) 629 636 assert status == 1 630 assert allclose(value, [14.068965517, 7.0344827586])637 assert numpy.allclose(value, [14.068965517, 7.0344827586]) 631 638 632 639 … … 641 648 status, value = intersection(line0, line1) 642 649 assert status == 1 643 assert allclose(value, [1.0, 1.0])650 assert numpy.allclose(value, [1.0, 1.0]) 644 651 645 652 … … 649 656 status, value = intersection(line0, line1) 650 657 assert status == 1 651 assert allclose(value, [1.0, 1.0])658 assert numpy.allclose(value, [1.0, 1.0]) 652 659 653 660 … … 679 686 common_end_point[1]) 680 687 msg += ' gave %s, \nbut when reversed we got %s' %(p1, p2) 681 assert allclose(p1, p2), msg688 assert numpy.allclose(p1, p2), msg 682 689 683 690 # Swap order of lines … … 685 692 assert status == 1 686 693 msg = 'Order of lines gave different results' 687 assert allclose(p1, p3), msg694 assert numpy.allclose(p1, p3), msg 688 695 689 696 … … 725 732 status, value = intersection(line0, line1) 726 733 assert status == 2 727 assert allclose(value, [[0,0], [3,0]])734 assert numpy.allclose(value, [[0,0], [3,0]]) 728 735 729 736 # Overlap 2 … … 733 740 status, value = intersection(line0, line1) 734 741 assert status == 2 735 assert allclose(value, [[-3, 0], [5,0]])742 assert numpy.allclose(value, [[-3, 0], [5,0]]) 736 743 737 744 # Inclusion 1 … … 741 748 status, value = intersection(line0, line1) 742 749 assert status == 2 743 assert allclose(value, line1)750 assert numpy.allclose(value, line1) 744 751 745 752 # Inclusion 2 … … 749 756 status, value = intersection(line0, line1) 750 757 assert status == 2 751 assert allclose(value, line0)758 assert numpy.allclose(value, line0) 752 759 753 760 … … 768 775 status, value = intersection(line0, line1) 769 776 assert status == 2 770 assert allclose(value, [[1, 7], [7, 19]])777 assert numpy.allclose(value, [[1, 7], [7, 19]]) 771 778 772 779 status, value = intersection(line1, line0) 773 780 assert status == 2 774 assert allclose(value, [[1, 7], [7, 19]])781 assert numpy.allclose(value, [[1, 7], [7, 19]]) 775 782 776 783 # Swap direction … … 779 786 status, value = intersection(line0, line1) 780 787 assert status == 2 781 assert allclose(value, [[7, 19], [1, 7]])788 assert numpy.allclose(value, [[7, 19], [1, 7]]) 782 789 783 790 line0 = [[0,5], [7,19]] … … 785 792 status, value = intersection(line0, line1) 786 793 assert status == 2 787 assert allclose(value, [[1, 7], [7, 19]])794 assert numpy.allclose(value, [[1, 7], [7, 19]]) 788 795 789 796 … … 793 800 status, value = intersection(line0, line1) 794 801 assert status == 2 795 assert allclose(value, [[1,7], [7, 19]])802 assert numpy.allclose(value, [[1,7], [7, 19]]) 796 803 797 804 line0 = [[0,5], [10,25]] … … 799 806 status, value = intersection(line0, line1) 800 807 assert status == 2 801 assert allclose(value, [[1,7], [7, 19]])808 assert numpy.allclose(value, [[1,7], [7, 19]]) 802 809 803 810 … … 806 813 status, value = intersection(line0, line1) 807 814 assert status == 2 808 assert allclose(value, [[7, 19], [1, 7]])815 assert numpy.allclose(value, [[7, 19], [1, 7]]) 809 816 810 817 … … 843 850 res = inside_polygon(points, polygon) 844 851 assert len(res) == 2 845 assert allclose(res, [0,1])852 assert numpy.allclose(res, [0,1]) 846 853 847 854 def test_polygon_area(self): … … 990 997 if __name__ == "__main__": 991 998 suite = unittest.makeSuite(Test_Polygon,'test') 992 #suite = unittest.makeSuite(Test_Polygon,'test_inside_polygon_geo_ref')999 ## suite = unittest.makeSuite(Test_Polygon,'test_polygon_function_constantsX') 993 1000 runner = unittest.TextTestRunner() 994 1001 runner.run(suite)
Note: See TracChangeset
for help on using the changeset viewer.