Add access for vtkToTensorMesh function
- from pr 140
- commits from: banesullivan
This patch makes available a feature that was tucked away in the `vtkModule` enabling users to back convert `vtkRectilinearGrid`s or `vtki.UnstructuredGrid`s to `TensorMesh` objects. This new feature is the `discretize.TensorMesh.vtkToTensorMesh()` function.
These changes are motivated by a need to easily and interactively create meshes in `vtki` then send those meshes back to `discretize` for use in `SimPEG`.
Example
The following example allows a user to repeatedly tweak a mesh with interactive visualization before deciding on a final mesh structure before sending that mesh to `discretize`:
**Note: the needs to be done in IPython**
Necessary imports
py
import vtki
from vtki import examples
import discretize
import numpy as np
Create a background plotting window that can be interacted with throughout a Jupyter notebook
py
Create a plotting window
p = vtki.BackgroundPlotter()
p.show_grid()
Download a sample topography dataset using `vtki` to surround with a mesh
py
Get a sample topo surface from vtki
Note: this requires vtki>=0.17.1
topo = examples.download_st_helens().warp_by_scalar()
p.add_mesh(topo, name='topo')
Repeatedly change this cell and rerun to decide on your mesh discretization
py
Create the mesh interactively
tweak these parameters and rerun this cell until satisfied
b = topo.bounds
xcoords = np.linspace(b[0], b[1], 50)
ycoords = np.linspace(b[2], b[3], 50)
zcoords = np.linspace(b[4]-5000, b[5], 50)
mesh = vtki.RectilinearGrid(xcoords, ycoords, zcoords)
p.add_mesh(mesh, name='mesh', opacity=0.5, show_edges=True)
output the mesh
mesh
<table><tr><th>RectilinearGrid</th><th>Information</th></tr><tr><td>N Cells</td><td>117649</td></tr><tr><td>N Points</td><td>125000</td></tr><tr><td>X Bounds</td><td>5.579e+05, 5.677e+05</td></tr><tr><td>Y Bounds</td><td>5.108e+06, 5.122e+06</td></tr><tr><td>Z Bounds</td><td>-3.636e+03, 3.225e+03</td></tr><tr><td>Volume</td><td>9.381e+11</td></tr><tr><td>N Scalars</td><td>0</td></tr></table>
Finally, send the mesh to `discretize` for use in other processing routines
py
Once satisfied, convert to discretize:
dmesh, _ = discretize.TensorMesh.vtkToTensorMesh(mesh)
dmesh
<discretize.TensorMesh.TensorMesh at 0xb2a555588>
And a GIF to demo
![ezgif com-video-to-gif](https://user-images.githubusercontent.com/22067021/53910336-d0e78900-4010-11e9-8cbe-7c2a06676bd7.gif)