TriMesh — HoloViews v1.18.3 (2024)

Download this notebook from GitHub (right-click to download).

Title
TriMesh Element
Dependencies
Bokeh
Backends
Bokeh
Matplotlib
import numpy as npimport holoviews as hvfrom holoviews import optsfrom scipy.spatial import Delaunayhv.extension('bokeh')

TriMesh — HoloViews v1.18.3 (1)TriMesh — HoloViews v1.18.3 (2)

A TriMesh represents a mesh of triangles represented as the simplexes and vertices. The simplexes represent the indices into the vertex data, made up of three indices per triangle. The mesh therefore follows a datastructure very similar to a graph, with the abstract connectivity between nodes stored on the TriMesh element itself, the node or vertex positions stored on a Nodes element and the concrete EdgePaths making up each triangle generated when required by accessing the edgepaths attribute.

Unlike a Graph each simplex is represented as the node indices of the three corners of each triangle rather than the usual source and target node.

We will begin with a simple random mesh, generated by sampling some random integers and then applying Delaunay triangulation, which is available in SciPy. We can then construct the TriMesh by passing it the simplexes and the vertices (or nodes).

n_verts = 100pts = np.random.randint(1, n_verts, (n_verts, 2))tris = Delaunay(pts)trimesh = hv.TriMesh((tris.simplices, pts))trimesh

To make this easier TriMesh also provides a convenient from_vertices method, which will apply the Delaunay triangulation and construct the TriMesh for us:

hv.TriMesh.from_vertices(np.random.randn(100, 2))

Just like the Graph element we can access the Nodes and EdgePaths via the .nodes and .edgepaths attributes respectively.

trimesh.nodes + trimesh.edgepaths

Now let’s make a slightly more interesting example by generating a more complex geometry. Here we will compute a geometry, then apply Delaunay triangulation again and finally apply a mask to drop nodes in the center.

# First create the x and y coordinates of the points.n_angles = 36n_radii = 8min_radius = 0.25radii = np.linspace(min_radius, 0.95, n_radii)angles = np.linspace(0, 2*np.pi, n_angles, endpoint=False)angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)angles[:, 1::2] += np.pi/n_anglesx = (radii*np.cos(angles)).flatten()y = (radii*np.sin(angles)).flatten()z = (np.cos(radii)*np.cos(angles*3.0)).flatten()nodes = np.column_stack([x, y, z])# Apply Delaunay triangulationdelaunay = Delaunay(np.column_stack([x, y]))# Mask off unwanted triangles.xmid = x[delaunay.simplices].mean(axis=1)ymid = y[delaunay.simplices].mean(axis=1)mask = np.where(xmid*xmid + ymid*ymid < min_radius*min_radius, 1, 0)simplices = delaunay.simplices[np.logical_not(mask)]

Once again we can simply supply the simplices and nodes to the TriMesh.

nodes = hv.Points(nodes, vdims='z')hv.TriMesh((simplices, nodes))

We can also do something more interesting, e.g. by adding a value dimension to the vertices and coloring the edges by the vertex averaged value using the edge_color plot option:

trimesh = hv.TriMesh((simplices, nodes))trimesh.opts( opts.TriMesh(cmap='viridis', edge_color='z', filled=True, height=400, inspection_policy='edges', tools=['hover'], width=400))

For full documentation and the available style and plot options, use hv.help(hv.TriMesh).

This web page was generated from a Jupyter notebook and not all interactivity will work on this website. Right click to download and run locally for full Python-backed interactivity.

Download this notebook from GitHub (right-click to download).

TriMesh — HoloViews v1.18.3 (2024)

FAQs

What is a TriMesh? ›

A TriMesh represents a mesh of triangles represented as the simplexes and vertices. The simplexes represent the indices into the vertex data, made up of three indices per triangle.

What is Python trimesh? ›

Trimesh is a pure Python 3.7+ library for loading and using triangular meshes with an emphasis on watertight surfaces. The goal of the library is to provide a full featured and well tested Trimesh object which allows for easy manipulation and analysis, in the style of the Polygon object in the Shapely library.

How to install trimesh in python? ›

For the easiest install with just numpy, pip can generally install trimesh cleanly on Windows, Linux, and OSX:
  1. pip install trimesh. ...
  2. # this will install all soft dependencies available on your current platform conda install -c conda-forge trimesh. ...
  3. pip install trimesh[easy]
Jul 26, 2019

Top Articles
Latest Posts
Article information

Author: Dr. Pierre Goyette

Last Updated:

Views: 6555

Rating: 5 / 5 (70 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Dr. Pierre Goyette

Birthday: 1998-01-29

Address: Apt. 611 3357 Yong Plain, West Audra, IL 70053

Phone: +5819954278378

Job: Construction Director

Hobby: Embroidery, Creative writing, Shopping, Driving, Stand-up comedy, Coffee roasting, Scrapbooking

Introduction: My name is Dr. Pierre Goyette, I am a enchanting, powerful, jolly, rich, graceful, colorful, zany person who loves writing and wants to share my knowledge and understanding with you.