# Examples from pyvista.Plotter.add_mesh
# ======================================

# Add a sphere to the plotter and show it with a custom scalar
# bar title.
import pyvista as pv
sphere = pv.Sphere()
sphere['Data'] = sphere.points[:, 2]
pl = pv.Plotter()
_ = pl.add_mesh(sphere, scalar_bar_args={'title': 'Z Position'})
pl.show()

# Plot using RGB on a single cell.  Note that since the number of
# points and the number of cells are identical, we have to pass
# `preference='cell'`.
import pyvista as pv
import numpy as np
vertices = np.array(
    [
        [0, 0, 0],
        [1, 0, 0],
        [0.5, 0.667, 0],
        [0.5, 0.33, 0.667],
    ]
)
faces = np.hstack([[3, 0, 1, 2], [3, 0, 3, 2], [3, 0, 1, 3], [3, 1, 2, 3]])
mesh = pv.PolyData(vertices, faces)
mesh.cell_data['colors'] = [
    [255, 255, 255],
    [0, 255, 0],
    [0, 0, 255],
    [255, 0, 0],
]
pl = pv.Plotter()
_ = pl.add_mesh(
    mesh,
    scalars='colors',
    lighting=False,
    rgb=True,
    preference='cell',
)
pl.camera_position = 'xy'
pl.show()

# Note how this varies from `preference=='point'`.  This is
# because each point is now being individually colored, versus
# in `preference=='point'`, each cell face is individually
# colored.
pl = pv.Plotter()
_ = pl.add_mesh(
    mesh,
    scalars='colors',
    lighting=False,
    rgb=True,
    preference='point',
)
pl.camera_position = 'xy'
pl.show()

# Plot a plane with a constant color and vary its opacity by point.
plane = pv.Plane()
plane.plot(
    color='b',
    opacity=np.linspace(0, 1, plane.n_points),
    show_edges=True,
)

# Plot the points of a sphere with Gaussian smoothing while coloring by z
# position.
mesh = pv.Sphere()
mesh.plot(
    scalars=mesh.points[:, 2],
    style='points_gaussian',
    opacity=0.5,
    point_size=10,
    render_points_as_spheres=False,
    show_scalar_bar=False,
)

# Plot spheres using points_gaussian style and scale them by radius.
N_SPHERES = 1_000_000
rng = np.random.default_rng(seed=0)
pos = rng.random((N_SPHERES, 3))
rad = rng.random(N_SPHERES) * 0.01
pdata = pv.PolyData(pos)
pdata['radius'] = rad
pdata.plot(
    style='points_gaussian',
    emissive=False,
    render_points_as_spheres=True,
)

# ----------------------------------------------------------------------
# Generated by sphinx-examples-as-code https://github.com/pyvista/sphinx-examples-as-code

