{ "cells": [ { "cell_type": "markdown", "id": "1ebff684-7942-44f9-bb32-706483d5e227", "metadata": {}, "source": [ "# Mesh Visualization\n", "\n", "Here building blocks for mesh visualization are introduced." ] }, { "cell_type": "markdown", "id": "dd611be7-6ad8-4aa9-a022-ad35cc6814db", "metadata": {}, "source": [ "## 2D Elements & Wireframe\n", "\n", "The `MeshData` object stores gpu-data of the mesh and makes it accessible to multiple renderers (surface/volume elements, wireframe data, coefficient function, ...)\n", "The mesh can be high order curved.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "cc50e9c2-06bd-4ffc-8c34-06a6b0e38a26", "metadata": {}, "outputs": [], "source": [ "from ngsolve import *\n", "from netgen.occ import *\n", "from ngsolve_webgpu import *\n", "from webgpu.jupyter import Draw\n", "\n", "sphere = Sphere((0, 0, 0), 1)\n", "geo = OCCGeometry(sphere)\n", "mesh = Mesh(geo.GenerateMesh(maxh=0.3))\n", "mesh.Curve(4)\n", "meshdata = MeshData(mesh)\n", "clipping = Clipping()\n", "surface_elements = MeshElements2d(meshdata, clipping=clipping)\n", "wireframe = MeshWireframe2d(meshdata, clipping=clipping)\n", "\n", "Draw([surface_elements, wireframe])" ] }, { "cell_type": "markdown", "id": "9d295ae5-9b37-4b39-b1fb-c8e890313455", "metadata": {}, "source": [ "## 3D Elements\n", "\n", "The renderer for 3d elements automatically sets the mesh data to also write 3d volume element information to the GPU.\n", "When adding options from the volume renderer to the gui a slider for shrinking the elements is added." ] }, { "cell_type": "code", "execution_count": null, "id": "2976de0f-56d4-4c08-b35b-20fe6d768ede", "metadata": {}, "outputs": [], "source": [ "from ngsolve import *\n", "from ngsolve_webgpu import *\n", "from webgpu.jupyter import Draw\n", "\n", "mesh = Mesh(unit_cube.GenerateMesh(maxh=0.2))\n", "meshdata = MeshData(mesh)\n", "clipping = Clipping()\n", "clipping.center = [0.5, 0.5, 0.5]\n", "# clipping.mode = clipping.Mode.PLANE # enables clipping\n", "volume_elements = MeshElements3d(meshdata, clipping=clipping)\n", "volume_elements.shrink = 0.8\n", "Draw(volume_elements)" ] }, { "cell_type": "markdown", "id": "464c3efb-9ea9-41ed-b557-ab11b54dd3d6", "metadata": {}, "source": [ "## Mesh Deformation\n", "\n", "we can deform the mesh with a function. Here we use `FunctionData`, similar to the `MeshData` before. It allows the function to be used by multiple renderer and even in multiple scenes with the data being only once on the GPU.\n", "The deformation data must be a 3 dim CF." ] }, { "cell_type": "code", "execution_count": null, "id": "a89dfad8-1dfd-418d-98d1-bc5ff8f03519", "metadata": {}, "outputs": [], "source": [ "from ngsolve import *\n", "from ngsolve_webgpu import *\n", "from webgpu.jupyter import Draw\n", "\n", "deformation = CF((0, 0, 0.2 * sin(10 * x)))\n", "mesh = Mesh(unit_square.GenerateMesh(maxh=0.2))\n", "meshdata = MeshData(mesh)\n", "def_data = FunctionData(meshdata, deformation, order=5)\n", "meshdata.deformation_data = def_data\n", "surface_elements = MeshElements2d(meshdata)\n", "wireframe = MeshWireframe2d(meshdata)\n", "scene = Draw([surface_elements, wireframe])" ] }, { "cell_type": "markdown", "id": "a7f5228f-44e3-4a44-a547-680a5462cdf0", "metadata": {}, "source": [ "## Entity Numbers\n", "\n", "The `EntityNumbers` renderer can display numbers for vertices, edges, facets, surface elements, and volume elements.\n", "See the [Entity Numbers](entity_numbers.ipynb) notebook for full documentation." ] }, { "cell_type": "code", "execution_count": null, "id": "22271402-675f-4675-ace1-570c066502b4", "metadata": {}, "outputs": [], "source": [ "from ngsolve import *\n", "from ngsolve_webgpu import *\n", "from webgpu.jupyter import Draw\n", "\n", "mesh = Mesh(unit_square.GenerateMesh(maxh=0.3))\n", "meshdata = MeshData(mesh)\n", "wireframe = MeshWireframe2d(meshdata)\n", "vertex_nums = EntityNumbers(meshdata, entity='vertices', font_size=15)\n", "edge_nums = EntityNumbers(meshdata, entity='edges', font_size=12)\n", "scene = Draw([wireframe, vertex_nums, edge_nums])" ] }, { "cell_type": "code", "execution_count": null, "id": "212a1ccd", "metadata": {}, "outputs": [], "source": [ "from netgen.occ import *\n", "from ngsolve import *\n", "from ngsolve_webgpu import *\n", "from webgpu.jupyter import Draw\n", "\n", "# 1D elements (segments) rendered as thick lines\n", "r1 = Rectangle(1,1).Face()\n", "r2 = r1.Move((1,0,0))\n", "s = Glue([r1,r2])\n", "mesh = s.GenerateMesh(maxh=0.2, dim=2)\n", "meshdata = MeshData(mesh)\n", "clipping = Clipping()\n", "surface_elements = MeshElements2d(meshdata, clipping=clipping)\n", "segments = MeshSegments(meshdata, clipping=clipping)\n", "\n", "Draw([surface_elements, segments])" ] }, { "cell_type": "code", "execution_count": null, "id": "2b4d6378-c67b-49e5-993f-1f328a0360cb", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.14.5" } }, "nbformat": 4, "nbformat_minor": 5 }