source: anuga_core/source/anuga/pmesh/mesh_interface.py @ 3616

Last change on this file since 3616 was 3616, checked in by ole, 18 years ago

Got Karratha running again.

File size: 6.3 KB
Line 
1
2#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3# Assume that the root AnuGA dir (inundation) is included in your pythonpath
4#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5
6from anuga.coordinate_transforms.geo_reference import Geo_reference,DEFAULT_ZONE
7from anuga.utilities.polygon import  point_in_polygon ,populate_polygon
8from anuga.utilities.numerical_tools import ensure_numeric
9from Numeric import Float
10from anuga.utilities.polygon import inside_polygon
11
12# This is due to pmesh being a package and a module and
13# the current dir being unknown
14try:
15    from anuga.pmesh.mesh import Mesh
16except ImportError: 
17    from mesh import Mesh
18
19import exceptions
20class PolygonError(exceptions.Exception): pass
21
22def create_mesh_from_regions(bounding_polygon,
23                             boundary_tags,
24                             maximum_triangle_area=None,
25                             filename=None,
26                             interior_regions=None,
27                             poly_geo_reference=None,
28                             mesh_geo_reference=None,
29                             minimum_triangle_angle=28.0,
30                             verbose=True):
31    """Create mesh from bounding polygons, and resolutions.
32
33    bounding_polygon is a list of points in Eastings and Northings,
34    relative to the poly_geo_reference.
35
36    Boundary tags is a dictionary of symbolic tags. For every tag there
37    is a list of indices referring to segments associated with that tag
38
39    maximum_triangle_area is the maximal area per triangle
40    for the bounding polygon, excluding the  interior regions.
41
42    Interior_regions is a list of tuples consisting of (polygon, resolution)
43    for each region to be separately refined.
44
45    This function does not allow segments to share points - use underlying
46    pmesh functionality for that
47
48    poly_geo_reference is the geo_reference of the bounding polygon and
49    the interior polygons.
50    If none, assume absolute.  Please pass one though, since absolute
51    references have a zone.
52   
53    mesh_geo_reference is the geo_reference of the mesh to be created.
54    If none is given one will be automatically generated.  It was use
55    the lower left hand corner of  bounding_polygon (absolute)
56    as the x and y values for the geo_ref.
57   
58    Returns the mesh instance if no finename is given
59
60    Note, interior regions should be fully nested, as overlaps may cause
61    unintended resolutions.
62   
63    """
64    #FIXME (OLE-DSG)
65    # check the segment indexes - throw an error if they are out of bounds
66    #(DSG) Yes!
67
68   
69    #In addition I reckon the polygons could be of class Geospatial_data
70    #(DSG) Yes!
71
72    # First check that interior polygons are fully contained in bounding
73    # polygon
74    #Note, Both poly's have the same geo_ref, therefore don't take into account
75    # geo_ref
76
77    # Simple check
78    bounding_polygon = ensure_numeric(bounding_polygon, Float)
79    msg = 'Bounding polygon must be a list of points or an Nx2 array'
80    assert len(bounding_polygon.shape) == 2, msg
81    assert bounding_polygon.shape[1] == 2, msg   
82
83
84    #
85    if interior_regions is not None:       
86        # Test that all the interior polygons are inside the bounding_poly
87        for interior_polygon, res in interior_regions:
88            indices = inside_polygon(interior_polygon, bounding_polygon,
89                                     closed = True, verbose = False)
90   
91            if len(indices) <> len(interior_polygon): 
92                msg = 'Interior polygon %s is outside bounding polygon: %s'\
93                      %(str(interior_polygon), str(bounding_polygon))
94                raise PolygonError, msg
95
96
97
98    # Resolve geo referencing       
99    if mesh_geo_reference is None:
100        xllcorner = min(bounding_polygon[:,0])
101        yllcorner = min(bounding_polygon[:,1])   
102        #
103        if poly_geo_reference is None:
104            zone = DEFAULT_ZONE
105        else:
106            zone = poly_geo_reference.get_zone()
107            [(xllcorner,yllcorner)] = poly_geo_reference.get_absolute( \
108            [(xllcorner,yllcorner)])
109        # create a geo_ref, based on the llc of the bounding_polygon
110        mesh_geo_reference = Geo_reference(xllcorner = xllcorner,
111                                           yllcorner = yllcorner,
112                                           zone = zone)
113
114    m = Mesh(geo_reference=mesh_geo_reference)
115
116    # Do bounding polygon
117    m.add_region_from_polygon(bounding_polygon,
118                              tags=boundary_tags,
119                              geo_reference=poly_geo_reference)
120
121    # Find one point inside region automatically
122    if interior_regions is not None:
123        excluded_polygons = []       
124        for polygon, res in interior_regions:
125            excluded_polygons.append( polygon )
126    else:
127        excluded_polygons = None
128
129    # Convert bounding poly to absolute values
130    # this sort of thing can be fixed with the geo_points class
131    if poly_geo_reference is not None:
132        bounding_polygon_absolute = \
133            poly_geo_reference.get_absolute(bounding_polygon)
134    else:
135        bounding_polygon_absolute = bounding_polygon
136   
137    inner_point = point_in_polygon(bounding_polygon_absolute)
138    inner = m.add_region(inner_point[0], inner_point[1])
139    inner.setMaxArea(maximum_triangle_area)
140
141    # Do interior regions
142    if interior_regions is not None:   
143        for polygon, res in interior_regions:
144            m.add_region_from_polygon(polygon,
145                                      geo_reference=poly_geo_reference)
146            # convert bounding poly to absolute values
147            if poly_geo_reference is not None:
148                polygon_absolute = \
149                    poly_geo_reference.get_absolute(polygon)
150            else:
151                polygon_absolute = polygon
152            inner_point = point_in_polygon(polygon_absolute)
153            region = m.add_region(inner_point[0], inner_point[1])
154            region.setMaxArea(res)
155           
156
157    if filename is None:
158        return m
159    else:
160        m.generate_mesh(minimum_triangle_angle=minimum_triangle_angle,
161                             verbose=verbose)
162        m.export_mesh_file(filename)
163
164
Note: See TracBrowser for help on using the repository browser.