Debian Codegraph @

src/meshzoo/_sphere.py

raw
1 import numpy as np
2
3 from ._platonic import icosa_surface, octa_surface, tetra_surface
4
5
6 def uv_sphere(num_points_per_circle: int, num_circles: int, radius: float = 1.0):
7 # Mesh parameters
8 n_phi = num_points_per_circle
9 n_theta = num_circles
10
11 # Generate suitable ranges for parametrization
12 phi_range = np.linspace(0.0, 2 * np.pi, num=n_phi, endpoint=False)
13 theta_range = np.linspace(
14 -np.pi / 2 + np.pi / (n_theta - 1),
15 np.pi / 2 - np.pi / (n_theta - 1),
16 num=n_theta - 2,
17 )
18
19 # nodes in the circles of latitude (except poles)
20 nodes = radius * np.array(
21 [[0.0, 0.0, -1.0]] # south pole
22 + [
23 [
24 np.cos(theta) * np.sin(phi),
25 np.cos(theta) * np.cos(phi),
26 np.sin(theta),
27 ]
28 for theta in theta_range
29 for phi in phi_range
30 ]
31 + [[0.0, 0.0, 1.0]] # north pole
32 )
33
34 south_pole_index = 0
35 north_pole_index = len(nodes) - 1
36
37 # create the elements (cells)
38 num_cells = 2 * (n_theta - 2) * n_phi
39 cells = []
40
41 # connections to south pole
42 for i in range(n_phi - 1):
43 cells.append([south_pole_index, i + 1, i + 2])
44 # close geometry
45 cells.append([south_pole_index, n_phi, 1])
46
47 # non-pole elements
48 for i in range(n_theta - 3):
49 for j in range(n_phi - 1):
50 cells += [
51 [i * n_phi + j + 2, i * n_phi + j + 1, (i + 1) * n_phi + j + 2],
52 [i * n_phi + j + 1, (i + 1) * n_phi + j + 1, (i + 1) * n_phi + j + 2],
53 ]
54
55 # close the geometry
56 for i in range(n_theta - 3):
57 cells += [
58 [i * n_phi + 1, (i + 1) * n_phi, (i + 1) * n_phi + 1],
59 [(i + 1) * n_phi + 1, (i + 1) * n_phi, (i + 2) * n_phi],
60 ]
61
62 # connections to the north pole
63 for i in range(n_phi - 1):
64 cells.append(
65 [
66 i + 1 + n_phi * (n_theta - 3) + 1,
67 i + n_phi * (n_theta - 3) + 1,
68 north_pole_index,
69 ]
70 )
71 # close geometry
72 cells.append(
73 [
74 0 + n_phi * (n_theta - 3) + 1,
75 n_phi - 1 + n_phi * (n_theta - 3) + 1,
76 north_pole_index,
77 ]
78 )
79 cells = np.array(cells)
80 assert len(cells) == num_cells, "Wrong element count."
81
82 return nodes, cells
83
84
85 def geo_sphere(num_points_per_circle: int, num_circles: int, radius=1.0):
86 # Mesh parameters
87 n_phi = num_points_per_circle
88 n_theta = num_circles
89
90 # Generate suitable ranges for parametrization
91 phi_range = np.linspace(0.0, 2 * np.pi, num=n_phi, endpoint=False)
92 theta_range = np.linspace(
93 -np.pi / 2 + np.pi / (n_theta - 1),
94 np.pi / 2 - np.pi / (n_theta - 1),
95 num=n_theta - 2,
96 )
97
98 # nodes in the circles of latitude (except poles)
99 nodes = radius * np.array(
100 [[0.0, 0.0, -1.0]] # south pole
101 + [
102 [
103 np.cos(theta) * np.sin(phi),
104 np.cos(theta) * np.cos(phi),
105 np.sin(theta),
106 ]
107 for theta in theta_range
108 for phi in phi_range
109 ]
110 + [[0.0, 0.0, 1.0]] # north pole
111 )
112
113 south_pole_index = 0
114 north_pole_index = len(nodes) - 1
115
116 # create the elements (cells)
117 tri = []
118 quad = []
119
120 # connections to south pole
121 for i in range(n_phi - 1):
122 tri.append([south_pole_index, i + 1, i + 2])
123 # close geometry
124 tri.append([south_pole_index, n_phi, 1])
125
126 # non-pole elements
127 for i in range(n_theta - 3):
128 for j in range(n_phi - 1):
129 quad += [
130 [
131 i * n_phi + j + 1,
132 i * n_phi + j + 2,
133 (i + 1) * n_phi + j + 2,
134 (i + 1) * n_phi + j + 1,
135 ],
136 ]
137
138 # close the geometry
139 for i in range(n_theta - 3):
140 quad += [
141 [i * n_phi + 1, (i + 1) * n_phi + 1, (i + 2) * n_phi, (i + 1) * n_phi],
142 ]
143
144 # connections to the north pole
145 for i in range(n_phi - 1):
146 tri.append(
147 [
148 i + 1 + n_phi * (n_theta - 3) + 1,
149 i + n_phi * (n_theta - 3) + 1,
150 north_pole_index,
151 ]
152 )
153 # close geometry
154 tri.append(
155 [
156 0 + n_phi * (n_theta - 3) + 1,
157 n_phi - 1 + n_phi * (n_theta - 3) + 1,
158 north_pole_index,
159 ]
160 )
161 tri = np.array(tri)
162 quad = np.array(quad)
163
164 return nodes, tri, quad
165
166
167 def tetra_sphere(n):
168 vertices, cells = tetra_surface(n)
169 # push all nodes to the sphere
170 norms = np.sqrt(np.einsum("ij,ij->i", vertices, vertices))
171 vertices = (vertices.T / norms).T
172 return vertices, cells
173
174
175 def octa_sphere(n: int):
176 vertices, cells = octa_surface(n)
177 # push all nodes to the sphere
178 norms = np.sqrt(np.einsum("ij,ij->i", vertices, vertices))
179 vertices = (vertices.T / norms).T
180 return vertices, cells
181
182
183 def icosa_sphere(n: int, flat_top: bool = False):
184 vertices, cells = icosa_surface(n, flat_top)
185 # push all nodes to the sphere
186 norms = np.sqrt(np.einsum("ij,ij->i", vertices, vertices))
187 vertices = (vertices.T / norms).T
188 return vertices, cells