emsarray.operations.triangulate

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 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 indices.

cell_indices is a numpy list of length T. Each entry indicates which polygon from the dataset a triangle is a part of.

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_indexes = 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_indexes = 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_indexes]
mesh.show()