mesh_tools module¶
This file contains a selection of useful vector operations and mesh operations such as io, cleaning, converting etc.
This file is imported by carto_mesh.
- mesh_tools.calcAvNormal(facets: pandas.core.frame.DataFrame) List[source]¶
Calculates the average normal of a dataframe of triangles. Dataframe must contain the components of the normals of each triangle in columns named ‘NormalX’, ‘NormalY’, ‘NormalZ’ as is the case for Carto data
- Args:
facets: A Pandas Dataframe of triangles. Must contain the following columns: ‘NormalX’, ‘NormalY’, ‘NormalZ’, ‘GroupID’
- Returns:
List: An array containing the components of the normalized average normal of the given triangles.
- mesh_tools.cleanMesh(pvmesh: pyvista.core.pointset.PolyData, tol: float, iterations: int = 10, print_si: bool = True) pyvista.core.pointset.PolyData[source]¶
Uses built-in PyVista and PyMesh methods to clean the mesh.
- Args:
pvmesh: The mesh in PyVista’s PolyData format
tol: Absolute tolerance to use in PyMesh’s remove_duplicated_vertices() and PyVista’s clean() methods
iterations: Amount of iterations to try and remove self-intersections with PyMesh
print_si: Print progress of self-intersection cleanup
- Returns:
PolyData: The cleaned mesh
- mesh_tools.cleanMesh_Meshtool(meshname: str, threshold: float = 0.3, ifmt: str = 'carp_txt', ofmt: str = 'carp_txt') None[source]¶
Calls upon meshtool in a shell command to clean a mesh.
- Args:
meshname: Name of the mesh to be cleaned threshold: Quality threshold. Cleaning will continue until this quality is reached, or the max amount of iterations has been reached
ifmt: The format of the input mesh. May be; carp_txt, carp_bin, vtk, vtk_bin, mmg, neu, purk, obj, off, gmsh, stellar, vcflow
ofmt: The format of the output mesh. May be: carp_txt, carp_bin, vtk, vtk_bin, vtk_polydata, mmg, neu, obj, off, stellar, vcflow, ensight_txt
- Returns:
None: Nothing. Writes out the cleaned mesh in the desired format.
- mesh_tools.colorFromCsv(meshdir: str = '') pyvista.core.pointset.PolyData[source]¶
Makes mesh from VerticesSection.csv and TrianglesSection.csv Assigns tags in .mesh file as scalar data on this mesh Returns mesh with this scalar data.
- Args:
meshdir: Directory containing VerticesSection.csv and TrianglesSection.csv
- Returns:
The mesh with scalar data applied in PyVista’s PolyData format
- mesh_tools.convertMesh_Meshtool(meshname: str, ifmt: str = 'vtk', ofmt: str = 'carp_txt') None[source]¶
Calls upon meshtool in a shell command to convert a mesh.
- Args:
meshname: Name of the mesh, excluding file extension.
ifmt: File format of the input mesh. Can be: carp_txt, carp_bin, vtk, vtk_bin, mmg, neu, purk, obj, off, gmsh, stellar, vcflow
ofmt: File format of the output mesh. Can be: carp_txt, carp_bin, vtk, vtk_bin, vtk_polydata, mmg, neu, obj, off, stellar, vcflow, ensight_txt
- Returns:
None: Nothing. Writes out the mesh in the desired format.
- mesh_tools.dist(co1: Union[numpy.ndarray, List, Tuple], co2: Union[numpy.ndarray, List, Tuple]) float[source]¶
Calculates the distance between two coordinates.
- Args:
co1: Array containing the components of the first coordinate
co2: Array containing the components of the second coordinate
- Returns:
float: Distance between the two coordinates
- mesh_tools.getEdgeLengths(mesh: pymesh.Mesh.Mesh) List[source]¶
Gets all edge lengths from a PyMesh mesh (used in
homogenizeMesh())- Args:
mesh: The input mesh in PyMesh’s Mesh format
- Returns:
List: A list containing all the lengths of the edges of the input mesh.
- mesh_tools.getGroupIds(csvfile: str = 'TrianglesSection.csv', col_name: str = 'GroupID', skip: Tuple = (0, - 1000000)) List[source]¶
Extract the tags from TrianglesSection.csv as generated by
__cartoToCsv()- Args:
csvfile: Name of the .csv file to read. Default = ‘TrianglesSection’. File must be .csv and contain a column <col_name>.
skip: Array of tags to ignore. These won’t be extracted. Default = [0, -1000000]
- Returns:
List: a list containing the unique tags in the input file
- mesh_tools.makeNonCollinear(pvmesh: pyvista.core.pointset.PolyData, edges: Union[numpy.ndarray, List, Tuple]) pyvista.core.pointset.PolyData[source]¶
Iterates mesh and replades points in the middle of two colinear edges. Edges must be 2D array
- Args:
pvmesh: The input mesh containing colinearities in PyVista’s PolyData format
edges: A list of point index pairs. Each index pair defines a colinear edge.
- Returns:
PolyData: The mesh with its colinear edges lifted.
- mesh_tools.makePyMesh(pvmesh: pyvista.core.pointset.PolyData) pymesh.Mesh.Mesh[source]¶
Converts PyVista mesh to PyMesh mesh
- Args:
pvmesh: The mesh in PyVista’s PolyData format
- Returns:
Mesh: The mesh as a PyMesh Mesh object
- mesh_tools.makePyVista(pmmesh: pymesh.Mesh.Mesh) pyvista.core.pointset.PolyData[source]¶
Converts PyMesh mesh to PyVista mesh (PolyData)
- Args:
pmmesh: the mesh in PyMesh.Mesh format
- Returns:
PolyData: The mesh in PyVista’s PolyData format
- mesh_tools.normalize(vector: Union[numpy.ndarray, List, Tuple]) List[source]¶
Normalizes a vector such that its length equals 1.
- Args:
vector: A 1xN array containing the vector components.
- Returns:
List: the normalized vector
- mesh_tools.pmToPvFaces(pmfaces: Union[numpy.ndarray, List, Tuple]) numpy.ndarray[source]¶
Given an array of pymesh triangles, where these triangles are arrays of the form [ind1, ind2, ind3], converts these faces to the PyVista representation of the form [3, ind1_0, ind2_0, ind3_0, 3, ind1_1, ind2_1, ind3_1, 3 …]
- Args:
pmfaces: A Nx3 array, where N = n_faces.
- Returns:
ndarray: A numpy ndarray containing the triangles in PyVista’s format
- mesh_tools.ptsToParaview(filename: str, column_name: str = 'meshID') None[source]¶
Takes a pts file, adds point ID as extra data to each point, writes out to csv file. This function is useful to convert meshes and read them in in ParaView. ParaView does not remember original mesh indexing during mesh operations.
- Args:
filename: Name of .pts file (including ‘.pts’ extension) to be converted to paraview-friendly format
column_name: Name of the column to contain the mesh indices. Default = “meshID”
- Returns:
None: Nothing. Writes out the mesh as a .csv file containing the columns ‘X’, ‘Y’, ‘Z’ and <column_name>
- mesh_tools.pvToPmCells(pyvistafaces: Union[numpy.ndarray, List, Tuple]) numpy.ndarray[source]¶
Given an array of pyvista faces, converts these faces to the Pymesh representation.
- Args:
pyvistafaces: An array containing the faces in PyVista format
- Returns:
ndarray: A numpy ndarray containing the triangles in PyMesh format
- mesh_tools.pvToPmFaces(pyvistafaces: Union[numpy.ndarray, List, Tuple]) numpy.ndarray[source]¶
Given an array of pyvista triangles, converts these faces to the PyMesh representation.
- Args:
pyvistafaces: An array containing the faces in PyVista format.
- Returns:
ndarray: A numpy ndarray containing the triangles in PyMesh format
- mesh_tools.writeToSmesh(mesh: pyvista.core.pointset.PolyData, name: str) None[source]¶
Writes out a .smesh file with a hole in the middle. This is used in
reconstruct()to be used as input for TetGen.- Args:
mesh: The surface mesh, ready to be tetrahedralized, in PyVista’s PolyData format.
name: Name of the input mesh, and corresponding output .smesh file
- Returns:
None: Nothing. Writes out a .smesh file.
