gmsh_mesh_builder module

class gmsh_mesh_builder.Mesher(geo_file_path: str, mesh_file_path: str, slicing_coefficient: int, n_longitudinal: int, n_transverse_cort: int, n_transverse_trab: int, n_radial: int, ellipsoid_fitting: bool = False)[source]

Bases: object

add_bspline_filling(bsplines: ndarray, intersections: ndarray, slicing_coefficient: int) ndarray[source]

Add B-spline fillings to the Gmsh model.

This function adds B-spline fillings between the given B-splines and intersections in the Gmsh model and returns an array of B-spline filling tags.

Parameters:
  • bsplines (ndarray) – The array of B-spline tags.

  • intersections (ndarray) – The array of intersection tags.

  • slicing_coefficient (int) – The slicing coefficient.

Returns:

The array of B-spline filling tags.

Return type:

ndarray

add_interslice_segments(point_tags_ext: ndarray, point_tags_int: ndarray) ndarray[source]

Add interslice segments to the Gmsh model.

This function adds interslice segments connecting the external and internal point tags in the Gmsh model and returns an array of segment tags.

Parameters:
  • point_tags_ext (ndarray) – The array of external point tags.

  • point_tags_int (ndarray) – The array of internal point tags.

Returns:

The array of interslice segment tags.

Return type:

ndarray

add_intersurface_planes(intersurface_line_tags: ndarray, intersection_line_tag_ext: ndarray, intersection_line_tag_int: ndarray) List[int][source]

Add intersurface planes to the Gmsh model.

This function adds intersurface planes between the given line tags and intersection line tags in the Gmsh model and returns a list of surface tags.

Parameters:
  • intersurface_line_tags (ndarray) – The array of intersurface line tags.

  • intersection_line_tag_ext (ndarray) – The array of external intersection line tags.

  • intersection_line_tag_int (ndarray) – The array of internal intersection line tags.

Returns:

The list of intersurface plane tags.

Return type:

List[int]

add_slice_surfaces(ext_tags: ndarray, int_tags: ndarray, interslice_seg_tags: ndarray) ndarray[source]

Add slice surfaces to the Gmsh model.

This function adds slice surfaces between the external, internal, and interslice segment tags in the Gmsh model and returns an array of surface tags.

Parameters:
  • ext_tags (ndarray) – The array of external tags.

  • int_tags (ndarray) – The array of internal tags.

  • interslice_seg_tags (ndarray) – The array of interslice segment tags.

Returns:

The array of slice surface tags.

Return type:

ndarray

add_volume(cortical_ext_surfs: ndarray, cortical_int_surfs: ndarray, slices_tags: ndarray, intersurface_surface_tags: List[int]) List[int][source]

Add volumes to the mesh based on the provided surface tags.

This function creates volumes by defining surface loops from the provided cortical and intersurface surface tags.

Parameters:
  • cortical_ext_surfs (ndarray) – Tags of external cortical surfaces.

  • cortical_int_surfs (ndarray) – Tags of internal cortical surfaces.

  • slices_tags (ndarray) – Tags of slice surfaces.

  • intersurface_surface_tags (List[int]) – Tags of intersurface surfaces.

Returns:

Tags of the created volumes.

Return type:

List[int]

analyse_mesh_quality(hiding_thresh: float) None[source]

Analyze the quality of the generated mesh.

This function runs the “AnalyseMeshQuality” plugin to evaluate the quality of the generated mesh based on the specified hiding threshold.

Parameters:

hiding_thresh (float) – The threshold for hiding elements based on quality.

Returns:

None

create_centre_splines(i: int, point_tags_c: ndarray, CENTER_ARC: float, signs: List[Tuple[int, int]]) List[int][source]

Create center splines in the Gmsh model.

This function creates center splines based on the given point tags, center arc, and signs, and returns a list of spline tags.

Parameters:
  • i (int) – The index of the current center of interest.

  • point_tags_c (ndarray) – The array of point tags for the center.

  • CENTER_ARC (float) – The center arc value.

  • signs (List[Tuple[int, int]]) – The list of sign tuples for the splines.

Returns:

The list of spline tags.

Return type:

List[int]

get_barycenters(tag_s: list[int]) ndarray[source]

Get barycenters of each volumetric element.

This function retrieves the barycenters of each volumetric element of type 5 (8-node hexahedron) or type 12 (27-node second order hexahedron) for the specified tags.

Parameters:

tag_s (list[int]) – List of tags for the volumetric elements.

Returns:

Array of barycenters in (x, y, z) format with shape (n_elms, 3).

Return type:

ndarray

get_centroids_dict(centroids: ndarray) Dict[int, ndarray][source]

Returns a dictionary of centroids with keys as indices and values as centroids.

Parameters:

centroids (np.array) – array of centroids in (x, y, z) format (shape: (n_elms, 3))

Returns:

dictionary of centroids with keys as indices and values as centroids

Return type:

centroids_dict (dict)

get_elm_volume(tag_s: list[int]) ndarray[source]

Returns an array of containing the volume of each element for the given tags.

Parameters:

tag_s (list[int]) – list of element tags to get volumes for

Returns:

array of element volumes (shape: (n_elms, 1))

Return type:

volumes (np.ndarray)

get_radius_longest_edge(tag_s: ndarray) float64[source]

Compute the radius of the longest edge for each element in the given tag_s.

Parameters:

tag_s – list([int]) List of element tags to compute the radius for.

Returns:

(float) The radius of the longest edge among all elements in tag_s.

Return type:

_

get_sort_coords(point_tags: List[ndarray]) ndarray[source]

Get and sort coordinates of points.

This function retrieves the coordinates of the given point tags, slices them into sub-arrays, and sorts each sub-array in a counterclockwise order.

Parameters:

point_tags (List[ndarray]) – The list of point tags.

Returns:

The sorted array of point tags.

Return type:

ndarray

get_trabecular_cortical_volume_mesh(trab_slice_surf_tags: List[int], trab_plane_inertia_tags: List[int], cortical_int_surfs: ndarray, trab_surfs_v: List[int]) List[int][source]

Generate the trabecular-cortical volume mesh.

This function creates the trabecular-cortical volume mesh by defining surface loops and volumes based on the provided surface and plane tags.

Parameters:
  • trab_slice_surf_tags (List[int]) – Tags of trabecular slice surfaces.

  • trab_plane_inertia_tags (List[int]) – Tags of trabecular plane inertia surfaces.

  • cortical_int_surfs (ndarray) – Tags of internal cortical surfaces.

  • trab_surfs_v (List[int]) – Tags of vertical trabecular surfaces.

Returns:

Tags of the created trabecular-cortical volumes.

Return type:

List[int]

gmsh_add_points(x: float32 | float64, y: float32 | float64, z: float32 | float64) int[source]

Add a point to the Gmsh model. https://gitlab.onelab.info/gmsh/gmsh/-/issues/456 https://bbanerjee.github.io/ParSim/fem/meshing/gmsh/quadrlateral-meshing-with-gmsh/

This function adds a point with the given coordinates to the Gmsh model and returns the point tag.

Parameters:
  • x (Union[float32, float64]) – The x-coordinate of the point.

  • y (Union[float32, float64]) – The y-coordinate of the point.

  • z (Union[float32, float64]) – The z-coordinate of the point.

Returns:

The tag of the added point.

Return type:

int

gmsh_geometry_formulation(array: ndarray, idx_list: ndarray) Tuple[ndarray, ndarray, ndarray, ndarray][source]

Formulate the geometry in the Gmsh model.

This function formulates the geometry in the Gmsh model by inserting points, intersection lines, B-splines, and B-spline fillings, and returns the tags of the indexed points, B-splines, intersection lines, and B-spline fillings.

Parameters:
  • array (ndarray) – The array of points.

  • idx_list (ndarray) – The list of indices for the intersection points.

Returns:

The tags of the indexed points, B-splines, intersection lines, and B-spline fillings.

Return type:

Tuple[ndarray, ndarray, ndarray, ndarray]

gmsh_get_bnds(nodes: dict) Tuple[Dict[uint64, ndarray], Dict[uint64, ndarray]][source]

Get the boundary nodes of the mesh.

This function identifies the boundary nodes of the mesh based on their z-coordinate and returns them as two dictionaries for the bottom and top boundaries.

Parameters:

nodes (dict) – The node set of the mesh.

Returns:

Dictionaries of bottom and top boundary nodes.

Return type:

Tuple[Dict[uint64, ndarray], Dict[uint64, ndarray]]

gmsh_get_elms() Dict[uint64, ndarray][source]

Get the elements of the mesh.

This function retrieves the elements of the mesh for each volume and returns them as a dictionary.

Returns:

A dictionary with element tags as keys and node tags as values.

Return type:

Dict[uint64, ndarray]

gmsh_get_nodes() Dict[uint64, ndarray][source]

Get the nodes of the mesh.

This function retrieves the nodes of the mesh for each physical group and returns them as a dictionary.

Returns:

A dictionary with node tags as keys and coordinates as values.

Return type:

Dict[uint64, ndarray]

gmsh_get_reference_point_coord(nodes: dict) ndarray[source]

Get the coordinates of the reference point.

This function calculates the coordinates of the reference point based on the center of mass of the nodes and the maximum z-coordinate.

Parameters:

nodes (dict) – The node set of the mesh.

Returns:

The coordinates of the reference point.

Return type:

ndarray

gmsh_insert_bspline(points: ndarray) int[source]

Insert a B-spline into the Gmsh model.

This function inserts a B-spline with the given points into the Gmsh model and returns the B-spline tag.

Parameters:

points (ndarray) – The points of the B-spline.

Returns:

The tag of the added B-spline.

Return type:

int

insert_bspline(array: ndarray, array_pts_tags: ndarray, indexed_points_coi: ndarray) ndarray[source]

Insert plane bsplines based on the slices’ points cloud Returns the list of the bspline tags and creates bspline in gmsh

Parameters:
  • array (np.ndarray) – array containing geometry point cloud

  • array_pts_tags (np.ndarray) – array containing the tags of the points

  • indexed_points_coi (np.ndarray) – indices at which the bspline is inserted (start-end points of the bspline)

Returns:

point tags of the bspline

Return type:

array_bspline (np.ndarray)

insert_closest_point(arr, closest_idx, values)[source]

Insert intersection point between two lines in the array :param arr: array of points :type arr: ndarray :param closest_idx: index of the closest point :type closest_idx: ndarray

Returns:

new array with intersection points

Return type:

ndarray

insert_ellipse_arcs(array: ndarray, center_tag: int)[source]

Insert ellipse arcs into the Gmsh model.

This function inserts ellipse arcs connecting consecutive points in the given array into the Gmsh model and returns a list of ellipse arc tags.

Parameters:
  • array (ndarray) – The array of points to be connected by ellipse arcs.

  • center_tag (int) – The tag of the center point for the ellipse arcs.

Returns:

The list of ellipse arc tags.

Return type:

List[int]

insert_intersection_line(point_tags: ndarray, idx_list: list) Tuple[ndarray, ndarray][source]

Insert intersection lines into the Gmsh model.

This function inserts lines connecting the intersection points in the given point tags array and returns the line tags and the sorted intersection points.

Parameters:
  • point_tags (ndarray) – The array of point tags.

  • idx_list (list) – The list of indices for the intersection points.

Returns:

The array of line tags and the sorted intersection points.

Return type:

Tuple[ndarray, ndarray]

insert_lines(array: ndarray) List[int][source]

Insert lines into the Gmsh model.

This function inserts lines connecting consecutive points in the given array into the Gmsh model and returns a list of line tags.

Parameters:

array (ndarray) – The array of points to be connected by lines.

Returns:

The list of line tags.

Return type:

List[int]

insert_points(array: ndarray) ndarray[source]

Insert points into the Gmsh model.

This function inserts the points from the given array into the Gmsh model and returns an array of point tags.

Parameters:

array (ndarray) – The array of points to be inserted.

Returns:

The array of point tags.

Return type:

ndarray

insert_tensor_of_inertia(array: ndarray, centroid: ndarray) tuple[source]

Insert the intersection points of the centroid with the contours.

This function calculates the intersection points of the centroid with the contours, finds the nearest neighbour of the intersection points, and inserts the intersection points into the contours.

Parameters:
  • array (ndarray) – The array of contour points.

  • centroid (ndarray) – The centroid of the cortex.

Returns:

The updated array with intersection points, the list of indices of the intersection points, and the intersection points.

Return type:

tuple

intersection_point(arr: ndarray, intersection: ndarray) Tuple[ndarray, int64][source]

Insert intersection point between two lines in the array :param arr: array of points :type arr: ndarray :param intersection_1: intersection point 1 :type intersection_1: ndarray :param intersection_2: intersection point 2 :type intersection_2: ndarray

Returns:

new array with intersection points

Return type:

ndarray

mesh_generate(dim: int, element_order: int, vol_tags: list)[source]

Generate the mesh for the specified dimension and element order.

This function sets various meshing options and generates the mesh for the specified dimension and element order.

Parameters:
  • dim (int) – The dimension of the mesh (1, 2, or 3).

  • element_order (int) – The order of the elements in the mesh.

  • vol_tags (list) – Tags of the volumes to be meshed.

Returns:

None

meshing_transfinite(longitudinal_line_tags: List[int] | ndarray, transverse_line_tags: List[int], radial_line_tags: List[int] | ndarray, surface_tags: List[int] | ndarray, volume_tags: List[int] | ndarray, phase: str = 'cort')[source]

Apply transfinite meshing to the specified geometric entities.

This function sets transfinite meshing properties for curves, surfaces, and volumes based on the provided tags and the specified phase.

Parameters:
  • longitudinal_line_tags (Union[List[int], ndarray]) – Tags of longitudinal lines.

  • transverse_line_tags (List[int]) – Tags of transverse lines.

  • radial_line_tags (Union[List[int], ndarray]) – Tags of radial lines.

  • surface_tags (Union[List[int], ndarray]) – Tags of surfaces.

  • volume_tags (Union[List[int], ndarray]) – Tags of volumes.

  • phase (str, optional) – The phase of the meshing process (“cort” or “trab”). Default is “cort”.

Returns:

None

nodes2coords(nodes, elements)[source]

Associate node coordinates to each element. https://github.com/dmelgarm/gmsh/blob/master/gmsh_tools.py#L254

This function associates the coordinates of the nodes to each element based on the provided node and element arrays.

Parameters:
  • nodes (ndarray) – The array of node coordinates.

  • elements (ndarray) – The array of element definitions.

Returns:

The array of element coordinates.

Return type:

ndarray

partition_lines(radius: int, centroid: ndarray) Tuple[ndarray, ndarray][source]
polygon_tensor_of_inertia(ext_arr: ndarray, int_arr: ndarray, true_coi: bool = True) tuple[source]

This function calculates the centroid of a polygon, which can be either the true centroid of the cortex or the centroid of the internal contour, based on the ‘true_coi’ parameter.

If ‘true_coi’ is True, the function calculates the true centroid of the cortex by subtracting the internal contours from the external contours. If the subtraction fails, the function logs an error, saves a plot of the polygons, and exits the program.

If ‘true_coi’ is False, the function calculates the centroid of the internal contour.

Parameters:
  • ext_arr (numpy.ndarray) – A 2D array representing the external contour of the polygon.

  • int_arr (numpy.ndarray) – A 2D array representing the internal contour of the polygon.

  • true_coi (bool, optional) – A flag indicating whether to calculate the true centroid of the cortex. Defaults to True.

Returns:

The coordinates of the calculated centroid.

Return type:

cortex_centroid (tuple)

query_closest_idx(list_1: list, list_2: list)[source]

This function takes two lists of indices, retrieves the corresponding coordinates from the model, and sorts the second list in a counter-clockwise order based on the coordinates.

Parameters:
  • list_1 (list) – A list of indices. Each index corresponds to a coordinate in the model. This list represents the ‘center of inertia’ coordinates.

  • list_2 (list) – A list of indices. Each index corresponds to a coordinate in the model. This list represents the ‘trabecular’ coordinates.

Returns:

The list of ‘trabecular’ indices sorted in a counter-clockwise order

based on the corresponding coordinates.

Return type:

list_2_sorted (list)

shapely_line_polygon_intersection(poly: ndarray, line_1: ndarray) List[Tuple[float, float, float]][source]

Find the intersection point between two lines :param line1: line 1 :type line1: shapely.geometry.LineString :param line2: line 2 :type line2: shapely.geometry.LineString

Returns:

intersection point

Return type:

shapely.geometry.Point

shift_point(arr, intersection)[source]

Shift a point in the array to the intersection point.

This function finds the closest point in the array to the given intersection point and replaces it with the intersection point.

Parameters:
  • arr (ndarray) – The array of points.

  • intersection (ndarray) – The intersection point to be inserted.

Returns:

The updated array with the intersection point inserted.

Return type:

ndarray

sort_bspline_cw(coords, point_tags)[source]

Sort B-spline points in clockwise order.

This function sorts the given B-spline points in a clockwise order based on their angles from the centroid and returns the sorted point tags.

Parameters:
  • coords (ndarray) – The coordinates of the B-spline points.

  • point_tags (ndarray) – The tags of the B-spline points.

Returns:

The sorted point tags.

Return type:

ndarray

sort_by_indexes(lst, indexes, reverse=False)[source]

Sort a list based on the provided indexes.

This function sorts the elements of a list according to the specified indexes.

Parameters:
  • lst (List) – The list to be sorted.

  • indexes (List[int]) – The indexes to sort the list by.

  • reverse (bool, optional) – Whether to sort in reverse order. Default is False.

Returns:

The sorted list.

Return type:

List

sort_ccw(coords: ndarray) ndarray[source]

Sort coordinates in counterclockwise order.

This function sorts the given coordinates in counterclockwise order based on their angles relative to the centroid.

Parameters:

coords (ndarray) – The coordinates to be sorted.

Returns:

The sorted indices of the coordinates.

Return type:

ndarray

sort_intersection_points(array: ndarray) ndarray[source]

Sort the intersection points in counterclockwise direction.

This function sorts the intersection points in the given array in a counterclockwise direction and returns the sorted array.

Parameters:

array (ndarray) – The array of intersection points to be sorted.

Returns:

The sorted array of intersection points.

Return type:

ndarray

split_dict_by_array_len(input_dict: Dict[int, ndarray], len1: int) Tuple[Dict[int, ndarray], Dict[int, ndarray]][source]

Split a dictionary into two based on the length of the arrays.

This function splits the input dictionary into two dictionaries based on the specified length of the arrays.

Parameters:
  • input_dict (Dict[int, ndarray]) – The input dictionary to be split.

  • len1 (int) – The length of the arrays for the first dictionary.

Returns:

The two resulting dictionaries.

Return type:

Tuple[Dict[int, ndarray], Dict[int, ndarray]]

trabecular_cortical_connection(coi_idx: ndarray, trab_point_tags: ndarray) List[int][source]

This function creates a connection (line) between each pair of points from two given lists in the model. The connections are created between each ‘center of interest’ point and the corresponding ‘trabecular’ point.

Parameters:
  • coi_idx (list[int]) – A list of indices. Each index corresponds to a ‘center of inertia’ point in the model.

  • trab_point_tags (list[int]) – A list of indices. Each index corresponds to a ‘trabecular’ point in the model. The order of indices matches with the ‘center of inerta’ points in coi_idx.

Returns:

A list of line tags. Each line tag represents a connection (line) created in the model.

Return type:

trab_cort_line_tags (list)

trabecular_planes_inertia(trab_cort_line_tags: List[int], trab_line_tags_v: List[int], intersection_line_tags_int: ndarray) List[int][source]

Create trabecular planes of inertia.

This function generates trabecular planes of inertia by defining curve loops and filling surfaces based on the provided line tags.

Parameters:
  • trab_cort_line_tags (List[int]) – Tags of cortical trabecular lines.

  • trab_line_tags_v (List[int]) – Tags of vertical trabecular lines.

  • intersection_line_tags_int (ndarray) – Tags of intersection lines.

Returns:

Tags of the created trabecular planes of inertia.

Return type:

List[int]

trabecular_slices(trab_cort_line_tags: List[int], trab_line_tags_h: List[int], cort_int_bspline_tags: ndarray) List[int][source]

Create trabecular slices.

This function generates trabecular slices by defining curve loops and plane surfaces based on the provided line and B-spline tags.

Parameters:
  • trab_cort_line_tags (List[int]) – Tags of cortical trabecular lines.

  • trab_line_tags_h (List[int]) – Tags of horizontal trabecular lines.

  • cort_int_bspline_tags (ndarray) – Tags of internal cortical B-splines.

Returns:

Tags of the created trabecular slices.

Return type:

List[int]

class gmsh_mesh_builder.TrabecularVolume(geo_file_path: str, mesh_file_path: str, slicing_coefficient: int, n_longitudinal: int, n_transverse_cort: int, n_transverse_trab: int, n_radial: int, QUAD_REFINEMENT: bool, ellipsoid_fitting: bool)[source]

Bases: Mesher

get_offset_points(array: ndarray, _center: List[float64], l_i: float64, l_j: float64) ndarray[source]

Calculate offset points from the center.

This function calculates the offset points from the center based on the principal axes lengths and the length factor.

Parameters:
  • array (ndarray) – Array of points.

  • _center (List[float64]) – Center point.

  • l_i (float64) – Length of the first principal axis.

  • l_j (float64) – Length of the second principal axis.

Returns:

Array with the offset points.

Return type:

ndarray

get_trabecular_position() ndarray[source]

Get the trabecular positions.

This function calculates the trabecular positions by splitting the input array into subarrays, calculating the principal axes lengths, and determining the offset points.

Returns:

Array of trabecular positions.

Return type:

ndarray

get_trabecular_vol(coi_idx: ndarray) Tuple[ndarray, List[int], List[int], List[int], List[int], List[int]][source]

Get the trabecular volume.

This function calculates the trabecular volume by determining the trabecular positions, sorting the coordinates, creating lines and surfaces, and finally generating the volume.

Parameters:

coi_idx (ndarray) – Array of center of interest indices.

Returns:

  • Array of point tags.

  • List of vertical line tags.

  • List of horizontal line tags.

  • List of vertical surface tags.

  • List of horizontal surface tags.

  • List of volume tags.

Return type:

Tuple[ndarray, List[int], List[int], List[int], List[int], List[int]]

get_vertices_coords(vertices_tags)[source]

Get the coordinates of vertices.

This function retrieves the coordinates of the specified vertices.

Parameters:

vertices_tags (List[int]) – List of vertex tags.

Returns:

Array of vertex coordinates.

Return type:

ndarray

principal_axes_length(array: ndarray) Tuple[float64, float64][source]

Calculate the lengths of the axes of inertia.

This function calculates the lengths of the principal axes for the given array of points.

Parameters:

array (ndarray) – Array of points.

Returns:

Lengths of the principal axes.

Return type:

Tuple[float64, float64]

set_length_factor(length_factor: float)[source]

Set the length factor.

This function sets the length factor used to calculate the size of the central trabecular region.

Parameters:

length_factor (float) – The length factor to be set.

Returns:

None

sort_trab_coords(point_tags: List[int]) List[ndarray][source]

Sort trabecular coordinates in counterclockwise order.

This function sorts the trabecular coordinates in counterclockwise order based on the provided point tags.

Parameters:

point_tags (List[int]) – List of point tags.

Returns:

List of sorted point tags.

Return type:

List[ndarray]