Triangle Strips#

This example shows how to build a simple pyvista.PolyData using triangle strips.

Triangle strips are a more efficient way of storing the connectivity of adjacent triangles.

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
PolyDataInformation
N Cells1
N Points8
N Strips1
X Bounds0.000e+00, 1.000e+00
Y Bounds0.000e+00, 3.000e+00
Z Bounds0.000e+00, 0.000e+00
N Arrays0


Plot the triangle strips#

Plot the PolyData and include the point labels using 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()
create polydata strips

Convert strips to triangles#

You can convert strips to triangle faces using triangulate.

trimesh = mesh.triangulate()
trimesh
PolyDataInformation
N Cells6
N Points8
N Strips0
X Bounds0.000e+00, 1.000e+00
Y Bounds0.000e+00, 3.000e+00
Z Bounds0.000e+00, 0.000e+00
N Arrays0


We can use this new 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
array([[3, 0, 1, 2],
       [3, 2, 1, 3],
       [3, 2, 3, 4],
       [3, 4, 3, 5],
       [3, 4, 5, 6],
       [3, 6, 5, 7]])

Convert triangles to strips#

Convert faces from a pyvista.PolyData to strips using strip(). Here, for demonstration purposes we convert the triangulated mesh back to a stripped mesh.

restripped = trimesh.strip()
restripped
PolyDataInformation
N Cells1
N Points8
N Strips1
X Bounds0.000e+00, 1.000e+00
Y Bounds0.000e+00, 3.000e+00
Z Bounds0.000e+00, 0.000e+00
N Arrays0


The output from the strip filter is, as expected, identical to the original mesh.

restripped == mesh
True

Total running time of the script: (0 minutes 0.194 seconds)

Gallery generated by Sphinx-Gallery