morphogen 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- morphogen/__init__.py +60 -0
- morphogen/__main__.py +7 -0
- morphogen/_platform.py +55 -0
- morphogen/_public.py +153 -0
- morphogen/_utils.py +80 -0
- morphogen/_version.py +1 -0
- morphogen/analysis/__init__.py +174 -0
- morphogen/analysis/anisotropy.py +135 -0
- morphogen/analysis/basic.py +38 -0
- morphogen/analysis/cells.py +54 -0
- morphogen/analysis/connectivity.py +156 -0
- morphogen/analysis/correlation.py +195 -0
- morphogen/analysis/covariance.py +80 -0
- morphogen/analysis/fractal.py +54 -0
- morphogen/analysis/measures.py +188 -0
- morphogen/analysis/morphology.py +265 -0
- morphogen/analysis/particles.py +160 -0
- morphogen/analysis/percolation.py +2034 -0
- morphogen/analysis/power_laws.py +143 -0
- morphogen/analysis/random_sets.py +351 -0
- morphogen/analysis/stereology.py +289 -0
- morphogen/analysis/surface.py +107 -0
- morphogen/analysis/topology.py +92 -0
- morphogen/automata/__init__.py +74 -0
- morphogen/automata/engine.py +211 -0
- morphogen/automata/growth.py +686 -0
- morphogen/automata/lattice.py +143 -0
- morphogen/automata/reaction.py +255 -0
- morphogen/automata/rules.py +353 -0
- morphogen/automata/tilings.py +1715 -0
- morphogen/cli.py +3526 -0
- morphogen/config.py +2167 -0
- morphogen/contact/__init__.py +113 -0
- morphogen/contact/experiments.py +368 -0
- morphogen/contact/hertz.py +104 -0
- morphogen/contact/kernels.py +827 -0
- morphogen/contact/periodic.py +278 -0
- morphogen/contact/problem.py +578 -0
- morphogen/contact/surfaces.py +310 -0
- morphogen/coupling/__init__.py +43 -0
- morphogen/coupling/ageing.py +143 -0
- morphogen/coupling/diffusion.py +226 -0
- morphogen/coupling/fenicsx.py +484 -0
- morphogen/coupling/oxidation.py +447 -0
- morphogen/coupling/plates.py +247 -0
- morphogen/design/__init__.py +51 -0
- morphogen/design/csp.py +616 -0
- morphogen/design/frames.py +277 -0
- morphogen/design/search.py +287 -0
- morphogen/design/tiles.py +336 -0
- morphogen/distributions.py +312 -0
- morphogen/domain.py +203 -0
- morphogen/dynamics/__init__.py +179 -0
- morphogen/dynamics/model.py +638 -0
- morphogen/dynamics/openradioss.py +643 -0
- morphogen/dynamics/results.py +515 -0
- morphogen/fracture/__init__.py +170 -0
- morphogen/fracture/bulk.py +349 -0
- morphogen/fracture/cohesive.py +1614 -0
- morphogen/fracture/ductile.py +557 -0
- morphogen/fracture/lefm.py +1168 -0
- morphogen/fracture/mixed.py +1178 -0
- morphogen/fracture/periodic.py +733 -0
- morphogen/fracture/phasefield.py +714 -0
- morphogen/fracture/phasefield_finite.py +368 -0
- morphogen/fracture/surface.py +572 -0
- morphogen/fracture/toughness.py +566 -0
- morphogen/gpu.py +108 -0
- morphogen/gui/__init__.py +8 -0
- morphogen/gui/api.py +1927 -0
- morphogen/gui/desktop.py +387 -0
- morphogen/gui/icon.png +0 -0
- morphogen/gui/index.html +1909 -0
- morphogen/gui/server.py +229 -0
- morphogen/gui/tools/__init__.py +629 -0
- morphogen/gui/tools/compare.py +149 -0
- morphogen/gui/tools/contact.py +391 -0
- morphogen/gui/tools/couplings.py +1085 -0
- morphogen/gui/tools/crackfront.py +245 -0
- morphogen/gui/tools/design.py +224 -0
- morphogen/gui/tools/dynamics.py +824 -0
- morphogen/gui/tools/fracture.py +2586 -0
- morphogen/gui/tools/interop.py +1654 -0
- morphogen/gui/tools/learning.py +595 -0
- morphogen/gui/tools/mechanics.py +3881 -0
- morphogen/gui/tools/nonlocal_damage.py +1321 -0
- morphogen/gui/tools/phasefield.py +215 -0
- morphogen/gui/tools/physics.py +597 -0
- morphogen/gui/tools/stability.py +1756 -0
- morphogen/gui/tools/statistics.py +1408 -0
- morphogen/gui/viewer.js +1100 -0
- morphogen/homogenization/__init__.py +210 -0
- morphogen/homogenization/acoustics.py +566 -0
- morphogen/homogenization/amg.py +1005 -0
- morphogen/homogenization/apparent.py +1046 -0
- morphogen/homogenization/behaviours.cu +2285 -0
- morphogen/homogenization/behaviours.py +944 -0
- morphogen/homogenization/beremin.py +263 -0
- morphogen/homogenization/bifurcation.py +1085 -0
- morphogen/homogenization/bloch.py +507 -0
- morphogen/homogenization/bounds.py +226 -0
- morphogen/homogenization/core.py +458 -0
- morphogen/homogenization/coupled.py +1135 -0
- morphogen/homogenization/crystal.py +2967 -0
- morphogen/homogenization/dilatation.py +454 -0
- morphogen/homogenization/elements.py +157 -0
- morphogen/homogenization/fatigue.py +768 -0
- morphogen/homogenization/fe.py +1739 -0
- morphogen/homogenization/fenicsx.py +307 -0
- morphogen/homogenization/fft.py +499 -0
- morphogen/homogenization/fft_nonlinear.py +391 -0
- morphogen/homogenization/finite.py +613 -0
- morphogen/homogenization/flow.py +361 -0
- morphogen/homogenization/frames.py +1125 -0
- morphogen/homogenization/gpu.py +1241 -0
- morphogen/homogenization/harmonic.py +427 -0
- morphogen/homogenization/identification.py +579 -0
- morphogen/homogenization/kernels.py +760 -0
- morphogen/homogenization/laws.py +2061 -0
- morphogen/homogenization/materials.py +1080 -0
- morphogen/homogenization/mfront.py +589 -0
- morphogen/homogenization/networks.py +880 -0
- morphogen/homogenization/nonlinear.py +2612 -0
- morphogen/homogenization/ntfa.py +577 -0
- morphogen/homogenization/paths.py +476 -0
- morphogen/homogenization/photonic.py +467 -0
- morphogen/homogenization/poroelastic.py +212 -0
- morphogen/homogenization/porous.py +1120 -0
- morphogen/homogenization/postprocess.py +290 -0
- morphogen/homogenization/solvers.py +1140 -0
- morphogen/homogenization/tensors.py +240 -0
- morphogen/homogenization/voxel_fe.py +457 -0
- morphogen/homogenization/xfem.py +1016 -0
- morphogen/homogenization/zmat.py +1258 -0
- morphogen/iga/__init__.py +48 -0
- morphogen/iga/analysis.py +508 -0
- morphogen/iga/basis.py +214 -0
- morphogen/iga/geometry.py +501 -0
- morphogen/iga/homogenization.py +219 -0
- morphogen/iga/optimization.py +700 -0
- morphogen/iga/shapes.py +331 -0
- morphogen/image.py +128 -0
- morphogen/io/__init__.py +246 -0
- morphogen/io/amitex.py +741 -0
- morphogen/io/ebsd.py +881 -0
- morphogen/io/export.py +196 -0
- morphogen/io/geometry.py +158 -0
- morphogen/io/medit.py +147 -0
- morphogen/io/meshes.py +844 -0
- morphogen/io/metaimage.py +97 -0
- morphogen/io/mfront.py +1104 -0
- morphogen/io/native.py +57 -0
- morphogen/io/radioss.py +764 -0
- morphogen/io/solvers.py +371 -0
- morphogen/io/tables.py +278 -0
- morphogen/io/tiff.py +66 -0
- morphogen/io/vtk.py +701 -0
- morphogen/io/xdmf.py +200 -0
- morphogen/kernels/__init__.py +5 -0
- morphogen/kernels/annealing.py +192 -0
- morphogen/kernels/cells.py +69 -0
- morphogen/kernels/collective.py +87 -0
- morphogen/kernels/force_biased.py +595 -0
- morphogen/kernels/implicit.py +145 -0
- morphogen/kernels/leaves.py +132 -0
- morphogen/kernels/packing.py +304 -0
- morphogen/kernels/raster.py +279 -0
- morphogen/kernels/rsa.py +371 -0
- morphogen/kernels/union.py +135 -0
- morphogen/kernels/watershed.py +190 -0
- morphogen/laser/__init__.py +234 -0
- morphogen/laser/architectures.py +375 -0
- morphogen/laser/bending.py +216 -0
- morphogen/laser/cells.py +1694 -0
- morphogen/laser/design.py +940 -0
- morphogen/laser/figures.py +227 -0
- morphogen/laser/heat.py +804 -0
- morphogen/laser/materials.py +691 -0
- morphogen/laser/mechanics.py +1670 -0
- morphogen/laser/metallurgy.py +615 -0
- morphogen/laser/patterns.py +828 -0
- morphogen/laser/periodic.py +392 -0
- morphogen/laser/process.py +362 -0
- morphogen/laser/run.py +534 -0
- morphogen/laser/solid.py +216 -0
- morphogen/laser/specimen.py +200 -0
- morphogen/laser/structures.py +708 -0
- morphogen/learning/__init__.py +81 -0
- morphogen/learning/behaviour.py +398 -0
- morphogen/learning/data.py +328 -0
- morphogen/learning/learned.cpp +668 -0
- morphogen/learning/logarithmic.py +202 -0
- morphogen/learning/mfront.py +409 -0
- morphogen/learning/networks.py +767 -0
- morphogen/learning/run.py +212 -0
- morphogen/learning/standard.py +814 -0
- morphogen/learning/training.py +235 -0
- morphogen/mesh/__init__.py +29 -0
- morphogen/mesh/conforming.py +1423 -0
- morphogen/mesh/curved.py +600 -0
- morphogen/mesh/exact.py +1717 -0
- morphogen/mesh/octree.py +350 -0
- morphogen/mesh/polycrystal.py +1041 -0
- morphogen/mesh/polyhedra.py +677 -0
- morphogen/mesh/regularize.py +570 -0
- morphogen/mesh/surface.py +97 -0
- morphogen/mesh/tetra.py +512 -0
- morphogen/mesh/voxel.py +124 -0
- morphogen/micromorphic/__init__.py +31 -0
- morphogen/micromorphic/damage.py +359 -0
- morphogen/micromorphic/ductile.py +534 -0
- morphogen/micromorphic/finite.py +220 -0
- morphogen/micromorphic/microfoam.py +787 -0
- morphogen/micromorphic/space.py +341 -0
- morphogen/models/__init__.py +195 -0
- morphogen/models/affine.py +384 -0
- morphogen/models/automata.py +592 -0
- morphogen/models/base.py +500 -0
- morphogen/models/boolean.py +310 -0
- morphogen/models/bouligand.py +331 -0
- morphogen/models/branching.py +241 -0
- morphogen/models/combination.py +430 -0
- morphogen/models/deadleaves.py +766 -0
- morphogen/models/design.py +289 -0
- morphogen/models/ebsd.py +206 -0
- morphogen/models/fibers.py +1476 -0
- morphogen/models/foam.py +594 -0
- morphogen/models/gaussian.py +314 -0
- morphogen/models/growth.py +459 -0
- morphogen/models/hardcore.py +317 -0
- morphogen/models/hierarchical.py +269 -0
- morphogen/models/honeycomb.py +749 -0
- morphogen/models/implicit.py +177 -0
- morphogen/models/laminate.py +470 -0
- morphogen/models/lattice.py +351 -0
- morphogen/models/lattice_field.py +345 -0
- morphogen/models/levelset_foam.py +563 -0
- morphogen/models/nacre.py +411 -0
- morphogen/models/nested.py +924 -0
- morphogen/models/osteons.py +374 -0
- morphogen/models/pieces.py +167 -0
- morphogen/models/planar.py +126 -0
- morphogen/models/reconstruction.py +203 -0
- morphogen/models/scale_rich.py +1409 -0
- morphogen/models/scale_rich_3d.py +1616 -0
- morphogen/models/tessellation.py +735 -0
- morphogen/models/tiling_field.py +302 -0
- morphogen/models/tpms.py +197 -0
- morphogen/models/transparent.py +406 -0
- morphogen/models/union.py +141 -0
- morphogen/optimization/__init__.py +43 -0
- morphogen/optimization/cell.py +295 -0
- morphogen/optimization/filters.py +115 -0
- morphogen/optimization/mma.py +226 -0
- morphogen/optimization/run.py +219 -0
- morphogen/optimization/topology.py +632 -0
- morphogen/orientations.py +568 -0
- morphogen/parallel.py +599 -0
- morphogen/pipeline.py +978 -0
- morphogen/primitives.py +188 -0
- morphogen/results.py +564 -0
- morphogen/rve/__init__.py +73 -0
- morphogen/rve/report.py +627 -0
- morphogen/rve/scaling.py +363 -0
- morphogen/rve/study.py +1054 -0
- morphogen/rve/windows.py +345 -0
- morphogen/seeding.py +51 -0
- morphogen/settings.py +118 -0
- morphogen/stopping.py +48 -0
- morphogen/symmetry.py +384 -0
- morphogen/templates/activator_inhibitor.yaml +22 -0
- morphogen/templates/aggregation.yaml +23 -0
- morphogen/templates/boolean_cylinders.yaml +20 -0
- morphogen/templates/boolean_spheres.yaml +22 -0
- morphogen/templates/bouligand.yaml +28 -0
- morphogen/templates/cellular_automaton.yaml +25 -0
- morphogen/templates/coated_spheres.yaml +26 -0
- morphogen/templates/contact_fibers.yaml +27 -0
- morphogen/templates/cox_nanocomposite.yaml +22 -0
- morphogen/templates/cross_linked_fibers.yaml +26 -0
- morphogen/templates/curved_arcs.yaml +28 -0
- morphogen/templates/curved_fibers.yaml +24 -0
- morphogen/templates/dead_leaves.yaml +22 -0
- morphogen/templates/delaunay_network.yaml +22 -0
- morphogen/templates/dense_packing.yaml +22 -0
- morphogen/templates/diffusion.yaml +33 -0
- morphogen/templates/ebsd.yaml +34 -0
- morphogen/templates/ebsd_map.ctf +3087 -0
- morphogen/templates/fracture_composite.yaml +42 -0
- morphogen/templates/fracture_rubber.yaml +47 -0
- morphogen/templates/gaussian_excursion.yaml +17 -0
- morphogen/templates/graded_lattice.yaml +24 -0
- morphogen/templates/hardcore_fibers.yaml +22 -0
- morphogen/templates/hardcore_spheres.yaml +22 -0
- morphogen/templates/hierarchical_lattice.yaml +22 -0
- morphogen/templates/homogenization_acoustic_foam.yaml +36 -0
- morphogen/templates/homogenization_beremin.yaml +50 -0
- morphogen/templates/homogenization_cleavage.yaml +51 -0
- morphogen/templates/homogenization_cleavage_band.yaml +55 -0
- morphogen/templates/homogenization_contact.yaml +43 -0
- morphogen/templates/homogenization_crack_closure.yaml +49 -0
- morphogen/templates/homogenization_crystal_plasticity.yaml +50 -0
- morphogen/templates/homogenization_crystal_plasticity_fft.yaml +52 -0
- morphogen/templates/homogenization_crystallization.yaml +30 -0
- morphogen/templates/homogenization_debonding.yaml +43 -0
- morphogen/templates/homogenization_detwinning.yaml +58 -0
- morphogen/templates/homogenization_ductile_damage.yaml +47 -0
- morphogen/templates/homogenization_fatigue.yaml +40 -0
- morphogen/templates/homogenization_grain_fracture.yaml +56 -0
- morphogen/templates/homogenization_gyroid.yaml +30 -0
- morphogen/templates/homogenization_intergranular.yaml +43 -0
- morphogen/templates/homogenization_interphase.yaml +42 -0
- morphogen/templates/homogenization_mixed_loading.yaml +33 -0
- morphogen/templates/homogenization_ntfa.yaml +56 -0
- morphogen/templates/homogenization_piezoelectric.yaml +27 -0
- morphogen/templates/homogenization_polycrystal.yaml +30 -0
- morphogen/templates/homogenization_polycrystal_inclusions.yaml +30 -0
- morphogen/templates/homogenization_poroelastic.yaml +26 -0
- morphogen/templates/homogenization_rubber.yaml +40 -0
- morphogen/templates/homogenization_slip_cleavage.yaml +50 -0
- morphogen/templates/homogenization_spheres.yaml +31 -0
- morphogen/templates/homogenization_transport.yaml +29 -0
- morphogen/templates/homogenization_twinning.yaml +54 -0
- morphogen/templates/homogenization_viscoelastic.yaml +34 -0
- morphogen/templates/homogenization_viscoplastic.yaml +42 -0
- morphogen/templates/homogenization_voronoi.yaml +26 -0
- morphogen/templates/homogenization_xfem.yaml +23 -0
- morphogen/templates/hyperuniform_cells.yaml +24 -0
- morphogen/templates/laguerre.yaml +18 -0
- morphogen/templates/laser_bending.yaml +35 -0
- morphogen/templates/laser_cells.yaml +38 -0
- morphogen/templates/laser_corrugated.yaml +36 -0
- morphogen/templates/level_set_foam.yaml +29 -0
- morphogen/templates/menger_sponge.yaml +17 -0
- morphogen/templates/nacre.yaml +25 -0
- morphogen/templates/nucleation_growth.yaml +25 -0
- morphogen/templates/optimize_auxetic.yaml +29 -0
- morphogen/templates/optimize_auxetic_3d.yaml +27 -0
- morphogen/templates/optimize_bulk.yaml +23 -0
- morphogen/templates/optimize_conductivity.yaml +20 -0
- morphogen/templates/osteons.yaml +28 -0
- morphogen/templates/penetrable_spheres.yaml +21 -0
- morphogen/templates/poisson_fibers.yaml +21 -0
- morphogen/templates/random_laminate.yaml +26 -0
- morphogen/templates/random_lattice.yaml +27 -0
- morphogen/templates/random_walk_fibers.yaml +26 -0
- morphogen/templates/reaction_diffusion.yaml +22 -0
- morphogen/templates/reconstruction.yaml +22 -0
- morphogen/templates/rve_cellular_automaton.yaml +23 -0
- morphogen/templates/rve_crystal_plasticity_fft.yaml +51 -0
- morphogen/templates/rve_multiphysics.yaml +43 -0
- morphogen/templates/rve_poisson_fibers.yaml +22 -0
- morphogen/templates/rve_poisson_fibers_fe.yaml +33 -0
- morphogen/templates/rve_voronoi_windows.yaml +20 -0
- morphogen/templates/scale_rich_network.yaml +29 -0
- morphogen/templates/scale_rich_plates.yaml +30 -0
- morphogen/templates/sequential_dead_leaves.yaml +25 -0
- morphogen/templates/strut_lattice.yaml +21 -0
- morphogen/templates/tile_assembly.yaml +28 -0
- morphogen/templates/tpms.yaml +19 -0
- morphogen/templates/trabecular_bone.yaml +29 -0
- morphogen/templates/transparent_dead_leaves.yaml +23 -0
- morphogen/templates/vascular_tree.yaml +23 -0
- morphogen/templates/voronoi.yaml +20 -0
- morphogen/templates/wood.yaml +34 -0
- morphogen/templates/workflow.yaml +56 -0
- morphogen/texture.py +665 -0
- morphogen/theory.py +162 -0
- morphogen/viz/__init__.py +12 -0
- morphogen/viz/export.py +244 -0
- morphogen/viz/figures.py +707 -0
- morphogen/viz/png.py +148 -0
- morphogen/viz/sections.py +696 -0
- morphogen/viz/surfaces.py +1352 -0
- morphogen/viz/video.py +904 -0
- morphogen/viz/workflow.py +488 -0
- morphogen-1.0.0.dist-info/METADATA +2316 -0
- morphogen-1.0.0.dist-info/RECORD +381 -0
- morphogen-1.0.0.dist-info/WHEEL +4 -0
- morphogen-1.0.0.dist-info/entry_points.txt +5 -0
- morphogen-1.0.0.dist-info/licenses/LICENSE +674 -0
morphogen/iga/shapes.py
ADDED
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
"""Geometries for isogeometric analyses: patches and assemblies with exact arcs.
|
|
2
|
+
|
|
3
|
+
A circular arc of half-angle ``a`` is a rational curve of degree 2: its ends, the point
|
|
4
|
+
where their tangents meet, and the weights ``(1, cos a, 1)`` (Piegl & Tiller 1997, ch.
|
|
5
|
+
7); an ellipse is the image of a circle by a linear map of its control points. The
|
|
6
|
+
patches here are *ruled* between two such curves — linear across, of the degree of the
|
|
7
|
+
curves along — with positive Jacobians:
|
|
8
|
+
|
|
9
|
+
* :func:`rectangle`, :func:`box`: a rectangle or a box, of any degree;
|
|
10
|
+
* :func:`annulus`: a sector of a ring (a thick-walled cylinder in plane strain);
|
|
11
|
+
* :func:`plate_with_hole`: a quarter of a square plate with a central hole, two patches
|
|
12
|
+
(the benchmark of Kirsch's solution, and of shape optimization);
|
|
13
|
+
* :func:`cell_with_hole`: a rectangular cell with a hole, four patches around it, whose
|
|
14
|
+
opposite sides match: a periodic cell (porous and architectured materials), the hole
|
|
15
|
+
a circle, an ellipse, or any closed curve through its control points; the cell may
|
|
16
|
+
be a parallelogram (the rhombus of a hexagonal array of holes);
|
|
17
|
+
* :func:`alternating_holes`: a sheet with elongated holes at the nodes of a square
|
|
18
|
+
lattice, turned in turn along its two axes — the pattern of the *rotating squares*,
|
|
19
|
+
auxetic when its ligaments are thin (Grima & Evans 2000; Bertoldi et al. 2010;
|
|
20
|
+
Taylor et al. 2014);
|
|
21
|
+
* :func:`tiling`: cells side by side as one assembly (several holes per period);
|
|
22
|
+
:func:`repeated`: a cell and its neighbours along its periods;
|
|
23
|
+
* :func:`extruded`: a surface swept along the third axis into a solid.
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
from __future__ import annotations
|
|
27
|
+
|
|
28
|
+
import math
|
|
29
|
+
|
|
30
|
+
import numpy as np
|
|
31
|
+
|
|
32
|
+
from morphogen.iga.basis import open_knots
|
|
33
|
+
from morphogen.iga.geometry import Multipatch, Patch
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def rectangle(lower=(0.0, 0.0), upper=(1.0, 1.0), degree: int = 1, phase: int = 0) -> Patch:
|
|
37
|
+
"""The rectangle of corners ``lower`` and ``upper``: one element of ``degree``."""
|
|
38
|
+
lower, upper = np.asarray(lower, dtype=float), np.asarray(upper, dtype=float)
|
|
39
|
+
n = int(degree) + 1
|
|
40
|
+
axes = [np.linspace(lower[d], upper[d], n) for d in range(2)]
|
|
41
|
+
points = np.stack(np.meshgrid(*axes, indexing="ij"), axis=-1)
|
|
42
|
+
knots = open_knots(n, degree)
|
|
43
|
+
return Patch((degree, degree), (knots, knots), points, None, phase)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def box(lower=(0.0, 0.0, 0.0), upper=(1.0, 1.0, 1.0), degree: int = 1, phase: int = 0) -> Patch:
|
|
47
|
+
"""The box of corners ``lower`` and ``upper``: one element of ``degree``."""
|
|
48
|
+
lower, upper = np.asarray(lower, dtype=float), np.asarray(upper, dtype=float)
|
|
49
|
+
n = int(degree) + 1
|
|
50
|
+
axes = [np.linspace(lower[d], upper[d], n) for d in range(3)]
|
|
51
|
+
points = np.stack(np.meshgrid(*axes, indexing="ij"), axis=-1)
|
|
52
|
+
knots = open_knots(n, degree)
|
|
53
|
+
return Patch((degree,) * 3, (knots,) * 3, points, None, phase)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def arc(
|
|
57
|
+
radius: float, start: float, end: float, centre=(0.0, 0.0)
|
|
58
|
+
) -> tuple[np.ndarray, np.ndarray]:
|
|
59
|
+
"""The three control points and weights of the circular arc from the angle ``start``
|
|
60
|
+
to ``end`` (less than a half-turn apart): exact, of degree 2."""
|
|
61
|
+
half = 0.5 * (end - start)
|
|
62
|
+
if not 0.0 < abs(half) < 0.5 * math.pi:
|
|
63
|
+
raise ValueError("arc: an angle between 0 and a half-turn")
|
|
64
|
+
middle = 0.5 * (start + end)
|
|
65
|
+
centre = np.asarray(centre, dtype=float)
|
|
66
|
+
points = np.array(
|
|
67
|
+
[
|
|
68
|
+
[radius * math.cos(start), radius * math.sin(start)],
|
|
69
|
+
[
|
|
70
|
+
radius / math.cos(half) * math.cos(middle),
|
|
71
|
+
radius / math.cos(half) * math.sin(middle),
|
|
72
|
+
],
|
|
73
|
+
[radius * math.cos(end), radius * math.sin(end)],
|
|
74
|
+
]
|
|
75
|
+
)
|
|
76
|
+
return points + centre, np.array([1.0, math.cos(half), 1.0])
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def ruled(first, second, weights_first=None, weights_second=None, degree: int = 2,
|
|
80
|
+
phase: int = 0) -> Patch: # fmt: skip
|
|
81
|
+
"""The patch between two curves of the same degree given by their control points
|
|
82
|
+
(``n x 2`` each, without inner knots): direction 0 along the curves, direction 1
|
|
83
|
+
from the first to the second (linear)."""
|
|
84
|
+
first, second = np.asarray(first, dtype=float), np.asarray(second, dtype=float)
|
|
85
|
+
n = len(first)
|
|
86
|
+
if first.shape != second.shape or n != int(degree) + 1:
|
|
87
|
+
raise ValueError("ruled: two curves of degree + 1 control points")
|
|
88
|
+
w1 = np.ones(n) if weights_first is None else np.asarray(weights_first, dtype=float)
|
|
89
|
+
w2 = np.ones(n) if weights_second is None else np.asarray(weights_second, dtype=float)
|
|
90
|
+
points = np.stack([first, second], axis=1)
|
|
91
|
+
weights = np.stack([w1, w2], axis=1)
|
|
92
|
+
return Patch((degree, 1), (open_knots(n, degree), open_knots(2, 1)), points, weights, phase)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def annulus(inner: float, outer: float, angle: float = 0.5 * math.pi, start: float = 0.0,
|
|
96
|
+
centre=(0.0, 0.0), phase: int = 0) -> Patch: # fmt: skip
|
|
97
|
+
"""A sector of the ring between the radii ``inner`` and ``outer``, from the angle
|
|
98
|
+
``start`` over ``angle`` (less than a half-turn): direction 0 along the arcs
|
|
99
|
+
(clockwise, so that the Jacobian is positive), direction 1 from the inner to the
|
|
100
|
+
outer radius."""
|
|
101
|
+
if not 0.0 < inner < outer:
|
|
102
|
+
raise ValueError("annulus: 0 < inner < outer")
|
|
103
|
+
p1, w1 = arc(inner, start + angle, start, centre)
|
|
104
|
+
p2, w2 = arc(outer, start + angle, start, centre)
|
|
105
|
+
return ruled(p1, p2, w1, w2, phase=phase)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def plate_with_hole(radius: float = 1.0, half_width: float = 4.0, phase: int = 0) -> Multipatch:
|
|
109
|
+
"""A quarter (``x, y >= 0``) of a square plate of half-width ``half_width`` with a
|
|
110
|
+
circular hole of ``radius`` at the origin: two patches, from the right and upper
|
|
111
|
+
sides (direction 1 = 0) to the arcs of the hole (direction 1 = 1), along which
|
|
112
|
+
direction 0 turns anticlockwise."""
|
|
113
|
+
if not 0.0 < radius < half_width:
|
|
114
|
+
raise ValueError("plate_with_hole: 0 < radius < half_width")
|
|
115
|
+
size = float(half_width)
|
|
116
|
+
quarter = 0.25 * math.pi
|
|
117
|
+
sides = [
|
|
118
|
+
np.array([[size, 0.0], [size, 0.5 * size], [size, size]]),
|
|
119
|
+
np.array([[size, size], [0.5 * size, size], [0.0, size]]),
|
|
120
|
+
]
|
|
121
|
+
patches = []
|
|
122
|
+
for k, side in enumerate(sides):
|
|
123
|
+
points, weights = arc(radius, k * quarter, (k + 1) * quarter)
|
|
124
|
+
patches.append(ruled(side, points, None, weights, phase=phase))
|
|
125
|
+
return Multipatch(patches)
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def _ellipse(hole) -> np.ndarray:
|
|
129
|
+
"""The linear map of the unit circle onto an ellipse: ``hole`` a radius, or ``(a,
|
|
130
|
+
b)`` or ``(a, b, angle)`` its half-axes and the inclination of the first."""
|
|
131
|
+
values = (float(hole),) * 2 if np.isscalar(hole) else tuple(float(v) for v in hole)
|
|
132
|
+
if len(values) not in (2, 3) or values[0] <= 0 or values[1] <= 0:
|
|
133
|
+
raise ValueError("hole: a radius, or the positive half-axes of an ellipse (and its angle)")
|
|
134
|
+
tilt = values[2] if len(values) > 2 else 0.0
|
|
135
|
+
rotation = np.array([[math.cos(tilt), -math.sin(tilt)], [math.sin(tilt), math.cos(tilt)]])
|
|
136
|
+
return rotation @ np.diag(values[:2]) @ rotation.T
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def _ellipse_arc(shape: np.ndarray, centre, start: float, end: float):
|
|
140
|
+
"""The exact arc of the ellipse that ``shape`` makes of the unit circle, between two
|
|
141
|
+
angles of the circle: control points and weights."""
|
|
142
|
+
points, weights = arc(1.0, start, end)
|
|
143
|
+
return points @ shape.T + np.asarray(centre, dtype=float), weights
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def _hole_arcs(hole, centre: np.ndarray, corners=None) -> list[tuple[np.ndarray, np.ndarray]]:
|
|
147
|
+
"""The four arcs (control points, weights) of a hole around ``centre``: from the
|
|
148
|
+
angles -45 to 45 degrees, then 45 to 135... (between the directions of the
|
|
149
|
+
``corners`` of the cell, when given). ``hole``: a radius, ``(a, b)`` or ``(a, b,
|
|
150
|
+
angle)`` the half-axes of an ellipse and its inclination, or the 8 control points of
|
|
151
|
+
a closed curve of four quadratic arcs (corner points included) with their weights."""
|
|
152
|
+
if np.isscalar(hole) or np.ndim(hole) == 1:
|
|
153
|
+
shape = _ellipse(hole)
|
|
154
|
+
if corners is None:
|
|
155
|
+
angles = (2 * np.arange(5) - 1) * 0.25 * math.pi
|
|
156
|
+
else: # towards the corners: the angles of the circle that the ellipse is made of
|
|
157
|
+
directions = (np.asarray(corners) - centre) @ np.linalg.inv(shape).T
|
|
158
|
+
angles = np.unwrap(np.arctan2(directions[:, 1], directions[:, 0]))
|
|
159
|
+
angles = np.append(angles, angles[0] + 2 * math.pi)
|
|
160
|
+
return [_ellipse_arc(shape, centre, angles[k], angles[k + 1]) for k in range(4)]
|
|
161
|
+
hole = np.asarray(hole, dtype=float)
|
|
162
|
+
if hole.shape != (8, 3):
|
|
163
|
+
raise ValueError(
|
|
164
|
+
"hole: a radius, the half-axes (and inclination) of an ellipse, or 8 control "
|
|
165
|
+
"points with their weights (x, y, w), anticlockwise from the angle -45 degrees"
|
|
166
|
+
)
|
|
167
|
+
arcs = []
|
|
168
|
+
for k in range(4):
|
|
169
|
+
rows = [2 * k, 2 * k + 1, (2 * k + 2) % 8]
|
|
170
|
+
arcs.append((hole[rows, :2], hole[rows, 2]))
|
|
171
|
+
return arcs
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def cell_with_hole(lengths=(1.0, 1.0), hole=0.25, elements: int = 1, origin=(0.0, 0.0),
|
|
175
|
+
phase: int = 0) -> Multipatch: # fmt: skip
|
|
176
|
+
"""The rectangular cell of ``lengths`` (its lower corner at ``origin``) with a hole
|
|
177
|
+
at its centre (see :func:`_hole_arcs`: a radius, an ellipse, or a closed curve): four
|
|
178
|
+
patches, one per side of the cell, from the side (direction 1 = 0) to the arc of the
|
|
179
|
+
hole that faces it (direction 1 = 1), direction 0 anticlockwise; ``elements`` along
|
|
180
|
+
each arc. Opposite sides have matching control points: a periodic cell.
|
|
181
|
+
|
|
182
|
+
``lengths`` may be the two vectors of a parallelogram instead (2 x 2, one per row,
|
|
183
|
+
anticlockwise): the cell of an oblique lattice, which the assembly then carries
|
|
184
|
+
(``lattice``) — ``[[1, 0], [0.5, sqrt(3) / 2]]`` for a hexagonal array of holes."""
|
|
185
|
+
lengths = np.asarray(lengths, dtype=float)
|
|
186
|
+
origin = np.asarray(origin, dtype=float)
|
|
187
|
+
lattice = None
|
|
188
|
+
if lengths.ndim == 2:
|
|
189
|
+
lattice = lengths
|
|
190
|
+
if lattice.shape != (2, 2) or np.linalg.det(lattice) <= 0:
|
|
191
|
+
raise ValueError("cell_with_hole: two vectors of the plane, anticlockwise")
|
|
192
|
+
first, second = lattice
|
|
193
|
+
else:
|
|
194
|
+
first, second = np.array([lengths[0], 0.0]), np.array([0.0, lengths[1]])
|
|
195
|
+
centre = origin + 0.5 * (first + second)
|
|
196
|
+
corners = origin + np.array([first, first + second, second, 0.0 * first])
|
|
197
|
+
patches = []
|
|
198
|
+
arcs = _hole_arcs(hole, centre, None if lattice is None else corners)
|
|
199
|
+
for k, (points, weights) in enumerate(arcs):
|
|
200
|
+
a, b = corners[k], corners[(k + 1) % 4]
|
|
201
|
+
side = np.array([a, 0.5 * (a + b), b])
|
|
202
|
+
patch = ruled(side, points, None, weights, phase=phase)
|
|
203
|
+
patches.append(patch.refined((int(elements), 1)) if elements > 1 else patch)
|
|
204
|
+
return Multipatch(patches, lattice=lattice)
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def alternating_holes(spacing: float = 1.0, hole=(0.35, 0.15), elements: int = 1,
|
|
208
|
+
phase: int = 0) -> Multipatch: # fmt: skip
|
|
209
|
+
"""A sheet with elliptic holes at the nodes of a square lattice of ``spacing``
|
|
210
|
+
along x and y, elongated in turn along x and along y: ``hole = (a, b)``, the
|
|
211
|
+
half-axes along and across a hole (``a + b < spacing``; a radius for circles), the
|
|
212
|
+
hole at the origin along x. Between four holes the sheet is a square grain, held to
|
|
213
|
+
its four neighbours by the ligaments left between the tip of a hole and the flank of
|
|
214
|
+
the next, of width ``spacing - a - b``: thin ligaments let the grains turn, and the
|
|
215
|
+
sheet widens when pulled (the *rotating squares*).
|
|
216
|
+
|
|
217
|
+
The assembly is two grains side by side, cut nowhere — ``[0, 2 spacing] x [0,
|
|
218
|
+
spacing]`` without its quarters of holes — whose periods are ``(spacing, spacing)``
|
|
219
|
+
and ``(spacing, -spacing)`` (its ``lattice``): two holes a period. Each grain is
|
|
220
|
+
eight patches around its centre, two along each of its quarters of holes, from the
|
|
221
|
+
arc (direction 1 = 0) to a curve between the centre of the grain and the middle of
|
|
222
|
+
a ligament, which it crosses squarely (direction 1 = 1), with ``elements`` along each
|
|
223
|
+
arc. The patches 0 and 1 are those of the hole at the origin: their arcs run from
|
|
224
|
+
its flank ``(0, b)`` to a point ``p``, then from ``p`` to its tip ``(a, 0)``, facing
|
|
225
|
+
the curves from the middle of the left ligament to the centre and from the centre
|
|
226
|
+
to the middle of the lower ligament; the other quarters are their images by the
|
|
227
|
+
symmetries of the pattern (the mirrors x = 0 and y = 0, the quarter-turn about the
|
|
228
|
+
centre of a grain)."""
|
|
229
|
+
d = float(spacing)
|
|
230
|
+
shape = _ellipse(hole)
|
|
231
|
+
a, b = float(shape[0, 0]), float(shape[1, 1])
|
|
232
|
+
if np.ndim(hole) == 1 and len(hole) > 2:
|
|
233
|
+
raise ValueError("alternating_holes: hole = (a, b), the half-axes along and across")
|
|
234
|
+
if not a + b < d:
|
|
235
|
+
raise ValueError("alternating_holes: a + b < spacing (holes apart)")
|
|
236
|
+
centre = np.array([0.5 * d, 0.5 * d])
|
|
237
|
+
middle = np.array([0.5 * (a + d - b), 0.0]) # of the ligament of the lower side
|
|
238
|
+
left = np.array([0.0, 0.5 * (b + d - a)]) # of that of the left side
|
|
239
|
+
# the point of the arc of the hole seen from the centre half-way between them
|
|
240
|
+
angle = math.atan2(middle[1] - centre[1], middle[0] - centre[0]) - 0.25 * math.pi
|
|
241
|
+
ray = np.array([math.cos(angle), math.sin(angle)])
|
|
242
|
+
roots = np.roots(
|
|
243
|
+
[
|
|
244
|
+
(ray[0] / a) ** 2 + (ray[1] / b) ** 2,
|
|
245
|
+
2.0 * (centre[0] * ray[0] / a**2 + centre[1] * ray[1] / b**2),
|
|
246
|
+
(centre[0] / a) ** 2 + (centre[1] / b) ** 2 - 1.0,
|
|
247
|
+
]
|
|
248
|
+
)
|
|
249
|
+
hit = centre + float(np.min(roots.real)) * ray
|
|
250
|
+
split = math.atan2(hit[1] / b, hit[0] / a)
|
|
251
|
+
flank, flank_weights = _ellipse_arc(shape, (0.0, 0.0), 0.5 * math.pi, split)
|
|
252
|
+
tip, tip_weights = _ellipse_arc(shape, (0.0, 0.0), split, 0.0)
|
|
253
|
+
# from the centre to the middle of a ligament, which the curve crosses squarely
|
|
254
|
+
pieces = [
|
|
255
|
+
(flank, flank_weights, np.array([left, [0.25 * d, left[1]], centre])),
|
|
256
|
+
(tip, tip_weights, np.array([centre, [middle[0], 0.25 * d], middle])),
|
|
257
|
+
]
|
|
258
|
+
turn = np.array([[0.0, -1.0], [1.0, 0.0]])
|
|
259
|
+
patches = []
|
|
260
|
+
for mirrored in (False, True):
|
|
261
|
+
for k in range(4):
|
|
262
|
+
rotation = np.linalg.matrix_power(turn, k)
|
|
263
|
+
for curve, weights, line in pieces:
|
|
264
|
+
first = centre + (curve - centre) @ rotation.T
|
|
265
|
+
second = centre + (line - centre) @ rotation.T
|
|
266
|
+
if mirrored: # the second grain: the mirror image in the line x = spacing
|
|
267
|
+
first = (first * [-1.0, 1.0] + [2.0 * d, 0.0])[::-1]
|
|
268
|
+
second = (second * [-1.0, 1.0] + [2.0 * d, 0.0])[::-1]
|
|
269
|
+
weights = weights[::-1]
|
|
270
|
+
patch = ruled(first, second, weights, None, phase=phase)
|
|
271
|
+
patches.append(patch.refined((int(elements), 1)) if elements > 1 else patch)
|
|
272
|
+
return Multipatch(patches, lattice=[[d, d], [d, -d]])
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
def tiling(cells) -> Multipatch:
|
|
276
|
+
"""The assembly of several assemblies (or patches) placed side by side: their
|
|
277
|
+
patches together, the control points of their common sides merged."""
|
|
278
|
+
patches = []
|
|
279
|
+
for cell in cells:
|
|
280
|
+
patches.extend([cell] if isinstance(cell, Patch) else cell.patches)
|
|
281
|
+
return Multipatch(patches)
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
def repeated(cell: Multipatch, counts=(2, 2)) -> Multipatch:
|
|
285
|
+
"""A cell and its neighbours: ``counts`` copies along each of its periods (those of
|
|
286
|
+
its ``lattice``, or the sides of its box), as one assembly — for figures, or a
|
|
287
|
+
larger period."""
|
|
288
|
+
lattice = np.diag(cell.upper - cell.lower) if cell.lattice is None else cell.lattice
|
|
289
|
+
counts = (int(counts),) * cell.dim if np.isscalar(counts) else tuple(int(c) for c in counts)
|
|
290
|
+
if len(counts) != cell.dim or min(counts) < 1:
|
|
291
|
+
raise ValueError("repeated: a count of 1 or more per period")
|
|
292
|
+
patches = []
|
|
293
|
+
for index in np.ndindex(*counts):
|
|
294
|
+
shift = np.asarray(index, dtype=float) @ lattice
|
|
295
|
+
patches.extend(p.with_points(p.points + shift) for p in cell.patches)
|
|
296
|
+
return Multipatch(patches, cell.tolerance, lattice=np.asarray(counts)[:, None] * lattice)
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
def extruded(surface, height: float, degree: int = 1) -> Multipatch:
|
|
300
|
+
"""The solid swept by a surface (a patch or an assembly in the plane) along the
|
|
301
|
+
third axis over ``height``: direction 2 of each patch, of ``degree``."""
|
|
302
|
+
surfaces = [surface] if isinstance(surface, Patch) else surface.patches
|
|
303
|
+
n = int(degree) + 1
|
|
304
|
+
levels = np.linspace(0.0, float(height), n)
|
|
305
|
+
patches = []
|
|
306
|
+
for p in surfaces:
|
|
307
|
+
if p.ndim != 2 or p.dim != 2:
|
|
308
|
+
raise ValueError("extruded: surfaces of the plane")
|
|
309
|
+
points = np.empty((*p.shape, n, 3))
|
|
310
|
+
points[..., :2] = p.points[:, :, None, :]
|
|
311
|
+
points[..., 2] = levels
|
|
312
|
+
weights = np.repeat(p.weights[:, :, None], n, axis=2)
|
|
313
|
+
patches.append(
|
|
314
|
+
Patch((*p.degrees, degree), (*p.knots, open_knots(n, degree)), points, weights, p.phase)
|
|
315
|
+
)
|
|
316
|
+
return Multipatch(patches)
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
__all__ = [
|
|
320
|
+
"alternating_holes",
|
|
321
|
+
"annulus",
|
|
322
|
+
"arc",
|
|
323
|
+
"box",
|
|
324
|
+
"cell_with_hole",
|
|
325
|
+
"extruded",
|
|
326
|
+
"plate_with_hole",
|
|
327
|
+
"rectangle",
|
|
328
|
+
"repeated",
|
|
329
|
+
"ruled",
|
|
330
|
+
"tiling",
|
|
331
|
+
]
|
morphogen/image.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"""Voxel images: a 3D array on a regular grid with its geometry and metadata."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
|
+
|
|
7
|
+
import numpy as np
|
|
8
|
+
|
|
9
|
+
from morphogen._utils import as_triplet
|
|
10
|
+
from morphogen.domain import Domain, Grid
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@dataclass
|
|
14
|
+
class VoxelImage:
|
|
15
|
+
"""3D image of a microstructure.
|
|
16
|
+
|
|
17
|
+
Attributes
|
|
18
|
+
----------
|
|
19
|
+
data
|
|
20
|
+
Array of shape ``(nx, ny, nz)`` indexed ``[i, j, k]`` along ``(x, y, z)``.
|
|
21
|
+
Typical contents: phase indices (``uint8``), cell labels (``int32``), partial
|
|
22
|
+
volume fractions or level-set values (``float32``).
|
|
23
|
+
spacing
|
|
24
|
+
Voxel edge lengths.
|
|
25
|
+
origin
|
|
26
|
+
Lower corner of voxel ``(0, 0, 0)`` (not its centre).
|
|
27
|
+
periodic
|
|
28
|
+
Whether the image is periodic (measurements then wrap around the faces).
|
|
29
|
+
name
|
|
30
|
+
Name of the array in exported files (e.g. ``"phase"``, ``"material"``).
|
|
31
|
+
metadata
|
|
32
|
+
JSON-able information (model, seed, version...).
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
data: np.ndarray
|
|
36
|
+
spacing: tuple[float, float, float] = (1.0, 1.0, 1.0)
|
|
37
|
+
origin: tuple[float, float, float] = (0.0, 0.0, 0.0)
|
|
38
|
+
periodic: bool = False
|
|
39
|
+
name: str = "phase"
|
|
40
|
+
metadata: dict = field(default_factory=dict)
|
|
41
|
+
|
|
42
|
+
def __post_init__(self) -> None:
|
|
43
|
+
self.data = np.asarray(self.data)
|
|
44
|
+
if self.data.ndim != 3:
|
|
45
|
+
raise ValueError(f"VoxelImage: expected a 3D array, got shape {self.data.shape}")
|
|
46
|
+
self.spacing = as_triplet(self.spacing, "spacing", positive=True)
|
|
47
|
+
self.origin = as_triplet(self.origin, "origin")
|
|
48
|
+
self.periodic = bool(self.periodic)
|
|
49
|
+
|
|
50
|
+
@classmethod
|
|
51
|
+
def from_grid(cls, data, grid: Grid, *, name: str = "phase", metadata=None) -> VoxelImage:
|
|
52
|
+
return cls(
|
|
53
|
+
data=data,
|
|
54
|
+
spacing=tuple(grid.spacing),
|
|
55
|
+
origin=tuple(grid.origin),
|
|
56
|
+
periodic=grid.domain.periodic,
|
|
57
|
+
name=name,
|
|
58
|
+
metadata=dict(metadata or {}),
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
@property
|
|
62
|
+
def shape(self) -> tuple[int, int, int]:
|
|
63
|
+
return tuple(int(n) for n in self.data.shape)
|
|
64
|
+
|
|
65
|
+
@property
|
|
66
|
+
def size(self) -> np.ndarray:
|
|
67
|
+
"""Physical extent of the image."""
|
|
68
|
+
return np.array(self.shape) * np.array(self.spacing)
|
|
69
|
+
|
|
70
|
+
@property
|
|
71
|
+
def voxel_volume(self) -> float:
|
|
72
|
+
return float(np.prod(self.spacing))
|
|
73
|
+
|
|
74
|
+
@property
|
|
75
|
+
def volume(self) -> float:
|
|
76
|
+
return float(np.prod(self.size))
|
|
77
|
+
|
|
78
|
+
@property
|
|
79
|
+
def n_voxels(self) -> int:
|
|
80
|
+
return int(self.data.size)
|
|
81
|
+
|
|
82
|
+
def domain(self, unit: str = "") -> Domain:
|
|
83
|
+
return Domain(size=tuple(self.size), origin=self.origin, periodic=self.periodic, unit=unit)
|
|
84
|
+
|
|
85
|
+
def grid(self) -> Grid:
|
|
86
|
+
return Grid(self.domain(), self.shape)
|
|
87
|
+
|
|
88
|
+
def with_data(self, data, *, name: str | None = None) -> VoxelImage:
|
|
89
|
+
"""New image on the same grid."""
|
|
90
|
+
return VoxelImage(
|
|
91
|
+
data=data,
|
|
92
|
+
spacing=self.spacing,
|
|
93
|
+
origin=self.origin,
|
|
94
|
+
periodic=self.periodic,
|
|
95
|
+
name=self.name if name is None else name,
|
|
96
|
+
metadata=dict(self.metadata),
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
def layer(self, axis: int = 2, index: int | None = None) -> VoxelImage:
|
|
100
|
+
"""One layer of voxels normal to ``axis`` (the middle one by default), as an image
|
|
101
|
+
one voxel thick of the same voxels: a planar section of a volume, for a study in
|
|
102
|
+
its plane (a plane strain for the mechanics) on far fewer voxels. Periodic as the
|
|
103
|
+
image (the fields then do not vary across the layer)."""
|
|
104
|
+
if axis not in (0, 1, 2):
|
|
105
|
+
raise ValueError(f"layer: axis 0, 1 or 2, got {axis!r}")
|
|
106
|
+
n = self.shape[axis]
|
|
107
|
+
k = n // 2 if index is None else int(index)
|
|
108
|
+
if not 0 <= k < n:
|
|
109
|
+
raise ValueError(f"layer: index from 0 to {n - 1}, got {index!r}")
|
|
110
|
+
data = np.take(self.data, [k], axis=axis)
|
|
111
|
+
origin = list(self.origin)
|
|
112
|
+
origin[axis] += k * self.spacing[axis]
|
|
113
|
+
return VoxelImage(np.ascontiguousarray(data), self.spacing, tuple(origin), self.periodic,
|
|
114
|
+
self.name, dict(self.metadata)) # fmt: skip
|
|
115
|
+
|
|
116
|
+
def crop(self, start, stop) -> VoxelImage:
|
|
117
|
+
"""Sub-image ``data[start[0]:stop[0], ...]`` (never periodic)."""
|
|
118
|
+
s = as_triplet(start, "start", integer=True)
|
|
119
|
+
e = as_triplet(stop, "stop", integer=True)
|
|
120
|
+
sub = self.data[s[0] : e[0], s[1] : e[1], s[2] : e[2]]
|
|
121
|
+
origin = tuple(o + i * h for o, i, h in zip(self.origin, s, self.spacing, strict=True))
|
|
122
|
+
return VoxelImage(sub, self.spacing, origin, False, self.name, dict(self.metadata))
|
|
123
|
+
|
|
124
|
+
def __repr__(self) -> str:
|
|
125
|
+
return (
|
|
126
|
+
f"VoxelImage(name={self.name!r}, shape={self.shape}, dtype={self.data.dtype}, "
|
|
127
|
+
f"spacing={self.spacing}, periodic={self.periodic})"
|
|
128
|
+
)
|