emsarray.operations.triangulate#

Operations for making a triangular mesh out of the polygons of a dataset.

triangulate_dataset(dataset)#

Triangulate the polygon cells of a dataset

A mesh can be constructed from this triangulation, for example using Holoviews TriMesh or trimesh.Trimesh.

Parameters:

dataset – The dataset to triangulate

Returns:

tuple of vertices, triangles, and cell indices. – A tuple of three lists is returned, containing vertices, triangles, and cell indices respectively.

Each vertex is a tuple of (x, y) or (lon, lat) coordinates.

Each triangle is a tuple of three integers, indicating which vertices make up the triangle.

The cell indices tie the triangles to the original cell polygon, allowing you to plot data on the triangle mesh.

Examples

Using holoviews. Try this in an IPython notebook for nice visualisations:

import emsarray
import holoviews as hv
from emsarray.operations import triangulate_dataset
from holoviews import opts
hv.extension('bokeh')

# Triangulate the dataset
dataset = emsarray.tuorial.open_dataset("austen")
vertices, triangles, cell_indices = triangulate_dataset(dataset)

# This takes a while to render
mesh = hv.TriMesh((triangles, vertices))
mesh

Using trimesh. This should pop up a new window to display the output:

import emsarray
import numpy
import trimesh
from emsarray.operations import triangulate_dataset

dataset = emsarray.tutorial.open_dataset("gbr4")
vertices, triangles, cell_indices = triangulate_dataset(dataset)
# Trimesh expects 3D vertices.
vertices = numpy.c_[vertices, numpy.zeros(len(vertices))]
mesh = trimesh.Trimesh(vertices=vertices, faces=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_indices]
mesh.show()