forked from akanazawa/cmr
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmeshzoo.py
290 lines (245 loc) · 10.1 KB
/
meshzoo.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
280
281
282
283
284
285
286
287
288
289
290
# -*- coding: utf-8 -*-
# The MIT License (MIT)
# Copyright (c) 2016-2019 Nico Schlömer
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Code Borrowed from [meshzoo](https://github.com/nschloe/meshzoo)
import numpy
# pylint: disable=too-many-locals, too-many-statements
def _refine(node_coords, cells_nodes, edge_nodes, cells_edges):
"""Canonically refine a mesh by inserting nodes at all edge midpoints
and make four triangular elements where there was one.
This is a very crude refinement; don't use for actual applications.
"""
num_nodes = len(node_coords)
num_new_nodes = len(edge_nodes)
# new_nodes = numpy.empty(num_new_nodes, dtype=numpy.dtype((float, 2)))
node_coords.resize(num_nodes + num_new_nodes, 3, refcheck=False)
# Set starting index for new nodes.
new_node_gid = num_nodes
# After the refinement step, all previous edge-node associations will be
# obsolete, so record *all* the new edges.
num_edges = len(edge_nodes)
num_cells = len(cells_nodes)
assert num_cells == len(cells_edges)
num_new_edges = 2 * num_edges + 3 * num_cells
new_edges_nodes = numpy.empty(num_new_edges, dtype=numpy.dtype((int, 2)))
new_edge_gid = 0
# After the refinement step, all previous cell-node associations will be
# obsolete, so record *all* the new cells.
num_new_cells = 4 * num_cells
new_cells_nodes = numpy.empty(num_new_cells, dtype=numpy.dtype((int, 3)))
new_cells_edges = numpy.empty(num_new_cells, dtype=numpy.dtype((int, 3)))
new_cell_gid = 0
is_edge_divided = numpy.zeros(num_edges, dtype=bool)
edge_midpoint_gids = numpy.empty(num_edges, dtype=int)
edge_newedges_gids = numpy.empty(num_edges, dtype=numpy.dtype((int, 2)))
# Loop over all elements.
for cell_id, cell in enumerate(zip(cells_edges, cells_nodes)):
cell_edges, cell_nodes = cell
# Divide edges.
local_edge_midpoint_gids = numpy.empty(3, dtype=int)
local_edge_newedges = numpy.empty(3, dtype=numpy.dtype((int, 2)))
local_neighbor_midpoints = [[], [], []]
local_neighbor_newedges = [[], [], []]
for k, edge_gid in enumerate(cell_edges):
edgenodes_gids = edge_nodes[edge_gid]
if is_edge_divided[edge_gid]:
# Edge is already divided. Just keep records for the cell
# creation.
local_edge_midpoint_gids[k] = edge_midpoint_gids[edge_gid]
local_edge_newedges[k] = edge_newedges_gids[edge_gid]
else:
# Create new node at the edge midpoint.
node_coords[new_node_gid] = 0.5 * (
node_coords[edgenodes_gids[0]] + node_coords[edgenodes_gids[1]]
)
local_edge_midpoint_gids[k] = new_node_gid
new_node_gid += 1
edge_midpoint_gids[edge_gid] = local_edge_midpoint_gids[k]
# Divide edge into two.
new_edges_nodes[new_edge_gid] = numpy.array(
[edgenodes_gids[0], local_edge_midpoint_gids[k]]
)
new_edge_gid += 1
new_edges_nodes[new_edge_gid] = numpy.array(
[local_edge_midpoint_gids[k], edgenodes_gids[1]]
)
new_edge_gid += 1
local_edge_newedges[k] = [new_edge_gid - 2, new_edge_gid - 1]
edge_newedges_gids[edge_gid] = local_edge_newedges[k]
# Do the household.
is_edge_divided[edge_gid] = True
# Keep a record of the new neighbors of the old nodes.
# Get local node IDs.
edgenodes_lids = [
numpy.nonzero(cell_nodes == edgenodes_gids[0])[0][0],
numpy.nonzero(cell_nodes == edgenodes_gids[1])[0][0],
]
local_neighbor_midpoints[edgenodes_lids[0]].append(
local_edge_midpoint_gids[k]
)
local_neighbor_midpoints[edgenodes_lids[1]].append(
local_edge_midpoint_gids[k]
)
local_neighbor_newedges[edgenodes_lids[0]].append(local_edge_newedges[k][0])
local_neighbor_newedges[edgenodes_lids[1]].append(local_edge_newedges[k][1])
new_edge_opposite_of_local_node = numpy.empty(3, dtype=int)
# New edges: Connect the three midpoints.
for k in range(3):
new_edges_nodes[new_edge_gid] = local_neighbor_midpoints[k]
new_edge_opposite_of_local_node[k] = new_edge_gid
new_edge_gid += 1
# Create new elements.
# Center cell:
new_cells_nodes[new_cell_gid] = local_edge_midpoint_gids
new_cells_edges[new_cell_gid] = new_edge_opposite_of_local_node
new_cell_gid += 1
# The three corner elements:
for k in range(3):
new_cells_nodes[new_cell_gid] = numpy.array(
[
cells_nodes[cell_id][k],
local_neighbor_midpoints[k][0],
local_neighbor_midpoints[k][1],
]
)
new_cells_edges[new_cell_gid] = numpy.array(
[
new_edge_opposite_of_local_node[k],
local_neighbor_newedges[k][0],
local_neighbor_newedges[k][1],
]
)
new_cell_gid += 1
return node_coords, new_cells_nodes, new_edges_nodes, new_cells_edges
def create_edges(cells_nodes):
"""Setup edge-node and edge-cell relations. Adapted from voropy.
"""
# Create the idx_hierarchy (nodes->edges->cells), i.e., the value of
# `self.idx_hierarchy[0, 2, 27]` is the index of the node of cell 27, edge
# 2, node 0. The shape of `self.idx_hierarchy` is `(2, 3, n)`, where `n` is
# the number of cells. Make sure that the k-th edge is opposite of the k-th
# point in the triangle.
local_idx = numpy.array([[1, 2], [2, 0], [0, 1]]).T
# Map idx back to the nodes. This is useful if quantities which are in
# idx shape need to be added up into nodes (e.g., equation system rhs).
nds = cells_nodes.T
idx_hierarchy = nds[local_idx]
s = idx_hierarchy.shape
a = numpy.sort(idx_hierarchy.reshape(s[0], s[1] * s[2]).T)
b = numpy.ascontiguousarray(a).view(
numpy.dtype((numpy.void, a.dtype.itemsize * a.shape[1]))
)
_, idx, inv, cts = numpy.unique(
b, return_index=True, return_inverse=True, return_counts=True
)
# No edge has more than 2 cells. This assertion fails, for example, if
# cells are listed twice.
assert all(cts < 3)
edge_nodes = a[idx]
cells_edges = inv.reshape(3, -1).T
return edge_nodes, cells_edges
def show2d(*args, **kwargs):
import matplotlib.pyplot as plt
plot2d(*args, **kwargs)
plt.show()
return
def plot2d(points, cells, mesh_color="k", show_axes=False):
"""Plot a 2D mesh using matplotlib.
"""
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
fig = plt.figure()
ax = fig.gca()
plt.axis("equal")
if not show_axes:
ax.set_axis_off()
xmin = numpy.amin(points[:, 0])
xmax = numpy.amax(points[:, 0])
ymin = numpy.amin(points[:, 1])
ymax = numpy.amax(points[:, 1])
width = xmax - xmin
xmin -= 0.1 * width
xmax += 0.1 * width
height = ymax - ymin
ymin -= 0.1 * height
ymax += 0.1 * height
ax.set_xlim(xmin, xmax)
ax.set_ylim(ymin, ymax)
edge_nodes, _ = create_edges(cells)
# Get edges, cut off z-component.
e = points[edge_nodes][:, :, :2]
line_segments = LineCollection(e, color=mesh_color)
ax.add_collection(line_segments)
return fig
def iso_sphere(ref_steps=4):
# Start off with an isosahedron and refine.
# Construction from
# <http://blog.andreaskahler.com/2009/06/creating-icosphere-mesh-in-code.html>.
# Create 12 vertices of a icosahedron.
t = (1.0 + numpy.sqrt(5.0)) / 2.0
nodes = numpy.array(
[
[-1, +t, +0],
[+1, +t, +0],
[-1, -t, +0],
[+1, -t, +0],
#
[+0, -1, +t],
[+0, +1, +t],
[+0, -1, -t],
[+0, +1, -t],
#
[+t, +0, -1],
[+t, +0, +1],
[-t, +0, -1],
[-t, +0, +1],
]
)
cells_nodes = numpy.array(
[
[0, 11, 5],
[0, 5, 1],
[0, 1, 7],
[0, 7, 10],
[0, 10, 11],
[1, 5, 9],
[5, 11, 4],
[11, 10, 2],
[10, 7, 6],
[7, 1, 8],
[3, 9, 4],
[3, 4, 2],
[3, 2, 6],
[3, 6, 8],
[3, 8, 9],
[4, 9, 5],
[2, 4, 11],
[6, 2, 10],
[8, 6, 7],
[9, 8, 1],
]
)
# Refine.
edge_nodes, cells_edges = create_edges(cells_nodes)
args = nodes, cells_nodes, edge_nodes, cells_edges
for _ in range(ref_steps):
args = _refine(*args)
# push all nodes to the sphere
nodes = args[0]
nodes = (nodes.T / numpy.sqrt(numpy.einsum("ij,ij->i", nodes, nodes)).T).T
return nodes, args[1]