Skip to content

Commit

Permalink
Merge pull request #71 from autotwin/spheres
Browse files Browse the repository at this point in the history
  • Loading branch information
hovey authored Sep 13, 2024
2 parents ddf542e + dbe546a commit 789d243
Show file tree
Hide file tree
Showing 16 changed files with 1,950 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
fail-fast: false
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']
python-version: ['3.9', '3.10', '3.11', '3.12']
runs-on: ${{ matrix.os }}
steps:
- name: checkout
Expand Down Expand Up @@ -41,7 +41,7 @@ jobs:
fail-fast: false
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']
python-version: ['3.9', '3.10', '3.11', '3.12']
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 2 additions & 0 deletions book/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

- [Installation](installation.md)
- [Mathematics](mathematics.md)
- [Examples](examples/README.md)
- [Spheres](examples/spheres/README.md)

# Reference Guide

Expand Down
3 changes: 3 additions & 0 deletions book/examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Examples

Following are examples created with `automesh`.
117 changes: 117 additions & 0 deletions book/examples/spheres/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# Spheres

## Segmentation

Using [spheres.py](spheres.py),

```python
<!-- cmdrun cat spheres.py -->
```

create very coarse spheres of varying
resolution (`radius=1`, `radius=3`, and `radius=5`), as shown below:

![spheres.png](spheres.png)

For the `radius=1` case, the underyling data structure appears as:

```python
spheres["ball_1"]

array([[[0, 0, 0],
[0, 1, 0],
[0, 0, 0]],

[[0, 1, 0],
[1, 1, 1],
[0, 1, 0]],

[[0, 0, 0],
[0, 1, 0],
[0, 0, 0]]], dtype=uint8)
```

For the `radius=3` case, the underyling data structure appears as:

```python
spheres["ball_3"]

array([[[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]],

[[0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0]],

[[0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0]],

[[0, 0, 0, 1, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 0],
[1, 1, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 1, 0, 0, 0]],

[[0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0]],

[[0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0]],

[[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]]], dtype=uint8)
```

Because of the large size of `ball_5`, its data structure is not shown
here.

These data structures are saved to

* [spheres_ball_1_.npy](spheres_ball_1_.npy)
* [spheres_ball_3_.npy](spheres_ball_3_.npy)
* [spheres_ball_5_.npy](spheres_ball_5_.npy)

## Mesh

```sh
cargo run -- -i book/examples/spheres/spheres_ball_1_.npy -o book/examples/spheres/spheres_ball_1_.inp -x 3 -y 3 -z 3
```

```sh
cargo run -- -i book/examples/spheres/spheres_ball_3_.npy -o book/examples/spheres/spheres_ball_3_.inp -x 7 -y 7 -z 7
```

```sh
cargo run -- -i book/examples/spheres/spheres_ball_5_.npy -o book/examples/spheres/spheres_ball_5_.inp -x 11 -y 11 -z 11
```
Binary file added book/examples/spheres/spheres.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
82 changes: 82 additions & 0 deletions book/examples/spheres/spheres.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
"""This module demonstrates how to create a voxelized sphere and export
it as a .npy file.
Items that need to be installed into the virtual environment:
pip install scikit-image
"""

import matplotlib.pyplot as plt
from matplotlib.colors import LightSource
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from pathlib import Path
from typing import Final

from skimage.morphology import ball


# User input begin

spheres = {
"ball_1": ball(radius=1),
"ball_3": ball(radius=3),
"ball_5": ball(radius=5),
}

aa = Path(__file__)
bb = aa.with_suffix(".png")

# Visualize the elements.
# width, height = 8, 4
width, height = 6, 3
fig = plt.figure(figsize=(width, height))
# fig = plt.figure(figsize=(8, 8))

el, az, roll = 63, -110, 0
cmap = plt.get_cmap(name="tab10")
num_colors = len(spheres)
voxel_alpha: Final[float] = 0.9

colors = cmap(np.linspace(0, 1, num_colors))
lightsource = LightSource(azdeg=325, altdeg=45) # azimuth, elevation
# lightsource = LightSource(azdeg=325, altdeg=90) # azimuth, elevation
dpi: Final[int] = 300 # resolution, dots per inch
serialize: Final[bool] = False # turn to True to save .png and .npy files
# User input end


idx = 1
for title, struc in spheres.items():
ax = fig.add_subplot(1, 3, idx, projection=Axes3D.name)
ax.voxels(
struc,
facecolors=colors[idx-1],
edgecolor=colors[idx-1],
alpha=voxel_alpha,
lightsource=lightsource)
ax.set_title(title)
idx += 1

# Set labels for the axes
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")

# Set the camera view
ax.set_aspect("equal")
ax.view_init(elev=el, azim=az, roll=roll)

if serialize:
cc = aa.with_stem("spheres_" + title + "_")
dd = cc.with_suffix(".npy")
# Save the data in .npy format
np.save(dd, struc)
print(f"Saved: {dd}")

fig.tight_layout()
plt.show()

if serialize:
fig.savefig(bb, dpi=dpi)
print(f"Saved: {bb}")
49 changes: 49 additions & 0 deletions book/examples/spheres/spheres_ball_1_.inp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
*HEADING
autotwin.automesh
version 0.1.3
autogenerated on 2024-09-11 23:05:42.323084 UTC
**
*NODE, NSET=ALLNODES
1, 1.000000e0, 1.000000e0, 0.000000e0
2, 2.000000e0, 1.000000e0, 0.000000e0
3, 1.000000e0, 2.000000e0, 0.000000e0
4, 2.000000e0, 2.000000e0, 0.000000e0
5, 1.000000e0, 0.000000e0, 1.000000e0
6, 2.000000e0, 0.000000e0, 1.000000e0
7, 0.000000e0, 1.000000e0, 1.000000e0
8, 1.000000e0, 1.000000e0, 1.000000e0
9, 2.000000e0, 1.000000e0, 1.000000e0
10, 3.000000e0, 1.000000e0, 1.000000e0
11, 0.000000e0, 2.000000e0, 1.000000e0
12, 1.000000e0, 2.000000e0, 1.000000e0
13, 2.000000e0, 2.000000e0, 1.000000e0
14, 3.000000e0, 2.000000e0, 1.000000e0
15, 1.000000e0, 3.000000e0, 1.000000e0
16, 2.000000e0, 3.000000e0, 1.000000e0
17, 1.000000e0, 0.000000e0, 2.000000e0
18, 2.000000e0, 0.000000e0, 2.000000e0
19, 0.000000e0, 1.000000e0, 2.000000e0
20, 1.000000e0, 1.000000e0, 2.000000e0
21, 2.000000e0, 1.000000e0, 2.000000e0
22, 3.000000e0, 1.000000e0, 2.000000e0
23, 0.000000e0, 2.000000e0, 2.000000e0
24, 1.000000e0, 2.000000e0, 2.000000e0
25, 2.000000e0, 2.000000e0, 2.000000e0
26, 3.000000e0, 2.000000e0, 2.000000e0
27, 1.000000e0, 3.000000e0, 2.000000e0
28, 2.000000e0, 3.000000e0, 2.000000e0
29, 1.000000e0, 1.000000e0, 3.000000e0
30, 2.000000e0, 1.000000e0, 3.000000e0
31, 1.000000e0, 2.000000e0, 3.000000e0
32, 2.000000e0, 2.000000e0, 3.000000e0
**
*ELEMENT, TYPE=C3D8R, ELSET=EB1
1, 1, 2, 4, 3, 8, 9, 13, 12
2, 5, 6, 9, 8, 17, 18, 21, 20
3, 7, 8, 12, 11, 19, 20, 24, 23
4, 8, 9, 13, 12, 20, 21, 25, 24
5, 9, 10, 14, 13, 21, 22, 26, 25
6, 12, 13, 16, 15, 24, 25, 28, 27
7, 20, 21, 25, 24, 29, 30, 32, 31
**
*SOLID SECTION, ELSET=EB1, MATERIAL=Default-Steel
Binary file added book/examples/spheres/spheres_ball_1_.npy
Binary file not shown.
Loading

0 comments on commit 789d243

Please sign in to comment.