1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | import sys |
---|
4 | |
---|
5 | from types import ListType, TupleType |
---|
6 | |
---|
7 | import anuga.mesh_engine.mesh_engine_c_layer as triang |
---|
8 | |
---|
9 | from Numeric import array, Float, Int32 |
---|
10 | |
---|
11 | from anuga.utilities.numerical_tools import ensure_numeric |
---|
12 | from anuga.utilities.anuga_exceptions import ANUGAError |
---|
13 | |
---|
14 | def 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 | #pass |
---|
109 | mode = 'j' + mode |
---|
110 | # j- Jettisons vertices that are not part of the final |
---|
111 | # triangulation from the output .node file (including duplicate |
---|
112 | # input vertices and vertices ``eaten'' by holes). - output a |
---|
113 | # list of neighboring triangles |
---|
114 | |
---|
115 | #print "points",points |
---|
116 | #print "segments", segments |
---|
117 | #print "segments.shape", segments.shape |
---|
118 | #print "holes", holes |
---|
119 | #print "regions", regions |
---|
120 | #print "pointatts", pointatts |
---|
121 | #print "segatts", segatts |
---|
122 | #print "mode", mode |
---|
123 | return triang.genMesh(points,segments,holes,regions, |
---|
124 | pointatts,segatts, mode, segments.flat) |
---|
125 | |
---|
126 | def add_area_tag(regions): |
---|
127 | """ |
---|
128 | So, what is the format? |
---|
129 | A list with |
---|
130 | [x,y,region_tag,area] OR [x,y,region_tag] |
---|
131 | if it's [x,y,region_tag], add a 4th element, value of 0.0. |
---|
132 | """ |
---|
133 | if isinstance(regions, ListType): |
---|
134 | for i, region in enumerate(regions): |
---|
135 | if len(region) == 3: |
---|
136 | if isinstance(region, TupleType): |
---|
137 | #FIXME: How do you convert a tuple to a list? |
---|
138 | # I can do it a stupid way.. |
---|
139 | tuple = region[:] |
---|
140 | regions[i] = [] |
---|
141 | for j in tuple: |
---|
142 | regions[i].append(j) |
---|
143 | regions[i].append(0.0) |
---|
144 | else: |
---|
145 | regions[i].append(0.0) |
---|
146 | |
---|
147 | # let ensure numeric catch this.. |
---|
148 | #len(region) <= 2: |
---|
149 | #msg = 'ERROR: Inconsistent regions array.' |
---|
150 | #raise msg |
---|
151 | #elif |
---|
152 | return regions |
---|
153 | |
---|
154 | if __name__ == "__main__": |
---|
155 | pass |
---|