.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/TPMS/graded_cell_type.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_TPMS_graded_cell_type.py: Morphing between TPMS types =========================== Continuous morphing between Schwarz P, gyroid, Fischer-Koch S and Schwarz D. The morphing between TPMS is managed by the TPMS_TYPES dictionary that maps control points to TPMS functions. The weight function is used to determine the contribution of each TPMS function to the final surface. **Note:** The weight function is an exponential function that transitions between control points. .. GENERATED FROM PYTHON SOURCE LINES 14-106 .. code-block:: Python from __future__ import annotations from collections.abc import Callable import numpy as np import numpy.typing as npt import pyvista as pv from microgen import Tpms from microgen.shape.surface_functions import ( fischer_koch_s, gyroid, schwarz_d, schwarz_p, ) ControlPoint = tuple[float, float, float] REPEAT = (4, 4, 2) TPMS_TYPES: dict[ControlPoint, Callable] = { (-1, 0, 0): schwarz_p, (1, 0, 0): gyroid, (0, 1, 0): fischer_koch_s, (0, -1, 0): schwarz_d, } def exp_func( x: npt.NDArray[np.float64], y: npt.NDArray[np.float64], z: npt.NDArray[np.float64], ) -> npt.NDArray[np.float64]: """Transition between points using exponential function.""" k = 0.5 norm = x**2 + y**2 + z**2 return 1 + np.exp(k * norm) def weight( x: npt.NDArray[np.float64], y: npt.NDArray[np.float64], z: npt.NDArray[np.float64], control_points: list[ControlPoint], index: int, ) -> npt.NDArray[np.float64]: """Weight function for morphing between points.""" denom = np.zeros_like(x) for p in control_points: denom += exp_func(x + p[0], y + p[1], z + p[2]) point = control_points[index] return exp_func(x + point[0], y + point[1], z + point[2]) / denom def multi_morph( tpms_types: dict[ControlPoint, Callable], x: npt.NDArray[np.float64], y: npt.NDArray[np.float64], z: npt.NDArray[np.float64], ) -> npt.NDArray[np.float64]: """Morphing between multiple functions.""" control_points = list(tpms_types.keys()) surfaces = list(tpms_types.values()) result = np.zeros_like(x) for index, surface_function in enumerate(surfaces): weight_func = weight(x, y, z, control_points, index) result += weight_func * surface_function(x, y, z) return result def trigraded( x: npt.NDArray[np.float64], y: npt.NDArray[np.float64], z: npt.NDArray[np.float64], ) -> npt.NDArray[np.float64]: """Morph between three TPMS functions.""" return multi_morph( tpms_types=TPMS_TYPES, x=x, y=y, z=z, ) geometry = Tpms( surface_function=trigraded, offset=0.3, repeat_cell=REPEAT, resolution=50, ) sheet = geometry.sheet .. GENERATED FROM PYTHON SOURCE LINES 107-113 .. code-block:: Python plotter = pv.Plotter() plotter.add_mesh(sheet, smooth_shading=True, color="#ef7d2d") plotter.camera_position = "iso" plotter.camera.zoom(1.1) plotter.show_axes() plotter.show() .. image-sg:: /examples/TPMS/images/sphx_glr_graded_cell_type_001.png :alt: graded cell type :srcset: /examples/TPMS/images/sphx_glr_graded_cell_type_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 11.091 seconds) .. _sphx_glr_download_examples_TPMS_graded_cell_type.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: graded_cell_type.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: graded_cell_type.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: graded_cell_type.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_