Skip to content

3D Rendering

This section demonstrates how to render MICRESS field data in 3D using MicPy.

While Field.plot() is suitable for quick 2D inspection, more advanced visualization, such as slicing, isosurfaces, and custom camera views, can be achieved by converting the data to VTK ImageData (vtkImageData) and using a VTK-based library like PyVista.

Minimal Example

from micpy.bin import read
import pyvista as pv

# Read a field (e.g. the last time step of a phase fraction)
field = read("A019_01_MgAl6_D_Dendritic_3D_Lin.frac", -1)

# Convert the field to VTK ImageData object
vti = field.as_vti()

# Render the VTK ImageData using PyVista
pv.plot(vti, cmap="micpy")

Dendritic microstructureDendritic microstructure

Extracting Slices

# Wrap the VTK ImageData object in a PyVista DataImage object
img = pv.wrap(vti)

# Create a slice along the x-axis
sl = img.slice(normal="x")

# Render the slice with the MicPy colormap
pv.plot(sl, cmap="micpy")

Slice along the x-axisSlice along the x-axis

# Wrap the VTK ImageData object in a PyVista DataImage object
img = pv.wrap(vti)

# Create a slice along the y-axis
sl = img.slice(normal="y")

# Render the slice with the MicPy colormap
pv.plot(sl, cmap="micpy")

Slice along the y-axisSlice along the y-axis

# Wrap the VTK ImageData object in a PyVista DataImage object
img = pv.wrap(vti)

# Create a slice along the z-axis
sl = img.slice(normal="z")

# Render the slice with the MicPy colormap
pv.plot(sl, cmap="micpy")

Slice along the z-axisSlice along the z-axis

# Wrap the VTK ImageData object in a PyVista DataImage object
img = pv.wrap(vti)

# Create a slice along the normal vector (1, 1, 0)
sl = img.slice(normal=[1, 1, 0])

# Render the slice with the MicPy colormap
pv.plot(sl, cmap="micpy")

Slice along the normal vector (1, 1, 0)Slice along the normal vector (1, 1, 0)

# Wrap the VTK ImageData object in a PyVista DataImage object
img = pv.wrap(vti)

# Create a slice along the x-axis at cell 123
sl = img.slice(normal="x", origin=(vti.spacing[0] * 123, img.center[1], img.center[2]))

# Render the slice with the MicPy colormap
pv.plot(sl, cmap="micpy")

Slice along the x-axis at cell 123Slice along the x-axis at cell 123

Visualizing an Isosurface

# Extract point data instead of cell data
vti_points = field.as_vti(data_mode="point_average")

# Wrap the VTK ImageData object in a PyVista DataImage object
img = pv.wrap(vti_points)

# Create an isosurface at 0.5 to visualize the phase boundaries
surface = img.contour(isosurfaces=[0.5])

# Render the isosurface with a grayscale colormap and no scalar bar
pv.plot(surface, cmap="lightgray", show_scalar_bar=False)

Isosurface at 0.5Isosurface at 0.5

Customizing the View

# Create a PyVista plotter and add the isosurface
plotter = pv.Plotter()
plotter.add_mesh(surface, cmap="lightgray", show_scalar_bar=False)

# Set the camera position to view along the xy-plane and zoom in by a factor of 1.33
plotter.camera_position = "xy"
plotter.camera.zoom(1.33)

# Optional: Adjust the camera angles (azimuth, elevation, roll)
# plotter.camera.azimuth = 180
# plotter.camera.elevation = 90
# plotter.camera.roll = 45

# Enable axes and show the plot
plotter.show_axes()
plotter.show()

Custom view along the xy-planeCustom view along the xy-plane

Adding Bounds

# Create a PyVista plotter and add the isosurface
plotter = pv.Plotter()
plotter.add_mesh(surface, cmap="lightgray", show_scalar_bar=False)

# Set the camera position to view along the xz-plane
plotter.camera_position = "xz"

# Add bounds with tick labels
plotter.show_bounds(grid="back", location="outer", ticks="outside", fmt="%.3g")

# Show the plot
plotter.show()

Isosurface with boundsIsosurface with bounds

Sampling Data

from micpy.bin import read
import pyvista as pv

# Read the phase fraction and concentration fields as VTK ImageData with point data
frac = read("A019_01_MgAl6_D_Dendritic_3D_Lin.frac", -1).as_vti(data_mode="point_average")
conc = read("A019_01_MgAl6_D_Dendritic_3D_Lin.concAl", -1).as_vti(data_mode="point_average")

# Extract the isosurface from the phase fraction field
surface = pv.wrap(frac).contour(isosurfaces=[0.5])

# Sample the concentration values at the points of the isosurface
surface_sampled = surface.sample(conc)

# Render the sampled isosurface with the MicPy colormap and a custom scalar bar
pv.plot(
    surface_sampled,
    cmap="micpy_r",
    scalar_bar_args=dict(title="Al [wt.%]", vertical=True, position_y=0.25, height=0.5),
)

Sampled isosurface with concentration dataSampled isosurface with concentration data