Changeset 7686


Ignore:
Timestamp:
Apr 15, 2010, 8:45:36 PM (10 years ago)
Author:
hudson
Message:

Tickets 346/257: Added a simple brute-force is_complex function to find complex (ie pathological/self intersecting) polygons.
Added unit test to test above.

Location:
anuga_core/source/anuga/utilities
Files:
2 edited

Legend:

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

    r7553 r7686  
    322322    return False
    323323
     324def is_complex(polygon):
     325    """Check if a polygon is complex (self-intersecting)
     326    """
     327   
     328    polygon = ensure_numeric(polygon, num.float)
     329
     330    for i in range(0, len(polygon)-1):
     331        for j in range(i+1, len(polygon)-1):   
     332                (type, point) = intersection([polygon[i], polygon[i+1]], [polygon[j], polygon[j+1]])
     333
     334                if (abs(i-j) > 1 and type == 1) or (type == 2 and list(point[0]) != list(point[1])) or (type == 3) and type != 4:
     335#                    print 'self-intersecting polygon, type ', type, ' point', point, 'vertex indices ', i, j               
     336                    return True
     337       
     338    return False
     339   
    324340def is_inside_polygon_quick(point, polygon, closed=True, verbose=False):
    325341    """Determine if one point is inside a polygon
     
    978994        fields = line.split(delimiter)
    979995        polygon.append([float(fields[0]), float(fields[1])])
    980 
     996   
     997    # check this is a valid polygon   
     998    # JAMES: don't do this check yet, it's too slow, and there is already pathological data in the unit tests.
     999    # if is_complex(polygon):   
     1000            # msg = 'Self-intersecting polygon detected in file' + filename +'. '
     1001            # msg += 'Please fix.'
     1002            # raise Exception, msg
     1003   
    9811004    return polygon
    9821005
  • anuga_core/source/anuga/utilities/test_polygon.py

    r7308 r7686  
    18061806        assert res is False
    18071807
     1808    def test_is_polygon_complex(self):
     1809        concave_poly = [[0, 0], [10, 0], [5, 5], [10, 10], [0, 10]]
     1810        complex_poly = [[0, 0], [10, 0], [5, 5], [5, 15], [5, 7], [10, 10], [0, 10]]
     1811       
     1812        assert not is_complex(concave_poly)
     1813        assert is_complex(complex_poly)
     1814       
     1815       
    18081816################################################################################
    18091817
Note: See TracChangeset for help on using the changeset viewer.