.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/ImplicitShapes/gyroid_implicit_phase.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_ImplicitShapes_gyroid_implicit_phase.py: Gyroid as an implicit Phase =========================== End-to-end demonstration of the implicit-first pipeline of microgen 2.0, with no CAD detour. A :class:`microgen.Tpms` exposes a scalar SDF directly; wrapping it in a :class:`microgen.Phase` via :meth:`Phase.from_shape` gives access to :meth:`phase.surface_mesh`, :attr:`phase.pieces`, :attr:`phase.center_of_mass`, and the transforms :meth:`Phase.translate` / :meth:`Phase.scale`, none of which require the optional ``[cad]`` extra (no OCCT, no STEP, no gmsh). .. GENERATED FROM PYTHON SOURCE LINES 14-20 .. code-block:: Python import numpy as np import pyvista as pv from microgen import Phase, Sphere, Tpms, surface_functions .. GENERATED FROM PYTHON SOURCE LINES 21-22 Build an implicit gyroid TPMS — the field is a callable, no BREP built. .. GENERATED FROM PYTHON SOURCE LINES 22-30 .. code-block:: Python gyroid = Tpms( surface_function=surface_functions.gyroid, offset=0.3, cell_size=1.0, repeat_cell=1, resolution=20, ) .. GENERATED FROM PYTHON SOURCE LINES 31-32 Wrap the implicit Shape in a Phase. Field-backed: no CAD required. .. GENERATED FROM PYTHON SOURCE LINES 32-37 .. code-block:: Python phase = Phase.from_shape(gyroid, resolution=80) print(f"Phase bounds: {phase.bounds}") print(f"Phase period: {phase.period} (set by Tpms.cell_size)") print(f"Phase field-backed: {phase.field is not None}") .. rst-class:: sphx-glr-script-out .. code-block:: none Phase bounds: (np.float64(-0.5), np.float64(0.5), np.float64(-0.5), np.float64(0.5), np.float64(-0.5), np.float64(0.5)) Phase period: (1.0, 1.0, 1.0) (set by Tpms.cell_size) Phase field-backed: True .. GENERATED FROM PYTHON SOURCE LINES 38-39 Marching-cubes surface mesh straight from the SDF. .. GENERATED FROM PYTHON SOURCE LINES 39-43 .. code-block:: Python surface = phase.surface_mesh() surface.save("gyroid_phase_surface.stl") print(f"Surface mesh: {surface.n_cells} cells -> gyroid_phase_surface.stl") .. rst-class:: sphx-glr-script-out .. code-block:: none Surface mesh: 60552 cells -> gyroid_phase_surface.stl .. GENERATED FROM PYTHON SOURCE LINES 44-45 Moments computed by grid quadrature — no OCCT needed. .. GENERATED FROM PYTHON SOURCE LINES 45-48 .. code-block:: Python print(f"Center of mass: {phase.center_of_mass}") print(f"Inertia tensor diagonal: {np.diag(phase.inertia_matrix)}") .. rst-class:: sphx-glr-script-out .. code-block:: none Center of mass: [0.00201187 0.00201187 0.00201187] Inertia tensor diagonal: [0.08872475 0.08872475 0.08872475] .. GENERATED FROM PYTHON SOURCE LINES 49-50 Connected components via Phase.pieces (scipy.ndimage on the SDF grid). .. GENERATED FROM PYTHON SOURCE LINES 50-55 .. code-block:: Python pieces = phase.pieces print(f"Pieces: {len(pieces)} (gyroid 'sheet' part is multiply connected)") for i, piece in enumerate(pieces[:3]): print(f" piece[{i}]: volume={piece.volume:.4f}, com={piece.com}") .. rst-class:: sphx-glr-script-out .. code-block:: none Pieces: 1 (gyroid 'sheet' part is multiply connected) piece[0]: volume=0.5192, com=(0.0020118670886081214, 0.0020118670886081214, 0.0020118670886081214) .. GENERATED FROM PYTHON SOURCE LINES 56-57 Transforms return a new Phase by default (inplace=False). .. GENERATED FROM PYTHON SOURCE LINES 57-63 .. code-block:: Python moved = phase.translate((1.0, 0.0, 0.0)) print(f"\nTranslated phase COM: {moved.center_of_mass} (shifted by +1 in x)") scaled = phase.scale(2.0) print(f"Scaled phase bounds: {scaled.bounds} (cell doubled)") .. rst-class:: sphx-glr-script-out .. code-block:: none Translated phase COM: [1.00201187 0.00201187 0.00201187] (shifted by +1 in x) Scaled phase bounds: (-1.0, 1.0, -1.0, 1.0, -1.0, 1.0) (cell doubled) .. GENERATED FROM PYTHON SOURCE LINES 64-65 Compose with another implicit Shape via F-rep boolean ops. .. GENERATED FROM PYTHON SOURCE LINES 65-71 .. code-block:: Python hollowed = gyroid - Sphere(radius=0.3) # SDF difference, returns a new Shape hollow_phase = Phase.from_shape(hollowed, bounds=phase.bounds, resolution=80) print(f"\nHollowed phase pieces: {len(hollow_phase.pieces)}") hollow_phase.surface_mesh().save("gyroid_minus_sphere.stl") print("Wrote gyroid_minus_sphere.stl") .. rst-class:: sphx-glr-script-out .. code-block:: none Hollowed phase pieces: 1 Wrote gyroid_minus_sphere.stl .. GENERATED FROM PYTHON SOURCE LINES 72-73 The gyroid phase and the gyroid minus a sphere, side by side. .. GENERATED FROM PYTHON SOURCE LINES 73-79 .. code-block:: Python pl = pv.Plotter(shape=(1, 2), window_size=(1200, 600)) pl.add_mesh(surface, smooth_shading=True, color="#ef7d2d") pl.subplot(0, 1) pl.add_mesh(hollow_phase.surface_mesh(), smooth_shading=True, color="#ef7d2d") pl.link_views() pl.show() .. tab-set:: .. tab-item:: Static Scene .. image-sg:: /examples/ImplicitShapes/images/sphx_glr_gyroid_implicit_phase_001.png :alt: gyroid implicit phase :srcset: /examples/ImplicitShapes/images/sphx_glr_gyroid_implicit_phase_001.png :class: sphx-glr-single-img .. tab-item:: Interactive Scene .. offlineviewer:: /home/runner/work/microgen/microgen/docs/examples/ImplicitShapes/images/sphx_glr_gyroid_implicit_phase_001.vtksz .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.025 seconds) .. _sphx_glr_download_examples_ImplicitShapes_gyroid_implicit_phase.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: gyroid_implicit_phase.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: gyroid_implicit_phase.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: gyroid_implicit_phase.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_