.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_gallery/geometry/example_create_3d_surface_meshes.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__gallery_geometry_example_create_3d_surface_meshes.py: .. _example_create_3d_surface_meshes: Creating 3D surface meshes ========================== .. contents:: Table of Contents :local: :depth: 1 :backlinks: top The ``create_3d_*`` functions create 3D surface meshes (:math:`E = 3`) of height maps, cylinders and spheres, with ``quadrangle_4`` or ``triangle_3`` elements. All the meshes have a UV map, so they can be textured. .. seealso:: - :ref:`example_create_2d_surface_meshes` for the 2D meshes (:math:`E = 2`). - :func:`pysdic.quadrangle_4_to_triangle_3` for the conversion of quadrangles into triangles. .. GENERATED FROM PYTHON SOURCE LINES 24-30 Height map ---------- A height map is a surface :math:`z = f(x, y)`. The function is called once with the 2D grids of :math:`x` and :math:`y`. ``n_x`` and ``n_y`` are numbers of **vertices**: the vertex ``(i_x, i_y)`` is ``mesh.coordinates[i_y * n_x + i_x]``. .. GENERATED FROM PYTHON SOURCE LINES 30-50 .. code-block:: Python import numpy from pysdic import Plotter3D, create_3d_quadrangle_4_heightmap n_x, n_y = 15, 10 heightmap = create_3d_quadrangle_4_heightmap( lambda x, y: 0.3 * numpy.sin(numpy.pi * x) * numpy.cos(numpy.pi * y), x_bounds=(-1.0, 1.0), y_bounds=(-1.0, 1.0), n_x=n_x, n_y=n_y, ) print(heightmap) print(f"Vertex (i_x=3, i_y=2): {heightmap.coordinates[2 * n_x + 3]}") plotter = Plotter3D() plotter.add_mesh(heightmap, face_color="lightblue", edge_color="black", vertex_size=4) plotter.set_title("quadrangle_4 height map") plotter.show() .. image-sg:: /_gallery/geometry/images/sphx_glr_example_create_3d_surface_meshes_001.png :alt: example create 3d surface meshes :srcset: /_gallery/geometry/images/sphx_glr_example_create_3d_surface_meshes_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Mesh(n_vertices=150, n_dimensions=3, n_elements=126, n_vertices_per_element=4) - Element Type: 'quadrangle_4' - Element property: 'uvmap' with shape (126, 8) Vertex (i_x=3, i_y=2): [-0.57142857 -0.55555556 0.05078834] .. GENERATED FROM PYTHON SOURCE LINES 51-52 Non-uniform grids are given with ``x_array`` and ``y_array`` instead of the bounds. .. GENERATED FROM PYTHON SOURCE LINES 52-64 .. code-block:: Python refined = create_3d_quadrangle_4_heightmap( lambda x, y: 0.2 * x * y, x_array=numpy.geomspace(0.1, 2.0, 12), y_array=numpy.linspace(0.0, 1.0, 5), ) plotter = Plotter3D() plotter.add_mesh(refined, face_color="wheat", edge_color="black", show_vertices=False) plotter.set_title("Non-uniform grid") plotter.show() .. image-sg:: /_gallery/geometry/images/sphx_glr_example_create_3d_surface_meshes_002.png :alt: example create 3d surface meshes :srcset: /_gallery/geometry/images/sphx_glr_example_create_3d_surface_meshes_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 65-71 From quadrangles to triangles ----------------------------- Each quadrangle :math:`q` is split into the triangles :math:`2q` and :math:`2q + 1` along one of its diagonals. The vertices, the properties and the UV map are kept. The meshes are displayed side by side with ``warp_by``. .. GENERATED FROM PYTHON SOURCE LINES 71-90 .. code-block:: Python from pysdic import create_3d_triangle_3_heightmap, quadrangle_4_to_triangle_3 def shift(mesh, dx): # Displacement of all the vertices along x (to display several meshes side by side) return numpy.tile([dx, 0.0, 0.0], (mesh.n_vertices, 1)) small = create_3d_quadrangle_4_heightmap(x_bounds=(0.0, 1.0), y_bounds=(0.0, 1.0), n_x=5, n_y=5) plotter = Plotter3D() for i, diagonal in enumerate(["first", "second", "alternate", "random"]): triangles = quadrangle_4_to_triangle_3(small, diagonal, seed=0) plotter.add_mesh(triangles, warp_by=shift(triangles, 1.2 * i), face_color="lightgreen", edge_color="black", show_vertices=False) print(f"{diagonal:>9}: first triangles {triangles.elements[:2].tolist()}") plotter.set_title("Diagonals: first, second, alternate, random") plotter.show() .. image-sg:: /_gallery/geometry/images/sphx_glr_example_create_3d_surface_meshes_003.png :alt: example create 3d surface meshes :srcset: /_gallery/geometry/images/sphx_glr_example_create_3d_surface_meshes_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none first: first triangles [[0, 1, 6], [0, 6, 5]] second: first triangles [[0, 1, 5], [1, 6, 5]] alternate: first triangles [[0, 1, 6], [0, 6, 5]] random: first triangles [[0, 1, 6], [0, 6, 5]] .. GENERATED FROM PYTHON SOURCE LINES 91-92 :func:`pysdic.create_3d_triangle_3_heightmap` does both steps at once. .. GENERATED FROM PYTHON SOURCE LINES 92-103 .. code-block:: Python triangle_heightmap = create_3d_triangle_3_heightmap( lambda x, y: 0.3 * numpy.sin(numpy.pi * x) * numpy.cos(numpy.pi * y), x_bounds=(-1.0, 1.0), y_bounds=(-1.0, 1.0), n_x=n_x, n_y=n_y, diagonal="alternate", ) print(triangle_heightmap) .. rst-class:: sphx-glr-script-out .. code-block:: none Mesh(n_vertices=150, n_dimensions=3, n_elements=252, n_vertices_per_element=3) - Element Type: 'triangle_3' - Element property: 'uvmap' with shape (252, 6) .. GENERATED FROM PYTHON SOURCE LINES 104-112 UV map and texture ------------------ The UV map gives the texture coordinates of the vertices of each element. ``uv_layout`` chooses which corner of the image goes to which corner of the grid (see :func:`pysdic.create_uniform_quadrangle_4_mesh`): with the layout 0, the upper-left corner of the image (in gray here) is at the vertex :math:`(0, n_y - 1)`, and with the layout 3 at the vertex :math:`(0, 0)` (the texture is flipped along :math:`y`). .. GENERATED FROM PYTHON SOURCE LINES 112-123 .. code-block:: Python checkerboard = (255 * ((numpy.arange(256)[:, None] // 32 + numpy.arange(256)[None, :] // 32) % 2)).astype(numpy.uint8) checkerboard[:64, :64] = 128 # Upper-left corner of the image plotter = Plotter3D() for i, uv_layout in enumerate([0, 3]): mesh = create_3d_quadrangle_4_heightmap(x_bounds=(0.0, 1.0), y_bounds=(0.0, 1.0), n_x=10, n_y=10, uv_layout=uv_layout) plotter.add_mesh(mesh, warp_by=shift(mesh, 1.2 * i), texture=checkerboard, show_edges=False, show_vertices=False) plotter.set_title("UV layouts 0 and 3") plotter.show() .. image-sg:: /_gallery/geometry/images/sphx_glr_example_create_3d_surface_meshes_004.png :alt: example create 3d surface meshes :srcset: /_gallery/geometry/images/sphx_glr_example_create_3d_surface_meshes_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 124-130 Cylinders --------- A cylindrical surface of axis :math:`z` has a radius :math:`r = f(h, \theta)` (for an axisymmetric surface, ignore :math:`\theta`). With ``close=True``, the last and first angles are connected: the angles must stop one step before :math:`2\pi`. .. GENERATED FROM PYTHON SOURCE LINES 130-146 .. code-block:: Python from pysdic import create_3d_quadrangle_4_cylindrical, create_3d_triangle_3_cylindrical n_t = 24 closed_angles = (0.0, 2 * numpy.pi * (1 - 1 / n_t)) cylinder = create_3d_quadrangle_4_cylindrical( lambda h, t: 1.0 + 0.2 * numpy.cos(3 * t) * h, t_bounds=closed_angles, h_bounds=(0.0, 2.0), n_t=n_t, n_h=8, close=True ) print(cylinder) plotter = Plotter3D() plotter.add_mesh(cylinder, face_color="lightcoral", edge_color="black", show_vertices=False) plotter.set_title("Closed quadrangle_4 cylinder") plotter.show() .. image-sg:: /_gallery/geometry/images/sphx_glr_example_create_3d_surface_meshes_005.png :alt: example create 3d surface meshes :srcset: /_gallery/geometry/images/sphx_glr_example_create_3d_surface_meshes_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Mesh(n_vertices=192, n_dimensions=3, n_elements=168, n_vertices_per_element=4) - Element Type: 'quadrangle_4' - Element property: 'uvmap' with shape (168, 8) .. GENERATED FROM PYTHON SOURCE LINES 147-150 With ``triangle_3`` elements and ``stick=True``, the vertices on the axis are merged and the degenerate triangles removed: the surface of revolution :math:`r = \sqrt{1 - h^2}` with :math:`h \in [-1, 1]` is a closed sphere. .. GENERATED FROM PYTHON SOURCE LINES 150-168 .. code-block:: Python sphere_of_revolution = create_3d_triangle_3_cylindrical( lambda h, t: numpy.sqrt(numpy.clip(1.0 - h**2, 0.0, None)), t_bounds=closed_angles, h_bounds=(-1.0, 1.0), n_t=n_t, n_h=15, close=True, stick=True, ) print(sphere_of_revolution) print(f"Degenerate elements: {sphere_of_revolution.get_degenerated_elements_mask().sum()}") plotter = Plotter3D() plotter.add_mesh(sphere_of_revolution, face_color="khaki", edge_color="black", show_vertices=False) plotter.set_title("Sphere of revolution (stick=True)") plotter.show() .. image-sg:: /_gallery/geometry/images/sphx_glr_example_create_3d_surface_meshes_006.png :alt: example create 3d surface meshes :srcset: /_gallery/geometry/images/sphx_glr_example_create_3d_surface_meshes_006.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Mesh(n_vertices=314, n_dimensions=3, n_elements=624, n_vertices_per_element=3) - Element Type: 'triangle_3' - Element property: 'uvmap' with shape (624, 6) Degenerate elements: 0 .. GENERATED FROM PYTHON SOURCE LINES 169-177 Spheres ------- A spherical surface has a radius :math:`r = f(\theta, \phi)`, with the polar angle :math:`\theta` from the :math:`z`-axis and the azimuthal angle :math:`\phi`. The ``quadrangle_4`` mesh is open at the poles. The ``triangle_3`` mesh is closed by a vertex at each pole (``add_north`` and ``add_south``, True by default), so its polar angles must be strictly between 0 and :math:`\pi`. .. GENERATED FROM PYTHON SOURCE LINES 177-201 .. code-block:: Python from pysdic import create_3d_quadrangle_4_spherical, create_3d_triangle_3_spherical n_p = 30 closed_azimuths = (0.0, 2 * numpy.pi * (1 - 1 / n_p)) open_sphere = create_3d_quadrangle_4_spherical( t_bounds=(0.3, numpy.pi - 0.3), p_bounds=closed_azimuths, n_t=12, n_p=n_p, close=True ) closed_sphere = create_3d_triangle_3_spherical( lambda t, p: 1.0 + 0.1 * numpy.sin(4 * p) * numpy.sin(t), t_bounds=(0.1, numpy.pi - 0.1), p_bounds=closed_azimuths, n_t=15, n_p=n_p, close=True, ) print(open_sphere) print(closed_sphere) plotter = Plotter3D() plotter.add_mesh(open_sphere, face_color="lightsteelblue", edge_color="black", show_vertices=False) plotter.add_mesh(closed_sphere, warp_by=shift(closed_sphere, 2.5), face_color="plum", edge_color="black", show_vertices=False) plotter.set_title("Open quadrangle_4 sphere and closed triangle_3 sphere") plotter.show() .. image-sg:: /_gallery/geometry/images/sphx_glr_example_create_3d_surface_meshes_007.png :alt: example create 3d surface meshes :srcset: /_gallery/geometry/images/sphx_glr_example_create_3d_surface_meshes_007.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Mesh(n_vertices=360, n_dimensions=3, n_elements=330, n_vertices_per_element=4) - Element Type: 'quadrangle_4' - Element property: 'uvmap' with shape (330, 8) Mesh(n_vertices=452, n_dimensions=3, n_elements=900, n_vertices_per_element=3) - Element Type: 'triangle_3' - Element property: 'uvmap' with shape (900, 6) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.417 seconds) .. _sphx_glr_download__gallery_geometry_example_create_3d_surface_meshes.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: example_create_3d_surface_meshes.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: example_create_3d_surface_meshes.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: example_create_3d_surface_meshes.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_