Changeset 7866 for trunk/anuga_core/source/anuga/geometry/test_polygon.py
 Timestamp:
 Jun 22, 2010, 12:04:32 PM (13 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/anuga_core/source/anuga/geometry/test_polygon.py
r7858 r7866 1 1 #!/usr/bin/env python 2 3 """ Test suite to test polygon functionality. """ 2 4 3 5 import unittest 4 6 import numpy as num 5 from math import sqrt, pi6 7 from anuga.utilities.numerical_tools import ensure_numeric 7 8 from anuga.utilities.system_tools import get_pathname_from_package 8 9 9 from polygon import * 10 from polygon import _poly_xy 10 from polygon import _poly_xy, separate_points_by_polygon, \ 11 populate_polygon, polygon_area, is_inside_polygon, \ 12 read_polygon, point_on_line, point_in_polygon, \ 13 plot_polygons, outside_polygon, is_outside_polygon, \ 14 intersection, is_complex, is_inside_triangle, \ 15 interpolate_polyline, inside_polygon, in_and_outside_polygon 16 11 17 from polygon_function import Polygon_function 12 18 from anuga.coordinate_transforms.geo_reference import Geo_reference … … 401 407 402 408 polygon = [[0,0], [1,0], [0.5,1], [2, 1], [2,1], [0,1]] 403 points = [ [0.5, 1.4], [0.5, 0.5], [1, 0.5], [1.5, 0], [0.5, 1.5], [0.5, 0.5]] 409 points = [ [0.5, 1.4], [0.5, 0.5], [1, 0.5], [1.5, 0], [0.5, 1.5], \ 410 [0.5, 0.5]] 404 411 res, count = separate_points_by_polygon( points, polygon ) 405 412 … … 1401 1408 1402 1409 def test_no_intersection(self): 1410 """ Test 2 nontouching lines don't intersect. """ 1403 1411 line0 = [[1,1], [1,1]] 1404 1412 line1 = [[0,1], [0,0]] … … 1469 1477 1470 1478 # Overlap 1471 line0 = [[0, 5], [7,19]]1472 line1 = [[1, 7], [10,25]]1479 line0 = [[0, 5], [7, 19]] 1480 line1 = [[1, 7], [10, 25]] 1473 1481 status, value = intersection(line0, line1) 1474 1482 assert status == 2 … … 1512 1520 1513 1521 1514 def NOtest_inside_polygon_main(self): 1515 # FIXME (Ole): Why is this disabled? 1516 #print "inside",inside 1517 #print "outside",outside 1518 1519 assert not inside_polygon((0.5, 1.5), polygon) 1520 assert not inside_polygon((0.5, 0.5), polygon) 1521 assert not inside_polygon((0.5, 0.5), polygon) 1522 assert not inside_polygon((1.5, 0.5), polygon) 1523 1524 # Try point on borders 1525 assert inside_polygon((1., 0.5), polygon, closed=True) 1526 assert inside_polygon((0.5, 1), polygon, closed=True) 1527 assert inside_polygon((0., 0.5), polygon, closed=True) 1528 assert inside_polygon((0.5, 0.), polygon, closed=True) 1529 1530 assert not inside_polygon((0.5, 1), polygon, closed=False) 1531 assert not inside_polygon((0., 0.5), polygon, closed=False) 1532 assert not inside_polygon((0.5, 0.), polygon, closed=False) 1533 assert not inside_polygon((1., 0.5), polygon, closed=False) 1534 1522 def test_inside_polygon_main(self): 1535 1523 # From real example (that failed) 1536 1524 polygon = [[20,20], [40,20], [40,40], [20,40]] … … 1546 1534 1547 1535 def test_polygon_area(self): 1536 """ Test getting the area of a polygon. """ 1548 1537 # Simplest case: Polygon is the unit square 1549 1538 polygon = [[0,0], [1,0], [1,1], [0,1]] … … 1611 1600 # Another case 1612 1601 polygon3 = [[1,5], [10,1], [100,10], [50,10], [3,6]] 1613 v = plot_polygons([polygon2,polygon3], figname='test2')1602 plot_polygons([polygon2, polygon3], figname='test2') 1614 1603 1615 1604 for file in ['test1.png', 'test2.png']: … … 1619 1608 1620 1609 def test_inside_polygon_geospatial(self): 1610 """ Test geospatial coords inside poly. """ 1621 1611 #Simplest case: Polygon is the unit square 1622 polygon_absolute = [[0, 0], [1,0], [1,1], [0,1]]1612 polygon_absolute = [[0, 0], [1, 0], [1, 1], [0, 1]] 1623 1613 poly_geo_ref = Geo_reference(57, 100, 100) 1624 1614 polygon = poly_geo_ref.change_points_geo_ref(polygon_absolute) … … 1639 1629 assert is_inside_polygon(points_absolute, polygon_absolute) 1640 1630 1641 def NOtest_decimate_polygon(self): 1631 def test_decimate_polygon(self): 1632 from polygon import decimate_polygon 1642 1633 polygon = [[0,0], [10,10], [15,5], [20, 10], 1643 1634 [25,0], [30,10], [40,10], [35, 5]] … … 1645 1636 dpoly = decimate_polygon(polygon, factor=2) 1646 1637 1647 print dpoly1648 1649 1638 assert len(dpoly)*2==len(polygon) 1650 1639 1651 minx = maxx = polygon[0][0]1652 miny = maxy = polygon[0][1]1653 for point in polygon[1:]:1654 x, y = point1655 1656 if x < minx: minx = x1657 if x > maxx: maxx = x1658 if y < miny: miny = y1659 if y > maxy: maxy = y1660 1661 assert [minx, miny] in polygon1662 print minx, maxy1663 assert [minx, maxy] in polygon1664 assert [maxx, miny] in polygon1665 assert [maxx, maxy] in polygon1666 1640 1667 1641 def test_interpolate_polyline(self): … … 1756 1730 1757 1731 def test_is_inside_triangle_more(self): 1758 1732 """ Test if points inside triangles are detected correctly. """ 1759 1733 res = is_inside_triangle([0.5, 0.5], [[ 0.5, 0. ], 1760 1734 [ 0.5, 0.5], … … 1802 1776 1803 1777 def test_is_polygon_complex(self): 1804 """ Test a concave and a complex poly with is_complex, to make sure it can detect1805 selfintersection.1778 """ Test a concave and a complex poly with is_complex, to make 1779 sure it can detect selfintersection. 1806 1780 """ 1807 1781 concave_poly = [[0, 0], [10, 0], [5, 5], [10, 10], [0, 10]] 1808 complex_poly = [[0, 0], [10, 0], [5, 5], [4, 15], [5, 7], [10, 10], [0, 10]] 1782 complex_poly = [[0, 0], [10, 0], [5, 5], [4, 15], [5, 7], [10, 10], \ 1783 [0, 10]] 1809 1784 1810 1785 assert not is_complex(concave_poly) … … 1812 1787 1813 1788 def test_is_polygon_complex2(self): 1814 """Test a concave and a complex poly with is_complex, to make sure it can detect 1815 selfintersection. This test uses more complicated polygons. 1789 """ Test a concave and a complex poly with is_complex, to make sure it 1790 can detect selfintersection. This test uses more complicated 1791 polygons. 1816 1792 """ 1817 concave_poly = [[0, 0], [10, 0], [11,0], [5, 5], [7,6], [10, 10], [1,5], [0, 10]] 1818 complex_poly = [[0, 0], [12,12], [10, 0], [5, 5], [3,18], [4, 15], [5, 7], [10, 10], [0, 10], [16, 12]] 1793 concave_poly = [[0, 0], [10, 0], [11,0], [5, 5], [7, 6], [10, 10], \ 1794 [1, 5], [0, 10]] 1795 complex_poly = [[0, 0], [12, 12], [10, 0], [5, 5], [3,18], [4, 15], \ 1796 [5, 7], [10, 10], [0, 10], [16, 12]] 1819 1797 1820 1798 assert not is_complex(concave_poly)
Note: See TracChangeset
for help on using the changeset viewer.