From e00dcb62c00301d2b9fc53a59a6f450a49ec8c64 Mon Sep 17 00:00:00 2001 From: tkoyama010 Date: Sat, 2 Sep 2023 00:09:55 +0000 Subject: [PATCH] deploy: pyvista/pyvista@c711f3862e55d2205056e34b206d46190be92857 --- examples/02-plot/anti-aliasing.ipynb | 43 ++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/examples/02-plot/anti-aliasing.ipynb b/examples/02-plot/anti-aliasing.ipynb index 32e656b..89e98f5 100644 --- a/examples/02-plot/anti-aliasing.ipynb +++ b/examples/02-plot/anti-aliasing.ipynb @@ -15,7 +15,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Anti-Aliasing {#anti_aliasing_example}\n=============\n\nDemonstrate anti-aliasing within PyVista.\n\nPyVista supports three types of anti-aliasing:\n\n- `\"ssaa\"` - Super-Sample Anti-Aliasing\n- `\"msaa\"` - Multi-Sample Anti-Aliasing\n- `\"fxaa\"` - Fast Approximate Anti-Aliasing\n\nBy default, anti-aliasing is disabled, but can be enabled globally with:\n\n``` {.sourceCode .python}\n>>> import pyvista as pv\n>>> pv.global_theme.anti_aliasing = 'ssaa'\n```\n\n**Which anti-aliasing technique should you use?**\n\nThose who have PCs with high-end configuration should opt for `\"ssaa\"`\nor `\"msaa\"`. Low-end PCs should use `\"fxaa\"`.\n" + "Anti-Aliasing {#anti_aliasing_example}\n=============\n\nDemonstrate anti-aliasing within PyVista.\n\nPyVista supports three types of anti-aliasing:\n\n- SSAA - Super-Sample Anti-Aliasing\n- MSAA - Multi-Sample Anti-Aliasing\n- FXAA - Fast Approximate Anti-Aliasing\n\nBy default, MSAA anti-aliasing is enabled using 8 samples. This is the\ndefault for VTK.\n\n``` {.sourceCode .python}\n>>> import pyvista as pv\n>>> pv.global_theme.multi_samples\n8\n```\n\nYou can enable additional line smoothing by enabling SSAA or FXAA\n\n**Which anti-aliasing technique should you use?**\n\nNormally, the default MSAA anti-aliasing should be sufficient as it\nstrikes a balance between efficiency and quality. If you desire\nadditional smoothing, you can either increase the number of\n`multi_samples` or use SSAA. Low-end PCs should consider FXAA.\n" ] }, { @@ -26,7 +26,7 @@ }, "outputs": [], "source": [ - "import pyvista as pv\nfrom pyvista import examples\n\nbunny = examples.download_bunny()" + "import pyvista as pv\n\nmesh = pv.Icosphere()" ] }, { @@ -44,14 +44,14 @@ }, "outputs": [], "source": [ - "# obtained with `cpos = pl.show(return_cpos=True)`\ncpos = [(-0.08566, 0.18735, 0.20116), (-0.05332, 0.12168, -0.01215), (-0.00151, 0.95566, -0.29446)]\n\npl = pv.Plotter()\npl.add_mesh(bunny, show_edges=True)\npl.disable_anti_aliasing()\npl.camera_position = cpos\npl.show()" + "pl = pv.Plotter()\npl.add_mesh(mesh, style='wireframe', color='k', line_width=2)\npl.disable_anti_aliasing()\npl.camera.zoom(1.5)\npl.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Fast Approximate Anti-Aliasing (FXAA)\n=====================================\n\nFXAA is the most performant of all three anti-aliasing techniques. This\nis because, in terms of hardware or GPU, FXAA is not that demanding. It\ndirectly smooths the 2D image and this reduces the strain over GPU,\nmaking it best for low-end PCs.\n\nBecause FXAA only operates on the rendered image, FXAA may result in\nsmoothing out parts of the visual overlay that are usually kept sharp\nfor reasons of clarity as well as smoothing out textures. In general,\nFXAA is inferior to MSAA and SSAA.\n" + "Default: Multi-Sample Anti-Aliasing (MSAA)\n==========================================\n\nNext, let\\'s show the default anti-aliasing configuration. By default,\nPyVista uses 8 samples of MSAA.\n\nMSAA, or Multi-Sample Anti-Aliasing is an optimization of SSAA that\nreduces the amount of pixel shader evaluations that need to be computed\nby focusing on overlapping regions of the scene. The result is\nanti-aliasing along edges that is on par with SSAA and less\nanti-aliasing along surfaces as these make up the bulk of SSAA\ncomputations. MSAA is substantially less computationally expensive than\nSSAA and results in comparable image quality.\n" ] }, { @@ -62,14 +62,14 @@ }, "outputs": [], "source": [ - "pl = pv.Plotter()\npl.add_mesh(bunny, show_edges=True)\npl.enable_anti_aliasing('fxaa')\npl.camera_position = cpos\npl.show()" + "pl = pv.Plotter()\npl.add_mesh(mesh, style='wireframe', color='k', line_width=2)\npl.camera.zoom(1.5)\npl.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Multi-Sample Anti-Aliasing (MSAA)\n=================================\n\nMSAA, or Multi-Sample Anti-Aliasing is an optimization of SSAA that\nreduces the amount of pixel shader evaluations that need to be computed\nby focusing on overlapping regions of the scene. The result is\nanti-aliasing along edges that is on par with SSAA and less\nanti-aliasing along surfaces as these make up the bulk of SSAA\ncomputations. MSAA is substantially less computationally expensive than\nSSAA and results in comparable image quality.\n" + "You can increase the smoothing by increasing multi\\_samples\n" ] }, { @@ -80,14 +80,14 @@ }, "outputs": [], "source": [ - "pl = pv.Plotter()\npl.add_mesh(bunny, show_edges=True)\npl.enable_anti_aliasing('msaa')\npl.camera_position = cpos\npl.show()" + "pl = pv.Plotter()\npl.add_mesh(mesh, style='wireframe', color='k', line_width=2)\npl.enable_anti_aliasing('msaa', multi_samples=16)\npl.camera.zoom(1.5)\npl.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Super-Sample Anti-Aliasing (SSAA)\n=================================\n\nSSAA, or Super-Sample Anti-Aliasing is a brute force method of\nanti-aliasing. It results in the best image quality but comes at a\ntremendous resource cost. SSAA works by rendering the scene at a higher\nresolution. The final image is produced by downsampling the massive\nsource image using an averaging filter. This acts as a low pass filter\nwhich removes the high frequency components that would cause jaggedness.\n" + "Fast Approximate Anti-Aliasing (FXAA)\n=====================================\n\nFXAA is the most performant of all three anti-aliasing techniques. This\nis because, in terms of hardware or GPU, FXAA is not that demanding. It\ndirectly smooths the 2D image and this reduces the strain on the GPU,\nmaking it best for low-end PCs.\n\nBecause FXAA only operates on the rendered image, FXAA may result in\nsmoothing out parts of the visual overlay that are usually kept sharp\nfor reasons of clarity as well as smoothing out textures. In general,\nFXAA is inferior to MSAA and SSAA.\n\nNote how the line width has been adjusted for consistency.\n" ] }, { @@ -98,7 +98,32 @@ }, "outputs": [], "source": [ - "pl = pv.Plotter()\npl.add_mesh(bunny, show_edges=True, line_width=2) # lines are thinner in SSAA\npl.enable_anti_aliasing('ssaa')\npl.camera_position = cpos\npl.show()" + "pl = pv.Plotter()\npl.add_mesh(mesh, style='wireframe', color='k', line_width=1.5)\npl.camera.zoom(1.5)\npl.enable_anti_aliasing('fxaa')\npl.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Super-Sample Anti-Aliasing (SSAA)\n=================================\n\nSSAA, or Super-Sample Anti-Aliasing is a brute force method of\nanti-aliasing. It results in the best image quality but comes at a\ntremendous resource cost. SSAA works by rendering the scene at a higher\nresolution. The final image is produced by downsampling the massive\nsource image using an averaging filter. This acts as a low pass filter\nwhich removes the high frequency components that would cause jaggedness.\n\nNote how the line width has been adjusted for consistency.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "pl = pv.Plotter()\npl.add_mesh(mesh, style='wireframe', color='k', line_width=4)\npl.camera.zoom(1.5)\npl.enable_anti_aliasing('ssaa')\npl.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Compare render time\n===================\n\nYou can compare the time to render for each one of the anti-aliasing\napproaches with:\n\n``` {.sourceCode .python}\nn_render = 100\nfor anti_aliasing in [False, 'fxaa', 'msaa', 'ssaa']:\n\n pl = pv.Plotter(off_screen=True)\n pl.add_mesh(mesh, style='wireframe', color='k', line_width=4)\n pl.camera.zoom(1.5)\n if anti_aliasing:\n pl.enable_anti_aliasing(anti_aliasing)\n else:\n pl.disable_anti_aliasing()\n pl.show(auto_close=False)\n tstart = time.time()\n # repeately trigger a render via saving a screenshot\n for __ in range(n_render):\n pl.screenshot('tmp.png')\n telap = (time.time() - tstart)/n_render\n\n print(f'Render time for {str(anti_aliasing):6}: {telap*1000:.3f} ms')\n```\n\nHere are the timings from an NVIDIA Quadro P2000 and a Intel(R) Xeon(R)\nE-2288G CPU @ 3.70GHz:\n\n``` {.sourceCode .text}\nRender time for False : 37.045 ms\nRender time for fxaa : 40.458 ms\nRender time for msaa : 42.566 ms\nRender time for ssaa : 51.450 ms\n```\n" ] } ],