# Examples from pyvista.examples.cells.generate_cell_blocks
# =========================================================

# Generate a single `TRIANGLE` cell.
import pyvista as pv
from pyvista.examples import cells, generate_cell_blocks, plot_cell
triangle = generate_cell_blocks(pv.CellType.TRIANGLE)
plot_cell(triangle, cpos='xy')

# This is similar to the `Triangle()` grid, except it's a
# `MultiBlock` and its bounds are normalized to fit inside a 1x1x1 grid.
triangle

# Compare to the un-normalized bounds.
cells.Triangle()

# Use the `'parametric'` generator instead.
triangle = generate_cell_blocks(pv.CellType.TRIANGLE, 'parametric')
plot_cell(triangle, cpos='xy')

# Use the `'source'` generator instead.
triangle = generate_cell_blocks(pv.CellType.TRIANGLE, 'source')
plot_cell(triangle, cpos='xy')

# Generate multiple cell types. Here we generate all concrete linear 2D cells.
cell_types = [
    ctype
    for ctype in pv.CellType
    if ctype.dimension == 2 and ctype.is_linear
]
cell_types

cell_blocks = generate_cell_blocks(cell_types, shrink_factor=0.8)
plot_cell(cell_blocks, cpos='xy')

# Each block's name matches the name of the cell type.
cell_blocks.keys()

# Generate the same cell types using the parametric generator. This generator does not support
# triangle strip or polygon cells, so we skip these.
cell_blocks = generate_cell_blocks(
    cell_types, 'parametric', shrink_factor=0.8, unsupported_action='skip'
)
plot_cell(cell_blocks, cpos='xy')

# Use the source generator. It also does not support triangle strip, but it does support polygon.
# This time, we squeeze the blocks together instead of skipping them, and do not shrink them.
# This combination generates a continuous grid with no gaps.
cell_blocks = generate_cell_blocks(
    cell_types, 'source', unsupported_action='squeeze'
)
plot_cell(cell_blocks, cpos='xy')

# Generate cell types on a dimensioned grid.
lines = [pv.CellType.LINE] * 3
polygons = [pv.CellType.POLYGON] * 3
wedges = [pv.CellType.WEDGE] * 3
pyramids = [pv.CellType.PYRAMID] * 3
cell_types = [*lines, *polygons, *wedges, *pyramids]

# Plot them with 3 cells in the x-direction, and 4 cells in the y-direction.
cell_blocks = generate_cell_blocks(cell_types, block_dimensions=(3, 4, 1))
size_kwargs = dict(point_size=40, font_size=20)
plot_cell(cell_blocks, cpos='xy', **size_kwargs)

# Reverse the x and y dimension.
cell_blocks = generate_cell_blocks(cell_types, block_dimensions=(4, 3, 1))
plot_cell(cell_blocks, cpos='xy', **size_kwargs)

# Use the `'stop'` fill mode if there is a mismatch between the number of cell types and block
# dimensions. Here, the last two pyramid cell types are omitted.
cell_blocks = generate_cell_blocks(
    cell_types[:-2], block_dimensions=(3, 4, 1), fill_mode='stop'
)
plot_cell(cell_blocks, cpos='xy', **size_kwargs)

# Alternatively, cycle through the cell types again to completely fill the dimensions. In this
# case, the line type is reused to fill the gap.
cell_blocks = generate_cell_blocks(
    cell_types[:-2], block_dimensions=(3, 4, 1), fill_mode='cycle'
)
plot_cell(cell_blocks, cpos='xy', **size_kwargs)

# Generate a 5x5x5 grid comprised of all 3D concrete cell types with no gaps.
cell_types = [
    ctype
    for ctype in pv.CellType
    if ctype.dimension == 3 and ctype.vtk_class is not None
]
cell_blocks = generate_cell_blocks(
    cell_types,
    'source',
    block_dimensions=(5, 5, 5),
    unsupported_action='squeeze',
    fill_mode='cycle',
)
cell_blocks.plot(show_edges=True, opacity=0.5, line_width=3)

# Combine into a single grid and show `distinct_cell_types`.
cell_blocks.combine().distinct_cell_types

# Compare the first 25 cell types from the different generators. Note that some values, e.g.
# `17`, do not correspond to any cell type, so gaps are expected in all outputs.
kwargs = dict(
    cell_types=range(1, 26),
    block_dimensions=(5, 5, 1),
    unsupported_action='skip',
)
cell_blocks = generate_cell_blocks(generator='examples', **kwargs)
plot_cell(cell_blocks, cpos='xy', **size_kwargs)

cell_blocks = generate_cell_blocks(generator='parametric', **kwargs)
plot_cell(cell_blocks, cpos='xy', **size_kwargs)

cell_blocks = generate_cell_blocks(generator='source', **kwargs)
plot_cell(cell_blocks, cpos='xy', **size_kwargs)

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

