| 1 |
""" |
| 2 |
.. _interpolate_before_map_example: |
| 3 |
|
| 4 |
Interpolate Before Mapping |
| 5 |
~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 6 |
|
| 7 |
The :func:`add_mesh <pyvista.Plotter.add_mesh>` method has an |
| 8 |
``interpolate_before_map`` argument that affects the way scalar data is |
| 9 |
visualized with colors. The effect of this can vary depending on the |
| 10 |
dataset's topology and the chosen colormap. |
| 11 |
|
| 12 |
This example serves to demo the difference and why we've chosen to enable this |
| 13 |
by default. |
| 14 |
|
| 15 |
For more details, please see `What is InterpolateScalarsBeforeMapping in VTK? |
| 16 |
<https://www.kitware.com/what-is-interpolatescalarsbeforemapping-in-vtk/>`_ |
| 17 |
|
| 18 |
""" |
| 19 |
|
| 20 |
# sphinx_gallery_thumbnail_number = 4 |
| 21 |
import pyvista as pv |
| 22 |
|
| 23 |
# %% |
| 24 |
# Meshes are colored by the data on their nodes or cells - when coloring a mesh |
| 25 |
# by data on its nodes, the values must be interpolated across the faces of |
| 26 |
# cells. The process by which those scalars are interpolated is critical. |
| 27 |
# If the ``interpolate_before_map`` is left off, the color mapping occurs at |
| 28 |
# polygon points and colors are interpolated, which is generally less accurate |
| 29 |
# whereas if the ``interpolate_before_map`` is on, then the scalars will be |
| 30 |
# interpolated across the topology of the dataset which is more accurate. |
| 31 |
# |
| 32 |
# To summarize, when ``interpolate_before_map`` is off, the colors are |
| 33 |
# interpolated after rendering and when ``interpolate_before_map`` is on, the |
| 34 |
# scalars are interpolated across the mesh and those values are mapped to |
| 35 |
# colors. |
| 36 |
# |
| 37 |
# So lets take a look at the difference: |
| 38 |
|
| 39 |
# Load a cylinder which has cells with a wide spread |
| 40 |
cyl = pv.Cylinder(direction=(0, 0, 1), height=2).elevation() |
| 41 |
|
| 42 |
# Common display argument to make sure all else is constant |
| 43 |
dargs = dict(scalars='Elevation', cmap='rainbow', show_edges=True) |
| 44 |
|
| 45 |
pl = pv.Plotter(shape=(1, 2)) |
| 46 |
pl.add_mesh( |
| 47 |
cyl, |
| 48 |
interpolate_before_map=False, |
| 49 |
scalar_bar_args={'title': 'Elevation - not interpolated'}, |
| 50 |
**dargs, |
| 51 |
) |
| 52 |
pl.subplot(0, 1) |
| 53 |
pl.add_mesh( |
| 54 |
cyl, |
| 55 |
interpolate_before_map=True, |
| 56 |
scalar_bar_args={'title': 'Elevation - interpolated'}, |
| 57 |
**dargs, |
| 58 |
) |
| 59 |
pl.link_views() |
| 60 |
pl.camera_position = pv.CameraPosition( |
| 61 |
position=(-1.67, -5.10, 2.06), focal_point=(0.0, 0.0, 0.0), viewup=(0.00, 0.37, 0.93) |
| 62 |
) |
| 63 |
pl.show() |
| 64 |
|
| 65 |
# %% |
| 66 |
# Shown in the figure above, when not interpolating the scalars before mapping, |
| 67 |
# the colors (RGB values, not scalars) are interpolated between the vertices by |
| 68 |
# the underlying graphics library (OpenGL), and the colors shown are not |
| 69 |
# accurate. |
| 70 |
# |
| 71 |
# The same interpolation effect occurs for wireframe visualization too: |
| 72 |
|
| 73 |
# Common display argument to make sure all else is constant |
| 74 |
dargs = dict(scalars='Elevation', cmap='rainbow', show_edges=True, style='wireframe') |
| 75 |
|
| 76 |
pl = pv.Plotter(shape=(1, 2)) |
| 77 |
pl.add_mesh( |
| 78 |
cyl, |
| 79 |
interpolate_before_map=False, |
| 80 |
scalar_bar_args={'title': 'Elevation - not interpolated'}, |
| 81 |
**dargs, |
| 82 |
) |
| 83 |
pl.subplot(0, 1) |
| 84 |
pl.add_mesh( |
| 85 |
cyl, |
| 86 |
interpolate_before_map=True, |
| 87 |
scalar_bar_args={'title': 'Elevation - interpolated'}, |
| 88 |
**dargs, |
| 89 |
) |
| 90 |
pl.link_views() |
| 91 |
pl.camera_position = pv.CameraPosition( |
| 92 |
position=(-1.67, -5.10, 2.06), focal_point=(0.0, 0.0, 0.0), viewup=(0.00, 0.37, 0.93) |
| 93 |
) |
| 94 |
pl.show() |
| 95 |
|
| 96 |
# %% |
| 97 |
# The cylinder mesh above is a great example dataset for this as it has a wide |
| 98 |
# spread between the vertices (points are only at the top and bottom of the |
| 99 |
# cylinder) which means high surface are of the mesh has to be interpolated. |
| 100 |
# |
| 101 |
# However, most meshes don't have such a wide spread and the effects of |
| 102 |
# color interpolating are harder to notice. Let's take a look at a wavelet |
| 103 |
# example and try to figure out how the ``interpolate_before_map`` option |
| 104 |
# affects its rendering. |
| 105 |
wavelet = pv.Wavelet().clip('x') |
| 106 |
|
| 107 |
# Common display argument to make sure all else is constant |
| 108 |
dargs = dict(scalars='RTData', cmap='rainbow', show_edges=True) |
| 109 |
|
| 110 |
pl = pv.Plotter(shape=(1, 2)) |
| 111 |
pl.add_mesh( |
| 112 |
wavelet, |
| 113 |
interpolate_before_map=False, |
| 114 |
scalar_bar_args={'title': 'RTData - not interpolated'}, |
| 115 |
**dargs, |
| 116 |
) |
| 117 |
pl.subplot(0, 1) |
| 118 |
pl.add_mesh( |
| 119 |
wavelet, |
| 120 |
interpolate_before_map=True, |
| 121 |
scalar_bar_args={'title': 'RTData - interpolated'}, |
| 122 |
**dargs, |
| 123 |
) |
| 124 |
pl.link_views() |
| 125 |
pl.camera_position = pv.CameraPosition( |
| 126 |
position=(55.0, 16, 31), focal_point=(-5.0, 0.0, 0.0), viewup=(-0.22, 0.97, -0.09) |
| 127 |
) |
| 128 |
pl.show() |
| 129 |
|
| 130 |
# %% |
| 131 |
# This time is pretty difficult to notice the differences - they are there, |
| 132 |
# subtle, but present. The differences become more apparent when we decrease |
| 133 |
# the number of colors in colormap. |
| 134 |
# Let's take a look at the differences when using eight discrete colors via |
| 135 |
# the ``n_colors`` argument: |
| 136 |
|
| 137 |
dargs = dict(scalars='RTData', cmap='rainbow', show_edges=True, n_colors=8) |
| 138 |
|
| 139 |
pl = pv.Plotter(shape=(1, 2)) |
| 140 |
pl.add_mesh( |
| 141 |
wavelet, |
| 142 |
interpolate_before_map=False, |
| 143 |
scalar_bar_args={'title': 'RTData - not interpolated'}, |
| 144 |
**dargs, |
| 145 |
) |
| 146 |
pl.subplot(0, 1) |
| 147 |
pl.add_mesh( |
| 148 |
wavelet, |
| 149 |
interpolate_before_map=True, |
| 150 |
scalar_bar_args={'title': 'RTData - interpolated'}, |
| 151 |
**dargs, |
| 152 |
) |
| 153 |
pl.link_views() |
| 154 |
pl.camera_position = pv.CameraPosition( |
| 155 |
position=(55.0, 16, 31), focal_point=(-5.0, 0.0, 0.0), viewup=(-0.22, 0.97, -0.09) |
| 156 |
) |
| 157 |
pl.show() |
| 158 |
|
| 159 |
# %% |
| 160 |
# Left, ``interpolate_before_map`` OFF. Right, ``interpolate_before_map`` ON. |
| 161 |
# |
| 162 |
# Now that is much more compelling. On the right, the contours of the scalar |
| 163 |
# field are visible, but on the left, the contours are obscured due to the color |
| 164 |
# interpolation by OpenGL. In both cases, the colors at the vertices are the |
| 165 |
# same, the difference is how color is assigned between the vertices. |
| 166 |
# |
| 167 |
# In our opinion, color interpolation is not a preferred default for scientific |
| 168 |
# visualization and is why we have chosen to set the ``interpolate_before_map`` |
| 169 |
# flag to ``True``. |
| 170 |
# |
| 171 |
# .. tags:: plot |