Display#

Classes to define mesh for display use. Display mesh do not require good aspect ratios on elements.

class volmdlr.display.Mesh2D(vertices: ndarray[Any, dtype[float]], triangles: ndarray[Any, dtype[int]], name: str = '')[source]#

Bases: MeshMixin, DessiaObject

2D triangle mesh.

area() float[source]#

Calculate the total area of the 2D mesh as the sum of areas of triangles.

Returns:

The total area of the mesh.

Return type:

float

class volmdlr.display.Mesh3D(vertices: ndarray[Any, dtype[float]], triangles: ndarray[Any, dtype[int]], color: Tuple[float, float, float] | None = None, alpha: float = 1.0, name: str = '')[source]#

Bases: MeshMixin, Primitive3D

3D triangle mesh.

area() float[source]#

Calculate the total surface area of the 3D mesh as the sum of areas of triangles.

Returns:

The total surface area of the 3D mesh.

Return type:

float

property bounding_box#

Bounding box of current mesh.

compute_len_edges()[source]#

Compute the lengths of edges for each triangle in the mesh.

Returns:

Lengths of edges (3 edges per triangles) of dimensions n_simplices x 3 and edges of dimensions n_simplices x 3 x 2

Return type:

tuple[np.ndarray, np.ndarray]

decimate(target_count: int, update_rate: int = 5, aggressiveness: float = 7.0, max_iterations: int = 100, verbose: bool = False, lossless: bool = False, threshold_lossless: float = 0.001, alpha: float = 1e-09, k: int = 3, preserve_border: bool = True) Mesh3D[source]#

Decimate the Mesh3D, and return it as a new instance.

Vertices of the mesh should be merged (and maybe rounded) for efficient decimation.

Note: threshold = alpha * pow(iteration + k, aggressiveness)

Parameters:
  • target_count (int) – Target number of triangles. Not used if lossless is True.

  • update_rate (int) – Number of iterations between each update. If lossless flag is set to True, rate is 1.

  • aggressiveness (float) – Parameter controlling the growth rate of the threshold at each iteration when lossless is False.

  • max_iterations (int) – Maximal number of iterations.

  • verbose (bool) – Control verbosity.

  • lossless (bool) – Use the lossless simplification method.

  • threshold_lossless (float) – Maximal error after which a vertex is not deleted. Only for lossless method.

  • alpha (float) – Parameter for controlling the threshold growth.

  • k (int) – Parameter for controlling the threshold growth.

  • preserve_border (bool) – Flag for preserving vertices on open border.

Returns:

The decimated mesh.

Return type:

Mesh3D

property faces#

Get the mesh faces as Triangle3D objects.

Returns:

The triangles comosing the mesh.

Return type:

list[Triangle3D]

classmethod from_3mf_file(filepath: str, scale_factor: float = 0.001, merge_meshes: bool = True) Mesh3D | List[Mesh3D][source]#

Create a 3D mesh from an 3MF file.

Parameters:
  • filepath (str) – The path to the 3MF file.

  • scale_factor (float, optional) – The scale factor to apply to the mesh (default is 0.001).

  • merge_meshes (bool) – A flag to choose to merge all the 3mf meshes in one, or return a list of meshes.

Returns:

A new 3D mesh instance.

Return type:

Mesh3D

classmethod from_3mf_stream(stream: BinaryFile, scale_factor: float = 0.001, merge_meshes: bool = True) Mesh3D | List[Mesh3D][source]#

Create a 3D mesh from an 3MF stream.

Parameters:
  • stream (BinaryFile) – A binary stream containing 3MF data.

  • scale_factor (float, optional) – The scale factor to apply to the mesh (default is 0.001).

  • merge_meshes (bool) – A flag to choose to merge all the 3mf meshes in one, or return a list of meshes.

Returns:

A new 3D mesh instance.

Return type:

Mesh3D

classmethod from_obj_file(filepath: str, scale_factor: float = 0.001) Mesh3D[source]#

Create a 3D mesh from an OBJ file.

Parameters:
  • filepath (str) – The path to the OBJ file.

  • scale_factor (float, optional) – The scale factor to apply to the mesh (default is 0.001).

Returns:

A new 3D mesh instance.

Return type:

Mesh3D

classmethod from_obj_stream(stream: BinaryFile, scale_factor: float = 0.001) Mesh3D[source]#

Create a 3D mesh from an OBJ stream.

Parameters:
  • stream (BinaryFile) – A binary stream containing OBJ data.

  • scale_factor (float, optional) – The scale factor to apply to the mesh (default is 0.001).

Returns:

A new 3D mesh instance.

Return type:

Mesh3D

classmethod from_off_file(filepath: str, scale_factor: float = 0.001) Mesh3D[source]#

Create a 3D mesh from an OFF file.

Parameters:
  • filepath (str) – The path to the OFF file.

  • scale_factor (float, optional) – The scale factor to apply to the mesh (default is 0.001).

Returns:

A new 3D mesh instance.

Return type:

Mesh3D

classmethod from_off_stream(stream: BinaryFile, scale_factor: float = 0.001) Mesh3D[source]#

Create a 3D mesh from an OFF stream.

Parameters:
  • stream (BinaryFile) – A binary stream containing OFF data.

  • scale_factor (float, optional) – The scale factor to apply to the mesh (default is 0.001).

Returns:

A new 3D mesh instance.

Return type:

Mesh3D

classmethod from_ply_file(filepath: str, scale_factor: float = 0.001) Mesh3D[source]#

Create a 3D mesh from an PLY file.

Parameters:
  • filepath (str) – The path to the PLY file.

  • scale_factor (float, optional) – The scale factor to apply to the mesh (default is 0.001).

Returns:

A new 3D mesh instance.

Return type:

Mesh3D

classmethod from_ply_stream(stream: BinaryFile, scale_factor: float = 0.001) Mesh3D[source]#

Create a 3D mesh from an PLY stream.

Parameters:
  • stream (BinaryFile) – A binary stream containing PLY data.

  • scale_factor (float, optional) – The scale factor to apply to the mesh (default is 0.001).

Returns:

A new 3D mesh instance.

Return type:

Mesh3D

classmethod from_stl_file(filepath: str, scale_factor: float = 0.001) Mesh3D[source]#

Create a 3D mesh from an STL file.

Parameters:
  • filepath (str) – The path to the STL file.

  • scale_factor (float, optional) – The scale factor to apply to the mesh (default is 0.001).

Returns:

A new 3D mesh instance.

Return type:

Mesh3D

classmethod from_stl_stream(stream: BinaryFile, scale_factor: float = 0.001) Mesh3D[source]#

Create a 3D mesh from an STL stream.

Parameters:
  • stream (BinaryFile) – A binary stream containing STL data.

  • scale_factor (float, optional) – The scale factor to apply to the mesh (default is 0.001).

Returns:

A new 3D mesh instance.

Return type:

Mesh3D

classmethod from_trimesh(trimesh_: Trimesh) Mesh3D[source]#

Create a 3D mesh from a Trimesh object.

Parameters:

trimesh (Trimesh) – A Trimesh object representing the 3D mesh.

Returns:

A new 3D mesh instance.

Return type:

Mesh3D

classmethod from_trimesh_scene(trimesh_scene: Scene, scale_factor: float = 0.001) Mesh3D[source]#

Create a 3D mesh from a Trimesh Scene.

Parameters:
  • trimesh_scene (trimesh.Scene) – A Trimesh Scene containing multiple geometry objects.

  • scale_factor (float, optional) – The scale factor to apply to the mesh (default is 0.001).

Returns:

A new 3D mesh instance.

Return type:

Mesh3D

get_edges_triangles()[source]#

Compute lengths edges of triangles.

Returns:

A 3D numpy array representing edges of triangles. The dimensions are n_triangles x 3 x 2, where each entry contains the start and end points of an edge.

Return type:

np.ndarray

get_mesh_border()[source]#

Retrieve the topological border of a triangle mesh.

This function identifies and returns the edges that belong to only one triangle, effectively representing the border of the mesh.

Returns:

A tuple of two numpy arrays. The first array contains the unique border edges, and the second array includes all edges of the mesh.

Return type:

tuple[np.ndarray, np.ndarray]

minimum_distance(other_mesh: Mesh3D, return_points: bool = False)[source]#

Compute the minimum distance between this 3D mesh and another 3D mesh.

This is an approximation: only vertices are taken in account for minimum distance computation.

Parameters:
  • other_mesh (Mesh3D) – The other 3D mesh to compare against.

  • return_points (bool, optional) – Whether to return the closest points.

Returns:

The minimum distance between the two meshes, and optionally, the closest points.

Return type:

float or (float, ndarray[float], ndarray[float])

remove_large_triangles(threshold_edge_length: float) Mesh3D[source]#

Remove triangles from the mesh whose edge lengths exceed the specified threshold.

Parameters:

threshold_edge_length (float) – The maximum allowed edge length for a triangle to remain in the mesh.

Returns:

A new Mesh3D instance with large triangles removed.

Return type:

Mesh3D

save_to_3mf_file(filepath: str, scale_factor: float = 1000.0)[source]#

Save the 3D mesh to a 3MF file.

Parameters:
  • filepath (str) – The path to the 3MF file.

  • scale_factor (float, optional) – The scale factor to apply to the mesh (default is 1000.0).

save_to_3mf_stream(stream, scale_factor: float = 1000.0)[source]#

Save the 3D mesh to a 3MF stream.

Parameters:
  • stream (BinaryFile) – A binary stream to write the 3MF data.

  • scale_factor (float, optional) – The scale factor to apply to the mesh (default is 1000.0).

save_to_obj_file(filepath: str, scale_factor: float = 1000.0)[source]#

Save the 3D mesh to an OBJ file.

Parameters:
  • filepath (str) – The path to the OBJ file.

  • scale_factor (float, optional) – The scale factor to apply to the mesh (default is 1000.0).

save_to_obj_stream(stream, scale_factor: float = 1000.0)[source]#

Save the 3D mesh to an OBJ stream.

Parameters:
  • stream (BinaryFile) – A binary stream to write the OBJ data.

  • scale_factor (float, optional) – The scale factor to apply to the mesh (default is 1000.0).

save_to_off_file(filepath: str, scale_factor: float = 1000.0)[source]#

Save the 3D mesh to an OFF file.

Parameters:
  • filepath (str) – The path to the OFF file.

  • scale_factor (float, optional) – The scale factor to apply to the mesh (default is 1000.0).

save_to_off_stream(stream, scale_factor: float = 1000.0)[source]#

Save the 3D mesh to an OFF stream.

Parameters:
  • stream (BinaryFile) – A binary stream to write the OFF data.

  • scale_factor (float, optional) – The scale factor to apply to the mesh (default is 1000.0).

save_to_ply_file(filepath: str, scale_factor: float = 1000.0)[source]#

Save the 3D mesh to a PLY file.

Parameters:
  • filepath (str) – The path to the PLY file.

  • scale_factor (float, optional) – The scale factor to apply to the mesh (default is 1000.0).

save_to_ply_stream(stream, scale_factor: float = 1000.0)[source]#

Save the 3D mesh to a PLY stream.

Parameters:
  • stream (BinaryFile) – A binary stream to write the PLY data.

  • scale_factor (float, optional) – The scale factor to apply to the mesh (default is 1000.0).

save_to_stl_file(filepath: str, scale_factor: float = 1000.0)[source]#

Save the 3D mesh to an STL file.

Parameters:
  • filepath (str) – The path to the STL file.

  • scale_factor (float, optional) – The scale factor to apply to the mesh (default is 1000.0).

save_to_stl_stream(stream, scale_factor: float = 1000.0)[source]#

Save the 3D mesh to an STL stream.

Parameters:
  • stream (BinaryFile) – A binary stream to write the STL data.

  • scale_factor (float, optional) – The scale factor to apply to the mesh (default is 1000.0).

to_babylon()[source]#

Convert the mesh to the Babylon.js format.

This method rounds the vertices to 6 decimal places and returns the mesh in a Babylon.js compatible format. https://doc.babylonjs.com/how_to/custom

Returns:

A dictionary representing the mesh in Babylon.js format with ‘positions’ and ‘indices’ keys.

Return type:

dict

to_closed_shell()[source]#

Convert the Mesh3D object to a closed triangle shell.

Returns:

A closed triangle shell representation of the Mesh3D object.

Return type:

ClosedTriangleShell3D

to_open_shell()[source]#

Convert the Mesh3D object to an open triangle shell.

Returns:

An open triangle shell representation of the Mesh3D object.

Return type:

OpenTriangleShell3D

to_triangles3d()[source]#

Export the mesh faces as Triangle3D objects.

Returns:

The triangles comosing the mesh.

Return type:

list[Triangle3D]

to_trimesh()[source]#

Convert the Mesh3D instance to a Trimesh object.

Returns:

A Trimesh object representing the 3D mesh.

Return type:

Trimesh

triangular_faces()[source]#

Export the mesh faces as Triangle3D objects.

Returns:

The triangles comosing the mesh.

Return type:

list[Triangle3D]

triangulation() Mesh3D[source]#

Return self as triangulation to enable VolumeModel usage.

classmethod trimesh_scene_to_meshes(trimesh_scene: Scene, scale_factor: float = 0.001) List[Mesh3D][source]#

Create a 3D mesh from a Trimesh Scene.

Parameters:
  • trimesh_scene (trimesh.Scene) – A Trimesh Scene containing multiple geometry objects.

  • scale_factor (float, optional) – The scale factor to apply to the mesh (default is 0.001).

Returns:

A list of new 3D mesh instance.

Return type:

list[Mesh3D]

class volmdlr.display.MeshMixin[source]#

Bases: object

Mixin class for 2D and 3D meshes.

This is an abstract class.

MeshType = ~MeshType#
check_consistency() bool[source]#

Check the consistency of the Mesh instance.

This method verifies that all vertices referenced by triangles are within the valid range of vertex indices.

Returns:

True if the mesh is consistent, False otherwise.

Return type:

bool

classmethod dict_to_object(dict_: Dict[str, Any], *args, **kwargs) MeshType[source]#

Overload of ‘dict_to_object’ for numpy usage and memory perf.

property dimension: int#

Get the dimension of the mesh (“2” for 2D mesh or “3” for 3D mesh).

classmethod from_meshes(meshes: List[MeshType], merge_vertices: bool = False, merge_triangles: bool = False) MeshType[source]#

Merge two meshes.

Parameters:
  • meshes (MeshType) – A list of Mesh instance to merge all together.

  • merge_vertices (bool, optional) – Flag to indicate whether to merge vertices.

  • merge_triangles (bool, optional) – Flag to indicate whether to merge triangles.

Returns:

A new Mesh instance representing the merged meshes.

Return type:

MeshType

merge(other: MeshType, merge_vertices: bool = False, merge_triangles: bool = False) MeshType[source]#

Merge two meshes.

Parameters:
  • other (MeshType) – Another Mesh instance to merge with this instance.

  • merge_vertices (bool, optional) – Flag to indicate whether to merge vertices.

  • merge_triangles (bool, optional) – Flag to indicate whether to merge triangles.

Returns:

A new Mesh instance representing the merged meshes.

Return type:

MeshType

merge_triangles() MeshType[source]#

Merge duplicated triangles in the Mesh instance.

This method identifies and removes duplicate triangles, resulting in a Mesh with unique triangles.

Returns:

A new Mesh instance with merged triangles.

Return type:

MeshType

merge_vertices() MeshType[source]#

Merge duplicated vertices in the Mesh instance and remap triangles accordingly.

This method identifies duplicate vertices and combines them into a single unique set of vertices, updating the triangles to use the unique vertices.

Returns:

A new Mesh instance with merged vertices and updated triangles.

Return type:

MeshType

property n_triangles: int#

Get number of triangles in the mesh.

property n_vertices: int#

Get number of vertices in the mesh.

plot(ax=None, numbering: bool = False)[source]#

Plot the mesh with Matplotlib.

remove_degenerate_triangles(tol: float = 0.0) MeshType[source]#

Remove degenerate triangles from the Mesh instance.

Degenerate triangles are triangles with vertices that are too close to each other. This method checks for degenerate triangles based on a tolerance value and removes them.

Parameters:

tol (float, optional) – The tolerance value to determine whether a triangle is degenerate or not (default is 0.0).

Returns:

A new Mesh instance with degenerate triangles removed.

Return type:

MeshType

resize(scale_factor: float) MeshType[source]#

Resize the Mesh instance by scaling its vertices.

Parameters:

scale_factor (float) – The factor by which to scale the mesh.

Returns:

A new Mesh instance representing the scaled mesh.

Return type:

MeshType

round_vertices(decimals: int = 9) MeshType[source]#

Round the vertices of the Mesh instance to a given number of decimals.

Parameters:

decimals (int, optional) – The number of decimal places to round the vertices to (default is 9).

Returns:

A new Mesh instance with rounded vertices.

Return type:

MeshType

split_shared_vertices() MeshType[source]#

Split the shared vertices between triangles in the Mesh instance.

This method recreates distinct vertices for each triangle, effectively unmerging shared vertices. The resulting mesh will have three times the number of vertices as the number of triangles.

Returns:

A new Mesh instance with unmerged vertices and original triangles.

Return type:

MeshType

to_dict(*args, **kwargs)[source]#

Overload of ‘to_dict’ for numpy usage and memory perf.

triangles_cross_products()[source]#

Compute the cross products of edges for each triangle in the mesh.

Returns:

An array containing the cross products of edges for each triangle.

Return type:

np.ndarray[float]

triangles_vertices()[source]#

Get the actual triangles of the mesh represented by their vertices (not indices).

Returns:

An array containing the vertices of the triangles.

Return type:

np.ndarray[float]