emsarray.operations.triangulate#
Operations for making a triangular mesh out of the polygons of a dataset.
Examples
Using holoviews. Try this in an IPython notebook for nice visualisations:
import emsarray
import holoviews as hv
from holoviews import opts
hv.extension('bokeh')
# Triangulate the dataset
dataset = emsarray.tuorial.open_dataset("austen")
triangulation = dataset.ems.make_triangulation(dataset)
# This takes a while to render
mesh = hv.TriMesh((triangulation.triangles, triangulation.vertices))
mesh
Using trimesh. This should pop up a new window to display the output:
import emsarray
import numpy
import trimesh
dataset = emsarray.tutorial.open_dataset("gbr4")
triangulation = dataset.ems.make_triangulation(dataset)
# Trimesh expects 3D vertices.
vertices = numpy.c_[triangulation.vertices, numpy.zeros(len(triangulation.vertices))]
mesh = trimesh.Trimesh(vertices=vertices, faces=triangulation.triangles)
mesh.invert() # Trimesh grids expect the opposite winding order
depth = 1 - (dataset.data_vars["Mesh2_depth"].values / -200)
depth_colour = numpy.c_[depth, depth, depth, numpy.ones_like(depth)] * 255
mesh.visual.face_colors = depth_colour[cell_indexes]
mesh.show()
- class emsarray.operations.triangulate.Triangulation(*, vertices: numpy.ndarray, triangles: numpy.ndarray, face_indexes: numpy.ndarray, face_grid_kind: GridKind, vertex_grid_kind: GridKind | None = None)#
- emsarray.operations.triangulate.triangulate_dataset(dataset)#
Triangulate the polygon cells of a dataset in a naive way. Users should prefer to use
Convention.triangulate().- Parameters:
dataset – The dataset to triangulate
- Returns:
tupleofvertices,triangles, and cell_indexes – A tuple of three numpy arrays is returned, containing vertices, triangles, and cell indexes respectively.vertices is a numpy array of shape (V, 2) where V is the number of unique vertices in the dataset. The vertex coordinates are in (x, y) or (lon, lat) order.
triangles is a numpy array of shape (T, 3) where T is the number of triangles in the dataset. Each triangle is a set of three vertex indexes.
cell_indexes is a numpy list of length T. Each entry indicates which polygon from the dataset a triangle is a part of.
- emsarray.operations.triangulate.find_unique_vertices(polygons)#
Find all the unique coordinates in a collection of polygons.
- Parameters:
polygons (
numpy.ndarrayofshapely.Polygon) – A numpy array of polygons.- Returns:
numpy.ndarrayofshapely.Point– An array of all of the unique vertex coordinates in all the polygons, in some arbitrary order.
- emsarray.operations.triangulate.polygons_to_vertex_indexes(polygons, vertices)#
For a set of polygons and a set of all vertices, find the index into the vertex array of each vertex of the polygons.
- Parameters:
polygons (
numpy.ndarrayofshapely.Polygon) – A numpy array of polygons.vertices (
numpy.ndarrayofshapely.Point) – A numpy array of all vertices of these polygons.
- Returns:
numpy.ndarray– A numpy.ndarray of shape (# polygons, max node count), where # polygons is the length of the polygons array, and max node count is the node count of the largest polygon. For each vertext of each polygon, this specifies the index of that vertex coordinate in the vertices array.
- emsarray.operations.triangulate.triangulate(vertices, polygons, polygon_vertex_indexes)#
Triangulate a set of polygons.
- Parameters:
vertices (
numpy.ndarrayofshapely.Point) – All the unique vertices of all the polygonspolygons (
numpy.ndarrayofshapely.Polygon) – All the polygons to triangulatepolygon_vertex_indexes (
numpy.ndarray) – An array of arrays (or a two dimensional array) mapping vertices for each polygon to the correct index in the vertices array.
- Returns:
tupleofvertices,triangles, and cell_indexes – A tuple of three numpy arrays is returned, containing vertices, triangles, and cell indexes respectively.vertices is a numpy array of shape (V, 2) where V is the number of unique vertices in the dataset. The vertex coordinates are in (x, y) or (lon, lat) order.
triangles is a numpy array of shape (T, 3) where T is the number of triangles in the dataset. Each triangle is a set of three vertex indexes.
cell_indexes is a numpy list of length T. Each entry is the index of the polygon in the polygons array the triangle is a part of.