1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
"""
.. _strips_example:
Triangle Strips
~~~~~~~~~~~~~~~
This example shows how to build a simple :class:`pyvista.PolyData` using
triangle strips.
Triangle strips are a more efficient way of storing the connectivity of
adjacent triangles.
"""
# sphinx_gallery_start_ignore
from __future__ import annotations
PYVISTA_GALLERY_FORCE_STATIC_IN_DOCUMENT = True
# sphinx_gallery_end_ignore
import numpy as np
import pyvista as pv
# Create an array of points
points = np.array(
[
[1.0, 0.0, 0.0],
[0.0, 0.0, 0.0],
[1.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
[1.0, 2.0, 0.0],
[0.0, 2.0, 0.0],
[1.0, 3.0, 0.0],
[0.0, 3.0, 0.0],
],
)
# %%
# Build the connectivity of the strips
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# The first element is the number of points in the strip next three elements is the
# initial triangle the rest of the points is where the strip extends to.
strips = np.array([8, 0, 1, 2, 3, 4, 5, 6, 7])
# build the mesh
mesh = pv.PolyData(points, strips=strips)
mesh
# %%
# Plot the triangle strips
# ~~~~~~~~~~~~~~~~~~~~~~~~
# Plot the ``PolyData`` and include the point labels using
# :func:`add_point_labels() <pyvista.Plotter.add_point_labels>` so we can see how
# the PolyData is constructed using triangle strips.
pl = pv.Plotter()
pl.add_mesh(mesh, show_edges=True)
pl.add_point_labels(mesh.points, range(mesh.n_points))
pl.camera_position = 'yx'
pl.camera.zoom(1.2)
pl.show()
# %%
# Convert strips to triangles
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~
# You can convert strips to triangle faces using :func:`triangulate
# <pyvista.DataSetFilters.triangulate>`.
trimesh = mesh.triangulate()
trimesh
# %%
# We can use this new :class:`pyvista.PolyData` to see how VTK represents
# triangle strips as individual faces.
#
# See how the faces array is much larger (~3x more) even for this basic example
# even despite representing the same data.
#
# .. note::
# The faces array from VTK always contains padding (the number of points in
# the face) for each face in the face array. This is the initial ``3`` in
# the following face array.
faces = trimesh.faces.reshape(-1, 4)
faces
# %%
# Convert triangles to strips
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Convert faces from a :class:`pyvista.PolyData` to strips using :func:`strip()
# <pyvista.PolyDataFilters.strip>`. Here, for demonstration purposes we convert the
# triangulated mesh back to a stripped mesh.
restripped = trimesh.strip()
restripped
# %%
# The output from the ``strip`` filter is, as expected, identical to the
# original ``mesh``.
restripped == mesh
|