| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import numpy as np |
| 4 |
from numpy.typing import ArrayLike |
| 5 |
|
| 6 |
|
| 7 |
# backwards compatibility |
| 8 |
def cube( |
| 9 |
x0: float, |
| 10 |
x1: float, |
| 11 |
y0: float, |
| 12 |
y1: float, |
| 13 |
z0: float, |
| 14 |
z1: float, |
| 15 |
nx: int, |
| 16 |
ny: int, |
| 17 |
nz: int, |
| 18 |
): |
| 19 |
return cube_tetra( |
| 20 |
np.linspace(x0, x1, nx + 1), |
| 21 |
np.linspace(y0, y1, ny + 1), |
| 22 |
np.linspace(z0, z1, nz + 1), |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
def cube_hexa( |
| 27 |
x_range: ArrayLike, y_range: ArrayLike, z_range: ArrayLike |
| 28 |
) -> tuple[np.ndarray, np.ndarray]: |
| 29 |
x_range = np.asarray(x_range) |
| 30 |
y_range = np.asarray(y_range) |
| 31 |
z_range = np.asarray(z_range) |
| 32 |
|
| 33 |
nx1 = len(x_range) |
| 34 |
ny1 = len(y_range) |
| 35 |
nz1 = len(z_range) |
| 36 |
|
| 37 |
# Create the vertices. |
| 38 |
x, y, z = np.meshgrid(x_range, y_range, z_range, indexing="ij") |
| 39 |
|
| 40 |
# Alternative with slightly different order: |
| 41 |
# ``` |
| 42 |
# points = np.stack([x, y, z]).T.reshape(-1, 3) |
| 43 |
# ``` |
| 44 |
points = np.array([x, y, z]).T.reshape(-1, 3) |
| 45 |
|
| 46 |
# Create the cells. |
| 47 |
a = np.arange(len(points)).reshape(nz1, ny1, nx1) |
| 48 |
a = np.transpose(a, [2, 1, 0]) |
| 49 |
|
| 50 |
# `c` contains the indices of each "cube" like |
| 51 |
# |
| 52 |
# |
| 53 |
# c[7] c[6] |
| 54 |
# ________ |
| 55 |
# / /| |
| 56 |
# c[4] /_______/ | c[5] |
| 57 |
# | | | |
| 58 |
# | | | |
| 59 |
# | | / c[2] |
| 60 |
# |________|/ |
| 61 |
# |
| 62 |
# c[0] c[1] |
| 63 |
# |
| 64 |
cells = ( |
| 65 |
np.array( |
| 66 |
[ |
| 67 |
a[:-1, :-1, :-1], |
| 68 |
a[1:, :-1, :-1], |
| 69 |
a[1:, 1:, :-1], |
| 70 |
a[:-1, 1:, :-1], |
| 71 |
a[:-1, :-1, 1:], |
| 72 |
a[1:, :-1, 1:], |
| 73 |
a[1:, 1:, 1:], |
| 74 |
a[:-1, 1:, 1:], |
| 75 |
] |
| 76 |
) |
| 77 |
.reshape(8, -1) |
| 78 |
.T |
| 79 |
) |
| 80 |
|
| 81 |
return points, cells |
| 82 |
|
| 83 |
|
| 84 |
def cube_tetra(x_range: ArrayLike, y_range: ArrayLike, z_range: ArrayLike): |
| 85 |
x_range = np.asarray(x_range) |
| 86 |
y_range = np.asarray(y_range) |
| 87 |
z_range = np.asarray(z_range) |
| 88 |
|
| 89 |
nx1 = len(x_range) |
| 90 |
ny1 = len(y_range) |
| 91 |
nz1 = len(z_range) |
| 92 |
|
| 93 |
# Create the vertices. |
| 94 |
x, y, z = np.meshgrid(x_range, y_range, z_range, indexing="ij") |
| 95 |
# Alternative with slightly different order: |
| 96 |
# ``` |
| 97 |
# points = np.stack([x, y, z]).T.reshape(-1, 3) |
| 98 |
# ``` |
| 99 |
points = np.array([x, y, z]).T.reshape(-1, 3) |
| 100 |
|
| 101 |
# Create the elements (cells). |
| 102 |
a = np.arange(len(points)).reshape(nz1, ny1, nx1) |
| 103 |
a = np.transpose(a, [2, 1, 0]) |
| 104 |
|
| 105 |
# |
| 106 |
# `c` contains the indices of each "cube" like |
| 107 |
# |
| 108 |
# |
| 109 |
# c[7] c[6] |
| 110 |
# ________ |
| 111 |
# / /| |
| 112 |
# c[4] /_______/ | c[5] |
| 113 |
# | | | |
| 114 |
# | | | |
| 115 |
# | | / c[2] |
| 116 |
# |________|/ |
| 117 |
# |
| 118 |
# c[0] c[1] |
| 119 |
# |
| 120 |
c = [ |
| 121 |
a[:-1, :-1, :-1], |
| 122 |
a[1:, :-1, :-1], |
| 123 |
a[1:, 1:, :-1], |
| 124 |
a[:-1, 1:, :-1], |
| 125 |
a[:-1, :-1, 1:], |
| 126 |
a[1:, :-1, 1:], |
| 127 |
a[1:, 1:, 1:], |
| 128 |
a[:-1, 1:, 1:], |
| 129 |
] |
| 130 |
|
| 131 |
# 3D checkers |
| 132 |
idx = np.ones((nx1 - 1, ny1 - 1, nz1 - 1), dtype=bool) |
| 133 |
idx[::2, 1::2, ::2] = False |
| 134 |
idx[::2, ::2, 1::2] = False |
| 135 |
idx[1::2, ::2, ::2] = False |
| 136 |
idx[1::2, 1::2, 1::2] = False |
| 137 |
|
| 138 |
# There is 1 way to split a cube into 5 tetrahedra, |
| 139 |
# and 12 ways to split it into 6 tetrahedra. |
| 140 |
# See |
| 141 |
# <http://www.baumanneduard.ch/Splitting%20a%20cube%20in%20tetrahedras2.htm> |
| 142 |
# Also interesting: <https://en.wikipedia.org/wiki/Marching_tetrahedrons>. |
| 143 |
cells = [ |
| 144 |
# regular; make sure the order of the points is such that the signed volume is |
| 145 |
# positive |
| 146 |
[c[0][idx], c[1][idx], c[3][idx], c[4][idx]], |
| 147 |
[c[1][idx], c[2][idx], c[3][idx], c[6][idx]], |
| 148 |
[c[1][idx], c[3][idx], c[4][idx], c[6][idx]], |
| 149 |
[c[1][idx], c[4][idx], c[5][idx], c[6][idx]], |
| 150 |
[c[3][idx], c[4][idx], c[6][idx], c[7][idx]], |
| 151 |
# the rest rotated such that it fits with the others; basically we change |
| 152 |
# "bottom" and "top" of the dice |
| 153 |
[c[4][~idx], c[5][~idx], c[0][~idx], c[7][~idx]], |
| 154 |
[c[5][~idx], c[6][~idx], c[2][~idx], c[7][~idx]], |
| 155 |
[c[5][~idx], c[7][~idx], c[2][~idx], c[0][~idx]], |
| 156 |
[c[5][~idx], c[0][~idx], c[2][~idx], c[1][~idx]], |
| 157 |
[c[7][~idx], c[0][~idx], c[3][~idx], c[2][~idx]], |
| 158 |
] |
| 159 |
|
| 160 |
cells = np.column_stack([np.array(c).reshape(4, -1) for c in cells]).T |
| 161 |
|
| 162 |
return points, cells |