forked from romanzes637/gmsh_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
polygon.py
279 lines (263 loc) · 11 KB
/
polygon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import itertools
import time
from pprint import pprint
import gmsh
from connection import micromine
from complex import Complex
from primitive import Primitive
from support import check_file
class Polygon(Complex):
def __init__(self, factory, inputs, physical_names, reader, reader_kwargs,
coordinates_transform, mesh_size_coefficients):
if factory == 'occ':
factory_object = gmsh.model.occ
else:
factory_object = gmsh.model.geo
primitives = list()
for i, path in enumerate(inputs):
result = check_file(path)
reader_kwargs["path"] = result['path']
points, polygons = readers[reader](**reader_kwargs)
print('Transform coordinates')
points = transform_coordinates(points, coordinates_transform)
print('last point: {}'.format(points[-1]))
print('Evaluate groups')
groups_points, groups_polygons = evaluate_groups(points, polygons)
print('Number of groups points: {}\nNumber of groups polygons: {}'
.format([len(x) for x in groups_points],
[len(x) for x in groups_polygons]))
print('Groups: {}'.format(len(groups_points)))
print('Check closed volumes')
result = check_closed_volume(polygons, groups_polygons)
print('Closed volumes {}'.format(sum(result)))
print(result)
# Leave only closed volumes
closed_groups_points = list(
itertools.compress(groups_points, result))
closed_groups_polygons = list(
itertools.compress(groups_polygons, result))
print('Evaluate sizes')
sizes = evaluate_groups_points_mesh_sizes(points, polygons,
closed_groups_polygons)
print(sizes)
print('Correct sizes by coefficient')
k = mesh_size_coefficients[i]
print('Coefficient: {}'.format(k))
sizes = [k * x for x in sizes]
print(sizes)
print('Create closed volumes only')
vs = create_volumes(factory_object, points, polygons,
closed_groups_points, closed_groups_polygons,
sizes)
name = physical_names[i]
p = Primitive(factory, physical_name=name, inner_volumes=vs)
primitives.append(p)
Complex.__init__(self, factory, primitives)
def transform_coordinates(points, transform):
dx, dy, dz, k = transform
for i, p in enumerate(points):
x, y, z = p
points[i] = (k * x + dx, k * y + dy, k * z + dz)
return points
def evaluate_groups(points, polygons):
# Points neighbours
points_neighbours = dict()
points_polygons = dict()
report = dict()
for i, polygon in enumerate(polygons):
for point in polygon:
neighbours = [x for x in polygon if x != point]
points_neighbours.setdefault(point, set()).update(neighbours)
points_polygons.setdefault(point, list()).append(i)
n_neighbours = len(points_neighbours)
points_n_neighbours = [len(x) for x in points_neighbours.values()]
ns_avg = float(sum(points_n_neighbours)) / n_neighbours
ns_max = max(points_n_neighbours)
ns_min = min(points_n_neighbours)
report['n_points_neighbours'] = n_neighbours
report['point_neighbours_avg'] = ns_avg
report['point_neighbours_max'] = ns_max
report['point_neighbours_min'] = ns_min
# Points groups
points_groups = set()
# time_left = 0
# time_spent = 0
for i, p in enumerate(points):
# print('Point {}/{}'.format(i + 1, len(points)))
# start_time = time.time()
in_group = False
for g in points_groups:
if i in g:
in_group = True
break
# print('Already in the group: {}'.format(in_group))
if not in_group:
have_been = set()
to_go = points_neighbours[i]
while len(to_go) > 0:
have_been.update(to_go)
new_to_go = set()
for n in to_go:
n_neighbours = points_neighbours[n]
have_not_been = n_neighbours.difference(have_been)
new_to_go.update(have_not_been)
to_go = new_to_go
points_groups.add(frozenset(have_been))
# time_per_point = time.time() - start_time
# time_spent += time_per_point
# time_left = time_per_point * (len(points) - (i + 1))
# print('Groups: {}'.format(len(points_groups)))
# print('Lengths: {}'.format([len(x) for x in points_groups]))
# print('Time point: {}s'.format(time_per_point))
# print('Time spent: {}s'.format(time_spent))
# print('Time left: {}s'.format(time_left))
# with open('check_carcass.json', 'w') as f:
# data = dict()
# data['points_groups'] = [list(x) for x in points_groups]
# json.dump(data, f)
n_points_groups = len(points_groups)
groups_n_points = [len(x) for x in points_groups]
ps_avg = float(sum(groups_n_points)) / n_points_groups
ps_max = max(groups_n_points)
ps_min = min(groups_n_points)
report['n_points_groups'] = n_points_groups
report['group_points_avg'] = ps_avg
report['group_points_max'] = ps_max
report['group_points_min'] = ps_min
# Group's points and polygons
groups_points = list(points_groups)
groups_polygons = list()
for group in groups_points:
group_polygons = set()
for point in group:
point_polygons = points_polygons[point]
group_polygons.update(point_polygons)
groups_polygons.append(group_polygons)
pprint(report)
return groups_points, groups_polygons
def evaluate_groups_points_mesh_sizes(points, polygons, polygons_groups):
"""
Mesh size at points as min edge length
:param points:
:param polygons:
:param polygons_groups:
:return: mesh size at points
"""
sizes = list()
edges_square_lengths = dict()
for group in polygons_groups:
for polygon_index in group:
polygon = polygons[polygon_index]
for i in range(len(polygon) - 1):
start = polygon[i]
end = polygon[i + 1]
edge = frozenset([start, end])
if edge not in edges_square_lengths:
start_cs = points[start]
end_cs = points[end]
square_length = sum(
[(x[0] - x[1]) ** 2 for x in zip(end_cs, start_cs)])
edges_square_lengths[edge] = square_length
min_edge = min(edges_square_lengths, key=edges_square_lengths.get)
min_edge_length = edges_square_lengths[min_edge] ** 0.5
sizes.append(min_edge_length)
return sizes
def check_closed_volume(polygons, groups_polygons):
result = list()
# opened = set()
for group in groups_polygons:
number_of_edges = dict()
for polygon_index in group:
polygon = polygons[polygon_index]
# Edge loop
for i in range(len(polygon) - 1):
start = polygon[i]
end = polygon[i + 1]
edge = frozenset([start, end])
number_of_edges.setdefault(edge, 0)
number_of_edges[edge] += 1
# Close loop
start = polygon[-1]
end = polygon[0]
edge = frozenset([start, end])
number_of_edges.setdefault(edge, 0)
number_of_edges[edge] += 1
closed = True
for key, value in number_of_edges.items():
if value != 2: # Has no opposite edge
# print(key, value)
closed = False
break
# opened.add(key)
result.append(closed)
# print(opened)
return result
def create_volumes(factory_object, points, polygons, groups_points,
groups_polygons, points_mesh_sizes):
volumes = list()
for j, group_points in enumerate(groups_points):
group_polygons = groups_polygons[j]
print('Group: {}\nPoints: {}\nPolygons: {}'.format(
j + 1, len(group_points), len(group_polygons)))
print('Points')
lc = points_mesh_sizes[j]
point_index_to_tag = dict()
for point_index in group_points:
c1, c2, c3 = points[point_index]
tag = factory_object.addPoint(c1, c2, c3, lc)
point_index_to_tag[point_index] = tag
print('Lines and Surfaces')
surfaces = list()
pair_points_to_tag = dict()
for polygon_index in group_polygons:
curves = list()
polygon = polygons[polygon_index]
# Edges loop
for i in range(len(polygon) - 1):
point_index_0 = polygon[i]
point_index_1 = polygon[i + 1]
# Move forward the numbering by 1, because 0 cannot be negative
# Note: minus for second point is for distinguish 1 to 2 from
# 2 to 1 line in non ordered frozenset as
# (1, 2) set for 1 -> 2 line # and (1, -2) set for 2 -> 1 line
pair_points = frozenset(
[point_index_0 + 1, -(point_index_1 + 1)])
if pair_points not in pair_points_to_tag:
tag_0 = point_index_to_tag[point_index_0]
tag_1 = point_index_to_tag[point_index_1]
tag = factory_object.addLine(tag_0, tag_1)
pair_points_to_tag[pair_points] = tag
else:
tag = pair_points_to_tag[pair_points]
curves.append(tag)
# Close loop
point_index_0 = polygon[-1]
point_index_1 = polygon[0]
pair_points = frozenset([point_index_0 + 1, -(point_index_1 + 1)])
if pair_points not in pair_points_to_tag:
tag_0 = point_index_to_tag[point_index_0]
tag_1 = point_index_to_tag[point_index_1]
tag = factory_object.addLine(tag_0, tag_1)
pair_points_to_tag[pair_points] = tag
else:
tag = pair_points_to_tag[pair_points]
curves.append(tag)
# Surfaces
loop_tag = factory_object.addCurveLoop(curves)
if len(curves) <= 4:
if factory_object == gmsh.model.geo:
surface_tag = factory_object.addSurfaceFilling([loop_tag])
else:
surface_tag = factory_object.addSurfaceFilling(loop_tag)
else:
surface_tag = factory_object.addPlaneSurface([loop_tag])
surfaces.append(surface_tag)
# Surfaces
print('Volumes')
loop_tag = factory_object.addSurfaceLoop(surfaces)
volume_tag = factory_object.addVolume([loop_tag])
volumes.append(volume_tag)
return volumes
readers = {
"micromine": micromine.read
}