tidy3d.TriangleMesh#

class TriangleMesh[source]#

Bases: Geometry, ABC

Custom surface geometry given by a triangle mesh, as in the STL file format.

Parameters:
  • attrs (dict = {}) – Dictionary storing arbitrary metadata for a Tidy3D object. This dictionary can be freely used by the user for storing data without affecting the operation of Tidy3D as it is not used internally. Note that, unlike regular Tidy3D fields, attrs are mutable. For example, the following is allowed for setting an attr obj.attrs['foo'] = bar. Also note that Tidy3D` will raise a TypeError if attrs contain objects that can not be serialized. One can check if attrs are serializable by calling obj.json().

  • mesh_dataset (Optional[TriangleMeshDataset]) – Surface mesh data.

Example

>>> vertices = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]])
>>> faces = np.array([[1, 2, 3], [0, 3, 2], [0, 1, 3], [0, 2, 1]])
>>> stl_geom = TriangleMesh.from_vertices_faces(vertices, faces)

Attributes

bounds

Returns bounding box min and max coordinates.

triangles

The triangles of the surface mesh as an np.ndarray.

trimesh

A trimesh.Trimesh object representing the custom surface mesh geometry.

attrs

Methods

from_stl(filename[, scale, origin, solid_index])

Load a TriangleMesh directly from an STL file.

from_triangles(triangles)

Create a TriangleMesh from a numpy array containing the triangles of a surface mesh.

from_trimesh(mesh)

Create a TriangleMesh from a trimesh.Trimesh object.

from_vertices_faces(vertices, faces)

Create a TriangleMesh from numpy arrays containing the data of a surface mesh.

inside(x, y, z)

For input arrays x, y, z of arbitrary but identical shape, return an array with the same shape which is True for every point in zip(x, y, z) that is inside the volume of the Geometry, and False otherwise.

intersections_plane([x, y, z])

Returns list of shapely geometries at plane specified by one non-None value of x,y,z.

intersections_tilted_plane(normal, origin, to_2D)

Return a list of shapely geometries at the plane specified by normal and origin.

plot([x, y, z, ax])

Plot geometry cross section at single (x,y,z) coordinate.

Inherited Common Usage

mesh_dataset#
classmethod from_stl(filename, scale=1.0, origin=(0, 0, 0), solid_index=None, **kwargs)[source]#

Load a TriangleMesh directly from an STL file. The solid_index parameter can be used to select a single solid from the file. Otherwise, if the file contains a single solid, it will be loaded as a TriangleMesh; if the file contains multiple solids, they will all be loaded as a GeometryGroup.

Parameters:
  • filename (str) – The name of the STL file containing the surface geometry mesh data.

  • scale (float = 1.0) – The length scale for the loaded geometry (um). For example, a scale of 10.0 means that a vertex (1, 0, 0) will be placed at x = 10 um.

  • origin (Tuple[float, float, float] = (0, 0, 0)) – The origin of the loaded geometry, in units of scale. Translates from (0, 0, 0) to this point after applying the scaling.

  • solid_index (int = None) – If set, read a single solid with this index from the file.

Returns:

The geometry or geometry group from the file.

Return type:

Union[TriangleMesh, GeometryGroup]

classmethod from_trimesh(mesh)[source]#

Create a TriangleMesh from a trimesh.Trimesh object.

Parameters:

trimesh (trimesh.Trimesh) – The Trimesh object containing the surface geometry mesh data.

Returns:

The custom surface mesh geometry given by the trimesh.Trimesh provided.

Return type:

TriangleMesh

classmethod from_triangles(triangles)[source]#

Create a TriangleMesh from a numpy array containing the triangles of a surface mesh.

Parameters:

triangles (np.ndarray) – A numpy array of shape (N, 3, 3) storing the triangles of the surface mesh. The first index labels the triangle, the second index labels the vertex within a given triangle, and the third index is the coordinate (x, y, or z).

Returns:

The custom surface mesh geometry given by the triangles provided.

Return type:

TriangleMesh

classmethod from_vertices_faces(vertices, faces)[source]#

Create a TriangleMesh from numpy arrays containing the data of a surface mesh. The first array contains the vertices, and the second array contains faces formed from triples of the vertices.

Parameters:
  • vertices (np.ndarray) – A numpy array of shape (N, 3) storing the vertices of the surface mesh. The first index labels the vertex, and the second index is the coordinate (x, y, or z).

  • faces (np.ndarray) – A numpy array of shape (M, 3) storing the indices of the vertices of each face in the surface mesh. The first index labels the face, and the second index labels the vertex index within the vertices array.

Returns:

The custom surface mesh geometry given by the vertices and faces provided.

Return type:

TriangleMesh

property trimesh#

A trimesh.Trimesh object representing the custom surface mesh geometry.

property triangles#

The triangles of the surface mesh as an np.ndarray.

property bounds#

Returns bounding box min and max coordinates.

Returns:

Min and max bounds packaged as (minx, miny, minz), (maxx, maxy, maxz).

Return type:

Tuple[float, float, float], Tuple[float, float float]

intersections_tilted_plane(normal, origin, to_2D)[source]#

Return a list of shapely geometries at the plane specified by normal and origin.

Parameters:
  • normal (Coordinate) – Vector defining the normal direction to the plane.

  • origin (Coordinate) – Vector defining the plane origin.

  • to_2D (MatrixReal4x4) – Transformation matrix to apply to resulting shapes.

Returns:

List of 2D shapes that intersect plane. For more details refer to Shapely’s Documentation.

Return type:

List[shapely.geometry.base.BaseGeometry]

intersections_plane(x=None, y=None, z=None)[source]#

Returns list of shapely geometries at plane specified by one non-None value of x,y,z.

Parameters:
  • x (float = None) – Position of plane in x direction, only one of x,y,z can be specified to define plane.

  • y (float = None) – Position of plane in y direction, only one of x,y,z can be specified to define plane.

  • z (float = None) – Position of plane in z direction, only one of x,y,z can be specified to define plane.

Returns:

List of 2D shapes that intersect plane. For more details refer to Shapely’s Documentaton.

Return type:

List[shapely.geometry.base.BaseGeometry]

inside(x, y, z)[source]#

For input arrays x, y, z of arbitrary but identical shape, return an array with the same shape which is True for every point in zip(x, y, z) that is inside the volume of the Geometry, and False otherwise.

Parameters:
  • x (np.ndarray[float]) – Array of point positions in x direction.

  • y (np.ndarray[float]) – Array of point positions in y direction.

  • z (np.ndarray[float]) – Array of point positions in z direction.

Returns:

True for every point that is inside the geometry.

Return type:

np.ndarray[bool]

plot(x=None, y=None, z=None, ax=None, **patch_kwargs)[source]#

Plot geometry cross section at single (x,y,z) coordinate.

Parameters:
  • x (float = None) – Position of plane in x direction, only one of x,y,z can be specified to define plane.

  • y (float = None) – Position of plane in y direction, only one of x,y,z can be specified to define plane.

  • z (float = None) – Position of plane in z direction, only one of x,y,z can be specified to define plane.

  • ax (matplotlib.axes._subplots.Axes = None) – Matplotlib axes to plot on, if not specified, one is created.

  • **patch_kwargs – Optional keyword arguments passed to the matplotlib patch plotting of structure. For details on accepted values, refer to Matplotlib’s documentation.

Returns:

The supplied or created matplotlib axes.

Return type:

matplotlib.axes._subplots.Axes

__hash__()#

Hash method.