source: anuga_core/source/anuga/mesh_engine/mesh_engine.py @ 4449

Last change on this file since 4449 was 4449, checked in by duncan, 17 years ago

Getting triangle to automatically remove lone vertices

File size: 4.7 KB
Line 
1#!/usr/bin/env python
2
3import sys
4
5from types import ListType, TupleType
6
7import anuga.mesh_engine.mesh_engine_c_layer as triang
8
9from Numeric import array, Float, Int32
10
11from anuga.utilities.numerical_tools import ensure_numeric
12from anuga.utilities.anuga_exceptions import ANUGAError
13   
14def generate_mesh(points=None,
15                  segments=None,holes=None,regions=None,
16                  pointatts=None,segatts=None,
17                  mode=None, dummy_test=None):
18    """
19   
20    """
21    #FIXME (DSG-DSG): Catch parameters that are lists,
22    #instead of lists of lists
23    # check shape[1] is 2 etc
24
25    if points is None:
26        points = []
27
28    if segments is None:
29        segments = []
30
31    if holes is None:
32        holes = []
33       
34    if regions is None:
35        regions = []
36
37    if dummy_test is None:
38        dummy_test  = []
39       
40    try:
41        points =  ensure_numeric(points, Float)
42    except ValueError:
43        msg = 'ERROR: Inconsistent points array.'
44        raise ANUGAError, msg
45    if points.shape[1] <>2:
46        msg = 'ERROR: Bad shape points array.'
47        raise ANUGAError, msg
48
49    #print "pointatts",pointatts
50    # This is after points is numeric
51    if pointatts is None or pointatts == []:
52        pointatts = [[] for x in range(points.shape[0])]
53       
54    try:
55        # If Int is used, instead of Int32, it fails in Linux
56        segments = ensure_numeric(segments, Int32)
57    except ValueError:
58        msg = 'ERROR: Inconsistent segments array.'
59        raise ANUGAError, msg
60   
61    # This is after segments is numeric
62    if segatts is None or segatts == []:
63        segatts = [0 for x in range(segments.shape[0])]
64       
65    try:
66        holes = ensure_numeric(holes, Float)
67    except ValueError:
68        msg = 'ERROR: Inconsistent holess array.'
69        raise ANUGAError, msg
70
71   
72    regions = add_area_tag(regions)
73    try:
74        regions = ensure_numeric(regions, Float)
75    except  (ValueError, TypeError):
76        msg = 'ERROR: Inconsistent regions array.'
77        raise ANUGAError, msg
78       
79    if not regions.shape[0] == 0 and regions.shape[1] <= 2:
80        msg = 'ERROR: Bad shape points array.'
81        raise ANUGAError, msg
82   
83    try:
84        #print "pointatts",pointatts
85        pointatts = ensure_numeric(pointatts, Float)
86    except (ValueError, TypeError):
87        msg = 'ERROR: Inconsistent point attributes array.'
88        raise ANUGAError, msg
89
90    if pointatts.shape[0] <> points.shape[0]:
91        msg = """ERROR: Point attributes array not the same shape as
92        point array."""
93        raise ANUGAError, msg
94   
95    try:
96        segatts = ensure_numeric(segatts, Int32)
97    except ValueError:
98        msg = 'ERROR: Inconsistent point attributes array.'
99        raise ANUGAError, msg
100    if segatts.shape[0] <> segments.shape[0]:
101        msg = """ERROR: Segment attributes array not the same shape as
102        segment array."""
103        raise ANUGAError, msg
104
105   
106    #print "mode", mode
107    if mode.find('n'):
108        mode = 'j' + mode
109        # j- Jettisons vertices that are not part of the final
110        #    triangulation from the output .node file (including duplicate
111        #    input vertices and vertices ``eaten'' by holes).  - output a
112        #    list of neighboring triangles
113           
114    #print "points",points
115    #print "segments", segments
116    #print "segments.shape", segments.shape
117    #print "holes", holes
118    #print "regions", regions
119    #print "pointatts", pointatts
120    #print "segatts", segatts
121    #print "mode", mode
122    return triang.genMesh(points,segments,holes,regions,
123                          pointatts,segatts, mode, segments.flat)
124
125def add_area_tag(regions):
126    """
127    So, what is the format?
128    A list with
129    [x,y,region_tag,area] OR [x,y,region_tag]
130    if it's [x,y,region_tag], add a 4th element, value of 0.0.
131    """
132    if isinstance(regions, ListType):
133        for i, region in enumerate(regions):
134            if len(region) == 3:
135                if isinstance(region, TupleType):
136                    #FIXME: How do you convert a tuple to a list?
137                    # I can do it a stupid way..
138                    tuple = region[:]
139                    regions[i] = []
140                    for j in tuple:
141                        regions[i].append(j)
142                    regions[i].append(0.0)
143                else:
144                    regions[i].append(0.0)
145                   
146                # let ensure numeric catch this..   
147                #len(region) <= 2:
148                #msg = 'ERROR: Inconsistent regions array.'
149                #raise msg
150            #elif
151    return regions
152
153if __name__ == "__main__":
154    pass 
Note: See TracBrowser for help on using the repository browser.