# Examples from pyvista.PolyDataFilters.collision
# ===============================================

# Compute the collision between a sphere and the back faces of a
# cube and output the cell indices of the first 10 collisions.
import numpy as np
import pyvista as pv
mesh_a = pv.Sphere(radius=0.5)
mesh_b = pv.Cube(center=(0.5, 0.5, 0.5)).extract_cells([0, 2, 4])
collision, ncol = mesh_a.collision(mesh_b, cell_tolerance=1)
collision['ContactCells'][:10]

# Plot the collisions by creating a collision mask with the
# `"ContactCells"` field data.  Cells with a collision are
# colored red.
scalars = np.zeros(collision.n_cells, dtype=bool)
scalars[collision.field_data['ContactCells']] = True
pl = pv.Plotter()
_ = pl.add_mesh(
    collision,
    scalars=scalars,
    show_scalar_bar=False,
    cmap='bwr',
)
_ = pl.add_mesh(
    mesh_b,
    color='lightblue',
    line_width=5,
    opacity=0.7,
    show_edges=True,
)
pl.show()

# Alternatively, simply plot the collisions using the default
# `'collision_rgba'` array after enabling `generate_scalars`.
collision, ncol = mesh_a.collision(
    mesh_b, cell_tolerance=1, generate_scalars=True
)
collision.plot()

# See Collision for more examples using this filter.

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

