.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/ImplicitShapes/implicit_booleans.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_implicit_booleans.py: Boolean operations on implicit fields ===================================== Since microgen 2.0, every shape carries an implicit field ``f(x, y, z)``, negative inside. Booleans are then operations on numbers, not on boundary representations: - union ``a | b`` is ``min(fa, fb)``, - intersection ``a & b`` is ``max(fa, fb)``, - difference ``a - b`` is ``max(fa, -fb)``, - complement ``~a`` is ``-fa``. They cost nothing until the shape is meshed, never fail on tangent or coincident faces, apply to TPMS, spinodoids, lattices and primitives alike, and need no CAD kernel. Smooth variants add fillets of radius ``k`` at the junctions, which are hard to get with a BREP kernel. .. GENERATED FROM PYTHON SOURCE LINES 19-26 .. code-block:: Python import time import pyvista as pv from microgen import Box, Cylinder, Sphere, Tpms, implicit_ops, surface_functions .. GENERATED FROM PYTHON SOURCE LINES 28-32 Constructive solid geometry --------------------------- The classic CSG example: a box intersected with a sphere, minus three cylinders along the axes. .. GENERATED FROM PYTHON SOURCE LINES 32-49 .. code-block:: Python box = Box(dim=(1.0, 1.0, 1.0)) sphere = Sphere(radius=0.65) cylinders = ( Cylinder(height=1.5, radius=0.3) | Cylinder(height=1.5, radius=0.3, orientation=(90.0, 0.0, 0.0)) | Cylinder(height=1.5, radius=0.3, orientation=(0.0, 90.0, 90.0)) ) start = time.perf_counter() csg = (box & sphere) - cylinders print(f"Composing the CSG tree: {1e3 * (time.perf_counter() - start):.2f} ms") bounds = (-0.6, 0.6, -0.6, 0.6, -0.6, 0.6) start = time.perf_counter() csg_mesh = csg.generate_surface_mesh(bounds=bounds, resolution=120) print(f"Meshing it: {time.perf_counter() - start:.2f} s, {csg_mesh.n_cells} triangles") .. rst-class:: sphx-glr-script-out .. code-block:: none Composing the CSG tree: 0.04 ms Meshing it: 0.21 s, 151984 triangles .. GENERATED FROM PYTHON SOURCE LINES 50-55 Smooth booleans --------------- ``smooth_union``, ``smooth_intersection`` and ``smooth_difference`` blend the two fields over a distance ``k``: the edges of the CSG part become fillets. .. GENERATED FROM PYTHON SOURCE LINES 55-59 .. code-block:: Python smooth = box.smooth_intersection(sphere, k=0.1).smooth_difference(cylinders, k=0.1) smooth_mesh = smooth.generate_surface_mesh(bounds=bounds, resolution=120) smooth_mesh.plot(color="#ef7d2d", smooth_shading=True) .. tab-set:: .. tab-item:: Static Scene .. image-sg:: /examples/ImplicitShapes/images/sphx_glr_implicit_booleans_001.png :alt: implicit booleans :srcset: /examples/ImplicitShapes/images/sphx_glr_implicit_booleans_001.png :class: sphx-glr-single-img .. tab-item:: Interactive Scene .. offlineviewer:: /home/runner/work/microgen/microgen/docs/examples/ImplicitShapes/images/sphx_glr_implicit_booleans_001.vtksz .. GENERATED FROM PYTHON SOURCE LINES 60-66 Mixing primitives and TPMS -------------------------- The same operators combine any implicit shapes. Here, a gyroid is clipped by a sphere and joined to a solid core with a smooth union. :func:`microgen.implicit_ops.shell` turns a solid into a hollow wall of given thickness. .. GENERATED FROM PYTHON SOURCE LINES 66-78 .. code-block:: Python gyroid = Tpms( surface_function=surface_functions.gyroid, offset=0.6, cell_size=0.5, repeat_cell=3 ) gyroid_ball = (gyroid & Sphere(radius=0.7)).smooth_union(Sphere(radius=0.25), k=0.1) gyroid_ball_mesh = gyroid_ball.generate_surface_mesh( bounds=(-0.75, 0.75, -0.75, 0.75, -0.75, 0.75), resolution=150 ) octant = Box(dim=(1.0, 1.0, 1.0), center=(0.5, 0.5, 0.5)) shelled = implicit_ops.shell(Sphere(radius=0.6), thickness=0.08) - octant shelled_mesh = shelled.generate_surface_mesh(bounds=bounds, resolution=120) .. GENERATED FROM PYTHON SOURCE LINES 79-81 Hard CSG, smooth CSG, a gyroid ball with a solid core, and a hollow sphere cut open by removing one octant. .. GENERATED FROM PYTHON SOURCE LINES 81-95 .. code-block:: Python pl = pv.Plotter(shape=(2, 2), window_size=(1000, 1000)) for i, (title, mesh) in enumerate( [ ("(box & sphere) - cylinders", csg_mesh), ("smooth booleans, k = 0.1", smooth_mesh), ("gyroid & sphere, smooth union with a core", gyroid_ball_mesh), ("shell(sphere) - octant", shelled_mesh), ] ): pl.subplot(i // 2, i % 2) pl.add_text(title, font_size=10) pl.add_mesh(mesh, color="#ef7d2d", smooth_shading=True) pl.link_views() pl.show() .. image-sg:: /examples/ImplicitShapes/images/sphx_glr_implicit_booleans_002.png :alt: implicit booleans :srcset: /examples/ImplicitShapes/images/sphx_glr_implicit_booleans_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 5.229 seconds) .. _sphx_glr_download_examples_ImplicitShapes_implicit_booleans.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: implicit_booleans.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: implicit_booleans.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: implicit_booleans.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_