| 1 |
import numpy as np |
| 2 |
|
| 3 |
|
| 4 |
def tube(length: float = 1.0, radius: float = 1.0, n: int = 30): |
| 5 |
# Number of points along the width of the strip (>= 2) |
| 6 |
# Choose it such that we have approximately square boxes. |
| 7 |
nw = int(round(length * n / (2 * np.pi * radius))) |
| 8 |
|
| 9 |
# Generate suitable ranges for parametrization |
| 10 |
u_range = np.linspace(0.0, 2 * np.pi, num=n, endpoint=False) |
| 11 |
v_range = np.linspace(-0.5 * length, 0.5 * length, num=nw) |
| 12 |
|
| 13 |
# Create the vertices. |
| 14 |
proto_points = np.dstack(np.meshgrid(u_range, v_range, indexing="ij")).reshape( |
| 15 |
-1, 2 |
| 16 |
) |
| 17 |
points = np.column_stack( |
| 18 |
[ |
| 19 |
radius * np.cos(proto_points[:, 0]), |
| 20 |
radius * np.sin(proto_points[:, 0]), |
| 21 |
proto_points[:, 1], |
| 22 |
] |
| 23 |
) |
| 24 |
|
| 25 |
# create the elements (cells) |
| 26 |
cells = [] |
| 27 |
for i in range(n - 1): |
| 28 |
for j in range(nw - 1): |
| 29 |
cells.append([i * nw + j, (i + 1) * nw + j + 1, i * nw + j + 1]) |
| 30 |
cells.append([i * nw + j, (i + 1) * nw + j, (i + 1) * nw + j + 1]) |
| 31 |
|
| 32 |
# close the geometry |
| 33 |
for j in range(nw - 1): |
| 34 |
cells.append([(n - 1) * nw + j, j + 1, (n - 1) * nw + j + 1]) |
| 35 |
cells.append([(n - 1) * nw + j, j, j + 1]) |
| 36 |
|
| 37 |
return points, np.array(cells) |