# Examples from pyvista.ImageDataFilters.concatenate
# ==================================================

# Load a 2D image: `download_beach()`.
import pyvista as pv
from pyvista import examples
beach = examples.download_beach()

# Use `select_values()` to make a second version with white values converted to black
# to distinguish it from the original.
white = [255, 255, 255]
black = [0, 0, 0]
beach_black = beach.select_values(white, fill_value=black, invert=True)

# Concatenate them along the x-axis.
concatenated = beach.concatenate(beach_black, axis='x')
plot_kwargs = dict(
    rgb=True,
    lighting=False,
    cpos='xy',
    zoom='tight',
    show_axes=False,
    show_scalar_bar=False,
)
concatenated.plot(**plot_kwargs)

# Concatenate them along the y-axis.
concatenated = beach.concatenate(beach_black, axis='y')
concatenated.plot(**plot_kwargs)

# By default, concatenation requires that all off-axis dimensions match the input. Use
# the `mode` keyword to enable concatenation with mismatched dimensions.
# Load a second 2D image with different dimensions:
# `download_bird()`.
bird = examples.download_bird()
bird.dimensions
beach.dimensions

# Concatenate using `'resample-proportional'` mode to preserve the aspect ratio of the
# concatenated image. Linear interpolation with antialiasing is used to avoid sampling
# artifacts.
resample_kwargs = {'interpolation': 'linear', 'anti_aliasing': True}
concatenated = beach.concatenate(
    bird, mode='resample-proportional', resample_kwargs=resample_kwargs
)
concatenated.dimensions
concatenated.plot(**plot_kwargs)

# Use `'resample-proportional'` again but concontenate along the z-axis instead. The
# `bird`'s aspect ratio is preserved and padded with zeros so that it can be stacked
# on top of `beach`.
concatenated = beach.concatenate(
    bird,
    axis='z',
    mode='resample-proportional',
    resample_kwargs=resample_kwargs,
)
concatenated.dimensions
concatenated.plot(**plot_kwargs)

# Use `'resample-off-axis'` to only resample off-axis dimensions. This option may
# distort the image.
concatenated = beach.concatenate(
    bird, mode='resample-off-axis', resample_kwargs=resample_kwargs
)
concatenated.dimensions
concatenated.plot(**plot_kwargs)

# Use `'resample-match'` to resample all dimensions to match the input exactly. This
# option may also distort the image.
concatenated = beach.concatenate(
    bird, mode='resample-match', resample_kwargs=resample_kwargs
)
concatenated.dimensions
concatenated.plot(**plot_kwargs)

# Use the `'preserve-extents'` mode. Using this mode naively may not produce the
# desired result, e.g. if we concatenate `beach` with `bird`, the `beach` image is
# completely overwritten since their `extent` fully overlap.
beach.extent
bird.extent

concatenated = beach.concatenate(bird, mode='preserve-extents')
concatenated.extent
concatenated.plot(**plot_kwargs)

# Set the `beach` `offset` so that there is only partial
# overlap instead.
beach.offset = (-50, -50, 0)
beach.extent

concatenated = beach.concatenate(bird, mode='preserve-extents')
concatenated.extent
concatenated.plot(**plot_kwargs)

# Reverse the concatenation order, and use a green background value.
green = pv.Color('green').int_rgb
concatenated = bird.concatenate(
    beach, mode='preserve-extents', background_value=green
)
concatenated.extent
concatenated.plot(**plot_kwargs)

# Use `'crop-off-axis'` to only crop off-axis dimensions.
concatenated = beach.concatenate(bird, mode='crop-off-axis')
concatenated.dimensions
concatenated.plot(**plot_kwargs)

# Reverse the concatenation order.
concatenated = bird.concatenate(beach, mode='crop-off-axis')
concatenated.dimensions
concatenated.plot(**plot_kwargs)

# Use `'crop-match'` to center-crop the images to match the input's
# dimensions.
concatenated = beach.concatenate(bird, mode='crop-match')
concatenated.dimensions
concatenated.plot(**plot_kwargs)

# Reverse the concatenation order, and use a grey background value.
concatenated = bird.concatenate(
    beach, mode='crop-match', background_value=128
)
concatenated.dimensions
concatenated.plot(**plot_kwargs)

# Load a binary image: `download_yinyang()`.
yinyang = examples.download_yinyang()

# Use `component_policy` to concatenate grayscale images with RGB(A) images.
concatenated = yinyang.concatenate(
    beach, mode='resample-proportional', component_policy='promote_rgba'
)
concatenated.plot(**plot_kwargs)

# The grayscale portion of the image is promoted to RGB in this case and the entire
# image has 3 components:
concatenated.active_scalars.shape

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

