pymeshtool

create_image(shape, itk_dtype, *, num_comp=1, origin=None, voxel_size=None)

Create an empty image from a given shape and type.

Parameters:
  • shape (tuple[int, int, int]) – Size in X, Y, and Z direction.

  • itk_dtype (int) – Image data type.

  • num_comp (int, optional, default=1) – Number of components per pixel (max: 4).

  • origin (tuple[float, float, float], optional, default=None) – The X, Y, and Z coordinate of the image origin.

  • voxel_size (tuple[float, float, float], optional, default=None) – Voxel size in X, Y, and Z direction.

Returns:

The generated image object.

Return type:

Image

Raises:

TODO

See also

create_mesh(points, elem2node_dsp, elem2node_con, elem_types, *, elem_tags=None, num_fibers=1, refine_uniform=0)

Create a mesh from a point cloud and an element-to-node graph. The element-to- node graph is defined by the displacemenet array, elem2node_dsp, and the connectivity array, elem2node_dsp. The connectivity array contains the indices of the points that span each element, while the displacement array specifies the start index in the connectivity array. Example: points = [[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0]] e2ndsp = [0, 3, 6] e2ncon = [0, 1, 2, 1, 3, 2] etypes = [pymeshtool.ElementType.Tri, pymeshtool.ElementType.Tri] mesh = pymeshtool.create_mesh(points, e2ndsp, e2ncon, etype)

Parameters:
  • points (ndarray[float]) – Nx3 points array.

  • elem2node_dsp (ndarray[int]) – Element-to-node displacemenet array.

  • elem2node_con (ndarray[int]) – Element-to-node connectivity array.

  • elem_types (ndarray[int]) – Element type array.

  • elem_tags (ndarray[int], optional, default=None) – Element tag array.

  • num_fibers (int, optional, default=1) – Number of fibers per element, either 1 or 2.

  • refine_uniform (int, optional, default=0) – Number of uniform refinement iterations.

Returns:

The generated mesh object.

Return type:

Mesh

Raises:

TODO

get_max_par_threads()

Get the maximum number of parallel OpenMP threads.

Returns:

Maximum number of OpenMP threads.

Return type:

int

get_num_par_threads()

Get the number of parallel OpenMP threads used.

Returns:

Number of OpenMP threads used

Return type:

int

load_fibers(filename)

Read fibers from a CARP fiber file. Can be a text file (*.lon) or a binary binary file (*.blon).

Parameters:

filename (str) – Path to the fibers file.

Returns:

The fibers array.

Return type:

ndarray[float]

Raises:

TODO

load_points(filename)

Read points from a CARP points file. Can be a text file (*.pts) or a binary binary file (*.bpts).

Parameters:

filename (str) – Path to the points file.

Returns:

The points array.

Return type:

ndarray[float]

Raises:

TODO

load_vtx(filename)

Read vertex indices from a CARP vtx file.

Parameters:

filename (str) – Path to the vertex file.

Returns:

The vertex indices and the domain the vertices were defined in.

Return type:

tuple[ndarray[int], str]

Raises:

TODO

save_fibers(fibers, filename)

Save fibers to a CARP fiber file. Can be a text file (*.lon) or a binary binary file (*.blon).

Parameters:
  • fibers (ndarray[float]) – The fibers to save.

  • filename (str) – Path to the fibers file.

Return type:

None

Raises:

TODO

save_points(pnts, filename)

Save points to a CARP points file. Can be a text file (*.pts) or a binary binary file (*.bpts).

Parameters:
  • pnts (ndarray[float]) – The points array.

  • filename (str) – Path to the points file.

Return type:

None

Raises:

TODO

set_num_par_threads(np)

Set the number of parallel OpenMP threads to be used.

Parameters:

np (int) – Number of parallel threads.

Return type:

None

Raises:

TODO

class Mesh

PyMeshool Mesh object. Wrapper class for the meshtool mt_meshdata structure.

Class Attributes

element_tags

Set or get the element tags in the mesh.

Type:

ndarray[int] - The new element tags of same size.

Returns:

Array of the element tags in the mesh.

Return type:

ndarray[int]

Attention

The attribute returns a numpy array that only accesses the existing tag data of the underlying mesh structure without owning the data. This means that the mesh object is not deleted as long as there are numpy arrays pointing to the tag data.

fibers

Set or get the fibers in the mesh.

Type:

ndarray[float] - The new fiber data of same size.

Returns:

Array of the fibers in the mesh.

Return type:

ndarray[float]

Attention

The attribute returns a numpy array that only accesses the existing fiber data of the underlying mesh structure without owning the data. This means that the mesh object is not deleted as long as there are numpy arrays pointing to the fiber data.

num_elements

Get the number of elements in the mesh.

Returns:

The number of elements in the mesh.

Return type:

int

num_fibers

Get the number of fibers in the mesh. Should be a value in {0, 1, 2}.

Returns:

The number of fibers in the mesh.

Return type:

int

num_points

Get the number of points in the mesh.

Returns:

The number of points in the mesh.

Return type:

int

points

Set or get the points in the mesh.

Type:

ndarray[float] - The new point data of same size.

Returns:

Array of the points in the mesh.

Return type:

ndarray[float]

Attention

The attribute returns a numpy array that only accesses the existing point data of the underlying mesh structure without owning the data. This means that the mesh object is not deleted as long as there are numpy arrays pointing to the point data.

Class Functions

__deepcopy__()

Deep copy operator.

Returns:

A deepcopy of the mesh.

Return type:

Mesh

Raises:

AttributeError – If the mesh object was not created correctly.

__init__(basename, *, format='carp_txt', compute_connectivity=True, num_fibers=0)

Create a new mesh by loading from a file.

Parameters:
  • basename (str) – Basename of the file from which the mesh should be loaded from.

  • format (str, optional, default='carp_txt') – Format of the mesh file. Choices are: carp_txt: CARP text format, carp_bin: CARP binary format, vtk: vtk text format, vtk_bin: vtk binary format, vtu: vtu format, mmg: mmg format, neu: ensight format, obj: object format, off: off format, gmsh: gmsh format, stellar: stellar format, purk: purkinje format, vcflow: vcflow format

  • compute_connectivity (bool, optional, default=True) – If True, the full mesh connectivity is computed after the mesh was loaded.

  • num_fibers (int, optional, default=0) – Should be a value in {0, 1, 2}. Change the number of fibers in the the mesh. If num_fibers =0, the fiber number won’t be changed. If num_fibers =1 and the number of fibers in the mesh is 2, then the sheet fibers are removed. If num_fibers =2 and the number of fibers in the mesh is 1, then sheet fibers are added and longitudinal-orthogonal fibers are assigned.

Returns:

The new mesh object loaded from basename.

Return type:

Mesh

Raises:
  • AttributeError – If the mesh object was not initialized properly.

  • IOError – If the mesh format is unknown.

  • ValueError – If the number of fibers is not valid.

  • RuntimeError – If changing the fiber number fails.

See also

apply_split(splitop, *, return_mapping=False)

Apply a split to a given mesh that is defined by the split operation splitop. The format of the split operations is: tagA1,tagA2,..:tagB1,../tagA1,..:tagB1../.. where ‘,’ separates tags, ‘:’ separates tag groups to split, ‘/’ separates split operations. The splitting is applied to the elements defined by the ‘tagA’ tags. These MUST NOT repeat between several split operations!

Parameters:
  • splitop (str) – String defining the split operation.

  • return_mapping (bool, optional, default=False) – If True, the mapping object is returned.

Returns:

  • None - If splitop generates an empty split-list.

  • Mesh - The splitted mesh.

Return type:

None or Mesh

Raises:

ValueError – If the split operation op is empty or the parsing fails.

clean_topology(*, threshold=None)

Clean the mesh from bad topology definitions.

Parameters:

threshold (float, optional, default=None) – Distance threshold when checking co-location of vertices.

Returns:

The cleaned mesh or the mesh itself.

Return type:

Mesh

clear_full_connectivity()

Clear the node-to-element and the node-to-node connectivity graphs.

Return type:

None

Raises:

AttributeError – If the mesh object was not initialized properly.

compute_full_connectivity()

Compute node-to-element and the node-to-node connectivity graphs of the mesh.

Return type:

None

Raises:

AttributeError – If the mesh object was not initialized properly.

connected_vtx_components(selection)

Split vertex selection into connected components.

Parameters:

selection (ndarray[int]) – List of selected vertices.

Returns:

  • None - If no connected components were found.

  • tuple[ndarray[int], …] - Tuple of connected vertex selections.

Return type:

None or tuple[ndarray[int], …]

Raises:

TODO.

extract_gradient(data, *, nodal_input=True, return_nodal=False, normalize=False)

Compute gradient and gradient magnitude of a scalar function on a mesh.

Parameters:
  • data (ndarray[float]) – Scalar input data.

  • nodal_input (bool, optional, default=True) – True if data is defined on the points, False if defined on the elements.

  • return_nodal (bool, optional, default=False) – True if nodal data should be returned, False for element data.

  • normalize (bool, optional, default=False) – If True, the gradient vectors get normalized.

Returns:

Magnitude of the gradient vectors and the gradient vectors.

Return type:

tuple[ndarray[float], ndarray[float]]

Raises:
  • TypeError – If array conversion fails.

  • ValueError – If array is empty.

  • MemoryError – If memory could not be allocated.

extract_mesh(tags, *, compute_connectivity=True, return_mapping=False)

Extract a sub-mesh defined by a list of element tags from a given mesh.

Parameters:
  • tags (int | ndarray[int]) – List of element tags defining the sub-mesh.

  • compute_connectivity (bool, optional, default=True) – If True, the full mesh connectivity is computed after the mesh was extracted.

  • return_mapping (bool, optional, default=False) – If True, the mapping object is returned.

Returns:

  • None - If no region tag was found in the mesh or the entire mesh would be extracted.

  • Mesh - If a sub-mesh was extracted and return_mapping is False.

  • tuple[Mesh, Mapping] - If a sub-mesh was extracted and return_mapping is True.

Return type:

None or Mesh or tuple[Mesh, Mapping]

Raises:
  • TypeError – If the tags parameter can not be converted to a ndarray[int].

  • ValueError – If the tags array is empty.

extract_myocard(*, threshold=0.0, compute_connectivity=True, return_mapping=False)

Extract the myocardial sub-mesh defined by the elements with non-zero fibers.

Parameters:
  • threshold (float, optional, default=0.0) – Fibers with a length greater than the threshold value are considered as myocardium.

  • compute_connectivity (bool, optional, default=True) – If True, the full mesh connectivity is computed after the mesh was extracted.

  • return_mapping (bool, optional, default=False) – If True, the mapping object is returned.

Returns:

  • None - If no fibers are defined or no myocardial region was found in the mesh or the entire mesh defines the myocardial sub-mesh.

  • Mesh - If a myocardial region was extracted and return_mapping is False.

  • tuple[Mesh, Mapping] - If a myocardial region was extracted and return_mapping is True.

Return type:

None or Mesh or tuple[Mesh, Mapping]

Raises:

ValueError – If threshold is negative.

See also

extract_surface(*, setops='', coords=None, edge_threshold=0.0, angle_threshold=0.0, distance=0.0, lower_distance=0.0, hybrid_meshes=True, reindex_nodes=False, return_mapping=False)

Extract a sequence of surfaces defined by set operations on element tags. The format of the operations is: tagA1,tagA2,[surfA1,surfA2..]..[+-:]tagB1,tagB2,[surfB1,surfB2..].. where tag regions separated by ‘,’ will be unified into sub-meshes and their surface is computed. Alternatively, surfaces can be provided directly by *.surf files (only basename, no extension). If two surfaces are separated by ‘-’, the rhs surface will be removed from the lhs surface (set difference). Similarly, using ‘+’ will compute the surface union and if the sub-meshes are separated by ‘:’, the set intersection of the two sub-mesh surfaces will be computed. individual operations are separated by ‘;’.

Parameters:
  • setops (str, optional, default='') – Set operations defining the surfaces.

  • coords (ndarray[float], optional, default=None) – TODO

  • edge_threshold (float, optional, default=0.0) – TODO

  • angle_threshold (float, optional, default=0.0) – TODO

  • distance (float, optional, default=0.0) – TODO

  • lower_distance (float, optional, default=0.0) – TODO

  • hybrid_meshes (bool, optional, default=True) – If False, hybrid surfaces are converted to triangular surfaces.

  • reindex_nodes (bool, optional, default=False) – If True, nodes in the surface meshes are reindexed and their point arrays are restricted.

  • return_mapping (bool, optional, default=False) – If True, the mapping object is returned.

Returns:

  • None - If no surface was extracted.

  • tuple[Mesh, …] - Tuple of the extracted surface meshes.

  • tuple[tuple[Mesh, Mapping], …] - If return_mapping is True, a tuple of pairs with the extracted surface meshes and the corresponding mapping objects is returned.

Return type:

None or tuple[Mesh, …] or tuple[tuple[Mesh, Mapping], …]

Raises:

TypeError – If the parsing of the setops parameter fails.

extract_unreachable(*, mode, compute_connectivity=True, return_mapping=False)

Extract the myocardial sub-mesh defined by the elements with non-zero fibers.

Parameters:
  • mode (int) – Extraction mode. Choices are: <0: extract smallest unreachable sub-mesh,, >0: extract largest unreachable sub-mesh,, =0: extract all unreachable sub-meshes.

  • compute_connectivity (bool, optional, default=True) – If True, the full mesh connectivity is computed after the mesh was extracted.

  • return_mapping (bool, optional, default=False) – If True, the mapping object is returned.

Returns:

  • None - If no unreachable sub-mesh was found.

  • Mesh - If mode is not equal to zero and return_mapping is False.

  • tuple[Mesh, Mapping] - If mode is not equal to zero and return_mapping is True.

  • tuple[Mesh, …] - If mode is equal to zero and return_mapping is False.

  • tuple[tuple[Mesh, Mapping], …] - If mode is equal to zero and return_mapping is True.

Return type:

None or Mesh or tuple[Mesh, Mapping] or tuple[Mesh, …] or tuple[tuple[Mesh, Mapping], …]

Raises:

RuntimeError – If the sub-mesh extraction fails.

extract_vtx(tags)

Extract vertices from a region defined by a list of element tags.

Parameters:

tags (int | ndarray[int]) – List of element tags from which the vertices should be extracted from.

Returns:

  • None - If no vertices were extracted.

  • ndarray[int] - Array of vertex indices.

Return type:

None or ndarray[int]

Raises:
  • AttributeError – If the mesh object was not initialized properly.

  • IOError – If the mesh format is unknown.

extract_vtxhalo(block)

Extract connected halo vertex set(s) for a given block vertex set.

Parameters:

block (ndarray[int]) – Indices defining the vertex block.

Returns:

  • None - If no components were found.

  • tuple[ndarray[int], …] - Tuple of arrays holding the node indices of the connected halo components.

Return type:

None or tuple[ndarray[int], …]

Raises:
  • TypeError – If the block parameter can not be converted to a ndarray[int].

  • ValueError – If the block array is empty.

generate_distancefield(start_vtx, *, end_vtx=None)

Generate a distance field from one vertex list or the relative distance between two vertex lists.

Parameters:
  • start_vtx (ndarray[int]) – Start vertices of the distance field.

  • end_vtx (ndarray[int], optional, default=None) – Rnd vertices of the distance field.

Returns:

The nodal values of the computed distance field.

Return type:

ndarray[float]

Raises:
  • TypeError – If array conversion fails.

  • ValueError – If array is empty.

  • MemoryError – If memory could not be allocated.

generate_fibers(*, bath_tags=None)

Generate default fibers for a given mesh file. The optional element tags identify bath regions.

Parameters:

bath_tags (ndarray[int], optional, default=None) – Bath region tags.

Returns:

The modified input mesh with generated fibers.

Return type:

Mesh

Raises:

TypeError – If array elements are not of type integer or can’t be iterated.

generate_split(splitop)

Generate a split-list for a given mesh defined by the split operation splitop. The format of the split operations is: tagA1,tagA2,..:tagB1,../tagA1,..:tagB1../.. where ‘,’ separates tags, ‘:’ separates tag groups to split, ‘/’ separates split operations. The splitting is applied to the elements defined by the ‘tagA’ tags. These MUST NOT repeat between several split operations!

Parameters:

splitop (str) – String defining the split operation.

Returns:

The split-list defined by splitop. The dimension is (N, 3) where the first column holds the element index, the second column the old node index, and the third column the new node index.

Return type:

ndarray[int]

Raises:

ValueError – If the split operation op is empty or the parsing fails. MemoryError If the allocation of memory fails.

get_element_nodes()

Get a list of all nodes to which an element is attached.

Returns:

  • None - If the list of nodes is empty.

  • ndarray[int] - List of all nodes to which an element is attached.

Return type:

None or ndarray[int]

get_element_sizes()

Get the size of all the elements, for 1D elements the length is returned, for 2D elements the area is returned, and for 3D elements the volume is returned.

Returns:

List of the sizes of the elements.

Return type:

ndarray[float]

get_elements_in_selection(selection)

Get a list of indices of all the elements for which all nodes are in the provided nodal selection.

Parameters:

selection (ndarray[int]) – The nodale selection.

Returns:

  • None - If the list of selected elements is empty.

  • ndarray[int] - List of all elements within the nodal selection.

Return type:

None or ndarray[int]

has_full_connectivity()

Check if the node-to-element and the node-to-node connectivity graphs of the mesh are computed.

Returns:

True if the graphs are computed, False otherwise.

Return type:

bool

Raises:

AttributeError – If the mesh object was not initialized properly.

insert_mesh(sub_mesh, eidx, nidx)

The fiber and tag data of a mesh is inserted into another mesh.

Parameters:
  • sub_mesh (Mesh) – The sub-mesh to be inserted.

  • eidx (ndarray[int]) – The element-map.

  • nidx (ndarray[int]) – The node-map.

Return type:

None

Raises:
  • TypeError – If the array conversion fails.

  • ValueError – If the array is empty.

insert_meshdata(manifold, *, insert_op=0, mult_thr=0.0, trsp_dist=0.0, inp_grad=None, grad_thr=0.0)

Insert fiber and tag data of a mesh into another mesh based on vertex locations.

Parameters:
  • manifold (Mesh) – Mesh to insert into.

  • insert_op (int, optional, default=0) – Operation index. Choices are: 0: for only tags, 1: for only fibers, 2: for both.

  • mult_thr (float, optional, default=0.0) – Multiplication threshold.

  • trsp_dist (float, optional, default=0.0) – Transport distance.

  • inp_grad (ndarray[float], optional, default=None) – Input gradient array.

  • grad_thr (float, optional, default=0.0) – Gradient threshold.

Return type:

None

Raises:
  • TypeError – If the array conversion fails.

  • ValueError – If the array is empty.

interpolate_clouddata(points, data, *, mode=2)

Interpolate data from a point cloud onto a mesh using radial basis function interpolation.

Parameters:
  • points (ndarray[float]) – Coordinates of the point cloud.

  • data (ndarray[float]) – Input data.

  • mode (int, optional, default=2) – Choose between different interpolation modes. Choices are: 0: localized Shepard, 1: global Shepard, 2: RBF interpolation

Returns:

The interpolated data on the mesh.

Return type:

ndarray[float]

Raises:
  • TypeError – If array conversion fails.

  • ValueError – If array is empty.

  • MemoryError – If memory could not be allocated.

See also

  • ClouddataInterpolation

interpolate_elem2node(data, *, normalize=False)

Interpolate data from elements onto nodes.

Parameters:
  • data (ndarray[float]) – Input data array of certain shape. Choices are: (N): for scalar data., (N,3): for vector data., (N,9): and (N,3,3) for tensor/matrix data.

  • normalize (bool, optional, default=False) – If True, vector data is normalized.

Returns:

The interpolated data on the nodes.

Return type:

ndarray[float]

Raises:
  • TypeError – If array conversion fails.

  • ValueError – If array is empty.

  • MemoryError – If memory could not be allocated.

interpolate_node2elem(data, *, normalize=False)

Interpolate data from nodes onto elements.

Parameters:
  • data (ndarray[float]) – Input data array of certain shape. Choices are: (N): for scalar data., (N,3): for vector data., (N,9): and (N,3,3) for tensor/matrix data.

  • normalize (bool, optional, default=False) – If True, vector data is normalized.

Returns:

The interpolated data on the elements.

Return type:

ndarray[float]

Raises:
  • TypeError – If array conversion fails.

  • ValueError – If array is empty.

  • MemoryError – If memory could not be allocated.

interpolate_nodes(data, omesh, *, norm=False)

Interpolate nodal data from one mesh onto another.

Parameters:
  • data (ndarray[float]) – Input data array of certain shape. Choices are: (N): for scalar data., (N,3): for vector data., (N,9): and (N,3,3) for tensor/matrix data.

  • omesh (Mesh) – Mesh we interpolate to.

  • norm (bool, optional, default=False) – If True, vector data is normalized.

Returns:

The interpolated data on the target mesh.

Return type:

ndarray[float]

Raises:
  • TypeError – If array conversion fails.

  • ValueError – If array is empty.

  • MemoryError – If memory could not be allocated.

is_surface_mesh()

Check if mesh is surface mesh.

Returns:

True if mesh is a surface mesh, False otherwise.

Return type:

bool

merge(mesh, *, ignore_empty_interface=True)

Merge mesh with other mesh.

Parameters:
  • mesh (Mesh) – The mesh that gets merged into.

  • ignore_empty_interface (bool, optional, default=True) – Whether to raise an error if the interface between meshes is empty.

Returns:

The united mesh.

Return type:

Mesh

query_bbox()

Get the box bounding the mesh.

Returns:

Tuple holding the XYZ-coordinates of the point defining the lower-left-back corner of the box, the XYZ-coordinates of the point defining the upper-right-front corner of the box, the sizes of the bounding box in XYZ-direction and the length of the diagonal.

Return type:

tuple[tuple[float, float, float], tuple[float, float, float], tuple[float, float, float], float]

query_curvature(radius)

Calculate the curvature of a given surface mesh.

Parameters:

radius (float) – Radius parameter influencing the curvature calculation.

Returns:

The computed curvature values.

Return type:

ndarray[float]

Raises:
  • TypeError – If a conversion fails.

  • MemoryError – If memory could not be allocated.

query_edges(*, tags=None)

Get statistical parameters related to the edges in the given mesh or or sub- mesh.

Parameters:

tags (int | ndarray[int], optional, default=None) – Restrict query to elements of a certain tag region.

Returns:

  • None - If no edges are in the restriced region.

  • tuple[float, float, float] - The minimal, maximal and average edge length.

Return type:

None or tuple[float, float, float]

Raises:

TypeError – If the tags parameter can not be converted to a ndarray[int]. ValueError If the tags array is empty.

query_idx(coord, *, threshold=0.0, vertices=None)

Get indices in proximity to a given coordinate.

Parameters:
  • coord (ndarray[float]) – XYZ-coordinates of the point for which the nearest vertex is to be found.

  • threshold (float, optional, default=0.0) – Sets the proximity threshold for the coordinates.

  • vertices (ndarray[int], optional, default=None) – Node indices for additional filtering.

Returns:

List of indices in proximity to the given coordinates.

Return type:

ndarray[int]

Raises:
  • TypeError – If array conversion fails.

  • ValueError – If coords sequency is not of size 3 or an array is empty.

  • MemoryError – If memory could not be allocated.

query_idxlist(coords, *, threshold=0.0, vertices=None)

Get indices in proximity for all provided coordinates.

Parameters:
  • coords (ndarray[float]) – List of the XYZ-coordinates of the points for which the nearest vertex is to be found.

  • threshold (ndarray[float], optional, default=0.0) – Proximity threshold for each coordinate.

  • vertices (ndarray[float], optional, default=None) – Restricts query to indices of specific vertices.

Returns:

  • None - If the queried index list is empty.

  • tuple[ndarray[int], …] - Tuple of indices in proximity to the given coordinates.

Return type:

None or tuple[ndarray[int], …]

Raises:
  • TypeError – If array conversion fails.

  • ValueError – If array is empty.

  • MemoryError – If memory could not be allocated.

query_quality()

Get the quality of the elements.

Returns:

Array holding the quality for each element in the mesh.

Return type:

ndarray[float]

Raises:

MemoryError – If memory could not be allocated.

query_tags(*, vtx=None, tolerance=0.01)

Get tags information of a given mesh.

Parameters:
  • vtx (None | ndarray[int], optional, default=None) – Restrict query to elements connected to the nodes in the list.

  • tolerance (float, optional, default=0.01) – Fibers with a lenght greater than this value are considered as myocardium.

Returns:

Tuple, where the first entry is a dictionary mapping myocardial tags to the corresponding number of elements, the second entry is a dictionary mapping bath tags to the corresponding number of elements, and the third entry is number of elements considered.

Return type:

tuple[dict[int, int], dict[int, int], int]

Raises:
  • TypeError – If the vtx parameter can not be converted to a ndarray[int].

  • ValueError – If tolerance is negative.

refine_uniformly(num_it)

Uniformly refine mesh.

Parameters:

num_it (int) – Number of refinement iterations.

Returns:

The refined mesh

Return type:

Mesh

Raises:
  • TypeError – If the tags are not of type integer.

  • ValueError – If the object could not be iterated.

rotate(axis, angle, *, ignore_fibers=False)

Rotate the mesh around a given vector. If ignore_fibers is False, the fibers are not rotated.

Parameters:
  • axis (ndarray[float]) – Axis vector the mesh is rotated around.

  • angle (float) – Rotation angle.

  • ignore_fibers (bool, optional, default=False) – If True, fibers won’t be rotated.

Return type:

None

Raises:
  • AttributeError – If the mesh object was not initialized properly.

  • TypeError – If the array cannot be converted.

  • ValueError – If the array is empty.

save(basename, *, format='carp_txt')

Write the mesh to a file.

Parameters:
  • basename (str) – Basename of the file to which the mesh should be written to.

  • format (str, optional, default='carp_txt') – Format of the mesh file. Choices are: carp_txt: CARP text format, carp_bin: CARP binary format, vtk: vtk text format, vtk_bin: vtk binary format, vtu: vtu format, vtk_polydata: vtk polydata format, mmg: mmg format, neu: ensight format, obj: object format, off: off format, stellar: stellar format, vcflow: vcflow format

Return type:

None

Raises:
  • AttributeError – If the mesh object was not initialized properly.

  • IOError – If the mesh format is unknown.

See also

scale(scaling_factor)

Scale the mesh.

Parameters:

scaling_factor (float | tuple[float, float, float]) – The factor by which the mesh is scaled. Either a single scaling factor or a list of factors for the X, Y, and Z coordinate.

Return type:

None

Raises:

AttributeError – If the mesh object was not initialized properly.

smooth_mesh(tags, *, num_iterations=100, smoothing_coeff=0.15, num_laplace_levels=1, edge_threshold=0, max_quality_threshold=0.95)

Smooth surfaces and volume of a mesh.

Parameters:
  • tags (Iterable[int | Iterable[int]]) – List of tag sets. The tags in one set have a common surface. Surfaces between different tag sets will be smoothed.

  • num_iterations (int, optional, default=100) – Number of smoothing iter.

  • smoothing_coeff (float, optional, default=0.15) – Smoothing coefficient.

  • num_laplace_levels (int, optional, default=1) – Number of laplace levels used.

  • edge_threshold (float, optional, default=0) – Normal vector angle difference defining a sharp edge. If set to 0, edge detection is turned off. Negative values let meshtool skip edge vertices.

  • max_quality_threshold (float, optional, default=0.95) – Maximum allowed element quality metric. Set to 0 to disable quality checking.

Return type:

None

Raises:
  • TypeError – If the tags are not of type integer.

  • ValueError – If the object could not be iterated.

translate(displacement)

Translates the mesh with a given displacement vector.

Parameters:

displacement (ndarray[float]) – Displacement vector in X, Y, and Z directions.

Return type:

None

Raises:
  • AttributeError – If the mesh object was not initialized properly.

  • TypeError – If the array cannot be converted.

  • ValueError – If the array is empty.

class Mapping

PyMeshool Mapping object. Helper class to ease mapping of data between mesh and submesh.

Class Attributes

element_map

Get the element mesh-to-submesh index map.

Returns:

  • None - If the element map is empty.

  • ndarray[int] - The element mesh-to-submesh index map.

Return type:

None or ndarray[int]

Attention

The attribute returns a numpy array that only accesses the existing mapping data of the underlying mapping structure without owning the data. This means that the mapping object is not deleted as long as there are numpy arrays pointing to the mapping data.

mesh

Get the mesh related to the mapping object.

Returns:

The mesh related to the mapping object.

Return type:

Mesh

node_map

Get the node mesh-to-submesh index map.

Returns:

  • None - If the node map is empty.

  • ndarray[int] - The node mesh-to-submesh index map.

Return type:

None or ndarray[int]

Attention

The attribute returns a numpy array that only accesses the existing mapping data of the underlying mapping structure without owning the data. This means that the mapping object is not deleted as long as there are numpy arrays pointing to the mapping data.

submesh

Get the submesh related to the mapping object.

Returns:

The submesh related to the mapping object.

Return type:

Mesh

Class Functions

__matmul__(other)

Create a new mapping object by concatenating two consecutive mappings.

Parameters:

other (Mapping) – Subsequent mapping object.

Returns:

The concatenated mapping object.

Return type:

Mapping

Raises:
  • TypeError – If the either of two objects is not a Mapping object.

  • ValueError – If the two mapping objects are not subsequent.

insert_back()

Insert fiber and tag data from the submesh back to the mesh.

Return type:

None

Raises:

ValueError – If mesh, submesh, or mapping data are invalid.

insert_data(subdata, data, *, nodal_data=True)

Insert data defined on the submesh into a data array given on the mesh.

Parameters:
  • subdata (ndarray[number]) – The data defined on the submesh.

  • data (ndarray[number]) – The data defined on the mesh.

  • nodal_data (bool, optional, default=True) – Set True to insert nodal data, False for element data.

Returns:

The data array.

Return type:

ndarray[number]

Raises:
  • ValueError – If data has incorrect size or dimensions.

  • TypeError – If invalid mesh, submesh, mapping data, type or convertion error.

  • MemoryError – If unable to allocate memory.

map_selection(idx, *, nodal_selection=True, forward=True)

Map nodal or element selections between the mesh and the submesh.

Parameters:
  • idx (ndarray[int]) – Input selection array.

  • nodal_selection (bool, optional, default=True) – Set True to map a nodal selection, False for an element selection.

  • forward (bool, optional, default=True) – Set True to map from mesh to submesh, False to map from submesh to mesh.

Returns:

  • None - If the list of the mapped selection is empty.

  • ndarray[int] - Mapped selection array.

Return type:

None or ndarray[int]

Raises:
  • ValueError – If data has incorrect size or dimensions.

  • TypeError – If invalid mesh, submesh, mapping data, type or convertion error.

  • MemoryError – If unable to allocate memory.

prolongate(data, *, default=None, nodal_data=True)

Prolongate nodal or element data from the submesh to the mesh. If the entity is not in the mesh, the data is filled with the default value or with zero if default is None.

Parameters:
  • data (ndarray[number]) – Input data array.

  • default (number, optional, default=None) – Default value to use where mapping is undefined.

  • nodal_data (bool, optional, default=True) – Set True to map nodal data, False for element data.

Returns:

Mapped output data array.

Return type:

ndarray[number]

Raises:
  • ValueError – If data has incorrect size or dimensions.

  • TypeError – If invalid mesh, submesh, mapping data, type or convertion error.

  • MemoryError – If unable to allocate memory.

restrict(data, *, nodal_data=True)

Restrict nodal or element data from the mesh to the submesh.

Parameters:
  • data (ndarray[number]) – Input data array.

  • nodal_data (bool, optional, default=True) – Set True to map nodal data, False for element data.

Returns:

Mapped output data array.

Return type:

ndarray[number]

Raises:
  • ValueError – If data has incorrect size or dimensions.

  • TypeError – If invalid mesh, submesh, mapping data, type or convertion error.

  • MemoryError – If unable to allocate memory.

save(basename, *, binary=True)

Saves the mapping data (elem and node) to disk.

Parameters:
  • basename (str) – Basename to the output files.

  • binary (bool, optional, default=True) – Set True to write binary data, False for text data.

Return type:

None

Raises:
  • ValueError – If mapping data is empty.

  • TypeError – If mesh, submesh, or mapping data is invalid or empty.

class Image

PyMeshool Image object. Wrapper class for the meshtool itk_image structure.

Class Attributes

data_type

Get the data type of the image.

Returns:

The integer value representation of the image data type.

Return type:

int

dtype

Get the numpy data type of the image.

Returns:

The numpy type representation of the image data type.

Return type:

type

origin

Set or get the origin of the image.

Type:

tuple[float, float, float] - The X, Y, and Z position of the new origin.

Returns:

The X, Y, and Z position of the origin.

Return type:

tuple[float, float, float]

shape

Get the shape of the image.

Returns:

The dimension in X, Y, and Z directions and the number of components.

Return type:

tuple[int, int, int, int]

voxel_size

Set or get the voxel-size of the image.

Type:

tuple[float, float, float] - The new size in X, Y, and Z direction of an image voxel.

Returns:

The X, Y, and Z size of an image voxel.

Return type:

tuple[float, float, float]

voxels

Set or get the voxels of the image.

Type:

ndarray[number] - The new voxel data of same shape and data type.

Returns:

The voxel data.

Return type:

ndarray[number]

Attention

The attribute returns a numpy array that only accesses the existing voxel data of the underlying image structure without owning the data. This means that the image object is not deleted as long as there are numpy arrays pointing to the voxel data.

Class Functions

__deepcopy__()

Deep copy operator.

Returns:

A deep copy of the image.

Return type:

Image

Raises:

AttributeError – If the image object was not created correctly.

__init__(filename)

Create a new image by loading from a file.

Parameters:

filename (str) – Filename of the image file from which the image should be loaded from.

Returns:

The new image object loaded from filename.

Return type:

Image

Raises:
  • AttributeError – If the image object was not initialized properly.

  • IOError – If the image format is unknown.

change_type(new_type)

Change image data type.

Parameters:

new_type (int) – Index of new data type. Choices are: 1: unsigned_char, 2: char, 3: unsigned_short, 4: short, 5: unsigned_int, 6: int, 7: unsigned_long, 8: long, 9: float, 10: double, 11: color scalars

Returns:

The image with the new data type.

Return type:

Image

See also

crop()

Crop image to bounding-box.

Returns:

The cropped image.

Return type:

Image

extract_mesh(*, surface_mesh=False, tetrahedralize_mesh=False, scale=1.0, compute_connectivity=True, tags=None)

Extract mesh from the image.

Parameters:
  • surface_mesh (bool, optional, default=False) – If True, only a surface mesh is extracted.

  • tetrahedralize_mesh (bool, optional, default=False) – If True, the volumetric hex elements are converted to tets.

  • scale (float, optional, default=1.0) – Mesh scaling factor (> 0.0).

  • compute_connectivity (bool, optional, default=True) – If True, the full mesh connectivity is computed after the mesh was extracted.

  • tags (ndarray[int], optional, default=None) – List of tags of the regions to mesh. If None, the entire image is extracted.

Returns:

The extracted mesh.

Return type:

Mesh

extrude(mode, radius, region_tag, *, new_tag=-1, tags=None)

Extrude image data.

Parameters:
  • mode (int) – Extrusion mode. Choices are: <0: extrude inwards,, >0: extrude outwards,, =0: extrude in both directions.

  • radius (int) – Extrusion radius (>0).

  • region_tag (int) – Tag of the region to extrude.

  • new_tag (int, optional, default=-1) – Tag of the new extruded region, if negative, region_tag is taken.

  • tags (ndarray[int], optional, default=None) – List of tags, the region can grow into.

Returns:

The extruded image.

Return type:

Image

resample(refinemenet_factor)

Resample image data.

Parameters:

refinemenet_factor (float | tuple[float, float, float]) – Voxel refinemenet factor.

Returns:

The resampled image.

Return type:

Image

save(filename)

Write image to file.

Parameters:

filename (str) – Path to the output file. the format is specified by the file extension. Choices are: vtk: vtk structured points format, nrrd: nearly raw raster data format, raw: raw binary format (voxels only)

Return type:

None

Raises:

IOError – If the file extension is unknown.

class MeshInputFormat

Enumeration of all supported mesh input formats.

Enum Items

  • carp_bin: ‘carp_bin

  • carp_txt: ‘carp_txt

  • gmsh: ‘gmsh

  • mmg: ‘mmg

  • netgen: ‘neu

  • obj: ‘obj

  • off: ‘off

  • purk: ‘purk

  • stellar: ‘stellar

  • vcflow: ‘vcflow

  • vtk: ‘vtk

  • vtk_bin: ‘vtk_bin

  • vtu: ‘vtu

class MeshOutputFormat

Enumeration of all supported mesh output formats.

Enum Items

  • carp_bin: ‘carp_bin

  • carp_txt: ‘carp_txt

  • mmg: ‘mmg

  • netgen: ‘neu

  • obj: ‘obj

  • off: ‘off

  • stellar: ‘stellar

  • vcflow: ‘vcflow

  • vtk: ‘vtk

  • vtk_bin: ‘vtk_bin

  • vtk_polydata: ‘vtk_polydata

  • vtu: ‘vtu

class MeshElementType

Enumeration of all mesh element types.

Enum Items

  • Tetra: 0

  • Hexa: 1

  • Octa: 2

  • Pyramid: 3

  • Prism: 4

  • Quad: 5

  • Tri: 6

  • Line: 7

  • Node: 8

class MeshClouddataInterpolationMode

Enumeration of available clouddata interpolation modes.

Enum Items

  • localized_shepard: 0

  • global_shepard: 1

  • radial_basis_function: 2

class MeshExtractUnreachableMode

Enumeration of mesh unreachable extraction modes.

Enum Items

  • smallest: -1

  • all: 0

  • largest: 1

class ImageDType

Enumeration of image voxel data types.

Enum Items

  • UInt8: 1

  • Int8: 2

  • UInt16: 3

  • Int16: 4

  • UInt32: 5

  • Int32: 6

  • UInt64: 7

  • Int64: 8

  • Float32: 9

  • Float64: 10

  • ColorScalar: 11

class ImageExtrusionMode

Enumeration of image extrusion modes.

Enum Items

  • inwards: -1

  • everywhere: 0

  • outwards: 1