meshmagick.mesh module

This module concerns mesh data structures.

TODO: mettre des examples d’utilisation

class meshmagick.mesh.Mesh(vertices, faces, name=None)[source]

Bases: object

A class to handle unstructured meshes.

Parameters
  • vertices (array_like) – (nv x 3) Array of mesh vertices coordinates. Each line of the array represents one vertex coordinates

  • faces (array_like) – Arrays of mesh connectivities for faces. Each line of the array represents indices of vertices that form the face, expressed in counterclockwise order to ensure outward normals description.

  • name (str, optional) – The mesh’s name. If None, mesh is given an automatic name based on its internal ID.

__add__(mesh_to_add)[source]

Adds two meshes

Parameters

mesh_to_add (Mesh) – The other mesh instance to add to the current instance

Returns

The composite mesh

Return type

Mesh

Note

This method should not be called as is but it overides the + binary operator for convenience.

__str__()[source]

String representation of the mesh

Returns

Return type

str

property axis_aligned_bbox

Get the axis aligned bounding box of the mesh.

Returns

(xmin, xmax, ymin, ymax, zmin, zmax)

Return type

tuple

property boundaries

Get the list of boundaries of the mesh.

Returns

list that stores lists of boundary connected vertices

Return type

list

Note

The computation of boundaries should be in the future computed with help of VTK

copy()[source]

Get a copy of the current mesh instance.

Returns

mesh instance copy

Return type

Mesh

eval_plain_mesh_inertias(rho_medium=1023.0)[source]

Evaluates the mesh inertia under the assumption of an enclosed volume made of an homogeneous medium of the given density.

Parameters

rho_medium (float, optional) – The medium density (kg/m**3). Default is 1023 kg.m**3 (salt water)

Returns

The mesh inertia instance expressed at origin (0, 0, 0)

Return type

RigidBodyInertia

eval_shell_mesh_inertias(rho_medium=7850.0, thickness=0.02)[source]

Evaluates the mesh inertia under the assumption of an enclosed volume made of an homogeneous medium of the given density.

Parameters
  • rho_medium (float, optional) – The medium density (kg/m**3). Default is 7850 kg/m**3 (Steel density)

  • thickness (flaot, optional) – The hull thickness (m). Default is 0.02 m.

Returns

The mesh inertia instance expressed at origin (0, 0, 0)

Return type

RigidBodyInertia

extract_faces(id_faces_to_extract, return_index=False)[source]

Extracts a new mesh from a selection of faces ids

Parameters
  • id_faces_to_extract (ndarray) – Indices of faces that have to be extracted

  • return_index (bool) – Flag to output old indices

Returns

A new Mesh instance composed of the extracted faces

Return type

Mesh

property faces

Get the faces connectivity array of the mesh

Returns

Return type

ndarray

property faces_areas

Get the array of faces areas of the mesh

Returns

Return type

ndarray

property faces_centers

Get the array of faces centers of the mesh

Returns

Return type

ndarray

property faces_normals

Get the array of faces normals of the mesh

Returns

Return type

ndarray

property ff

Get the face / faces connectivity dictionary

Returns

Return type

dict

flip_normals()[source]

Flips every normals of the mesh.

get_face(face_id)[source]

Get the face described by its vertices connectivity

Parameters

face_id (int) – Face id

Returns

If the face is a triangle, the array has 3 components, else it has 4 (quadrangle)

Return type

ndarray

get_surface_integrals()[source]

Get the mesh surface integrals

Returns

The mesh surface integrals array

Return type

ndarray

has_surface_integrals()[source]
heal_mesh()[source]

Heals the mesh for different tests available.

It applies:

  • Unused vertices removal

  • Degenerate faces removal

  • Duplicate vertices merging

  • Triangles healing

  • Normal healing

heal_normals()[source]

Heals the mesh’s normals orientations so that they have a consistent orientation and try to make them outward.

heal_triangles()[source]

Makes the triangle connectivity consistent.

A general face is stored internally as a 4 integer array. It allows to describe indices of a quadrangle’s vertices. For triangles, the first index should be equal to the last. This method ensures that this rule is applied everywhere and correct bad triangles description.

property id

Get the id of the mesh

Returns

hash id of the instance

Return type

int

is_mesh_closed()[source]

Returns if the mesh is a closed manifold.

Returns

True if the mesh is closed (i.e. it has no boundaries)

Return type

bool

is_mesh_conformal()[source]

Returns if the mesh is conformal.

Returns

True if the mesh is conformal.

Return type

bool

Warning

This method is experimental. Use at your own risk !

is_triangle(face_id)[source]

Returns if a face is a triangle

Parameters

face_id (int) – Face id

Returns

True if the face with id face_id is a triangle

Return type

bool

property max_edge_length

The mesh’s maximum edge length

property mean_edge_length

The mesh’s mean edge length

merge_duplicates(atol=1e-08, return_index=False)[source]

Merges the duplicate vertices of the mesh.

Parameters
  • atol (float, optional) – Absolute tolerance. default is 1e-8

  • return_index (bool, optional) – Flag to return

Returns

new_id – Array of indices that merges the vertices. Returned if return_index = True

Return type

ndarray, optional

property min_edge_length

The mesh’s minimum edge length

mirror(plane)[source]

Mirrors the mesh instance with respect to a plane.

Parameters

plane (Plane) – The mirroring plane

property name

Get the name of the mesh

property nb_boundaries

Get the number of boundaries in the mesh

Returns

Number of boundaries

Return type

list

property nb_faces

Get the number of faces in the mesh

Returns

Return type

int

property nb_quadrangles

Get the number of quadrangles in the mesh

Returns

Return type

int

property nb_triangles

Get the number of triangles in the mesh

Returns

Return type

int

property nb_vertices

Get the number of vertices in the mesh

Returns

Return type

int

print_quality()[source]

Returns data on the mesh quality

It uses VTK and is reproduced from http://vtk.org/gitweb?p=VTK.git;a=blob;f=Filters/Verdict/Testing/Python/MeshQuality.py

property quadrangles_ids

Get the array of ids of qudrangle shaped faces

Returns

Return type

ndarray

quick_save(filename=None)[source]

Saves the current mesh instance in a VTK file.

It is mainly for debugging purpose.

Parameters

filename (str) – If None, the file is automatically saved under the name quick_save.vtp. If the name given does not have a .vtp extension, the latter is appended automatically.

remove_degenerated_faces(rtol=1e-05)[source]

Removes tiny triangles from the mesh.

Tiny triangles are those whose area is lower than the mean triangle area in the mesh times the relative tolerance given.

Parameters

rtol (float, optional) – Positive relative tolerance

remove_unused_vertices()[source]

Removes unused vertices in the mesh.

Those are vertices that are not used by any face connectivity.

rotate(angles)[source]

Rotates the mesh in 3D giving the 3 rotation angles that are defined around fixed axes.

Parameters

angles (array_like) – The 3 angles of the 3D rotation (rad)

Returns

The (3x3) rotation matrix that has been applied to rotate the mesh

Return type

ndarray

rotate_matrix(rotmat)[source]
rotate_x(thetax)[source]

Rotates the mesh around Ox axis.

Parameters

thetax (float) – Angle (rad)

Returns

The (3x3) rotation matrix that has been applied to rotate the mesh

Return type

ndarray

rotate_y(thetay)[source]

Rotates the mesh around Oy axis.

Parameters

thetay (float) – Angle (rad)

Returns

The (3x3) rotation matrix that has been applied to rotate the mesh

Return type

ndarray

rotate_z(thetaz)[source]

Rotates the mesh around Oz axis.

Parameters

thetaz (float) – Angle (rad)

Returns

The (3x3) rotation matrix that has been applied to rotate the mesh

Return type

ndarray

scale(alpha)[source]

Scales the mesh.

Parameters

alpha (float) – A positive scaling factor

scalex(alpha)[source]

Scales the mesh along the x axis.

Parameters

alpha (float) – A positive scaling factor

scaley(alpha)[source]

Scales the mesh along the y axis.

Parameters

alpha (float) – A positive scaling factor

scalez(alpha)[source]

Scales the mesh along the z axis.

Parameters

alpha (float) – A positive scaling factor

show()[source]

Shows the mesh in the meshmagick viewer

property squared_axis_aligned_bbox

Get a squared axis aligned bounding box of the mesh.

Returns

(xmin, xmax, ymin, ymax, zmin, zmax)

Return type

tuple

Note

This method differs from axis_aligned_bbox() by the fact that the bounding box that is returned is squared but have the same center as the AABB

symmetrize(plane)[source]

Symmetrize the mesh with respect to a plane.

Parameters

plane (Plane) – The plane of symmetry

translate(t)[source]

Translates the mesh in 3D giving the 3 distances along coordinate axes.

Parameters

t (array_like) – translation vector

translate_x(tx)[source]

Translates the mesh along the Ox axis.

Parameters

tx (float) – Distance

translate_y(ty)[source]

Translates the mesh along the Oy axis.

Parameters

ty (float) – Distance

translate_z(tz)[source]

Translates the mesh along the Oz axis.

Parameters

tz (float) – Distance

property triangles_ids

Get the array of ids of triangle shaped faces

Returns

Return type

ndarray

triangulate_quadrangles()[source]

Triangulates every quadrangles of the mesh by simple spliting.

Each quadrangle gives two triangles.

Note

No checking is made on the triangle quality is done.

property verbose

Get verbosity

Returns

Return type

bool

verbose_off()[source]

Set the verbosity level of the instance to off.

verbose_on()[source]

Set the verbosity level of the instance to on.

property vertices

Get the vertices array coordinate of the mesh

Returns

Return type

np.ndarray

property vf

Get the vertex / faces connectivity dictionary.

Returns

Return type

dict

property volume

Get the mesh enclosed volume

Returns

The mesh volume

Return type

float

property vv

Get the vertex / vertex connectivity dictionary.

Returns

Return type

dict

class meshmagick.mesh.Plane(normal=(0.0, 0.0, 1.0), scalar=0.0, name=None)[source]

Bases: object

Class to handle plane geometry.

A plane is represented by the equation \(\vec{n}.\vec{x} = c\) where \(\vec{n}\) is the plane’s normal, \(\vec{x}\) a point in the space and \(c\) a scalar parameter being the signed distance between the reference frame origin and the its otrhogonal projection on the plane.

Parameters
  • normal (array_like) – 3 component vector of the plane normal

  • scalar (float) – The scalar parameter of the plane

property c

Get the plane’s scalar parameter

coord_in_plane(points)[source]

Return the coordinates of points in the frame of the plane

Parameters

points (ndarray) – Array of points coordinates

Returns

output – Array of points coordinates in the frame of the plane

Return type

ndarray

flip_normal()[source]

Flips the Normal of the plane

get_edge_intersection(p0, p1)[source]

Returns the coordinates of the intersection point between the plane and the edge P0P1.

Parameters
  • p0 (ndarray) – Coordinates of point p0

  • p1 (ndarray) – Coordinates of point P1

Returns

I – Coordinates of intersection point

Return type

ndarray

get_normal_orientation_wrt_z()[source]

Returns the angles theta_x and theta_y giving the orientation of the plane normal

get_origin()[source]

Get the coordinates of the plane’s origin

get_point_dist_wrt_plane(points)[source]

Return the orthogonal distance of points with respect to the plane

Parameters

points (ndarray) – Array of points coordinates

Returns

dist – Array of distances of points with respect to the plane

Return type

ndarray

property normal

Get the plane’s normal

orthogonal_projection_on_plane(points)[source]

Returns the coordinates of the orthogonal projection of points

Parameters

points (ndarray) – Coordinates of the points to be projected

Returns

projected_points – Coordinates of the projection points

Return type

ndarray

rotate_normal(theta_x, theta_y)[source]

Rotates the current plane normal by fixed angles theta_x and theta_y.

Parameters
  • theta_x (float) – Angle of rotation around Ox (rad)

  • theta_y (float) – Angle of rotation around Oy (rad)

set_normal_from_angles(theta_x, theta_y)[source]

Set the normal orientation given angles theta_x and theta_y.

Parameters
  • theta_x (float) – Angle around Ox (rad)

  • theta_y (float) – Angle around Oy (rad)

set_plane_parameters(scalar, theta_x, theta_y)[source]

Updates the plane parameters (normal and scalar parameter) given scalar and angles.

Parameters
  • scalar (float) – Plane scalar parameter (m)

  • theta_x (float) – Normal angle around Ox (rad)

  • theta_y (float) – Normal angle around Oy (rad)