view Package

vol_utils Module

pymicro.view.vol_utils.hist(data, nb_bins=256, data_range=(0, 255), show=True, save=False, prefix='data', density=False)

Histogram of a data array.

Compute and plot the gray level histogram of the provided data array.

Parameters

data: the data array to analyse.

nb_bins: the number of bins in the histogram.

data_range: the data range to use in the histogram, (0,255) by default.

show: boolean to display the figure using pyplot (defalut True).

save: boolean to save the figure using pyplot (defalut False).

prefix: a string to use in the file name when saving the histogram as an image (defaut ‘data’).

density: a boolean to control wether the histogram is plotted using the probability density function, see numpy histogram function for more details (default False).

HDPE_0001-2_512x512x512_uint8_hist.png

Gray level histogram computed on a 512x512x512 8 bits image.

pymicro.view.vol_utils.flat(img, ref, dark)

Apply flat field correction to an image.

Parameters
  • img (np.array) – A 2D array representing the image to correct.

  • ref (np.array) – The reference image (without the sample), same shape as the image to correct.

  • dark (np.array) – A 2D numpy array representing the dark image (thermal noise of the camera).

Returns np.array float

the flat field corrected image (between 0 and 1) as a float32 numpy array.

pymicro.view.vol_utils.min_max_cumsum(data, cut=0.001, verbose=False)

Compute the indices corresponding to the edge of a distribution.

The min and max indices are calculated according to a cutoff value (0.001 by default) meaning 99.99% of the distribution lies within this range.

pymicro.view.vol_utils.auto_min_max(data, cut=0.0002, nb_bins=256, verbose=False)

Compute the min and max values in a numpy data array.

The min and max values are calculated based on the histogram of the array which is limited at both ends by the cut parameter (0.0002 by default). This means 99.98% of the values are within the [min, max] range.

Parameters
  • data – the data array to analyse.

  • cut (float) – the cut off value to use on the histogram (0.0002 by default).

  • nb_bins (int) – number of bins to use in the histogram (256 by default).

  • verbose (bool) – activate verbose mode (False by default).

Returns tuple (val_mini, val_maxi)

a tuple containing the min and max values.

pymicro.view.vol_utils.region_growing(data, seed, min_thr, max_thr, structure=None)

Simple region growing segmentation algorithm.

This function segment a part of an image by selecting the connected region within a given gray value range. Underneath, the method uses the label function of the ndimage package.

Parameters
  • data – the numpy data array to segment.

  • seed (tuple) – the seed point.

  • min_thr (float) – the lower bound of the gray value range.

  • max_thr (float) – the upper bound of the gray value range.

  • structure – structuring element, to use (None by default).

Returns region

a bool data array with True values for the segmented region.

pymicro.view.vol_utils.recad(data, mini, maxi)

Cast a numpy array into 8 bit data type.

This function change the data type of a numpy array into 8 bit. The data values are interpolated from [min, max] to [0, 255]. Both min and max values may be chosen manually or computed by the function auto_min_max.

Parameters
  • data – the data array to cast to uint8.

  • mini (float) – value to use as the minimum (will be 0 in the casted array).

  • maxi (float) – value to use as the maximum (will be 255 in the casted array).

Returns data_uint8

the data array casted to uint8.

pymicro.view.vol_utils.alpha_cmap(color='red', opacity=1.0)

Creating a particular colormap with transparency.

Only values equal to 255 will have a non zero alpha channel. This is typically used to overlay a binary result on initial data.

Parameters
  • color – the color to use for non transparent values (ie. 255).

  • opacity (float) – opacity value to use for visible pixels.

Returns mycmap

a fully transparent colormap except for 255 values.

pymicro.view.vol_utils.stitch(image_stack, nh=2, nv=1, pattern='E', hmove=None, vmove=None, adjust_bc=False, adjust_bc_nbins=256, verbose=False, show=True, save=False, save_name='stitch', save_ds=1, check=None)

Stich a series of images together.

Parameters
  • image_stack (list) – a list of the images to stitch.

  • nh (int) – number of images to stitch horizontally.

  • nv (int) – number of images to stitch vertically.

  • pattern (str) – stitching pattern (‘E’ or ‘S’).

  • hmove (tuple) – horizontal move between 2 images (first image width by default).

  • vmove (tuple) – vertical move between 2 images (first image height by default).

  • adjust_bc (bool) – adjust gray levels by comparing the histograms in the overlapping regions (False by default).

  • adjust_bc_nbins (int) – numbers of bins to use tin the histograms when adjusting the gray levels (256 by default).

  • verbose (bool) – activate verbose mode (False by default).

  • show (bool) – show the stitched image (True by default).

  • save (bool) – flag to save the image as png to the disk (False by default).

  • save_name (str) – name to use to save the stitched image (stitch by default).

  • save_ds (int) – downsampling factor using when saving the stitched image (1 by default).

  • check (int) – trigger plotting for the given image if set.

Returns full_im

the stitched image in [x, y] form.

pymicro.view.vol_utils.compute_affine_transform(fixed, moving)

Compute the affine transform by point set registration.

The affine transform is the composition of a translation and a linear map. The two ordered lists of points must be of the same length larger or equal to 3. The order of the points in the two list must match.

The 2D affine transform \(\mathbf{A}\) has 6 parameters (2 for the translation and 4 for the linear transform). The best estimate of \(\mathbf{A}\) can be computed using at least 3 pairs of matching points. Adding more pair of points will improve the quality of the estimate. The matching pairs are usually obtained by selecting unique features in both images and measuring their coordinates.

Parameters
  • fixed (list) – a list of the reference points.

  • moving (list) – a list of the moving points to register on the fixed point.

Returns translation, linear_map

the computed translation and linear map affine transform.

Thanks to Will Lenthe for helping with this code.

vtk_anim Module

The vtk_anim module define a set of classes to generate 3d animations with vtk in the form of a series of png images.

class pymicro.view.vtk_anim.vtkAnimationScene(ren, ren_size=(600, 600))

Bases: object

add_animation(anim)
write_image()
execute(iren, event)
render()
render_at(time=0.0)
class pymicro.view.vtk_anim.vtkAnimation(t, duration=10)

Bases: object

pre_execute()
post_execute(iren, event)
class pymicro.view.vtk_anim.vtkAnimCameraAroundZ(t, cam, turn=360)

Bases: pymicro.view.vtk_anim.vtkAnimation

Animate the camera around the vertical axis.

This class can be used to generate a series of images (default 36) while the camera rotate around the vertical axis (defined by the camera SetViewUp method).

execute(iren, event)

Execute method called to rotate the camera.

class pymicro.view.vtk_anim.vtkRotateActorAroundAxis(t=0, duration=10, axis=(0.0, 0.0, 1.0), angle=360)

Bases: pymicro.view.vtk_anim.vtkAnimation

set_actor(actor)

Set the actor for this animation.

This also keep a record of the actor initial transformation matrix.

Parameters

actor (vtkActor) – the actor on which to apply the animation.

execute(iren, event)

instruction block executed when a TimerEvent is captured by the vtkRotateActorAroundAxis.

If the time is not in [start, end] nothing is done. Otherwise the transform matrix corresponding to the 3D rotation is applied to the actor.

The transform matrix for this increment is the result of the multiplication of the rotation matrix for the current angle with the initial 4x4 matrix before any rotation (we keep a record of this in the user_transform_matrix attribute).

Parameters
  • iren (vtkRenderWindowInteractor) – the vtk render window interactor.

  • event – the captures event.

class pymicro.view.vtk_anim.vtkRotateActorAroundZAxis(t=0)

Bases: pymicro.view.vtk_anim.vtkRotateActorAroundAxis

class pymicro.view.vtk_anim.vtkAnimCameraToZ(t, cam)

Bases: pymicro.view.vtk_anim.vtkAnimation

execute(iren, event)
class pymicro.view.vtk_anim.vtkZoom(t, cam, zoom)

Bases: pymicro.view.vtk_anim.vtkAnimation

execute(iren, event)
class pymicro.view.vtk_anim.vtkSetVisibility(t, actor, visible=1, max_opacity=1, gradually=False)

Bases: pymicro.view.vtk_anim.vtkAnimation

execute(iren, event)
class pymicro.view.vtk_anim.vtkMoveActor(t, actor, motion)

Bases: pymicro.view.vtk_anim.vtkAnimation

execute(iren, event)
class pymicro.view.vtk_anim.vtkAnimLine(points, t1, t2)

Bases: pymicro.view.vtk_anim.vtkAnimation

execute(iren, event)
class pymicro.view.vtk_anim.vtkUpdateText(text_actor, str_method, t=0, duration=10)

Bases: pymicro.view.vtk_anim.vtkAnimation

execute(iren, event)

vtk_utils Module

pymicro.view.vtk_utils.to_vtk_type(type)

Function to get the VTK data type given a numpy data type.

Parameters

type (str) – The numpy data type like ‘uint8’, ‘uint16’…

Returns

A VTK data type.

pymicro.view.vtk_utils.rand_cmap(N=256, first_is_black=False, table_range=(0, 255))

Create a VTK lookup table with random colors.

The first color can be enforced to black and usually figure out the image background. The random seed is fixed to 13 in order to consistently produce the same colormap.

Parameters
  • N (int) – The number of colors in the colormap.

  • first_is_black (bool) – Force the first color to be black.

  • table_range (typle) – The range of the VTK lookup table

Returns

A vtkLookupTable lookup table with N random colors.

pymicro.view.vtk_utils.pv_rand_cmap(N=256, first_is_black=False)

Write out the random color map in paraview xml format.

This method print out the XML declaration of the random colormap. This may be saved to a text file and used in paraview.

Parameters
  • N (int) – The number of colors in the colormap.

  • first_is_black (bool) – Force the first color to be black.

pymicro.view.vtk_utils.pyplot_cmap(name='viridis', table_range=(0, 255))

Create a VTK colormap from pyplot.

Parameters

name (str) – the color map name to import from pyplot.

Returns

pymicro.view.vtk_utils.gray_cmap(table_range=(0, 255))

create a black and white colormap.

Parameters

table_range: 2 values tuple (default: (0,255)) start and end values for the table range.

Returns

A vtkLookupTable from black to white.

pymicro.view.vtk_utils.invert_cmap(ref_lut)

invert a VTK lookup table.

Parameters

ref_lut: The lookup table to invert.

Returns

A reverse vtkLookupTable.

pymicro.view.vtk_utils.hsv_cmap(N=64, table_range=(0, 255))

Create a VTK look up table similar to matlab’s hsv.

Parameters

N: int, number of colors in the table.

table_range: 2 values tuple (default: (0,255)) start and end values for the table range.

Returns

A vtkLookupTable.

pymicro.view.vtk_utils.jet_cmap(N=64, table_range=(0, 255))

Create a VTK look up table similar to matlab’s jet.

Parameters

N: int, number of colors in the table.

table_range: 2 values tuple (default: (0,255)) start and end values for the table range.

Returns

A vtkLookupTable from blue to red.

pymicro.view.vtk_utils.hot_cmap(table_range=(0, 255))

Create a VTK look up table similar to matlab’s hot.

Parameters

table_range: 2 values tuple (default: (0,255)) start and end values for the table range.

Returns

A vtkLookupTable from white to red.

pymicro.view.vtk_utils.add_hklplane_to_grain(hkl_plane, grid, orientation, origin=(0, 0, 0), opacity=1.0, show_normal=False, normal_length=1.0, show_intersection=False, color_intersection=<Mock name='mock.red' id='139657957593168'>)

Add a plane describing a crystal lattice plane to a VTK grid.

Parameters
  • hkl_plane – an instance of HklPlane describing the lattice plane.

  • grid – a vtkunstructuredgrid instance representing the geometry of the grain.

  • orientation – the grain orientation.

  • origin – the origin of the plane in the grain.

  • opacity (float) –

  • show_normal – A flag to show the plane normal.

  • normal_length – The length of the plane normal.

  • show_intersection – A flag to show the intersection of the plane with the grain.

  • color_intersection (tuple) – The color to display the intersection of the plane with the grain.

Returns

A VTK assembly with the grain, the plane, its normal and edge intersection if requested.

pymicro.view.vtk_utils.add_slip_system_to_grain(slip_system, grid, orientation, origin=(0, 0, 0), opacity=1.0, show_normal=False, normal_length=1.0, show_intersection=False, color_intersection=<Mock name='mock.red' id='139657957593168'>)

Add a slip system to a VTK grid.

Parameters
  • slip_system – an instance of SlipSystem describing the slip system.

  • grid – a vtkunstructuredgrid instance representing the geometry of the grain.

  • orientation – the grain orientation.

  • origin – the origin of the plane in the grain.

  • opacity (float) – opacity value of the plane.

  • show_normal – A flag to show the plane normal.

  • normal_length – The length of the plane normal.

  • show_intersection – A flag to show the intersection of the plane with the grain.

  • color_intersection (tuple) – The color to display the intersection of the plane with the grain.

Returns

A VTK assembly with the grain, the plane, its normal and edge intersection if requested and the slip

direction.

pymicro.view.vtk_utils.add_plane_to_grid(plane, grid, origin=None, color=None, opacity=0.3, show_normal=False, normal_length=1.0, show_intersection=False, color_intersection=<Mock name='mock.red' id='139657957593168'>)

Add a 3d plane inside another object.

This function adds a plane inside another object described by a mesh (vtkunstructuredgrid). The method is to use a vtkCutter with the mesh as input and the plane as the cut function. The plane normal can be displayed and its length controlled. This may be used directly to add hkl planes inside a lattice cell or a grain.

Parameters
  • plane – A VTK implicit function describing the plane to add.

  • grid – A VTK unstructured grid in which the plane is to be added.

  • origin (tuple) – (x, y, z) tuple to define the origin of the plane.

  • color (tuple) – A color defined by its rgb components.

  • opacity (float) – Opacity value of the plane actor.

  • show_normal (bool) – A flag to display the plane normal.

  • normal_length – The length of the plane normal vector (1.0 by default).

  • show_intersection (bool) – Also add the intersection of the plane with the grid in the assembly.

  • color_intersection (tuple) – The color to display the intersection of the plane with the grid.

Returns

A VTK assembly with the mesh, the plane, its normal and edge intersection if requested.

pymicro.view.vtk_utils.axes_actor(length=1.0, axisLabels=('x', 'y', 'z'), fontSize=20, color=None)

Build an actor for the cartesian axes.

Parameters
  • length (float or triple of float to specify the length of each axis individually.) – The arrow length of the axes (1.0 by default).

  • axisLabels (list) – Specify the axes labels (xyz by default), use axisLabels = None to hide the axis labels

  • fontSize (int) – Font size for the axes labels (20 by default).

  • color (tuple) – A single color defined by its rgb components (not set by default which keep the red, green, blue colors).

Returns

A VTK assembly representing the cartesian axes.

pymicro.view.vtk_utils.grain_3d(grain, hklplanes=None, show_normal=False, plane_origins=None, plane_opacity=1.0, show_orientation=False, N=2048, verbose=False)

Creates a 3d representation of a crystallographic grain.

This method creates a vtkActor object of the surface mesh representing a Grain object. An optional list of crystallographic planes can be given

Parameters
  • grain – the Grain object to be shown in 3d.

  • hklplanes (list) – the list of HklPlanes object to add to the assembly.

  • show_normal (bool) – show also the normal to the hkl planes if True.

  • plane_origins (list) – the list of each plane origin (3d scene coordinates).

  • plane_opacity (float) – set the opacity of the grain actor.

  • show_orientation (bool) – show also the grain orientation with a vtkAxesActor placed at the grain center if True.

  • N (int) – the number of colors to use in the colormap.

  • verbose (bool) – activate verbose mode.

Returns

a vtkAssembly of the grain mesh and the optional hkl planes.

pymicro.view.vtk_utils.add_grain_to_3d_scene(grain, hklplanes, show_orientation=False)
pymicro.view.vtk_utils.add_local_orientation_axes(orientation, axes_length=30)
pymicro.view.vtk_utils.add_HklPlanes_with_orientation_in_grain(grain, hklplanes=[])

Add some plane actors corresponding to a list of (hkl) planes to a grain actor.

pymicro.view.vtk_utils.unit_arrow_3d(start, vector, color=<Mock name='mock.orange' id='139657957623824'>, radius=0.03, make_unit=True, label=False, text=None, text_scale=0.1, vector_normal=None)
pymicro.view.vtk_utils.lattice_points(lattice, origin=(0.0, 0.0, 0.0), m=1, n=1, p=1)

Create a vtk representation of a the lattice points.

A vtkPoints instance is used to store the lattice points, including the points not on the lattice corners according to the system centering (may be P, I, F for instance).

Parameters
  • lattice (Lattice) – The Lattice instance from which to construct the points.

  • origin (tuple) – cartesian coordinates of the origin.

  • m (int) – the number of cells in the [100] direction (1 by default).

  • n (int) – the number of cells in the [010] direction (1 by default).

  • p (int) – the number of cells in the [001] direction (1 by default).

Returns

A vtkPoints with all the lattice points ordered such that the first 8*(m*n*p) points describe the lattice cells.

pymicro.view.vtk_utils.lattice_grid(lattice, origin=(0.0, 0.0, 0.0), m=1, n=1, p=1)

Create a mesh representation of a crystal lattice.

A vtkUnstructuredGrid instance is used with a hexaedron element corresponding to the lattice system. Any number of cells can be displayed (just one by default).

Parameters
  • lattice (Lattice) – The Lattice instance from which to construct the grid.

  • origin (tuple) – cartesian coordinates of the origin.

  • m (int) – the number of cells in the [100] direction (1 by default).

  • n (int) – the number of cells in the [010] direction (1 by default).

  • p (int) – the number of cells in the [001] direction (1 by default).

Returns

A vtkUnstructuredGrid with (m x n x p) hexaedron cell representing the crystal lattice.

pymicro.view.vtk_utils.hexagonal_lattice_grid(lattice, origin=[0.0, 0.0, 0.0])

Create a mesh representation of a hexagonal crystal lattice.

A vtkUnstructuredGrid instance is used with a hexagonal prism element corresponding to 3 unit cells of the lattice system.

Parameters
  • lattice (Lattice) – The Lattice instance from which to construct the grid.

  • origin (tuple) – cartesian coordinates of the origin.

Returns

A vtkUnstructuredGrid one hexagnal prism cell representing the crystal lattice.

pymicro.view.vtk_utils.lattice_edges(grid, tubeRadius=0.02, tubeColor=<Mock name='mock.grey' id='139657957616400'>)

Create the 3D representation of crystal lattice edges.

Parameters

grid: vtkUnstructuredGrid The vtkUnstructuredGrid instance representing the crystal lattice.

tubeRadius: float Radius of the tubes representing the atomic bonds (default: 0.02).

tubeColor: vtk color Color of the tubes representing the atomis bonds (default: grey).

Returns

The method return a vtk actor for lattice edges.

pymicro.view.vtk_utils.lattice_vertices(grid, sphereRadius=0.1, sphereColor=<Mock name='mock.blue' id='139657957623120'>)

Create the 3D representation of crystal lattice atoms.

Parameters

grid: vtkUnstructuredGrid The vtkUnstructuredGrid instance representing the crystal lattice.

sphereRadius: float Size of the spheres representing the atoms (default: 0.1).

sphereColor: vtk color Color of the spheres representing the atoms (default: blue).

Returns

The method return a vtk actor for lattice vertices.

pymicro.view.vtk_utils.crystal_vertices(crystal, origin=(0.0, 0.0, 0.0), m=1, n=1, p=1, hide_outside=True)

Create the 3D representation of the atoms in a given crystal, taking into account the crystal lattice and the basis which can be composed of any motif.

Parameters
  • origin (tuple) – cartesian coordinates of the origin.

  • m (int) – the number of cells in the [100] direction (1 by default).

  • n (int) – the number of cells in the [010] direction (1 by default).

  • p (int) – the number of cells in the [001] direction (1 by default).

  • hide_outside (bool) – do not displays atoms outside the displayed unit cells if True.

Returns

The method return a vtk actor with all the crystal atoms.

pymicro.view.vtk_utils.crystal_3d(crystal, origin=(0.0, 0.0, 0.0), m=1, n=1, p=1, sphereRadius=0.1, tubeRadius=0.02, sphereColor=<Mock name='mock.blue' id='139657957623120'>, tubeColor=<Mock name='mock.grey' id='139657957616400'>, hide_outside=True)
pymicro.view.vtk_utils.lattice_3d(lattice, origin=(0.0, 0.0, 0.0), m=1, n=1, p=1, sphereRadius=0.05, tubeRadius=0.02, sphereColor=<Mock name='mock.black' id='139657957614736'>, tubeColor=<Mock name='mock.grey' id='139657957616400'>, crystal_orientation=None, show_atoms=True, show_edges=True, cell_clip=False)

Create the 3D representation of a crystal lattice.

The lattice edges are shown using a vtkTubeFilter and the atoms are displayed using spheres. Both tube and sphere radius can be controlled. Crystal orientation can also be provided which rotates the whole assembly appropriately.

The origin of the actor can be t=either specified directly using a tuple or set using a string as follow:

  • mid the middle of the lattice cell(s)

l = Lattice.cubic(1.0)
cubic = lattice_3d(l)
ren = vtk.vtkRenderer()
ren.AddActor(cubic)
render(ren, display=True)
lattice_3d

A 3D view of a cubic lattice.

Parameters
  • lattice (Lattice) – The Lattice instance representing the crystal lattice.

  • origin (tuple or string) – cartesian coordinates of the origin.

  • m (int) – the number of cells in the [100] direction (1 by default).

  • n (int) – the number of cells in the [010] direction (1 by default).

  • p (int) – the number of cells in the [001] direction (1 by default).

  • sphereRadius (float) – Size of the spheres representing the atoms (default: 0.05).

  • tubeRadius (float) – Radius of the tubes representing the atomic bonds (default: 0.02).

  • sphereColor (tuple) – Color of the spheres representing the atoms (default: black).

  • tubeColor (tuple) – Color of the tubes representing the atomis bonds (default: grey).

  • crystal_orientation – The crystal Orientation with respect to the sample coordinate system (default: None).

  • show_atoms (bool) – Control if the atoms are shown (default: True).

  • show_edges (bool) – Control if the eges of the lattice are shown (default: True).

  • cell_clip (bool) – Clip the lattice points glyphs by the cell (default: False).

Returns

The method return a vtk assembly combining lattice edges and vertices.

pymicro.view.vtk_utils.lattice_3d_with_planes(lattice, hklplanes, plane_origins=None, plane_colors=None, show_normal=True, plane_opacity=1.0, **kwargs)

Create the 3D representation of a crystal lattice.

HklPlanes can be displayed within the lattice cell with their normals. A single vtk actor in form of an assembly is returned. Additional parameters are passed to the lattice_3d method to control how the lattice is pictured.

l = Lattice.cubic(1.0)
o = Orientation.from_euler([344.0, 125.0, 217.0])
hklplanes = Hklplane.get_family('111')
cubic = lattice_3d_with_planes(l, hklplanes, show_normal=True, \
  plane_opacity=0.5, crystal_orientation=o)
s3d = Scene3D()
s3d.add(cubic)
s3d.render()
lattice_3d_with_planes

A 3D view of a cubic lattice with all four 111 planes displayed.

Parameters
  • lattice – An instance of Lattice corresponding to the crystal lattice to be displayed.

  • hklplanes – A list of HklPlane instances to add to the lattice.

  • plane_origins – A list of tuples describing the plane origins (must be the same length as hklplanes), if None, the planes are created to pass through the middle of the lattice (default).

  • plane_colors – A list of tuples describing the plane colors (must be the same length as hklplanes), if None, the planes are left gray (default).

  • show_normal (bool) – Control if the slip plane normals are shown (default: True).

  • plane_opacity (float) – A float number in the [0.,1.0] range controlling the slip plane opacity.

  • **kwargs

    additional parameters are passed to the lattice_3d method.

Returns

The method return a vtkAssembly that can be directly added to a renderer.

pymicro.view.vtk_utils.lattice_3d_with_plane_series(lattice, hkl, nps=1, **kwargs)

Create the 3D representation of a crystal lattice with a series of hkl planes.

HklPlanes can be displayed within the lattice cell with their normals. A single vtk actor in form of an assembly is returned. Additional parameters are passed to the lattice_3d method to control how the lattice is pictured.

l = Lattice.cubic(1.0)
orientation = Orientation.from_euler([0, 54.74, 135])  # correspond to 111 fiber texture
copper = (1.000000, 0.780392, 0.494117)  # nice copper color
copper_lattice = lattice_3d_with_plane_series(l, (1, -1, 1), nps=4, crystal_orientation=orientation, \
    origin='mid', show_atoms=True, sphereColor=copper, sphereRadius=0.1)
s3d = Scene3D()
s3d.add(cubic)
s3d.render()
Cu111_with_planes

A 3D view of a copper lattice with a series of successive (111) planes displayed.

Parameters
  • lattice – An instance of Lattice corresponding to the crystal lattice to be displayed.

  • hkl – A tuple of the 3 miller indices.

  • nps (int) – The number of planes to display in the series (1 by default).

  • **kwargs

    additional parameters are passed to the lattice_3d_with_planes method.

Returns

The method return a vtkAssembly that can be directly added to a renderer.

pymicro.view.vtk_utils.pole_figure_3d(pf, radius=1.0, show_lattice=False)

Method to display a pole figure in 3d.

This method displays a sphere of radius 1 with a crystal lattice placed at the center (only shown if the show_lattice parameter is True). The poles associated with the pole figure are shown on the outbounding sphere as well as the projection n the equatorial plane.

Parameters
  • pf – the PoleFigure instance .

  • radius (float) – the outbounding sphere radius (1 by default).

  • show_lattice – a flag to show the crystal lattice in the center of the sphere.

Returns

a vtk assembly that can be added to a Scene3D instance.

pymicro.view.vtk_utils.apply_translation_to_actor(actor, trans)

Transform the actor (or whole assembly) using the specified translation.

Parameters
  • actor (vtkActor) – the vtk actor.

  • trans – a 3 component numpy vector or sequence describing the translation to apply in scene units.

pymicro.view.vtk_utils.apply_rotation_to_actor(actor, R)

Transform the actor with a given rotation matrix.

Parameters
  • actor (vtkActor) – the vtk actor.

  • R – a (3x3) array representing the rotation matrix.

pymicro.view.vtk_utils.apply_orientation_to_actor(actor, orientation)

Transform the actor (or whole assembly) using the specified orientation. Here we could use the three euler angles associated with the orientation with the RotateZ and RotateX methods of the actor but the components of the orientation matrix are used directly since they are known without any ambiguity.

Parameters
pymicro.view.vtk_utils.load_STL_actor(name, ext='STL', verbose=False, color=<Mock name='mock.grey' id='139657957616400'>, feature_edges=False)

Read a STL file and return the corresponding vtk actor.

Parameters
  • name (str) – the base name of the file to read.

  • ext (str) – extension of the file to read.

  • verbose (bool) – verbose mode.

  • color (tuple) – the color to use for the actor.

  • feature_edges (bool) – show boundary edges (default False).

Returns

the 3d solid in the form of a vtk actor.

pymicro.view.vtk_utils.is_in_array(cad_path, step, origin=[0.0, 0.0, 0.0])

Function to compute the coordinates of the points within a CAD volume, with a given step.

pymicro.view.vtk_utils.read_image_data(file_name, size, header_size=0, data_type='uint8', verbose=False)

vtk helper function to read a 3d data file. The size is needed in the form (x, y, z) as well a string describing the data type in numpy format (uint8 is assumed by default). Lower file left and little endian are assumed.

Parameters

file_name: the name of the file to read.

size: a sequence of three numbers describing the size of the 3d data set

header_size: size of the header to skip in bytes (0 by default)

data_type: a string describing the data type in numpy format (‘uint8’ by default)

verbose: verbose mode (False by default)

Returns

A VTK data array

pymicro.view.vtk_utils.data_outline(data, corner=False, color=<Mock name='mock.black' id='139657957614736'>)

vtk helper function to draw a bounding box around a volume.

pymicro.view.vtk_utils.box_3d(origin=(0, 0, 0), size=(100, 100, 100), line_color=<Mock name='mock.black' id='139657957614736'>)

Create a box of a given size.

Parameters
  • origin (tuple) – the origin of the box in the laboratory frame.

  • size (tuple) – the size of the box.

  • line_color – the color to use to draw the box.

Returns

pymicro.view.vtk_utils.detector_3d(detector, image_name=None, show_axes=False, see_reference=True)

Create a 3D detector on a 3D scene, using all tilts. See_reference allow to plot an empty detector with dashed edge without any tilts.

Parameters
  • detector – RegArrayDetector2d

  • image_name – str of the image to plot in the 3D detector ‘image.png’

Returns

vtk actor

pymicro.view.vtk_utils.build_line_mesh(points)

Function to construct a vtkUnstructuredGrid representing a line mesh.

Parameters

points (list) – the list of points.

Returns line_mesh

the vtkUnstructuredGrid.

pymicro.view.vtk_utils.line_actor(line_grid)
pymicro.view.vtk_utils.line_3d(start_point, end_point)

Function to draw a line in a 3d scene.

Parameters
  • start_point (tuple) – the line starting point.

  • end_point (tuple) – the line ending point.

Returns vtkActor

The method return a vtkActor of the line that can be directly added to a 3d scene.

pymicro.view.vtk_utils.circle_line_3d(center=(0, 0, 0), radius=1, normal=(0, 0, 1), resolution=1)

Function to draw a circle in a 3d scene.

Parameters
  • center (tuple) – the center of the circle.

  • radius (float) – the radius of the circle.

  • normal (tuple) – the normal to the plane of the circle.

  • resolution (float) – the resolution in degree.

Returns vtkActor

The method return a vtkActor that can be directly added to a 3d scene.

pymicro.view.vtk_utils.point_cloud_3d(data_points, point_color=(0, 0, 0), point_size=1)

Function to display a point cloud in a 3d scene.

Parameters
  • data_points (list) – the list of points in he cloud.

  • point_color (tuple) – the color to use to display the points.

  • point_size (float) – the size of the points.

Returns

The method return a vtkActor of the point cloud that can be directly added to a 3d scene.

pymicro.view.vtk_utils.contourFilter(data, value, color=<Mock name='mock.grey' id='139657957616400'>, diffuseColor=<Mock name='mock.grey' id='139657957616400'>, opacity=1.0, discrete=False)

This method create an actor running a contour filter through the given data set.

The data set can be equally given in numpy or VTK format (it will be converted to VTK if needed). The method may require a fair amount of memory so downsample your data if you can.

Params data

the dataset to map, in numpy or VTK format.

Params float value

numeric value to use for contouring.

Params color

the solid color to use for the contour actor.

Params diffuseColor

the diffusive color to use for the contour actor.

Params float opacity

the opacity value to use for the actor (1.0 by default).

Params bool discrete

use vtkDiscreteMarchingCubes if True (False by default).

Returns

The method return a vtkActor that can be directly added to a renderer.

pymicro.view.vtk_utils.volren(data, alpha_channel=None, color_function=None)

Volume rendering for a 3d array using standard ray casting.

Parameters
  • data – the dataset to render, in numpy or VTK format.

  • alpha_channel – a vtkPiecewiseFunction instance, default to linear between 0 and 255 if not given.

Returns

The method return a vtkVolume that can be added to a renderer.

pymicro.view.vtk_utils.elevationFilter(data, value, low_high_range, low_point=None, high_point=None)

Create an isosurface and map it with an elevation filter.

Parameters
  • data – the dataset to map, in VTK format.

  • value (float) – the value to use to create the isosurface.

  • low_high_range (tuple) – range to use in the elevation filter in the form (low, high).

  • low_point (tuple) – lower point defining the axis from which to compute the elevation.

If not specified, (0, 0, low) is assumed. :param tuple high_point: lower point defining the axis from which to compute the elevation. If not specified, (0, 0, high) is assumed. :returns vtkActor: The method return an actor that can be directly added to a renderer.

pymicro.view.vtk_utils.numpy_array_to_vtk_grid(data, cell_data=True, array_name=None)

Transform a 3d numpy data array into a vtk uniform grid with scalar data.

Parameters
  • data – the 3d numpy data array, possibly with 3 components using a 4th dimension.

  • cell_data (bool) – flag to assign cell data, as opposed to point data, to the grid (True by default).

  • array_name (str) – an optional name for the vtk array.

Return vtkUniformGrid

The method return a vtkUniformGrid with scalar data initialized from

the provided numpy array.

pymicro.view.vtk_utils.map_data_with_clip(data, lut=<Mock name='mock.vtkLookupTable()' id='139657957644176'>, cell_data=True)

This method construct an actor to map a 3d dataset.

1/8 of the data is clipped out to have a better view of the interior. It requires a fair amount of memory so downsample your data if you can (it may not be visible at all on the resulting image).

data = read_image_data(im_file, size)
ren = vtk.vtkRenderer()
actor = map_data_with_clip(data)
ren.AddActor(actor)
render(ren, display=True)
pa66gf30_clip_3d

A 3D view of a polyamid sample with reinforcing glass fibers.

Parameters

data: the dataset to map, in numpy or VTK format.

lut: VTK look up table (default: gray_cmap).

cell_data: boolean to map cell data or point data if False (True by default)

Returns

The method return a vtkActor that can be directly added to a renderer.

pymicro.view.vtk_utils.map_data(data, function, lut=<Mock name='mock.vtkLookupTable()' id='139657957644176'>, cell_data=True)

This method construct an actor to map a 3d dataset.

It requires a fair amount of memory so downsample your data if you can (it may not be visible at all on the resulting image).

Parameters

data: the dataset to map, in numpy or VTK format.

function: VTK implicit function where to map the data.

lut: VTK look up table (default: gray_cmap).

cell_data: boolean to map cell data or point data if False (True by default)

Returns

The method return a vtkActor that can be directly added to a renderer.

pymicro.view.vtk_utils.set_opacity(assembly, opacity)
pymicro.view.vtk_utils.color_bar(title, lut=None, fmt='%.1e', width=0.5, height=0.075, num_labels=7, font_size=26)
pymicro.view.vtk_utils.text(text, font_size=20, color=(0, 0, 0), hor_align='center', coords=(0.5, 0.5))

Create a 2D text actor to add to a 3d scene.

Params int font_size

the font size (20 by default).

Params tuple color

the face color (black by default).

Params str hor_align

horizontal alignment, should be ‘left’, ‘center’ or ‘right’ (center by default).

Params tuple coords

a sequence of two values between 0 and 1.

:returns an actor for the text to add to a renderer.

pymicro.view.vtk_utils.setup_camera(size=(100, 100, 100))

Setup the camera with usual viewing parameters.

The camera is looking at the center of the data with the Z-axis vertical.

Parameters

size: the size of the 3d data set (100x100x100 by default).

pymicro.view.vtk_utils.render(ren, ren_size=(600, 600), display=True, save=False, name='render_3d.png', key_pressed_callback=None)

Render the VTK scene in 3D.

Given a vtkRenderer, this function does the actual 3D rendering. It can be used to display the scene interactlively and/or save a still image in png format.

Parameters

ren: the VTK renderer with containing all the actors.

ren_size: a tuple with two value to set the size of the image in pixels (defalut 600x600).

display: a boolean to control if the scene has to be displayed interactively to the user (default True).

save: a boolean to to control if the scene has to be saved as a png image (default False).

name: a string to used when saving the scene as an image (default is ‘render_3d.png’).

key_pressed_callback a function (functions are first class variables) called in interactive mode when a key is pressed.

pymicro.view.vtk_utils.extract_poly_data(grid, inside=True, boundary=True)

Convert from vtkUnstructuredGrid to vtkPolyData.

Parameters
  • grid – the vtkUnstructuredGrid instance.

  • inside (bool) – flag to extract inside cells or not.

  • boundary (bool) – flag to include boundary cells.

Returns

the vtkExtractGeometry filter.

pymicro.view.vtk_utils.select(grid, id_list, verbose=False)

Select cells in vtkUnstructuredGrid based on a list of indices.

Parameters
  • grid (vtkUnstructuredGrid) – the grid on which to perform the selection.

  • id_list (list) – the indices of the cells to select.

  • verbose (bool) – a flag to activate verbose mode.

Returns

a new vtkUnstructuredGrid containing the selected cells.

pymicro.view.vtk_utils.show_array(data, map_scalars=False, lut=None, hide_zero_values=True)

Create a 3d actor representing a numpy array.

Given a 3d array, this function compute the skin of the volume. The scalars can be mapped to the created surface and the colormap adjusted. If the data is in numpy format it is converted to VTK first.

Parameters
  • data – the dataset, in numpy or VTK format.

  • map_scalars (bool) – map the scalar in the data array to the created

surface (False by default). :param lut: a vtk lookup table (colormap) used to map the scalars. :param bool hide_zero_values: blank cells with a value of zero (True by default) :return: a vtk actor that can be added to a rendered to show the 3d array.

pymicro.view.vtk_utils.show_mesh(grid, map_scalars=False, lut=None, show_edges=False, edge_color=(0.0, 0.0, 0.0), edge_line_width=1.0)

Create a 3d actor representing a mesh.

Parameters
  • grid – the vtkUnstructuredGrid object.

  • map_scalars (bool) – map the scalar in the data array to the created surface (False by default).

  • lut – a vtk lookup table (colormap) used to map the scalars.

  • show_edges (bool) – display the mesh edges (False by default).

  • edge_color (tuple) – color to use for the mesh edges (black by default).

  • edge_line_width (float) – width of the edge lines (1.0 by default).

Returns

a vtk actor that can be added to a rendered to show the 3d array.

pymicro.view.vtk_utils.show_grains(data, num_colors=2048)

Create a 3d actor of all the grains in a labeled numpy array.

Given a 3d numpy array, this function compute the skin of all the grains (labels > 0). the background is assumed to be zero and is removed. The actor produced is colored by the grain ids using the random color map, see rand_cmap.

Parameters

data: a labeled numpy array.

num_colors: number of colors in the lookup table (2048 by default)

Returns a vtk actor that can be added to a rendered to show all the grains colored by their id.

pymicro.view.vtk_utils.show_boundaries(grid, array_id=0, array_name=None, write=False)

Create an actor representing the boundaries separating different values of a given array. The values have to be one of the integer type.

Parameters
  • grid (vtkUnstructuredGrid) – the unstructured grid referencing the data array.

  • array_id (int) – the index of the array to process (default 0).

  • array_name (str) – the name of the array to process.

  • write (bool) – flag to write the boundary polydata to the disk.

Returns

a VTK actor containing the boundaries.

pymicro.view.vtk_utils.edges_actor(polydata, linewidth=1.0, linecolor=<Mock name='mock.black' id='139657957614736'>)
pymicro.view.vtk_utils.xray_arrow()
pymicro.view.vtk_utils.slits(size, x_slits=0)

Create a 3d schematic represenation of X-ray slits.

The 3d represenation is made of 4 corners of the given size along the Y and Z axes.

Parameters:

size: a (X,Y,Z) tuple giving the size of the illuminated volume. The first value of the tuple is not used.

x_slits: position of the slits along the X axis (0 be default).

Returns:

A vtk assembly of the 4 corners representing the slits.

pymicro.view.vtk_utils.pin_hole(inner_radius=100, outer_radius=200)
pymicro.view.vtk_utils.zone_plate(thk=50, sep=25, n_rings=5)

Create a 3d schematic represenation of a Fresnel zone plate.

The 3d represenation is made of a number or concentric rings separated by a specific distance which control the X-ray focalisation.

Parameters:

thk: ring thickness (50 by default).

sep: ring spacing (25 by default).

Returns:

A vtk assembly of the rings composing the Fresnel zone plate.

pymicro.view.vtk_utils.grid_vol_view(scan)
pymicro.view.vtk_utils.vol_view(scan)
pymicro.view.vtk_utils.ask_for_map_file(dir, scan_name)