Notebook source code: notebooks/how_to/11_hierarchical_mesh.ipynb
Run it yourself on binder Binder badge

How to create a hierarchical mesh and what can be done with it?#

 In [ ]:
import geomstats.backend as gs

from geomfum.dataset import NotebooksDataset
from geomfum.shape import TriangleMesh
from geomfum.shape.hierarchical import HierarchicalMesh

Load mesh.

 In [2]:
dataset = NotebooksDataset()

mesh = TriangleMesh.from_file(dataset.get_filename("cat-00"))

mesh.n_vertices, mesh.n_faces
INFO: Data has already been downloaded... using cached file ('C:\Users\giuli\.geomfum\data\cat-00.off').
 Out [2]:
(7207, 14410)

Create a hierarchical mesh.

 In [4]:
hmesh = HierarchicalMesh.from_registry(mesh, min_n_samples=1000)
C:\Users\giuli\OneDrive\Research\geomfum_proj\geomfum\geomfum\wrap\pyrmt.py:69: UserWarning: The given NumPy array is not writable, and PyTorch does not support non-writable tensors. This means writing to this tensor will result in undefined behavior. You may want to copy the array to protect its data or make it writable before converting it to a tensor. This type of warning will be suppressed for the rest of this program. (Triggered internally at C:\actions-runner\_work\pytorch\pytorch\pytorch\torch\csrc\utils\tensor_numpy.cpp:209.)
  return TriangleMesh(gs.asarray(rlow.vertices), gs.asarray(rlow.triangles))

This structure contains two objects, a low-resolution mesh (hmesh.low) and a high-resolution object (hmesh.high, which is mesh).

Scalars from the low-resolution mesh can be transferred to the high-resolution mesh via scalar_low_high.

 In [ ]:
low_scalar = gs.random.uniform(size=hmesh.low.n_vertices)

high_scalar = hmesh.scalar_low_high(low_scalar)

low_scalar.shape, high_scalar.shape
(torch.Size([1004]), (7207,))

In particular, this can be used to extend a low-resolution basis (see section 3.3. of ReMatching).

 In [10]:
hmesh.low.laplacian.find_spectrum(spectrum_size=10, set_as_basis=True)

hmesh.extend_basis(set_as_basis=True)

hmesh.low.basis, hmesh.high.basis
C:\Users\giuli\OneDrive\Research\geomfum_proj\geomfum\geomfum\_backend\pytorch\sparse.py:22: UserWarning: Sparse CSC tensor support is in beta state. If you miss a functionality in the sparse tensor support, please submit a feature request to https://github.com/pytorch/pytorch/issues. (Triggered internally at C:\actions-runner\_work\pytorch\pytorch\pytorch\aten\src\ATen\SparseCsrTensorImpl.cpp:55.)
  return _torch.sparse_csc_tensor(ccol_indices, row_indices, values, size=array.shape)
 Out [10]:
(<geomfum.basis.LaplaceEigenBasis at 0x1fc468b9350>,
 <geomfum.basis.EigenBasis at 0x1fc4679eb90>)

Further reading#